/
html5_notifier.js
145 lines (137 loc) · 4.67 KB
/
html5_notifier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* html5_notifier
* Shows a desktop notification every time a new (recent) mail comes in
*
* @version 0.5.0 - 19.12.2013
* @author Tilman Stremlau <tilman@stremlau.net>
* @website stremlau.net/html5_notifier
* @licence GNU GPL
*
**/
function rcmail_show_notification(message)
{
if (use_notifications)
{
if ("Notification" in window) {
var notification = new Notification(rcmail.gettext('notification_title', 'html5_notifier').replace('[from]', message.from), {
icon: './plugins/html5_notifier/images/new_mail.png',
body: message.subject
});
notification.onclick = function() {
if(message.opentype == '1') {
rcmail.open_window('?_task=mail&_action=show&_uid='+message.uid);
} else {
window.open('?_task=mail&_action=show&_extwin=1&_uid='+message.uid);
}
}
if (parseInt(message.duration) > 0)
{
setTimeout(function(){ notification.close(); }, (parseInt(message.duration)*1000));
}
}
}
}
function rcmail_browser_notifications()
{
if ("Notification" in window && Notification.permission) {
if (Notification.permission === "granted") {
rcmail.display_message(rcmail.gettext('ok_notifications', 'html5_notifier'), 'notice');
}
else {
Notification.requestPermission(rcmail_check_notifications);
}
}
else if (window.webkitNotifications) {
if (window.webkitNotifications.checkPermission() == 0)
{
rcmail.display_message(rcmail.gettext('ok_notifications', 'html5_notifier'), 'notice');
}
else
{
window.webkitNotifications.requestPermission(rcmail_check_notifications);
}
}
else
{
rcmail.display_message(rcmail.gettext('no_notifications', 'html5_notifier'), 'error');
}
}
function rcmail_browser_notifications_test() {
if (use_notifications)
{
rcmail.display_message(rcmail.gettext('check_ok', 'html5_notifier'), 'notice');
var message = new Object();
message.duration = 8;
message.uid = 0;
message.subject = 'It Works!';
message.from = 'TESTMAN';
message.opentype = $('select[name=_html5_notifier_popuptype]').val();
rcmail_show_notification(message);
}
else
{
if ("Notification" in window && Notification.permission) {
if (Notification.permission == 'denied') {
rcmail.display_message(rcmail.gettext('check_fail_blocked', 'html5_notifier'), 'error');
return false;
}
}
else if (window.webkitNotifications)
{
if (window.webkitNotifications.checkPermission() == 2)
{
rcmail.display_message(rcmail.gettext('check_fail_blocked', 'html5_notifier'), 'error');
return false;
}
}
rcmail.display_message(rcmail.gettext('check_fail', 'html5_notifier'), 'error');
}
}
function rcmail_browser_notifications_colorate() {
if ("Notification" in window && Notification.permission) {
var broco = $('#rcmfd_html5_notifier_browser_conf');
if (broco)
{
switch (Notification.permission)
{
case 'granted': broco.css('color', 'green'); break;
case 'default': broco.css('color', 'orange'); break;
case 'denied': broco.css('color', 'red'); break;
}
}
}
else if (window.webkitNotifications)
{
var broco = $('#rcmfd_html5_notifier_browser_conf');
if (broco)
{
switch (window.webkitNotifications.checkPermission())
{
case 0: broco.css('color', 'green'); break;
case 1: broco.css('color', 'orange'); break;
case 2: broco.css('color', 'red'); break;
}
}
}
}
var use_notifications = false;
var rcmail_check_notifications = function(e)
{
if ("Notification" in window && Notification.permission) {
if (Notification.permission === "granted") {
use_notifications = true;
}
}
else if (window.webkitNotifications)
{
if (window.webkitNotifications.checkPermission() == 0) {
use_notifications = true;
}
}
rcmail_browser_notifications_colorate();
}
if (window.rcmail)
{
rcmail.addEventListener('plugin.showNotification', rcmail_show_notification);
rcmail.addEventListener('init', rcmail_check_notifications);
}