2018-09-05 02:59:34 +02:00
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2008-08-10 15:40:49 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
Global Events Dispatcher module.
|
|
|
|
|
|
|
|
:author: Mateusz Biliński <mateusz@bilinski.it>
|
|
|
|
:since: 8th August 2008
|
|
|
|
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
|
2011-05-18 19:44:43 +02:00
|
|
|
:copyright: Copyright (2011) Yann Leboulanger <asterix@lagaule.org>
|
2008-08-10 15:40:49 +02:00
|
|
|
:license: GPL
|
|
|
|
'''
|
|
|
|
|
2018-09-17 21:11:45 +02:00
|
|
|
import logging
|
2011-11-22 22:51:33 +01:00
|
|
|
import traceback
|
2018-12-19 02:17:12 +01:00
|
|
|
import inspect
|
2011-11-22 22:51:33 +01:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
from nbxmpp import NodeProcessed
|
2018-09-17 21:11:45 +02:00
|
|
|
|
2011-10-07 17:51:57 +02:00
|
|
|
log = logging.getLogger('gajim.c.ged')
|
2008-08-10 15:40:49 +02:00
|
|
|
|
2011-05-01 22:09:50 +02:00
|
|
|
PRECORE = 10
|
|
|
|
CORE = 20
|
|
|
|
POSTCORE = 30
|
|
|
|
PREGUI = 40
|
|
|
|
PREGUI1 = 50
|
2010-07-20 10:35:38 +02:00
|
|
|
GUI1 = 60
|
2011-05-01 22:09:50 +02:00
|
|
|
POSTGUI1 = 70
|
|
|
|
PREGUI2 = 80
|
|
|
|
GUI2 = 90
|
|
|
|
POSTGUI2 = 100
|
|
|
|
POSTGUI = 110
|
2008-08-10 15:40:49 +02:00
|
|
|
|
2011-05-18 19:44:43 +02:00
|
|
|
OUT_PREGUI = 10
|
|
|
|
OUT_PREGUI1 = 20
|
|
|
|
OUT_GUI1 = 30
|
|
|
|
OUT_POSTGUI1 = 40
|
|
|
|
OUT_PREGUI2 = 50
|
|
|
|
OUT_GUI2 = 60
|
|
|
|
OUT_POSTGUI2 = 70
|
|
|
|
OUT_POSTGUI = 80
|
|
|
|
OUT_PRECORE = 90
|
|
|
|
OUT_CORE = 100
|
|
|
|
OUT_POSTCORE = 110
|
|
|
|
|
2018-09-16 11:56:56 +02:00
|
|
|
class GlobalEventsDispatcher:
|
2010-01-19 22:06:00 +01:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.handlers = {}
|
2010-01-19 22:06:00 +01:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
def register_event_handler(self, event_name, priority, handler):
|
|
|
|
if event_name in self.handlers:
|
|
|
|
handlers_list = self.handlers[event_name]
|
|
|
|
i = 0
|
2018-09-17 21:11:45 +02:00
|
|
|
for i, handler_tuple in enumerate(handlers_list):
|
|
|
|
if priority < handler_tuple[0]:
|
2010-04-08 01:20:17 +02:00
|
|
|
break
|
2010-09-17 12:41:00 +02:00
|
|
|
else:
|
|
|
|
# no event with smaller prio found, put it at the end
|
|
|
|
i += 1
|
2010-01-19 22:06:00 +01:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
handlers_list.insert(i, (priority, handler))
|
|
|
|
else:
|
|
|
|
self.handlers[event_name] = [(priority, handler)]
|
2010-01-19 22:06:00 +01:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
def remove_event_handler(self, event_name, priority, handler):
|
|
|
|
if event_name in self.handlers:
|
|
|
|
try:
|
|
|
|
self.handlers[event_name].remove((priority, handler))
|
2016-11-05 15:57:13 +01:00
|
|
|
except ValueError as error:
|
2018-09-17 18:57:00 +02:00
|
|
|
log.warning(
|
|
|
|
'''Function (%s) with priority "%s" never
|
|
|
|
registered as handler of event "%s". Couldn\'t remove.
|
|
|
|
Error: %s''', handler, priority, event_name, error)
|
2010-01-19 22:06:00 +01:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
def raise_event(self, event_name, *args, **kwargs):
|
2018-12-19 02:17:12 +01:00
|
|
|
log.debug('Raise event: %s', event_name)
|
2010-04-08 01:20:17 +02:00
|
|
|
if event_name in self.handlers:
|
2012-04-08 17:53:53 +02:00
|
|
|
node_processed = False
|
2018-12-18 14:08:27 +01:00
|
|
|
# Iterate over a copy of the handlers list, so while iterating
|
|
|
|
# the original handlers list can be modified
|
|
|
|
for _priority, handler in list(self.handlers[event_name]):
|
2011-11-22 22:51:33 +01:00
|
|
|
try:
|
2018-12-19 02:17:12 +01:00
|
|
|
if inspect.ismethod(handler):
|
|
|
|
log.debug('Call handler %s on %s',
|
|
|
|
handler.__name__,
|
|
|
|
handler.__self__)
|
|
|
|
else:
|
|
|
|
log.debug('Call handler %s', handler.__name__)
|
2011-11-22 22:51:33 +01:00
|
|
|
if handler(*args, **kwargs):
|
|
|
|
return True
|
2012-04-08 17:53:53 +02:00
|
|
|
except NodeProcessed:
|
|
|
|
node_processed = True
|
2013-01-05 00:03:36 +01:00
|
|
|
except Exception:
|
2017-11-07 22:56:15 +01:00
|
|
|
log.error('Error while running an event handler: %s',
|
|
|
|
handler)
|
2011-11-22 22:51:33 +01:00
|
|
|
traceback.print_exc()
|
2012-04-08 17:53:53 +02:00
|
|
|
if node_processed:
|
|
|
|
raise NodeProcessed
|