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