From 786ef9640094cd0f8e77ec34a9ce59646757d2ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Sat, 9 Dec 2017 00:15:26 +0100 Subject: [PATCH] Port InformationEvent handling to new interface --- gajim/common/connection.py | 56 ++++++----------- gajim/common/connection_handlers.py | 21 +++---- gajim/common/connection_handlers_events.py | 10 +++ gajim/common/jingle_rtp.py | 8 +-- gajim/common/protocol/bytestream.py | 5 +- gajim/common/zeroconf/connection_zeroconf.py | 5 +- gajim/dialog_messages.py | 66 ++++++++++++++++++++ gajim/gui_interface.py | 26 +++++--- 8 files changed, 127 insertions(+), 70 deletions(-) diff --git a/gajim/common/connection.py b/gajim/common/connection.py index bb4cea81d..3e8c9771a 100644 --- a/gajim/common/connection.py +++ b/gajim/common/connection.py @@ -275,19 +275,15 @@ class CommonConnection: try: self.check_jid(jid) except helpers.InvalidFormat: - app.nec.push_incoming_event(InformationEvent(None, - conn=self, level='error', pri_txt=_('Invalid JID'), - sec_txt=_('It is not possible to send a message ' - 'to %s, this JID is not valid.') % jid)) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='invalid-jid', args=jid)) return else: try: self.check_jid(obj.jid) except helpers.InvalidFormat: - app.nec.push_incoming_event(InformationEvent(None, conn=self, - level='error', pri_txt=_('Invalid JID'), sec_txt=_( - 'It is not possible to send a message to %s, this JID is not ' - 'valid.') % obj.jid)) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='invalid-jid', args=jid)) return if obj.message and not obj.xhtml and app.config.get( @@ -937,11 +933,9 @@ class Connection(CommonConnection, ConnectionHandlers): self.disconnect(on_purpose=True) return if not data[1]: # wrong answer - app.nec.push_incoming_event(InformationEvent(None, - conn=self, level='error', pri_txt=_('Invalid answer'), - sec_txt=_('Transport %(name)s answered wrongly to ' - 'register request: %(error)s') % {'name': data[0], - 'error': data[3]})) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='invalid-answer', + kwargs={'name': data[0], 'error': data[3]})) return is_form = data[2] conf = data[1] @@ -1063,11 +1057,9 @@ class Connection(CommonConnection, ConnectionHandlers): try: helpers.idn_to_ascii(custom_h) except Exception: - app.nec.push_incoming_event(InformationEvent(None, - conn=self, level='error', - pri_txt=_('Wrong Custom Hostname'), - sec_txt='Wrong custom hostname "%s". Ignoring it.' \ - % custom_h)) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='invalid-custom-hostname', + args=custom_h)) use_custom = False # create connection if it doesn't already exist @@ -1584,11 +1576,8 @@ class Connection(CommonConnection, ConnectionHandlers): app.nec.push_incoming_event(PrivacyListRemovedEvent(None, conn=self, list_name=privacy_list)) else: - app.nec.push_incoming_event(InformationEvent(None, conn=self, - level='error', pri_txt=_('Error while removing privacy ' - 'list'), sec_txt=_('Privacy list %s has not been removed. ' - 'It is maybe active in one of your connected resources. ' - 'Deactivate it and try again.') % privacy_list)) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='privacy-list-error', args=privacy_list)) nbxmpp.features_nb.delPrivacyList(self.connection, privacy_list, _on_del_privacy_list_result) @@ -1771,10 +1760,8 @@ class Connection(CommonConnection, ConnectionHandlers): if not self.privacy_rules_supported: app.nec.push_incoming_event(OurShowEvent(None, conn=self, show=app.SHOW_LIST[self.connected])) - app.nec.push_incoming_event(InformationEvent(None, conn=self, - level='error', pri_txt=_('Invisibility not supported'), - sec_txt=_('Account %s doesn\'t support invisibility.') % \ - self.name)) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='invisibility-not-supported', args=self.name)) return # If we are already connected, and privacy rules are supported, send # offline presence first as it's required by XEP-0126 @@ -1896,10 +1883,8 @@ class Connection(CommonConnection, ConnectionHandlers): self.disconnect(on_purpose=True) app.nec.push_incoming_event(OurShowEvent(None, conn=self, show='offline')) - app.nec.push_incoming_event(InformationEvent(None, - conn=self, level='error', pri_txt=_('Invisibility not ' - 'supported'), sec_txt=_('Account %s doesn\'t support ' - 'invisibility.') % self.name)) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='invisibility-not-supported', args=self.name)) return if self.blocking_supported: self._request_blocking() @@ -2917,12 +2902,9 @@ class Connection(CommonConnection, ConnectionHandlers): if result.getID() == id_: on_remove_success(True) return - app.nec.push_incoming_event(InformationEvent(None, - conn=self, level='error', - pri_txt=_('Unregister failed'), - sec_txt=_('Unregistration with server %(server)s ' - 'failed: %(error)s') % {'server': hostname, - 'error': result.getErrorMsg()})) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='unregister-error', + kwargs={'server': hostname, 'error': result.getErrorMsg()})) on_remove_success(False) con.RegisterHandler('iq', _on_answer, 'result', system=True) con.SendAndWaitForResponse(iq) diff --git a/gajim/common/connection_handlers.py b/gajim/common/connection_handlers.py index 5ac99fc48..018045b77 100644 --- a/gajim/common/connection_handlers.py +++ b/gajim/common/connection_handlers.py @@ -118,10 +118,8 @@ class ConnectionDisco: callback() return app.nec.push_incoming_event( - InformationEvent(None, conn=self, - level='error', - pri_txt=_('Unable to join Groupchat'), - sec_txt=error)) + InformationEvent( + None, dialog_name='unable-join-groupchat', args=error)) def request_register_agent_info(self, agent): if not self.connection or self.connected < 2: @@ -138,9 +136,8 @@ class ConnectionDisco: def _agent_registered_cb(self, con, resp, agent): if resp.getType() == 'result': - app.nec.push_incoming_event(InformationEvent(None, conn=self, - level='info', pri_txt=_('Registration succeeded'), sec_txt=_( - 'Registration with agent %s succeeded') % agent)) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='agent-register-success', args=agent)) self.request_subscription(agent, auto_auth=True) self.agent_registrations[agent]['roster_push'] = True if self.agent_registrations[agent]['sub_received']: @@ -148,11 +145,11 @@ class ConnectionDisco: p = self.add_sha(p) self.connection.send(p) if resp.getType() == 'error': - app.nec.push_incoming_event(InformationEvent(None, conn=self, - level='error', pri_txt=_('Registration failed'), sec_txt=_( - 'Registration with agent %(agent)s failed with error %(error)s:' - ' %(error_msg)s') % {'agent': agent, 'error': resp.getError(), - 'error_msg': resp.getErrorMsg()})) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='agent-register-error', + kwargs={'agent': agent, + 'error': resp.getError(), + 'error_msg': resp.getErrorMsg()})) def register_agent(self, agent, info, is_form=False): if not self.connection or self.connected < 2: diff --git a/gajim/common/connection_handlers_events.py b/gajim/common/connection_handlers_events.py index 88c78ffc5..4b97fb637 100644 --- a/gajim/common/connection_handlers_events.py +++ b/gajim/common/connection_handlers_events.py @@ -2905,8 +2905,18 @@ class InformationEvent(nec.NetworkIncomingEvent): base_network_events = [] def init(self): + self.args = None + self.kwargs = {} + self.dialog_name = None self.popup = True + def generate(self): + if self.args is None: + self.args = () + else: + self.args = (self.args,) + return True + class BlockingEvent(nec.NetworkIncomingEvent): name = 'blocking' base_network_events = [] diff --git a/gajim/common/jingle_rtp.py b/gajim/common/jingle_rtp.py index 08bcf0353..cc9c2c0f5 100644 --- a/gajim/common/jingle_rtp.py +++ b/gajim/common/jingle_rtp.py @@ -228,11 +228,9 @@ class JingleRTPContent(JingleContent): if not self.stream_failed_once: app.nec.push_incoming_event( InformationEvent( - None, conn=self.session.connection, level='error', - pri_txt=_('GStreamer error'), - sec_txt=_('Error: %(error)s\nDebug: %(debug)s' % { - 'error': message.get_structure().get_value('gerror'), - 'debug': message.get_structure().get_value('debug')}))) + None, dialog_name='gstreamer-error', + kwargs={'error': message.get_structure().get_value('gerror'), + 'debug': message.get_structure().get_value('debug')})) sink_pad = self.p2psession.get_property('sink-pad') diff --git a/gajim/common/protocol/bytestream.py b/gajim/common/protocol/bytestream.py index 9bf53dab9..24f3bc6f4 100644 --- a/gajim/common/protocol/bytestream.py +++ b/gajim/common/protocol/bytestream.py @@ -401,9 +401,8 @@ class ConnectionSocks5Bytestream(ConnectionBytestream): self._add_streamhosts_to_query(query, sender, port, my_ips) except socket.gaierror: from gajim.common.connection_handlers_events import InformationEvent - app.nec.push_incoming_event(InformationEvent(None, conn=self, - level='error', pri_txt=_('Wrong host'), - sec_txt=_('Invalid local address? :-O'))) + app.nec.push_incoming_event( + InformationEvent(None, dialog_name='wrong-host')), def _add_addiditional_streamhosts_to_query(self, query, file_props): sender = file_props.sender diff --git a/gajim/common/zeroconf/connection_zeroconf.py b/gajim/common/zeroconf/connection_zeroconf.py index 5eeb029c2..a82a9958a 100644 --- a/gajim/common/zeroconf/connection_zeroconf.py +++ b/gajim/common/zeroconf/connection_zeroconf.py @@ -189,9 +189,8 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf): alt_name=alt_name)) def _on_error(self, message): - app.nec.push_incoming_event(InformationEvent(None, conn=self, - level='error', pri_txt=_('Avahi error'), sec_txt=_('%s\nLink-local ' - 'messaging might not work properly.') % message)) + app.nec.push_incoming_event(InformationEvent( + None, dialog_name='avahi-error', args=message)) def connect(self, show='online', msg=''): self.get_config_values_or_default() diff --git a/gajim/dialog_messages.py b/gajim/dialog_messages.py index 5542084a3..a58c91b64 100644 --- a/gajim/dialog_messages.py +++ b/gajim/dialog_messages.py @@ -23,6 +23,7 @@ from gi.repository import GLib from gajim.common.app import app from gajim.dialogs import ErrorDialog +from gajim.dialogs import InformationDialog Message = namedtuple('Message', ['title', 'text', 'dialog']) @@ -37,6 +38,12 @@ messages = { '%s', ErrorDialog), + 'invalid-jid': Message( + _('Invalid JID'), + _('It is not possible to send a message ' + 'to %s, this JID is not valid.'), + ErrorDialog), + 'unread-events-on-remove-account': Message( _('Unread events'), _('Read all pending events before removing this account.'), @@ -67,6 +74,65 @@ messages = { _('The JID you entered is already in the list. Choose another one.'), ErrorDialog), + 'invalid-answer': Message( + _('Invalid answer'), + _('Transport %(name)s answered wrongly to ' + 'register request: %(error)s'), + ErrorDialog), + + 'invalid-custom-hostname': Message( + _('Wrong Custom Hostname'), + _('Wrong custom hostname "%s". Ignoring it.'), + ErrorDialog), + + 'privacy-list-error': Message( + _('Error while removing privacy list'), + _('Privacy list %s has not been removed. ' + 'It is maybe active in one of your connected resources. ' + 'Deactivate it and try again.'), + ErrorDialog), + + 'invisibility-not-supported': Message( + _('Invisibility not supported'), + _('Account %s doesn\'t support invisibility.'), + ErrorDialog), + + 'unregister-error': Message( + _('Unregister failed'), + _('Unregistration with server %(server)s failed: %(error)s'), + ErrorDialog), + + 'agent-register-success': Message( + _('Registration succeeded'), + _('Registration with agent %s succeeded'), + InformationDialog), + + 'agent-register-error': Message( + _('Registration failed'), + _('Registration with agent %(agent)s failed with error %(error)s: ' + '%(error_msg)s'), + ErrorDialog), + + 'unable-join-groupchat': Message( + _('Unable to join Groupchat'), + '%s', + ErrorDialog), + + 'gstreamer-error': Message( + _('GStreamer error'), + _('Error: %(error)s\nDebug: %(debug)s'), + ErrorDialog), + + 'wrong-host': Message( + _('Wrong host'), + _('Invalid local address? :-O'), + ErrorDialog), + + 'avahi-error': Message( + _('Avahi error'), + _('%s\nLink-local messaging might not work properly.'), + ErrorDialog), + } diff --git a/gajim/gui_interface.py b/gajim/gui_interface.py index adddd6a7d..4ef992bb3 100644 --- a/gajim/gui_interface.py +++ b/gajim/gui_interface.py @@ -132,17 +132,23 @@ class Interface: @staticmethod def handle_event_information(obj): - if obj.popup: - if obj.level == 'error': - cls = dialogs.ErrorDialog - elif obj.level == 'warn': - cls = dialogs.WarningDialog - elif obj.level == 'info': - cls = dialogs.InformationDialog - else: - return + if not obj.popup: + return - cls(obj.pri_txt, GLib.markup_escape_text(obj.sec_txt)) + if obj.dialog_name is not None: + get_dialog(obj.dialog_name, *obj.args, **obj.kwargs) + return + + if obj.level == 'error': + cls = dialogs.ErrorDialog + elif obj.level == 'warn': + cls = dialogs.WarningDialog + elif obj.level == 'info': + cls = dialogs.InformationDialog + else: + return + + cls(obj.pri_txt, GLib.markup_escape_text(obj.sec_txt)) @staticmethod def raise_dialog(name, *args, **kwargs):