quick_frontend, primitivus: notifications refactoring

replaced old "alerts" system by a more generic one which use listeners and can activate callbacks on notification click.
This commit is contained in:
Goffi 2016-06-27 22:36:22 +02:00
commit b388d2b172
4 changed files with 131 additions and 139 deletions

View file

@ -31,8 +31,8 @@ class Const(constants.Const):
('selected_focus', 'default,bold', 'dark red'),
('default', 'default', 'default'),
('default_focus', 'default,bold', 'default'),
('alert', 'default,underline', 'default'),
('alert_focus', 'default,bold,underline', 'default'),
('cl_notifs', 'default,underline', 'yellow'),
('cl_notifs_focus', 'default,bold,underline', 'yellow'),
# Messages
('date', 'light gray', 'default'),
('my_nick', 'dark red,bold', 'default'),
@ -100,6 +100,3 @@ class Const(constants.Const):
MODE_COMMAND = 'COMMAND'
GROUP_DATA_FOLDED = 'folded'
# contacts and contact list
ALERT_HEADER='(%i) '

View file

@ -144,18 +144,16 @@ class ContactList(PrimitivusWidget, QuickContactList):
"""Method called when a contact is clicked
@param use_bare_jid: True if use_bare_jid is set in self._buildEntityWidget.
If True, all jids in self._alerts with the same bare jid has contact_wid.data will be removed
@param contact_wid: widget of the contact, must have the entity set in data attribute
@param selected: boolean returned by the widget, telling if it is selected
"""
entity = contact_wid.data
self.contact_list.removeAlerts(entity, use_bare_jid)
self.host.modeHint(C.MODE_INSERTION)
self._emit('click', entity)
# Methods to build the widget
def _buildEntityWidget(self, entity, keys=None, use_bare_jid=False, with_alert=True, with_show_attr=True, markup_prepend=None, markup_append = None):
def _buildEntityWidget(self, entity, keys=None, use_bare_jid=False, with_notifs=True, with_show_attr=True, markup_prepend=None, markup_append = None):
"""Build one contact markup data
@param entity (jid.JID): entity to build
@ -164,8 +162,8 @@ class ContactList(PrimitivusWidget, QuickContactList):
If key starts with "cache_", it will be checked in cache,
else, getattr will be done on entity with the key (e.g. getattr(entity, 'node')).
If nothing full or keys is None, full entity is used.
@param use_bare_jid (bool): if True, use bare jid for alerts and selected comparisons
@param with_alert (bool): if True, show alert if entity is in self._alerts
@param use_bare_jid (bool): if True, use bare jid for selected comparisons
@param with_notifs (bool): if True, show notification count
@param with_show_attr (bool): if True, show color corresponding to presence status
@param markup_prepend (list): markup to prepend to the generated one before building the widget
@param markup_append (list): markup to append to the generated one before building the widget
@ -199,12 +197,12 @@ class ContactList(PrimitivusWidget, QuickContactList):
else:
entity_attr = 'default'
alerts_count = len(self.contact_list.getAlerts(entity, use_bare_jid=use_bare_jid))
if with_alert and alerts_count:
entity_attr = 'alert'
header = C.ALERT_HEADER % alerts_count
notifs = self.host.getNotifs(entity.bare, profile=self.profile)
if notifs:
entity_attr = 'cl_notifs'
header = u'({}) '.format(len(notifs))
else:
header = ''
header = u''
markup.append((entity_attr, entity_txt))
if markup_prepend:

View file

@ -294,7 +294,7 @@ class TarotGame(QuickTarotGame, urwid.WidgetWrap):
QuickTarotGame.tarotGameInvalidCardsHandler(self, phase, played_cards, invalid_cards)
self.hand_wid.update(self.hand)
if self._autoplay == None: # No dialog if there is autoplay
self.parent.host.notify(_('Cards played are invalid !'))
self.parent.host.barNotify(_('Cards played are invalid !'))
self.parent.host.redraw()
def tarotGameCardsPlayedHandler(self, player, cards):

View file

@ -586,11 +586,24 @@ class PrimitivusApp(QuickApp, InputHistory):
else:
self.notif_bar.addPopUp(pop_up_widget)
def notify(self, message):
def barNotify(self, message):
""""Notify message to user via notification bar"""
self.notif_bar.addMessage(message)
self.redraw()
def notify(self, type_, entity=None, message=None, subject=None, callback=None, cb_args=None, widget=None, profile=C.PROF_KEY_NONE):
if widget is None or widget is not None and widget != self.selected_widget:
# we ignore notification if the widget is selected but we can
# still do a desktop notification is the X window has not the focus
super(PrimitivusApp, self).notify(type_, entity, message, subject, callback, cb_args, widget, profile)
if not self.x_notify.hasFocus():
if message is None:
message = _("{app}: a new event has just happened{entity}").format(
app=C.APP_NAME,
entity=u' ({})'.format(entity) if entity else '')
self.x_notify.sendNotification(message)
def newWidget(self, widget):
if self.selected_widget is None:
self.selectWidget(widget)
@ -651,6 +664,7 @@ class PrimitivusApp(QuickApp, InputHistory):
# we have clicked on a private MUC conversation
chat_widget = self.widgets.getOrCreateWidget(Chat, entity, on_new_widget=None, force_hash = Chat.getPrivateHash(contact_list.profile, entity), profile=contact_list.profile)
else:
self.clearNotifs(entity, profile=contact_list.profile)
chat_widget = self.widgets.getOrCreateWidget(Chat, entity, on_new_widget=None, profile=contact_list.profile)
self.selectWidget(chat_widget)
self.menu_roller.addMenu(_('Chat menu'), chat_widget.getMenu(), C.MENU_ID_WIDGET)