Properly handle numeric events
[quassel.git] / src / common / eventmanager.cpp
index 2a20b21..7ef4494 100644 (file)
 
 #include "eventmanager.h"
 
+#include <QCoreApplication>
+#include <QEvent>
 #include <QDebug>
 
 #include "event.h"
+#include "ircevent.h"
 
 EventManager::EventManager(QObject *parent) : QObject(parent) {
 
 }
 
+EventManager::~EventManager() {
+  // pending events won't be delivered anymore, but we do need to delete them
+  qDeleteAll(_eventQueue);
+}
+
 QMetaEnum EventManager::eventEnum() const {
   if(!_enum.isValid()) {
     int eventEnumIndex = metaObject()->indexOfEnumerator("EventType");
@@ -48,6 +56,10 @@ EventManager::EventType EventManager::eventGroupByName(const QString &name) cons
   return type == Invalid? Invalid : static_cast<EventType>(type & EventGroupMask);
 }
 
+QString EventManager::enumName(EventType type) const {
+  return eventEnum().valueToKey(type);
+}
+
 /* NOTE:
    Registering and calling handlers works fine even if they specify a subclass of Event as their parameter.
    However, this most probably is a result from a reinterpret_cast somewhere deep inside Qt, so there is *no*
@@ -72,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;
   }
 }
@@ -102,15 +131,51 @@ void EventManager::registerEventHandler(QList<EventType> events, QObject *object
 
 // not threadsafe! if we should want that, we need to add a mutexed queue somewhere in this general area.
 void EventManager::sendEvent(Event *event) {
-  dispatchEvent(event);
+  // qDebug() << "Sending" << event;
+  _eventQueue.append(event);
+  if(_eventQueue.count() == 1) // we're not currently processing another event
+    processEvents();
+}
+
+void EventManager::customEvent(QEvent *event) {
+  if(event->type() == QEvent::User) {
+    processEvents();
+    event->accept();
+  }
+}
+
+void EventManager::processEvents() {
+  // we only process one event at a time for now, and let Qt's own event processing come in between
+  if(_eventQueue.isEmpty())
+    return;
+  dispatchEvent(_eventQueue.first());
+  _eventQueue.removeFirst();
+  if(_eventQueue.count())
+    QCoreApplication::postEvent(this, new QEvent(QEvent::User));
 }
 
 void EventManager::dispatchEvent(Event *event) {
+  //qDebug() << "Dispatching" << event;
+
   // we try handlers from specialized to generic by masking the enum
 
   // 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