Commit fe3c1b4f authored by Philipp Hörist's avatar Philipp Hörist

Refactor UserNickname into own module

parent b04f9af4
......@@ -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()
......
......@@ -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
......
......@@ -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
......
......@@ -151,6 +151,7 @@ class PEPEventType(IntEnum):
TUNE = 1
MOOD = 2
LOCATION = 3
NICKNAME = 4
ACTIVITIES = {
......
......@@ -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_
......
# 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 <http://www.gnu.org/licenses/>.
# 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')
......@@ -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]
......@@ -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)
......
......@@ -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(
......
......@@ -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)
......
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