X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fevent.cpp;h=02d3f66722b8be84bb1a28b4a67c4e6d2635cf50;hp=19b56a7d487ecc21a94a5bb20aa2f8055788816b;hb=db00831bca59a012242d1ad5fac52a20c6cd2956;hpb=6f38b6fdeb73e726c24b26a97b98c9cfe0fc8a0e diff --git a/src/common/event.cpp b/src/common/event.cpp index 19b56a7d..02d3f667 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2012 by the Quassel Project * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,109 +15,123 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #include "ctcpevent.h" #include "ircevent.h" -#include "networkevent.h" #include "messageevent.h" +#include "networkevent.h" +#include "peer.h" +#include "signalproxy.h" Event::Event(EventManager::EventType type) - : _type(type) - , _valid(true) -{ - -} - + : _type(type) +{} -Event::Event(EventManager::EventType type, QVariantMap &map) - : _type(type) - , _valid(true) +Event::Event(EventManager::EventType type, QVariantMap& map) + : _type(type) { - if(!map.contains("flags") || !map.contains("timestamp")) { - qWarning() << "Received invalid serialized event:" << map; - setValid(false); - return; - } - - setFlags(static_cast(map.take("flags").toInt())); // TODO sanity check? - setTimestamp(QDateTime::fromTime_t(map.take("timestamp").toUInt())); + if (!map.contains("flags") || !map.contains("timestamp")) { + qWarning() << "Received invalid serialized event:" << map; + setValid(false); + return; + } + + Q_ASSERT(SignalProxy::current()); + Q_ASSERT(SignalProxy::current()->sourcePeer()); + + setFlags(static_cast(map.take("flags").toInt())); // TODO sanity check? + + if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::LongTime)) { + // timestamp is a qint64, signed rather than unsigned + setTimestamp(QDateTime::fromMSecsSinceEpoch(map.take("timestamp").toLongLong())); + } + else { + setTimestamp(QDateTime::fromTime_t(map.take("timestamp").toUInt())); + } } - -void Event::toVariantMap(QVariantMap &map) const { - map["type"] = static_cast(type()); - map["flags"] = static_cast(flags()); - map["timestamp"] = timestamp().toTime_t(); +void Event::toVariantMap(QVariantMap& map) const +{ + Q_ASSERT(SignalProxy::current()); + Q_ASSERT(SignalProxy::current()->targetPeer()); + + map["type"] = static_cast(type()); + map["flags"] = static_cast(flags()); + if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::LongTime)) { + // toMSecs returns a qint64, signed rather than unsigned + map["timestamp"] = timestamp().toMSecsSinceEpoch(); + } + else { + map["timestamp"] = timestamp().toTime_t(); + } } - -QVariantMap Event::toVariantMap() const { - QVariantMap map; - toVariantMap(map); - return map; +QVariantMap Event::toVariantMap() const +{ + QVariantMap map; + toVariantMap(map); + return map; } - -Event *Event::fromVariantMap(QVariantMap &map, Network *network) { - int inttype = map.take("type").toInt(); - // sanity check if we have a valid enum value - if(EventManager::enumName(inttype).isEmpty()) { - qWarning() << "Received a serialized event with unknown type" << inttype; - return 0; - } - - EventManager::EventType type = static_cast(inttype); - if(type == EventManager::Invalid || type == EventManager::GenericEvent) - return 0; - - EventManager::EventType group = static_cast(type & EventManager::EventGroupMask); - - Event *e = 0; - - // we use static create() functions to keep group-specific special cases in the files they belong - // e.g. IrcEventRawMessage - switch(group) { - case EventManager::NetworkEvent: - e = NetworkEvent::create(type, map, network); - break; - case EventManager::IrcServerEvent: - // not in use! - break; - case EventManager::IrcEvent: - e = IrcEvent::create(type, map, network); - break; - case EventManager::MessageEvent: - e = MessageEvent::create(type, map, network); - break; - case EventManager::CtcpEvent: - e = CtcpEvent::create(type, map, network); - break; - default: - break; - } - - if(!e) { - qWarning() << "Can't create event of type" << type; - return 0; - } - - if(!map.isEmpty()) { - qWarning() << "Event creation from map did not consume all data:" << map; - } - - return e; +Event* Event::fromVariantMap(QVariantMap& map, Network* network) +{ + int inttype = map.take("type").toInt(); + // sanity check if we have a valid enum value + if (EventManager::enumName(inttype).isEmpty()) { + qWarning() << "Received a serialized event with unknown type" << inttype; + return nullptr; + } + + auto type = static_cast(inttype); + if (type == EventManager::Invalid || type == EventManager::GenericEvent) + return nullptr; + + auto group = static_cast(type & EventManager::EventGroupMask); + + Event* e = nullptr; + + // we use static create() functions to keep group-specific special cases in the files they belong + // e.g. IrcEventRawMessage + switch (group) { + case EventManager::NetworkEvent: + e = NetworkEvent::create(type, map, network); + break; + case EventManager::IrcServerEvent: + // not in use! + break; + case EventManager::IrcEvent: + e = IrcEvent::create(type, map, network); + break; + case EventManager::MessageEvent: + e = MessageEvent::create(type, map, network); + break; + case EventManager::CtcpEvent: + e = CtcpEvent::create(type, map, network); + break; + default: + break; + } + + if (!e) { + qWarning() << "Can't create event of type" << type; + return nullptr; + } + + if (!map.isEmpty()) { + qWarning() << "Event creation from map did not consume all data:" << map; + } + + return e; } - -QDebug operator<<(QDebug dbg, Event *e) { - dbg.nospace() << qPrintable(e->className()) << "(" - << "type = 0x" << qPrintable(QString::number(e->type(), 16)); - e->debugInfo(dbg); - //<< ", data = " << e->data(); // we don't use data anywhere yet - dbg.nospace() << ", flags = 0x" << qPrintable(QString::number(e->flags(), 16)) - << ")"; - return dbg.space(); +QDebug operator<<(QDebug dbg, Event* e) +{ + dbg.nospace() << qPrintable(e->className()) << "(" + << "type = 0x" << qPrintable(QString::number(e->type(), 16)); + e->debugInfo(dbg); + //<< ", data = " << e->data(); // we don't use data anywhere yet + dbg.nospace() << ", flags = 0x" << qPrintable(QString::number(e->flags(), 16)) << ")"; + return dbg.space(); } -