diff frontends/src/primitivus/contact_list.py @ 1000:6f1e03068b5f

primitivus: fixes contact group update
author souliane <souliane@mailoo.org>
date Sun, 27 Apr 2014 18:22:12 +0200
parents b12706d164d7
children 0a9986452bba
line wrap: on
line diff
--- a/frontends/src/primitivus/contact_list.py	Fri Apr 11 11:02:42 2014 +0200
+++ b/frontends/src/primitivus/contact_list.py	Sun Apr 27 18:22:12 2014 +0200
@@ -234,20 +234,32 @@
         self.update()
 
     def replace(self, jid, groups=None, attributes=None):
-        """add a contact to the list if doesn't exist, else update it"""
+        """Add a contact to the list if doesn't exist, else update it.
+
+        @param jid (JID)
+        @param groups (list): list of groups or None to ignore the groups membership.
+        @param attributes (dict)
+
+        XXX: None value for 'groups' has a different meaning than [None] which is for the default group.
+        """
         QuickContactList.replace(self, jid, groups, attributes)
         if jid.bare in self.specials:
             return
-        if not groups:
-            groups = [None]
         if not attributes:
-            attributes={}
-        assert isinstance(groups, list)
+            attributes = {}
         assert isinstance(jid, JID)
-        for group in groups:
-            if not self.groups.has_key(group):
-                self.groups[group] = [True,set()]  #[unfold,list_of_contacts]
-            self.groups[group][1].add(jid.bare)
+        if groups is not None:
+            if not groups:
+                groups = [None]
+            for group in [group for group in self.groups if group not in groups]:
+                try:  # remove the contact from a previous group
+                    self.groups[group][1].remove(jid.bare)
+                except KeyError:
+                    pass
+            for group in groups:
+                if group not in self.groups:
+                    self.groups[group] = [True, set()]  # [unfold, list_of_contacts]
+                self.groups[group][1].add(jid.bare)
         self.update()
 
     def remove(self, jid):
@@ -264,9 +276,9 @@
             del self.groups[group]
         self.update()
 
-    def add(self, jid, param_groups=[None]):
+    def add(self, jid, param_groups=None):
         """add a contact to the list"""
-        self.replace(jid,param_groups)
+        self.replace(jid, param_groups)
 
     def setSpecial(self, special_jid, special_type, show=False):
         """Set entity as a special