1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
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.
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.
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/>.
24 #include "NetworkWatcher.h"
25 #include "ImapAccess.h"
31 NetworkWatcher::NetworkWatcher(ImapAccess
*parent
, Model
*model
):
32 QObject(parent
), m_imapAccess(parent
), m_model(model
), m_desiredPolicy(NETWORK_OFFLINE
)
34 // No assert for m_imapAccess. The test suite doesn't create one.
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
);
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
);
46 NetworkPolicy
NetworkWatcher::effectiveNetworkPolicy() const
48 return m_model
->networkPolicy();
51 /** @short Start the reconnect attempt cycle */
52 void NetworkWatcher::attemptReconnect()
54 // Update the reconnect time-out value. Double it up to a max value of MAX_RECONNECT_TIMEOUT secs.
55 m_reconnectTimer
->setInterval(qMin(MAX_RECONNECT_TIMEOUT
, m_reconnectTimer
->interval()*2));
56 m_reconnectTimer
->start();
57 m_model
->logTrace(0, Common::LogKind::LOG_OTHER
, QStringLiteral("Network"),
58 tr("Attempting to reconnect in %n secs", 0, m_reconnectTimer
->interval()/1000));
59 emit
reconnectAttemptScheduled(m_reconnectTimer
->interval());
62 void NetworkWatcher::resetReconnectTimer()
64 m_reconnectTimer
->stop();
65 m_reconnectTimer
->setInterval(MIN_RECONNECT_TIMEOUT
/2);
66 emit
resetReconnectState();
69 void NetworkWatcher::handleConnectionStateChanged(uint parserId
, Imap::ConnectionState state
)
72 // Only treat a real, 100%-successful connection as a succeeding one. This is because
73 // some of our socket backends (QProcess, for example) do not really have any possibility of signaling
74 // "hey, network is available" to the other side.
76 // How come? Because "a process has started" could very well mean "yeha, SSH has launched, but we
77 // don't know anything about the net yet". We *could* hack the QProcess backend to wait for a first byte
78 // and signal "ok, connected" only afterwards, but that sounds a bit ugly because it uses "magic" knowledge
79 // that in case IMAP, a server always greets us with something, which would be a bit layer-violating.
80 // Not that the current enum values of the ConnectionState are free of IMAP idioms, but...
81 if (state
>= CONN_STATE_AUTHENTICATED
&& state
< CONN_STATE_LOGOUT
) {
82 resetReconnectTimer();
86 /** @short Set model's network policy to the desiredPolicy */
87 void NetworkWatcher::tryReconnect()
89 m_model
->setNetworkPolicy(m_desiredPolicy
);
92 /** @short Set the network access policy to "no access allowed" */
93 void NetworkWatcher::setNetworkOffline()
95 resetReconnectTimer();
96 setDesiredNetworkPolicy(NETWORK_OFFLINE
);
97 emit
desiredNetworkPolicyChanged(NETWORK_OFFLINE
);
100 /** @short Set the network access policy to "possible, but expensive" */
101 void NetworkWatcher::setNetworkExpensive()
103 if (!m_imapAccess
|| !m_imapAccess
->isConfigured())
106 resetReconnectTimer();
107 setDesiredNetworkPolicy(NETWORK_EXPENSIVE
);
108 emit
desiredNetworkPolicyChanged(NETWORK_EXPENSIVE
);
111 /** @short Set the network access policy to "it's cheap to use it" */
112 void NetworkWatcher::setNetworkOnline()
114 if (!m_imapAccess
|| !m_imapAccess
->isConfigured())
117 resetReconnectTimer();
118 setDesiredNetworkPolicy(NETWORK_ONLINE
);
119 emit
desiredNetworkPolicyChanged(NETWORK_ONLINE
);
122 NetworkPolicy
NetworkWatcher::desiredNetworkPolicy() const
124 return m_desiredPolicy
;