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