From f2210a56914c302529375699fa9855ca9b6152be Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 29 Feb 2024 13:46:42 +0100 Subject: [PATCH] Avoid warnings of network information backend showing in CLI Lazy-initialize the network information backend so warnings it possibly shows don't disturb the output of the CLI (which does not need the network information backend anyway). --- syncthingconnector/syncthingconnection.cpp | 23 +++++++++++++--------- syncthingconnector/syncthingconnection.h | 3 +++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/syncthingconnector/syncthingconnection.cpp b/syncthingconnector/syncthingconnection.cpp index 38cfcda..d8a5bb4 100644 --- a/syncthingconnector/syncthingconnection.cpp +++ b/syncthingconnector/syncthingconnection.cpp @@ -114,6 +114,9 @@ SyncthingConnection::SyncthingConnection( , m_recordFileChanges(false) , m_useDeprecatedRoutes(true) , m_pausingOnMeteredConnection(false) +#ifdef SYNCTHINGCONNECTION_SUPPORT_METERED + , m_handlingMeteredConnectionInitialized(false) +#endif { m_trafficPollTimer.setInterval(SyncthingConnectionSettings::defaultTrafficPollInterval); m_trafficPollTimer.setTimerType(Qt::VeryCoarseTimer); @@ -135,14 +138,6 @@ SyncthingConnection::SyncthingConnection( setupTestData(); #endif - // initialize handling of metered connections -#ifdef SYNCTHINGCONNECTION_SUPPORT_METERED - QNetworkInformation::loadBackendByFeatures(QNetworkInformation::Feature::Metered); - if (const auto *const networkInformation = QNetworkInformation::instance()) { - QObject::connect(networkInformation, &QNetworkInformation::isMeteredChanged, this, &SyncthingConnection::handleMeteredConnection); - } -#endif - setLoggingFlags(loggingFlags); // allow initializing the default value for m_useDeprecatedRoutes via environment variable @@ -268,7 +263,17 @@ void SyncthingConnection::disablePolling() void SyncthingConnection::setPausingOnMeteredConnection(bool pausingOnMeteredConnection) { if (m_pausingOnMeteredConnection != pausingOnMeteredConnection) { - m_pausingOnMeteredConnection = pausingOnMeteredConnection; + if ((m_pausingOnMeteredConnection = pausingOnMeteredConnection)) { + // initialize handling of metered connections +#ifdef SYNCTHINGCONNECTION_SUPPORT_METERED + if (!m_handlingMeteredConnectionInitialized) { + QNetworkInformation::loadBackendByFeatures(QNetworkInformation::Feature::Metered); + if (const auto *const networkInformation = QNetworkInformation::instance()) { + QObject::connect(networkInformation, &QNetworkInformation::isMeteredChanged, this, &SyncthingConnection::handleMeteredConnection); + } + } +#endif + } if (m_hasConfig) { handleMeteredConnection(); } diff --git a/syncthingconnector/syncthingconnection.h b/syncthingconnector/syncthingconnection.h index c03e6a6..51bd6c9 100644 --- a/syncthingconnector/syncthingconnection.h +++ b/syncthingconnector/syncthingconnection.h @@ -444,6 +444,9 @@ private: bool m_recordFileChanges; bool m_useDeprecatedRoutes; bool m_pausingOnMeteredConnection; +#ifdef SYNCTHINGCONNECTION_SUPPORT_METERED + bool m_handlingMeteredConnectionInitialized; +#endif }; /*!