quick_frontend, primitivus: display the number of alerts for each contact and not only a symbol (*)

This commit is contained in:
souliane 2015-11-24 13:31:28 +01:00
commit 54e9afe5a3
4 changed files with 53 additions and 26 deletions

View file

@ -98,4 +98,4 @@ class Const(constants.Const):
GROUP_DATA_FOLDED = 'folded' GROUP_DATA_FOLDED = 'folded'
# contacts and contact list # contacts and contact list
ALERT_HEADER='(*) ' ALERT_HEADER='(%i) '

View file

@ -136,7 +136,7 @@ class ContactList(PrimitivusWidget, QuickContactList):
@param selected: boolean returned by the widget, telling if it is selected @param selected: boolean returned by the widget, telling if it is selected
""" """
entity = contact_wid.data entity = contact_wid.data
self.removeAlert(entity, use_bare_jid) self.removeAlerts(entity, use_bare_jid)
self.host.modeHint(C.MODE_INSERTION) self.host.modeHint(C.MODE_INSERTION)
self._emit('click', entity) self._emit('click', entity)
@ -163,10 +163,8 @@ class ContactList(PrimitivusWidget, QuickContactList):
""" """
markup = [] markup = []
if use_bare_jid: if use_bare_jid:
alerts = {entity.bare for entity in self._alerts}
selected = {entity.bare for entity in self._selected} selected = {entity.bare for entity in self._selected}
else: else:
alerts = self._alerts
selected = self._selected selected = self._selected
if keys is None: if keys is None:
entity_txt = entity entity_txt = entity
@ -191,9 +189,10 @@ class ContactList(PrimitivusWidget, QuickContactList):
else: else:
entity_attr = 'default' 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' entity_attr = 'alert'
header = C.ALERT_HEADER header = C.ALERT_HEADER % alerts_count
else: else:
header = '' header = ''

View file

@ -296,7 +296,7 @@ class QuickApp(object):
@property @property
def alerts_count(self): def alerts_count(self):
"""Count the over whole alerts for all contact lists""" """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): def registerSignal(self, function_name, handler=None, iface="core", with_profile=True):
"""Register a handler for a signal """Register a handler for a signal
@ -510,13 +510,14 @@ class QuickApp(object):
chat_widget.newMessage(from_jid, target, msg, type_, extra, profile) chat_widget.newMessage(from_jid, target, msg, type_, extra, profile)
# ContactList alert # ContactList alert
visible = False if not from_me:
for widget in self.visible_widgets: visible = False
if isinstance(widget, quick_chat.QuickChat) and widget.manageMessage(from_jid, type_): for widget in self.visible_widgets:
visible = True if isinstance(widget, quick_chat.QuickChat) and widget.manageMessage(from_jid, type_):
break visible = True
if not visible: break
contact_list.setAlert(from_jid.bare if type_ == C.MESS_TYPE_GROUPCHAT else from_jid) 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): 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: if callback is None:

View file

@ -58,8 +58,8 @@ class QuickContactList(QuickWidget):
# contacts in roster (bare jids) # contacts in roster (bare jids)
self._roster = set() self._roster = set()
# entities with an alert (usually a waiting message), full jid # entities with alert(s) and their counts (usually a waiting message), dict{full jid: int)
self._alerts = set() self._alerts = dict()
# selected entities, full jid # selected entities, full jid
self._selected = set() self._selected = set()
@ -347,7 +347,7 @@ class QuickContactList(QuickWidget):
show = self.getCache(entity, C.PRESENCE_SHOW) show = self.getCache(entity, C.PRESENCE_SHOW)
if check_resource: if check_resource:
alerts = self._alerts alerts = self._alerts.keys()
selected = self._selected selected = self._selected
else: else:
alerts = {alert.bare for alert in self._alerts} alerts = {alert.bare for alert in self._alerts}
@ -396,12 +396,16 @@ class QuickContactList(QuickWidget):
self._groups[group]['jids'].remove(entity_bare) self._groups[group]['jids'].remove(entity_bare)
if not self._groups[group]['jids']: if not self._groups[group]['jids']:
self._groups.pop(group) 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() to_remove = set()
for set_entity in set_: for set_entity in iterable:
if set_entity.bare == entity.bare: if set_entity.bare == entity.bare:
to_remove.add(set_entity) 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() self.update()
def onPresenceUpdate(self, entity, show, priority, statuses, profile): def onPresenceUpdate(self, entity, show, priority, statuses, profile):
@ -461,16 +465,33 @@ class QuickContactList(QuickWidget):
self._selected.add(entity) self._selected.add(entity)
self.update() self.update()
def setAlert(self, entity): def getAlerts(self, entity, use_bare_jid=False):
"""Set an alert on the entity (usually for a waiting message) """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) @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.update()
self.host.updateAlertsCounter() 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). """Eventually remove an alert on the entity (usually for a waiting message).
@param entity(jid.JID): entity (resource is significant) @param entity(jid.JID): entity (resource is significant)
@ -481,9 +502,15 @@ class QuickContactList(QuickWidget):
for alert_entity in self._alerts: for alert_entity in self._alerts:
if alert_entity.bare == entity.bare: if alert_entity.bare == entity.bare:
to_remove.add(alert_entity) 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: else:
self._alerts.discard(entity) try:
del self._alerts[entity]
except KeyError:
return # nothing changed
self.update() self.update()
self.host.updateAlertsCounter() self.host.updateAlertsCounter()