Mercurial > libervia-web
annotate browser_side/notification.py @ 442:17259c2ff96f
browser_side: changes about the UI (remarks from Franck):
- select "Register" by default instead of "Login"
- make the "Register" and "Login" button more visible
- do not use the "New message" button, directly display an empty entry
- "New message" button would be bigger (if we use it again)
- remove the background from the microblog entry header
- "toggle syntax" button is displayed below the text area and with plain text (no icon)
- icons for entry action are displayed in this order: "reply", "edit", "delete"
- icon for replying is bigger than the others
- used a "smiley" from tango as default avatar
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 14 May 2014 12:24:38 +0200 |
parents | d52f529a6d42 |
children |
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__) |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
4 from pyjamas import Window |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
5 from pyjamas.Timer import Timer |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
6 from browser_side import dialog |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
7 from sat.core.i18n import _ |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
8 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
9 TIMER_DELAY = 5000 |
328
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 |
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 class Notification(object): |
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 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
|
15 background, send desktop notifications on messages. |
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 Requires both Web Notifications and Page Visibility API. |
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 |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 def __init__(self): |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
21 self.enabled = False |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
22 user_agent = None |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
23 notif_permission = None |
328
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 JS(""" |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 if (!('hidden' in document)) |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 document.hidden = false; |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
28 user_agent = navigator.userAgent |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
29 |
328
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 if (!('Notification' in window)) |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 return; |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
33 notif_permission = Notification.permission |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
34 |
328
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 if (Notification.permission === 'granted') |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 this.enabled = true; |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 else if (Notification.permission === 'default') { |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 Notification.requestPermission(function(permission){ |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 if (permission !== 'granted') |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 return; |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
43 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
|
44 }); |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 } |
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 |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
48 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
|
49 self.user_agent = user_agent |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
50 self._installChromiumWorkaround() |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
51 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
52 wnd().onfocus = self.onFocus |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
53 # wnd().onblur = self.onBlur |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
54 self._notif_count = 0 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
55 self._orig_title = Window.getTitle() |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
56 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
57 def _installChromiumWorkaround(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
58 # 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
|
59 # see https://code.google.com/p/chromium/issues/detail?id=274284 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
60 # FIXME: need to be removed if Chromium behaviour changes |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
61 try: |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
62 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
|
63 version = int(version_full.split('.')[0]) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
64 except (IndexError, ValueError): |
439
d52f529a6d42
browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents:
424
diff
changeset
|
65 log.warning("Can't find Chromium version") |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
66 version = 0 |
439
d52f529a6d42
browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents:
424
diff
changeset
|
67 log.info("Chromium version: %d" % (version,)) |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
68 if version < 22: |
439
d52f529a6d42
browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents:
424
diff
changeset
|
69 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
|
70 return |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
71 if version < 32: |
424
4ba4b099d266
browser side: notifications: fixed dialog message in Chromium workaround
Goffi <goffi@goffi.org>
parents:
423
diff
changeset
|
72 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
|
73 return |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
74 |
439
d52f529a6d42
browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents:
424
diff
changeset
|
75 log.info("==> Installing Chromium notifications request workaround <==") |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
76 self._old_click = wnd().onclick |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
77 wnd().onclick = self._chromiumWorkaround |
328
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
79 def _chromiumWorkaround(self): |
439
d52f529a6d42
browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents:
424
diff
changeset
|
80 log.info("Activating workaround") |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
81 JS(""" |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
82 Notification.requestPermission(function(permission){ |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
83 if (permission !== 'granted') |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
84 return; |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
85 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
|
86 }); |
835a8ae799e7
Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 """) |
423
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
88 wnd().onclick = self._old_click |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
89 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
90 def onFocus(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
91 Window.setTitle(self._orig_title) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
92 self._notif_count = 0 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
93 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
94 # def onBlur(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
95 # pass |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
96 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
97 def isHidden(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
98 JS("""return document.hidden;""") |
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 _notify(self, title, body, icon): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
101 if not self.enabled: |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
102 return |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
103 notification = None |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
104 JS(""" |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
105 notification = new Notification(title, {body: body, icon: icon}); |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
106 // Probably won’t work, but it doesn’t hurt to try. |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
107 notification.addEventListener('click', function() { |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
108 window.focus(); |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
109 }); |
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 notification.onshow = lambda: Timer(TIMER_DELAY, lambda timer: notification.close()) |
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 def highlightTab(self): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
114 self._notif_count += 1 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
115 Window.setTitle("%s (%d)" % (self._orig_title, self._notif_count)) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
116 |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
117 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
|
118 if self.isHidden(): |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
119 self._notify(title, body, icon) |
f9130176bc8d
browser side: notifications enhancements:
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
120 self.highlightTab() |