X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Feventmanager.cpp;h=552765a243dccf9c83f9ad06b51e64863228f858;hp=c5f01b630c4d8a4a51cb801841f4cb7f982e2b60;hb=80cd03dc284fecbac2b7db1ede215f82023b72d9;hpb=03b1230e44adca6b808a6a702aa5173e578a1160 diff --git a/src/common/eventmanager.cpp b/src/common/eventmanager.cpp index c5f01b63..552765a2 100644 --- a/src/common/eventmanager.cpp +++ b/src/common/eventmanager.cpp @@ -21,7 +21,6 @@ #include "eventmanager.h" #include -#include #include "event.h" @@ -29,11 +28,45 @@ EventManager::EventManager(QObject *parent) : QObject(parent) { } -void EventManager::registerObject(QObject *object, Priority priority, const QString &methodPrefix) { - int eventEnumIndex = metaObject()->indexOfEnumerator("EventType"); - Q_ASSERT(eventEnumIndex >= 0); - QMetaEnum eventEnum = metaObject()->enumerator(eventEnumIndex); +QMetaEnum EventManager::eventEnum() const { + if(!_enum.isValid()) { + int eventEnumIndex = metaObject()->indexOfEnumerator("EventType"); + Q_ASSERT(eventEnumIndex >= 0); + _enum = metaObject()->enumerator(eventEnumIndex); + Q_ASSERT(_enum.isValid()); + } + return _enum; +} + +EventManager::EventType EventManager::eventTypeByName(const QString &name) const { + int val = eventEnum().keyToValue(name.toLatin1()); + return (val == -1) ? Invalid : static_cast(val); +} + +EventManager::EventType EventManager::eventGroupByName(const QString &name) const { + EventType type = eventTypeByName(name); + 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* + type safety. If the event sent is of the wrong class type, you'll get a neat segfault! + Thus, we need to make sure that events are of the correct class type when sending! + + We might add a registration-time check later, which will require matching the enum base name (e.g. "IrcEvent") with + the type the handler claims to support. This still won't protect us from someone sending an IrcEvent object + with an enum type "NetworkIncoming", for example. + + Another way would be to add a check into the various Event subclasses, such that the ctor matches the given event type + with the actual class. Possibly (optionally) using rtti... +*/ + +void EventManager::registerObject(QObject *object, Priority priority, const QString &methodPrefix) { for(int i = object->metaObject()->methodOffset(); i < object->metaObject()->methodCount(); i++) { QString methodSignature(object->metaObject()->method(i).signature()); @@ -43,7 +76,7 @@ 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 = eventEnum().keyToValue(methodSignature.toAscii()); if(eventType < 0) { qWarning() << Q_FUNC_INFO << QString("Could not find EventType %1").arg(methodSignature); continue; @@ -73,6 +106,7 @@ 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) { + // qDebug() << "Sending" << event; dispatchEvent(event); }