From ec383094a149ea409686beb7694d06d5b49d048c Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Tue, 28 Sep 2010 09:32:55 +0200 Subject: [PATCH] Properly handle numeric events --- src/common/eventmanager.cpp | 40 +++++++++++++++++++++++++++++++++---- src/common/eventmanager.h | 1 + 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/common/eventmanager.cpp b/src/common/eventmanager.cpp index 96648646..7ef44942 100644 --- a/src/common/eventmanager.cpp +++ b/src/common/eventmanager.cpp @@ -25,6 +25,7 @@ #include #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)].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 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 diff --git a/src/common/eventmanager.h b/src/common/eventmanager.h index 698b3746..780d21b7 100644 --- a/src/common/eventmanager.h +++ b/src/common/eventmanager.h @@ -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 }; -- 2.20.1