diff frontends/src/primitivus/chat.py @ 1748:3a6cd1c14974

frontends: small message refactorisation: - factorize some treatments that were done both in Primitivus and Libervia - the way it was done, especially with QuickChat.printMessage returning a tuple, was quite disturbing - FIXME: we can probably remove some arguments and do a deeper factorization e.g with QuickChatText
author souliane <souliane@mailoo.org>
date Sat, 12 Dec 2015 12:18:54 +0100
parents 7751b5a51586
children 77870d2e2902
line wrap: on
line diff
--- a/frontends/src/primitivus/chat.py	Tue Dec 15 17:43:36 2015 +0100
+++ b/frontends/src/primitivus/chat.py	Sat Dec 12 12:18:54 2015 +0100
@@ -297,21 +297,15 @@
     def onPrivateCreated(self, widget):
         self.host.contact_lists[widget.profile].specialResourceVisible(widget.target)
 
-    def printMessage(self, from_jid, msg, extra=None, profile=C.PROF_KEY_NONE):
-        assert isinstance(from_jid, jid.JID)
-        if extra is None:
-            extra = {}
-        try:
-            timestamp = float(extra['timestamp'])
-        except KeyError:
-            timestamp = None
-        try:
-            nick, mymess = QuickChat.printMessage(self, from_jid, msg, extra, profile)
-        except TypeError:
-            # None is returned, the message is managed
-            return
-        new_text = ChatText(self, timestamp, nick, mymess, msg)
+    def printMessage(self, nick, my_message, message, timestamp, extra=None, profile=C.PROF_KEY_NONE):
+        """Print message in chat window.
 
+        @param nick (unicode): author nick
+        @param my_message (boolean): True if profile is the author
+        @param message (unicode): message content
+        @param extra (dict): extra data
+        """
+        new_text = ChatText(self, timestamp, nick, my_message, message)
         if timestamp and self.content:
             for idx in range(len(self.content) - 1, -1, -1):
                 current_text = self.content[idx]
@@ -321,8 +315,8 @@
 
                 # we discard double messages, to avoid backlog / history conflict
                 # FIXME: messages that have been sent several times will be displayed only once
-                if ((idx and self.content[idx - 1].message == msg) or
-                    (self.content[idx].message == msg) or
+                if ((idx and self.content[idx - 1].message == message) or
+                    (self.content[idx].message == message) or
                     (idx < len(self.content) - 2 and self.content[idx + 1].message)):
                     return
 
@@ -334,7 +328,7 @@
             # XXX: do not send notifications for each line of the history being displayed
             # FIXME: this must be changed in the future if the timestamp is passed with
             # all messages and not only with the messages coming from the history.
-            self._notify(from_jid, msg)
+            self._notify(nick, message)
 
     def printInfo(self, msg, type_='normal', extra=None):
         """Print general info
@@ -354,10 +348,11 @@
         self.content.append(_widget)
         self._notify(msg=msg)
 
-    def _notify(self, from_jid="somebody", msg=""):
+    def _notify(self, contact="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
+
+        @param contact (unicode): contact who wrote to the users
+        @param msg (unicode): the message that has been received
         """
         if msg == "":
             return
@@ -368,9 +363,9 @@
         self.host.redraw()
         if not self.host.x_notify.hasFocus():
             if self.type == C.CHAT_ONE2ONE:
-                self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % from_jid)
+                self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % contact)
             elif self.nick is not None and self.nick.lower() in msg.lower():
-                self.host.x_notify.sendNotification(_("Primitivus: %(user)s mentioned you in room '%(room)s'") % {'user': from_jid, 'room': self.target})
+                self.host.x_notify.sendNotification(_("Primitivus: %(user)s mentioned you in room '%(room)s'") % {'user': contact, 'room': self.target})
 
     # MENU EVENTS #
     def onTarotRequest(self, menu):