1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
23 #include "client-export.h"
25 #include <QNetworkConfigurationManager>
30 # include <QSslSocket>
32 # include <QTcpSocket>
35 #include "coreaccount.h"
36 #include "remotepeer.h"
39 class ClientAuthHandler;
40 class CoreAccountModel;
46 class CLIENT_EXPORT CoreConnection : public QObject
60 CoreConnection(QObject* parent = nullptr);
64 bool isConnected() const;
65 ConnectionState state() const;
66 CoreAccount currentAccount() const;
68 bool isEncrypted() const;
69 bool isLocalConnection() const;
71 int progressMinimum() const;
72 int progressMaximum() const;
73 int progressValue() const;
74 QString progressText() const;
76 //! Check if we consider the last connect as reconnect
77 bool wasReconnect() const { return _wasReconnect; }
79 QPointer<Peer> peer() const;
82 bool connectToCore(AccountId = 0);
83 void reconnectToCore();
84 void disconnectFromCore();
85 void internalSessionStateReceived(const Protocol::SessionState& sessionState);
87 void setupCore(const Protocol::SetupData& setupData);
90 void stateChanged(CoreConnection::ConnectionState);
91 void encrypted(bool isEncrypted = true);
93 void lagUpdated(int msecs);
95 void connectionError(const QString& errorMsg);
96 void connectionErrorPopup(const QString& errorMsg);
97 void connectionMsg(const QString& msg);
100 void progressRangeChanged(int minimum, int maximum);
101 void progressValueChanged(int value);
102 void progressTextChanged(const QString&);
104 void startCoreSetup(const QVariantList& backendInfo, const QVariantList& authenticatorInfo);
105 void coreSetupSuccess();
106 void coreSetupFailed(const QString& error);
108 void connectToInternalCore(QPointer<InternalPeer> connection);
110 // These signals MUST be handled synchronously!
111 void userAuthenticationRequired(CoreAccount*, bool* valid, const QString& errorMessage = QString());
112 void handleNoSslInClient(bool* accepted);
113 void handleNoSslInCore(bool* accepted);
115 void handleSslErrors(const QSslSocket* socket, bool* accepted, bool* permanently);
119 void connectToCurrentAccount();
120 void disconnectFromCore(const QString& errorString, bool wantReconnect = true);
122 void coreSocketError(QAbstractSocket::SocketError error, const QString& errorString);
123 void coreSocketDisconnected();
126 void networkInitDone();
127 void checkSyncState();
129 void loginToCore(const QString& user, const QString& password, bool remember); // for config wizard
130 void syncToCore(const Protocol::SessionState& sessionState);
132 void resetConnection(bool wantReconnect = false);
134 void onConnectionReady();
135 void onLoginSuccessful(const CoreAccount& account);
136 void onHandshakeComplete(RemotePeer* peer, const Protocol::SessionState& sessionState);
138 void updateProgress(int value, int maximum);
139 void setProgressText(const QString& text);
140 void setProgressValue(int value);
141 void setProgressMinimum(int minimum);
142 void setProgressMaximum(int maximum);
144 void setState(ConnectionState state);
146 void networkDetectionModeChanged(const QVariant& mode);
147 void pingTimeoutIntervalChanged(const QVariant& interval);
148 void reconnectIntervalChanged(const QVariant& interval);
149 void reconnectTimeout();
151 void onlineStateChanged(bool isOnline);
154 QPointer<ClientAuthHandler> _authHandler;
155 QPointer<Peer> _peer;
156 ConnectionState _state{Disconnected};
158 QTimer _reconnectTimer;
159 bool _wantReconnect{false};
160 bool _wasReconnect{false};
162 QSet<QObject*> _netsToSync;
164 int _progressMinimum{0}, _progressMaximum{-1}, _progressValue{-1};
165 QString _progressText;
167 bool _resetting{false};
169 CoreAccount _account;
170 CoreAccountModel* accountModel() const;
172 QPointer<QNetworkConfigurationManager> _qNetworkConfigurationManager;
174 friend class CoreConfigWizard;
177 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
180 inline int CoreConnection::progressMinimum() const
182 return _progressMinimum;
184 inline int CoreConnection::progressMaximum() const
186 return _progressMaximum;
188 inline int CoreConnection::progressValue() const
190 return _progressValue;
192 inline QString CoreConnection::progressText() const
194 return _progressText;
197 inline CoreConnection::ConnectionState CoreConnection::state() const
201 inline bool CoreConnection::isConnected() const
203 return state() >= Connected;
205 inline CoreAccount CoreConnection::currentAccount() const