diff frontends/wix/main_window.py @ 53:6dfe5bb10008

Wix: groups in contact list, first draft
author Goffi <goffi@goffi.org>
date Sun, 10 Jan 2010 16:42:09 +1100
parents 6455fb62ff83
children 2ce9e350cdf9
line wrap: on
line diff
--- a/frontends/wix/main_window.py	Thu Jan 07 01:55:30 2010 +1100
+++ b/frontends/wix/main_window.py	Sun Jan 10 16:42:09 2010 +1100
@@ -74,14 +74,17 @@
         wx.SimpleHtmlListBox.__init__(self, parent, -1)
         QuickContactList.__init__(self, CM)
         self.host = parent
+        self.groups = {}  #list contacts in each groups, key = group
+        self.Bind(wx.EVT_LISTBOX, self.onSelected)
         self.Bind(wx.EVT_LISTBOX_DCLICK, self.onActivated)
 
-    def __find_idx(self, jid, reverse=False):
+    def __find_idx(self, entity, reverse=False):
         """Find indexes of given jid in contact list
         @return: list of indexes"""
         result=[]
         for i in range(self.GetCount()):
-            if self.GetClientData(i).short == jid.short:
+            if (type(entity) == JID and type(self.GetClientData(i)) == JID and self.GetClientData(i).short == entity.short) or\
+                self.GetClientData(i) == entity:
                 result.append(i)
         return result
 
@@ -96,6 +99,12 @@
     def disconnect(self, jid):
         self.remove(jid) #for now, we only show online contacts
 
+    def __presentGroup(self, group):
+        """Make a nice presentation for the contact groups"""
+        html = """[%s]""" % group
+
+        return html
+
     def __presentItem(self, jid):
         """Make a nice presentation of the contact in the list."""
         name = self.CM.getAttr(jid,'name')
@@ -133,9 +142,21 @@
     def add(self, jid):
         """add a contact to the list"""
         debug ("adding %s",jid)
-        idx = self.Append(self.__presentItem(jid))
+        groups = self.CM.getAttr(jid, 'groups')
+        if not groups:
+            idx = self.Append(self.__presentItem(jid), jid)
+        else:
+            for group in groups:
+                indexes = self.__find_idx(group)
+                gp_idx = 0
+                if not indexes:  #this is a new group, we have to create it
+                    gp_idx = self.Append(self.__presentGroup(group), group)
+                else:
+                    gp_idx = indexes[0]
 
-        self.SetClientData(idx, jid)
+                self.Insert(self.__presentItem(jid), gp_idx+1, jid)
+
+
 
     def remove(self, jid):
         """remove a contact from the list"""
@@ -145,6 +166,16 @@
         for i in list_idx:
             self.Delete(i)
 
+    def onSelected(self, event):
+        """Called when a contact is selected."""
+        data = self.getSelection()
+        if type(data) == JID:
+            event.Skip()
+        else:
+            #We don't want to select groups
+            self.SetSelection(wx.NOT_FOUND)
+            event.Skip(False)
+    
     def onActivated(self, event):
         """Called when a contact is clicked or activated with keyboard."""
         data = self.getSelection()