2ef7d1a902b1633c79fe3dd9df97c3f51be461ef
[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();
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 connectionWarnings(const QStringList &warnings);
84   void connectionMsg(const QString &msg);
85   void disconnected();
86
87   void progressRangeChanged(int minimum, int maximum);
88   void progressValueChanged(int value);
89   void progressTextChanged(const QString &);
90
91   void startCoreSetup(const QVariantList &);
92   void startInternalCore();
93   void connectToInternalCore(SignalProxy *proxy);
94
95   // These signals MUST be handled synchronously!
96   void userAuthenticationRequired(CoreAccount *, bool *valid, const QString &errorMessage = QString());
97   void handleNoSslInClient(bool *accepted);
98   void handleNoSslInCore(bool *accepted);
99 #ifdef HAVE_SSL
100   void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
101 #endif
102
103 private slots:
104   void connectToCurrentAccount();
105
106   void socketStateChanged(QAbstractSocket::SocketState);
107   void coreSocketError(QAbstractSocket::SocketError);
108   void coreHasData();
109   void coreSocketConnected();
110   void coreSocketDisconnected();
111
112   void clientInitAck(const QVariantMap &msg);
113
114   // for sync progress
115   void networkInitDone();
116   void checkSyncState();
117
118   void syncToCore(const QVariantMap &sessionState);
119   void internalSessionStateReceived(const QVariant &packedState);
120   void sessionStateReceived(const QVariantMap &state);
121
122   void resetConnection();
123   void connectionReady();
124   //void doCoreSetup(const QVariant &setupData);
125
126   void loginToCore(const QString &previousError = QString());
127   void loginSuccess();
128   void loginFailed(const QString &errorMessage);
129
130   void updateProgress(int value, int maximum);
131   void setProgressText(const QString &text);
132   void setProgressValue(int value);
133   void setProgressMinimum(int minimum);
134   void setProgressMaximum(int maximum);
135
136   void setState(QAbstractSocket::SocketState socketState);
137   void setState(ConnectionState state);
138
139 #ifdef HAVE_SSL
140   void sslSocketEncrypted();
141   void sslErrors();
142 #endif
143
144 private:
145   CoreAccountModel *_model;
146   CoreAccount _account;
147   QVariantMap _coreMsgBuffer;
148
149   QPointer<QIODevice> _socket;
150   quint32 _blockSize;
151   ConnectionState _state;
152
153   QSet<Network *> _netsToSync;
154   int _numNetsToSync;
155   int _progressMinimum, _progressMaximum, _progressValue;
156   QString _progressText;
157
158   QString _coreInfoString(const QVariantMap &);
159
160   inline CoreAccountModel *accountModel() const;
161 };
162
163 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
164
165 // Inlines
166 int CoreConnection::progressMinimum() const { return _progressMinimum; }
167 int CoreConnection::progressMaximum() const { return _progressMaximum; }
168 int CoreConnection::progressValue() const { return _progressValue; }
169 QString CoreConnection::progressText() const { return _progressText; }
170
171 CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
172 bool CoreConnection::isConnected() const { return state() >= Connected; }
173 CoreAccount CoreConnection::currentAccount() const { return _account; }
174 CoreAccountModel *CoreConnection::accountModel() const { return _model; }
175
176 #ifdef HAVE_SSL
177 const QSslSocket *CoreConnection::sslSocket() const { return qobject_cast<QSslSocket *>(_socket); }
178 #endif
179
180 #endif