comparison 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
comparison
equal deleted inserted replaced
494:5d8632a7bfde 495:587fe75d1b16
32 from pyjamas import DOM 32 from pyjamas import DOM
33 from __pyjamas__ import doc 33 from __pyjamas__ import doc
34 34
35 from constants import Const as C 35 from constants import Const as C
36 from jid import JID 36 from jid import JID
37 import base_panels
38 import base_widget 37 import base_widget
39 import panels 38 import panels
40 import html_tools 39 import html_tools
41 40
42 41
93 @param waiting: True if message are waiting""" 92 @param waiting: True if message are waiting"""
94 self.waiting = waiting 93 self.waiting = waiting
95 self.refresh() 94 self.refresh()
96 95
97 96
98 class ContactBox(base_widget.DragLabel, VerticalPanel, ClickHandler): 97 class ContactMenuBar(base_widget.WidgetMenuBar):
99 def __init__(self, host, jid, name=None, handleClick=True): 98
99 ITEM_TPL = "<img src='media/icons/misc/%s.png' />"
100
101 def __init__(self, host, menu_data):
102 base_widget.WidgetMenuBar.__init__(self, host)
103 self.addCachedMenus(C.MENU_ROSTER_JID_CONTEXT, menu_data)
104 self.addCachedMenus(C.MENU_JID_CONTEXT, menu_data)
105
106 def onBrowserEvent(self, event):
107 base_widget.WidgetMenuBar.onBrowserEvent(self, event)
108 event.stopPropagation() # prevent opening the chat dialog
109
110
111 class ContactBox(VerticalPanel, ClickHandler, base_widget.DragLabel):
112
113 def __init__(self, host, jid, name=None, click_listener=None, handle_menu=None):
100 VerticalPanel.__init__(self, StyleName='contactBox', VerticalAlignment='middle') 114 VerticalPanel.__init__(self, StyleName='contactBox', VerticalAlignment='middle')
101 base_widget.DragLabel.__init__(self, jid, "CONTACT") 115 base_widget.DragLabel.__init__(self, jid, "CONTACT")
102 self.host = host 116 self.host = host
103 self.jid = jid 117 self.jid = jid
104 self.label = ContactLabel(jid, name) 118 self.label = ContactLabel(jid, name)
105 self.avatar = Image() 119 self.avatar = Image()
106 self.updateAvatar(host.getAvatar(jid)) 120 self.updateAvatar(host.getAvatar(jid))
107 self.add(self.avatar) 121 extra = HorizontalPanel()
122 extra.add(self.avatar)
123 self.add(extra)
108 self.add(self.label) 124 self.add(self.label)
109 if handleClick: 125 if click_listener:
110 ClickHandler.__init__(self) 126 ClickHandler.__init__(self)
111 self.addClickListener(self) 127 self.addClickListener(self)
128 self.click_listener = click_listener
129
130 if handle_menu:
131 extra.add(ContactMenuBar(host, {'jid': jid}))
112 132
113 def setMessageWaiting(self, waiting): 133 def setMessageWaiting(self, waiting):
114 """Show a visual indicator if message are waiting 134 """Show a visual indicator if message are waiting
115 135
116 @param waiting: True if message are waiting""" 136 @param waiting: True if message are waiting"""
123 """ 143 """
124 self.avatar.setVisible(url != C.DEFAULT_AVATAR) 144 self.avatar.setVisible(url != C.DEFAULT_AVATAR)
125 self.avatar.setUrl(url) 145 self.avatar.setUrl(url)
126 146
127 def onClick(self, sender): 147 def onClick(self, sender):
128 self.host.getOrCreateLiberviaWidget(panels.ChatPanel, self.jid) 148 self.click_listener(self.jid)
129 149
130 150
131 class GroupList(VerticalPanel): 151 class GroupList(VerticalPanel):
132 152
133 def __init__(self, parent): 153 def __init__(self, parent):
155 class GenericContactList(VerticalPanel): 175 class GenericContactList(VerticalPanel):
156 """Class that can be used to represent a contact list, but not necessarily 176 """Class that can be used to represent a contact list, but not necessarily
157 the one that is displayed on the left side. Special features like popup menu 177 the one that is displayed on the left side. Special features like popup menu
158 panel or changing the contact states must be done in a sub-class.""" 178 panel or changing the contact states must be done in a sub-class."""
159 179
160 def __init__(self, host, handleClick=False): 180 def __init__(self, host, handle_click=False, handle_menu=False):
161 VerticalPanel.__init__(self) 181 VerticalPanel.__init__(self)
162 self.host = host 182 self.host = host
163 self.contacts = [] 183 self.contacts = []
164 self.handleClick = handleClick 184 self.click_listener = None
165 185 self.handle_menu = handle_menu
166 def add(self, jid, name=None, add_item_cb=None): 186
187 if handle_click:
188 def cb(contact_jid):
189 self.host.getOrCreateLiberviaWidget(panels.ChatPanel, contact_jid)
190 self.click_listener = cb
191
192 def add(self, jid, name=None):
167 """Add a contact to the list. 193 """Add a contact to the list.
168 194
169 @param jid (str): JID of the contact 195 @param jid (str): JID of the contact
170 @param name (str): optional name of the contact 196 @param name (str): optional name of the contact
171 @param add_item_cb (method): to be called on the contact's widget after it's been added to the list
172 """ 197 """
173 if jid in self.contacts: 198 if jid in self.contacts:
174 return 199 return
175 index = 0 200 index = 0
176 for contact_ in self.contacts: 201 for contact_ in self.contacts:
177 if contact_ > jid: 202 if contact_ > jid:
178 break 203 break
179 index += 1 204 index += 1
180 self.contacts.insert(index, jid) 205 self.contacts.insert(index, jid)
181 box = ContactBox(self.host, jid, name, handleClick=self.handleClick) 206 box = ContactBox(self.host, jid, name, self.click_listener, self.handle_menu)
182 VerticalPanel.insert(self, box, index) 207 VerticalPanel.insert(self, box, index)
183 if add_item_cb:
184 add_item_cb(box)
185 208
186 def remove(self, jid): 209 def remove(self, jid):
187 box = self.getContactBox(jid) 210 box = self.getContactBox(jid)
188 if not box: 211 if not box:
189 return 212 return
219 242
220 class ContactList(GenericContactList): 243 class ContactList(GenericContactList):
221 """The contact list that is displayed on the left side.""" 244 """The contact list that is displayed on the left side."""
222 245
223 def __init__(self, host): 246 def __init__(self, host):
224 GenericContactList.__init__(self, host, handleClick=True) 247 GenericContactList.__init__(self, host, handle_click=True, handle_menu=True)
225 self.menu_entries = {"blog": {"title": "Public blog..."}}
226 self.context_menu = base_panels.PopupMenuPanel(entries=self.menu_entries,
227 hide=self.contextMenuHide,
228 callback=self.contextMenuCallback,
229 vertical=False, style={"selected": "menu-selected"})
230 248
231 def contextMenuHide(self, sender, key): 249 def contextMenuHide(self, sender, key):
232 """Return True if the item for that sender should be hidden.""" 250 """Return True if the item for that sender should be hidden."""
233 # TODO: enable the blogs of users that are on another server 251 # TODO: enable the blogs of users that are on another server
234 return JID(sender.jid).domain != self.host._defaultDomain 252 return JID(sender.jid).domain != self.host._defaultDomain
235 253
236 def contextMenuCallback(self, sender, key):
237 if key == "blog":
238 # TODO: use the bare when all blogs can be retrieved
239 node = JID(sender.jid).node
240 web_panel = panels.WebPanel(self.host, "/blog/%s" % node)
241 self.host.addTab("%s's blog" % node, web_panel)
242 else:
243 sender.onClick(sender)
244
245 def add(self, jid_s, name=None): 254 def add(self, jid_s, name=None):
246 """Add a contact 255 """Add a contact
247 256
248 @param jid_s (str): JID as unicode 257 @param jid_s (str): JID as unicode
249 @param name (str): nickname 258 @param name (str): nickname
250 """ 259 """
251 GenericContactList.add(self, jid_s, name, add_item_cb=lambda item: self.context_menu.registerRightClickSender(item)) 260 GenericContactList.add(self, jid_s, name)
252 261
253 def setState(self, jid, type_, state): 262 def setState(self, jid, type_, state):
254 """Change the appearance of the contact, according to the state 263 """Change the appearance of the contact, according to the state
255 @param jid: jid which need to change state 264 @param jid: jid which need to change state
256 @param type_: one of availability, messageWaiting 265 @param type_: one of availability, messageWaiting