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