changeset e59e6777df1d in /home/hg/repos/gajim

details:http://hg.gajim.org/gajim?cmd=changeset;node=e59e6777df1d
description: import some improvements from xmpppy and use it where it's usefull

diffstat:

 src/common/connection.py          |  24 +++++++++---------
 src/common/connection_handlers.py |  20 +++++++-------
 src/common/xmpp/protocol.py       |  51 +++++++++++++++++++++++++++++---------
 3 files changed, 60 insertions(+), 35 deletions(-)

diffs (truncated from 312 to 300 lines):

diff -r cb442708efd2 -r e59e6777df1d src/common/connection.py
--- a/src/common/connection.py  Mon Oct 31 09:52:23 2011 +0100
+++ b/src/common/connection.py  Mon Oct 31 10:47:28 2011 +0100
@@ -1503,14 +1503,14 @@
         Build a Privacy rule stanza for invisibility
         """
         iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '')
-        l = iq.getTag('query').setTag('list', {'name': name})
+        l = iq.setQuery().setTag('list', {'name': name})
         i = l.setTag('item', {'action': action, 'order': str(order)})
         i.setTag('presence-out')
         return iq
 
     def build_invisible_rule(self):
         iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '')
-        l = iq.getTag('query').setTag('list', {'name': 'invisible'})
+        l = iq.setQuery().setTag('list', {'name': 'invisible'})
         if self.name in gajim.interface.status_sent_to_groups and \
         len(gajim.interface.status_sent_to_groups[self.name]) > 0:
             for group in gajim.interface.status_sent_to_groups[self.name]:
@@ -1540,7 +1540,7 @@
         if not gajim.account_is_connected(self.name):
             return
         iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '')
-        iq.getTag('query').setTag('active', {'name': name})
+        iq.setQuery().setTag('active', {'name': name})
         self.connection.send(iq)
 
     def send_invisible_presence(self, msg, signed, initial = False):
@@ -1892,7 +1892,7 @@
         if name:
             infos['name'] = name
         iq = common.xmpp.Iq('set', common.xmpp.NS_ROSTER)
-        q = iq.getTag('query')
+        q = iq.setQuery()
         item = q.addChild('item', attrs=infos)
         for g in groups:
             item.addChild('group').setData(g)
@@ -1937,7 +1937,7 @@
         if not gajim.account_is_connected(self.name):
             return
         iq = common.xmpp.Iq('set', common.xmpp.NS_REGISTER, to = agent)
-        iq.getTag('query').setTag('remove')
+        iq.setQuery().setTag('remove')
         id_ = self.connection.getAnID()
         iq.setID(id_)
         self.awaiting_answers[id_] = (AGENT_REMOVED, agent)
@@ -2395,7 +2395,7 @@
             return
         iq = common.xmpp.Iq(typ = 'set', queryNS = common.xmpp.NS_MUC_OWNER,
                 to = room_jid)
-        destroy = iq.getTag('query').setTag('destroy')
+        destroy = iq.setQuery().setTag('destroy')
         if reason:
             destroy.setTagData('reason', reason)
         if jid:
@@ -2442,7 +2442,7 @@
             return
         iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\
                 common.xmpp.NS_MUC_ADMIN)
-        item = iq.getTag('query').setTag('item')
+        item = iq.setQuery().setTag('item')
         item.setAttr('nick', nick)
         item.setAttr('role', role)
         if reason:
@@ -2457,7 +2457,7 @@
             return
         iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\
                 common.xmpp.NS_MUC_ADMIN)
-        item = iq.getTag('query').setTag('item')
+        item = iq.setQuery().setTag('item')
         item.setAttr('jid', jid)
         item.setAttr('affiliation', affiliation)
         if reason:
@@ -2469,7 +2469,7 @@
             return
         iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS = \
                 common.xmpp.NS_MUC_ADMIN)
-        item = iq.getTag('query')
+        item = iq.setQuery()
         for jid in users_dict:
             item_tag = item.addChild('item', {'jid': jid,
                     'affiliation': users_dict[jid]['affiliation']})
@@ -2482,7 +2482,7 @@
             return
         iq = common.xmpp.Iq(typ = 'get', to = room_jid, queryNS = \
                 common.xmpp.NS_MUC_ADMIN)
-        item = iq.getTag('query').setTag('item')
+        item = iq.setQuery().setTag('item')
         item.setAttr('affiliation', affiliation)
         self.connection.send(iq)
 
@@ -2491,7 +2491,7 @@
             return
         iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\
                 common.xmpp.NS_MUC_OWNER)
-        query = iq.getTag('query')
+        query = iq.setQuery()
         form.setAttr('type', 'submit')
         query.addChild(node = form)
         self.connection.send(iq)
@@ -2616,7 +2616,7 @@
     def send_search_form(self, jid, form, is_form):
         iq = common.xmpp.Iq(typ = 'set', to = jid, queryNS = \
             common.xmpp.NS_SEARCH)
-        item = iq.getTag('query')
+        item = iq.setQuery()
         if is_form:
             item.addChild(node=form)
         else:
diff -r cb442708efd2 -r e59e6777df1d src/common/connection_handlers.py
--- a/src/common/connection_handlers.py Mon Oct 31 09:52:23 2011 +0100
+++ b/src/common/connection_handlers.py Mon Oct 31 10:47:28 2011 +0100
@@ -151,7 +151,7 @@
             return
         if is_form:
             iq = common.xmpp.Iq('set', common.xmpp.NS_REGISTER, to=agent)
-            query = iq.getTag('query')
+            query = iq.setQuery()
             info.setAttr('type', 'submit')
             query.addChild(node=info)
             self.connection.SendAndCallForResponse(iq,
@@ -233,8 +233,7 @@
         log.debug('DiscoverInfoGetCB')
         if not self.connection or self.connected < 2:
             return
-        q = iq_obj.getTag('query')
-        node = q.getAttr('node')
+        node = iq_obj.getQuerynode()
 
         if self.commandInfoQuery(con, iq_obj):
             raise common.xmpp.NodeProcessed
@@ -245,7 +244,7 @@
             raise common.xmpp.NodeProcessed
 
         iq = iq_obj.buildReply('result')
-        q = iq.getTag('query')
+        q = iq.setQuery()
         if node:
             q.setAttr('node', node)
         q.addChild('identity', attrs=gajim.gajim_identity)
@@ -390,7 +389,7 @@
         iq = common.xmpp.Iq(typ='get')
         if jid:
             iq.setTo(jid)
-        iq.setTag(common.xmpp.NS_VCARD + ' vCard')
+        iq.setQuery('vCard').setNamespace(common.xmpp.NS_VCARD)
 
         id_ = self.connection.getAnID()
         iq.setID(id_)
@@ -532,6 +531,8 @@
                 return
             if iq_obj.getType() == 'result':
                 query = iq_obj.getTag('query')
+                if not query:
+                    return
                 delimiter = query.getTagData('roster')
                 if delimiter:
                     self.nested_group_delimiter = delimiter
@@ -651,9 +652,8 @@
                         with_ = element.getAttr('with')
                         start_ = element.getAttr('start')
                         self.request_collection_page(with_, start_)
-                    elif element.getName() == 'removed':
+                    #elif element.getName() == 'removed':
                         # do nothing
-                        pass
 
         del self.awaiting_answers[id_]
 
@@ -1470,7 +1470,7 @@
         if obj.conn.name != self.name:
             return
         iq_obj = obj.stanza.buildReply('result')
-        qp = iq_obj.getTag('query')
+        qp = iq_obj.setQuery()
         qp.setTagData('name', 'Gajim')
         qp.setTagData('version', gajim.version)
         send_os = gajim.config.get_per('accounts', self.name, 'send_os_info')
@@ -1493,7 +1493,7 @@
         if HAS_IDLE and gajim.config.get_per('accounts', self.name,
         'send_idle_time'):
             iq_obj = obj.stanza.buildReply('result')
-            qp = iq_obj.getTag('query')
+            qp = iq_obj.setQuery()
             qp.attrs['seconds'] = int(self.sleeper.getIdleSec())
         else:
             iq_obj = obj.stanza.buildReply('error')
@@ -1520,7 +1520,7 @@
             return
         if gajim.config.get_per('accounts', self.name, 'send_time_info'):
             iq_obj = obj.stanza.buildReply('result')
-            qp = iq_obj.getTag('query')
+            qp = iq_obj.setQuery()
             qp.setTagData('utc', strftime('%Y%m%dT%H:%M:%S', gmtime()))
             qp.setTagData('tz', helpers.decode_string(tzname[daylight]))
             qp.setTagData('display', helpers.decode_string(strftime('%c',
diff -r cb442708efd2 -r e59e6777df1d src/common/xmpp/protocol.py
--- a/src/common/xmpp/protocol.py       Mon Oct 31 09:52:23 2011 +0100
+++ b/src/common/xmpp/protocol.py       Mon Oct 31 10:47:28 2011 +0100
@@ -779,10 +779,11 @@
     def buildReply(self, text=None):
         """
         Builds and returns another message object with specified text. The to,
