comparison src/browser/sat_browser/contact.py @ 567:ee9c7bd266ad

browser_side: implements "Show offline contacts" and "Show empty groups" parameters
author souliane <souliane@mailoo.org>
date Mon, 06 Oct 2014 12:47:03 +0200
parents 3d8e8f693576
children f0c8a4d51890
comparison
equal deleted inserted replaced
566:8a607044ecfa 567:ee9c7bd266ad
176 176
177 def remove(self, group): 177 def remove(self, group):
178 for wid in self: 178 for wid in self:
179 if isinstance(wid, GroupLabel) and wid.group == group: 179 if isinstance(wid, GroupLabel) and wid.group == group:
180 VerticalPanel.remove(self, wid) 180 VerticalPanel.remove(self, wid)
181 return
182
183 def getGroupBox(self, group):
184 """get the widget of a group
185
186 @param group (str): the group
187 @return: GroupLabel instance if present, else None"""
188 for wid in self:
189 if isinstance(wid, GroupLabel) and wid.group == group:
190 return wid
191 return None
181 192
182 193
183 class GenericContactList(VerticalPanel): 194 class GenericContactList(VerticalPanel):
184 """Class that can be used to represent a contact list, but not necessarily 195 """Class that can be used to represent a contact list, but not necessarily
185 the one that is displayed on the left side. Special features like popup menu 196 the one that is displayed on the left side. Special features like popup menu
195 if handle_click: 206 if handle_click:
196 def cb(contact_jid): 207 def cb(contact_jid):
197 self.host.getOrCreateLiberviaWidget(panels.ChatPanel, contact_jid) 208 self.host.getOrCreateLiberviaWidget(panels.ChatPanel, contact_jid)
198 self.click_listener = cb 209 self.click_listener = cb
199 210
200 def add(self, jid, name=None): 211 def add(self, jid_s, name=None):
201 """Add a contact to the list. 212 """Add a contact to the list.
202 213
203 @param jid (str): JID of the contact 214 @param jid (str): JID of the contact
204 @param name (str): optional name of the contact 215 @param name (str): optional name of the contact
205 """ 216 """
206 assert(isinstance(jid, str)) 217 assert(isinstance(jid_s, str))
207 if jid in self.contacts: 218 if jid_s in self.contacts:
208 return 219 return
209 index = 0 220 index = 0
210 for contact_ in self.contacts: 221 for contact_ in self.contacts:
211 if contact_ > jid: 222 if contact_ > jid_s:
212 break 223 break
213 index += 1 224 index += 1
214 self.contacts.insert(index, jid) 225 self.contacts.insert(index, jid_s)
215 box = ContactBox(self.host, jid, name, self.click_listener, self.handle_menu) 226 box = ContactBox(self.host, jid_s, name, self.click_listener, self.handle_menu)
216 VerticalPanel.insert(self, box, index) 227 VerticalPanel.insert(self, box, index)
217 228
218 def remove(self, jid): 229 def remove(self, jid_s):
219 box = self.getContactBox(jid) 230 box = self.getContactBox(jid_s)
220 if not box: 231 if not box:
221 return 232 return
222 VerticalPanel.remove(self, box) 233 VerticalPanel.remove(self, box)
223 self.contacts.remove(jid) 234 self.contacts.remove(jid_s)
224 235
225 def isContactPresent(self, contact_jid): 236 def isContactPresent(self, contact_jid):
226 """Return True if a contact is present in the panel""" 237 """Return True if a contact is present in the panel"""
227 return contact_jid in self.contacts 238 return contact_jid in self.contacts
228 239
229 def getContacts(self): 240 def getContacts(self):
230 return self.contacts 241 return self.contacts
231 242
232 def getContactBox(self, contact_jid): 243 def getContactBox(self, contact_jid_s):
233 """get contactList widget of a contact 244 """get the widget of a contact
245
246 @param contact_jid_s (str): the contact
234 @return: ContactBox instance if present, else None""" 247 @return: ContactBox instance if present, else None"""
235 for wid in self: 248 for wid in self:
236 if isinstance(wid, ContactBox) and wid.jid == contact_jid: 249 if isinstance(wid, ContactBox) and wid.jid == contact_jid_s:
237 return wid 250 return wid
238 return None 251 return None
239 252
240 def updateAvatar(self, jid_s, url): 253 def updateAvatar(self, jid_s, url):
241 """Update the avatar of the given contact 254 """Update the avatar of the given contact
252 class ContactList(GenericContactList): 265 class ContactList(GenericContactList):
253 """The contact list that is displayed on the left side.""" 266 """The contact list that is displayed on the left side."""
254 267
255 def __init__(self, host): 268 def __init__(self, host):
256 GenericContactList.__init__(self, host, handle_click=True, handle_menu=True) 269 GenericContactList.__init__(self, host, handle_click=True, handle_menu=True)
257
258 def contextMenuHide(self, sender, key):
259 """Return True if the item for that sender should be hidden."""
260 # TODO: enable the blogs of users that are on another server
261 return JID(sender.jid).domain != self.host._defaultDomain
262
263 def add(self, jid_s, name=None):
264 """Add a contact
265
266 @param jid_s (str): JID as unicode
267 @param name (str): nickname
268 """
269 GenericContactList.add(self, jid_s, name)
270 270
271 def setState(self, jid, type_, state): 271 def setState(self, jid, type_, state):
272 """Change the appearance of the contact, according to the state 272 """Change the appearance of the contact, according to the state
273 @param jid: jid which need to change state 273 @param jid: jid which need to change state
274 @param type_: one of availability, messageWaiting 274 @param type_: one of availability, messageWaiting
359 if self.host.uni_box: 359 if self.host.uni_box:
360 self.host.uni_box.removeKey(_key % group) 360 self.host.uni_box.removeKey(_key % group)
361 361
362 for group in _new_groups.difference(_current_groups): 362 for group in _new_groups.difference(_current_groups):
363 # We add the contact to the groups he joined 363 # We add the contact to the groups he joined
364 if not group in self.groups.keys(): 364 if group not in self.groups.keys():
365 self.groups[group] = set() 365 self.groups[group] = set()
366 self._groupList.add(group) 366 self._groupList.add(group)
367 if self.host.uni_box: 367 if self.host.uni_box:
368 self.host.uni_box.addKey(_key % group) 368 self.host.uni_box.addKey(_key % group)
369 self.groups[group].add(jid_s) 369 self.groups[group].add(jid_s)
370 370
371 # We add the contact to contact list, it will check if contact already exists 371 # We add the contact to contact list, it will check if contact already exists
372 self._contact_list.add(jid_s) 372 self._contact_list.add(jid_s)
373 self.updateVisibility([jid_s], self.getContactGroups(jid_s))
373 374
374 def removeContact(self, jid): 375 def removeContact(self, jid):
375 """Remove contacts from groups where he is and contact list""" 376 """Remove contacts from groups where he is and contact list"""
376 self.updateContact(jid, {}, []) # we remove contact from every group 377 self.updateContact(jid, {}, []) # we remove contact from every group
377 self._contact_list.remove(jid) 378 self._contact_list.remove(jid)
378 379
379 def setConnected(self, jid, resource, availability, priority, statuses): 380 def setConnected(self, jid_s, resource, availability, priority, statuses):
380 """Set connection status 381 """Set connection status
381 @param jid: JID userhost as unicode 382 @param jid_s (str): JID userhost as unicode
382 """ 383 """
383 if availability == 'unavailable': 384 if availability == 'unavailable':
384 if jid in self.connected: 385 if jid_s in self.connected:
385 if resource in self.connected[jid]: 386 if resource in self.connected[jid_s]:
386 del self.connected[jid][resource] 387 del self.connected[jid_s][resource]
387 if not self.connected[jid]: 388 if not self.connected[jid_s]:
388 del self.connected[jid] 389 del self.connected[jid_s]
389 else: 390 else:
390 if not jid in self.connected: 391 if jid_s not in self.connected:
391 self.connected[jid] = {} 392 self.connected[jid_s] = {}
392 self.connected[jid][resource] = (availability, priority, statuses) 393 self.connected[jid_s][resource] = (availability, priority, statuses)
393 394
394 # check if the contact is connected with another resource, use the one with highest priority 395 # check if the contact is connected with another resource, use the one with highest priority
395 if jid in self.connected: 396 if jid_s in self.connected:
396 max_resource = max_priority = None 397 max_resource = max_priority = None
397 for tmp_resource in self.connected[jid]: 398 for tmp_resource in self.connected[jid_s]:
398 if max_priority is None or self.connected[jid][tmp_resource][1] >= max_priority: 399 if max_priority is None or self.connected[jid_s][tmp_resource][1] >= max_priority:
399 max_resource = tmp_resource 400 max_resource = tmp_resource
400 max_priority = self.connected[jid][tmp_resource][1] 401 max_priority = self.connected[jid_s][tmp_resource][1]
401 if availability == "unavailable": # do not check the priority here, because 'unavailable' has a dummy one 402 if availability == "unavailable": # do not check the priority here, because 'unavailable' has a dummy one
402 priority = max_priority 403 priority = max_priority
403 availability = self.connected[jid][max_resource][0] 404 availability = self.connected[jid_s][max_resource][0]
404 if jid not in self.connected or priority >= max_priority: 405 if jid_s not in self.connected or priority >= max_priority:
405 # case 1: jid not in self.connected means all resources are disconnected, update with 'unavailable' 406 # case 1: jid not in self.connected means all resources are disconnected, update with 'unavailable'
406 # case 2: update (or confirm) with the values of the resource which takes precedence 407 # case 2: update (or confirm) with the values of the resource which takes precedence
407 self._contact_list.setState(jid, "availability", availability) 408 self._contact_list.setState(jid_s, "availability", availability)
408 409
409 # update the connected contacts chooser live 410 # update the connected contacts chooser live
410 if hasattr(self.host, "room_contacts_chooser") and self.host.room_contacts_chooser is not None: 411 if hasattr(self.host, "room_contacts_chooser") and self.host.room_contacts_chooser is not None:
411 self.host.room_contacts_chooser.resetContacts() 412 self.host.room_contacts_chooser.resetContacts()
413
414 self.updateVisibility([jid_s], self.getContactGroups(jid_s))
412 415
413 def setContactMessageWaiting(self, jid, waiting): 416 def setContactMessageWaiting(self, jid, waiting):
414 """Show an visual indicator that contact has send a message 417 """Show an visual indicator that contact has send a message
415 @param jid: jid of the contact 418 @param jid: jid of the contact
416 @param waiting: True if message are waiting""" 419 @param waiting: True if message are waiting"""
483 486
484 @param jid_s (str): contact jid 487 @param jid_s (str): contact jid
485 @param url (str): image url 488 @param url (str): image url
486 """ 489 """
487 self._contact_list.updateAvatar(jid_s, url) 490 self._contact_list.updateAvatar(jid_s, url)
491
492 def hasVisibleMembers(self, group):
493 """Tell if the given group actually has visible members
494
495 @param group (str): the group to check
496 @return: boolean
497 """
498 for jid in self.groups[group]:
499 if self._contact_list.getContactBox(jid).isVisible():
500 return True
501 return False
502
503 def offlineContactsToShow(self):
504 """Tell if offline contacts should be visible according to the user settings
505
506 @return: boolean
507 """
508 return self.host.getCachedParam('General', C.SHOW_OFFLINE_CONTACTS) == 'true'
509
510 def emtyGroupsToShow(self):
511 """Tell if empty groups should be visible according to the user settings
512
513 @return: boolean
514 """
515 return self.host.getCachedParam('General', C.SHOW_EMPTY_GROUPS) == 'true'
516
517 def updateVisibility(self, jids, groups):
518 """Set the widgets visibility for the given contacts and groups
519
520 @param jids (list[str]): list of JID
521 @param groups (list[str]): list of groups
522 """
523 for jid_s in jids:
524 self._contact_list.getContactBox(jid_s).setVisible(jid_s in self.connected or self.offlineContactsToShow())
525 for group in groups:
526 self._groupList.getGroupBox(group).setVisible(self.hasVisibleMembers(group) or self.emtyGroupsToShow())
527
528 def refresh(self):
529 """Show or hide disconnected contacts and empty groups"""
530 self.updateVisibility(self._contact_list.contacts, self.groups.keys())