]> git.donarmstrong.com Git - roundcube.git/blob - plugins/newmail_notifier/newmail_notifier.js
Imported Upstream version 0.7
[roundcube.git] / plugins / newmail_notifier / newmail_notifier.js
1 /**
2  * New Mail Notifier plugin script
3  *
4  * @version 0.3
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     if (prop.desktop)
26         newmail_notifier_desktop(rcmail.gettext('body', 'newmail_notifier'));
27 }
28
29 // Stops notification
30 function newmail_notifier_stop(prop)
31 {
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;
36     }
37 }
38
39 // Basic notification: window.focus and favicon change
40 function newmail_notifier_basic()
41 {
42     var w = rcmail.is_framed() ? window.parent : window;
43
44     w.focus();
45
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);
49
50     rcmail.env.favicon_href = oldlink.attr('href');
51     link.replaceAll(oldlink);
52 }
53
54 // Sound notification
55 function newmail_notifier_sound()
56 {
57     var elem, src = 'plugins/newmail_notifier/sound.wav';
58
59     // HTML5
60     try {
61         elem = $('<audio src="' + src + '" />');
62         elem.get(0).play();
63     }
64     // old method
65     catch (e) {
66         elem = $('<embed id="sound" src="' + src + '" hidden=true autostart=true loop=false />');
67         elem.appendTo($('body'));
68         window.setTimeout("$('#sound').remove()", 5000);
69     }
70 }
71
72 // Desktop notification (need Chrome or Firefox with a plugin)
73 function newmail_notifier_desktop(body)
74 {
75     var dn = window.webkitNotifications;
76
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() {
83             this.cancel();
84         }
85         popup.show();
86         setTimeout(function() { popup.cancel(); }, 10000); // close after 10 seconds
87         rcmail.newmail_popup = popup;
88         return true;
89     }
90
91     return false;
92 }
93
94 function newmail_notifier_test_desktop()
95 {
96     var dn = window.webkitNotifications,
97         txt = rcmail.gettext('testbody', 'newmail_notifier');
98
99     if (dn) {
100         if (!dn.checkPermission())
101             newmail_notifier_desktop(txt);
102         else
103             dn.requestPermission(function() {
104                 if (!newmail_notifier_desktop(txt))
105                     rcmail.display_message(rcmail.gettext('desktopdisabled', 'newmail_notifier'), 'error');
106             });
107     }
108     else
109         rcmail.display_message(rcmail.gettext('desktopunsupported', 'newmail_notifier'), 'error');
110 }
111
112 function newmail_notifier_test_basic()
113 {
114     newmail_notifier_basic();
115 }
116
117 function newmail_notifier_test_sound()
118 {
119     newmail_notifier_sound();
120 }