0019a534469f7c92b068ec1fe7975a6ef412d92b
[quassel.git] / src / client / coreconnection.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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 public slots:
77     bool connectToCore(AccountId = 0);
78     void reconnectToCore();
79     void disconnectFromCore();
80
81     void setupCore(const Protocol::SetupData &setupData);
82
83 signals:
84     void stateChanged(CoreConnection::ConnectionState);
85     void encrypted(bool isEncrypted = true);
86     void synchronized();
87     void lagUpdated(int msecs);
88
89     void connectionError(const QString &errorMsg);
90     void connectionErrorPopup(const QString &errorMsg);
91     void connectionMsg(const QString &msg);
92     void disconnected();
93
94     void progressRangeChanged(int minimum, int maximum);
95     void progressValueChanged(int value);
96     void progressTextChanged(const QString &);
97
98     void startCoreSetup(const QVariantList &backendInfo);
99     void coreSetupSuccess();
100     void coreSetupFailed(const QString &error);
101
102     void startInternalCore();
103     void connectToInternalCore(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 #ifdef HAVE_SSL
110     void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
111 #endif
112
113 private slots:
114     void connectToCurrentAccount();
115     void disconnectFromCore(const QString &errorString, bool wantReconnect = true);
116
117     void coreSocketError(QAbstractSocket::SocketError error, const QString &errorString);
118     void coreSocketDisconnected();
119
120     // for sync progress
121     void networkInitDone();
122     void checkSyncState();
123
124     void loginToCore(const QString &user, const QString &password, bool remember); // for config wizard
125     void syncToCore(const Protocol::SessionState &sessionState);
126     void internalSessionStateReceived(const Protocol::SessionState &sessionState);
127
128     void resetConnection(bool wantReconnect = false);
129
130     void onConnectionReady();
131     void onLoginSuccessful(const CoreAccount &account);
132     void onHandshakeComplete(RemotePeer *peer, const Protocol::SessionState &sessionState);
133
134     void updateProgress(int value, int maximum);
135     void setProgressText(const QString &text);
136     void setProgressValue(int value);
137     void setProgressMinimum(int minimum);
138     void setProgressMaximum(int maximum);
139
140     void setState(ConnectionState state);
141
142     void networkDetectionModeChanged(const QVariant &mode);
143     void pingTimeoutIntervalChanged(const QVariant &interval);
144     void reconnectIntervalChanged(const QVariant &interval);
145     void reconnectTimeout();
146
147     void onlineStateChanged(bool isOnline);
148
149 private:
150     QPointer<ClientAuthHandler> _authHandler;
151     QPointer<Peer> _peer;
152     ConnectionState _state;
153
154     QTimer _reconnectTimer;
155     bool _wantReconnect;
156     bool _wasReconnect;
157
158     QSet<QObject *> _netsToSync;
159     int _numNetsToSync;
160     int _progressMinimum, _progressMaximum, _progressValue;
161     QString _progressText;
162
163     bool _resetting;
164
165     CoreAccount _account;
166     CoreAccountModel *accountModel() const;
167
168     QPointer<QNetworkConfigurationManager> _qNetworkConfigurationManager;
169
170     friend class CoreConfigWizard;
171 };
172
173
174 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
175
176 // Inlines
177 inline int CoreConnection::progressMinimum() const { return _progressMinimum; }
178 inline int CoreConnection::progressMaximum() const { return _progressMaximum; }
179 inline int CoreConnection::progressValue() const { return _progressValue; }
180 inline QString CoreConnection::progressText() const { return _progressText; }
181
182 inline CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
183 inline bool CoreConnection::isConnected() const { return state() >= Connected; }
184 inline CoreAccount CoreConnection::currentAccount() const { return _account; }