1 # -*- coding: utf-8 -*-
3 ## This file is part of Gajim.
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.
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/>.
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>
28 log
= logging
.getLogger('gajim.common.ged')
42 class GlobalEventsDispatcher(object):
47 def register_event_handler(self
, event_name
, priority
, handler
):
48 if event_name
in self
.handlers
:
49 handlers_list
= self
.handlers
[event_name
]
51 for i
, h
in enumerate(handlers_list
):
55 # no event with smaller prio found, put it at the end
58 handlers_list
.insert(i
, (priority
, handler
))
60 self
.handlers
[event_name
] = [(priority
, handler
)]
62 def remove_event_handler(self
, event_name
, priority
, handler
):
63 if event_name
in self
.handlers
:
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
):