summaryrefslogtreecommitdiff
path: root/src/handler.py
blob: dd8480619b84207fdbfd63aa6547403f01f15b5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright 2009, 2010 Erwan Briand
# Copyright 2010, Florent Le Coz <louizatakk@fedoraproject.org>

# This program 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 of the License.

# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

from singleton import Singleton

#Todo, it's not a singleton. Oh, also, remove-me

class Handler(Singleton):
    """
    This class is the global handler for the software's signals.
    """
    __is_first_instance = True

    def __init__(self):
        if Handler.__is_first_instance:
            Handler.__is_first_instance = False

            self.__signals__ = {

                'on-connected': list(),
                # At the end of a successful connection process.
                # emitted when presence confirmation is received
                # Args: jid

                'join-room': list(),
                # Join a room.
                # Args: room, nick

                'room-presence': list(),
                # A presence is received
                # Args: the stanza object

                'room-message': list(),
                # A message is received
                # Args: the stanza object

                'private-message': list(),
                # A message is received
                # Args: the stanza object

                'room-delayed-message': list(),
                # A message is received
                # Args: the stanza object

                'send-version': list(),
                # We send our version
                # Args: the stanza we reply to

                'send-time': list(),
                # We send our time
                # Args: the stanza we reply to

                'error-message': list(),
                # We send our time
                # Args: the stanza we reply to

                'error': list()
                # We send our time
                # Args: the stanza we reply to
            }

    def connect(self, signal, func):
        """Connect a function to a signal."""
        if func not in self.__signals__[signal]:
            self.__signals__[signal].append(func)

    def emit(self, signal, **kwargs):
        """Emit a signal."""
        if signal in self.__signals__:
            for func in self.__signals__[signal]:
                func(**kwargs)