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