1 /***************************************************************************
2 * Copyright (C) 2009 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #ifndef CORECONNECTION_H_
22 #define CORECONNECTION_H_
24 // TODO: support system application proxy (new in Qt 4.6)
30 # include <QSslSocket>
32 # include <QTcpSocket>
36 # include <Solid/Networking>
39 #include "coreaccount.h"
42 class CoreAccountModel;
46 class CoreConnection : public QObject {
50 enum ConnectionState {
58 CoreConnection(CoreAccountModel *model, QObject *parent = 0);
62 inline bool isConnected() const;
63 inline ConnectionState state() const;
64 inline CoreAccount currentAccount() const;
66 bool isEncrypted() const;
67 bool isLocalConnection() const;
69 inline int progressMinimum() const;
70 inline int progressMaximum() const;
71 inline int progressValue() const;
72 inline QString progressText() const;
74 //! Check if we consider the last connect as reconnect
75 inline bool wasReconnect() const { return _wasReconnect; }
78 inline const QSslSocket *sslSocket() const;
82 bool connectToCore(AccountId = 0);
83 void reconnectToCore();
84 void disconnectFromCore();
87 void stateChanged(CoreConnection::ConnectionState);
88 void encrypted(bool isEncrypted = true);
90 void lagUpdated(int msecs);
92 void connectionError(const QString &errorMsg);
93 void connectionErrorPopup(const QString &errorMsg);
94 void connectionWarnings(const QStringList &warnings);
95 void connectionMsg(const QString &msg);
98 void progressRangeChanged(int minimum, int maximum);
99 void progressValueChanged(int value);
100 void progressTextChanged(const QString &);
102 void startCoreSetup(const QVariantList &);
103 void coreSetupSuccess();
104 void coreSetupFailed(const QString &error);
106 void startInternalCore();
107 void connectToInternalCore(SignalProxy *proxy);
109 // These signals MUST be handled synchronously!
110 void userAuthenticationRequired(CoreAccount *, bool *valid, const QString &errorMessage = QString());
111 void handleNoSslInClient(bool *accepted);
112 void handleNoSslInCore(bool *accepted);
114 void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
118 void connectToCurrentAccount();
119 void disconnectFromCore(const QString &errorString, bool wantReconnect = true);
121 void socketStateChanged(QAbstractSocket::SocketState);
122 void coreSocketError(QAbstractSocket::SocketError);
124 void coreSocketConnected();
125 void coreSocketDisconnected();
127 void clientInitAck(const QVariantMap &msg);
130 void networkInitDone();
131 void checkSyncState();
133 void syncToCore(const QVariantMap &sessionState);
134 void internalSessionStateReceived(const QVariant &packedState);
135 void sessionStateReceived(const QVariantMap &state);
137 void resetConnection(bool wantReconnect = false);
138 void connectionReady();
140 void loginToCore(const QString &user, const QString &password, bool remember); // for config wizard
141 void loginToCore(const QString &previousError = QString());
143 void loginFailed(const QString &errorMessage);
145 void doCoreSetup(const QVariant &setupData);
147 void updateProgress(int value, int maximum);
148 void setProgressText(const QString &text);
149 void setProgressValue(int value);
150 void setProgressMinimum(int minimum);
151 void setProgressMaximum(int maximum);
153 void setState(QAbstractSocket::SocketState socketState);
154 void setState(ConnectionState state);
157 void sslSocketEncrypted();
161 void networkDetectionModeChanged(const QVariant &mode);
162 void pingTimeoutIntervalChanged(const QVariant &interval);
163 void reconnectIntervalChanged(const QVariant &interval);
164 void reconnectTimeout();
167 void solidNetworkStatusChanged(Solid::Networking::Status status);
171 CoreAccountModel *_model;
172 CoreAccount _account;
173 QVariantMap _coreMsgBuffer;
175 QPointer<QAbstractSocket> _socket;
177 ConnectionState _state;
179 QTimer _reconnectTimer;
182 QSet<QObject *> _netsToSync;
184 int _progressMinimum, _progressMaximum, _progressValue;
185 QString _progressText;
187 QString _coreInfoString(const QVariantMap &);
189 bool _requestedDisconnect;
191 inline CoreAccountModel *accountModel() const;
193 friend class CoreConfigWizard;
196 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
199 int CoreConnection::progressMinimum() const { return _progressMinimum; }
200 int CoreConnection::progressMaximum() const { return _progressMaximum; }
201 int CoreConnection::progressValue() const { return _progressValue; }
202 QString CoreConnection::progressText() const { return _progressText; }
204 CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
205 bool CoreConnection::isConnected() const { return state() >= Connected; }
206 CoreAccount CoreConnection::currentAccount() const { return _account; }
207 CoreAccountModel *CoreConnection::accountModel() const { return _model; }
210 const QSslSocket *CoreConnection::sslSocket() const { return qobject_cast<QSslSocket *>(_socket); }