diff frontends/src/primitivus/contact_list.py @ 501:e9634d2e7b38

core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1: - QuickContactManagement is not used anymore and will be removed, ContactList + Core are used instead - disconnected contacts are now displayed in Primitivus (M-d to show/hide them) - avatars are temporary unavailable in wix - new bridge method: getContactsFromGroup
author Goffi <goffi@goffi.org>
date Tue, 25 Sep 2012 00:58:34 +0200
parents 00d3679976ab
children debcf5dd404a
line wrap: on
line diff
--- a/frontends/src/primitivus/contact_list.py	Wed Sep 05 00:19:32 2012 +0200
+++ b/frontends/src/primitivus/contact_list.py	Tue Sep 25 00:58:34 2012 +0200
@@ -28,12 +28,13 @@
 class ContactList(urwid.WidgetWrap, QuickContactList):
     signals = ['click','change']
 
-    def __init__(self, host, CM, on_click=None, on_change=None, user_data=None):
+    def __init__(self, host, on_click=None, on_change=None, user_data=None):
         self.host = host
         self.selected = None
         self.groups={}
         self.alert_jid=set()
         self.show_status = False
+        self.show_disconnected = False
         
         #we now build the widget
         self.frame = urwid.Frame(self.__buildList())
@@ -43,9 +44,9 @@
             urwid.connect_signal(self, 'click', on_click, user_data)
         if on_change:
             urwid.connect_signal(self, 'change', on_change, user_data)
-        QuickContactList.__init__(self, CM)
+        QuickContactList.__init__(self)
 
-    def _update(self):
+    def update(self):
         """Update display, keep focus"""
         widget, position = self.frame.body.get_focus()
         self.frame.body = self.__buildList()
@@ -53,10 +54,16 @@
             self.frame.body.set_focus(position)
         self.host.redraw()
 
+    def update_jid(self, jid):
+        self.update()
+
     def keypress(self, size, key):
         if key == "meta s": #user wants to (un)hide contacts' statuses
             self.show_status = not self.show_status
-            self._update()
+            self.update()
+        elif key == "meta d": #user wants to (un)hide disconnected contacts
+            self.show_disconnected = not self.show_disconnected
+            self.update()
         return super(ContactList, self).keypress(size, key) 
     
     def __contains__(self, jid):
@@ -77,12 +84,12 @@
     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._update()
+        self.update()
 
     def __groupClicked(self, group_wid):
         group = self.groups[group_wid.getValue()]
         group[0] = not group[0]
-        self._update()
+        self.update()
         self.setFocus(group_wid.getValue())
 
     def __contactClicked(self, contact_wid, selected):
@@ -92,7 +99,7 @@
                 widget.setState(widget.data == self.selected, invisible=True)
         if self.selected in self.alert_jid:
             self.alert_jid.remove(self.selected)
-            self._update()
+        self.update()
         self._emit('click')
 
     def __buildContact(self, content, param_contacts):
@@ -100,13 +107,20 @@
         @param content: widget list, e.g. SimpleListWalker
         @param contacts: list of JID"""
         contacts = list(param_contacts)
-        contacts.sort()
+        
+        widgets = [] #list of built widgets
+        
         for contact in contacts:
             jid=JID(contact) 
-            name = self.CM.getAttr(jid,'name')
-            nick = self.CM.getAttr(jid,'nick')
-            status = self.CM.getAttr(jid, 'status')
-            show = self.CM.getAttr(jid, 'show')
+            name = self.getCache(jid, 'name')
+            nick = self.getCache(jid, 'nick')
+            status = self.getCache(jid, 'status')
+            show = self.getCache(jid, 'show')
+            if show == None:
+                show = "unavailable"
+            if (not self.show_disconnected and show == "unavailable"
+                and not contact in self.alert_jid and contact != self.selected):
+                continue
             show_icon, show_attr = const_SHOW_ICON.get(show,('','default'))
             contact_disp = ('alert' if contact in self.alert_jid else show_attr, nick or name or jid.node or jid.short)
             display = [ show_icon + " " , contact_disp]
@@ -118,6 +132,12 @@
                                                 selected = contact==self.selected,
                                                 header=header)
             widget.data = contact
+            widget.comp = contact_disp[1].lower() #value to use for sorting
+            widgets.append(widget)
+       
+        widgets.sort(key=lambda widget: widget.comp)
+
+        for widget in widgets:
             content.append(widget)
             urwid.connect_signal(widget, 'change', self.__contactClicked)
 
@@ -125,7 +145,7 @@
         """Build the main contact list widget"""
         content = urwid.SimpleListWalker([])
         group_keys = self.groups.keys()
-        group_keys.sort()
+        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:
@@ -145,28 +165,30 @@
                 widget.setState(False, invisible=True)
 
 
-    def get_contact(self):
+    def getContact(self):
         """Return contact currently selected"""
         return self.selected
             
-    def clear_contacts(self):
+    def clearContacts(self):
         """clear all the contact list"""
         self.groups={}
         self.selected = None
         self.unselectAll()
-        self._update()
+        self.update()
 
-    def replace(self, jid, groups=[None]):
+    def replace(self, jid, groups=None, attributes=None):
         """add a contact to the list if doesn't exist, else update it"""
+        if not groups:
+            groups = [None]
+        if not attributes:
+            attributes={}
         assert isinstance(groups, list)
         assert isinstance(jid, 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._update()
+        self.update()
 
 
         """contacts = self.list_wid.getAllValues()
@@ -176,10 +198,6 @@
             self.list_wid.changeValues(contacts)
             self._emit('change')"""
     
-    def disconnect(self, jid):
-        """mark a contact disconnected"""
-        self.remove(jid.short)
-    
     def remove(self, param_jid):
         """remove a contact from the list"""
         groups_to_remove = []
@@ -192,7 +210,7 @@
                     groups_to_remove.append(group)
         for group in groups_to_remove:
             del self.groups[group]
-        self._update()
+        self.update()
     
     def add(self, jid, param_groups=[None]):
         """add a contact to the list"""