Mercurial > libervia-web
annotate browser_side/notification.py @ 436:73f767a9ac2f
server_side: added constant SERVICE_PROFILE to remove hard-coded value
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 01 May 2014 11:31:51 +0200 |
parents | 4ba4b099d266 |
children | d52f529a6d42 |
rev | line source |
---|---|
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
1 from __pyjamas__ import JS, wnd |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
2 from pyjamas import Window |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
3 from pyjamas.Timer import Timer |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
4 from browser_side import dialog |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
5 from sat.core.i18n import _ |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
6 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
7 TIMER_DELAY = 5000 |
328
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 class Notification(object): |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 """ |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 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
|
13 background, send desktop notifications on messages. |
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 Requires both Web Notifications and Page Visibility API. |
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 |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 def __init__(self): |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
19 self.enabled = False |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
20 user_agent = None |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
21 notif_permission = None |
328
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 JS(""" |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 if (!('hidden' in document)) |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 document.hidden = false; |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
26 user_agent = navigator.userAgent |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
27 |
328
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 if (!('Notification' in window)) |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 return; |
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 notif_permission = Notification.permission |
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.permission === 'granted') |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 this.enabled = true; |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 else if (Notification.permission === 'default') { |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 Notification.requestPermission(function(permission){ |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 if (permission !== 'granted') |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 return; |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
41 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
|
42 }); |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 } |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 """) |
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 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
|
47 self.user_agent = user_agent |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
48 self._installChromiumWorkaround() |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
49 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
50 wnd().onfocus = self.onFocus |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
51 # wnd().onblur = self.onBlur |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
52 self._notif_count = 0 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
53 self._orig_title = Window.getTitle() |
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 def _installChromiumWorkaround(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
56 # 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
|
57 # see https://code.google.com/p/chromium/issues/detail?id=274284 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
58 # FIXME: need to be removed if Chromium behaviour changes |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
59 try: |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
60 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
|
61 version = int(version_full.split('.')[0]) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
62 except (IndexError, ValueError): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
63 print "Can't find Chromium version" |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
64 version = 0 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
65 print "Chromium version: %d" % (version,) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
66 if version < 22: |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
67 print "Notification use the old prefixed version or are unmanaged" |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
68 return |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
69 if version < 32: |
424
4ba4b099d266
browser side: notifications: fixed dialog message in Chromium workaround
Goffi <goffi@goffi.org>
parents:
423
diff
changeset
|
70 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
|
71 return |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
72 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
73 print "==> Installing Chromium notifications request workaround <==" |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
74 self._old_click = wnd().onclick |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
75 wnd().onclick = self._chromiumWorkaround |
328
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
77 def _chromiumWorkaround(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
78 print "Activating workaround" |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
79 JS(""" |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
80 Notification.requestPermission(function(permission){ |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
81 if (permission !== 'granted') |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
82 return; |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
83 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
|
84 }); |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 """) |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
86 wnd().onclick = self._old_click |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
87 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
88 def onFocus(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
89 Window.setTitle(self._orig_title) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
90 self._notif_count = 0 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
91 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
92 # def onBlur(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
93 # pass |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
94 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
95 def isHidden(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
96 JS("""return document.hidden;""") |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
97 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
98 def _notify(self, title, body, icon): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
99 if not self.enabled: |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
100 return |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
101 notification = None |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
102 JS(""" |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
103 notification = new Notification(title, {body: body, icon: icon}); |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
104 // Probably won’t work, but it doesn’t hurt to try. |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
105 notification.addEventListener('click', function() { |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
106 window.focus(); |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
107 }); |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
108 """) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
109 notification.onshow = lambda: Timer(TIMER_DELAY, lambda timer: notification.close()) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
110 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
111 def highlightTab(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
112 self._notif_count += 1 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
113 Window.setTitle("%s (%d)" % (self._orig_title, self._notif_count)) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
114 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
115 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
|
116 if self.isHidden(): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
117 self._notify(title, body, icon) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
118 self.highlightTab() |