-        from and thread properties of new message are pre-set as reply to this
-        message
+        from, thread and type properties of new message are pre-set as reply to
+        this message
         """
-        m = Message(to=self.getFrom(), frm=self.getTo(), body=text)
+        m = Message(to=self.getFrom(), frm=self.getTo(), body=text,
+            typ=self.getType())
         th = self.getThread()
         if th:
             m.setThread(th)
@@ -939,11 +940,20 @@
         if queryNS:
             self.setQueryNS(queryNS)
 
+    def getQuery(self):
+        """
+        Return the IQ's child element if it exists, None otherwise.
+        """
+        children = self.getChildren()
+        if children and self.getType() != 'error' and \
+        children[0].getName() != 'error':
+            return children[0]
+
     def getQueryNS(self):
         """
         Return the namespace of the 'query' child element
         """
-        tag = self.getTag('query')
+        tag = self.getQuery()
         if tag:
             return tag.getNamespace()
 
@@ -951,13 +961,15 @@
         """
         Return the 'node' attribute value of the 'query' child element
         """
-        return self.getTagAttr('query', 'node')
+        tag = self.getQuery()
+        if tag:
+            return tag.getAttr('node')
 
     def getQueryPayload(self):
         """
         Return the 'query' child element payload
         """
-        tag = self.getTag('query')
+        tag = self.getQuery()
         if tag:
             return tag.getPayload()
 
@@ -965,36 +977,49 @@
         """
         Return the 'query' child element child nodes
         """
-        tag = self.getTag('query')
+        tag = self.getQuery()
         if tag:
             return tag.getChildren()
 
+    def setQuery(self, name=None):
+        """
+        Change the name of the query node, creating it if needed. Keep the
+        existing name if none is given (use 'query' if it's a creation).
+        Return the query node.
+        """
+        query = self.getQuery()
+        if query is None:
+            query = self.addChild('query')
+        if name is not None:
+            query.setName(name)
+        return query
+
     def setQueryNS(self, namespace):
         """
         Set the namespace of the 'query' child element
         """
-        self.setTag('query').setNamespace(namespace)
+        self.setQuery().setNamespace(namespace)
 
     def setQueryPayload(self, payload):
         """
         Set the 'query' child element payload
         """
-        self.setTag('query').setPayload(payload)
+        self.setQuery().setPayload(payload)
 
     def setQuerynode(self, node):
         """
         Set the 'node' attribute value of the 'query' child element
         """
-        self.setTagAttr('query', 'node', node)
+        self.setQuery().setAttr('node', node)
 
     def buildReply(self, typ):
         """
_______________________________________________
Commits mailing list
[email protected]
http://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to