56a5a29eba702f997c078f1b4ba0d7bcb19ad620
[quassel.git] / src / client / coreconnection.h
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 #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         Disconnected,
53         Connecting,
54         Connected,
55         Synchronizing,
56         Synchronized
57     };
58
59     CoreConnection(QObject *parent = nullptr);
60
61     void init();
62
63     bool isConnected() const;
64     ConnectionState state() const;
65     CoreAccount currentAccount() const;
66
67     bool isEncrypted() const;
68     bool isLocalConnection() const;
69
70     int progressMinimum() const;
71     int progressMaximum() const;
72     int progressValue() const;
73     QString progressText() const;
74
75     //! Check if we consider the last connect as reconnect
76     bool wasReconnect() const { return _wasReconnect; }
77
78     QPointer<Peer> peer() const;
79
80 public slots:
81     bool connectToCore(AccountId = 0);
82     void reconnectToCore();
83     void disconnectFromCore();
84
85     void setupCore(const Protocol::SetupData &setupData);
86
87 signals:
88     void stateChanged(CoreConnection::ConnectionState);
89     void encrypted(bool isEncrypted = true);
90     void synchronized();
91     void lagUpdated(int msecs);
92
93     void connectionError(const QString &errorMsg);
94     void connectionErrorPopup(const QString &errorMsg);
95     void connectionMsg(const QString &msg);
96     void disconnected();
97
98     void progressRangeChanged(int minimum, int maximum);
99     void progressValueChanged(int value);
100     void progressTextChanged(const QString &);
101
102     void startCoreSetup(const QVariantList &backendInfo, const QVariantList &authenticatorInfo);
103     void coreSetupSuccess();
104     void coreSetupFailed(const QString &error);
105
106     void connectToInternalCore(QPointer<InternalPeer> connection);
107
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);
112 #ifdef HAVE_SSL
113     void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
114 #endif
115
116 private slots:
117     void connectToCurrentAccount();
118     void disconnectFromCore(const QString &errorString, bool wantReconnect = true);
119
120     void coreSocketError(QAbstractSocket::SocketError error, const QString &errorString);
121     void coreSocketDisconnected();
122
123     // for sync progress
124     void networkInitDone();
125     void checkSyncState();
126
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);
130
131     void resetConnection(bool wantReconnect = false);
132
133     void onConnectionReady();
134     void onLoginSuccessful(const CoreAccount &account);
135     void onHandshakeComplete(RemotePeer *peer, const Protocol::SessionState &sessionState);
136
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);
142
143     void setState(ConnectionState state);
144
145     void networkDetectionModeChanged(const QVariant &mode);
146     void pingTimeoutIntervalChanged(const QVariant &interval);
147     void reconnectIntervalChanged(const QVariant &interval);
148     void reconnectTimeout();
149
150     void onlineStateChanged(bool isOnline);
151
152 private:
153     QPointer<ClientAuthHandler> _authHandler;
154     QPointer<Peer> _peer;
155     ConnectionState _state;
156
157     QTimer _reconnectTimer;
158     bool _wantReconnect;
159     bool _wasReconnect;
160
161     QSet<QObject *> _netsToSync;
162     int _numNetsToSync;
163     int _progressMinimum, _progressMaximum, _progressValue;
164     QString _progressText;
165
166     bool _resetting;
167
168     CoreAccount _account;
169     CoreAccountModel *accountModel() const;
170
171     QPointer<QNetworkConfigurationManager> _qNetworkConfigurationManager;
172
173     friend class CoreConfigWizard;
174 };
175
176
177 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
178
179 // Inlines
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; }
184
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; }