comparison plugins/newmail_notifier/newmail_notifier.js @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 /**
2 * New Mail Notifier plugin script
3 *
4 * @author Aleksander Machniak <alec@alec.pl>
5 *
6 * @licstart The following is the entire license notice for the
7 * JavaScript code in this file.
8 *
9 * Copyright (c) 2013-2016, The Roundcube Dev Team
10 *
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.
18 */
19
20 if (window.rcmail && rcmail.env.task == 'mail') {
21 rcmail.addEventListener('plugin.newmail_notifier', newmail_notifier_run)
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 });
28 }
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)
38 newmail_notifier_desktop(rcmail.get_label('body', 'newmail_notifier'));
39 }
40
41 // Stops notification
42 function newmail_notifier_stop(prop)
43 {
44 // revert original favicon
45 if (rcmail.env.favicon_href && rcmail.env.favicon_changed && (!prop || prop.action != 'check-recent')) {
46 $('<link rel="shortcut icon" href="'+rcmail.env.favicon_href+'"/>').replaceAll('link[rel="shortcut icon"]');
47 rcmail.env.favicon_changed = 0;
48 }
49
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) {}
56 }
57
58 // Basic notification: window.focus and favicon change
59 function newmail_notifier_basic()
60 {
61 var w = rcmail.is_framed() ? window.parent : window,
62 path = rcmail.assets_path('plugins/newmail_notifier');
63
64 w.focus();
65
66 // we cannot simply change a href attribute, we must to replace the link element (at least in FF)
67 var link = $('<link rel="shortcut icon">').attr('href', path + '/favicon.ico'),
68 oldlink = $('link[rel="shortcut icon"]', w.document);
69
70 if (!rcmail.env.favicon_href)
71 rcmail.env.favicon_href = oldlink.attr('href');
72
73 rcmail.env.favicon_changed = 1;
74 link.replaceAll(oldlink);
75
76 // Add IE icon overlay if we're pinned to Taskbar
77 try {
78 if (window.external.msIsSiteMode()) {
79 window.external.msSiteModeSetIconOverlay(path + '/overlay.ico', rcmail.get_label('title', 'newmail_notifier'));
80 }
81 } catch(e) {}
82 }
83
84 // Sound notification
85 function newmail_notifier_sound()
86 {
87 var elem, src = rcmail.assets_path('plugins/newmail_notifier/sound'),
88 plugin = navigator.mimeTypes ? navigator.mimeTypes['audio/mp3'] : {};
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';
94
95 // HTML5
96 try {
97 elem = $('<audio>').attr('src', src);
98 elem.get(0).play();
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
108 // Desktop notification
109 // - Require window.Notification API support (Chrome 22+ or Firefox 22+)
110 function newmail_notifier_desktop(body, disabled_callback)
111 {
112 var timeout = rcmail.env.newmail_notifier_timeout || 10,
113 icon = rcmail.assets_path('plugins/newmail_notifier/mail.png'),
114 success_callback = function() {
115 var popup = new window.Notification(rcmail.get_label('title', 'newmail_notifier'), {
116 dir: "auto",
117 lang: "",
118 body: body,
119 tag: "newmail_notifier",
120 icon: icon
121 });
122 popup.onclick = function() { this.close(); };
123 setTimeout(function() { popup.close(); }, timeout * 1000);
124 };
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;
135 }
136 catch (e) {
137 return false;
138 }
139 }
140
141 function newmail_notifier_test_desktop()
142 {
143 var status = newmail_notifier_desktop(rcmail.get_label('testbody', 'newmail_notifier'), function() {
144 rcmail.display_message(rcmail.get_label('desktopdisabled', 'newmail_notifier'), 'error');
145 });
146
147 if (!status) {
148 rcmail.display_message(rcmail.get_label('desktopunsupported', 'newmail_notifier'), 'error');
149 }
150 }
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();
160 }