gajim-plural/gajim/common/modules/user_avatar.py

83 lines
2.8 KiB
Python

# 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-0084: User Avatar
import logging
import nbxmpp
from gajim.common import app
from gajim.common.modules.base import BaseModule
from gajim.common.modules.util import event_node
log = logging.getLogger('gajim.c.m.user_avatar')
class UserAvatar(BaseModule):
_nbxmpp_extends = 'UserAvatar'
_nbxmpp_methods = [
'request_avatar'
]
def __init__(self, con):
BaseModule.__init__(self, con)
self._register_pubsub_handler(self._avatar_metadata_received)
@event_node(nbxmpp.NS_AVATAR_METADATA)
def _avatar_metadata_received(self, _con, _stanza, properties):
data = properties.pubsub_event.data
empty = properties.pubsub_event.empty
jid = str(properties.jid)
own_jid = self._con.get_own_jid().getBare()
if empty:
# Remove avatar
log.info('Remove: %s', jid)
app.contacts.set_avatar(self._account, jid, None)
app.logger.set_avatar_sha(own_jid, jid, None)
app.interface.update_avatar(self._account, jid)
else:
if properties.is_self_message:
sha = app.config.get_per(
'accounts', self._account, 'avatar_sha')
else:
sha = app.contacts.get_avatar_sha(self._account, jid)
if sha == data.id:
log.info('Avatar already known: %s %s', jid, data.id)
return
log.info('Request: %s %s', jid, data.id)
self._nbxmpp('UserAvatar').request_avatar(
jid, data.id, callback=self._avatar_received)
def _avatar_received(self, result):
log.info('Received Avatar: %s %s', result.jid, result.sha)
app.interface.save_avatar(result.data)
if self._con.get_own_jid().bareMatch(result.jid):
app.config.set_per('accounts', self._account, 'avatar_sha', result.sha)
else:
own_jid = self._con.get_own_jid().getBare()
app.logger.set_avatar_sha(own_jid, str(result.jid), result.sha)
app.contacts.set_avatar(self._account, str(result.jid), result.sha)
app.interface.update_avatar(self._account, str(result.jid))
def get_instance(*args, **kwargs):
return UserAvatar(*args, **kwargs), 'UserAvatar'