Commit 078de46b authored by Yann Leboulanger's avatar Yann Leboulanger

use NEC to handle ping events

parent 9d21a708
......@@ -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.
......
......@@ -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)
......
......@@ -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 = []
......@@ -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],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment