Basic event dispatching (with priorities)
[quassel.git] / src / common / eventmanager.h
index 78a72ac..b19e8ee 100644 (file)
@@ -37,40 +37,55 @@ public:
     Append
   };
 
+  enum Priority {
+    VeryLowPriority,
+    LowPriority,
+    NormalPriority,
+    HighPriority,
+    HighestPriority
+  };
+
   /*
 
   */
+  /* These values make sense! Don't change without knowing what you do! */
   enum EventType {
     Invalid                     = 0xffffffff,
-    GenericEvent                  = 0x000000,
-
-    IrcServerEvent                = 0x010000,
-    IrcServerLooking              = 0x010001,
-    IrcServerConnecting           = 0x010002,
-    IrcServerConnected            = 0x010003,
-    IrcServerConnectionFailure    = 0x010004,
-    IrcServerDisconnected         = 0x010005,
-    IrcServerQuit                 = 0x010006,
-
-    IrcServerIncoming             = 0x010007,
-
-    IrcEvent                      = 0x020000,
-    IrcEventCap                   = 0x020001,
-    IrcEventCapAuthenticate       = 0x020002,
-    IrcEventInvite                = 0x020003,
-    IrcEventJoin                  = 0x020004,
-    IrcEventKick                  = 0x020005,
-    IrcEventMode                  = 0x020006,
-    IrcEventNick                  = 0x020007,
-    IrcEventNotice                = 0x020008,
-    IrcEventPart                  = 0x020009,
-    IrcEventPing                  = 0x02000a,
-    IrcEventPong                  = 0x02000b,
-    IrcEventPrivmsg               = 0x02000c,
-    IrcEventQuit                  = 0x02000d,
-    IrcEventTopic                 = 0x02000e,
-
-    IrcEventNumeric               = 0x021000 /* needs 1000 (0x03e8) consecutive free values! */
+    GenericEvent                = 0x00000000,
+
+    // for event group handlers (handleIrcEvent() will handle all IrcEvent* enums)
+    // event groups are specified by bits 20-24
+    EventGroupMask              = 0x00ff0000,
+
+    NetworkEvent                = 0x00010000,
+    NetworkConnecting,
+    NetworkInitializing,
+    NetworkInitialized,
+    NetworkReconnecting,
+    NetworkDisconnecting,
+    NetworkDisconnected,
+    NetworkIncoming,
+
+    IrcServerEvent              = 0x00020000,
+    IrcServerIncoming,
+
+    IrcEvent                    = 0x00030000,
+    IrcEventCap,
+    IrcEventCapAuthenticate,
+    IrcEventInvite,
+    IrcEventJoin,
+    IrcEventKick,
+    IrcEventMode,
+    IrcEventNick,
+    IrcEventNotice,
+    IrcEventPart,
+    IrcEventPing,
+    IrcEventPong,
+    IrcEventPrivmsg,
+    IrcEventQuit,
+    IrcEventTopic,
+
+    IrcEventNumeric             = 0x00031000, /* needs 1000 (0x03e8) consecutive free values! */
   };
 
   EventManager(QObject *parent = 0);
@@ -79,27 +94,41 @@ public:
   QStringList providesEnums();
 
 public slots:
-  void registerObject(QObject *object, RegistrationMode mode = Append, const QString &methodPrefix = "handle");
-  void registerEventHandler(EventType event, QObject *object, const char *slot, RegistrationMode mode = Append);
-  void registerEventHandler(QList<EventType> events, QObject *object, const char *slot, RegistrationMode mode = Append);
-
-  //void sendEvent(Event *event);
+  void registerObject(QObject *object, Priority priority = NormalPriority, const QString &methodPrefix = "handle");
+  void registerEventHandler(EventType event, QObject *object, const char *slot, Priority priority = NormalPriority);
+  void registerEventHandler(QList<EventType> events, QObject *object, const char *slot, Priority priority = NormalPriority);
+
+  //! Send an event to the registered handlers
+  /**
+    The EventManager takes ownership of the event and will delete it once it's processed.
+    NOTE: This method is not threadsafe!
+    @param event The event to be dispatched
+   */
+  void sendEvent(Event *event);
 
 private:
   struct Handler {
     QObject *object;
     int methodIndex;
+    Priority priority;
 
-    explicit Handler(QObject *obj = 0, int method = 0) {
+    explicit Handler(QObject *obj = 0, int method = 0, Priority prio = NormalPriority) {
       object = obj;
       methodIndex = method;
+      priority = prio;
     }
   };
 
-  typedef QHash<EventType, QList<Handler> > HandlerHash;
+  typedef QHash<uint, QList<Handler> > HandlerHash;
 
   inline const HandlerHash &registeredHandlers() const { return _registeredHandlers; }
   inline HandlerHash &registeredHandlers() { return _registeredHandlers; }
+
+  //! Add handlers to an existing sorted (by priority) handler list
+  void insertHandlers(const QList<Handler> &newHandlers, QList<Handler> &existing);
+
+  void dispatchEvent(Event *event);
+
   HandlerHash _registeredHandlers;
 
 };