diff src/browser/sat_browser/contact.py @ 495:587fe75d1b16

browser_side: handle menus of type ROOM, SINGLE and ROSTER_JID
author souliane <souliane@mailoo.org>
date Tue, 15 Jul 2014 18:45:33 +0200
parents 5911f71acc80
children 60be99de3808
line wrap: on
line diff
--- a/src/browser/sat_browser/contact.py	Tue Jul 15 18:43:55 2014 +0200
+++ b/src/browser/sat_browser/contact.py	Tue Jul 15 18:45:33 2014 +0200
@@ -34,7 +34,6 @@
 
 from constants import Const as C
 from jid import JID
-import base_panels
 import base_widget
 import panels
 import html_tools
@@ -95,8 +94,23 @@
         self.refresh()
 
 
-class ContactBox(base_widget.DragLabel, VerticalPanel, ClickHandler):
-    def __init__(self, host, jid, name=None, handleClick=True):
+class ContactMenuBar(base_widget.WidgetMenuBar):
+
+    ITEM_TPL = "<img src='media/icons/misc/%s.png' />"
+
+    def __init__(self, host, menu_data):
+        base_widget.WidgetMenuBar.__init__(self, host)
+        self.addCachedMenus(C.MENU_ROSTER_JID_CONTEXT, menu_data)
+        self.addCachedMenus(C.MENU_JID_CONTEXT, menu_data)
+
+    def onBrowserEvent(self, event):
+        base_widget.WidgetMenuBar.onBrowserEvent(self, event)
+        event.stopPropagation()  # prevent opening the chat dialog
+
+
+class ContactBox(VerticalPanel, ClickHandler, base_widget.DragLabel):
+
+    def __init__(self, host, jid, name=None, click_listener=None, handle_menu=None):
         VerticalPanel.__init__(self, StyleName='contactBox', VerticalAlignment='middle')
         base_widget.DragLabel.__init__(self, jid, "CONTACT")
         self.host = host
@@ -104,11 +118,17 @@
         self.label = ContactLabel(jid, name)
         self.avatar = Image()
         self.updateAvatar(host.getAvatar(jid))
-        self.add(self.avatar)
+        extra = HorizontalPanel()
+        extra.add(self.avatar)
+        self.add(extra)
         self.add(self.label)
-        if handleClick:
+        if click_listener:
             ClickHandler.__init__(self)
             self.addClickListener(self)
+            self.click_listener = click_listener
+
+        if handle_menu:
+            extra.add(ContactMenuBar(host, {'jid': jid}))
 
     def setMessageWaiting(self, waiting):
         """Show a visual indicator if message are waiting
@@ -125,7 +145,7 @@
         self.avatar.setUrl(url)
 
     def onClick(self, sender):
-        self.host.getOrCreateLiberviaWidget(panels.ChatPanel, self.jid)
+        self.click_listener(self.jid)
 
 
 class GroupList(VerticalPanel):
@@ -157,18 +177,23 @@
     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, handleClick=False):
+    def __init__(self, host, handle_click=False, handle_menu=False):
         VerticalPanel.__init__(self)
         self.host = host
         self.contacts = []
-        self.handleClick = handleClick
+        self.click_listener = None
+        self.handle_menu = handle_menu
 
-    def add(self, jid, name=None, add_item_cb=None):
+        if handle_click:
+            def cb(contact_jid):
+                self.host.getOrCreateLiberviaWidget(panels.ChatPanel, contact_jid)
+            self.click_listener = cb
+
+    def add(self, jid, name=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
@@ -178,10 +203,8 @@
                 break
             index += 1
         self.contacts.insert(index, jid)
-        box = ContactBox(self.host, jid, name, handleClick=self.handleClick)
+        box = ContactBox(self.host, jid, name, self.click_listener, self.handle_menu)
         VerticalPanel.insert(self, box, index)
-        if add_item_cb:
-            add_item_cb(box)
 
     def remove(self, jid):
         box = self.getContactBox(jid)
@@ -221,34 +244,20 @@
     """The contact list that is displayed on the left side."""
 
     def __init__(self, host):
-        GenericContactList.__init__(self, host, handleClick=True)
-        self.menu_entries = {"blog": {"title": "Public blog..."}}
-        self.context_menu = base_panels.PopupMenuPanel(entries=self.menu_entries,
-                                           hide=self.contextMenuHide,
-                                           callback=self.contextMenuCallback,
-                                           vertical=False, style={"selected": "menu-selected"})
+        GenericContactList.__init__(self, host, handle_click=True, handle_menu=True)
 
     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 = panels.WebPanel(self.host, "/blog/%s" % node)
-            self.host.addTab("%s's blog" % node, web_panel)
-        else:
-            sender.onClick(sender)
-
     def add(self, jid_s, name=None):
         """Add a contact
 
         @param jid_s (str): JID as unicode
         @param name (str): nickname
         """
-        GenericContactList.add(self, jid_s, name, add_item_cb=lambda item: self.context_menu.registerRightClickSender(item))
+        GenericContactList.add(self, jid_s, name)
 
     def setState(self, jid, type_, state):
         """Change the appearance of the contact, according to the state