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