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