changeset 634:16a5da120b7f frontends_multi_profiles

browser side (contact list): Contact list improvments: - removed optional arguments from getContactBox: they are now managed by the hosting panel, as the arguments should be same for all contacts in a panel - contact name is no more specified on ContactBox creation, is gotten by the ContactLabel itself using the cache (TODO: "nick" listener) - renamed setMessageXXX to setAlert for consitency with QuickFrontend (furthermore the alert could be used for something else than waiting messages) - ContactBoxes are stored using bare jid - parent is used in ContactBox instead of host
author Goffi <goffi@goffi.org>
date Mon, 23 Feb 2015 18:44:57 +0100
parents 617f7a5c5312
children 40c3c6aaafaa
files src/browser/sat_browser/contact_list.py
diffstat 1 files changed, 57 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/sat_browser/contact_list.py	Mon Feb 23 18:43:15 2015 +0100
+++ b/src/browser/sat_browser/contact_list.py	Mon Feb 23 18:44:57 2015 +0100
@@ -93,24 +93,31 @@
 
 
 class ContactLabel(HTML):
-    def __init__(self, jid_, name=None):
+    """Display a contact in HTML, selecting best display (jid/nick/etc)"""
+
+    def __init__(self, host, jid_):
+        # TODO: add a listener for nick changes
         HTML.__init__(self)
-        self.name = name or unicode(jid_)
-        self.waiting = False
+        self.host = host
+        self.jid = jid_.bare
+        self.nick = self.host.contact_lists[C.PROF_KEY_NONE].getCache(self.jid, "nick")
+        self.alert = False
         self.refresh()
         self.setStyleName('contactLabel')
 
     def refresh(self):
-        if self.waiting:
-            wait_html = "<b>(*)</b>&nbsp;"
-        self.setHTML("%(wait)s%(name)s" % {'wait': wait_html,
-                                           'name': html_tools.html_sanitize(self.name)})
+        alert_html = "<strong>(*)</strong>&nbsp;" if self.alert else ""
+        contact_html = html_tools.html_sanitize(self.nick or unicode(self.jid))
+        html = "%(alert)s%(contact)s" % {'alert': alert_html,
+                                         'contact': contact_html}
+        self.setHTML(html)
 
-    def setMessageWaiting(self, waiting):
-        """Show a visual indicator if message are waiting
+    def setAlert(self, alert):
+        """Show a visual indicator
 
-        @param waiting: True if message are waiting"""
-        self.waiting = waiting
+        @param alert: True if alert must be shown
+        """
+        self.alert = alert
         self.refresh()
 
 
@@ -131,37 +138,31 @@
 
 class ContactBox(VerticalPanel, ClickHandler, base_widget.DragLabel):
 
-    def __init__(self, host, jid_, name=None, on_click=None, handle_menu=None):
+    def __init__(self, parent, jid_):
         """
-
-        @param host (SatWebFrontend): %(doc_host)s
+        @param parent (ContactPanel): ContactPanel hosting this box
         @param jid_ (jid.JID): contact JID
-        @param name (unicode): contact alias
-        @param on_click (callable): click callback
-        @param handle_menu (bool): if True, bind a popup menu to the avatar
         """
         VerticalPanel.__init__(self, StyleName='contactBox', VerticalAlignment='middle')
-        base_widget.DragLabel.__init__(self, jid_, "CONTACT", host)
-        self.jid = jid_
-        self.label = ContactLabel(jid_, name)
-        self.avatar = ContactMenuBar(self, host) if handle_menu else Image()
-        self.updateAvatar(host.getAvatarURL(jid_))
+        ClickHandler.__init__(self)
+        base_widget.DragLabel.__init__(self, jid_, "CONTACT", parent.host)
+        self.jid = jid_.bare
+        self.label = ContactLabel(parent.host, self.jid)
+        self.avatar = ContactMenuBar(self, parent.host) if parent.handle_menu else Image()
+        self.updateAvatar(parent.host.getAvatarURL(self.jid))
         self.add(self.avatar)
         self.add(self.label)
-        if on_click:
-            ClickHandler.__init__(self)
-            self.addClickListener(self)
-            self._on_click = on_click
+        self.addClickListener(self)
 
     def addMenus(self, menu_bar):
         menu_bar.addCachedMenus(C.MENU_ROSTER_JID_CONTEXT, {'jid': unicode(self.jid)})
         menu_bar.addCachedMenus(C.MENU_JID_CONTEXT, {'jid': unicode(self.jid)})
 
-    def setMessageWaiting(self, waiting):
-        """Show a visual indicator if message are waiting
+    def setAlert(self, alert):
+        """Show a visual indicator
 
