From 88da6869b4662520fe2181a69cbc9d96e50ea1de Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 12 Oct 2012 15:29:45 +0000 Subject: When a message is corrected, display it correctly in place of the previous one. --- src/core.py | 19 +++++++++++++++---- src/tabs.py | 26 ++++++++++++++++++++------ src/text_buffer.py | 20 +++++++++++++++++--- 3 files changed, 52 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/core.py b/src/core.py index 71e66175..fd212f88 100644 --- a/src/core.py +++ b/src/core.py @@ -2348,7 +2348,11 @@ class Core(object): else: delayed = False date = None - conversation._text_buffer.add_message(body, date, nickname=remote_nick, nick_color=get_theme().COLOR_REMOTE_USER, history=delayed) + replaced_id = message['replace']['id'] + if replaced_id is not '': + conversation.modify_message(body, replaced_id, message['id']) + else: + conversation._text_buffer.add_message(body, date, nickname=remote_nick, nick_color=get_theme().COLOR_REMOTE_USER, history=delayed) if conversation.remote_wants_chatstates is None and not delayed: if message['chat_state']: conversation.remote_wants_chatstates = True @@ -2400,7 +2404,10 @@ class Core(object): body = xhtml.get_body_from_message_stanza(message) if body: date = date if delayed == True else None - if tab.add_message(body, date, nick_from, history=True if date else False): + replaced_id = message['replace']['id'] + if replaced_id is not '': + tab.modify_message(body, replaced_id, message['id']) + elif tab.add_message(body, date, nick_from, history=True if date else False, identifier=message['id']): self.events.trigger('highlight', message, tab) if tab is self.current_tab(): tab.text_win.refresh() @@ -2438,8 +2445,12 @@ class Core(object): self.events.trigger('private_msg', message, tab) if not body or not tab: return - tab.add_message(body, time=None, nickname=nick_from, - forced_user=self.get_tab_by_name(room_from, tabs.MucTab).get_user_by_name(nick_from)) + replaced_id = message['replace']['id'] + if replaced_id is not '': + tab.modify_message(body, replaced_id, message['id']) + else: + tab.add_message(body, time=None, nickname=nick_from, + forced_user=self.get_tab_by_name(room_from, tabs.MucTab).get_user_by_name(nick_from)) conversation = self.get_tab_by_name(jid.full, tabs.PrivateTab) if conversation and conversation.remote_wants_chatstates is None: if message['chat_state']: diff --git a/src/tabs.py b/src/tabs.py index 2895b784..beb98cc9 100644 --- a/src/tabs.py +++ b/src/tabs.py @@ -1564,7 +1564,11 @@ class MucTab(ChatTab): return user return None - def add_message(self, txt, time=None, nickname=None, forced_user=None, nick_color=None, history=None): + def modify_message(self, txt, old_id, new_id): + self._text_buffer.modify_message(txt, old_id, new_id) + self.core.refresh_window() + + def add_message(self, txt, time=None, nickname=None, forced_user=None, nick_color=None, history=None, identifier=None): """ Note that user can be None even if nickname is not None. It happens when we receive an history message said by someone who is not @@ -1591,7 +1595,7 @@ class MucTab(ChatTab): if highlight: nick_color = highlight time = time or datetime.now() - self._text_buffer.add_message(txt, time, nickname, nick_color, history, user, highlight=highlight) + self._text_buffer.add_message(txt, time, nickname, nick_color, history, user, highlight=highlight, identifier=identifier) return highlight class PrivateTab(ChatTab): @@ -1869,12 +1873,17 @@ class PrivateTab(ChatTab): if reason: self.add_message(txt=reason) - def add_message(self, txt, time=None, nickname=None, forced_user=None, nick_color=None): + def modify_message(self, txt, old_id, new_id): + self._text_buffer.modify_message(txt, old_id, new_id) + self.core.refresh_window() + + def add_message(self, txt, time=None, nickname=None, forced_user=None, nick_color=None, identifier=None): self._text_buffer.add_message(txt, time=time, nickname=nickname, nick_color=nick_color, history=None, - user=forced_user) + user=forced_user, + identifier=identifier) class RosterInfoTab(Tab): """ @@ -2958,12 +2967,17 @@ class ConversationTab(ChatTab): if config.get_by_tabname('send_chat_states', 'true', self.general_jid, True) == 'true': self.send_chat_state('gone') - def add_message(self, txt, time=None, nickname=None, forced_user=None, nick_color=None): + def modify_message(self, txt, old_id, new_id): + self._text_buffer.modify_message(txt, old_id, new_id) + self.core.refresh_window() + + def add_message(self, txt, time=None, nickname=None, forced_user=None, nick_color=None, identifier=None): self._text_buffer.add_message(txt, time=time, nickname=nickname, nick_color=nick_color, history=None, - user=forced_user) + user=forced_user, + identifier=identifier) class MucListTab(Tab): diff --git a/src/text_buffer.py b/src/text_buffer.py index b615e96c..1dc042c6 100644 --- a/src/text_buffer.py +++ b/src/text_buffer.py @@ -18,7 +18,7 @@ from datetime import datetime from config import config from theming import get_theme -Message = collections.namedtuple('Message', 'txt nick_color time str_time nickname user') +Message = collections.namedtuple('Message', 'txt nick_color time str_time nickname user identifier') class TextBuffer(object): """ @@ -35,7 +35,7 @@ class TextBuffer(object): def add_window(self, win): self.windows.append(win) - def add_message(self, txt, time=None, nickname=None, nick_color=None, history=None, user=None, highlight=False): + def make_message(self, txt, time, nickname, nick_color, history, user, identifier): time = time or datetime.now() if txt.startswith('/me '): if nick_color: @@ -50,7 +50,12 @@ class TextBuffer(object): msg = Message(txt='%s\x19o'%(txt.replace('\t', ' '),), nick_color=nick_color, time=time, str_time=time.strftime("%Y-%m-%d %H:%M:%S")\ if history else time.strftime("%H:%M:%S"),\ - nickname=nickname, user=user) + nickname=nickname, user=user, identifier=identifier) + log.debug('Set message %s with %s.' % (identifier, msg)) + return msg + + def add_message(self, txt, time=None, nickname=None, nick_color=None, history=None, user=None, highlight=False, identifier=None): + msg = self.make_message(txt, time, nickname, nick_color, history, user, identifier) self.messages.append(msg) while len(self.messages) > self.messages_nb_limit: self.messages.pop(0) @@ -64,6 +69,15 @@ class TextBuffer(object): window.scroll_up(nb) return ret_val or 1 + def modify_message(self, txt, old_id, new_id): + for i, msg in enumerate(self.messages): + if msg.identifier == old_id: + message = self.make_message(txt, msg.time, msg.nickname, msg.nick_color, None, msg.user, new_id) + self.messages[i] = message + log.debug('Replacing message %s with %s.' % (old_id, new_id)) + return + log.debug('Message %s not found in text_buffer, abort replacement.' % (identifier)) + def del_window(self, win): self.windows.remove(win) -- cgit v1.2.3