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