Provide (de)serialization for all event types
[quassel.git] / src / common / eventmanager.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 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 <QMetaEnum>
25
26 #include "types.h"
27
28 class Event;
29 class Network;
30
31 class EventManager : public QObject {
32   Q_OBJECT
33   Q_FLAGS(EventFlag EventFlags)
34   Q_ENUMS(EventType)
35
36 public:
37
38   enum RegistrationMode {
39     Prepend,
40     Append
41   };
42
43   enum Priority {
44     VeryLowPriority,
45     LowPriority,
46     NormalPriority,
47     HighPriority,
48     HighestPriority
49   };
50
51   enum EventFlag {
52     Self     = 0x01, ///< Self-generated (user input) event
53     Fake     = 0x08, ///< Ignore this in CoreSessionEventProcessor
54     Netsplit = 0x10, ///< Netsplit join/part, ignore on display
55     Backlog  = 0x20,
56     Silent   = 0x40, ///< Don't generate a MessageEvent
57     Stopped  = 0x80
58   };
59   Q_DECLARE_FLAGS(EventFlags, EventFlag)
60
61   /*
62
63   */
64   /* These values make sense! Don't change without knowing what you do! */
65   enum EventType {
66     Invalid                     = 0xffffffff,
67     GenericEvent                = 0x00000000,
68
69     // for event group handlers (handleIrcEvent() will handle all IrcEvent* enums)
70     // event groups are specified by bits 20-24
71     EventGroupMask              = 0x00ff0000,
72
73     NetworkEvent                = 0x00010000,
74     NetworkConnecting,
75     NetworkInitializing,
76     NetworkInitialized,
77     NetworkReconnecting,
78     NetworkDisconnecting,
79     NetworkDisconnected,
80     NetworkSplitJoin,
81     NetworkSplitQuit,
82     NetworkIncoming,
83
84     IrcServerEvent              = 0x00020000,
85     IrcServerIncoming,
86     IrcServerParseError,
87
88     IrcEvent                    = 0x00030000,
89     IrcEventAuthenticate,
90     IrcEventCap,
91     IrcEventInvite,
92     IrcEventJoin,
93     IrcEventKick,
94     IrcEventMode,
95     IrcEventNick,
96     IrcEventNotice,
97     IrcEventPart,
98     IrcEventPing,
99     IrcEventPong,
100     IrcEventPrivmsg,
101     IrcEventQuit,
102     IrcEventTopic,
103     IrcEventRawPrivmsg, ///< Undecoded privmsg (still needs CTCP parsing)
104     IrcEventRawNotice,  ///< Undecoded notice (still needs CTCP parsing)
105     IrcEventUnknown,    ///< Unknown non-numeric cmd
106
107     IrcEventNumeric             = 0x00031000, /* needs 1000 (0x03e8) consecutive free values! */
108     IrcEventNumericMask         = 0x00000fff, /* for checking if an event is numeric */
109
110     MessageEvent                = 0x00040000, ///< Stringified event suitable for converting to Message
111
112     CtcpEvent                   = 0x00050000,
113     CtcpEventFlush
114   };
115
116   EventManager(QObject *parent = 0);
117
118   static EventType eventTypeByName(const QString &name);
119   static EventType eventGroupByName(const QString &name);
120   static QString enumName(EventType type);
121   static QString enumName(int type); // for sanity tests
122
123   Event *createEvent(const QVariantMap &map);
124
125 public slots:
126   void registerObject(QObject *object, Priority priority = NormalPriority,
127                       const QString &methodPrefix = "process",
128                       const QString &filterPrefix = "filter");
129   void registerEventHandler(EventType event, QObject *object, const char *slot,
130                             Priority priority = NormalPriority, bool isFilter = false);
131   void registerEventHandler(QList<EventType> events, QObject *object, const char *slot,
132                             Priority priority = NormalPriority, bool isFilter = false);
133
134   void registerEventFilter(EventType event, QObject *object, const char *slot);
135   void registerEventFilter(QList<EventType> events, QObject *object, const char *slot);
136
137   //! Send an event to the registered handlers
138   /**
139     The EventManager takes ownership of the event and will delete it once it's processed.
140     @param event The event to be dispatched
141    */
142   void postEvent(Event *event);
143
144 protected:
145   virtual Network *networkById(NetworkId id) const = 0;
146   virtual void customEvent(QEvent *event);
147
148 private:
149   struct Handler {
150     QObject *object;
151     int methodIndex;
152     Priority priority;
153
154     explicit Handler(QObject *obj = 0, int method = 0, Priority prio = NormalPriority) {
155       object = obj;
156       methodIndex = method;
157       priority = prio;
158     }
159   };
160
161   typedef QHash<uint, QList<Handler> > HandlerHash;
162
163   inline const HandlerHash &registeredHandlers() const { return _registeredHandlers; }
164   inline HandlerHash &registeredHandlers() { return _registeredHandlers; }
165
166   inline const HandlerHash &registeredFilters() const { return _registeredFilters; }
167   inline HandlerHash &registeredFilters() { return _registeredFilters; }
168
169   //! Add handlers to an existing sorted (by priority) handler list
170   void insertHandlers(const QList<Handler> &newHandlers, QList<Handler> &existing, bool checkDupes = false);
171   //! Add filters to an existing filter hash
172   void insertFilters(const QList<Handler> &newFilters, QHash<QObject *, Handler> &existing);
173
174   int findEventType(const QString &methodSignature, const QString &methodPrefix) const;
175
176   void processEvent(Event *event);
177   void dispatchEvent(Event *event);
178
179   //! @return the EventType enum
180   static QMetaEnum eventEnum();
181
182   HandlerHash _registeredHandlers;
183   HandlerHash _registeredFilters;
184   QList<Event *> _eventQueue;
185   static QMetaEnum _enum;
186 };
187
188 Q_DECLARE_OPERATORS_FOR_FLAGS(EventManager::EventFlags);
189
190 #endif