src: Yearly copyright bump
[quassel.git] / src / client / coreconnection.h
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 #pragma once
22
23 #include "client-export.h"
24
25 #include <QNetworkConfigurationManager>
26 #include <QPointer>
27 #include <QTimer>
28
29 #ifdef HAVE_SSL
30 #    include <QSslSocket>
31 #else
32 #    include <QTcpSocket>
33 #endif
34
35 #include "coreaccount.h"
36 #include "remotepeer.h"
37 #include "types.h"
38
39 class ClientAuthHandler;
40 class CoreAccountModel;
41 class InternalPeer;
42 class Network;
43 class Peer;
44 class SignalProxy;
45
46 class CLIENT_EXPORT CoreConnection : public QObject
47 {
48     Q_OBJECT
49
50 public:
51     enum ConnectionState
52     {
53         Disconnected,
54         Connecting,
55         Connected,
56         Synchronizing,
57         Synchronized
58     };
59
60     CoreConnection(QObject* parent = nullptr);
61
62     void init();
63
64     bool isConnected() const;
65     ConnectionState state() const;
66     CoreAccount currentAccount() const;
67
68     bool isEncrypted() const;
69     bool isLocalConnection() const;
70
71     int progressMinimum() const;
72     int progressMaximum() const;
73     int progressValue() const;
74     QString progressText() const;
75
76     //! Check if we consider the last connect as reconnect
77     bool wasReconnect() const { return _wasReconnect; }
78
79     QPointer<Peer> peer() const;
80
81 public slots:
82     bool connectToCore(AccountId = 0);
83     void reconnectToCore();
84     void disconnectFromCore();
85     void internalSessionStateReceived(const Protocol::SessionState& sessionState);
86
87     void setupCore(const Protocol::SetupData& setupData);
88
89 signals:
90     void stateChanged(CoreConnection::ConnectionState);
91     void encrypted(bool isEncrypted = true);
92     void synchronized();
93     void lagUpdated(int msecs);
94
95     void connectionError(const QString& errorMsg);
96     void connectionErrorPopup(const QString& errorMsg);
97     void connectionMsg(const QString& msg);
98     void disconnected();
99
100     void progressRangeChanged(int minimum, int maximum);
101     void progressValueChanged(int value);
102     void progressTextChanged(const QString&);
103
104     void startCoreSetup(const QVariantList& backendInfo, const QVariantList& authenticatorInfo);
105     void coreSetupSuccess();
106     void coreSetupFailed(const QString& error);
107
108     void connectToInternalCore(QPointer<InternalPeer> connection);
109
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);
114 #ifdef HAVE_SSL
115     void handleSslErrors(const QSslSocket* socket, bool* accepted, bool* permanently);
116 #endif
117
118 private slots:
119     void connectToCurrentAccount();
120     void disconnectFromCore(const QString& errorString, bool wantReconnect = true);
121
122     void coreSocketError(QAbstractSocket::SocketError error, const QString& errorString);
123     void coreSocketDisconnected();
124
125     // for sync progress
126     void networkInitDone();
127     void checkSyncState();
128
129     void loginToCore(const QString& user, const QString& password, bool remember);  // for config wizard
130     void syncToCore(const Protocol::SessionState& sessionState);
131
132     void resetConnection(bool wantReconnect = false);
133
134     void onConnectionReady();
135     void onLoginSuccessful(const CoreAccount& account);
136     void onHandshakeComplete(RemotePeer* peer, const Protocol::SessionState& sessionState);
137
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);
143
144     void setState(ConnectionState state);
145
146     void networkDetectionModeChanged(const QVariant& mode);
147     void pingTimeoutIntervalChanged(const QVariant& interval);
148     void reconnectIntervalChanged(const QVariant& interval);
149     void reconnectTimeout();
150
151     void onlineStateChanged(bool isOnline);
152
153 private:
154     QPointer<ClientAuthHandler> _authHandler;
155     QPointer<Peer> _peer;
156     ConnectionState _state{Disconnected};
157
158     QTimer _reconnectTimer;
159     bool _wantReconnect{false};
160     bool _wasReconnect{false};
161
162     QSet<QObject*> _netsToSync;
163     int _numNetsToSync;
164     int _progressMinimum{0}, _progressMaximum{-1}, _progressValue{-1};
165     QString _progressText;
166
167     bool _resetting{false};
168
169     CoreAccount _account;
170     CoreAccountModel* accountModel() const;
171
172     QPointer<QNetworkConfigurationManager> _qNetworkConfigurationManager;
173
174     friend class CoreConfigWizard;
175 };
176
177 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
178
179 // Inlines
180 inline int CoreConnection::progressMinimum() const
181 {
182     return _progressMinimum;
183 }
184 inline int CoreConnection::progressMaximum() const
185 {
186     return _progressMaximum;
187 }
188 inline int CoreConnection::progressValue() const
189 {
190     return _progressValue;
191 }
192 inline QString CoreConnection::progressText() const
193 {
194     return _progressText;
195 }
196
197 inline CoreConnection::ConnectionState CoreConnection::state() const
198 {
199     return _state;
200 }
201 inline bool CoreConnection::isConnected() const
202 {
203     return state() >= Connected;
204 }
205 inline CoreAccount CoreConnection::currentAccount() const
206 {
207     return _account;
208 }