1 /***************************************************************************
2 * Copyright (C) 2005-2018 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
51 enum ConnectionState {
59 CoreConnection(QObject *parent = nullptr);
63 bool isConnected() const;
64 ConnectionState state() const;
65 CoreAccount currentAccount() const;
67 bool isEncrypted() const;
68 bool isLocalConnection() const;
70 int progressMinimum() const;
71 int progressMaximum() const;
72 int progressValue() const;
73 QString progressText() const;
75 //! Check if we consider the last connect as reconnect
76 bool wasReconnect() const { return _wasReconnect; }
78 QPointer<Peer> peer() const;
81 bool connectToCore(AccountId = 0);
82 void reconnectToCore();
83 void disconnectFromCore();
85 void setupCore(const Protocol::SetupData &setupData);
88 void stateChanged(CoreConnection::ConnectionState);
89 void encrypted(bool isEncrypted = true);
91 void lagUpdated(int msecs);
93 void connectionError(const QString &errorMsg);
94 void connectionErrorPopup(const QString &errorMsg);
95 void connectionMsg(const QString &msg);
98 void progressRangeChanged(int minimum, int maximum);
99 void progressValueChanged(int value);
100 void progressTextChanged(const QString &);
102 void startCoreSetup(const QVariantList &backendInfo, const QVariantList &authenticatorInfo);
103 void coreSetupSuccess();
104 void coreSetupFailed(const QString &error);
106 void connectToInternalCore(QPointer<InternalPeer> connection);
108 // These signals MUST be handled synchronously!
109 void userAuthenticationRequired(CoreAccount *, bool *valid, const QString &errorMessage = QString());
110 void handleNoSslInClient(bool *accepted);
111 void handleNoSslInCore(bool *accepted);
113 void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
117 void connectToCurrentAccount();
118 void disconnectFromCore(const QString &errorString, bool wantReconnect = true);
120 void coreSocketError(QAbstractSocket::SocketError error, const QString &errorString);
121 void coreSocketDisconnected();
124 void networkInitDone();
125 void checkSyncState();
127 void loginToCore(const QString &user, const QString &password, bool remember); // for config wizard
128 void syncToCore(const Protocol::SessionState &sessionState);
129 void internalSessionStateReceived(const Protocol::SessionState &sessionState);
131 void resetConnection(bool wantReconnect = false);
133 void onConnectionReady();
134 void onLoginSuccessful(const CoreAccount &account);
135 void onHandshakeComplete(RemotePeer *peer, const Protocol::SessionState &sessionState);
137 void updateProgress(int value, int maximum);
138 void setProgressText(const QString &text);
139 void setProgressValue(int value);
140 void setProgressMinimum(int minimum);
141 void setProgressMaximum(int maximum);
143 void setState(ConnectionState state);
145 void networkDetectionModeChanged(const QVariant &mode);
146 void pingTimeoutIntervalChanged(const QVariant &interval);
147 void reconnectIntervalChanged(const QVariant &interval);
148 void reconnectTimeout();
150 void onlineStateChanged(bool isOnline);
153 QPointer<ClientAuthHandler> _authHandler;
154 QPointer<Peer> _peer;
155 ConnectionState _state{Disconnected};
157 QTimer _reconnectTimer;
158 bool _wantReconnect{false};
159 bool _wasReconnect{false};
161 QSet<QObject *> _netsToSync;
163 int _progressMinimum{0}, _progressMaximum{-1}, _progressValue{-1};
164 QString _progressText;
166 bool _resetting{false};
168 CoreAccount _account;
169 CoreAccountModel *accountModel() const;
171 QPointer<QNetworkConfigurationManager> _qNetworkConfigurationManager;
173 friend class CoreConfigWizard;
177 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
180 inline int CoreConnection::progressMinimum() const { return _progressMinimum; }
181 inline int CoreConnection::progressMaximum() const { return _progressMaximum; }
182 inline int CoreConnection::progressValue() const { return _progressValue; }
183 inline QString CoreConnection::progressText() const { return _progressText; }
185 inline CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
186 inline bool CoreConnection::isConnected() const { return state() >= Connected; }
187 inline CoreAccount CoreConnection::currentAccount() const { return _account; }