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