frontends (QuickApp), tools (TriggerManager): frontends can also use triggers + add those for sending and receiving a message

This commit is contained in:
souliane 2015-03-03 15:05:10 +01:00
commit 5d1681de93
2 changed files with 21 additions and 5 deletions

View file

@ -22,6 +22,7 @@ log = getLogger(__name__)
from sat.core.i18n import _
from sat.core import exceptions
from sat.tools.misc import TriggerManager
from sat_frontends.tools import jid
from sat_frontends.quick_frontend.quick_widgets import QuickWidgetsManager
@ -219,6 +220,9 @@ class QuickApp(object):
# listeners
self._listeners = {} # key: listener type ("avatar", "selected", etc), value: list of callbacks
# triggers
self.trigger = TriggerManager() # trigger are used to change the default behaviour
## bridge ##
try:
self.bridge = create_bridge()
@ -453,6 +457,10 @@ class QuickApp(object):
def newMessageHandler(self, from_jid_s, msg, type_, to_jid_s, extra, profile):
from_jid = jid.JID(from_jid_s)
to_jid = jid.JID(to_jid_s)
if not self.trigger.point("newMessageTrigger", from_jid, msg, type_, to_jid, extra, profile=profile):
return
from_me = from_jid.bare == self.profiles[profile].whoami.bare
target = to_jid if from_me else from_jid
@ -486,7 +494,11 @@ class QuickApp(object):
callback = lambda dummy=None: None # FIXME: optional argument is here because pyjamas doesn't support callback without arg with json proxy
if errback is None:
errback = lambda failure: self.showDialog(failure.fullname, failure.message, "error")
self.bridge.sendMessage(to_jid, message, subject, mess_type, extra, profile_key, callback=callback, errback=errback)
if not self.trigger.point("sendMessageTrigger", to_jid, message, subject, mess_type, extra, callback, errback, profile_key=profile_key):
return
self.bridge.sendMessage(unicode(to_jid), message, subject, mess_type, extra, profile_key, callback=callback, errback=errback)
def newAlertHandler(self, msg, title, alert_type, profile):
assert alert_type in ['INFO', 'ERROR']

View file

@ -20,7 +20,6 @@
"""Misc usefull classes"""
from sat.core.i18n import _
import sys
from sat.core.log import getLogger
log = getLogger(__name__)
@ -39,8 +38,13 @@ class TriggerManager(object):
"""This class manage triggers: code which interact to change the behaviour
of SàT"""
MIN_PRIORITY = float('-inf')
MAX_PRIORITY = float('+inf')
try: # FIXME: to be removed when a better solution is found
MIN_PRIORITY = float('-inf')
MAX_PRIORITY = float('+inf')
except: # XXX: Pyjamas will bug if you specify ValueError here
# Pyjamas uses the JS Float class
MIN_PRIORITY = Number.NEGATIVE_INFINITY
MAX_PRIORITY = Number.POSITIVE_INFINITY
def __init__(self):
self.__triggers = {}
@ -81,7 +85,7 @@ class TriggerManager(object):
if point_name not in self.__triggers:
return True
for priority,trigger in self.__triggers[point_name]:
for priority, trigger in self.__triggers[point_name]:
try:
if not trigger(*args, **kwargs):
return False