Provide (de)serialization for all event types
[quassel.git] / src / common / networkevent.h
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 #ifndef NETWORKEVENT_H
22 #define NETWORKEVENT_H
23
24 #include <QStringList>
25 #include <QVariantList>
26
27 #include "event.h"
28 #include "network.h"
29
30 class NetworkEvent : public Event {
31
32 public:
33   explicit NetworkEvent(EventManager::EventType type, Network *network)
34     : Event(type),
35       _network(network)
36   {}
37
38   inline NetworkId networkId() const { return network()? network()->networkId() : NetworkId(); }
39   inline Network *network() const { return _network; }
40
41   static Event *create(EventManager::EventType type, QVariantMap &map, Network *network);
42
43 protected:
44   explicit NetworkEvent(EventManager::EventType type, QVariantMap &map, Network *network);
45   void toVariantMap(QVariantMap &map) const;
46
47   virtual inline QString className() const { return "NetworkEvent"; }
48   virtual inline void debugInfo(QDebug &dbg) const { dbg.nospace() << ", net = " << qPrintable(_network->networkName()); }
49
50 private:
51   Network *_network;
52 };
53
54 /*****************************************************************************/
55
56 class NetworkConnectionEvent : public NetworkEvent {
57
58 public:
59   explicit NetworkConnectionEvent(EventManager::EventType type, Network *network, Network::ConnectionState state)
60     : NetworkEvent(type, network),
61       _state(state)
62   {}
63
64   inline Network::ConnectionState connectionState() const { return _state; }
65   inline void setConnectionState(Network::ConnectionState state) { _state = state; }
66
67 protected:
68   explicit NetworkConnectionEvent(EventManager::EventType type, QVariantMap &map, Network *network);
69   void toVariantMap(QVariantMap &map) const;
70
71   virtual inline QString className() const { return "NetworkConnectionEvent"; }
72   virtual inline void debugInfo(QDebug &dbg) const {
73     NetworkEvent::debugInfo(dbg);
74     dbg.nospace() << ", state = " << qPrintable(QString::number(_state));
75   }
76
77 private:
78   Network::ConnectionState _state;
79
80   friend class NetworkEvent;
81 };
82
83 class NetworkDataEvent : public NetworkEvent {
84
85 public:
86   explicit NetworkDataEvent(EventManager::EventType type, Network *network, const QByteArray &data)
87     : NetworkEvent(type, network),
88       _data(data)
89   {}
90
91   inline QByteArray data() const { return _data; }
92   inline void setData(const QByteArray &data) { _data = data; }
93
94 protected:
95   explicit NetworkDataEvent(EventManager::EventType type, QVariantMap &map, Network *network);
96   void toVariantMap(QVariantMap &map) const;
97
98   virtual inline QString className() const { return "NetworkDataEvent"; }
99   virtual inline void debugInfo(QDebug &dbg) const {
100     NetworkEvent::debugInfo(dbg);
101     dbg.nospace() << ", data = " << data();
102   }
103
104 private:
105   QByteArray _data;
106
107   friend class NetworkEvent;
108 };
109
110 class NetworkSplitEvent : public NetworkEvent {
111
112 public:
113   explicit NetworkSplitEvent(EventManager::EventType type,
114                              Network *network,
115                              const QString &channel,
116                              const QStringList &users,
117                              const QString &quitMsg)
118     : NetworkEvent(type, network),
119       _channel(channel),
120       _users(users),
121       _quitMsg(quitMsg)
122   {}
123
124   inline QString channel() const { return _channel; }
125   inline QStringList users() const { return _users; }
126   inline QString quitMessage() const { return _quitMsg; }
127
128 protected:
129   explicit NetworkSplitEvent(EventManager::EventType type, QVariantMap &map, Network *network);
130   void toVariantMap(QVariantMap &map) const;
131
132   virtual inline QString className() const { return "NetworkSplitEvent"; }
133   virtual inline void debugInfo(QDebug &dbg) const {
134     NetworkEvent::debugInfo(dbg);
135     dbg.nospace() << ", channel = " << qPrintable(channel())
136                   << ", users = " << users()
137                   << ", quitmsg = " << quitMessage();
138   }
139
140 private:
141   QString _channel;
142   QStringList _users;
143   QString _quitMsg;
144
145   friend class NetworkEvent;
146 };
147
148
149 #endif