# HG changeset patch # User souliane # Date 1448368288 -3600 # Node ID 683b76c1145db12f6a337a69ec35f6f8e62fd6f2 # Parent 62cd8fc1aef769a43fa360b0a738c4a6b799cfe9 quick_frontend, primitivus: display the number of alerts for each contact and not only a symbol (*) diff -r 62cd8fc1aef7 -r 683b76c1145d frontends/src/primitivus/constants.py --- a/frontends/src/primitivus/constants.py Tue Nov 24 10:39:22 2015 +0100 +++ b/frontends/src/primitivus/constants.py Tue Nov 24 13:31:28 2015 +0100 @@ -98,4 +98,4 @@ GROUP_DATA_FOLDED = 'folded' # contacts and contact list - ALERT_HEADER='(*) ' + ALERT_HEADER='(%i) ' diff -r 62cd8fc1aef7 -r 683b76c1145d frontends/src/primitivus/contact_list.py --- a/frontends/src/primitivus/contact_list.py Tue Nov 24 10:39:22 2015 +0100 +++ b/frontends/src/primitivus/contact_list.py Tue Nov 24 13:31:28 2015 +0100 @@ -136,7 +136,7 @@ @param selected: boolean returned by the widget, telling if it is selected """ entity = contact_wid.data - self.removeAlert(entity, use_bare_jid) + self.removeAlerts(entity, use_bare_jid) self.host.modeHint(C.MODE_INSERTION) self._emit('click', entity) @@ -163,10 +163,8 @@ """ markup = [] if use_bare_jid: - alerts = {entity.bare for entity in self._alerts} selected = {entity.bare for entity in self._selected} else: - alerts = self._alerts selected = self._selected if keys is None: entity_txt = entity @@ -191,9 +189,10 @@ else: entity_attr = 'default' - if with_alert and entity in alerts: + alerts_count = self.getAlerts(entity, use_bare_jid=use_bare_jid) + if with_alert and alerts_count: entity_attr = 'alert' - header = C.ALERT_HEADER + header = C.ALERT_HEADER % alerts_count else: header = '' diff -r 62cd8fc1aef7 -r 683b76c1145d frontends/src/quick_frontend/quick_app.py --- a/frontends/src/quick_frontend/quick_app.py Tue Nov 24 10:39:22 2015 +0100 +++ b/frontends/src/quick_frontend/quick_app.py Tue Nov 24 13:31:28 2015 +0100 @@ -296,7 +296,7 @@ @property def alerts_count(self): """Count the over whole alerts for all contact lists""" - return sum([len(clist._alerts) for clist in self.contact_lists.values()]) + return sum([sum(clist._alerts.values()) for clist in self.contact_lists.values()]) def registerSignal(self, function_name, handler=None, iface="core", with_profile=True): """Register a handler for a signal @@ -510,13 +510,14 @@ chat_widget.newMessage(from_jid, target, msg, type_, extra, profile) # ContactList alert - visible = False - for widget in self.visible_widgets: - if isinstance(widget, quick_chat.QuickChat) and widget.manageMessage(from_jid, type_): - visible = True - break - if not visible: - contact_list.setAlert(from_jid.bare if type_ == C.MESS_TYPE_GROUPCHAT else from_jid) + if not from_me: + visible = False + for widget in self.visible_widgets: + if isinstance(widget, quick_chat.QuickChat) and widget.manageMessage(from_jid, type_): + visible = True + break + if not visible: + contact_list.addAlert(from_jid.bare if type_ == C.MESS_TYPE_GROUPCHAT else from_jid) def sendMessage(self, to_jid, message, subject='', mess_type="auto", extra={}, callback=None, errback=None, profile_key=C.PROF_KEY_NONE): if callback is None: diff -r 62cd8fc1aef7 -r 683b76c1145d frontends/src/quick_frontend/quick_contact_list.py --- a/frontends/src/quick_frontend/quick_contact_list.py Tue Nov 24 10:39:22 2015 +0100 +++ b/frontends/src/quick_frontend/quick_contact_list.py Tue Nov 24 13:31:28 2015 +0100 @@ -58,8 +58,8 @@ # contacts in roster (bare jids) self._roster = set() - # entities with an alert (usually a waiting message), full jid - self._alerts = set() + # entities with alert(s) and their counts (usually a waiting message), dict{full jid: int) + self._alerts = dict() # selected entities, full jid self._selected = set() @@ -347,7 +347,7 @@ show = self.getCache(entity, C.PRESENCE_SHOW) if check_resource: - alerts = self._alerts + alerts = self._alerts.keys() selected = self._selected else: alerts = {alert.bare for alert in self._alerts} @@ -396,12 +396,16 @@ self._groups[group]['jids'].remove(entity_bare) if not self._groups[group]['jids']: self._groups.pop(group) - for set_ in (self._selected, self._alerts, self._specials, self._special_extras): + for iterable in (self._selected, self._alerts, self._specials, self._special_extras): to_remove = set() - for set_entity in set_: + for set_entity in iterable: if set_entity.bare == entity.bare: to_remove.add(set_entity) - set_.difference_update(to_remove) + if isinstance(iterable, set): + iterable.difference_update(to_remove) + else: # XXX: self._alerts is a dict + for item in to_remove: + del iterable[item] self.update() def onPresenceUpdate(self, entity, show, priority, statuses, profile): @@ -461,16 +465,33 @@ self._selected.add(entity) self.update() - def setAlert(self, entity): - """Set an alert on the entity (usually for a waiting message) + def getAlerts(self, entity, use_bare_jid=False): + """Return the number of alerts set to this entity. + + @param entity (jid.JID): entity + @param use_bare_jid (bool): if True, cumulate the alerts of all the resources sharing the same bare JID + @return int + """ + if not use_bare_jid: + return self._alerts.get(entity, 0) + + alerts = {} + for contact in self._alerts: + alerts.setdefault(contact.bare, 0) + alerts[contact.bare] += self._alerts[contact] + return alerts.get(entity.bare, 0) + + def addAlert(self, entity): + """Increase the alerts counter for this entity (usually for a waiting message) @param entity(jid.JID): entity which must displayed in alert mode (resource is significant) """ - self._alerts.add(entity) + self._alerts.setdefault(entity, 0) + self._alerts[entity] += 1 self.update() self.host.updateAlertsCounter() - def removeAlert(self, entity, use_bare_jid=True): + def removeAlerts(self, entity, use_bare_jid=True): """Eventually remove an alert on the entity (usually for a waiting message). @param entity(jid.JID): entity (resource is significant) @@ -481,9 +502,15 @@ for alert_entity in self._alerts: if alert_entity.bare == entity.bare: to_remove.add(alert_entity) - self._alerts.difference_update(to_remove) + if not to_remove: + return # nothing changed + for entity in to_remove: + del self._alerts[entity] else: - self._alerts.discard(entity) + try: + del self._alerts[entity] + except KeyError: + return # nothing changed self.update() self.host.updateAlertsCounter()