Add a flag to enable Qt deprecation warnings on Qt < 5.13
[quassel.git] / src / common / internalpeer.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 "internalpeer.h"
22
23 #include "util.h"
24
25 using namespace Protocol;
26
27 InternalPeer::InternalPeer(QObject* parent)
28     : Peer(nullptr, parent)
29 {
30     static bool registered = []() {
31         qRegisterMetaType<QPointer<InternalPeer>>();
32         qRegisterMetaType<Protocol::SyncMessage>();
33         qRegisterMetaType<Protocol::RpcCall>();
34         qRegisterMetaType<Protocol::InitRequest>();
35         qRegisterMetaType<Protocol::InitData>();
36         return true;
37     }();
38     Q_UNUSED(registered)
39
40     setFeatures(Quassel::Features{});
41 }
42
43 InternalPeer::~InternalPeer()
44 {
45     if (_isOpen) {
46         emit disconnected();
47     }
48 }
49
50 QString InternalPeer::description() const
51 {
52     return tr("internal connection");
53 }
54
55 QString InternalPeer::address() const
56 {
57     return tr("internal connection");
58 }
59
60 quint16 InternalPeer::port() const
61 {
62     return 0;
63 }
64
65 bool InternalPeer::isOpen() const
66 {
67     return _isOpen;
68 }
69
70 bool InternalPeer::isSecure() const
71 {
72     return true;
73 }
74
75 bool InternalPeer::isLocal() const
76 {
77     return true;
78 }
79
80 void InternalPeer::close(const QString& reason)
81 {
82     Q_UNUSED(reason);
83     _isOpen = false;
84 }
85
86 int InternalPeer::lag() const
87 {
88     return 0;
89 }
90
91 ::SignalProxy* InternalPeer::signalProxy() const
92 {
93     return _proxy;
94 }
95
96 void InternalPeer::setSignalProxy(::SignalProxy* proxy)
97 {
98     if (!proxy && _proxy) {
99         _proxy = nullptr;
100         if (_isOpen) {
101             _isOpen = false;
102             emit disconnected();
103         }
104         return;
105     }
106
107     if (proxy && !_proxy) {
108         _proxy = proxy;
109         _isOpen = true;
110         return;
111     }
112
113     qWarning() << Q_FUNC_INFO << "Changing the SignalProxy is not supported!";
114 }
115
116 void InternalPeer::setPeer(InternalPeer* peer)
117 {
118     connect(peer,
119             selectOverload<const Protocol::SyncMessage&>(&InternalPeer::dispatchMessage),
120             this,
121             selectOverload<const Protocol::SyncMessage&>(&InternalPeer::handleMessage));
122     connect(peer,
123             selectOverload<const Protocol::RpcCall&>(&InternalPeer::dispatchMessage),
124             this,
125             selectOverload<const Protocol::RpcCall&>(&InternalPeer::handleMessage));
126     connect(peer,
127             selectOverload<const Protocol::InitRequest&>(&InternalPeer::dispatchMessage),
128             this,
129             selectOverload<const Protocol::InitRequest&>(&InternalPeer::handleMessage));
130     connect(peer,
131             selectOverload<const Protocol::InitData&>(&InternalPeer::dispatchMessage),
132             this,
133             selectOverload<const Protocol::InitData&>(&InternalPeer::handleMessage));
134
135     connect(peer, &Peer::disconnected, this, &InternalPeer::peerDisconnected);
136
137     _isOpen = true;
138 }
139
140 void InternalPeer::peerDisconnected()
141 {
142     disconnect(sender(), nullptr, this, nullptr);
143     if (_isOpen) {
144         _isOpen = false;
145         emit disconnected();
146     }
147 }
148
149 void InternalPeer::dispatch(const SyncMessage& msg)
150 {
151     emit dispatchMessage(msg);
152 }
153
154 void InternalPeer::dispatch(const RpcCall& msg)
155 {
156     emit dispatchMessage(msg);
157 }
158
159 void InternalPeer::dispatch(const InitRequest& msg)
160 {
161     emit dispatchMessage(msg);
162 }
163
164 void InternalPeer::dispatch(const InitData& msg)
165 {
166     emit dispatchMessage(msg);
167 }
168
169 void InternalPeer::handleMessage(const Protocol::SyncMessage& msg)
170 {
171     handle(msg);
172 }
173
174 void InternalPeer::handleMessage(const Protocol::RpcCall& msg)
175 {
176     handle(msg);
177 }
178
179 void InternalPeer::handleMessage(const Protocol::InitRequest& msg)
180 {
181     handle(msg);
182 }
183
184 void InternalPeer::handleMessage(const Protocol::InitData& msg)
185 {
186     handle(msg);
187 }
188
189 template<class T>
190 void InternalPeer::handle(const T& msg)
191 {
192     static auto setSourcePeer = [](Peer* peer) {
193         auto p = SignalProxy::current();
194         if (p) {
195             p->setSourcePeer(peer);
196         }
197     };
198
199     setSourcePeer(this);
200     Peer::handle(msg);
201     setSourcePeer(nullptr);
202 }