diff browser_side/contact.py @ 55:d5266c41ca24

Roster list update, contact deletion + some refactoring
author Goffi <goffi@goffi.org>
date Sun, 29 May 2011 02:13:53 +0200
parents f25c4077f6b9
children 12e889a683ce
line wrap: on
line diff
--- a/browser_side/contact.py	Sat May 28 20:18:14 2011 +0200
+++ b/browser_side/contact.py	Sun May 29 02:13:53 2011 +0200
@@ -90,31 +90,53 @@
         _item.addMouseListener(self._parent)
         DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
         VerticalPanel.add(self, _item)
+
+    def remove(self, group):
+        for wid in self:
+            if isinstance(wid, GroupLabel) and wid.group == group:
+                VerticalPanel.remove(self, wid)
     
 class ContactList(VerticalPanel):
 
     def __init__(self):
         VerticalPanel.__init__(self)
-        self.contacts=[]
+        self.contacts = set()
 
-    def __iter__(self):
-        return self.contacts.__iter__()
-    
     def add(self, jid, name=None):
+        if jid in self.contacts:
+            return
+        self.contacts.add(jid)
         _item = ContactLabel(jid, name)
         DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
         VerticalPanel.add(self, _item)
-        self.contacts.append(_item)
+
+    def remove(self, jid):
+        wid = self.getContactLabel(jid)
+        if not wid:
+            return
+        VerticalPanel.remove(self, wid)
+        self.contacts.remove(jid)
+
+    def isContactPresent(self, contact_jid):
+        """Return True if a contact is present in the panel"""
+        return contact_jid in self.contacts
+
+    def getContacts(self):
+        return self.contacts
+
+    def getContactLabel(self, contact_jid):
+        """get contactList widget of a contact
+        @return: ContactLabel item if present, else None"""
+        for wid in self:
+            if isinstance(wid, ContactLabel) and wid.jid == contact_jid:
+                return wid
+        return None
 
     def setState(self, jid, state):
         """Change the appearance of the contact, according to the state
         @param jid: jid which need to change state
         @param state: 'unavailable' if not connected, else presence like RFC6121 #4.7.2.1"""
-        _item = None
-        for contact in self.contacts:
-            if contact.jid == jid:
-                _item = contact
-                break
+        _item = self.getContactLabel(jid)
         if _item:
             if state == 'unavailable':
                 _item.removeStyleName('contactConnected')
@@ -151,19 +173,40 @@
         self.add(self.vPanel)
         self.setStyleName('contactBox')
 
-    def addContact(self, jid, attributes, groups):
-        """Add a contact to the panel
+    def updateContact(self, jid, attributes, groups):
+        """Add a contact to the panel if it doesn't exist, update it else
         @param jid: jid
         @attributes: cf SàT Bridge API's newContact
         @param groups: list of groups"""
-        for group in groups:
+        _current_groups = self.getContactGroups(jid)
+        _new_groups = set(groups)
+        _key = "@%s: "
+
+        for group in _current_groups.difference(_new_groups):
+            #We remove the contact from the groups where he isn't anymore
+            self.groups[group].remove(jid)
+            if not self.groups[group]:
+                #The group is now empty, we must remove it
+                del self.groups[group]
+                self._groupList.remove(group)
+                self.host.uni_box.removeKey(_key % group)
+
+        for group in _new_groups.difference(_current_groups):
+            #We add the contact to the groups he joined
             if not self.groups.has_key(group):
                 self.groups[group] = set()
                 self._groupList.add(group)
-                self.host.uni_box.addKey("@%s: " % group)
+                self.host.uni_box.addKey(_key % group)
             self.groups[group].add(jid)
+        
+        #We add the contact to contact list, it will check if contact already exists
         self._contact_list.add(jid)
 
+    def removeContact(self, jid):
+        """Remove contacts from groups where he is and contact list"""
+        self.updateContact(jid, {}, []) #we remove contact from every group
+        self._contact_list.remove(jid)
+
     def setConnected(self, jid, resource, availability, priority, statuses):
         """Set connection status"""
         if availability=='unavailable':
@@ -182,12 +225,23 @@
         """return a list of all jid (bare jid) connected"""
         return self.connected.keys()
 
+    def getContactGroups(self, contact_jid):
+        """Get groups where contact is
+       @param group: string of single group, or list of string
+       @param contact_jid: jid to test
+        """
+        result=set()
+        for group in self.groups:
+            if self.isContactInGroup(group, contact_jid):
+                result.add(group)
+        return result
+
     def isContactInGroup(self, group, contact_jid):
        """Test if the contact_jid is in the group
        @param group: string of single group, or list of string
        @param contact_jid: jid to test
        @return: True if contact_jid is in on of the groups"""
-       if self.groups.has_key(group) and contact_jid in self.groups[group]:
+       if group in self.groups and contact_jid in self.groups[group]:
            return True
        return False
 
@@ -198,6 +252,9 @@
                 return True
         return False
 
+    def getContacts(self):
+        return self._contact_list.getContacts()
+
     def getGroups(self):
         return self.groups.keys()