X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Feventmanager.cpp;h=7ef44942db63c0a8fe58cb49df16027d32f6906e;hp=2a20b21410f5aad21b0c154d280befd1bdec9dbd;hb=ec383094a149ea409686beb7694d06d5b49d048c;hpb=cd78f70e75d797cb6217b32fceebdfb6022fbe83 diff --git a/src/common/eventmanager.cpp b/src/common/eventmanager.cpp index 2a20b214..7ef44942 100644 --- a/src/common/eventmanager.cpp +++ b/src/common/eventmanager.cpp @@ -20,14 +20,22 @@ #include "eventmanager.h" +#include +#include #include #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(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)].append(handler); + registeredHandlers()[eventType].append(handler); qDebug() << "Registered event handler for" << methodSignature << "in" << object; } } @@ -102,15 +131,51 @@ void EventManager::registerEventHandler(QList 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 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