diff src/browser/sat_browser/contact_panel.py @ 673:e489218886d7 frontends_multi_profiles

browser_side: add attribute "merge_resources" to ContactsPanel to display the MUC occupants + override Chat.replaceUser and Chat.removeUser
author souliane <souliane@mailoo.org>
date Wed, 11 Mar 2015 19:01:27 +0100
parents 2201ff543a05
children 849ffb24d5bf
line wrap: on
line diff
--- a/src/browser/sat_browser/contact_panel.py	Wed Mar 11 12:50:19 2015 +0100
+++ b/src/browser/sat_browser/contact_panel.py	Wed Mar 11 19:01:27 2015 +0100
@@ -82,10 +82,14 @@
     Special features like popup menu panel or changing the contact states must be done in a sub-class.
     """
 
-    def __init__(self, host, contacts_click=None, contacts_style=None, contacts_menus=True, contacts_display=C.CONTACT_DEFAULT_DISPLAY):
+    def __init__(self, host, merge_resources=True, contacts_click=None,
+                 contacts_style=None, contacts_menus=True,
+                 contacts_display=C.CONTACT_DEFAULT_DISPLAY):
         """
 
         @param host (SatWebFrontend): host instance
+        @param merge_resources (bool): if True, the entities sharing the same
+            bare JID will also share the same contact box.
         @param contacts_click (callable): click callback for the contact boxes
         @param contacts_style (unicode): CSS style name for the contact boxes
         @param contacts_menus (tuple): define the menu types that fit this
@@ -95,6 +99,7 @@
         """
         VerticalPanel.__init__(self)
         self.host = host
+        self.merge_resources = merge_resources
         self._contacts = {}  # entity jid to ContactBox map
         self.click_listener = None
 
@@ -105,6 +110,14 @@
         self.contacts_menus = contacts_menus
         self.contacts_display = contacts_display
 
+    def _key(self, contact_jid):
+        """Return internal key for this contact.
+
+        @param contact_jid (jid.JID): contact JID
+        @return: jid.JID
+        """
+        return contact_jid.bare if self.merge_resources else contact_jid
+
     def setList(self, jids):
         """set all contacts in the list in one shot.
 
@@ -117,54 +130,62 @@
             # the display doesn't change
             return
         self.clear()
-        for jid_ in jids:
-            assert isinstance(jid_, jid.JID)
-            box = self.getContactBox(jid_)
-            VerticalPanel.append(self, box)
-
-    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
+        for contact_jid in jids:
+            assert isinstance(contact_jid, jid.JID)
+            self.addContact(contact_jid)
 
     def getContactBox(self, contact_jid):
-        """get the Contactbox of a contact
+        """Get a contact box for a contact, add it if it doesn't exist yet.
 
-        if the Contactbox doesn't exists, it will be created
-        @param contact_jid (jid.JID): the contact
-        @return: ContactBox instance
+        @param contact_jid (jid.JID): contact JID
+        @return: ContactBox
         """
         try:
-            return self._contacts[contact_jid.bare]
+            return self._contacts[self._key(contact_jid)]
         except KeyError:
             box = contact_widget.ContactBox(self.host, contact_jid,
                                             style_name=self.contacts_style,
                                             menu_types=self.contacts_menus,
                                             display=self.contacts_display)
-            self._contacts[contact_jid.bare] = box
+            self._contacts[self._key(contact_jid)] = box
             return box
 
-    def updateAvatar(self, jid_, url):
+    def addContact(self, contact_jid):
+        """Add a contact to the list.
+
+        @param contact_jid (jid.JID): contact JID
+        """
+        box = self.getContactBox(contact_jid)
+        if box not in self.children:
+            VerticalPanel.append(self, box)
+
+    def removeContact(self, contact_jid):
+        """Remove a contact from the list.
+
+        @param contact_jid (jid.JID): contact JID
+        """
+        box = self._contacts.pop(self._key(contact_jid))
+        VerticalPanel.remove(self, box)
+
+    def updateAvatar(self, contact_jid, url):
         """Update the avatar of the given contact
 
-        @param jid_ (jid.JID): contact jid
+        @param contact_jid (jid.JID): contact JID
         @param url (unicode): image url
         """
         try:
-            self.getContactBox(jid_).updateAvatar(url)
+            self.getContactBox(contact_jid).updateAvatar(url)
         except TypeError:
             pass
 
-    def updateNick(self, jid_, new_nick):
-        """Update the avatar of the given contact
+    def updateNick(self, contact_jid, new_nick):
+        """Update the avatar of the given contact.
 
-        @param jid_ (jid.JID): contact jid
+        @param contact_jid (jid.JID): contact JID
         @param new_nick (unicode): new nick of the contact
         """
         try:
-            self.getContactBox(jid_).updateNick(new_nick)
+            self.getContactBox(contact_jid).updateNick(new_nick)
         except TypeError:
             pass