From fe3c1b4fbdb206c1d9fac096d37dffce0f394f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Thu, 5 Jul 2018 20:20:22 +0200 Subject: [PATCH] Refactor UserNickname into own module --- gajim/chat_control.py | 2 +- gajim/common/connection.py | 3 +- gajim/common/connection_handlers.py | 47 +---------- gajim/common/const.py | 1 + gajim/common/modules/pep.py | 4 + gajim/common/modules/user_nickname.py | 78 +++++++++++++++++++ gajim/common/pep.py | 32 +------- .../zeroconf/connection_handlers_zeroconf.py | 2 +- gajim/profile_window.py | 4 +- gajim/roster_window.py | 2 +- 10 files changed, 92 insertions(+), 83 deletions(-) create mode 100644 gajim/common/modules/user_nickname.py diff --git a/gajim/chat_control.py b/gajim/chat_control.py index 0a9b9dd11..c4c56521d 100644 --- a/gajim/chat_control.py +++ b/gajim/chat_control.py @@ -422,7 +422,7 @@ class ChatControl(ChatControlBase): if obj.jid != self.contact.jid: return - if obj.pep_type == 'nickname': + if obj.pep_type == 'nick': self.update_ui() self.parent_win.redraw_tab(self) self.parent_win.show_title() diff --git a/gajim/common/connection.py b/gajim/common/connection.py index 1b69f49d8..a01741377 100644 --- a/gajim/common/connection.py +++ b/gajim/common/connection.py @@ -82,6 +82,7 @@ from gajim.common.modules.user_activity import UserActivity from gajim.common.modules.user_tune import UserTune from gajim.common.modules.user_mood import UserMood from gajim.common.modules.user_location import UserLocation +from gajim.common.modules.user_nickname import UserNickname from gajim.common.connection_handlers import * from gajim.common.contacts import GC_Contact from gajim.gtkgui_helpers import get_action @@ -680,6 +681,7 @@ class Connection(CommonConnection, ConnectionHandlers): self.register_module('UserTune', UserTune, self) self.register_module('UserMood', UserMood, self) self.register_module('UserLocation', UserLocation, self) + self.register_module('UserNickname', UserNickname, self) app.ged.register_event_handler('privacy-list-received', ged.CORE, self._nec_privacy_list_received) @@ -765,7 +767,6 @@ class Connection(CommonConnection, ConnectionHandlers): # We are doing disconnect at so many places, better use one function in all def disconnect(self, on_purpose=False): app.interface.music_track_changed(None, None, self.name) - self.reset_awaiting_pep() self.get_module('PEP').reset_stored_publish() self.on_purpose = on_purpose self.connected = 0 diff --git a/gajim/common/connection_handlers.py b/gajim/common/connection_handlers.py index 631587d71..8b0a5943e 100644 --- a/gajim/common/connection_handlers.py +++ b/gajim/common/connection_handlers.py @@ -281,48 +281,6 @@ class ConnectionDisco: app.nec.push_incoming_event(AgentInfoReceivedEvent(None, conn=self, stanza=iq_obj)) - -class ConnectionPEP(object): - - def __init__(self, account, dispatcher, pubsub_connection): - self._account = account - self._dispatcher = dispatcher - self._pubsub_connection = pubsub_connection - self.reset_awaiting_pep() - - def pep_change_account_name(self, new_name): - self._account = new_name - - def reset_awaiting_pep(self): - self.to_be_sent_nick = None - - def send_awaiting_pep(self): - """ - Send pep info that were waiting for connection - """ - if self.to_be_sent_nick: - self.send_nick(self.to_be_sent_nick) - self.reset_awaiting_pep() - - def send_nickname(self, nick): - if self.connected == 1: - # We are connecting, keep nick in mem and send it when we'll be - # connected - self.to_be_sent_nick = nick - return - if not self.pep_supported: - return - item = nbxmpp.Node('nick', {'xmlns': nbxmpp.NS_NICK}) - item.addData(nick) - self.get_module('PubSub').send_pb_publish('', nbxmpp.NS_NICK, item, '0') - - def retract_nickname(self): - if not self.pep_supported: - return - - self.get_module('PubSub').send_pb_retract('', nbxmpp.NS_NICK, '0') - - # basic connection handlers used here and in zeroconf class ConnectionHandlersBase: def __init__(self): @@ -790,7 +748,7 @@ class ConnectionHandlersBase: class ConnectionHandlers(ConnectionArchive313, ConnectionSocks5Bytestream, ConnectionDisco, -ConnectionCommands, ConnectionPEP, ConnectionCaps, +ConnectionCommands, ConnectionCaps, ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream, ConnectionHTTPUpload): def __init__(self): @@ -798,8 +756,6 @@ ConnectionHTTPUpload): ConnectionSocks5Bytestream.__init__(self) ConnectionIBBytestream.__init__(self) ConnectionCommands.__init__(self) - ConnectionPEP.__init__(self, account=self.name, dispatcher=self, - pubsub_connection=self) ConnectionHTTPUpload.__init__(self) # Handle presences BEFORE caps @@ -1395,7 +1351,6 @@ ConnectionHTTPUpload): # Inform GUI we just signed in app.nec.push_incoming_event(SignedInEvent(None, conn=self)) - self.send_awaiting_pep() self.get_module('PEP').send_stored_publish() self.continue_connect_info = None diff --git a/gajim/common/const.py b/gajim/common/const.py index 5397bded9..df9f1213d 100644 --- a/gajim/common/const.py +++ b/gajim/common/const.py @@ -151,6 +151,7 @@ class PEPEventType(IntEnum): TUNE = 1 MOOD = 2 LOCATION = 3 + NICKNAME = 4 ACTIVITIES = { diff --git a/gajim/common/modules/pep.py b/gajim/common/modules/pep.py index 496254963..520f7331e 100644 --- a/gajim/common/modules/pep.py +++ b/gajim/common/modules/pep.py @@ -195,6 +195,10 @@ class AbstractPEPData: type_ = PEPEventType + def asMarkupText(self): + '''SHOULD be implemented by subclasses''' + return '' + def __eq__(self, other): return other == self.type_ diff --git a/gajim/common/modules/user_nickname.py b/gajim/common/modules/user_nickname.py new file mode 100644 index 000000000..7740fc0c8 --- /dev/null +++ b/gajim/common/modules/user_nickname.py @@ -0,0 +1,78 @@ +# This file is part of Gajim. +# +# Gajim is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation; version 3 only. +# +# Gajim is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Gajim. If not, see . + +# XEP-0172: User Nickname + +import logging + +import nbxmpp + +from gajim.common import app +from gajim.common.const import PEPEventType +from gajim.common.exceptions import StanzaMalformed +from gajim.common.modules.pep import AbstractPEPModule, AbstractPEPData + +log = logging.getLogger('gajim.c.m.user_nickname') + + +class UserNicknameData(AbstractPEPData): + + type_ = PEPEventType.NICKNAME + + def __init__(self, nickname): + self._pep_specific_data = nickname + + def get_nick(self): + return self._pep_specific_data or '' + + +class UserNickname(AbstractPEPModule): + + name = 'nick' + namespace = nbxmpp.NS_NICK + pep_class = UserNicknameData + store_publish = True + _log = log + + def __init__(self, con): + AbstractPEPModule.__init__(self, con, con.name) + + self.handlers = [] + + def _extract_info(self, item): + nick = '' + child = item.getTag('nick', namespace=nbxmpp.NS_NICK) + if child is None: + raise StanzaMalformed('No nick node') + nick = child.getData() + + return nick or None + + def _build_node(self, data): + item = nbxmpp.Node('nick', {'xmlns': nbxmpp.NS_NICK}) + if data is None: + return + item.addData(data) + return item + + def _update_contacts(self, jid, user_pep): + for contact in app.contacts.get_contacts(self._account, str(jid)): + contact.contact_name = user_pep.get_nick() + + if jid == self._con.get_own_jid().getStripped(): + if user_pep: + app.nicks[self._account] = user_pep.get_nick() + else: + app.nicks[self._account] = app.config.get_per( + 'accounts', self._account, 'name') diff --git a/gajim/common/pep.py b/gajim/common/pep.py index d64a295d7..a7fbf9cdb 100644 --- a/gajim/common/pep.py +++ b/gajim/common/pep.py @@ -26,7 +26,6 @@ import logging log = logging.getLogger('gajim.c.pep') -import nbxmpp from gajim.common import app @@ -81,35 +80,6 @@ class AbstractPEP(object): pass -class UserNicknamePEP(AbstractPEP): - '''XEP-0172: User Nickname''' - - type_ = 'nickname' - namespace = nbxmpp.NS_NICK - - def _extract_info(self, items): - nick = '' - for item in items.getTags('item'): - child = item.getTag('nick') - if child: - nick = child.getData() - break - - retracted = items.getTag('retract') or not nick - return (nick, retracted) - - def _update_contacts(self, jid, account): - nick = '' if self._retracted else self._pep_specific_data - for contact in app.contacts.get_contacts(account, jid): - contact.contact_name = nick - - def _update_account(self, account): - if self._retracted: - app.nicks[account] = app.config.get_per('accounts', account, 'name') - else: - app.nicks[account] = self._pep_specific_data - - class AvatarNotificationPEP(AbstractPEP): '''XEP-0084: Avatars''' @@ -154,4 +124,4 @@ class AvatarNotificationPEP(AbstractPEP): SUPPORTED_PERSONAL_USER_EVENTS = [ - UserNicknamePEP, AvatarNotificationPEP] + AvatarNotificationPEP] diff --git a/gajim/common/zeroconf/connection_handlers_zeroconf.py b/gajim/common/zeroconf/connection_handlers_zeroconf.py index f67a75293..9806b1c9e 100644 --- a/gajim/common/zeroconf/connection_handlers_zeroconf.py +++ b/gajim/common/zeroconf/connection_handlers_zeroconf.py @@ -50,7 +50,7 @@ class ConnectionVcard: class ConnectionHandlersZeroconf(ConnectionVcard, ConnectionSocks5BytestreamZeroconf, ConnectionCommands, -connection_handlers.ConnectionPEP, connection_handlers.ConnectionHandlersBase, +connection_handlers.ConnectionHandlersBase, connection_handlers.ConnectionJingle): def __init__(self): ConnectionVcard.__init__(self) diff --git a/gajim/profile_window.py b/gajim/profile_window.py index 08b2a7b3f..ca6a595a8 100644 --- a/gajim/profile_window.py +++ b/gajim/profile_window.py @@ -329,9 +329,9 @@ class ProfileWindow(Gtk.ApplicationWindow): nick = '' if 'NICKNAME' in vcard_: nick = vcard_['NICKNAME'] - app.connections[self.account].send_nickname(nick) + app.connections[self.account].get_module('UserNickname').send(nick) if nick == '': - app.connections[self.account].retract_nickname() + app.connections[self.account].get_module('UserNickname').retract() nick = app.config.get_per('accounts', self.account, 'name') app.nicks[self.account] = nick app.connections[self.account].get_module('VCardTemp').send_vcard( diff --git a/gajim/roster_window.py b/gajim/roster_window.py index 4876f2fbc..b37de2b01 100644 --- a/gajim/roster_window.py +++ b/gajim/roster_window.py @@ -2651,7 +2651,7 @@ class RosterWindow: if obj.jid == app.get_jid_from_account(obj.conn.name): self.draw_account(obj.conn.name) - if obj.pep_type == 'nickname': + if obj.pep_type == 'nick': self.draw_contact(obj.jid, obj.conn.name) else: self.draw_pep(obj.jid, obj.conn.name, obj.pep_type)