uisupport: Provide helpers for dealing with widget changes
[quassel.git] / src / common / peer.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "common-export.h"
24
25 #include <QAbstractSocket>
26 #include <QDataStream>
27 #include <QPointer>
28
29 #include "authhandler.h"
30 #include "protocol.h"
31 #include "quassel.h"
32 #include "signalproxy.h"
33
34 class COMMON_EXPORT Peer : public QObject
35 {
36     Q_OBJECT
37
38 public:
39     explicit Peer(AuthHandler *authHandler, QObject *parent = nullptr);
40
41     virtual Protocol::Type protocol() const = 0;
42     virtual QString description() const = 0;
43
44     virtual SignalProxy *signalProxy() const = 0;
45     virtual void setSignalProxy(SignalProxy *proxy) = 0;
46
47     QDateTime connectedSince() const;
48     void setConnectedSince(const QDateTime &connectedSince);
49
50     QString buildDate() const;
51     void setBuildDate(const QString &buildDate);
52
53     QString clientVersion() const;
54     void setClientVersion(const QString &clientVersion);
55
56     bool hasFeature(Quassel::Feature feature) const;
57     Quassel::Features features() const;
58     void setFeatures(Quassel::Features features);
59
60     int id() const;
61     void setId(int id);
62
63     AuthHandler *authHandler() const;
64
65     virtual bool isOpen() const = 0;
66     virtual bool isSecure() const = 0;
67     virtual bool isLocal() const = 0;
68
69     virtual int lag() const = 0;
70
71     virtual QString address() const = 0;
72     virtual quint16 port() const = 0;
73
74 public slots:
75     /* Handshake messages */
76     virtual void dispatch(const Protocol::RegisterClient &) = 0;
77     virtual void dispatch(const Protocol::ClientDenied &) = 0;
78     virtual void dispatch(const Protocol::ClientRegistered &) = 0;
79     virtual void dispatch(const Protocol::SetupData &) = 0;
80     virtual void dispatch(const Protocol::SetupFailed &) = 0;
81     virtual void dispatch(const Protocol::SetupDone &) = 0;
82     virtual void dispatch(const Protocol::Login &) = 0;
83     virtual void dispatch(const Protocol::LoginFailed &) = 0;
84     virtual void dispatch(const Protocol::LoginSuccess &) = 0;
85     virtual void dispatch(const Protocol::SessionState &) = 0;
86
87     /* Sigproxy messages */
88     virtual void dispatch(const Protocol::SyncMessage &) = 0;
89     virtual void dispatch(const Protocol::RpcCall &) = 0;
90     virtual void dispatch(const Protocol::InitRequest &) = 0;
91     virtual void dispatch(const Protocol::InitData &) = 0;
92
93     virtual void close(const QString &reason = QString()) = 0;
94
95 signals:
96     void disconnected();
97     void secureStateChanged(bool secure = true);
98     void lagUpdated(int msecs);
99
100 protected:
101     template<typename T>
102     void handle(const T &protoMessage);
103
104 private:
105     QPointer<AuthHandler> _authHandler;
106
107     QDateTime _connectedSince;
108
109     QString _buildDate;
110     QString _clientVersion;
111     Quassel::Features _features;
112
113     int _id = -1;
114 };
115
116 // We need to special-case Peer* in attached signals/slots, so typedef it for the meta type system
117 using PeerPtr = Peer *;
118 Q_DECLARE_METATYPE(PeerPtr)
119
120 QDataStream &operator<<(QDataStream &out, PeerPtr ptr);
121 QDataStream &operator>>(QDataStream &in, PeerPtr &ptr);
122
123
124 // Template method needed in the header
125 template<typename T> inline
126 void Peer::handle(const T &protoMessage)
127 {
128     switch(protoMessage.handler()) {
129         case Protocol::Handler::SignalProxy:
130             if (!signalProxy()) {
131                 qWarning() << Q_FUNC_INFO << "Cannot handle message without a SignalProxy!";
132                 return;
133             }
134             signalProxy()->handle(this, protoMessage);
135             break;
136
137         case Protocol::Handler::AuthHandler:
138             if (!authHandler()) {
139                 qWarning() << Q_FUNC_INFO << "Cannot handle auth messages without an active AuthHandler!";
140                 return;
141             }
142             authHandler()->handle(protoMessage);
143             break;
144
145         default:
146             qWarning() << Q_FUNC_INFO << "Unknown handler for protocol message!";
147             return;
148     }
149 }