diff frontends/src/quick_frontend/quick_contact_list.py @ 2015:20fb71b656e3

quick_frontend, primitivus (contact_list): improved and simplified handling of "special" entities: - special_extras has been removed - specials handle all entities (bare + full) in a single set
author Goffi <goffi@goffi.org>
date Sun, 24 Jul 2016 17:47:09 +0200
parents 8c4087fd034a
children f09562b0704d
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_contact_list.py	Tue Jul 19 21:25:22 2016 +0200
+++ b/frontends/src/quick_frontend/quick_contact_list.py	Sun Jul 24 17:47:09 2016 +0200
@@ -56,10 +56,9 @@
         # for different profiles (e.g. directed presence)
         self._cache = {}
 
-        # special entities (groupchat, gateways, etc), bare jids
+        # special entities (groupchat, gateways, etc)
+        # may be bare or full jid
         self._specials = set()
-        # extras are specials with full jids (e.g.: private MUC conversation)
-        self._special_extras = set()
 
         # group data contain jids in groups and misc frontend data
         # None key is used for jids with not group
@@ -178,20 +177,6 @@
         """
         return self._cache[entity]
 
-    def getSpecialExtras(self, special_type=None):
-        """Return special extras with given type
-
-        If special_type is None, return all special extras.
-
-        @param special_type(unicode, None): one of special type (e.g. C.CONTACT_SPECIAL_GROUP)
-            None to return all special extras.
-        @return (set[jid.JID])
-        """
-        if special_type is None:
-            return self._special_extras
-        specials = self.getSpecials(special_type)
-        return {extra for extra in self._special_extras if extra.bare in specials}
-
     def _gotContacts(self, contacts):
         """Called during filling, add contacts and notice parent that contacts are filled"""
         for contact in contacts:
@@ -289,22 +274,25 @@
         """Set special flag on an entity
 
         @param entity(jid.JID): jid of the special entity
+            if the jid is full, will be added to special extras
         @param special_type: one of special type (e.g. C.CONTACT_SPECIAL_GROUP) or None to remove special flag
         """
         assert special_type in C.CONTACT_SPECIAL_ALLOWED + (None,)
         self.setCache(entity, C.CONTACT_SPECIAL, special_type)
 
-    def getSpecials(self, special_type=None):
+    def getSpecials(self, special_type=None, bare=False):
         """Return all the bare JIDs of the special roster entities of with given type.
 
-        If special_type is None, return all specials.
-        @param special_type: one of special type (e.g. C.CONTACT_SPECIAL_GROUP) or None to return all specials.
-        @return: set(jid.JID)
+        @param special_type(unicode, None): if not None, filter by special type (e.g. C.CONTACT_SPECIAL_GROUP)
+        @param bare(bool): return only bare jids if True
+        @return (iter[jid.JID]): found special entities
         """
-        if special_type is None:
-            return self._specials
-        return set([entity for entity in self._specials if self.getCache(entity, C.CONTACT_SPECIAL) == special_type])
-
+        for entity in self._specials:
+            if bare and entity.resource:
+                continue
+            if special_type is not None and self.getCache(entity, C.CONTACT_SPECIAL) != special_type:
+                continue
+            yield entity
 
     def disconnect(self):
         # for now we just clear contacts on disconnect
@@ -320,7 +308,6 @@
             self._cache.clear()
         self._groups.clear()
         self._specials.clear()
-        self._special_extras.clear()
         self._roster.clear()
         self.update()
 
@@ -369,9 +356,9 @@
         if C.CONTACT_SPECIAL in attributes:
             if attributes[C.CONTACT_SPECIAL] is None:
                 del attributes[C.CONTACT_SPECIAL]
-                self._specials.remove(entity_bare)
+                self._specials.remove(entity)
             else:
-                self._specials.add(entity_bare)
+                self._specials.add(entity)
                 cache[C.CONTACT_MAIN_RESOURCE] = None
 
         # now the attributes we keep in cache
@@ -441,7 +428,7 @@
             self._groups[group]['jids'].remove(entity_bare)
             if not self._groups[group]['jids']:
                 self._groups.pop(group) # FIXME: we use pop because of pyjamas: http://wiki.goffi.org/wiki/Issues_with_Pyjamas/en
-        for iterable in (self._selected, self._specials, self._special_extras):
+        for iterable in (self._selected, self._specials):
             to_remove = set()
             for set_entity in iterable:
                 if set_entity.bare == entity.bare: