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