changeset 678:a630b94280d5

primitivus: code factorization for user notification
author souliane <souliane@mailoo.org>
date Mon, 21 Oct 2013 15:52:28 +0200
parents 9a50aa7feefb
children 59c9a7ff903d
files frontends/src/primitivus/chat.py
diffstat 1 files changed, 20 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/chat.py	Fri Oct 18 19:23:03 2013 +0200
+++ b/frontends/src/primitivus/chat.py	Mon Oct 21 15:52:28 2013 +0200
@@ -262,60 +262,56 @@
     def printMessage(self, from_jid, msg, profile, timestamp=""):
         assert isinstance(from_jid, JID)
         try:
-            jid,nick,mymess = QuickChat.printMessage(self, from_jid, msg, profile, timestamp)
+            jid, nick, mymess = QuickChat.printMessage(self, from_jid, msg, profile, timestamp)
         except TypeError:
             return
 
         new_text = ChatText(self, timestamp or None, nick, mymess, msg)
 
         if timestamp and self.content:
-            for idx in range(len(self.content)-1,-1,-1):
+            for idx in range(len(self.content) - 1, -1, -1):
                 current_text = self.content[idx]
                 if new_text.timestamp < current_text.timestamp and idx > 0:
-                    continue #the new message is older, we need to insert it upper
+                    continue  # the new message is older, we need to insert it upper
 
                 #we discard double messages, to avoid backlog / history conflict
-                if ((idx and self.content[idx-1].message == msg) or
+                if ((idx and self.content[idx - 1].message == msg) or
                     (self.content[idx].message == msg) or
-                    (idx<len(self.content)-2 and self.content[idx+1].message)):
+                    (idx < len(self.content) - 2 and self.content[idx + 1].message)):
                     return
 
-                self.content.insert(idx+1, new_text)
+                self.content.insert(idx + 1, new_text)
                 break
         else:
             self.content.append(new_text)
+        self._notify(from_jid, msg)
 
-        if self.text_list.get_focus()[1] == len(self.content)-2:
-            #we don't change focus if user is not at the bottom
-            #as that mean that he is probably watching discussion history
-            self.text_list.set_focus(len(self.content)-1)
-        self.host.redraw()
-        if not self.host.x_notify.hasFocus():
-            if self.type=="one2one":
-                self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % from_jid)
-            elif self.getUserNick().lower() in msg.lower():
-                self.host.x_notify.sendNotification(_("Primitivus: Somebody pinged your name in %s room") % self.target)
-
-    def printInfo(self, msg, type='normal', timestamp=""):
+    def printInfo(self, msg, type_='normal', timestamp=""):
         """Print general info
         @param msg: message to print
         @type: one of:
             normal: general info like "toto has joined the room"
             me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
         """
-        #FIXME: duplicated code, this must be refactored
         _widget = ChatText(self, timestamp or None, None, False, msg, is_info=True)
         self.content.append(_widget)
-        if self.text_list.get_focus()[1] == len(self.content)-2:
+        self._notify(msg=msg)
+
+    def _notify(self, from_jid="somebody", msg=""):
+        """Notify the user of a new message if primitivus doesn't have the focus.
+        @param from_jid: contact who wrote to the users
+        @param msg: the message that has been received
+        """
+        if self.text_list.get_focus()[1] == len(self.content) - 2:
             #we don't change focus if user is not at the bottom
             #as that mean that he is probably watching discussion history
-            self.text_list.set_focus(len(self.content)-1)
+            self.text_list.set_focus(len(self.content) - 1)
         self.host.redraw()
         if not self.host.x_notify.hasFocus():
-            if self.type=="one2one":
-                self.host.x_notify.sendNotification(_("Primitivus: there is a message about you"))
+            if self.type == "one2one":
+                self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % from_jid)
             elif self.getUserNick().lower() in msg.lower():
-                self.host.x_notify.sendNotification(_("Primitivus: Somebody is talking about you in %s room") % self.target)
+                self.host.x_notify.sendNotification(_("Primitivus: %s mentioned you in room '%s'") % (from_jid, self.target))
 
     def startGame(self, game_type, referee, players):
         """Configure the chat window to start a game"""