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