Fix lag display
[quassel.git] / src / client / coreconnection.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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 #ifndef CORECONNECTION_H_
22 #define CORECONNECTION_H_
23
24 // TODO: support system application proxy (new in Qt 4.6)
25
26 #include "QPointer"
27 #include "QTimer"
28
29 #ifdef HAVE_SSL
30 #  include <QSslSocket>
31 #else
32 #  include <QTcpSocket>
33 #endif
34
35 #ifdef HAVE_KDE
36 #  include <Solid/Networking>
37 #endif
38
39 #include "coreaccount.h"
40 #include "remotepeer.h"
41 #include "types.h"
42
43 class CoreAccountModel;
44 class InternalPeer;
45 class Network;
46 class Peer;
47 class SignalProxy;
48
49 class CoreConnection : public QObject
50 {
51     Q_OBJECT
52
53 public:
54     enum ConnectionState {
55         Disconnected,
56         Connecting,
57         Connected,
58         Synchronizing,
59         Synchronized
60     };
61
62     CoreConnection(CoreAccountModel *model, QObject *parent = 0);
63
64     void init();
65
66     bool isConnected() const;
67     ConnectionState state() const;
68     CoreAccount currentAccount() const;
69
70     bool isEncrypted() const;
71     bool isLocalConnection() const;
72
73     int progressMinimum() const;
74     int progressMaximum() const;
75     int progressValue() const;
76     QString progressText() const;
77
78     //! Check if we consider the last connect as reconnect
79     bool wasReconnect() const { return _wasReconnect; }
80
81 #ifdef HAVE_SSL
82     const QSslSocket *sslSocket() const;
83 #endif
84
85 public slots:
86     bool connectToCore(AccountId = 0);
87     void reconnectToCore();
88     void disconnectFromCore();
89
90 signals:
91     void stateChanged(CoreConnection::ConnectionState);
92     void encrypted(bool isEncrypted = true);
93     void synchronized();
94     void lagUpdated(int msecs);
95
96     void connectionError(const QString &errorMsg);
97     void connectionErrorPopup(const QString &errorMsg);
98     void connectionWarnings(const QStringList &warnings);
99     void connectionMsg(const QString &msg);
100     void disconnected();
101
102     void progressRangeChanged(int minimum, int maximum);
103     void progressValueChanged(int value);
104     void progressTextChanged(const QString &);
105
106     void startCoreSetup(const QVariantList &);
107     void coreSetupSuccess();
108     void coreSetupFailed(const QString &error);
109
110     void startInternalCore();
111     void connectToInternalCore(InternalPeer *connection);
112
113     // These signals MUST be handled synchronously!
114     void userAuthenticationRequired(CoreAccount *, bool *valid, const QString &errorMessage = QString());
115     void handleNoSslInClient(bool *accepted);
116     void handleNoSslInCore(bool *accepted);
117 #ifdef HAVE_SSL
118     void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
119 #endif
120
121 private slots:
122     void connectToCurrentAccount();
123     void disconnectFromCore(const QString &errorString, bool wantReconnect = true);
124
125     void socketStateChanged(QAbstractSocket::SocketState);
126     void coreSocketError(QAbstractSocket::SocketError);
127     void coreHasData(const QVariant &item);
128     void coreSocketConnected();
129     void coreSocketDisconnected();
130
131     void clientInitAck(const QVariantMap &msg);
132
133     // for sync progress
134     void networkInitDone();
135     void checkSyncState();
136
137     void syncToCore(const QVariantMap &sessionState);
138     void internalSessionStateReceived(const QVariant &packedState);
139     void sessionStateReceived(const QVariantMap &state);
140
141     void resetConnection(bool wantReconnect = false);
142     void connectionReady();
143
144     void loginToCore(const QString &user, const QString &password, bool remember); // for config wizard
145     void loginToCore(const QString &previousError = QString());
146     void loginSuccess();
147     void loginFailed(const QString &errorMessage);
148
149     void doCoreSetup(const QVariant &setupData);
150
151     void updateProgress(int value, int maximum);
152     void setProgressText(const QString &text);
153     void setProgressValue(int value);
154     void setProgressMinimum(int minimum);
155     void setProgressMaximum(int maximum);
156
157     void setState(QAbstractSocket::SocketState socketState);
158     void setState(ConnectionState state);
159
160 #ifdef HAVE_SSL
161     void sslSocketEncrypted();
162     void sslErrors();
163 #endif
164
165     void networkDetectionModeChanged(const QVariant &mode);
166     void pingTimeoutIntervalChanged(const QVariant &interval);
167     void reconnectIntervalChanged(const QVariant &interval);
168     void reconnectTimeout();
169
170 #ifdef HAVE_KDE
171     void solidNetworkStatusChanged(Solid::Networking::Status status);
172 #endif
173
174 private:
175     CoreAccountModel *_model;
176     CoreAccount _account;
177     QVariantMap _coreMsgBuffer;
178
179     QPointer<QTcpSocket> _socket;
180     QPointer<Peer> _peer;
181     ConnectionState _state;
182
183     QTimer _reconnectTimer;
184     bool _wantReconnect;
185
186     QSet<QObject *> _netsToSync;
187     int _numNetsToSync;
188     int _progressMinimum, _progressMaximum, _progressValue;
189     QString _progressText;
190
191     QString _coreInfoString(const QVariantMap &);
192     bool _wasReconnect;
193     bool _requestedDisconnect;
194     bool _resetting;
195
196     inline CoreAccountModel *accountModel() const;
197
198     friend class CoreConfigWizard;
199 };
200
201
202 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
203
204 // Inlines
205 inline int CoreConnection::progressMinimum() const { return _progressMinimum; }
206 inline int CoreConnection::progressMaximum() const { return _progressMaximum; }
207 inline int CoreConnection::progressValue() const { return _progressValue; }
208 inline QString CoreConnection::progressText() const { return _progressText; }
209
210 inline CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
211 inline bool CoreConnection::isConnected() const { return state() >= Connected; }
212 inline CoreAccount CoreConnection::currentAccount() const { return _account; }
213 inline CoreAccountModel *CoreConnection::accountModel() const { return _model; }
214
215 #ifdef HAVE_SSL
216 inline const QSslSocket *CoreConnection::sslSocket() const { return qobject_cast<QSslSocket *>(_socket); }
217 #endif
218
219 #endif