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