primitivus: better chat text insertion in chat window

This commit is contained in:
Goffi 2012-10-21 12:55:47 +02:00
commit 7c6c829ea5

View file

@ -241,15 +241,26 @@ class Chat(urwid.WidgetWrap, QuickChat):
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