diff frontends/src/primitivus/chat.py @ 520:4d7248f4c577

primitivus: better chat text insertion in chat window
author Goffi <goffi@goffi.org>
date Sun, 21 Oct 2012 12:55:47 +0200
parents 75216d94a89d
children 3f8c2a0f20e5
line wrap: on
line diff
--- a/frontends/src/primitivus/chat.py	Sun Oct 21 12:55:27 2012 +0200
+++ b/frontends/src/primitivus/chat.py	Sun Oct 21 12:55:47 2012 +0200
@@ -241,15 +241,26 @@
             jid,nick,mymess = QuickChat.printMessage(self, from_jid, msg, profile, timestamp)
         except TypeError:
             return
-        if timestamp:
-            for _chat_text in self.content:
-                if (msg == _chat_text.message and
-                    timestamp - time.mktime(_chat_text.timestamp) < 5):
-                    #we discard double messages, to avoid backlog / history conflict
+
+        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):
+                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
+                
+                #we discard double messages, to avoid backlog / history conflict
+                if idx and self.content[idx-1].message == msg:
                     return
-        self.content.append(ChatText(self, timestamp or None, nick, mymess, msg))
-        if timestamp:
-            self.content.sort(key=lambda chat_text: chat_text.timestamp)
+                if idx<len(self.content)-2 and self.content[idx+1].message == msg:
+                    return
+
+                self.content.insert(idx+1, new_text)
+                break
+        else:
+            self.content.append(new_text)
+        
         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