comparison frontends/src/quick_frontend/quick_contact_list.py @ 1658:683b76c1145d

quick_frontend, primitivus: display the number of alerts for each contact and not only a symbol (*)
author souliane <souliane@mailoo.org>
date Tue, 24 Nov 2015 13:31:28 +0100
parents 6a6fe840c3a6
children 58ecc0e2e6fc
comparison
equal deleted inserted replaced
1657:62cd8fc1aef7 1658:683b76c1145d
56 self._groups = {} # groups to group data map 56 self._groups = {} # groups to group data map
57 57
58 # contacts in roster (bare jids) 58 # contacts in roster (bare jids)
59 self._roster = set() 59 self._roster = set()
60 60
61 # entities with an alert (usually a waiting message), full jid 61 # entities with alert(s) and their counts (usually a waiting message), dict{full jid: int)
62 self._alerts = set() 62 self._alerts = dict()
63 63
64 # selected entities, full jid 64 # selected entities, full jid
65 self._selected = set() 65 self._selected = set()
66 66
67 # we keep our own jid 67 # we keep our own jid
345 @return (bool): True if that contact should be showed in the list 345 @return (bool): True if that contact should be showed in the list
346 """ 346 """
347 show = self.getCache(entity, C.PRESENCE_SHOW) 347 show = self.getCache(entity, C.PRESENCE_SHOW)
348 348
349 if check_resource: 349 if check_resource:
350 alerts = self._alerts 350 alerts = self._alerts.keys()
351 selected = self._selected 351 selected = self._selected
352 else: 352 else:
353 alerts = {alert.bare for alert in self._alerts} 353 alerts = {alert.bare for alert in self._alerts}
354 selected = {selected.bare for selected in self._selected} 354 selected = {selected.bare for selected in self._selected}
355 return ((show is not None and show != C.PRESENCE_UNAVAILABLE) 355 return ((show is not None and show != C.PRESENCE_UNAVAILABLE)
394 del self._cache[entity_bare] 394 del self._cache[entity_bare]
395 for group in groups: 395 for group in groups:
396 self._groups[group]['jids'].remove(entity_bare) 396 self._groups[group]['jids'].remove(entity_bare)
397 if not self._groups[group]['jids']: 397 if not self._groups[group]['jids']:
398 self._groups.pop(group) 398 self._groups.pop(group)
399 for set_ in (self._selected, self._alerts, self._specials, self._special_extras): 399 for iterable in (self._selected, self._alerts, self._specials, self._special_extras):
400 to_remove = set() 400 to_remove = set()
401 for set_entity in set_: 401 for set_entity in iterable:
402 if set_entity.bare == entity.bare: 402 if set_entity.bare == entity.bare:
403 to_remove.add(set_entity) 403 to_remove.add(set_entity)
404 set_.difference_update(to_remove) 404 if isinstance(iterable, set):
405 iterable.difference_update(to_remove)
406 else: # XXX: self._alerts is a dict
407 for item in to_remove:
408 del iterable[item]
405 self.update() 409 self.update()
406 410
407 def onPresenceUpdate(self, entity, show, priority, statuses, profile): 411 def onPresenceUpdate(self, entity, show, priority, statuses, profile):
408 """Update entity's presence status 412 """Update entity's presence status
409 413
459 """ 463 """
460 log.debug(u"select %s" % entity) 464 log.debug(u"select %s" % entity)
461 self._selected.add(entity) 465 self._selected.add(entity)
462 self.update() 466 self.update()
463 467
464 def setAlert(self, entity): 468 def getAlerts(self, entity, use_bare_jid=False):
465 """Set an alert on the entity (usually for a waiting message) 469 """Return the number of alerts set to this entity.
470
471 @param entity (jid.JID): entity
472 @param use_bare_jid (bool): if True, cumulate the alerts of all the resources sharing the same bare JID
473 @return int
474 """
475 if not use_bare_jid:
476 return self._alerts.get(entity, 0)
477
478 alerts = {}
479 for contact in self._alerts:
480 alerts.setdefault(contact.bare, 0)
481 alerts[contact.bare] += self._alerts[contact]
482 return alerts.get(entity.bare, 0)
483
484 def addAlert(self, entity):
485 """Increase the alerts counter for this entity (usually for a waiting message)
466 486
467 @param entity(jid.JID): entity which must displayed in alert mode (resource is significant) 487 @param entity(jid.JID): entity which must displayed in alert mode (resource is significant)
468 """ 488 """
469 self._alerts.add(entity) 489 self._alerts.setdefault(entity, 0)
490 self._alerts[entity] += 1
470 self.update() 491 self.update()
471 self.host.updateAlertsCounter() 492 self.host.updateAlertsCounter()
472 493
473 def removeAlert(self, entity, use_bare_jid=True): 494 def removeAlerts(self, entity, use_bare_jid=True):
474 """Eventually remove an alert on the entity (usually for a waiting message). 495 """Eventually remove an alert on the entity (usually for a waiting message).
475 496
476 @param entity(jid.JID): entity (resource is significant) 497 @param entity(jid.JID): entity (resource is significant)
477 @param use_bare_jid (bool): if True, ignore the resource 498 @param use_bare_jid (bool): if True, ignore the resource
478 """ 499 """
479 if use_bare_jid: 500 if use_bare_jid:
480 to_remove = set() 501 to_remove = set()
481 for alert_entity in self._alerts: 502 for alert_entity in self._alerts:
482 if alert_entity.bare == entity.bare: 503 if alert_entity.bare == entity.bare:
483 to_remove.add(alert_entity) 504 to_remove.add(alert_entity)
484 self._alerts.difference_update(to_remove) 505 if not to_remove:
506 return # nothing changed
507 for entity in to_remove:
508 del self._alerts[entity]
485 else: 509 else:
486 self._alerts.discard(entity) 510 try:
511 del self._alerts[entity]
512 except KeyError:
513 return # nothing changed
487 self.update() 514 self.update()
488 self.host.updateAlertsCounter() 515 self.host.updateAlertsCounter()
489 516
490 def _showOfflineContacts(self, show_str): 517 def _showOfflineContacts(self, show_str):
491 self.showOfflineContacts(C.bool(show_str)) 518 self.showOfflineContacts(C.bool(show_str))