qa: Remove lots of superfluous semicolons
[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     , _valid(true)
31 {
32 }
33
34
35 Event::Event(EventManager::EventType type, QVariantMap &map)
36     : _type(type)
37     , _valid(true)
38 {
39     if (!map.contains("flags") || !map.contains("timestamp")) {
40         qWarning() << "Received invalid serialized event:" << map;
41         setValid(false);
42         return;
43     }
44
45     Q_ASSERT(SignalProxy::current());
46     Q_ASSERT(SignalProxy::current()->sourcePeer());
47
48     setFlags(static_cast<EventManager::EventFlags>(map.take("flags").toInt())); // TODO sanity check?
49
50     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::LongTime)) {
51         // timestamp is a qint64, signed rather than unsigned
52         setTimestamp(QDateTime::fromMSecsSinceEpoch(map.take("timestamp").toLongLong()));
53     } else {
54         setTimestamp(QDateTime::fromTime_t(map.take("timestamp").toUInt()));
55     }
56 }
57
58
59 void Event::toVariantMap(QVariantMap &map) const
60 {
61     Q_ASSERT(SignalProxy::current());
62     Q_ASSERT(SignalProxy::current()->targetPeer());
63
64     map["type"] = static_cast<int>(type());
65     map["flags"] = static_cast<int>(flags());
66     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::LongTime)) {
67         // toMSecs returns a qint64, signed rather than unsigned
68         map["timestamp"] = timestamp().toMSecsSinceEpoch();
69     } else {
70         map["timestamp"] = timestamp().toTime_t();
71     }
72 }
73
74
75 QVariantMap Event::toVariantMap() const
76 {
77     QVariantMap map;
78     toVariantMap(map);
79     return map;
80 }
81
82
83 Event *Event::fromVariantMap(QVariantMap &map, Network *network)
84 {
85     int inttype = map.take("type").toInt();
86     // sanity check if we have a valid enum value
87     if (EventManager::enumName(inttype).isEmpty()) {
88         qWarning() << "Received a serialized event with unknown type" << inttype;
89         return 0;
90     }
91
92     EventManager::EventType type = static_cast<EventManager::EventType>(inttype);
93     if (type == EventManager::Invalid || type == EventManager::GenericEvent)
94         return 0;
95
96     EventManager::EventType group = static_cast<EventManager::EventType>(type & EventManager::EventGroupMask);
97
98     Event *e = 0;
99
100     // we use static create() functions to keep group-specific special cases in the files they belong
101     // e.g. IrcEventRawMessage
102     switch (group) {
103     case EventManager::NetworkEvent:
104         e = NetworkEvent::create(type, map, network);
105         break;
106     case EventManager::IrcServerEvent:
107         // not in use!
108         break;
109     case EventManager::IrcEvent:
110         e = IrcEvent::create(type, map, network);
111         break;
112     case EventManager::MessageEvent:
113         e = MessageEvent::create(type, map, network);
114         break;
115     case EventManager::CtcpEvent:
116         e = CtcpEvent::create(type, map, network);
117         break;
118     default:
119         break;
120     }
121
122     if (!e) {
123         qWarning() << "Can't create event of type" << type;
124         return 0;
125     }
126
127     if (!map.isEmpty()) {
128         qWarning() << "Event creation from map did not consume all data:" << map;
129     }
130
131     return e;
132 }
133
134
135 QDebug operator<<(QDebug dbg, Event *e)
136 {
137     dbg.nospace() << qPrintable(e->className()) << "("
138     << "type = 0x" << qPrintable(QString::number(e->type(), 16));
139     e->debugInfo(dbg);
140     //<< ", data = " << e->data(); // we don't use data anywhere yet
141     dbg.nospace() << ", flags = 0x" << qPrintable(QString::number(e->flags(), 16))
142     << ")";
143     return dbg.space();
144 }