3c21b797400fb955ef424f95d936684e6c530012
[quassel.git] / src / client / coreconnection.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef CORECONNECTION_H_
22 #define CORECONNECTION_H_
23
24 #include "QPointer"
25 #include "QTimer"
26
27 #ifdef HAVE_SSL
28 #  include <QSslSocket>
29 #else
30 #  include <QTcpSocket>
31 #endif
32
33 #ifdef HAVE_KDE4
34 #  include <Solid/Networking>
35 #endif
36
37 #include <QNetworkConfigurationManager>
38
39 #include "coreaccount.h"
40 #include "remotepeer.h"
41 #include "types.h"
42
43 class ClientAuthHandler;
44 class CoreAccountModel;
45 class InternalPeer;
46 class Network;
47 class Peer;
48 class SignalProxy;
49
50 class CoreConnection : public QObject
51 {
52     Q_OBJECT
53
54 public:
55     enum ConnectionState {
56         Disconnected,
57         Connecting,
58         Connected,
59         Synchronizing,
60         Synchronized
61     };
62
63     CoreConnection(QObject *parent = 0);
64
65     void init();
66
67     bool isConnected() const;
68     ConnectionState state() const;
69     CoreAccount currentAccount() const;
70
71     bool isEncrypted() const;
72     bool isLocalConnection() const;
73
74     int progressMinimum() const;
75     int progressMaximum() const;
76     int progressValue() const;
77     QString progressText() const;
78
79     //! Check if we consider the last connect as reconnect
80     bool wasReconnect() const { return _wasReconnect; }
81
82 public slots:
83     bool connectToCore(AccountId = 0);
84     void reconnectToCore();
85     void disconnectFromCore();
86
87     void setupCore(const Protocol::SetupData &setupData);
88
89 signals:
90     void stateChanged(CoreConnection::ConnectionState);
91     void encrypted(bool isEncrypted = true);
92     void synchronized();
93     void lagUpdated(int msecs);
94
95     void connectionError(const QString &errorMsg);
96     void connectionErrorPopup(const QString &errorMsg);
97     void connectionMsg(const QString &msg);
98     void disconnected();
99
100     void progressRangeChanged(int minimum, int maximum);
101     void progressValueChanged(int value);
102     void progressTextChanged(const QString &);
103
104     void startCoreSetup(const QVariantList &backendInfo);
105     void coreSetupSuccess();
106     void coreSetupFailed(const QString &error);
107
108     void startInternalCore();
109     void connectToInternalCore(InternalPeer *connection);
110
111     // These signals MUST be handled synchronously!
112     void userAuthenticationRequired(CoreAccount *, bool *valid, const QString &errorMessage = QString());
113     void handleNoSslInClient(bool *accepted);
114     void handleNoSslInCore(bool *accepted);
115 #ifdef HAVE_SSL
116     void handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently);
117 #endif
118
119 private slots:
120     void connectToCurrentAccount();
121     void disconnectFromCore(const QString &errorString, bool wantReconnect = true);
122
123     void coreSocketError(QAbstractSocket::SocketError error, const QString &errorString);
124     void coreSocketDisconnected();
125
126     // for sync progress
127     void networkInitDone();
128     void checkSyncState();
129
130     void loginToCore(const QString &user, const QString &password, bool remember); // for config wizard
131     void syncToCore(const Protocol::SessionState &sessionState);
132     void internalSessionStateReceived(const Protocol::SessionState &sessionState);
133
134     void resetConnection(bool wantReconnect = false);
135
136     void onConnectionReady();
137     void onLoginSuccessful(const CoreAccount &account);
138     void onHandshakeComplete(RemotePeer *peer, const Protocol::SessionState &sessionState);
139
140     void updateProgress(int value, int maximum);
141     void setProgressText(const QString &text);
142     void setProgressValue(int value);
143     void setProgressMinimum(int minimum);
144     void setProgressMaximum(int maximum);
145
146     void setState(ConnectionState state);
147
148     void networkDetectionModeChanged(const QVariant &mode);
149     void pingTimeoutIntervalChanged(const QVariant &interval);
150     void reconnectIntervalChanged(const QVariant &interval);
151     void reconnectTimeout();
152
153 #ifdef HAVE_KDE4
154     void solidNetworkStatusChanged(Solid::Networking::Status status);
155 #endif
156     void onlineStateChanged(bool isOnline);
157
158 private:
159     QPointer<ClientAuthHandler> _authHandler;
160     QPointer<Peer> _peer;
161     ConnectionState _state;
162
163     QTimer _reconnectTimer;
164     bool _wantReconnect;
165     bool _wasReconnect;
166
167     QSet<QObject *> _netsToSync;
168     int _numNetsToSync;
169     int _progressMinimum, _progressMaximum, _progressValue;
170     QString _progressText;
171
172     bool _resetting;
173
174     CoreAccount _account;
175     CoreAccountModel *accountModel() const;
176
177     QPointer<QNetworkConfigurationManager> _qNetworkConfigurationManager;
178
179     friend class CoreConfigWizard;
180 };
181
182
183 Q_DECLARE_METATYPE(CoreConnection::ConnectionState)
184
185 // Inlines
186 inline int CoreConnection::progressMinimum() const { return _progressMinimum; }
187 inline int CoreConnection::progressMaximum() const { return _progressMaximum; }
188 inline int CoreConnection::progressValue() const { return _progressValue; }
189 inline QString CoreConnection::progressText() const { return _progressText; }
190
191 inline CoreConnection::ConnectionState CoreConnection::state() const { return _state; }
192 inline bool CoreConnection::isConnected() const { return state() >= Connected; }
193 inline CoreAccount CoreConnection::currentAccount() const { return _account; }
194
195 #endif