From 078de46bf015a0ecf2e0ee931496dcf8450108a6 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Mon, 15 Nov 2010 17:35:19 +0100 Subject: [PATCH] use NEC to handle ping events --- src/chat_control.py | 21 +++++++++++++ src/common/connection.py | 9 ++++-- src/common/connection_handlers_events.py | 12 ++++++++ src/gui_interface.py | 38 ------------------------ 4 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/chat_control.py b/src/chat_control.py index fe586cf1e..0d609f220 100644 --- a/src/chat_control.py +++ b/src/chat_control.py @@ -212,6 +212,21 @@ class ChatControlBase(MessageControl, ChatCommandProcessor, CommandTools): if self.parent_win: self.parent_win.redraw_tab(self) + def _nec_ping_sent(self, obj): + if self.contact != obj.contact: + return + self.print_conversation(_('Ping?'), 'status') + + def _nec_ping_reply(self, obj): + if self.contact != obj.contact: + return + self.print_conversation(_('Pong! (%s s.)') % obj.seconds, 'status') + + def _nec_ping_error(self, obj): + if self.contact != obj.contact: + return + self.print_conversation(_('Error.'), 'status') + def handle_message_textview_mykey_press(self, widget, event_keyval, event_keymod): """ @@ -446,6 +461,12 @@ class ChatControlBase(MessageControl, ChatCommandProcessor, CommandTools): gajim.ged.register_event_handler('our-show', ged.GUI1, self._nec_our_status) + gajim.ged.register_event_handler('ping-sent', ged.GUI1, + self._nec_ping_sent) + gajim.ged.register_event_handler('ping-reply', ged.GUI1, + self._nec_ping_reply) + gajim.ged.register_event_handler('ping-error', ged.GUI1, + self._nec_ping_error) # This is bascially a very nasty hack to surpass the inability # to properly use the super, because of the old code. diff --git a/src/common/connection.py b/src/common/connection.py index 539511925..9d9b5958e 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -1366,7 +1366,8 @@ class Connection(CommonConnection, ConnectionHandlers): id_ = self.connection.getAnID() if pingTo: to = pingTo.get_full_jid() - self.dispatch('PING_SENT', (pingTo)) + gajim.nec.push_incoming_event(PingSentEvent(None, conn=self, + contact=pingTo)) else: to = gajim.config.get_per('accounts', self.name, 'hostname') self.awaiting_xmpp_ping_id = id_ @@ -1376,10 +1377,12 @@ class Connection(CommonConnection, ConnectionHandlers): def _on_response(resp): timePong = time_time() if not common.xmpp.isResultNode(resp): - self.dispatch('PING_ERROR', (pingTo)) + gajim.nec.push_incoming_event(PingErrorEvent(None, conn=self, + contact=pingTo)) return timeDiff = round(timePong - timePing, 2) - self.dispatch('PING_REPLY', (pingTo, timeDiff)) + gajim.nec.push_incoming_event(PingReplyEvent(None, conn=self, + contact=pingTo, seconds=timeDiff)) if pingTo: timePing = time_time() self.connection.SendAndCallForResponse(iq, _on_response) diff --git a/src/common/connection_handlers_events.py b/src/common/connection_handlers_events.py index 920f2e273..4a00c2cad 100644 --- a/src/common/connection_handlers_events.py +++ b/src/common/connection_handlers_events.py @@ -1322,3 +1322,15 @@ class ConnectionLostEvent(nec.NetworkIncomingEvent): gajim.nec.push_incoming_event(OurShowEvent(None, conn=self.conn, show='offline')) return True + +class PingSentEvent(nec.NetworkIncomingEvent): + name = 'ping-sent' + base_network_events = [] + +class PingReplyEvent(nec.NetworkIncomingEvent): + name = 'ping-reply' + base_network_events = [] + +class PingErrorEvent(nec.NetworkIncomingEvent): + name = 'ping-error' + base_network_events = [] diff --git a/src/gui_interface.py b/src/gui_interface.py index 34751b57e..d10aa8c9f 100644 --- a/src/gui_interface.py +++ b/src/gui_interface.py @@ -1368,41 +1368,6 @@ class Interface: input_str=data, is_modal=True, ok_handler=on_ok, cancel_handler=on_cancel) - def handle_event_ping_sent(self, account, contact): - if contact.jid == contact.get_full_jid(): - # If contact is a groupchat user - jids = [contact.jid] - else: - jids = [contact.jid, contact.get_full_jid()] - for jid in jids: - ctrl = self.msg_win_mgr.get_control(jid, account) - if ctrl: - ctrl.print_conversation(_('Ping?'), 'status') - - def handle_event_ping_reply(self, account, data): - contact = data[0] - seconds = data[1] - if contact.jid == contact.get_full_jid(): - # If contact is a groupchat user - jids = [contact.jid] - else: - jids = [contact.jid, contact.get_full_jid()] - for jid in jids: - ctrl = self.msg_win_mgr.get_control(jid, account) - if ctrl: - ctrl.print_conversation(_('Pong! (%s s.)') % seconds, 'status') - - def handle_event_ping_error(self, account, contact): - if contact.jid == contact.get_full_jid(): - # If contact is a groupchat user - jids = [contact.jid] - else: - jids = [contact.jid, contact.get_full_jid()] - for jid in jids: - ctrl = self.msg_win_mgr.get_control(jid, account) - if ctrl: - ctrl.print_conversation(_('Error.'), 'status') - def handle_event_resource_conflict(self, obj): # ('RESOURCE_CONFLICT', account, ()) # First we go offline, but we don't overwrite status message @@ -1768,9 +1733,6 @@ class Interface: [self.handle_event_privacy_lists_active_default], 'PRIVACY_LIST_REMOVED': [self.handle_event_privacy_list_removed], 'ZC_NAME_CONFLICT': [self.handle_event_zc_name_conflict], - 'PING_SENT': [self.handle_event_ping_sent], - 'PING_REPLY': [self.handle_event_ping_reply], - 'PING_ERROR': [self.handle_event_ping_error], 'PEP_CONFIG': [self.handle_event_pep_config], 'UNIQUE_ROOM_ID_UNSUPPORTED': \ [self.handle_event_unique_room_id_unsupported],