Properly handle numeric events
authorManuel Nickschas <sputnick@quassel-irc.org>
Tue, 28 Sep 2010 07:32:55 +0000 (09:32 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Wed, 13 Oct 2010 23:06:31 +0000 (01:06 +0200)
src/common/eventmanager.cpp
src/common/eventmanager.h

index 9664864..7ef4494 100644 (file)
@@ -25,6 +25,7 @@
 #include <QDebug>
 
 #include "event.h"
+#include "ircevent.h"
 
 EventManager::EventManager(QObject *parent) : QObject(parent) {
 
@@ -83,13 +84,30 @@ void EventManager::registerObject(QObject *object, Priority priority, const QStr
     methodSignature = methodSignature.section('(',0,0);  // chop the attribute list
     methodSignature = methodSignature.mid(methodPrefix.length()); // strip prefix
 
-    int eventType = eventEnum().keyToValue(methodSignature.toAscii());
+    int eventType = -1;
+
+    // special handling for numeric IrcEvents: IrcEvent042 gets mapped to IrcEventNumeric + 42
+    if(methodSignature.length() == 8+3 && methodSignature.startsWith("IrcEvent")) {
+      int num = methodSignature.right(3).toUInt();
+      if(num > 0) {
+        QString numericSig = methodSignature.left(methodSignature.length() - 3) + "Numeric";
+        eventType = eventEnum().keyToValue(numericSig.toAscii());
+        if(eventType < 0) {
+          qWarning() << Q_FUNC_INFO << "Could not find EventType" << numericSig << "for handling" << methodSignature;
+          continue;
+        }
+        eventType += num;
+      }
+    }
+
+    if(eventType < 0)
+      eventType = eventEnum().keyToValue(methodSignature.toAscii());
     if(eventType < 0) {
-      qWarning() << Q_FUNC_INFO << QString("Could not find EventType %1").arg(methodSignature);
+      qWarning() << Q_FUNC_INFO << "Could not find EventType" << methodSignature;
       continue;
     }
     Handler handler(object, i, priority);
-    registeredHandlers()[static_cast<EventType>(eventType)].append(handler);
+    registeredHandlers()[eventType].append(handler);
     qDebug() << "Registered event handler for" << methodSignature << "in" << object;
   }
 }
@@ -143,7 +161,21 @@ void EventManager::dispatchEvent(Event *event) {
 
   // build a list sorted by priorities that contains all eligible handlers
   QList<Handler> handlers;
-  EventType type = event->type();
+  uint type = event->type();
+
+  // special handling for numeric IrcEvents
+  if((type & ~IrcEventNumericMask) == IrcEventNumeric) {
+    ::IrcEventNumeric *numEvent = static_cast< ::IrcEventNumeric *>(event);
+    if(!numEvent)
+      qWarning() << "Invalid event type for IrcEventNumeric!";
+    else {
+      int num = numEvent->number();
+      if(num > 0)
+        insertHandlers(registeredHandlers().value(type + num), handlers);
+    }
+  }
+
+  // exact type
   insertHandlers(registeredHandlers().value(type), handlers);
 
   // check if we have a generic handler for the event group
index 698b374..780d21b 100644 (file)
@@ -96,6 +96,7 @@ public:
     IrcEventUnknown,    ///< Unknown non-numeric cmd
 
     IrcEventNumeric             = 0x00031000, /* needs 1000 (0x03e8) consecutive free values! */
+    IrcEventNumericMask         = 0x00000fff, /* for checking if an event is numeric */
 
     MessageEvent                = 0x00040000, ///< Stringified event suitable for converting to Message
   };