diff src/browser/sat_browser/contact_list.py @ 600:32dbbc941123 frontends_multi_profiles

browser_side: fixes the contact group manager
author souliane <souliane@mailoo.org>
date Fri, 06 Feb 2015 17:53:01 +0100
parents a099990f77a6
children 49ccfc22116c 7af8f4ab3675
line wrap: on
line diff
--- a/src/browser/sat_browser/contact_list.py	Fri Feb 06 19:31:30 2015 +0100
+++ b/src/browser/sat_browser/contact_list.py	Fri Feb 06 17:53:01 2015 +0100
@@ -256,6 +256,7 @@
 
         @param contact_jid (jid.JID): the contact
         @return: ContactBox instance if present, else None"""
+        assert isinstance(contact_jid, jid.JID)
         for wid in self:
             if isinstance(wid, ContactBox) and wid.jid == contact_jid:
                 return wid
@@ -585,3 +586,42 @@
     # def refresh(self):
     #     """Show or hide disconnected contacts and empty groups"""
     #     self.updateVisibility(self._contacts_panel.contacts, self.groups.keys())
+
+
+def mayContainJID(iterable, item):
+    """Tells if the given item is in the iterable, works with JID.
+
+    @param iterable(object): list, set or another iterable object
+    @param item (object): element
+    @return: bool
+    """
+    # Pyjamas JID-friendly implementation of the "in" operator. Since our JID
+    # doesn't inherit from str, without this method the test would return True
+    # only when the objects references are the same.
+    if isinstance(item, jid.JID):
+        return hash(item) in [hash(other) for other in iterable if isinstance(other, jid.JID)]
+    return super(type(iterable), iterable).__contains__(self, item)
+
+
+class JIDSet(set):
+    """JID set implementation for Pyjamas"""
+
+    def __contains__(self, item):
+        return mayContainJID(self, item)
+
+
+class JIDList(list):
+    """JID list implementation for Pyjamas"""
+
+    def __contains__(self, item):
+        return mayContainJID(self, item)
+
+
+class JIDDict(dict):
+    """JID dict implementation for Pyjamas (a dict with JID keys)"""
+
+    def __contains__(self, item):
+        return mayContainJID(self, item)
+
+    def keys(self):
+        return JIDSet(dict.keys(self))