Mercurial > libervia-web
changeset 328:835a8ae799e7
Add notifications support, fixes bug 7.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 23 Feb 2013 16:27:32 +0100 |
parents | 6126bd24e7dd |
children | c144b603fb93 |
files | browser_side/notification.py libervia.py |
diffstat | 2 files changed, 52 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/browser_side/notification.py Sat Feb 23 16:27:32 2013 +0100 @@ -0,0 +1,45 @@ +from __pyjamas__ import JS + + +class Notification(object): + """ + If the browser supports it, the user allowed it to and the tab is in the + background, send desktop notifications on messages. + + Requires both Web Notifications and Page Visibility API. + """ + + def __init__(self): + JS(""" + this.enabled = false; + + if (!('hidden' in document)) + document.hidden = false; + + if (!('Notification' in window)) + return; + + if (Notification.permission === 'granted') + this.enabled = true; + + else if (Notification.permission === 'default') { + Notification.requestPermission(function(permission){ + if (permission !== 'granted') + return; + + this.enabled = true; + }); + } + """) + + def notify(self, title, body, icon='/media/icons/apps/48/sat.png'): + JS(""" + if (this.enabled && document.hidden) { + notification = Notification(title, {body: body, icon: icon}); + + // Probably won’t work, but it doesn’t hurt to try. + notification.addEventListener('click', function() { + window.focus(); + }); + } + """)
--- a/libervia.py Tue Jan 07 23:49:39 2014 +0100 +++ b/libervia.py Sat Feb 23 16:27:32 2013 +0100 @@ -35,6 +35,7 @@ from browser_side.jid import JID from browser_side.xmlui import XMLUI from browser_side.tools import html_sanitize +from browser_side.notification import Notification from sat_frontends.tools.misc import InputHistory from sat_frontends.tools.strings import getURLParams @@ -180,6 +181,7 @@ self.avatars_cache = {} # keep track of jid's avatar hash (key=jid, value=file) self._register_box = None RootPanel().add(self.panel) + self.notification = Notification() DOM.addEventPreview(self) self._register = RegisterCall() self._register.call('registerParams', None) @@ -305,6 +307,9 @@ panel = self.tab_panel.tabBar.getTabWidget(tab_index) panel.addWidget(wid) + def displayNotification(self, title, body): + self.notification.notify(title, body) + def _isRegisteredCB(self, registered): if not registered: self._register_box = RegisterBox(self.logged) @@ -648,10 +653,11 @@ _to = JID(to_jid) other = _to if _from.bare == self.whoami.bare else _from lib_wid = self.getLiberviaWidget(panels.ChatPanel, other, ignoreOtherTabs=False) + self.displayNotification(_from, msg) if lib_wid is not None: lib_wid.printMessage(_from, msg, extra) else: - # The message has not been showed, we must indicate it + # The message has not been shown, we must indicate it self.contact_panel.setContactMessageWaiting(other.bare, True) def _presenceUpdateCb(self, entity, show, priority, statuses):