Basic event dispatching (with priorities)
[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 <QHash>
25 #include <QObject>
26
27 class Event;
28
29 class EventManager : public QObject {
30   Q_OBJECT
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   /*
49
50   */
51   /* These values make sense! Don't change without knowing what you do! */
52   enum EventType {
53     Invalid                     = 0xffffffff,
54     GenericEvent                = 0x00000000,
55
56     // for event group handlers (handleIrcEvent() will handle all IrcEvent* enums)
57     // event groups are specified by bits 20-24
58     EventGroupMask              = 0x00ff0000,
59
60     NetworkEvent                = 0x00010000,
61     NetworkConnecting,
62     NetworkInitializing,
63     NetworkInitialized,
64     NetworkReconnecting,
65     NetworkDisconnecting,
66     NetworkDisconnected,
67     NetworkIncoming,
68
69     IrcServerEvent              = 0x00020000,
70     IrcServerIncoming,
71
72     IrcEvent                    = 0x00030000,
73     IrcEventCap,
74     IrcEventCapAuthenticate,
75     IrcEventInvite,
76     IrcEventJoin,
77     IrcEventKick,
78     IrcEventMode,
79     IrcEventNick,
80     IrcEventNotice,
81     IrcEventPart,
82     IrcEventPing,
83     IrcEventPong,
84     IrcEventPrivmsg,
85     IrcEventQuit,
86     IrcEventTopic,
87
88     IrcEventNumeric             = 0x00031000, /* needs 1000 (0x03e8) consecutive free values! */
89   };
90
91   EventManager(QObject *parent = 0);
92   //virtual ~EventManager();
93
94   QStringList providesEnums();
95
96 public slots:
97   void registerObject(QObject *object, Priority priority = NormalPriority, const QString &methodPrefix = "handle");
98   void registerEventHandler(EventType event, QObject *object, const char *slot, Priority priority = NormalPriority);
99   void registerEventHandler(QList<EventType> events, QObject *object, const char *slot, Priority priority = NormalPriority);
100
101   //! Send an event to the registered handlers
102   /**
103     The EventManager takes ownership of the event and will delete it once it's processed.
104     NOTE: This method is not threadsafe!
105     @param event The event to be dispatched
106    */
107   void sendEvent(Event *event);
108
109 private:
110   struct Handler {
111     QObject *object;
112     int methodIndex;
113     Priority priority;
114
115     explicit Handler(QObject *obj = 0, int method = 0, Priority prio = NormalPriority) {
116       object = obj;
117       methodIndex = method;
118       priority = prio;
119     }
120   };
121
122   typedef QHash<uint, QList<Handler> > HandlerHash;
123
124   inline const HandlerHash &registeredHandlers() const { return _registeredHandlers; }
125   inline HandlerHash &registeredHandlers() { return _registeredHandlers; }
126
127   //! Add handlers to an existing sorted (by priority) handler list
128   void insertHandlers(const QList<Handler> &newHandlers, QList<Handler> &existing);
129
130   void dispatchEvent(Event *event);
131
132   HandlerHash _registeredHandlers;
133
134 };
135
136 #endif