new way to handle incominf messages, new notification event.
[gajim.git] / src / common / ged.py
blob81af8d9955c1d019642c21e6ae37df607e8293d2
1 # -*- coding: utf-8 -*-
3 ## This file is part of Gajim.
4 ##
5 ## Gajim is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## Gajim is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
14 ## You should have received a copy of the GNU General Public License
15 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
18 '''
19 Global Events Dispatcher module.
21 :author: Mateusz Biliński <mateusz@bilinski.it>
22 :since: 8th August 2008
23 :copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
24 :license: GPL
25 '''
27 import logging
28 log = logging.getLogger('gajim.common.ged')
30 PRECORE = 10
31 CORE = 20
32 POSTCORE = 30
33 PREGUI = 40
34 PREGUI1 = 50
35 GUI1 = 60
36 POSTGUI1 = 70
37 PREGUI2 = 80
38 GUI2 = 90
39 POSTGUI2 = 100
40 POSTGUI = 110
42 class GlobalEventsDispatcher(object):
44 def __init__(self):
45 self.handlers = {}
47 def register_event_handler(self, event_name, priority, handler):
48 if event_name in self.handlers:
49 handlers_list = self.handlers[event_name]
50 i = 0
51 for i, h in enumerate(handlers_list):
52 if priority < h[0]:
53 break
54 else:
55 # no event with smaller prio found, put it at the end
56 i += 1
58 handlers_list.insert(i, (priority, handler))
59 else:
60 self.handlers[event_name] = [(priority, handler)]
62 def remove_event_handler(self, event_name, priority, handler):
63 if event_name in self.handlers:
64 try:
65 self.handlers[event_name].remove((priority, handler))
66 except ValueError, error:
67 log.warn('''Function (%s) with priority "%s" never registered
68 as handler of event "%s". Couldn\'t remove. Error: %s'''
69 %(handler, priority, event_name, error))
71 def raise_event(self, event_name, *args, **kwargs):
72 log.debug('%s\nArgs: %s'%(event_name, str(args)))
73 if event_name in self.handlers:
74 for priority, handler in self.handlers[event_name]:
75 if handler(*args, **kwargs):
76 return True