1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
2
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <QTimer>
24 #include "NetworkWatcher.h"
25 #include "ImapAccess.h"
26 #include "Model.h"
27
28 namespace Imap {
29 namespace Mailbox {
30
NetworkWatcher(ImapAccess * parent,Model * model)31 NetworkWatcher::NetworkWatcher(ImapAccess *parent, Model *model):
32 QObject(parent), m_imapAccess(parent), m_model(model), m_desiredPolicy(NETWORK_OFFLINE)
33 {
34 // No assert for m_imapAccess. The test suite doesn't create one.
35 Q_ASSERT(m_model);
36 connect(m_model, &Model::networkPolicyChanged, this, &NetworkWatcher::effectiveNetworkPolicyChanged);
37 connect(m_model, &Model::networkError, this, &NetworkWatcher::attemptReconnect);
38 connect(m_model, &Model::connectionStateChanged, this, &NetworkWatcher::handleConnectionStateChanged);
39
40 m_reconnectTimer = new QTimer(this);
41 m_reconnectTimer->setSingleShot(true);
42 m_reconnectTimer->setInterval(MIN_RECONNECT_TIMEOUT/2);
43 connect(m_reconnectTimer, &QTimer::timeout, this, &NetworkWatcher::tryReconnect);
44 }
45
46 /** @short Start the reconnect attempt cycle */
attemptReconnect()47 void NetworkWatcher::attemptReconnect()
48 {
49 // Update the reconnect time-out value. Double it up to a max value of MAX_RECONNECT_TIMEOUT secs.
50 m_reconnectTimer->setInterval(qMin(MAX_RECONNECT_TIMEOUT, m_reconnectTimer->interval()*2));
51 m_reconnectTimer->start();
52 m_model->logTrace(0, Common::LogKind::LOG_OTHER, QStringLiteral("Network"),
53 tr("Attempting to reconnect in %n secs", 0, m_reconnectTimer->interval()/1000));
54 emit reconnectAttemptScheduled(m_reconnectTimer->interval());
55 }
56
resetReconnectTimer()57 void NetworkWatcher::resetReconnectTimer()
58 {
59 m_reconnectTimer->stop();
60 m_reconnectTimer->setInterval(MIN_RECONNECT_TIMEOUT/2);
61 emit resetReconnectState();
62 }
63
handleConnectionStateChanged(uint parserId,Imap::ConnectionState state)64 void NetworkWatcher::handleConnectionStateChanged(uint parserId, Imap::ConnectionState state)
65 {
66 Q_UNUSED(parserId);
67 // These states signify that the socket is in QAbstractSocket::ConnectedState and (maybe) mark success after
68 // a series of reconnect attempts. Timers for reconnection should be reset at this point.
69 if (state == CONN_STATE_CONNECTED_PRETLS_PRECAPS || state == CONN_STATE_SSL_HANDSHAKE) {
70 resetReconnectTimer();
71 }
72 }
73
74 /** @short Set model's network policy to the desiredPolicy */
tryReconnect()75 void NetworkWatcher::tryReconnect()
76 {
77 m_model->setNetworkPolicy(m_desiredPolicy);
78 }
79
80 /** @short Set the network access policy to "no access allowed" */
setNetworkOffline()81 void NetworkWatcher::setNetworkOffline()
82 {
83 resetReconnectTimer();
84 setDesiredNetworkPolicy(NETWORK_OFFLINE);
85 emit desiredNetworkPolicyChanged(NETWORK_OFFLINE);
86 }
87
88 /** @short Set the network access policy to "possible, but expensive" */
setNetworkExpensive()89 void NetworkWatcher::setNetworkExpensive()
90 {
91 if (!m_imapAccess || !m_imapAccess->isConfigured())
92 return;
93
94 resetReconnectTimer();
95 setDesiredNetworkPolicy(NETWORK_EXPENSIVE);
96 emit desiredNetworkPolicyChanged(NETWORK_EXPENSIVE);
97 }
98
99 /** @short Set the network access policy to "it's cheap to use it" */
setNetworkOnline()100 void NetworkWatcher::setNetworkOnline()
101 {
102 if (!m_imapAccess || !m_imapAccess->isConfigured())
103 return;
104
105 resetReconnectTimer();
106 setDesiredNetworkPolicy(NETWORK_ONLINE);
107 emit desiredNetworkPolicyChanged(NETWORK_ONLINE);
108 }
109
desiredNetworkPolicy() const110 NetworkPolicy NetworkWatcher::desiredNetworkPolicy() const
111 {
112 return m_desiredPolicy;
113 }
114
115 }
116 }
117