1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include "common-export.h"
32 class COMMON_EXPORT EventManager : public QObject
35 Q_FLAGS(EventFlag EventFlags)
56 Self = 0x01, ///< Self-generated (user input) event
57 Fake = 0x08, ///< Ignore this in CoreSessionEventProcessor
58 Netsplit = 0x10, ///< Netsplit join/part, ignore on display
60 Silent = 0x40, ///< Don't generate a MessageEvent
63 Q_DECLARE_FLAGS(EventFlags, EventFlag)
68 /* These values make sense! Don't change without knowing what you do! */
72 GenericEvent = 0x00000000,
74 // for event group handlers (handleIrcEvent() will handle all IrcEvent* enums)
75 // event groups are specified by bits 20-24
76 EventGroupMask = 0x00ff0000,
78 NetworkEvent = 0x00010000,
89 IrcServerEvent = 0x00020000,
93 IrcEvent = 0x00030000,
112 IrcEventError, /// ERROR message from server
113 IrcEventSetname, ///< Updated realname information
115 IrcEventRawPrivmsg, ///< Undecoded privmsg (still needs CTCP parsing)
116 IrcEventRawNotice, ///< Undecoded notice (still needs CTCP parsing)
117 IrcEventUnknown, ///< Unknown non-numeric cmd
119 IrcEventNumeric = 0x00031000, /* needs 1000 (0x03e8) consecutive free values! */
120 IrcEventNumericMask = 0x00000fff, /* for checking if an event is numeric */
122 MessageEvent = 0x00040000, ///< Stringified event suitable for converting to Message
124 CtcpEvent = 0x00050000,
127 KeyEvent = 0x00060000
130 EventManager(QObject* parent = nullptr);
132 static EventType eventTypeByName(const QString& name);
133 static EventType eventGroupByName(const QString& name);
134 static QString enumName(EventType type);
135 static QString enumName(int type); // for sanity tests
137 Event* createEvent(const QVariantMap& map);
140 void registerObject(QObject* object,
141 Priority priority = NormalPriority,
142 const QString& methodPrefix = "process",
143 const QString& filterPrefix = "filter");
144 void registerEventHandler(EventType event, QObject* object, const char* slot, Priority priority = NormalPriority, bool isFilter = false);
145 void registerEventHandler(
146 QList<EventType> events, QObject* object, const char* slot, Priority priority = NormalPriority, bool isFilter = false);
148 void registerEventFilter(EventType event, QObject* object, const char* slot);
149 void registerEventFilter(QList<EventType> events, QObject* object, const char* slot);
151 //! Send an event to the registered handlers
153 The EventManager takes ownership of the event and will delete it once it's processed.
154 @param event The event to be dispatched
156 void postEvent(Event* event);
159 virtual Network* networkById(NetworkId id) const = 0;
160 void customEvent(QEvent* event) override;
169 explicit Handler(QObject* obj = nullptr, int method = 0, Priority prio = NormalPriority)
172 methodIndex = method;
177 using HandlerHash = QHash<uint, QList<Handler>>;
179 inline const HandlerHash& registeredHandlers() const { return _registeredHandlers; }
180 inline HandlerHash& registeredHandlers() { return _registeredHandlers; }
182 inline const HandlerHash& registeredFilters() const { return _registeredFilters; }
183 inline HandlerHash& registeredFilters() { return _registeredFilters; }
185 //! Add handlers to an existing sorted (by priority) handler list
186 void insertHandlers(const QList<Handler>& newHandlers, QList<Handler>& existing, bool checkDupes = false);
187 //! Add filters to an existing filter hash
188 void insertFilters(const QList<Handler>& newFilters, QHash<QObject*, Handler>& existing);
190 int findEventType(const QString& methodSignature, const QString& methodPrefix) const;
192 void processEvent(Event* event);
193 void dispatchEvent(Event* event);
195 //! @return the EventType enum
196 static QMetaEnum eventEnum();
198 HandlerHash _registeredHandlers;
199 HandlerHash _registeredFilters;
200 QList<Event*> _eventQueue;
201 static QMetaEnum _enum;
204 Q_DECLARE_OPERATORS_FOR_FLAGS(EventManager::EventFlags)