#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");
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;
}
}
// not threadsafe! if we should want that, we need to add a mutexed queue somewhere in this general area.
void EventManager::sendEvent(Event *event) {
// qDebug() << "Sending" << event;
- dispatchEvent(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