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