diff src/browser/sat_browser/contact.py @ 567:ee9c7bd266ad

browser_side: implements "Show offline contacts" and "Show empty groups" parameters
author souliane <souliane@mailoo.org>
date Mon, 06 Oct 2014 12:47:03 +0200
parents 3d8e8f693576
children f0c8a4d51890
line wrap: on
line diff
--- a/src/browser/sat_browser/contact.py	Sat Oct 04 10:28:20 2014 +0200
+++ b/src/browser/sat_browser/contact.py	Mon Oct 06 12:47:03 2014 +0200
@@ -178,6 +178,17 @@
         for wid in self:
             if isinstance(wid, GroupLabel) and wid.group == group:
                 VerticalPanel.remove(self, wid)
+                return
+
+    def getGroupBox(self, group):
+        """get the widget of a group
+
+        @param group (str): the group
+        @return: GroupLabel instance if present, else None"""
+        for wid in self:
+            if isinstance(wid, GroupLabel) and wid.group == group:
+                return wid
+        return None
 
 
 class GenericContactList(VerticalPanel):
@@ -197,30 +208,30 @@
                 self.host.getOrCreateLiberviaWidget(panels.ChatPanel, contact_jid)
             self.click_listener = cb
 
-    def add(self, jid, name=None):
+    def add(self, jid_s, name=None):
         """Add a contact to the list.
 
         @param jid (str): JID of the contact
         @param name (str): optional name of the contact
         """
-        assert(isinstance(jid, str))
-        if jid in self.contacts:
+        assert(isinstance(jid_s, str))
+        if jid_s in self.contacts:
             return
         index = 0
         for contact_ in self.contacts:
-            if contact_ > jid:
+            if contact_ > jid_s:
                 break
             index += 1
-        self.contacts.insert(index, jid)
-        box = ContactBox(self.host, jid, name, self.click_listener, self.handle_menu)
+        self.contacts.insert(index, jid_s)
+        box = ContactBox(self.host, jid_s, name, self.click_listener, self.handle_menu)
         VerticalPanel.insert(self, box, index)
 
-    def remove(self, jid):
-        box = self.getContactBox(jid)
+    def remove(self, jid_s):
+        box = self.getContactBox(jid_s)
         if not box:
             return
         VerticalPanel.remove(self, box)
-        self.contacts.remove(jid)
+        self.contacts.remove(jid_s)
 
     def isContactPresent(self, contact_jid):
         """Return True if a contact is present in the panel"""
@@ -229,11 +240,13 @@
     def getContacts(self):
         return self.contacts
 
-    def getContactBox(self, contact_jid):
-        """get contactList widget of a contact
+    def getContactBox(self, contact_jid_s):
+        """get the widget of a contact
+
+        @param contact_jid_s (str): the contact
         @return: ContactBox instance if present, else None"""
         for wid in self:
-            if isinstance(wid, ContactBox) and wid.jid == contact_jid:
+            if isinstance(wid, ContactBox) and wid.jid == contact_jid_s:
                 return wid
         return None
 
@@ -255,19 +268,6 @@
     def __init__(self, host):
         GenericContactList.__init__(self, host, handle_click=True, handle_menu=True)
 
-    def contextMenuHide(self, sender, key):
-        """Return True if the item for that sender should be hidden."""
-        # TODO: enable the blogs of users that are on another server
-        return JID(sender.jid).domain != self.host._defaultDomain
-
-    def add(self, jid_s, name=None):
-        """Add a contact
-
-        @param jid_s (str): JID as unicode
-        @param name (str): nickname
-        """
-        GenericContactList.add(self, jid_s, name)
-
     def setState(self, jid, type_, state):
         """Change the appearance of the contact, according to the state
         @param jid: jid which need to change state
@@ -361,7 +361,7 @@
 
         for group in _new_groups.difference(_current_groups):
             # We add the contact to the groups he joined
-            if not group in self.groups.keys():
+            if group not in self.groups.keys():
                 self.groups[group] = set()
                 self._groupList.add(group)
                 if self.host.uni_box:
@@ -370,45 +370,48 @@
 
         # We add the contact to contact list, it will check if contact already exists
         self._contact_list.add(jid_s)