-        @param waiting: True if message are waiting"""
-        self.label.setMessageWaiting(waiting)
+        @param alert: True if alert indicator show be shown"""
+        self.label.setAlert(alert)
 
     def updateAvatar(self, url):
         """Update the avatar.
@@ -171,7 +172,12 @@
         self.avatar.setUrl(url)
 
     def onClick(self, sender):
-        self._on_click(self.jid)
+        try:
+            self.parent.onClick(self.jid)
+        except AttributeError:
+            pass
+        else:
+            self.setAlert(False)
 
 
 class GroupPanel(VerticalPanel):
@@ -225,17 +231,21 @@
     Special features like popup menu panel or changing the contact states must be done in a sub-class.
     """
 
-    def __init__(self, host, handle_click=False, handle_menu=False):
+    def __init__(self, parent, handle_click=True, handle_menu=True):
+        """@param on_click (callable): click callback (used if ContactBox is created)
+        @param handle_menu (bool): if True, bind a popup menu to the avatar (used if ContactBox is created)
+        """ # FIXME
         VerticalPanel.__init__(self)
-        self.host = host
+        self._parent = parent
+        self.host = parent.host
         self._contacts = {} # entity jid to ContactBox map
         self.click_listener = None
         self.handle_menu = handle_menu
 
         if handle_click:
             def cb(contact_jid):
-                host.displayWidget(chat.Chat, contact_jid, type_=C.CHAT_ONE2ONE)
-            self.click_listener = cb
+                self.host.displayWidget(chat.Chat, contact_jid, type_=C.CHAT_ONE2ONE)
+            self.onClick = cb
 
     def display(self, jids):
         """Display a contact in the list.
@@ -261,22 +271,18 @@
     def getContacts(self):
         return self._contacts
 
-    def getContactBox(self, contact_jid, name=None, on_click=None, handle_menu=None):
+    def getContactBox(self, contact_jid):
         """get the Contactbox of a contact
 
         if the Contactbox doesn't exists, it will be created
         @param contact_jid (jid.JID): the contact
-        @param name (unicode): contact alias (used if ContactBox is created)
-        @param on_click (callable): click callback (used if ContactBox is created)
-        @param handle_menu (bool): if True, bind a popup menu to the avatar (used if ContactBox is created)
         @return: ContactBox instance
         """
-        assert isinstance(contact_jid, jid.JID)
         try:
-            return self._contacts[contact_jid]
+            return self._contacts[contact_jid.bare]
         except KeyError:
-            box = ContactBox(self.host, contact_jid, name, on_click, handle_menu)
-            self._contacts[contact_jid] = box
+            box = ContactBox(self, contact_jid)
+            self._contacts[contact_jid.bare] = box
             return box
 
     def updateAvatar(self, jid_, url):
@@ -294,11 +300,12 @@
 class ContactsPanel(BaseContactsPanel):
     """The contact list that is displayed on the left side."""
 
-    def __init__(self, host):
-        BaseContactsPanel.__init__(self, host, handle_click=True, handle_menu=True)
+    def __init__(self, parent):
+        BaseContactsPanel.__init__(self, parent, handle_click=True, handle_menu=True)
 
     def setState(self, jid_, type_, state):
         """Change the appearance of the contact, according to the state
+
         @param jid_ (jid.JID): jid.JID which need to change state
         @param type_ (unicode): one of "availability", "messageWaiting"
         @param state:
@@ -313,10 +320,11 @@
                 state = C.PRESENCE_UNAVAILABLE
             setPresenceStyle(contact_box.label, state)
         elif type_ == 'messageWaiting':
-            contact_box.setMessageWaiting(state)
+            contact_box.setAlert(state)
 
 
 class ContactTitleLabel(base_widget.DragLabel, Label, ClickHandler):
+
     def __init__(self, host, text):
         Label.__init__(self, text)  # , Element=DOM.createElement('div')
         self.setStyleName('contactTitle')
@@ -334,11 +342,12 @@
     def __init__(self, host):
         QuickContactList.__init__(self, host, C.PROF_KEY_NONE)
         SimplePanel.__init__(self)
+        self.host = host
         self.scroll_panel = ScrollPanel()
         self.vPanel = VerticalPanel()
         _title = ContactTitleLabel(host, 'Contacts')
         DOM.setStyleAttribute(_title.getElement(), "cursor", "pointer")
-        self._contacts_panel = ContactsPanel(host)
+        self._contacts_panel = ContactsPanel(self)
         self._contacts_panel.setStyleName('contactPanel') # FIXME: style doesn't exists !
         self._group_panel = GroupPanel(self)
 
@@ -569,6 +578,7 @@
         @param group (unicode): the group to check
         @return: boolean
         """
+        raise Exception # FIXME: remove this method
         for jid_ in self.groups[group]:
             if self._contacts_panel.getContactBox(jid_).isVisible():
                 return True