diff src/browser/sat_browser/contact_panel.py @ 695:e86490a7c76e

browser_side: don't rebuild the whole list on contact list update
author souliane <souliane@mailoo.org>
date Sun, 19 Apr 2015 13:10:41 +0200
parents 7a9c7b9f6a28
children 102bab805ec1
line wrap: on
line diff
--- a/src/browser/sat_browser/contact_panel.py	Thu Apr 16 14:57:02 2015 +0200
+++ b/src/browser/sat_browser/contact_panel.py	Sun Apr 19 13:10:41 2015 +0200
@@ -84,15 +84,24 @@
         @param jids (list[jid.JID]): jids to display (the order is kept)
         @param name (unicode): optional name of the contact
         """
-        # FIXME: we do a full clear and add boxes after, we should only remove recently hidden boxes and add new ones, and re-order
-        current = [box.jid for box in self.children if isinstance(box, contact_widget.ContactBox)]
-        if current == jids:
+        current_jids = [box.jid for box in self.children if isinstance(box, contact_widget.ContactBox)]
+        if current_jids == jids:
             # the display doesn't change
             return
-        self.clear()
+        for contact_jid in set(current_jids).difference(jids):
+            self.removeContactBox(contact_jid)
+        current_index = 0
+        insert_count = 0
         for contact_jid in jids:
+            try:
+                if current_jids[current_index] == contact_jid:
+                    current_index += 1
+                    continue
+            except IndexError:
+                pass
             assert isinstance(contact_jid, jid.JID)
-            self.updateContactBox(contact_jid)
+            self.updateContactBox(contact_jid, current_index + insert_count)
+            insert_count += 1
 
     def getContactBox(self, contact_jid):
         """Get the contact box for the given contact.
@@ -105,10 +114,11 @@
         except KeyError:
             return None
 
-    def updateContactBox(self, contact_jid):
+    def updateContactBox(self, contact_jid, index=None):
         """Add a contact or update it if it already exists.
 
         @param contact_jid (jid.JID): contact JID
+        @param index (int): insertion index if the contact is added
         @return: ContactBox
         """
         box = self.getContactBox(contact_jid)
@@ -121,7 +131,10 @@
                                             display=self.contacts_display,
                                             plugin_menu_context=self.contacts_menus)
             self._contacts[self._key(contact_jid)] = box
-            VerticalPanel.append(self, box)
+            if index:
+                VerticalPanel.insert(self, box, index)
+            else:
+                VerticalPanel.append(self, box)
         return box
 
     def removeContactBox(self, contact_jid):