+        self.updateVisibility([jid_s], self.getContactGroups(jid_s))
 
     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):
+    def setConnected(self, jid_s, resource, availability, priority, statuses):
         """Set connection status
-        @param jid: JID userhost as unicode
+        @param jid_s (str): JID userhost as unicode
         """
         if availability == 'unavailable':
-            if jid in self.connected:
-                if resource in self.connected[jid]:
-                    del self.connected[jid][resource]
-                if not self.connected[jid]:
-                    del self.connected[jid]
+            if jid_s in self.connected:
+                if resource in self.connected[jid_s]:
+                    del self.connected[jid_s][resource]
+                if not self.connected[jid_s]:
+                    del self.connected[jid_s]
         else:
-            if not jid in self.connected:
-                self.connected[jid] = {}
-            self.connected[jid][resource] = (availability, priority, statuses)
+            if jid_s not in self.connected:
+                self.connected[jid_s] = {}
+            self.connected[jid_s][resource] = (availability, priority, statuses)
 
         # check if the contact is connected with another resource, use the one with highest priority
-        if jid in self.connected:
+        if jid_s in self.connected:
             max_resource = max_priority = None
-            for tmp_resource in self.connected[jid]:
-                if max_priority is None or self.connected[jid][tmp_resource][1] >= max_priority:
+            for tmp_resource in self.connected[jid_s]:
+                if max_priority is None or self.connected[jid_s][tmp_resource][1] >= max_priority:
                     max_resource = tmp_resource
-                    max_priority = self.connected[jid][tmp_resource][1]
+                    max_priority = self.connected[jid_s][tmp_resource][1]
             if availability == "unavailable":  # do not check the priority here, because 'unavailable' has a dummy one
                 priority = max_priority
-                availability = self.connected[jid][max_resource][0]
-        if jid not in self.connected or priority >= max_priority:
+                availability = self.connected[jid_s][max_resource][0]
+        if jid_s not in self.connected or priority >= max_priority:
             # case 1: jid not in self.connected means all resources are disconnected, update with 'unavailable'
             # case 2: update (or confirm) with the values of the resource which takes precedence
-            self._contact_list.setState(jid, "availability", availability)
+            self._contact_list.setState(jid_s, "availability", availability)
 
         # update the connected contacts chooser live
         if hasattr(self.host, "room_contacts_chooser") and self.host.room_contacts_chooser is not None:
-                self.host.room_contacts_chooser.resetContacts()
+            self.host.room_contacts_chooser.resetContacts()
+
+        self.updateVisibility([jid_s], self.getContactGroups(jid_s))
 
     def setContactMessageWaiting(self, jid, waiting):
         """Show an visual indicator that contact has send a message
@@ -485,3 +488,43 @@
         @param url (str): image url
         """
         self._contact_list.updateAvatar(jid_s, url)
+
+    def hasVisibleMembers(self, group):
+        """Tell if the given group actually has visible members
+
+        @param group (str): the group to check
+        @return: boolean
+        """
+        for jid in self.groups[group]:
+            if self._contact_list.getContactBox(jid).isVisible():
+                return True
+        return False
+
+    def offlineContactsToShow(self):
+        """Tell if offline contacts should be visible according to the user settings
+
+        @return: boolean
+        """
+        return self.host.getCachedParam('General', C.SHOW_OFFLINE_CONTACTS) == 'true'
+
+    def emtyGroupsToShow(self):
+        """Tell if empty groups should be visible according to the user settings
+
+        @return: boolean
+        """
+        return self.host.getCachedParam('General', C.SHOW_EMPTY_GROUPS) == 'true'
+
+    def updateVisibility(self, jids, groups):
+        """Set the widgets visibility for the given contacts and groups
+
+        @param jids (list[str]): list of JID
+        @param groups (list[str]): list of groups
+        """
+        for jid_s in jids:
+            self._contact_list.getContactBox(jid_s).setVisible(jid_s in self.connected or self.offlineContactsToShow())
+        for group in groups:
+            self._groupList.getGroupBox(group).setVisible(self.hasVisibleMembers(group) or self.emtyGroupsToShow())
+
+    def refresh(self):
+        """Show or hide disconnected contacts and empty groups"""
+        self.updateVisibility(self._contact_list.contacts, self.groups.keys())