From d2c5494c3d5412bfc1dc3e819e88a636407025fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Mon, 25 Oct 2021 20:23:24 +0200 Subject: [PATCH] QNI: Windows support for the isMetered API As a drive-by: fix some improper indentation Task-number: QTBUG-91024 Change-Id: I29e04aff3638dfb2aab9d40650c55a48baba7222 Reviewed-by: Marc Mutz Reviewed-by: Edward Welbourne --- .../qnetworklistmanagerevents.cpp | 46 ++++++++++++------- .../qnetworklistmanagerevents.h | 5 +- ...rklistmanagernetworkinformationbackend.cpp | 3 ++ 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.cpp b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.cpp index dcc1439482..65824cc262 100644 --- a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.cpp +++ b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.cpp @@ -141,10 +141,10 @@ bool QNetworkListManagerEvents::start() token = NetworkInformation::NetworkStatusChanged( [this](const winrt::Windows::Foundation::IInspectable sender) { Q_UNUSED(sender); - emit transportMediumChanged(getTransportMedium()); + emitWinRTUpdates(); }); // Emit initial state - emit transportMediumChanged(getTransportMedium()); + emitWinRTUpdates(); #endif return true; @@ -208,16 +208,14 @@ bool QNetworkListManagerEvents::checkBehindCaptivePortal() return false; } +#ifdef SUPPORTS_WINRT +namespace { +using namespace winrt::Windows::Networking::Connectivity; // NB: this isn't part of "network list manager", but sadly NLM doesn't have an // equivalent API (at least not that I've found...)! -QNetworkInformation::TransportMedium QNetworkListManagerEvents::getTransportMedium() +[[nodiscard]] +QNetworkInformation::TransportMedium getTransportMedium(const ConnectionProfile &profile) { -#ifdef SUPPORTS_WINRT - using namespace winrt::Windows::Networking::Connectivity; - ConnectionProfile profile = NetworkInformation::GetInternetConnectionProfile(); - if (profile == nullptr) - return QNetworkInformation::TransportMedium::Unknown; - if (profile.IsWwanConnectionProfile()) return QNetworkInformation::TransportMedium::Cellular; if (profile.IsWlanConnectionProfile()) @@ -233,18 +231,34 @@ QNetworkInformation::TransportMedium QNetworkListManagerEvents::getTransportMedi auto fromIanaId = [](quint32 ianaId) -> QNetworkInformation::TransportMedium { // https://www.iana.org/assignments/ianaiftype-mib/ianaiftype-mib switch (ianaId) { - case 6: - return QNetworkInformation::TransportMedium::Ethernet; - case 71: // Should be handled before entering this lambda - return QNetworkInformation::TransportMedium::WiFi; + case 6: + return QNetworkInformation::TransportMedium::Ethernet; + case 71: // Should be handled before entering this lambda + return QNetworkInformation::TransportMedium::WiFi; } return QNetworkInformation::TransportMedium::Unknown; }; return fromIanaId(adapter.IanaInterfaceType()); -#else - return QNetworkInformation::TransportMedium::Unknown; -#endif } +[[nodiscard]] bool getMetered(const ConnectionProfile &profile) +{ + ConnectionCost cost = profile.GetConnectionCost(); + NetworkCostType type = cost.NetworkCostType(); + return type == NetworkCostType::Fixed || type == NetworkCostType::Variable; +} +} // unnamed namespace + +void QNetworkListManagerEvents::emitWinRTUpdates() +{ + using namespace winrt::Windows::Networking::Connectivity; + ConnectionProfile profile = NetworkInformation::GetInternetConnectionProfile(); + if (profile == nullptr) + return; + emit transportMediumChanged(getTransportMedium(profile)); + emit isMeteredChanged(getMetered(profile)); +} +#endif + QT_END_NAMESPACE diff --git a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.h b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.h index 8618a09a84..4c4625adb5 100644 --- a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.h +++ b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.h @@ -99,14 +99,15 @@ public: signals: void connectivityChanged(NLM_CONNECTIVITY); void transportMediumChanged(QNetworkInformation::TransportMedium); + void isMeteredChanged(bool); private: - [[nodiscard]] QNetworkInformation::TransportMedium getTransportMedium(); - ComPtr networkListManager = nullptr; ComPtr connectionPoint = nullptr; #ifdef SUPPORTS_WINRT + void emitWinRTUpdates(); + winrt::event_token token; #endif diff --git a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagernetworkinformationbackend.cpp b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagernetworkinformationbackend.cpp index 3e6e4074bb..4cdc80cd5d 100644 --- a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagernetworkinformationbackend.cpp +++ b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagernetworkinformationbackend.cpp @@ -169,6 +169,9 @@ QNetworkListManagerNetworkInformationBackend::QNetworkListManagerNetworkInformat connect(managerEvents.Get(), &QNetworkListManagerEvents::transportMediumChanged, this, &QNetworkListManagerNetworkInformationBackend::setTransportMedium); + + connect(managerEvents.Get(), &QNetworkListManagerEvents::isMeteredChanged, this, + &QNetworkListManagerNetworkInformationBackend::setMetered); } QNetworkListManagerNetworkInformationBackend::~QNetworkListManagerNetworkInformationBackend()