]> git.donarmstrong.com Git - roundcube.git/blob - plugins/newmail_notifier/newmail_notifier.js
6afd66aee83fc689c5d51e5dd791a224c8707072
[roundcube.git] / plugins / newmail_notifier / newmail_notifier.js
1 /**
2  * New Mail Notifier plugin script
3  *
4  * @version 0.2
5  * @author Aleksander Machniak <alec@alec.pl>
6  */
7
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);
15     });
16 }
17
18 // Executes notification methods
19 function newmail_notifier_run(prop)
20 {
21     if (prop.basic)
22         newmail_notifier_basic();
23     if (prop.sound)
24         newmail_notifier_sound();
25 }
26
27 // Stops notification
28 function newmail_notifier_stop(prop)
29 {
30     // revert original favicon
31     if (rcmail.env.favicon_href && (!prop || prop.action != 'check-recent')) {
32         $('<link rel="shortcut icon" href="'+rcmail.env.favicon_href+'"/>').replaceAll('link[rel="shortcut icon"]');
33         rcmail.env.favicon_href = null;
34     }
35 }
36
37 // Basic notification: window.focus and favicon change
38 function newmail_notifier_basic()
39 {
40     window.focus();
41
42     // we cannot simply change a href attribute, we must to replace the link element (at least in FF)
43     var link = $('<link rel="shortcut icon" href="plugins/newmail_notifier/favicon.ico"/>'),
44         oldlink = $('link[rel="shortcut icon"]');
45
46     rcmail.env.favicon_href = oldlink.attr('href');
47     link.replaceAll(oldlink);
48 }
49
50 // Sound notification
51 function newmail_notifier_sound()
52 {
53     var elem, src = 'plugins/newmail_notifier/sound.wav';
54
55     // HTML5
56     try {
57         elem = $('<audio src="' + src + '" />');
58         elem.get(0).play();
59     }
60     // old method
61     catch (e) {
62         elem = $('<embed id="sound" src="' + src + '" hidden=true autostart=true loop=false />');
63         elem.appendTo($('body'));
64         window.setTimeout("$('#sound').remove()", 5000);
65     }
66 }