diff frontends/src/primitivus/contact_list.py @ 1230:3abc6563a0d2

primitivus: implement parameter "Show empty groups"
author souliane <souliane@mailoo.org>
date Mon, 06 Oct 2014 13:54:41 +0200
parents 03661d1b216a
children 93a5e2673929
line wrap: on
line diff
--- a/frontends/src/primitivus/contact_list.py	Mon Oct 06 12:44:58 2014 +0200
+++ b/frontends/src/primitivus/contact_list.py	Mon Oct 06 13:54:41 2014 +0200
@@ -41,6 +41,9 @@
         self.alert_jid=set()
         self.show_status = False
         self.show_disconnected = False
+        self.show_empty_groups = True
+        # TODO: this may lead to two successive UI refresh and needs an optimization
+        self.host.bridge.asyncGetParamA(C.SHOW_EMPTY_GROUPS, "General", profile_key=host.profile, callback=self.showEmptyGroups)
         self.host.bridge.asyncGetParamA(C.SHOW_OFFLINE_CONTACTS, "General", profile_key=host.profile, callback=self.showOfflineContacts)
 
         #we now build the widget
@@ -141,13 +144,13 @@
         self.update()
         self._emit('click')
 
-    def __buildContact(self, content, param_contacts):
+    def __buildContact(self, content, contacts):
         """Add contact representation in widget list
         @param content: widget list, e.g. SimpleListWalker
-        @param contacts: list of JID"""
-        contacts = list(param_contacts)
-
-        widgets = [] #list of built widgets
+        @param contacts (list): list of JID userhosts"""
+        if not contacts:
+            return
+        widgets = []  # list of built widgets
 
         for contact in contacts:
             if contact.startswith(C.PRIVATE_PREFIX):
@@ -155,28 +158,27 @@
                 show_icon = ''
                 status = ''
             else:
-                jid=JID(contact)
+                jid = JID(contact)
                 name = self.getCache(jid, 'name')
                 nick = self.getCache(jid, 'nick')
                 status = self.getCache(jid, 'status')
                 show = self.getCache(jid, 'show')
-                if show == None:
+                if show is None:
                     show = "unavailable"
-                if (not self.show_disconnected and show == "unavailable"
-                    and not contact in self.alert_jid and contact != self.selected):
+                if not self.contactToShow(contact):
                     continue
                 show_icon, show_attr = C.PRESENCE.get(show, ('', 'default'))
                 contact_disp = ('alert' if contact in self.alert_jid else show_attr, nick or name or jid.node or jid.bare)
-            display = [ show_icon + " " , contact_disp]
+            display = [show_icon + " ", contact_disp]
             if self.show_status:
-                status_disp = ('status',"\n  " + status) if status else ""
+                status_disp = ('status', "\n  " + status) if status else ""
                 display.append(status_disp)
             header = '(*) ' if contact in self.alert_jid else ''
             widget = sat_widgets.SelectableText(display,
-                                                selected = contact==self.selected,
+                                                selected=contact == self.selected,
                                                 header=header)
             widget.data = contact
-            widget.comp = contact_disp[1].lower() #value to use for sorting
+            widget.comp = contact_disp[1].lower()  # value to use for sorting
             widgets.append(widget)
 
         widgets.sort(key=lambda widget: widget.comp)
@@ -212,18 +214,37 @@
             content.append(urwid.Divider('='))
 
         group_keys = self.groups.keys()
-        group_keys.sort(key = lambda x: x.lower() if x else x)
+        group_keys.sort(key=lambda x: x.lower() if x else x)
         for key in group_keys:
             unfolded = self.groups[key][0]
-            if key!=None:
+            contacts = list(self.groups[key][1])
+            if key is not None and (self.nonEmptyGroup(contacts) or self.show_empty_groups):
                 header = '[-]' if unfolded else '[+]'
-                widget = sat_widgets.ClickableText(key,header=header+' ')
+                widget = sat_widgets.ClickableText(key, header=header + ' ')
                 content.append(widget)
                 urwid.connect_signal(widget, 'click', self.__groupClicked)
             if unfolded:
-                self.__buildContact(content, self.groups[key][1])
+                self.__buildContact(content, contacts)
         return urwid.ListBox(content)
 
+    def contactToShow(self, contact):
+        """Tell if the contact should be showed or hidden.
+
+        @param contact (str): JID userhost of the contact
+        @return: True if that contact should be showed in the list"""
+        return self.getCache(JID(contact), 'show') != "unavailable" or self.show_disconnected or contact in self.alert_jid or contact == self.selected
+
+    def nonEmptyGroup(self, contacts):
+        """Tell if a contact group contains some contacts to show.
+
+        @param contacts (list[str]): list of JID userhosts
+        @return: bool
+        """
+        for contact in contacts:
+            if self.contactToShow(contact):
+                return True
+        return False
+
     def unselectAll(self):
         """Unselect all contacts"""
         self.selected = None
@@ -326,3 +347,10 @@
             return
         self.show_disconnected = show
         self.update()
+
+    def showEmptyGroups(self, show):
+        show = C.bool(show)
+        if self.show_empty_groups == show:
+            return
+        self.show_empty_groups = show
+        self.update()