From 7e47315bcb23afd966332136dca72566664401dc Mon Sep 17 00:00:00 2001 From: Kevin Kofler Date: Mon, 14 Mar 2022 18:47:23 +0100 Subject: [PATCH] mmplugin: set all connections to not autoconnect on setMobileDataEnabled(false) (#182) When disabling mobile data, instead of setting the autoconnect flag to true for the current active connection, set it to false for all connections without exception. When reenabling mobile data, instead of looking for a connection with autoconnect set, look for the connection with the latest last use timestamp, set that one to autoconnect, and also connect it immediately. There should not be any connection with autoconnect enabled if mobile data is disabled, because that makes NetworkManager reconnect it after a reboot or a modem restart. The autoconnect state on the modem is apparently lost on reboot. Also change the SignalIndicator::mobileDataEnabled() getter to account for the above. Fixes #182. --- components/mmplugin/signalindicator.cpp | 56 ++++++++++++++++++------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/components/mmplugin/signalindicator.cpp b/components/mmplugin/signalindicator.cpp index cb985463..3a67c597 100644 --- a/components/mmplugin/signalindicator.cpp +++ b/components/mmplugin/signalindicator.cpp @@ -47,11 +47,30 @@ bool SignalIndicator::mobileDataSupported() const bool SignalIndicator::mobileDataEnabled() const { + // no modem -> no mobile data -> report disabled if (!m_nmModem) { return false; } - return m_nmModem->state() == NetworkManager::Device::Activated || m_nmModem->autoconnect(); + // mobile data already activated -> report enabled + if (m_nmModem->state() == NetworkManager::Device::Activated) { + return true; + } + + // autoconnect disabled on the entire modem -> report disabled + if (!m_nmModem->autoconnect()) { + return false; + } + + // at least one connection set to autoconnect -> report enabled + for (NetworkManager::Connection::Ptr con : m_nmModem->availableConnections()) { + if (con->settings()->autoconnect()) { + return true; + } + } + + // modem, but no connection, set to autoconnect -> report disabled (#182) + return false; } void SignalIndicator::setMobileDataEnabled(bool enabled) @@ -59,30 +78,37 @@ void SignalIndicator::setMobileDataEnabled(bool enabled) if (!m_nmModem) { return; } - if (!enabled) { m_nmModem->setAutoconnect(false); - - // before disconnecting, we ensure the current active connection is set to autoconnect + // we need to also set all connections to not autoconnect (#182) for (NetworkManager::Connection::Ptr con : m_nmModem->availableConnections()) { - if (con->uuid() == m_nmModem->activeConnection()->uuid()) { - con->settings()->setAutoconnect(true); - } else { - con->settings()->setAutoconnect(false); - } + con->settings()->setAutoconnect(false); + con->update(con->settings()->toMap()); } - m_nmModem->disconnectInterface().waitForFinished(); } else { m_nmModem->setAutoconnect(true); - - // activate the connection that is set to autoconnect + // activate the connection that was last used + QDateTime latestTimestamp; + NetworkManager::Connection::Ptr latestCon; for (NetworkManager::Connection::Ptr con : m_nmModem->availableConnections()) { - if (con->settings()->autoconnect()) { - NetworkManager::activateConnection(con->path(), m_nmModem->uni(), ""); - break; + QDateTime timestamp = con->settings()->timestamp(); + // if con was not used yet, skip it, otherwise: + // if we have no latestTimestamp yet, con is the latest + // otherwise, compare the timestamps + // in case of a tie, use the first connection that was found + if (!timestamp.isNull() && (latestTimestamp.isNull() || timestamp > latestTimestamp)) { + latestTimestamp = timestamp; + latestCon = con; } } + // if we found the last used connection + if (!latestCon.isNull()) { + // set it to autoconnect and connect it immediately + latestCon->settings()->setAutoconnect(true); + latestCon->update(latestCon->settings()->toMap()); + NetworkManager::activateConnection(latestCon->path(), m_nmModem->uni(), ""); + } } }