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