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