diff 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
line wrap: on
line diff
--- 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()