changeset 834:2491898b3041

browser (chat, notification): remove the textual alerts counter, merge it with the favicon counter
author souliane <souliane@mailoo.org>
date Tue, 12 Jan 2016 17:59:07 +0100
parents 4db064a70fc7
children a964ff397484
files src/browser/libervia_main.py src/browser/sat_browser/chat.py src/browser/sat_browser/notification.py
diffstat 3 files changed, 44 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/libervia_main.py	Tue Jan 12 16:55:43 2016 +0100
+++ b/src/browser/libervia_main.py	Tue Jan 12 17:59:07 2016 +0100
@@ -47,6 +47,7 @@
 from sat_browser import register
 from sat_browser.contact_list import ContactList
 from sat_browser import main_panel
+from sat_browser import chat
 from sat_browser import blog
 from sat_browser import xmlui
 from sat_browser import dialog
@@ -87,8 +88,8 @@
         self._register_box = None
         RootPanel().add(self.panel)
 
-        self.notification = notification.Notification()
         self.alerts_counter = notification.FaviconCounter()
+        self.notification = notification.Notification(self.alerts_counter)
         DOM.addEventPreview(self)
         self.importPlugins()
         self._register = json.RegisterCall()
@@ -272,9 +273,6 @@
             panel = self.tab_panel.deck.getWidget(tab_index)
         panel.addWidget(wid)
 
-    def displayNotification(self, title, body):
-        self.notification.notify(title, body)
-
     def gotMenus(self, backend_menus):
         """Put the menus data in cache and build the main menu bar
 
@@ -612,11 +610,20 @@
     def _newAlert(self, message, title, alert_type):
         dialog.InfoDialog(title, message).show()
 
-    def updateAlertsCounter(self):
+    def isHidden(self):
+        """Tells if the frontend window is hidden.
+
+        @return bool
+        """
+        return self.notification.isHidden()
+
+    def updateAlertsCounter(self, extra_inc=0):
         """Update the over whole alerts counter
 
+        @param extra_inc (int): extra counter
         """
-        self.alerts_counter.update(self.alerts_count)
+        extra = self.alerts_counter.extra + extra_inc
+        self.alerts_counter.update(self.alerts_count, extra=extra)
 
     def _paramUpdate(self, name, value, category, refresh=True):
         """This is called when the paramUpdate signal is received, but also
--- a/src/browser/sat_browser/chat.py	Tue Jan 12 16:55:43 2016 +0100
+++ b/src/browser/sat_browser/chat.py	Tue Jan 12 17:59:07 2016 +0100
@@ -219,6 +219,7 @@
         @param extra (dict): message data
         @param link_cb: method to call when the info is clicked, ignored if type_ is not 'link'
         """
+        QuickChat.printInfo(self, msg, type_, extra)
         if extra is None:
             extra = {}
         if type_ == 'normal':
@@ -238,11 +239,20 @@
         self.content_scroll.scrollToBottom()
 
     def printMessage(self, nick, my_message, message, timestamp, extra=None, profile=C.PROF_KEY_NONE):
+        QuickChat.printMessage(self, nick, my_message, message, timestamp, extra, profile)
         if extra is None:
             extra = {}
         self.content.add(ChatText(timestamp, nick, my_message, message, extra))
         self.content_scroll.scrollToBottom()
 
+    def notify(self, contact="somebody", msg=""):
+        """Notify the user of a new message if primitivus doesn't have the focus.
+
+        @param contact (unicode): contact who wrote to the users
+        @param msg (unicode): the message that has been received
+        """
+        self.host.notification.notify(contact, msg)
+
     def printDayChange(self, day):
         """Display the day on a new line.
 
--- a/src/browser/sat_browser/notification.py	Tue Jan 12 16:55:43 2016 +0100
+++ b/src/browser/sat_browser/notification.py	Tue Jan 12 17:59:07 2016 +0100
@@ -20,7 +20,12 @@
     Requires both Web Notifications and Page Visibility API.
     """
 
-    def __init__(self):
+    def __init__(self, alerts_counter):
+        """
+
+        @param alerts_counter (FaviconCounter): counter instance
+        """
+        self.alerts_counter = alerts_counter
         self.enabled = False
         user_agent = None
         notif_permission = None
@@ -54,8 +59,6 @@
 
         wnd().onfocus = self.onFocus
         # wnd().onblur = self.onBlur
-        self._notif_count = 0
-        self._orig_title = Window.getTitle()
 
     def _installChromiumWorkaround(self):
         # XXX: Workaround for Chromium behaviour, it's doens't manage requestPermission on onLoad event
@@ -91,8 +94,7 @@
         wnd().onclick = self._old_click
 
     def onFocus(self, event=None):
-        Window.setTitle(self._orig_title)
-        self._notif_count = 0
+        self.alerts_counter.update(extra=0)
 
     # def onBlur(self, event=None):
     #     pass
@@ -113,14 +115,9 @@
            """)
         notification.onshow = lambda: Timer(TIMER_DELAY, lambda timer: notification.close())
 
-    def highlightTab(self):
-        self._notif_count += 1
-        Window.setTitle("%s (%d)" % (self._orig_title, self._notif_count))
-
     def notify(self, title, body, icon='/media/icons/apps/48/sat.png'):
         if self.isHidden():
             self._notify(title, body, icon)
-            self.highlightTab()
 
 
 class FaviconCounter(object):
@@ -136,5 +133,17 @@
         });
         """)
 
-    def update(self, count):
-        self.counter.badge(count)
+        self.count = 0  # messages that are not displayed
+        self.extra = 0  # messages that are displayed but the window is hidden
+
+    def update(self, count=None, extra=None):
+        """Update the favicon counter.
+
+        @param count (int): primary counter
+        @param extra (int): extra counter
+        """
+        if count is not None:
+            self.count = count
+        if extra is not None:
+            self.extra = extra
+        self.counter.badge(self.count + self.extra)