54d2721436f5743defccc98df6303944981ea874
[quassel.git] / src / common / eventmanager.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef EVENTMANAGER_H
22 #define EVENTMANAGER_H
23
24 #include <QHash>
25 #include <QObject>
26
27 class Event;
28
29 class EventManager : public QObject {
30   Q_OBJECT
31   Q_ENUMS(EventType)
32
33 public:
34
35   enum RegistrationMode {
36     Prepend,
37     Append
38   };
39
40   enum Priority {
41     VeryLowPriority,
42     LowPriority,
43     NormalPriority,
44     HighPriority,
45     HighestPriority
46   };
47
48   /*
49
50   */
51   /* These values make sense! Don't change without knowing what you do! */
52   enum EventType {
53     Invalid                     = 0xffffffff,
54     GenericEvent                = 0x00000000,
55
56     // for event group handlers (handleIrcEvent() will handle all IrcEvent* enums)
57     // event groups are specified by bits 20-24
58     EventGroupMask              = 0x00ff0000,
59
60     NetworkEvent                = 0x00010000,
61     NetworkConnecting,
62     NetworkInitializing,
63     NetworkInitialized,
64     NetworkReconnecting,
65     NetworkDisconnecting,
66     NetworkDisconnected,
67     NetworkIncoming,
68
69     IrcServerEvent              = 0x00020000,
70     IrcServerIncoming,
71     IrcServerParseError,
72
73     IrcEvent                    = 0x00030000,
74     IrcEventCap,
75     IrcEventCapAuthenticate,
76     IrcEventInvite,
77     IrcEventJoin,
78     IrcEventKick,
79     IrcEventMode,
80     IrcEventNick,
81     IrcEventNotice,
82     IrcEventPart,
83     IrcEventPing,
84     IrcEventPong,
85     IrcEventPrivmsg,
86     IrcEventQuit,
87     IrcEventTopic,
88
89     IrcEventNumeric             = 0x00031000, /* needs 1000 (0x03e8) consecutive free values! */
90   };
91
92   EventManager(QObject *parent = 0);
93   //virtual ~EventManager();
94
95   QStringList providesEnums();
96
97 public slots:
98   void registerObject(QObject *object, Priority priority = NormalPriority, const QString &methodPrefix = "handle");
99   void registerEventHandler(EventType event, QObject *object, const char *slot, Priority priority = NormalPriority);
100   void registerEventHandler(QList<EventType> events, QObject *object, const char *slot, Priority priority = NormalPriority);
101
102   //! Send an event to the registered handlers
103   /**
104     The EventManager takes ownership of the event and will delete it once it's processed.
105     NOTE: This method is not threadsafe!
106     @param event The event to be dispatched
107    */
108   void sendEvent(Event *event);
109
110 private:
111   struct Handler {
112     QObject *object;
113     int methodIndex;
114     Priority priority;
115
116     explicit Handler(QObject *obj = 0, int method = 0, Priority prio = NormalPriority) {
117       object = obj;
118       methodIndex = method;
119       priority = prio;
120     }
121   };
122
123   typedef QHash<uint, QList<Handler> > HandlerHash;
124
125   inline const HandlerHash &registeredHandlers() const { return _registeredHandlers; }
126   inline HandlerHash &registeredHandlers() { return _registeredHandlers; }
127
128   //! Add handlers to an existing sorted (by priority) handler list
129   void insertHandlers(const QList<Handler> &newHandlers, QList<Handler> &existing);
130
131   void dispatchEvent(Event *event);
132
133   HandlerHash _registeredHandlers;
134
135 };
136
137 #endif