diff browser_side/contact.py @ 252:b77940d8a9bf

browser_side: isolate the basic stuff of ContactList in the new GenericContactList class
author souliane <souliane@mailoo.org>
date Fri, 08 Nov 2013 17:07:27 +0100
parents a25aa882e09a
children da0487f0a2e7
line wrap: on
line diff
--- a/browser_side/contact.py	Tue Nov 05 21:12:14 2013 +0100
+++ b/browser_side/contact.py	Fri Nov 08 17:07:27 2013 +0100
@@ -97,40 +97,25 @@
                 VerticalPanel.remove(self, wid)
 
 
-class ContactList(VerticalPanel):
+class GenericContactList(VerticalPanel):
+    """Class that can be used to represent a contact list, but not necessarily
+    the one that is displayed on the left side. Special features like popup menu
+    panel or changing the contact states must be done in a sub-class."""
 
     def __init__(self, host):
         VerticalPanel.__init__(self)
         self.host = host
         self.contacts = set()
-        self.menu_entries = {"blog": {"title": "Public blog..."}}
-        self.context_menu = PopupMenuPanel(entries=self.menu_entries,
-                                           hide=self.contextMenuHide,
-                                           callback=self.contextMenuCallback,
-                                           vertical=False, menu_style="menu")
 
-    def contextMenuHide(self, sender, key):
-        """Return True if the item for that sender should be hidden."""
-        # TODO: enable the blogs of users that are on another server
-        return JID(sender.jid).domain != self.host._defaultDomain
-
-    def contextMenuCallback(self, sender, key):
-        if key == "blog":
-            # TODO: use the bare when all blogs can be retrieved
-            node = JID(sender.jid).node
-            web_panel = WebPanel(self.host, "blog/%s" % node)
-            self.host.addTab(web_panel, "%s's blog" % node)
-        else:
-            sender.onClick(sender)
-
-    def add(self, jid, name=None):
+    def add(self, jid, name=None, item_cb=None):
         if jid in self.contacts:
             return
         self.contacts.add(jid)
         _item = ContactLabel(self.host, jid, name)
         DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
         VerticalPanel.add(self, _item)
-        self.context_menu.registerRightClickSender(_item)
+        if item_cb is not None:
+            item_cb(_item)
 
     def remove(self, jid):
         wid = self.getContactLabel(jid)
@@ -154,10 +139,41 @@
                 return wid
         return None
 
-    def setState(self, jid, type, state):
+
+class ContactList(GenericContactList):
+    """The contact list that is displayed on the left side."""
+
+    def __init__(self, host):
+        GenericContactList.__init__(self, host)
+        self.menu_entries = {"blog": {"title": "Public blog..."}}
+        self.context_menu = PopupMenuPanel(entries=self.menu_entries,
+                                           hide=self.contextMenuHide,
+                                           callback=self.contextMenuCallback,
+                                           vertical=False, menu_style="menu")
+
+    def contextMenuHide(self, sender, key):
+        """Return True if the item for that sender should be hidden."""
+        # TODO: enable the blogs of users that are on another server
+        return JID(sender.jid).domain != self.host._defaultDomain
+
+    def contextMenuCallback(self, sender, key):
+        if key == "blog":
+            # TODO: use the bare when all blogs can be retrieved
+            node = JID(sender.jid).node
+            web_panel = WebPanel(self.host, "/blog/%s" % node)
+            self.host.addTab(web_panel, "%s's blog" % node)
+        else:
+            sender.onClick(sender)
+
+    def add(self, jid, name=None):
+        def item_cb(item):
+            self.context_menu.registerRightClickSender(item)
+        GenericContactList.add(self, jid, name, item_cb)
+
+    def setState(self, jid, type_, state):
         """Change the appearance of the contact, according to the state
         @param jid: jid which need to change state
-        @param type: one of availability, messageWaiting
+        @param type_: one of availability, messageWaiting
         @param state:
             - for messageWaiting type:
                 True if message are waiting
@@ -165,12 +181,12 @@
                 'unavailable' if not connected, else presence like RFC6121 #4.7.2.1"""
         _item = self.getContactLabel(jid)
         if _item:
-            if type == 'availability':
+            if type_ == 'availability':
                 if state == 'unavailable':
                     _item.removeStyleName('contactConnected')
                 else:
                     _item.addStyleName('contactConnected')
-            elif type == 'messageWaiting':
+            elif type_ == 'messageWaiting':
                 _item.setMessageWaiting(state)