f3094deb4827afd606a3850cd2f9607d4263393b
[quassel.git] / src / common / networkevent.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 "networkevent.h"
22
23 Event* NetworkEvent::create(EventManager::EventType type, QVariantMap& map, Network* network)
24 {
25     switch (type) {
26     case EventManager::NetworkIncoming:
27         return new NetworkDataEvent(type, map, network);
28
29     case EventManager::NetworkConnecting:
30     case EventManager::NetworkInitializing:
31     case EventManager::NetworkInitialized:
32     case EventManager::NetworkReconnecting:
33     case EventManager::NetworkDisconnecting:
34     case EventManager::NetworkDisconnected:
35         return new NetworkConnectionEvent(type, map, network);
36
37     case EventManager::NetworkSplitJoin:
38     case EventManager::NetworkSplitQuit:
39         return new NetworkSplitEvent(type, map, network);
40
41     default:
42         return nullptr;
43     }
44 }
45
46 NetworkEvent::NetworkEvent(EventManager::EventType type, QVariantMap& map, Network* network)
47     : Event(type, map)
48     , _network(network)
49 {}
50
51 void NetworkEvent::toVariantMap(QVariantMap& map) const
52 {
53     Event::toVariantMap(map);
54     map["network"] = networkId().toInt();
55 }
56
57 NetworkDataEvent::NetworkDataEvent(EventManager::EventType type, QVariantMap& map, Network* network)
58     : NetworkEvent(type, map, network)
59 {
60     _data = map.take("data").toByteArray();
61 }
62
63 void NetworkDataEvent::toVariantMap(QVariantMap& map) const
64 {
65     NetworkEvent::toVariantMap(map);
66     map["data"] = data();
67 }
68
69 NetworkConnectionEvent::NetworkConnectionEvent(EventManager::EventType type, QVariantMap& map, Network* network)
70     : NetworkEvent(type, map, network)
71 {
72     _state = static_cast<Network::ConnectionState>(map.take("state").toInt());  // FIXME: check enum plausibility
73 }
74
75 void NetworkConnectionEvent::toVariantMap(QVariantMap& map) const
76 {
77     NetworkEvent::toVariantMap(map);
78     map["state"] = connectionState();
79 }
80
81 NetworkSplitEvent::NetworkSplitEvent(EventManager::EventType type, QVariantMap& map, Network* network)
82     : NetworkEvent(type, map, network)
83 {
84     _channel = map.take("channel").toString();
85     _users = map.take("users").toStringList();
86     _quitMsg = map.take("quitMessage").toString();
87 }
88
89 void NetworkSplitEvent::toVariantMap(QVariantMap& map) const
90 {
91     NetworkEvent::toVariantMap(map);
92     map["channel"] = channel();
93     map["users"] = users();
94     map["quitMessage"] = quitMessage();
95 }