diff frontends/src/quick_frontend/quick_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 c74015dc2785
children d047535e3ed5
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_chat.py	Tue Dec 15 17:43:36 2015 +0100
+++ b/frontends/src/quick_frontend/quick_chat.py	Sat Dec 12 12:18:54 2015 +0100
@@ -140,20 +140,21 @@
             log_msg += _(u" (%d messages)" % size)
         log.debug(log_msg)
 
+        target = self.target.bare
+
         def onHistory(history):
             for line in history:
-                timestamp, from_jid, to_jid, message, type_, extra = line # FIXME: extra is unused !
+                timestamp, from_jid, to_jid, message, type_, extra = line  # FIXME: extra is unused !
                 if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or
                    (self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)):
                     continue
-                self.printMessage(jid.JID(from_jid), message, {'timestamp':timestamp}, profile)
+                extra["timestamp"] = timestamp
+                self.newMessage(jid.JID(from_jid), target, message, type_, extra, profile)
             self.afterHistoryPrint()
 
         def onHistoryError(err):
             log.error(_("Can't get history"))
 
-        target = self.target.bare
-
         self.host.bridge.getHistory(unicode(self.host.profiles[profile].whoami.bare), unicode(target), size, True, search, profile, callback=onHistory, errback=onHistoryError)
 
     def _get_nick(self, entity):
@@ -181,27 +182,39 @@
             # we have a private message, we forward it to a private conversation widget
             chat_widget = self.getOrCreatePrivateWidget(target)
             chat_widget.newMessage(from_jid, target, msg, type_, extra, profile)
+            return
+        if type_ == C.MESS_TYPE_INFO:
+            self.printInfo(msg, extra=extra)
         else:
-            if type_ == C.MESS_TYPE_INFO:
-                self.printInfo(msg, extra=extra)
+            nick = self._get_nick(from_jid)
+            if msg.startswith('/me '):
+                self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', extra=extra)
             else:
-                self.printMessage(from_jid, msg, extra, profile)
+                # my_message is True if message comes from local user
+                my_message = (from_jid.resource == self.nick) if self.type == C.CHAT_GROUP else (from_jid.bare == self.host.profiles[profile].whoami.bare)
+                try:
+                    timestamp = float(extra['timestamp'])
+                except KeyError:
+                    timestamp = None
+                self.printMessage(nick, my_message, msg, timestamp, extra, profile)
 
-    def printMessage(self, from_jid, msg, extra=None, profile=C.PROF_KEY_NONE):
-        """Print message in chat window. Must be implemented by child class"""
-        nick = self._get_nick(from_jid)
-        mymess = (from_jid.resource == self.nick) if self.type == C.CHAT_GROUP else (from_jid.bare == self.host.profiles[profile].whoami.bare) #mymess = True if message comes from local user
-        if msg.startswith('/me '):
-            self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', extra=extra)
-            return
-        return nick, mymess
+    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
+        """
+        raise NotImplementedError
 
     def printInfo(self, msg, type_='normal', extra=None):
-        """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"
+        """Print general info.
+
+        @param msg (unicode): message to print
+        @param type_ (unicode):
+            - 'normal': general info like "toto has joined the room"
+            - 'me': "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
         @param extra (dict): message data
         """
         raise NotImplementedError