f1afab9f418a2bd35427fb5e7c5e98a55d514310
[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   inline int progressMinimum() const;
62   inline int progressMaximum() const;
63   inline int progressValue() const;
64   inline QString progressText() const;
65
66 public slots:
67   bool connectToCore(AccountId = 0);
68   void reconnectToCore();
69   void disconnectFromCore();
70
71 //  void useInternalCore();
72
73 signals:
74   void stateChanged(CoreConnection::ConnectionState);
75   void synchronized();
76
77   void connectionError(const QString &errorMsg);
78   void connectionWarnings(const QStringList &warnings);
79   void connectionMsg(const QString &msg);
80   void disconnected();
81
82   void progressRangeChanged(int minimum, int maximum);
83   void progressValueChanged(int value);
84   void progressTextChanged(const QString &);
85
86   void startCoreSetup(const QVariantList &);
87   void startInternalCore();
88   void connectToInternalCore(SignalProxy *proxy);
89
90   // This signal MUST be handled synchronously!
91   void userAuthenticationRequired(CoreAccount *, bool *valid, const QString &errorMessage = QString());
92
93   void handleIgnoreWarnings(bool permanently);
94
95 private slots:
96   void connectToCurrentAccount();
97
98   void socketStateChanged(QAbstractSocket::SocketState);
99   void coreSocketError(QAbstractSocket::SocketError);
100   void coreHasData();
101   void coreSocketConnected();
102   void coreSocketDisconnected();
103
104   void clientInitAck(const QVariantMap &msg);
105
106   // for sync progress
107   void networkInitDone();
108   void checkSyncState();
109
110   void syncToCore(const QVariantMap &sessionState);
111   void internalSessionStateReceived(const QVariant &packedState);
112   void sessionStateReceived(const QVariantMap &state);
113
114   void setWarningsHandler(const char *slot);
115   void resetWarningsHandler();
116   void resetConnection();
117   void connectionReady();
118   //void doCoreSetup(const QVariant &setupData);
119
120   void loginToCore(const QString &previousError = QString());
121   void loginSuccess();
122   void loginFailed(const QString &errorMessage);
123
124   void updateProgress(int value, int maximum);
125   void setProgressText(const QString &text);
126   void setProgressValue(int value);
127   void setProgressMinimum(int minimum);
128   void setProgressMaximum(int maximum);
129
130   void setState(QAbstractSocket::SocketState socketState);
131   void setState(ConnectionState state);
132
133 private:
134   CoreAccountModel *_model;
135   CoreAccount _account;
136   QVariantMap _coreMsgBuffer;
137
138   QPointer<QIODevice> _socket;
139   quint32 _blockSize;
140   ConnectionState _state;
141
142   QSet<Network *> _netsToSync;
143   int _numNetsToSync;
144   int _progressMinimum, _progressMaximum, _progressValue;
145   QString _progressText;
146
147   QString _coreInfoString(const QVariantMap &);
148
149   inline CoreAccountModel *accountModel() const;
150 };
151
152 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
153
154 // Inlines
155 int CoreConnection::progressMinimum() const { return _progressMinimum; }
156 int CoreConnection::progressMaximum() const { return _progressMaximum; }
157 int CoreConnection::progressValue() const { return _progressValue; }
158 QString CoreConnection::progressText() const { return _progressText; }
159
160 CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
161 bool CoreConnection::isConnected() const { return state() >= Connected; }
162 CoreAccount CoreConnection::currentAccount() const { return _account; }
163 CoreAccountModel *CoreConnection::accountModel() const { return _model; }
164
165 #endif