modernize: Replace most remaining old-style connects by PMF ones
[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     void internalSessionStateReceived(const Protocol::SessionState &sessionState);
85
86     void setupCore(const Protocol::SetupData &setupData);
87
88 signals:
89     void stateChanged(CoreConnection::ConnectionState);
90     void encrypted(bool isEncrypted = true);
91     void synchronized();
92     void lagUpdated(int msecs);
93
94     void connectionError(const QString &errorMsg);
95     void connectionErrorPopup(const QString &errorMsg);
96     void connectionMsg(const QString &msg);
97     void disconnected();
98
99     void progressRangeChanged(int minimum, int maximum);
100     void progressValueChanged(int value);
101     void progressTextChanged(const QString &);
102
103     void startCoreSetup(const QVariantList &backendInfo, const QVariantList &authenticatorInfo);
104     void coreSetupSuccess();
105     void coreSetupFailed(const QString &error);
106
107     void connectToInternalCore(QPointer<InternalPeer> connection);
108
109     // These signals MUST be handled synchronously!
110     void userAuthenticationRequired(CoreAccount *, bool *valid, const QString &errorMessage = QString());
111     void handleNoSslInClient(bool *accepted);
112     void handleNoSslInCore(bool *accepted);
113 #ifdef HAVE_SSL
114     void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
115 #endif
116
117 private slots:
118     void connectToCurrentAccount();
119     void disconnectFromCore(const QString &errorString, bool wantReconnect = true);
120
121     void coreSocketError(QAbstractSocket::SocketError error, const QString &errorString);
122     void coreSocketDisconnected();
123
124     // for sync progress
125     void networkInitDone();
126     void checkSyncState();
127
128     void loginToCore(const QString &user, const QString &password, bool remember); // for config wizard
129     void syncToCore(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{Disconnected};
156
157     QTimer _reconnectTimer;
158     bool _wantReconnect{false};
159     bool _wasReconnect{false};
160
161     QSet<QObject *> _netsToSync;
162     int _numNetsToSync;
163     int _progressMinimum{0}, _progressMaximum{-1}, _progressValue{-1};
164     QString _progressText;
165
166     bool _resetting{false};
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; }