annotate src/browser/sat_browser/notification.py @ 533:19fc2ebc02dd

browser_side: management of new "info" newMessage type
author Goffi <goffi@goffi.org>
date Fri, 05 Sep 2014 19:29:35 +0200
parents 97c72fe4a5f2
children e11e34ac0f67
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
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 439
diff changeset
8
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 439
diff changeset
9 import dialog
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
10
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
11 TIMER_DELAY = 5000
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12
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 class Notification(object):
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 """
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 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
17 background, send desktop notifications on messages.
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 Requires both Web Notifications and Page Visibility API.
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 """
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 def __init__(self):
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
23 self.enabled = False
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
24 user_agent = None
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
25 notif_permission = None
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 JS("""
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 if (!('hidden' in document))
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 document.hidden = false;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
30 user_agent = navigator.userAgent
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
31
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 if (!('Notification' in window))
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 return;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
35 notif_permission = Notification.permission
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
36
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 if (Notification.permission === 'granted')
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 this.enabled = true;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 else if (Notification.permission === 'default') {
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 Notification.requestPermission(function(permission){
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 if (permission !== 'granted')
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 return;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
45 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
46 });
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
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
50 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
51 self.user_agent = user_agent
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
52 self._installChromiumWorkaround()
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
53
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
54 wnd().onfocus = self.onFocus
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
55 # wnd().onblur = self.onBlur
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
56 self._notif_count = 0
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
57 self._orig_title = Window.getTitle()
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
58
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
59 def _installChromiumWorkaround(self):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
60 # 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
61 # see https://code.google.com/p/chromium/issues/detail?id=274284
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
62 # FIXME: need to be removed if Chromium behaviour changes
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
63 try:
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
64 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
65 version = int(version_full.split('.')[0])
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
66 except (IndexError, ValueError):
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
67 log.warning("Can't find Chromium version")
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
68 version = 0
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
69 log.info("Chromium version: %d" % (version,))
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
70 if version < 22:
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
71 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
72 return
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
73 if version < 32:
424
4ba4b099d266 browser side: notifications: fixed dialog message in Chromium workaround
Goffi <goffi@goffi.org>
parents: 423
diff changeset
74 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
75 return
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
76
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
77 log.info("==> Installing Chromium notifications request workaround <==")
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
78 self._old_click = wnd().onclick
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
79 wnd().onclick = self._chromiumWorkaround
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
81 def _chromiumWorkaround(self):
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 424
diff changeset
82 log.info("Activating workaround")
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
83 JS("""
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
84 Notification.requestPermission(function(permission){
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
85 if (permission !== 'granted')
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
86 return;
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
87 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
88 });
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 """)
423
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
90 wnd().onclick = self._old_click
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 onFocus(self):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
93 Window.setTitle(self._orig_title)
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
94 self._notif_count = 0
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
95
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
96 # def onBlur(self):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
97 # pass
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
98
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
99 def isHidden(self):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
100 JS("""return document.hidden;""")
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
101
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
102 def _notify(self, title, body, icon):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
103 if not self.enabled:
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
104 return
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
105 notification = None
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
106 JS("""
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
107 notification = new Notification(title, {body: body, icon: icon});
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
108 // Probably won’t work, but it doesn’t hurt to try.
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
109 notification.addEventListener('click', function() {
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
110 window.focus();
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
111 });
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 notification.onshow = lambda: Timer(TIMER_DELAY, lambda timer: notification.close())
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 highlightTab(self):
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
116 self._notif_count += 1
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
117 Window.setTitle("%s (%d)" % (self._orig_title, self._notif_count))
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
118
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
119 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
120 if self.isHidden():
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
121 self._notify(title, body, icon)
f9130176bc8d browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents: 328
diff changeset
122 self.highlightTab()