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