From a9b6bf5817d84250f02a7559df1a4578060d6cab Mon Sep 17 00:00:00 2001 From: Sebastian Wolf Date: Sun, 10 Jul 2022 22:39:52 +0200 Subject: [PATCH] React to network configuration changes, fixes #504 --- src/fernschreiberutils.cpp | 6 +-- src/tdlibwrapper.cpp | 81 +++++++++++++++++++++++++++++++++++++- src/tdlibwrapper.h | 13 ++++++ 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/fernschreiberutils.cpp b/src/fernschreiberutils.cpp index c36aa3e..4bef03f 100644 --- a/src/fernschreiberutils.cpp +++ b/src/fernschreiberutils.cpp @@ -61,7 +61,9 @@ namespace { const QString MESSAGE_CONTENT_TYPE_VENUE("messageVenue"); } -FernschreiberUtils::FernschreiberUtils(QObject *parent) : QObject(parent) +FernschreiberUtils::FernschreiberUtils(QObject *parent) + : QObject(parent) + , manager(new QNetworkAccessManager(this)) { LOG("Initializing audio recorder..."); @@ -92,8 +94,6 @@ FernschreiberUtils::FernschreiberUtils(QObject *parent) : QObject(parent) } else { LOG("Unable to initialize geolocation!"); } - - this->manager = new QNetworkAccessManager(this); } FernschreiberUtils::~FernschreiberUtils() diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 6beeff6..eba0455 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -53,7 +53,11 @@ namespace { const QString CHAT_LIST_MAIN("chatListMain"); } -TDLibWrapper::TDLibWrapper(AppSettings *appSettings, MceInterface *mceInterface, QObject *parent) : QObject(parent), manager(new QNetworkAccessManager(this)), joinChatRequested(false) +TDLibWrapper::TDLibWrapper(AppSettings *appSettings, MceInterface *mceInterface, QObject *parent) + : QObject(parent) + , manager(new QNetworkAccessManager(this)) + , networkConfigurationManager(new QNetworkConfigurationManager(this)) + , joinChatRequested(false) { LOG("Initializing TD Lib..."); this->appSettings = appSettings; @@ -82,6 +86,8 @@ TDLibWrapper::TDLibWrapper(AppSettings *appSettings, MceInterface *mceInterface, connect(this->appSettings, SIGNAL(useOpenWithChanged()), this, SLOT(handleOpenWithChanged())); connect(this->appSettings, SIGNAL(storageOptimizerChanged()), this, SLOT(handleStorageOptimizerChanged())); + connect(networkConfigurationManager, SIGNAL(configurationChanged(QNetworkConfiguration)), this, SLOT(handleNetworkConfigurationChanged(QNetworkConfiguration))); + this->setLogVerbosityLevel(); this->setOptionInteger("notification_group_count_max", 5); } @@ -1459,6 +1465,40 @@ void TDLibWrapper::setMessageReaction(qlonglong chatId, qlonglong messageId, con this->sendRequest(requestObject); } +void TDLibWrapper::setNetworkType(NetworkType networkType) +{ + LOG("Set network type" << networkType); + + QVariantMap requestObject; + requestObject.insert(_TYPE, "setNetworkType"); + requestObject.insert(_EXTRA, "setNetworkType"); + QVariantMap networkTypeObject; + switch (networkType) { + case Mobile: + networkTypeObject.insert(_TYPE, "networkTypeMobile"); + break; + case MobileRoaming: + networkTypeObject.insert(_TYPE, "networkTypeMobileRoaming"); + break; + case None: + networkTypeObject.insert(_TYPE, "networkTypeNone"); + break; + case Other: + networkTypeObject.insert(_TYPE, "networkTypeOther"); + break; + case WiFi: + networkTypeObject.insert(_TYPE, "networkTypeWiFi"); + break; + default: + networkTypeObject.insert(_TYPE, "networkTypeOther"); + break; + } + + requestObject.insert("type", networkTypeObject); + + this->sendRequest(requestObject); +} + void TDLibWrapper::searchEmoji(const QString &queryString) { LOG("Searching emoji" << queryString); @@ -1948,6 +1988,45 @@ void TDLibWrapper::handleSponsoredMessage(qlonglong chatId, const QVariantMap &m } } + +void TDLibWrapper::handleNetworkConfigurationChanged(const QNetworkConfiguration &config) +{ + LOG("A network configuration changed: " << config.bearerTypeName() << config.state()); + LOG("Checking overall network state..."); + + bool wifiFound = false; + bool mobileFound = false; + + QList activeConfigurations = networkConfigurationManager->allConfigurations(QNetworkConfiguration::Active); + QListIterator configurationIterator(activeConfigurations); + while (configurationIterator.hasNext()) { + QNetworkConfiguration activeConfiguration = configurationIterator.next(); + if (activeConfiguration.bearerType() == QNetworkConfiguration::BearerWLAN + || activeConfiguration.bearerType() == QNetworkConfiguration::BearerEthernet) { + LOG("Active WiFi found..."); + wifiFound = true; + } + if (activeConfiguration.bearerType() == QNetworkConfiguration::Bearer2G + || activeConfiguration.bearerType() == QNetworkConfiguration::Bearer3G + || activeConfiguration.bearerType() == QNetworkConfiguration::Bearer4G + || activeConfiguration.bearerType() == QNetworkConfiguration::BearerCDMA2000 + || activeConfiguration.bearerType() == QNetworkConfiguration::BearerEVDO + || activeConfiguration.bearerType() == QNetworkConfiguration::BearerHSPA + || activeConfiguration.bearerType() == QNetworkConfiguration::BearerLTE + || activeConfiguration.bearerType() == QNetworkConfiguration::BearerWCDMA) { + LOG("Active mobile connection found..."); + mobileFound = true; + } + } + if (wifiFound) { + this->setNetworkType(NetworkType::WiFi); + } else if (mobileFound) { + this->setNetworkType(NetworkType::Mobile); + } else { + this->setNetworkType(NetworkType::None); + } +} + void TDLibWrapper::handleGetPageSourceFinished() { LOG("TDLibWrapper::handleGetPageSourceFinished"); diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 33560de..1d1ec6d 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "tdlibreceiver.h" #include "dbusadaptor.h" @@ -114,6 +115,15 @@ public: }; Q_ENUM(UserPrivacySettingRule) + enum NetworkType { + Mobile, + MobileRoaming, + None, + Other, + WiFi + }; + Q_ENUM(NetworkType) + class Group { public: Group(qlonglong id) : groupId(id) { } @@ -239,6 +249,7 @@ public: Q_INVOKABLE void getMessageAvailableReactions(qlonglong chatId, qlonglong messageId); Q_INVOKABLE void getPageSource(const QString &address); Q_INVOKABLE void setMessageReaction(qlonglong chatId, qlonglong messageId, const QString &reaction); + Q_INVOKABLE void setNetworkType(NetworkType networkType); // Others (candidates for extraction ;)) Q_INVOKABLE void searchEmoji(const QString &queryString); @@ -352,6 +363,7 @@ public slots: void handleUserPrivacySettingRules(const QVariantMap &rules); void handleUpdatedUserPrivacySettingRules(const QVariantMap &updatedRules); void handleSponsoredMessage(qlonglong chatId, const QVariantMap &message); + void handleNetworkConfigurationChanged(const QNetworkConfiguration &config); void handleGetPageSourceFinished(); @@ -366,6 +378,7 @@ private: private: void *tdLibClient; QNetworkAccessManager *manager; + QNetworkConfigurationManager *networkConfigurationManager; AppSettings *appSettings; MceInterface *mceInterface; TDLibReceiver *tdLibReceiver;