Introduce (very) basic, incomplete version of CoreConnection
[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
40 class CoreConnection : public QObject {
41   Q_OBJECT
42
43 public:
44   enum ConnectionState {
45     Disconnected,
46     Connecting,
47     Connected,
48     Synchronizing,
49     Synchronized
50   };
51
52   CoreConnection(CoreAccountModel *model, QObject *parent = 0);
53
54   void init();
55   void start();
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   void connectToCore(AccountId);
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
88   // This signal MUST be handled synchronously!
89   void userAuthenticationRequired(CoreAccount *, const QString &errorMessage = QString());
90
91   void handleIgnoreWarnings(bool permanently);
92
93 private slots:
94   void socketStateChanged(QAbstractSocket::SocketState);
95   void coreSocketError(QAbstractSocket::SocketError);
96   void coreHasData();
97   void coreSocketConnected();
98   void coreSocketDisconnected();
99
100   void clientInitAck(const QVariantMap &msg);
101
102   // for sync progress
103   void networkInitDone();
104   void checkSyncState();
105
106   void syncToCore(const QVariantMap &sessionState);
107   //void internalSessionStateReceived(const QVariant &packedState);
108   void sessionStateReceived(const QVariantMap &state);
109
110   void setWarningsHandler(const char *slot);
111   void resetWarningsHandler();
112   void resetConnection();
113   void connectionReady();
114   //void doCoreSetup(const QVariant &setupData);
115
116   void loginToCore();
117   void loginSuccess();
118   void loginFailed(const QString &errorMessage);
119
120   void updateProgress(int value, int maximum);
121   void setProgressText(const QString &text);
122   void setProgressValue(int value);
123   void setProgressMinimum(int minimum);
124   void setProgressMaximum(int maximum);
125
126   void setState(QAbstractSocket::SocketState socketState);
127   void setState(ConnectionState state);
128
129 private:
130   CoreAccountModel *_model;
131   CoreAccount _account;
132   QVariantMap _coreMsgBuffer;
133
134   QPointer<QIODevice> _socket;
135   quint32 _blockSize;
136   ConnectionState _state;
137
138   QSet<Network *> _netsToSync;
139   int _numNetsToSync;
140   int _progressMinimum, _progressMaximum, _progressValue;
141   QString _progressText;
142
143   QString _coreInfoString(const QVariantMap &);
144
145   inline CoreAccountModel *accountModel() const;
146 };
147
148 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
149
150 // Inlines
151 int CoreConnection::progressMinimum() const { return _progressMinimum; }
152 int CoreConnection::progressMaximum() const { return _progressMaximum; }
153 int CoreConnection::progressValue() const { return _progressValue; }
154 QString CoreConnection::progressText() const { return _progressText; }
155
156 CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
157 bool CoreConnection::isConnected() const { return state() >= Connected; }
158 CoreAccount CoreConnection::currentAccount() const { return _account; }
159 CoreAccountModel *CoreConnection::accountModel() const { return _model; }
160
161 #endif