From 4c482cbfe8b09fdcb383c6b7540015e6f871c208 Mon Sep 17 00:00:00 2001 From: lovetox Date: Mon, 10 Oct 2016 19:10:18 +0200 Subject: [PATCH] Store optional fields correctly in Bookmarks --- src/common/connection.py | 27 ++++++++++------- src/common/connection_handlers_events.py | 37 +++++++++++++++--------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/common/connection.py b/src/common/connection.py index 751d923af..f20cbfc2a 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -2474,7 +2474,8 @@ class Connection(CommonConnection, ConnectionHandlers): """ if not gajim.account_is_connected(self.name): return - if self.pubsub_supported and storage_type != 'xml': + if self.pubsub_supported and self.pubsub_publish_options_supported \ + and storage_type != 'xml': self.send_pb_retrieve('', 'storage:bookmarks') # some server (ejabberd) are so slow to answer that we request via XML # if we don't get answer in the next 30 seconds @@ -2489,15 +2490,17 @@ class Connection(CommonConnection, ConnectionHandlers): storage_type can be set to 'pubsub' or 'xml' so store in only one method else it will be stored on both """ + NS_GAJIM_BM = 'xmpp:gajim.org/bookmarks' if not gajim.account_is_connected(self.name): return iq = nbxmpp.Node(tag='storage', attrs={'xmlns': 'storage:bookmarks'}) for bm in self.bookmarks: - iq2 = iq.addChild(name = "conference") + iq2 = iq.addChild(name="conference") iq2.setAttr('jid', bm['jid']) iq2.setAttr('autojoin', bm['autojoin']) - iq2.setAttr('minimize', bm['minimize']) iq2.setAttr('name', bm['name']) + iq2.setTag('minimize', namespace=NS_GAJIM_BM). \ + setData(bm['minimize']) # Only add optional elements if not empty # Note: need to handle both None and '' as empty # thus shouldn't use "is not None" @@ -2506,21 +2509,23 @@ class Connection(CommonConnection, ConnectionHandlers): if bm.get('password', None): iq2.setTagData('password', bm['password']) if bm.get('print_status', None): - iq2.setTagData('print_status', bm['print_status']) + iq2.setTag('print_status', namespace=NS_GAJIM_BM). \ + setData(bm['print_status']) - if self.pubsub_supported and self.pubsub_publish_options_supported and \ - storage_type != 'xml': + if self.pubsub_supported and self.pubsub_publish_options_supported and\ + storage_type != 'xml': options = nbxmpp.Node(nbxmpp.NS_DATA + ' x', - attrs={'type': 'submit'}) - f = options.addChild('field', attrs={'var': 'FORM_TYPE', - 'type': 'hidden'}) + attrs={'type': 'submit'}) + f = options.addChild('field', + attrs={'var': 'FORM_TYPE', 'type': 'hidden'}) f.setTagData('value', nbxmpp.NS_PUBSUB_PUBLISH_OPTIONS) - f = options.addChild('field', attrs={'var': 'pubsub#persist_items'}) + f = options.addChild('field', + attrs={'var': 'pubsub#persist_items'}) f.setTagData('value', 'true') f = options.addChild('field', attrs={'var': 'pubsub#access_model'}) f.setTagData('value', 'whitelist') self.send_pb_publish('', 'storage:bookmarks', iq, 'current', - options=options) + options=options) if storage_type != 'pubsub': iqA = nbxmpp.Iq(typ='set') iqB = iqA.addChild(name='query', namespace=nbxmpp.NS_PRIVATE) diff --git a/src/common/connection_handlers_events.py b/src/common/connection_handlers_events.py index 7b21f9170..af66f5655 100644 --- a/src/common/connection_handlers_events.py +++ b/src/common/connection_handlers_events.py @@ -499,33 +499,44 @@ class PrivateStorageReceivedEvent(nec.NetworkIncomingEvent): self.namespace = self.storage_node.getNamespace() return True + class BookmarksHelper: def parse_bookmarks(self): self.bookmarks = [] + NS_GAJIM_BM = 'xmpp:gajim.org/bookmarks' confs = self.storage_node.getTags('conference') for conf in confs: autojoin_val = conf.getAttr('autojoin') - if autojoin_val is None: # not there (it's optional) + if not autojoin_val: # not there (it's optional) autojoin_val = False - minimize_val = conf.getAttr('minimize') - if minimize_val is None: # not there (it's optional) - minimize_val = False - print_status = conf.getTagData('print_status') + minimize_val = conf.getTag('minimize', namespace=NS_GAJIM_BM) + if not minimize_val: # not there, try old Gajim behaviour + minimize_val = conf.getAttr('minimize') + if not minimize_val: # not there (it's optional) + minimize_val = False + else: + minimize_val = minimize_val.getData() + + print_status = conf.getTag('print_status', namespace=NS_GAJIM_BM) if not print_status: print_status = conf.getTagData('show_status') + else: + print_status = print_status.getData() + try: jid = helpers.parse_jid(conf.getAttr('jid')) except helpers.InvalidFormat: - log.warning('Invalid JID: %s, ignoring it' % conf.getAttr('jid')) + log.warning('Invalid JID: %s, ignoring it' + % conf.getAttr('jid')) continue - bm = {'name': conf.getAttr('name'), - 'jid': jid, - 'autojoin': autojoin_val, - 'minimize': minimize_val, - 'password': conf.getTagData('password'), - 'nick': conf.getTagData('nick'), - 'print_status': print_status} + bm = {'name': conf.getAttr('name'), + 'jid': jid, + 'autojoin': autojoin_val, + 'minimize': minimize_val, + 'password': conf.getTagData('password'), + 'nick': conf.getTagData('nick'), + 'print_status': print_status} bm_jids = [b['jid'] for b in self.bookmarks] if bm['jid'] not in bm_jids: