5342636f737f14f454ea858d1614ac48a1ddb810
[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
28 #ifdef HAVE_SSL
29 #  include <QSslSocket>
30 #else
31 #  include <QTcpSocket>
32 #endif
33
34 #include "coreaccount.h"
35 #include "types.h"
36
37 class CoreAccountModel;
38 class Network;
39 class SignalProxy;
40
41 class CoreConnection : public QObject {
42   Q_OBJECT
43
44 public:
45   enum ConnectionState {
46     Disconnected,
47     Connecting,
48     Connected,
49     Synchronizing,
50     Synchronized
51   };
52
53   CoreConnection(CoreAccountModel *model, QObject *parent = 0);
54
55   void init();
56
57   inline ConnectionState state() const;
58   inline bool isConnected() const;
59   inline CoreAccount currentAccount() const;
60
61   bool isEncrypted() const;
62
63   inline int progressMinimum() const;
64   inline int progressMaximum() const;
65   inline int progressValue() const;
66   inline QString progressText() const;
67
68 #ifdef HAVE_SSL
69   inline const QSslSocket *sslSocket() const;
70 #endif
71
72 public slots:
73   bool connectToCore(AccountId = 0);
74   void reconnectToCore();
75   void disconnectFromCore(const QString &errorString = QString());
76
77 signals:
78   void stateChanged(CoreConnection::ConnectionState);
79   void encrypted(bool isEncrypted = true);
80   void synchronized();
81
82   void connectionError(const QString &errorMsg);
83   void connectionErrorPopup(const QString &errorMsg);
84   void connectionWarnings(const QStringList &warnings);
85   void connectionMsg(const QString &msg);
86   void disconnected();
87
88   void progressRangeChanged(int minimum, int maximum);
89   void progressValueChanged(int value);
90   void progressTextChanged(const QString &);
91
92   void startCoreSetup(const QVariantList &);
93   void startInternalCore();
94   void connectToInternalCore(SignalProxy *proxy);
95
96   // These signals MUST be handled synchronously!
97   void userAuthenticationRequired(CoreAccount *, bool *valid, const QString &errorMessage = QString());
98   void handleNoSslInClient(bool *accepted);
99   void handleNoSslInCore(bool *accepted);
100 #ifdef HAVE_SSL
101   void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
102 #endif
103
104 private slots:
105   void connectToCurrentAccount();
106
107   void socketStateChanged(QAbstractSocket::SocketState);
108   void coreSocketError(QAbstractSocket::SocketError);
109   void coreHasData();
110   void coreSocketConnected();
111   void coreSocketDisconnected();
112
113   void clientInitAck(const QVariantMap &msg);
114
115   // for sync progress
116   void networkInitDone();
117   void checkSyncState();
118
119   void syncToCore(const QVariantMap &sessionState);
120   void internalSessionStateReceived(const QVariant &packedState);
121   void sessionStateReceived(const QVariantMap &state);
122
123   void resetConnection();
124   void connectionReady();
125   //void doCoreSetup(const QVariant &setupData);
126
127   void loginToCore(const QString &previousError = QString());
128   void loginSuccess();
129   void loginFailed(const QString &errorMessage);
130
131   void updateProgress(int value, int maximum);
132   void setProgressText(const QString &text);
133   void setProgressValue(int value);
134   void setProgressMinimum(int minimum);
135   void setProgressMaximum(int maximum);
136
137   void setState(QAbstractSocket::SocketState socketState);
138   void setState(ConnectionState state);
139
140 #ifdef HAVE_SSL
141   void sslSocketEncrypted();
142   void sslErrors();
143 #endif
144
145 private:
146   CoreAccountModel *_model;
147   CoreAccount _account;
148   QVariantMap _coreMsgBuffer;
149
150   QPointer<QIODevice> _socket;
151   quint32 _blockSize;
152   ConnectionState _state;
153
154   QSet<Network *> _netsToSync;
155   int _numNetsToSync;
156   int _progressMinimum, _progressMaximum, _progressValue;
157   QString _progressText;
158
159   QString _coreInfoString(const QVariantMap &);
160
161   inline CoreAccountModel *accountModel() const;
162 };
163
164 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
165
166 // Inlines
167 int CoreConnection::progressMinimum() const { return _progressMinimum; }
168 int CoreConnection::progressMaximum() const { return _progressMaximum; }
169 int CoreConnection::progressValue() const { return _progressValue; }
170 QString CoreConnection::progressText() const { return _progressText; }
171
172 CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
173 bool CoreConnection::isConnected() const { return state() >= Connected; }
174 CoreAccount CoreConnection::currentAccount() const { return _account; }
175 CoreAccountModel *CoreConnection::accountModel() const { return _model; }
176
177 #ifdef HAVE_SSL
178 const QSslSocket *CoreConnection::sslSocket() const { return qobject_cast<QSslSocket *>(_socket); }
179 #endif
180
181 #endif