2 * New Mail Notifier plugin script
5 * @author Aleksander Machniak <alec@alec.pl>
8 if (window.rcmail && rcmail.env.task == 'mail') {
9 rcmail.addEventListener('plugin.newmail_notifier', newmail_notifier_run);
10 rcmail.addEventListener('actionbefore', newmail_notifier_stop);
11 rcmail.addEventListener('init', function() {
12 // bind to messages list select event, so favicon will be reverted on message preview too
13 if (rcmail.message_list)
14 rcmail.message_list.addEventListener('select', newmail_notifier_stop);
18 // Executes notification methods
19 function newmail_notifier_run(prop)
22 newmail_notifier_basic();
24 newmail_notifier_sound();
26 newmail_notifier_desktop(rcmail.gettext('body', 'newmail_notifier'));
30 function newmail_notifier_stop(prop)
32 // revert original favicon
33 if (rcmail.env.favicon_href && (!prop || prop.action != 'check-recent')) {
34 $('<link rel="shortcut icon" href="'+rcmail.env.favicon_href+'"/>').replaceAll('link[rel="shortcut icon"]');
35 rcmail.env.favicon_href = null;
39 // Basic notification: window.focus and favicon change
40 function newmail_notifier_basic()
42 var w = rcmail.is_framed() ? window.parent : window;
46 // we cannot simply change a href attribute, we must to replace the link element (at least in FF)
47 var link = $('<link rel="shortcut icon" href="plugins/newmail_notifier/favicon.ico"/>'),
48 oldlink = $('link[rel="shortcut icon"]', w.document);
50 rcmail.env.favicon_href = oldlink.attr('href');
51 link.replaceAll(oldlink);
55 function newmail_notifier_sound()
57 var elem, src = 'plugins/newmail_notifier/sound.wav';
61 elem = $('<audio src="' + src + '" />');
66 elem = $('<embed id="sound" src="' + src + '" hidden=true autostart=true loop=false />');
67 elem.appendTo($('body'));
68 window.setTimeout("$('#sound').remove()", 5000);
72 // Desktop notification (need Chrome or Firefox with a plugin)
73 function newmail_notifier_desktop(body)
75 var dn = window.webkitNotifications;
77 if (dn && !dn.checkPermission()) {
78 if (rcmail.newmail_popup)
79 rcmail.newmail_popup.cancel();
80 var popup = window.webkitNotifications.createNotification('plugins/newmail_notifier/mail.png',
81 rcmail.gettext('title', 'newmail_notifier'), body);
82 popup.onclick = function() {
86 setTimeout(function() { popup.cancel(); }, 10000); // close after 10 seconds
87 rcmail.newmail_popup = popup;
94 function newmail_notifier_test_desktop()
96 var dn = window.webkitNotifications,
97 txt = rcmail.gettext('testbody', 'newmail_notifier');
100 if (!dn.checkPermission())
101 newmail_notifier_desktop(txt);
103 dn.requestPermission(function() {
104 if (!newmail_notifier_desktop(txt))
105 rcmail.display_message(rcmail.gettext('desktopdisabled', 'newmail_notifier'), 'error');
109 rcmail.display_message(rcmail.gettext('desktopunsupported', 'newmail_notifier'), 'error');
112 function newmail_notifier_test_basic()
114 newmail_notifier_basic();
117 function newmail_notifier_test_sound()
119 newmail_notifier_sound();