diff frontends/primitivus/contact_list.py @ 125:8d611eb9ae48

primitivus: contact list enhancement - primitivus: contact list now display groups and sexy name (instead of bare jid) - primitivus: contact list now show an alert when somebody want to contact you, and its chat window is not on the screen - primitivus: cursor is now visible when going to chat window (useful to go back in logs)
author Goffi <goffi@goffi.org>
date Mon, 12 Jul 2010 17:50:00 +0800
parents 961e0898271f
children 2240f34f6452
line wrap: on
line diff
--- a/frontends/primitivus/contact_list.py	Thu Jul 08 19:47:54 2010 +0800
+++ b/frontends/primitivus/contact_list.py	Mon Jul 12 17:50:00 2010 +0800
@@ -21,6 +21,7 @@
 
 import urwid
 from quick_frontend.quick_contact_list import QuickContactList
+from tools.jid import JID
 import custom_widgets
 
 
@@ -29,13 +30,13 @@
 
     def __init__(self, host, CM, on_click=None, on_change=None, user_data=None):
         self.host = host
-               
-        self.list_wid = custom_widgets.GenericList([], style=['single','no_first_select'], align='left', on_click=self.__contactClicked, on_change=on_change)
-
+        self.selected = None
+        self.groups={}
+        self.alert_jid=set()
+        
         #we now build the widget
-        frame_body = self.list_wid
-        frame = urwid.Frame(frame_body)
-        self.main_widget = custom_widgets.LabelLine(frame, custom_widgets.SurroundedText(_("Contacts")))
+        self.frame = urwid.Frame(self.__buildList())
+        self.main_widget = custom_widgets.LabelLine(self.frame, custom_widgets.SurroundedText(_("Contacts")))
         urwid.WidgetWrap.__init__(self, self.main_widget)
         if on_click:
             urwid.connect_signal(self, 'click', on_click, user_data)
@@ -44,38 +45,126 @@
         QuickContactList.__init__(self, CM)
 
     def __contains__(self, jid):
-        contacts = self.list_wid.getAllValues()
-        return jid.short in contacts
+        for group in self.groups:
+            if jid.short in self.groups[group][1]:
+                return True
+        return False
+
+    def setFocus(self, name):
+        """give focus to the first group or contact with the given name"""
+        idx = 0
+        for widget in self.frame.body.body:
+            if widget.getValue() == name:
+                self.frame.body.set_focus(idx)
+                return
+            idx+=1
+
+    def putAlert(self, jid):
+        """Put an alert on the jid to get attention from user (e.g. for new message)"""
+        self.alert_jid.add(jid.short)
+        self.frame.body = self.__buildList()
+        self.host.redraw()
+
+    def __groupClicked(self, group_wid):
+        group = self.groups[group_wid.getValue()]
+        group[0] = not group[0]
+        self.frame.body = self.__buildList()
+        self.host.redraw()
+        self.setFocus(group_wid.getValue())
 
-    def __contactClicked(self, list_wid):
+    def __contactClicked(self, contact_wid, selected):
+        self.selected = contact_wid.data
+        for widget in self.frame.body.body:
+            if widget.__class__ == custom_widgets.SelectableText:
+                widget.setState(widget.data == self.selected, invisible=True)
+        if self.selected in self.alert_jid:
+            self.alert_jid.remove(self.selected)
+            self.frame.body = self.__buildList()
+            self.host.redraw()
         self._emit('click')
 
+    def __buildContact(self, content, param_contacts):
+        """Add contact representation in widget list
+        @param content: widget list, e.g. SimpleListWalker
+        @param contacts: list of JID"""
+        contacts = list(param_contacts)
+        contacts.sort()
+        for contact in contacts:
+            jid=JID(contact) 
+            name = self.CM.getAttr(jid,'name')
+            nick = self.CM.getAttr(jid,'nick')
+            display = nick or name or jid.node or jid.short
+            header = '(*) ' if contact in self.alert_jid else ''
+            widget = custom_widgets.SelectableText(display, selected = contact==self.selected, header=header, data=contact)
+            if contact in self.alert_jid:
+                widget.setAttribute('default','alert')
+            content.append(widget)
+            urwid.connect_signal(widget, 'change', self.__contactClicked)
+
+    def __buildList(self):
+        """Build the main contact list widget"""
+        content = urwid.SimpleListWalker([])
+        group_keys = self.groups.keys()
+        group_keys.sort()
+        for key in group_keys:
+            unfolded = self.groups[key][0]
+            if key!=None:
+                header = '[-]' if unfolded else '[+]'
+                widget = custom_widgets.ClickableText(key,header=header+' ')
+                content.append(widget)
+                urwid.connect_signal(widget, 'click', self.__groupClicked)
+            if unfolded:
+                self.__buildContact(content, self.groups[key][1])
+        return urwid.ListBox(content)
+
     def get_contact(self):
         """Return contact currently selected"""
-        return self.list_wid.getSelectedValue()
+        return self.selected
             
     def clear_contacts(self):
         """clear all the contact list"""
-        self.list_wid.changeValues([])
+        self.groups={}
 
-    def replace(self, jid, groups=None):
+    def replace(self, jid, groups=[None]):
         """add a contact to the list if doesn't exist, else update it"""
-        contacts = self.list_wid.getAllValues()
+        assert groups.__class__ == list
+        assert jid.__class__ == JID
+        if not groups:
+            groups=[None]
+        for group in groups:
+            if not self.groups.has_key(group):
+                self.groups[group] = [True,set()]  #[unfold,list_of_contacts]
+            self.groups[group][1].add(jid.short)
+        self.frame.body = self.__buildList()
+        self.host.redraw()
+
+
+        """contacts = self.list_wid.getAllValues()
         if jid.short not in contacts:
             contacts.append(jid.short)
             contacts.sort()
             self.list_wid.changeValues(contacts)
-            self._emit('change')
+            self._emit('change')"""
     
     def disconnect(self, jid):
         """mark a contact disconnected"""
-        self.remove(jid)
+        self.remove(jid.short)
     
     def remove(self, jid):
         """remove a contact from the list"""
-        self.list_wid.deleteValue(jid.short)
+        groups_to_remove = []
+        for group in self.groups:
+            contacts = self.groups[group][1]
+            if jid.short in contacts:
+                contacts.remove(jid.short)
+                if not len(contacts):
+                    groups_to_remove.append(group)
+        for group in groups_to_remove:
+            del self.groups[group]
+        self.frame.body = self.__buildList()
+        self.host.redraw()
     
-    def add(self, jid, param_groups=None):
+    def add(self, jid, param_groups=[None]):
         """add a contact to the list"""
-        self.replace(jid)
+        self.replace(jid,param_groups)