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