#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

@@ -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));
}
}