Refactor annotations code into own module
This commit is contained in:
parent
a0bc6f0155
commit
c39da015cc
|
@ -67,6 +67,7 @@ from gajim.common.modules.entity_time import EntityTime
|
|||
from gajim.common.modules.software_version import SoftwareVersion
|
||||
from gajim.common.modules.ping import Ping
|
||||
from gajim.common.modules.search import Search
|
||||
from gajim.common.modules.annotations import Annotations
|
||||
from gajim.common.connection_handlers import *
|
||||
from gajim.common.contacts import GC_Contact
|
||||
from gajim.gtkgui_helpers import get_action
|
||||
|
@ -627,7 +628,6 @@ class Connection(CommonConnection, ConnectionHandlers):
|
|||
self.last_time_to_reconnect = None
|
||||
self.new_account_info = None
|
||||
self.new_account_form = None
|
||||
self.annotations = {}
|
||||
self.last_io = app.idlequeue.current_time()
|
||||
self.last_sent = []
|
||||
self.password = passwords.get_password(name)
|
||||
|
@ -665,6 +665,7 @@ class Connection(CommonConnection, ConnectionHandlers):
|
|||
self.register_module('SoftwareVersion', SoftwareVersion, self)
|
||||
self.register_module('Ping', Ping, self)
|
||||
self.register_module('Search', Search, self)
|
||||
self.register_module('Annotations', Annotations, self)
|
||||
|
||||
app.ged.register_event_handler('privacy-list-received', ged.CORE,
|
||||
self._nec_privacy_list_received)
|
||||
|
@ -1773,7 +1774,7 @@ class Connection(CommonConnection, ConnectionHandlers):
|
|||
self.get_bookmarks()
|
||||
|
||||
# Get annotations
|
||||
self.get_annotations()
|
||||
self.get_module('Annotations').get_annotations()
|
||||
|
||||
# Inform GUI we just signed in
|
||||
app.nec.push_incoming_event(SignedInEvent(None, conn=self))
|
||||
|
@ -2380,34 +2381,6 @@ class Connection(CommonConnection, ConnectionHandlers):
|
|||
self.connection.send(iq)
|
||||
app.log('bookmarks').info('Bookmarks published (PrivateStorage)')
|
||||
|
||||
def get_annotations(self):
|
||||
"""
|
||||
Get Annonations from storage as described in XEP 0048, and XEP 0145
|
||||
"""
|
||||
self.annotations = {}
|
||||
if not app.account_is_connected(self.name):
|
||||
return
|
||||
iq = nbxmpp.Iq(typ='get')
|
||||
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
||||
iq2.addChild(name='storage', namespace='storage:rosternotes')
|
||||
self.connection.send(iq)
|
||||
|
||||
def store_annotations(self):
|
||||
"""
|
||||
Set Annonations in private storage as described in XEP 0048, and XEP 0145
|
||||
"""
|
||||
if not app.account_is_connected(self.name):
|
||||
return
|
||||
iq = nbxmpp.Iq(typ='set')
|
||||
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
||||
iq3 = iq2.addChild(name='storage', namespace='storage:rosternotes')
|
||||
for jid in self.annotations.keys():
|
||||
if self.annotations[jid]:
|
||||
iq4 = iq3.addChild(name = "note")
|
||||
iq4.setAttr('jid', jid)
|
||||
iq4.setData(self.annotations[jid])
|
||||
self.connection.send(iq)
|
||||
|
||||
def get_roster_delimiter(self):
|
||||
"""
|
||||
Get roster group delimiter from storage as described in XEP 0083
|
||||
|
|
|
@ -1335,9 +1335,6 @@ ConnectionHTTPUpload):
|
|||
|
||||
app.nec.register_incoming_event(PrivateStorageBookmarksReceivedEvent)
|
||||
app.nec.register_incoming_event(BookmarksReceivedEvent)
|
||||
app.nec.register_incoming_event(
|
||||
PrivateStorageRosternotesReceivedEvent)
|
||||
app.nec.register_incoming_event(RosternotesReceivedEvent)
|
||||
app.nec.register_incoming_event(StreamConflictReceivedEvent)
|
||||
app.nec.register_incoming_event(StreamOtherHostReceivedEvent)
|
||||
app.nec.register_incoming_event(MessageReceivedEvent)
|
||||
|
@ -1354,8 +1351,6 @@ ConnectionHTTPUpload):
|
|||
ged.CORE, self._nec_roster_set_received)
|
||||
app.ged.register_event_handler('private-storage-bookmarks-received',
|
||||
ged.CORE, self._nec_private_storate_bookmarks_received)
|
||||
app.ged.register_event_handler('private-storage-rosternotes-received',
|
||||
ged.CORE, self._nec_private_storate_rosternotes_received)
|
||||
app.ged.register_event_handler('roster-received', ged.CORE,
|
||||
self._nec_roster_received)
|
||||
app.ged.register_event_handler('iq-error-received', ged.CORE,
|
||||
|
@ -1391,8 +1386,6 @@ ConnectionHTTPUpload):
|
|||
ged.CORE, self._nec_roster_set_received)
|
||||
app.ged.remove_event_handler('private-storage-bookmarks-received',
|
||||
ged.CORE, self._nec_private_storate_bookmarks_received)
|
||||
app.ged.remove_event_handler('private-storage-rosternotes-received',
|
||||
ged.CORE, self._nec_private_storate_rosternotes_received)
|
||||
app.ged.remove_event_handler('roster-received', ged.CORE,
|
||||
self._nec_roster_received)
|
||||
app.ged.remove_event_handler('iq-error-received', ged.CORE,
|
||||
|
@ -1587,12 +1580,6 @@ ConnectionHTTPUpload):
|
|||
if resend_to_pubsub:
|
||||
self.store_bookmarks('pubsub')
|
||||
|
||||
def _nec_private_storate_rosternotes_received(self, obj):
|
||||
if obj.conn.name != self.name:
|
||||
return
|
||||
for jid in obj.annotations:
|
||||
self.annotations[jid] = obj.annotations[jid]
|
||||
|
||||
def _PrivateCB(self, con, iq_obj):
|
||||
"""
|
||||
Private Data (XEP 048 and 049)
|
||||
|
@ -2011,7 +1998,7 @@ ConnectionHTTPUpload):
|
|||
self.get_bookmarks()
|
||||
|
||||
# Get annotations from private namespace
|
||||
self.get_annotations()
|
||||
self.get_module('Annotations').get_annotations()
|
||||
|
||||
# Inform GUI we just signed in
|
||||
app.nec.push_incoming_event(SignedInEvent(None, conn=self))
|
||||
|
|
|
@ -450,36 +450,6 @@ class BookmarksReceivedEvent(nec.NetworkIncomingEvent):
|
|||
self.bookmarks = self.base_event.bookmarks
|
||||
return True
|
||||
|
||||
class PrivateStorageRosternotesReceivedEvent(nec.NetworkIncomingEvent):
|
||||
name = 'private-storage-rosternotes-received'
|
||||
base_network_events = ['private-storage-received']
|
||||
|
||||
def generate(self):
|
||||
self.conn = self.base_event.conn
|
||||
if self.base_event.namespace != nbxmpp.NS_ROSTERNOTES:
|
||||
return
|
||||
notes = self.base_event.storage_node.getTags('note')
|
||||
self.annotations = {}
|
||||
for note in notes:
|
||||
try:
|
||||
jid = helpers.parse_jid(note.getAttr('jid'))
|
||||
except helpers.InvalidFormat:
|
||||
log.warning('Invalid JID: %s, ignoring it' % note.getAttr('jid'))
|
||||
continue
|
||||
annotation = note.getData()
|
||||
self.annotations[jid] = annotation
|
||||
if self.annotations:
|
||||
return True
|
||||
|
||||
class RosternotesReceivedEvent(nec.NetworkIncomingEvent):
|
||||
name = 'rosternotes-received'
|
||||
base_network_events = ['private-storage-rosternotes-received']
|
||||
|
||||
def generate(self):
|
||||
self.conn = self.base_event.conn
|
||||
self.annotations = self.base_event.annotations
|
||||
return True
|
||||
|
||||
class PubsubReceivedEvent(nec.NetworkIncomingEvent):
|
||||
name = 'pubsub-received'
|
||||
base_network_events = []
|
||||
|
|
|
@ -0,0 +1,92 @@
|
|||
# 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-0145: Annotations
|
||||
|
||||
import logging
|
||||
|
||||
import nbxmpp
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common import helpers
|
||||
|
||||
log = logging.getLogger('gajim.c.m.annotations')
|
||||
|
||||
|
||||
class Annotations:
|
||||
def __init__(self, con):
|
||||
self._con = con
|
||||
self._account = con.name
|
||||
self._server = self._con.get_own_jid().getDomain()
|
||||
|
||||
self.handlers = []
|
||||
self.annotations = {}
|
||||
|
||||
def get_annotations(self):
|
||||
if not app.account_is_connected(self._account):
|
||||
return
|
||||
|
||||
log.info('Request annotations for %s', self._server)
|
||||
iq = nbxmpp.Iq(typ='get')
|
||||
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
||||
iq2.addChild(name='storage', namespace='storage:rosternotes')
|
||||
|
||||
self._con.connection.SendAndCallForResponse(iq, self._result_received)
|
||||
|
||||
def _result_received(self, stanza):
|
||||
if not nbxmpp.isResultNode(stanza):
|
||||
log.info('Error: %s', stanza.getError())
|
||||
return
|
||||
|
||||
log.info('Received annotations from %s', self._server)
|
||||
self.annotations = {}
|
||||
query = stanza.getTag('query')
|
||||
storage_node = query.getTag('storage')
|
||||
if storage_node is None:
|
||||
return
|
||||
|
||||
notes = storage_node.getTags('note')
|
||||
if notes is None:
|
||||
return
|
||||
|
||||
for note in notes:
|
||||
try:
|
||||
jid = helpers.parse_jid(note.getAttr('jid'))
|
||||
except helpers.InvalidFormat:
|
||||
log.warning('Invalid JID: %s, ignoring it',
|
||||
note.getAttr('jid'))
|
||||
continue
|
||||
self.annotations[jid] = note.getData()
|
||||
|
||||
def store_annotations(self):
|
||||
if not app.account_is_connected(self._account):
|
||||
return
|
||||
|
||||
iq = nbxmpp.Iq(typ='set')
|
||||
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
||||
iq3 = iq2.addChild(name='storage', namespace='storage:rosternotes')
|
||||
for jid in self.annotations.keys():
|
||||
if self.annotations[jid]:
|
||||
iq4 = iq3.addChild(name='note')
|
||||
iq4.setAttr('jid', jid)
|
||||
iq4.setData(self.annotations[jid])
|
||||
|
||||
self._con.connection.SendAndCallForResponse(
|
||||
iq, self._store_result_received)
|
||||
|
||||
def _store_result_received(self, stanza):
|
||||
if not nbxmpp.isResultNode(stanza):
|
||||
log.warning('Storing rosternotes failed: %s', stanza.getError())
|
||||
return
|
||||
log.info('Storing rosternotes successful')
|
|
@ -109,7 +109,8 @@ class VcardWindow:
|
|||
self.set_entity_time)
|
||||
|
||||
self.fill_jabber_page()
|
||||
annotations = app.connections[self.account].annotations
|
||||
con = app.connections[self.account]
|
||||
annotations = con.get_module('Annotations').annotations
|
||||
if self.contact.jid in annotations:
|
||||
buffer_ = self.xml.get_object('textview_annotation').get_buffer()
|
||||
buffer_.set_text(annotations[self.contact.jid])
|
||||
|
@ -140,12 +141,13 @@ class VcardWindow:
|
|||
GLib.source_remove(self.update_progressbar_timeout_id)
|
||||
del app.interface.instances[self.account]['infos'][self.contact.jid]
|
||||
buffer_ = self.xml.get_object('textview_annotation').get_buffer()
|
||||
annotation = buffer_.get_text(buffer_.get_start_iter(),
|
||||
new_annotation = buffer_.get_text(buffer_.get_start_iter(),
|
||||
buffer_.get_end_iter(), True)
|
||||
connection = app.connections[self.account]
|
||||
if annotation != connection.annotations.get(self.contact.jid, ''):
|
||||
connection.annotations[self.contact.jid] = annotation
|
||||
connection.store_annotations()
|
||||
con = app.connections[self.account]
|
||||
annotations = con.get_module('Annotations').annotations
|
||||
if new_annotation != annotations.get(self.contact.jid, ''):
|
||||
annotations[self.contact.jid] = new_annotation
|
||||
con.get_module('Annotations').store_annotations()
|
||||
app.ged.remove_event_handler('version-result-received', ged.GUI1,
|
||||
self.set_os_info)
|
||||
app.ged.remove_event_handler('time-result-received', ged.GUI1,
|
||||
|
|
Loading…
Reference in New Issue