diff src/browser/sat_browser/contact.py @ 479:c21ea1fe3593

browser side: small refactoring to prepare displaying avatars in the contact panel
author souliane <souliane@mailoo.org>
date Sat, 14 Jun 2014 19:10:51 +0200
parents 97c72fe4a5f2
children 50b286866739
line wrap: on
line diff
--- a/src/browser/sat_browser/contact.py	Thu Jun 12 22:37:22 2014 +0200
+++ b/src/browser/sat_browser/contact.py	Sat Jun 14 19:10:51 2014 +0200
@@ -38,22 +38,23 @@
 import html_tools
 
 
-def setPresenceStyle(element, presence, base_style="contact"):
+def setPresenceStyle(widget, presence, base_style="contactLabel"):
     """
     Set the CSS style of a contact's element according to its presence.
-    @param item: the UI element of the contact
-    @param presence: a value in ("", "chat", "away", "dnd", "xa").
-    @param base_style: the base name of the style to apply
+
+    @param widget (Widget): the UI element of the contact
+    @param presence (str): a value in ("", "chat", "away", "dnd", "xa").
+    @param base_style (str): the base name of the style to apply
     """
-    if not hasattr(element, 'presence_style'):
-        element.presence_style = None
+    if not hasattr(widget, 'presence_style'):
+        widget.presence_style = None
     style = '%s-%s' % (base_style, presence or 'connected')
-    if style == element.presence_style:
+    if style == widget.presence_style:
         return
-    if element.presence_style is not None:
-        element.removeStyleName(element.presence_style)
-    element.addStyleName(style)
-    element.presence_style = style
+    if widget.presence_style is not None:
+        widget.removeStyleName(widget.presence_style)
+    widget.addStyleName(style)
+    widget.presence_style = style
 
 
 class GroupLabel(base_widget.DragLabel, Label, ClickHandler):
@@ -77,24 +78,25 @@
         self.name = name or jid
         self.waiting = False
         self.jid = jid
-        self._fill()
-        self.setStyleName('contact')
+        self.refresh()
+        self.setStyleName('contactLabel')
         base_widget.DragLabel.__init__(self, jid, "CONTACT")
         if handleClick:
             ClickHandler.__init__(self)
             self.addClickListener(self)
 
-    def _fill(self):
+    def refresh(self):
         if self.waiting:
-            _wait_html = "<b>(*)</b>&nbsp;"
-        self.setHTML("%(wait)s%(name)s" % {'wait': _wait_html,
+            wait_html = "<b>(*)</b>&nbsp;"
+        self.setHTML("%(wait)s%(name)s" % {'wait': wait_html,
                                            'name': html_tools.html_sanitize(self.name)})
 
     def setMessageWaiting(self, waiting):
         """Show a visual indicator if message are waiting
+
         @param waiting: True if message are waiting"""
         self.waiting = waiting
-        self._fill()
+        self.refresh()
 
     def onClick(self, sender):
         self.host.getOrCreateLiberviaWidget(panels.ChatPanel, self.jid)
@@ -135,7 +137,13 @@
         self.contacts = []
         self.handleClick = handleClick
 
-    def add(self, jid, name=None, item_cb=None):
+    def add(self, jid, name=None, add_item_cb=None):
+        """Add a contact to the list.
+
+        @param jid (str): JID of the contact
+        @param name (str): optional name of the contact
+        @param add_item_cb (method): to be called on the contact's widget after it's been added to the list
+        """
         if jid in self.contacts:
             return
         index = 0
@@ -147,8 +155,8 @@
         _item = ContactLabel(self.host, jid, name, handleClick=self.handleClick)
         DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
         VerticalPanel.insert(self, _item, index)
-        if item_cb is not None:
-            item_cb(_item)
+        if add_item_cb:
+            add_item_cb(box)
 
     def remove(self, jid):
         wid = self.getContactLabel(jid)
@@ -204,9 +212,7 @@
         @param jid_s (str): JID as unicode
         @param name (str): nickname
         """
-        def item_cb(item):
-            self.context_menu.registerRightClickSender(item)
-        GenericContactList.add(self, jid_s, name, item_cb)
+        GenericContactList.add(self, jid_s, name, add_item_cb=lambda item: self.context_menu.registerRightClickSender(item))
 
     def setState(self, jid, type_, state):
         """Change the appearance of the contact, according to the state
@@ -264,7 +270,7 @@
         self.vPanel.add(self._contact_list)
         self.scroll_panel.add(self.vPanel)
         self.add(self.scroll_panel)
-        self.setStyleName('contactBox')
+        self.setStyleName('contactPanel')
         Window.addWindowResizeListener(self)
 
     def onWindowResized(self, width, height):