uisupport: Provide helpers for dealing with widget changes
[quassel.git] / src / common / event.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "ctcpevent.h"
22 #include "ircevent.h"
23 #include "networkevent.h"
24 #include "messageevent.h"
25 #include "peer.h"
26 #include "signalproxy.h"
27
28 Event::Event(EventManager::EventType type)
29     : _type(type)
30 {
31 }
32
33
34 Event::Event(EventManager::EventType type, QVariantMap &map)
35     : _type(type)
36 {
37     if (!map.contains("flags") || !map.contains("timestamp")) {
38         qWarning() << "Received invalid serialized event:" << map;
39         setValid(false);
40         return;
41     }
42
43     Q_ASSERT(SignalProxy::current());
44     Q_ASSERT(SignalProxy::current()->sourcePeer());
45
46     setFlags(static_cast<EventManager::EventFlags>(map.take("flags").toInt())); // TODO sanity check?
47
48     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::LongTime)) {
49         // timestamp is a qint64, signed rather than unsigned
50         setTimestamp(QDateTime::fromMSecsSinceEpoch(map.take("timestamp").toLongLong()));
51     } else {
52         setTimestamp(QDateTime::fromTime_t(map.take("timestamp").toUInt()));
53     }
54 }
55
56
57 void Event::toVariantMap(QVariantMap &map) const
58 {
59     Q_ASSERT(SignalProxy::current());
60     Q_ASSERT(SignalProxy::current()->targetPeer());
61
62     map["type"] = static_cast<int>(type());
63     map["flags"] = static_cast<int>(flags());
64     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::LongTime)) {
65         // toMSecs returns a qint64, signed rather than unsigned
66         map["timestamp"] = timestamp().toMSecsSinceEpoch();
67     } else {
68         map["timestamp"] = timestamp().toTime_t();
69     }
70 }
71
72
73 QVariantMap Event::toVariantMap() const
74 {
75     QVariantMap map;
76     toVariantMap(map);
77     return map;
78 }
79
80
81 Event *Event::fromVariantMap(QVariantMap &map, Network *network)
82 {
83     int inttype = map.take("type").toInt();
84     // sanity check if we have a valid enum value
85     if (EventManager::enumName(inttype).isEmpty()) {
86         qWarning() << "Received a serialized event with unknown type" << inttype;
87         return nullptr;
88     }
89
90     auto type = static_cast<EventManager::EventType>(inttype);
91     if (type == EventManager::Invalid || type == EventManager::GenericEvent)
92         return nullptr;
93
94     auto group = static_cast<EventManager::EventType>(type & EventManager::EventGroupMask);
95
96     Event *e = nullptr;
97
98     // we use static create() functions to keep group-specific special cases in the files they belong
99     // e.g. IrcEventRawMessage
100     switch (group) {
101     case EventManager::NetworkEvent:
102         e = NetworkEvent::create(type, map, network);
103         break;
104     case EventManager::IrcServerEvent:
105         // not in use!
106         break;
107     case EventManager::IrcEvent:
108         e = IrcEvent::create(type, map, network);
109         break;
110     case EventManager::MessageEvent:
111         e = MessageEvent::create(type, map, network);
112         break;
113     case EventManager::CtcpEvent:
114         e = CtcpEvent::create(type, map, network);
115         break;
116     default:
117         break;
118     }
119
120     if (!e) {
121         qWarning() << "Can't create event of type" << type;
122         return nullptr;
123     }
124
125     if (!map.isEmpty()) {
126         qWarning() << "Event creation from map did not consume all data:" << map;
127     }
128
129     return e;
130 }
131
132
133 QDebug operator<<(QDebug dbg, Event *e)
134 {
135     dbg.nospace() << qPrintable(e->className()) << "("
136     << "type = 0x" << qPrintable(QString::number(e->type(), 16));
137     e->debugInfo(dbg);
138     //<< ", data = " << e->data(); // we don't use data anywhere yet
139     dbg.nospace() << ", flags = 0x" << qPrintable(QString::number(e->flags(), 16))
140     << ")";
141     return dbg.space();
142 }