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