Semi-yearly copyright bump
[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 startInternalCore();
105     void connectToInternalCore(InternalPeer *connection);
106
107     // These signals MUST be handled synchronously!
108     void userAuthenticationRequired(CoreAccount *, bool *valid, const QString &errorMessage = QString());
109     void handleNoSslInClient(bool *accepted);
110     void handleNoSslInCore(bool *accepted);
111 #ifdef HAVE_SSL
112     void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
113 #endif
114
115 private slots:
116     void connectToCurrentAccount();
117     void disconnectFromCore(const QString &errorString, bool wantReconnect = true);
118
119     void coreSocketError(QAbstractSocket::SocketError error, const QString &errorString);
120     void coreSocketDisconnected();
121
122     // for sync progress
123     void networkInitDone();
124     void checkSyncState();
125
126     void loginToCore(const QString &user, const QString &password, bool remember); // for config wizard
127     void syncToCore(const Protocol::SessionState &sessionState);
128     void internalSessionStateReceived(const Protocol::SessionState &sessionState);
129
130     void resetConnection(bool wantReconnect = false);
131
132     void onConnectionReady();
133     void onLoginSuccessful(const CoreAccount &account);
134     void onHandshakeComplete(RemotePeer *peer, const Protocol::SessionState &sessionState);
135
136     void updateProgress(int value, int maximum);
137     void setProgressText(const QString &text);
138     void setProgressValue(int value);
139     void setProgressMinimum(int minimum);
140     void setProgressMaximum(int maximum);
141
142     void setState(ConnectionState state);
143
144     void networkDetectionModeChanged(const QVariant &mode);
145     void pingTimeoutIntervalChanged(const QVariant &interval);
146     void reconnectIntervalChanged(const QVariant &interval);
147     void reconnectTimeout();
148
149     void onlineStateChanged(bool isOnline);
150
151 private:
152     QPointer<ClientAuthHandler> _authHandler;
153     QPointer<Peer> _peer;
154     ConnectionState _state;
155
156     QTimer _reconnectTimer;
157     bool _wantReconnect;
158     bool _wasReconnect;
159
160     QSet<QObject *> _netsToSync;
161     int _numNetsToSync;
162     int _progressMinimum, _progressMaximum, _progressValue;
163     QString _progressText;
164
165     bool _resetting;
166
167     CoreAccount _account;
168     CoreAccountModel *accountModel() const;
169
170     QPointer<QNetworkConfigurationManager> _qNetworkConfigurationManager;
171
172     friend class CoreConfigWizard;
173 };
174
175
176 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
177
178 // Inlines
179 inline int CoreConnection::progressMinimum() const { return _progressMinimum; }
180 inline int CoreConnection::progressMaximum() const { return _progressMaximum; }
181 inline int CoreConnection::progressValue() const { return _progressValue; }
182 inline QString CoreConnection::progressText() const { return _progressText; }
183
184 inline CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
185 inline bool CoreConnection::isConnected() const { return state() >= Connected; }
186 inline CoreAccount CoreConnection::currentAccount() const { return _account; }