changeset 1987:ab439ffe4113

primitivus (chat): moved occupants widget to a dedicated class + display occupants count in footer
author Goffi <goffi@goffi.org>
date Fri, 01 Jul 2016 00:00:11 +0200
parents fbd313cfd40b
children 3f0d22565684
files frontends/src/primitivus/chat.py
diffstat 1 files changed, 47 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/chat.py	Fri Jul 01 00:00:09 2016 +0200
+++ b/frontends/src/primitivus/chat.py	Fri Jul 01 00:00:11 2016 +0200
@@ -36,6 +36,8 @@
 import bisect
 
 
+OCCUPANTS_FOOTER = _(u"{} occupants")
+
 class MessageWidget(urwid.WidgetWrap):
 
     def __init__(self, mess_data):
@@ -198,6 +200,46 @@
         return markup
 
 
+class OccupantsWidget(urwid.WidgetWrap):
+
+    def __init__(self, parent):
+        self.parent = parent
+        self.occupants_walker = urwid.SimpleListWalker([])
+        self.occupants_footer = urwid.Text('', align='center')
+        self.updateFooter()
+        occupants_widget = urwid.Frame(urwid.ListBox(self.occupants_walker), footer=self.occupants_footer)
+        super(OccupantsWidget, self).__init__(occupants_widget)
+        occupants_list = sorted(self.parent.occupants.keys(), key=lambda o:o.lower())
+        for occupant in occupants_list:
+            occupant_data = self.parent.occupants[occupant]
+            self.occupants_walker.append(OccupantWidget(occupant_data))
+
+    def updateFooter(self):
+        """update footer widget"""
+        txt = OCCUPANTS_FOOTER.format(len(self.parent.occupants))
+        self.occupants_footer.set_text(txt)
+
+    def getNicks(self, start=u''):
+        """Return nicks of all occupants
+
+        @param start(unicode): only return nicknames which start with this text
+        """
+        return [w.nick for w in self.occupants_walker if isinstance(w, OccupantWidget) and w.nick.startswith(start)]
+
+    def addUser(self, occupant_data):
+        """add a user to the list"""
+        bisect.insort(self.occupants_walker, OccupantWidget(occupant_data))
+        self.updateFooter()
+        self.parent.host.redraw()  # FIXME: should not be necessary
+
+    def removeUser(self, occupant_data):
+        """remove a user from the list"""
+        for widget in occupant_data.widgets:
+            self.occupants_walker.remove(widget)
+        self.updateFooter()
+        self.parent.host.redraw()  # FIXME: should not be necessary
+
+
 class Chat(PrimitivusWidget, quick_chat.QuickChat):
 
     def __init__(self, host, target, type_=C.CHAT_ONE2ONE, occupants=None, subject=None, profiles=None):
@@ -213,18 +255,11 @@
         # we must adapt the behaviour with the type
         if type_ == C.CHAT_GROUP:
             if len(self.chat_colums.contents) == 1:
-                self.occupants_walker = urwid.SimpleListWalker([])
-                # TODO: put a real ContactPanel class here, based on FocusWidget ?
-                self.occupants_widgets = urwid.ListBox(self.occupants_walker)
+                self.occupants_widget = OccupantsWidget(self)
                 # FIXME
                 # , option_type=sat_widgets.ClickableText, on_click=self._occupantsClicked)
-                self.occupants_panel = sat_widgets.VerticalSeparator(self.occupants_widgets)
+                self.occupants_panel = sat_widgets.VerticalSeparator(self.occupants_widget)
                 self._appendOccupantsPanel()
-                occupants_list = sorted(self.occupants.keys(), key=lambda o:o.lower())
-                for occupant in occupants_list:
-                    occupant_data = self.occupants[occupant]
-                    self.occupants_walker.append(OccupantWidget(occupant_data))
-
                 self.host.addListener('presence', self.presenceListener, [profiles])
 
         # focus marker is a separator indicated last visible message before focus was lost
@@ -273,7 +308,7 @@
 
         space = text.rfind(" ")
         start = text[space + 1:]
-        words = [w.nick for w in self.occupants_walker if isinstance(w, OccupantWidget) and w.nick.startswith(start)]
+        words = self.occupants_widget.getNicks(start)
         if not words:
             return text
         try:
@@ -415,13 +450,12 @@
 
     def addUser(self, nick):
         occupant = super(Chat, self).addUser(nick)
-        bisect.insort(self.occupants_walker, OccupantWidget(occupant))
+        self.occupants_widget.addUser(occupant)
 
     def removeUser(self, occupant_data):
         occupant = super(Chat, self).removeUser(occupant_data)
         if occupant is not None:
-            for widget in occupant.widgets:
-                self.occupants_walker.remove(widget)
+            self.occupants_widget.removeUser(occupant)
 
     def _occupantsClicked(self, list_wid, clicked_wid):
         # FIXME: not called anymore after refactoring