Aleksander Machniak
2016-03-17 ca332d58628a3b88f22795d9b34d7d0ad8c1b97a
commit | author | age
48e9c1 1 /**
T 2  * New Mail Notifier plugin script
3  *
4  * @author Aleksander Machniak <alec@alec.pl>
b34d67 5  *
TB 6  * @licstart  The following is the entire license notice for the
7  * JavaScript code in this file.
8  *
1aa581 9  * Copyright (c) 2013-2016, The Roundcube Dev Team
b34d67 10  *
TB 11  * The JavaScript code in this page is free software: you can redistribute it
12  * and/or modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation, either version 3 of
14  * the License, or (at your option) any later version.
15  *
16  * @licend  The above is the entire license notice
17  * for the JavaScript code in this file.
48e9c1 18  */
T 19
20 if (window.rcmail && rcmail.env.task == 'mail') {
8f8bea 21     rcmail.addEventListener('plugin.newmail_notifier', newmail_notifier_run)
AM 22         .addEventListener('actionbefore', newmail_notifier_stop)
23         .addEventListener('init', function() {
24             // bind to messages list select event, so favicon will be reverted on message preview too
25             if (rcmail.message_list)
26                 rcmail.message_list.addEventListener('select', newmail_notifier_stop);
27         });
48e9c1 28 }
T 29
30 // Executes notification methods
31 function newmail_notifier_run(prop)
32 {
33     if (prop.basic)
34         newmail_notifier_basic();
35     if (prop.sound)
36         newmail_notifier_sound();
37     if (prop.desktop)
8f8bea 38         newmail_notifier_desktop(rcmail.get_label('body', 'newmail_notifier'));
48e9c1 39 }
T 40
41 // Stops notification
42 function newmail_notifier_stop(prop)
43 {
44     // revert original favicon
f49e28 45     if (rcmail.env.favicon_href && rcmail.env.favicon_changed && (!prop || prop.action != 'check-recent')) {
48e9c1 46         $('<link rel="shortcut icon" href="'+rcmail.env.favicon_href+'"/>').replaceAll('link[rel="shortcut icon"]');
f49e28 47         rcmail.env.favicon_changed = 0;
48e9c1 48     }
67a525 49
RK 50     // Remove IE icon overlay if we're pinned to Taskbar
51     try {
52         if(window.external.msIsSiteMode()) {
53             window.external.msSiteModeClearIconOverlay();
54         }
55     } catch(e) {}
48e9c1 56 }
T 57
58 // Basic notification: window.focus and favicon change
59 function newmail_notifier_basic()
60 {
217a1f 61     var w = rcmail.is_framed() ? window.parent : window,
AM 62         path = rcmail.assets_path('plugins/newmail_notifier');
48e9c1 63
T 64     w.focus();
65
66     // we cannot simply change a href attribute, we must to replace the link element (at least in FF)
217a1f 67     var link = $('<link rel="shortcut icon">').attr('href', path + '/favicon.ico'),
48e9c1 68         oldlink = $('link[rel="shortcut icon"]', w.document);
T 69
f49e28 70     if (!rcmail.env.favicon_href)
AM 71         rcmail.env.favicon_href = oldlink.attr('href');
72
73     rcmail.env.favicon_changed = 1;
48e9c1 74     link.replaceAll(oldlink);
67a525 75
RK 76     // Add IE icon overlay if we're pinned to Taskbar
77     try {
78         if (window.external.msIsSiteMode()) {
8f8bea 79             window.external.msSiteModeSetIconOverlay(path + '/overlay.ico', rcmail.get_label('title', 'newmail_notifier'));
67a525 80         }
RK 81     } catch(e) {}
48e9c1 82 }
T 83
84 // Sound notification
85 function newmail_notifier_sound()
86 {
217a1f 87     var elem, src = rcmail.assets_path('plugins/newmail_notifier/sound'),
bba13c 88         plugin = navigator.mimeTypes ? navigator.mimeTypes['audio/mp3'] : {};
AM 89
90     // Internet Explorer does not support wav files,
91     // support in other browsers depends on enabled plugins,
92     // so we use wav as a fallback
93     src += bw.ie || (plugin && plugin.enabledPlugin) ? '.mp3' : '.wav';
48e9c1 94
T 95     // HTML5
96     try {
217a1f 97         elem = $('<audio>').attr('src', src);
48e9c1 98         elem.get(0).play();
T 99     }
100     // old method
101     catch (e) {
102         elem = $('<embed id="sound" src="' + src + '" hidden=true autostart=true loop=false />');
103         elem.appendTo($('body'));
104         window.setTimeout("$('#sound').remove()", 5000);
105     }
106 }
107
2b81b0 108 // Desktop notification
1aa581 109 // - Require window.Notification API support (Chrome 22+ or Firefox 22+)
AM 110 function newmail_notifier_desktop(body, disabled_callback)
48e9c1 111 {
217a1f 112     var timeout = rcmail.env.newmail_notifier_timeout || 10,
1aa581 113         icon = rcmail.assets_path('plugins/newmail_notifier/mail.png'),
AM 114         success_callback = function() {
115             var popup = new window.Notification(rcmail.get_label('title', 'newmail_notifier'), {
2b81b0 116                 dir: "auto",
KC 117                 lang: "",
118                 body: body,
119                 tag: "newmail_notifier",
217a1f 120                 icon: icon
2b81b0 121             });
1aa581 122             popup.onclick = function() { this.close(); };
f806ed 123             setTimeout(function() { popup.close(); }, timeout * 1000);
1aa581 124         };
AM 125
126     try {
127         window.Notification.requestPermission(function(perm) {
128             if (perm == 'granted')
129                 success_callback();
130             else if (perm == 'denied' && disabled_callback)
131                 disabled_callback();
132         });
133
134         return true;
48e9c1 135     }
2b81b0 136     catch (e) {
1aa581 137         return false;
2b81b0 138     }
48e9c1 139 }
T 140
141 function newmail_notifier_test_desktop()
142 {
1aa581 143     var status = newmail_notifier_desktop(rcmail.get_label('testbody', 'newmail_notifier'), function() {
AM 144         rcmail.display_message(rcmail.get_label('desktopdisabled', 'newmail_notifier'), 'error');
145     });
48e9c1 146
1aa581 147     if (!status) {
AM 148         rcmail.display_message(rcmail.get_label('desktopunsupported', 'newmail_notifier'), 'error');
2b81b0 149     }
48e9c1 150 }
T 151
152 function newmail_notifier_test_basic()
153 {
154     newmail_notifier_basic();
155 }
156
157 function newmail_notifier_test_sound()
158 {
159     newmail_notifier_sound();
28d3ba 160 }