comparison src/browser/sat_browser/notification.py @ 467:97c72fe4a5f2

browser_side: import fixes: - moved browser modules in a sat_browser packages, to avoid import conflicts with std lib (e.g. logging), and let pyjsbuild work normaly - refactored bad import practices: classes are most of time not imported directly, module is imported instead.
author Goffi <goffi@goffi.org>
date Mon, 09 Jun 2014 22:15:26 +0200
parents src/browser/notification.py@981ed669d3b3
children e11e34ac0f67
comparison
equal deleted inserted replaced
466:01880aa8ea2d 467:97c72fe4a5f2
1 from __pyjamas__ import JS, wnd
2 from sat.core.log import getLogger
3 log = getLogger(__name__)
4 from sat.core.i18n import _
5
6 from pyjamas import Window
7 from pyjamas.Timer import Timer
8
9 import dialog
10
11 TIMER_DELAY = 5000
12
13
14 class Notification(object):
15 """
16 If the browser supports it, the user allowed it to and the tab is in the
17 background, send desktop notifications on messages.
18
19 Requires both Web Notifications and Page Visibility API.
20 """
21
22 def __init__(self):
23 self.enabled = False
24 user_agent = None
25 notif_permission = None
26 JS("""
27 if (!('hidden' in document))
28 document.hidden = false;
29
30 user_agent = navigator.userAgent
31
32 if (!('Notification' in window))
33 return;
34
35 notif_permission = Notification.permission
36
37 if (Notification.permission === 'granted')
38 this.enabled = true;
39
40 else if (Notification.permission === 'default') {
41 Notification.requestPermission(function(permission){
42 if (permission !== 'granted')
43 return;
44
45 self.enabled = true; //need to use self instead of this
46 });
47 }
48 """)
49
50 if "Chrome" in user_agent and notif_permission not in ['granted', 'denied']:
51 self.user_agent = user_agent
52 self._installChromiumWorkaround()
53
54 wnd().onfocus = self.onFocus
55 # wnd().onblur = self.onBlur
56 self._notif_count = 0
57 self._orig_title = Window.getTitle()
58
59 def _installChromiumWorkaround(self):
60 # XXX: Workaround for Chromium behaviour, it's doens't manage requestPermission on onLoad event
61 # see https://code.google.com/p/chromium/issues/detail?id=274284
62 # FIXME: need to be removed if Chromium behaviour changes
63 try:
64 version_full = [s for s in self.user_agent.split() if "Chrome" in s][0].split('/')[1]
65 version = int(version_full.split('.')[0])
66 except (IndexError, ValueError):
67 log.warning("Can't find Chromium version")
68 version = 0
69 log.info("Chromium version: %d" % (version,))
70 if version < 22:
71 log.info("Notification use the old prefixed version or are unmanaged")
72 return
73 if version < 32:
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()
75 return
76
77 log.info("==> Installing Chromium notifications request workaround <==")
78 self._old_click = wnd().onclick
79 wnd().onclick = self._chromiumWorkaround
80
81 def _chromiumWorkaround(self):
82 log.info("Activating workaround")
83 JS("""
84 Notification.requestPermission(function(permission){
85 if (permission !== 'granted')
86 return;
87 self.enabled = true; //need to use self instead of this
88 });
89 """)
90 wnd().onclick = self._old_click
91
92 def onFocus(self):
93 Window.setTitle(self._orig_title)
94 self._notif_count = 0
95
96 # def onBlur(self):
97 # pass
98
99 def isHidden(self):
100 JS("""return document.hidden;""")
101
102 def _notify(self, title, body, icon):
103 if not self.enabled:
104 return
105 notification = None
106 JS("""
107 notification = new Notification(title, {body: body, icon: icon});
108 // Probably won’t work, but it doesn’t hurt to try.
109 notification.addEventListener('click', function() {
110 window.focus();
111 });
112 """)
113 notification.onshow = lambda: Timer(TIMER_DELAY, lambda timer: notification.close())
114
115 def highlightTab(self):
116 self._notif_count += 1
117 Window.setTitle("%s (%d)" % (self._orig_title, self._notif_count))
118
119 def notify(self, title, body, icon='/media/icons/apps/48/sat.png'):
120 if self.isHidden():
121 self._notify(title, body, icon)
122 self.highlightTab()