1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-03 18:25:35 +00:00

#4739 Webkit notifications for downloading/seeding complete events

This commit is contained in:
Mitchell Livingston 2012-05-19 21:37:29 +00:00
parent 0c4bcc52ae
commit a484f90975
4 changed files with 69 additions and 0 deletions

View file

@ -33,6 +33,7 @@
<script type="text/javascript" src="./javascript/file-row.js"></script>
<script type="text/javascript" src="./javascript/dialog.js"></script>
<script type="text/javascript" src="./javascript/formatter.js"></script>
<script type="text/javascript" src="./javascript/notifications.js"></script>
<title>Transmission Web Interface</title>
</head>
<body id="transmission_body">
@ -313,6 +314,7 @@
<li id="tipjar">Transmission Tip Jar</li>
<li class="separator"></li>
<li id="statistics">Statistics</li>
<li id="toggle_notifications" style="display: none">Notifcations</li>
<li class="separator"></li>
<li>Total Download Rate
<ul id="footer_download_rate_menu">

View file

@ -0,0 +1,40 @@
var Notifications = {};
$(document).ready(function () {
if (!window.webkitNotifications) {
return;
}
var notificationsEnabled = (window.webkitNotifications.checkPermission() === 0),
toggle = $('#toggle_notifications');
toggle.show();
updateMenuTitle();
$(transmission).bind('downloadComplete seedingComplete', function (event, torrent) {
var title = (event.type == 'downloadComplete' ? 'Download' : 'Seeding') + ' complete',
content = torrent.getName(),
notification;
notification = window.webkitNotifications.createNotification('style/transmission/images/logo.png', title, content);
notification.show();
setTimeout(function () {
notification.cancel();
}, 5000);
});
function updateMenuTitle() {
toggle.html((notificationsEnabled ? 'Disable' : 'Enable') + ' Notifications');
}
Notifications.toggle = function () {
if (window.webkitNotifications.checkPermission() !== 0) {
window.webkitNotifications.requestPermission(function () {
notificationsEnabled = (window.webkitNotifications.checkPermission() === 0);
updateMenuTitle();
});
} else {
notificationsEnabled = !notificationsEnabled;
updateMenuTitle();
}
};
});

View file

@ -119,13 +119,26 @@ Torrent.prototype =
initialize: function(data)
{
this.fields = {};
this.fieldObservers = {};
this.refresh (data);
},
notifyOnFieldChange: function(field, callback) {
this.fieldObservers[field] = this.fieldObservers[field] || [];
this.fieldObservers[field].push(callback);
},
setField: function(o, name, value)
{
var i, observer;
if (o[name] === value)
return false;
if (o == this.fields && this.fieldObservers[name] && this.fieldObservers[name].length) {
for (i=0; observer=this.fieldObservers[name][i]; ++i) {
observer.call(this, value, o[name], name);
}
}
o[name] = value;
return true;
},

View file

@ -680,6 +680,10 @@ Transmission.prototype =
this.setSortDirection(dir);
break;
case 'toggle_notifications':
Notifications && Notifications.toggle();
break;
default:
console.log('unhandled: ' + id);
break;
@ -723,6 +727,16 @@ Transmission.prototype =
// do we need more info for this torrent?
if(!('name' in t.fields) || !('status' in t.fields))
needinfo.push(id);
t.notifyOnFieldChange('status', $.proxy(function (newValue, oldValue) {
if (oldValue === Torrent._StatusDownload && (newValue == Torrent._StatusSeed || newValue == Torrent._StatusSeedWait)) {
$(this).trigger('downloadComplete', [t]);
} else if (oldValue === Torrent._StatusSeed && newValue === Torrent._StatusStopped && t.isFinished()) {
$(this).trigger('seedingComplete', [t]);
} else {
$(this).trigger('statusChange', [t]);
}
}, this));
}
}