comparison src/browser/notification.py @ 449:981ed669d3b3

/!\ reorganize all the file hierarchy, move the code and launching script to src: - browser_side --> src/browser - public --> src/browser_side/public - libervia.py --> src/browser/libervia_main.py - libervia_server --> src/server - libervia_server/libervia.sh --> src/libervia.sh - twisted --> src/twisted - new module src/common - split constants.py in 3 files: - src/common/constants.py - src/browser/constants.py - src/server/constants.py - output --> html (generated by pyjsbuild during the installation) - new option/parameter "data_dir" (-d) to indicates the directory containing html and server_css - setup.py installs libervia to the following paths: - src/common --> <LIB>/libervia/common - src/server --> <LIB>/libervia/server - src/twisted --> <LIB>/twisted - html --> <SHARE>/libervia/html - server_side --> <SHARE>libervia/server_side - LIBERVIA_INSTALL environment variable takes 2 new options with prompt confirmation: - clean: remove previous installation directories - purge: remove building and previous installation directories You may need to update your sat.conf and/or launching script to update the following options/parameters: - ssl_certificate - data_dir
author souliane <souliane@mailoo.org>
date Tue, 20 May 2014 06:41:16 +0200
parents browser_side/notification.py@d52f529a6d42
children
comparison
equal deleted inserted replaced
448:14c35f7f1ef5 449:981ed669d3b3
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()