comparison src/browser/sat_browser/contact.py @ 480:50b286866739

browser side: display avatars in the contact panel
author souliane <souliane@mailoo.org>
date Sat, 14 Jun 2014 19:20:27 +0200
parents c21ea1fe3593
children 5911f71acc80
comparison
equal deleted inserted replaced
479:c21ea1fe3593 480:50b286866739
21 from sat.core.log import getLogger 21 from sat.core.log import getLogger
22 log = getLogger(__name__) 22 log = getLogger(__name__)
23 from pyjamas.ui.SimplePanel import SimplePanel 23 from pyjamas.ui.SimplePanel import SimplePanel
24 from pyjamas.ui.ScrollPanel import ScrollPanel 24 from pyjamas.ui.ScrollPanel import ScrollPanel
25 from pyjamas.ui.VerticalPanel import VerticalPanel 25 from pyjamas.ui.VerticalPanel import VerticalPanel
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel
26 from pyjamas.ui.ClickListener import ClickHandler 27 from pyjamas.ui.ClickListener import ClickHandler
27 from pyjamas.ui.Label import Label 28 from pyjamas.ui.Label import Label
28 from pyjamas.ui.HTML import HTML 29 from pyjamas.ui.HTML import HTML
30 from pyjamas.ui.Image import Image
29 from pyjamas import Window 31 from pyjamas import Window
30 from pyjamas import DOM 32 from pyjamas import DOM
31 from __pyjamas__ import doc 33 from __pyjamas__ import doc
32 34
35 from constants import Const as C
33 from jid import JID 36 from jid import JID
34
35 import base_panels 37 import base_panels
36 import base_widget 38 import base_widget
37 import panels 39 import panels
38 import html_tools 40 import html_tools
39 41
100 102
101 def onClick(self, sender): 103 def onClick(self, sender):
102 self.host.getOrCreateLiberviaWidget(panels.ChatPanel, self.jid) 104 self.host.getOrCreateLiberviaWidget(panels.ChatPanel, self.jid)
103 105
104 106
107 class ContactBox(HorizontalPanel):
108 def __init__(self, host, jid, name=None, handleClick=True):
109 HorizontalPanel.__init__(self, StyleName='contactBox', VerticalAlignment='middle')
110 self.jid = jid
111 self.label = ContactLabel(host, jid, name, handleClick)
112 self.avatar = Image()
113 self.updateAvatar(host.getAvatar(jid))
114 self.add(self.label)
115 spacer = Label(' ')
116 self.add(spacer)
117 self.setCellWidth(spacer, '100%')
118 self.add(self.avatar)
119 self.setCellHorizontalAlignment(self.avatar, 'right')
120
121 def setMessageWaiting(self, waiting):
122 """Show a visual indicator if message are waiting
123
124 @param waiting: True if message are waiting"""
125 self.label.setMessageWaiting(waiting)
126
127 def updateAvatar(self, url):
128 """Update the avatar
129
130 @param url (str): image url
131 """
132 self.avatar.setVisible(url != C.DEFAULT_AVATAR)
133 self.avatar.setUrl(url)
134
135
105 class GroupList(VerticalPanel): 136 class GroupList(VerticalPanel):
106 137
107 def __init__(self, parent): 138 def __init__(self, parent):
108 VerticalPanel.__init__(self) 139 VerticalPanel.__init__(self)
109 self.setStyleName('groupList') 140 self.setStyleName('groupList')
150 for contact_ in self.contacts: 181 for contact_ in self.contacts:
151 if contact_ > jid: 182 if contact_ > jid:
152 break 183 break
153 index += 1 184 index += 1
154 self.contacts.insert(index, jid) 185 self.contacts.insert(index, jid)
155 _item = ContactLabel(self.host, jid, name, handleClick=self.handleClick) 186 box = ContactBox(self.host, jid, name, handleClick=self.handleClick)
156 DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer") 187 VerticalPanel.insert(self, box, index)
157 VerticalPanel.insert(self, _item, index)
158 if add_item_cb: 188 if add_item_cb:
159 add_item_cb(box) 189 add_item_cb(box)
160 190
161 def remove(self, jid): 191 def remove(self, jid):
162 wid = self.getContactLabel(jid) 192 box = self.getContactBox(jid)
163 if not wid: 193 if not box:
164 return 194 return
165 VerticalPanel.remove(self, wid) 195 VerticalPanel.remove(self, box)
166 self.contacts.remove(jid) 196 self.contacts.remove(jid)
167 197
168 def isContactPresent(self, contact_jid): 198 def isContactPresent(self, contact_jid):
169 """Return True if a contact is present in the panel""" 199 """Return True if a contact is present in the panel"""
170 return contact_jid in self.contacts 200 return contact_jid in self.contacts
171 201
172 def getContacts(self): 202 def getContacts(self):
173 return self.contacts 203 return self.contacts
174 204
175 def getContactLabel(self, contact_jid): 205 def getContactBox(self, contact_jid):
176 """get contactList widget of a contact 206 """get contactList widget of a contact
177 @return: ContactLabel item if present, else None""" 207 @return: ContactBox instance if present, else None"""
178 for wid in self: 208 for wid in self:
179 if isinstance(wid, ContactLabel) and wid.jid == contact_jid: 209 if isinstance(wid, ContactBox) and wid.jid == contact_jid:
180 return wid 210 return wid
181 return None 211 return None
212
213 def updateAvatar(self, jid_s, url):
214 """Update the avatar of the given contact
215
216 @param jid_s (str): contact jid
217 @param url (str): image url
218 """
219 try:
220 self.getContactBox(jid_s).updateAvatar(url)
221 except TypeError:
222 pass
182 223
183 224
184 class ContactList(GenericContactList): 225 class ContactList(GenericContactList):
185 """The contact list that is displayed on the left side.""" 226 """The contact list that is displayed on the left side."""
186 227
221 @param state: 262 @param state:
222 - for messageWaiting type: 263 - for messageWaiting type:
223 True if message are waiting 264 True if message are waiting
224 - for availability type: 265 - for availability type:
225 'unavailable' if not connected, else presence like RFC6121 #4.7.2.1""" 266 'unavailable' if not connected, else presence like RFC6121 #4.7.2.1"""
226 _item = self.getContactLabel(jid) 267 contact_box = self.getContactBox(jid)
227 if _item: 268 if contact_box:
228 if type_ == 'availability': 269 if type_ == 'availability':
229 setPresenceStyle(_item, state) 270 setPresenceStyle(contact_box.label, state)
230 elif type_ == 'messageWaiting': 271 elif type_ == 'messageWaiting':
231 _item.setMessageWaiting(state) 272 contact_box.setMessageWaiting(state)
232 273
233 274
234 class ContactTitleLabel(base_widget.DragLabel, Label, ClickHandler): 275 class ContactTitleLabel(base_widget.DragLabel, Label, ClickHandler):
235 def __init__(self, host, text): 276 def __init__(self, host, text):
236 Label.__init__(self, text) # , Element=DOM.createElement('div') 277 Label.__init__(self, text) # , Element=DOM.createElement('div')
390 return True 431 return True
391 return False 432 return False
392 433
393 def isContactInRoster(self, contact_jid): 434 def isContactInRoster(self, contact_jid):
394 """Test if the contact is in our roster list""" 435 """Test if the contact is in our roster list"""
395 for _contact_label in self._contact_list: 436 for contact_box in self._contact_list:
396 if contact_jid == _contact_label.jid: 437 if contact_jid == contact_box.jid:
397 return True 438 return True
398 return False 439 return False
399 440
400 def getContacts(self): 441 def getContacts(self):
401 return self._contact_list.getContacts() 442 return self._contact_list.getContacts()
414 455
415 def onMouseEnter(self, sender): 456 def onMouseEnter(self, sender):
416 if isinstance(sender, GroupLabel): 457 if isinstance(sender, GroupLabel):
417 for contact in self._contact_list: 458 for contact in self._contact_list:
418 if contact.jid in self.groups[sender.group]: 459 if contact.jid in self.groups[sender.group]:
419 contact.addStyleName("selected") 460 contact.label.addStyleName("selected")
420 461
421 def onMouseLeave(self, sender): 462 def onMouseLeave(self, sender):
422 if isinstance(sender, GroupLabel): 463 if isinstance(sender, GroupLabel):
423 for contact in self._contact_list: 464 for contact in self._contact_list:
424 if contact.jid in self.groups[sender.group]: 465 if contact.jid in self.groups[sender.group]:
425 contact.removeStyleName("selected") 466 contact.label.removeStyleName("selected")
467
468 def updateAvatar(self, jid_s, url):
469 """Update the avatar of the given contact
470
471 @param jid_s (str): contact jid
472 @param url (str): image url
473 """
474 self._contact_list.updateAvatar(jid_s, url)