annotate src/browser/sat_browser/notification.py @ 714:03e9fe91081c

browser_side: use favico.js to display alerts counter
author souliane <souliane@mailoo.org>
date Mon, 20 Jul 2015 10:18:02 +0200
parents e11e34ac0f67
children 2491898b3041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
1 from __pyjamas__ import JS, wnd
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
2 from sat.core.log import getLogger
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
3 log = getLogger(__name__)
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 439
diff changeset
4 from sat.core.i18n import _
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 439
diff changeset
5
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
6 from pyjamas import Window
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
7 from pyjamas.Timer import Timer
714
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
8 import favico.min.js
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 439
diff changeset
9
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 439
diff changeset
10 import dialog
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
11
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
12 TIMER_DELAY = 5000
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 class Notification(object):
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 """
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 If the browser supports it, the user allowed it to and the tab is in the
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 background, send desktop notifications on messages.
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 Requires both Web Notifications and Page Visibility API.
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 """
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 def __init__(self):
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
24 self.enabled = False
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
25 user_agent = None
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
26 notif_permission = None
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 JS("""
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 if (!('hidden' in document))
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 document.hidden = false;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
31 user_agent = navigator.userAgent
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
32
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 if (!('Notification' in window))
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 return;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
36 notif_permission = Notification.permission
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
37
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 if (Notification.permission === 'granted')
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 this.enabled = true;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 else if (Notification.permission === 'default') {
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 Notification.requestPermission(function(permission){
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 if (permission !== 'granted')
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 return;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
46 self.enabled = true; //need to use self instead of this
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 });
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 }
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 """)
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
51 if "Chrome" in user_agent and notif_permission not in ['granted', 'denied']:
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
52 self.user_agent = user_agent
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
53 self._installChromiumWorkaround()
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
54
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
55 wnd().onfocus = self.onFocus
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
56 # wnd().onblur = self.onBlur
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
57 self._notif_count = 0
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
58 self._orig_title = Window.getTitle()
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
59
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
60 def _installChromiumWorkaround(self):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
61 # XXX: Workaround for Chromium behaviour, it's doens't manage requestPermission on onLoad event
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
62 # see https://code.google.com/p/chromium/issues/detail?id=274284
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
63 # FIXME: need to be removed if Chromium behaviour changes
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
64 try:
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
65 version_full = [s for s in self.user_agent.split() if "Chrome" in s][0].split('/')[1]
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
66 version = int(version_full.split('.')[0])
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
67 except (IndexError, ValueError):
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
68 log.warning("Can't find Chromium version")
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
69 version = 0
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
70 log.info("Chromium version: %d" % (version,))
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
71 if version < 22:
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
72 log.info("Notification use the old prefixed version or are unmanaged")
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
73 return
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
74 if version < 32:
424
4ba4b099d266 browser side: notifications: fixed dialog message in Chromium workaround
Goffi <goffi@goffi.org>
parents: 423
diff changeset
75 dialog.InfoDialog(_("Notifications activation for Chromium"), _('You need to activate notifications manually for your Chromium version.<br/>To activate notifications, click on the favicon on the left of the address bar')).show()
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
76 return
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
77
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
78 log.info("==> Installing Chromium notifications request workaround <==")
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
79 self._old_click = wnd().onclick
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
80 wnd().onclick = self._chromiumWorkaround
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
82 def _chromiumWorkaround(self):
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
83 log.info("Activating workaround")
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
84 JS("""
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
85 Notification.requestPermission(function(permission){
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
86 if (permission !== 'granted')
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
87 return;
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
88 self.enabled = true; //need to use self instead of this
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 });
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 """)
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
91 wnd().onclick = self._old_click
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
92
591
e11e34ac0f67 browser_side: fixes Notification.onFocus
souliane <souliane@mailoo.org>
parents: 467
diff changeset
93 def onFocus(self, event=None):
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
94 Window.setTitle(self._orig_title)
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
95 self._notif_count = 0
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
96
591
e11e34ac0f67 browser_side: fixes Notification.onFocus
souliane <souliane@mailoo.org>
parents: 467
diff changeset
97 # def onBlur(self, event=None):
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
98 # pass
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
99
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
100 def isHidden(self):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
101 JS("""return document.hidden;""")
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
102
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
103 def _notify(self, title, body, icon):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
104 if not self.enabled:
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
105 return
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
106 notification = None
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
107 JS("""
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
108 notification = new Notification(title, {body: body, icon: icon});
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
109 // Probably won’t work, but it doesn’t hurt to try.
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
110 notification.addEventListener('click', function() {
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
111 window.focus();
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
112 });
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
113 """)
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
114 notification.onshow = lambda: Timer(TIMER_DELAY, lambda timer: notification.close())
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
115
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
116 def highlightTab(self):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
117 self._notif_count += 1
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
118 Window.setTitle("%s (%d)" % (self._orig_title, self._notif_count))
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
119
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
120 def notify(self, title, body, icon='/media/icons/apps/48/sat.png'):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
121 if self.isHidden():
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
122 self._notify(title, body, icon)
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
123 self.highlightTab()
714
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
124
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
125
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
126 class FaviconCounter(object):
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
127 """Display numbers over the favicon to signal e.g. waiting messages"""
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
128
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
129 def __init__(self):
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
130 # XXX: the file favico.min.js is loaded from public/libervia.html because I get NS_ERROR_FAILURE when it's loaded with Pyjamas. It sounds like a context issue, with the favicon not being found.
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
131
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
132 JS("""
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
133 self.counter = new top.Favico({
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
134 animation : 'slide',
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
135 bgColor: '#5CB85C',
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
136 });
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
137 """)
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
138
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
139 def update(self, count):
03e9fe91081c browser_side: use favico.js to display alerts counter
souliane <souliane@mailoo.org>
parents: 591
diff changeset
140 self.counter.badge(count)