From da955aadf5d5189b4f8de3eb2ed1b2f2f129cb28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Fri, 21 May 2021 16:36:57 +0200 Subject: QNetworkInformation: Behind captive portal This patch adds the API, with no supporting backends Task-number: QTBUG-93848 Change-Id: I50454717f928819e1b990df91872675e842f9987 Reviewed-by: Edward Welbourne Reviewed-by: Timur Pocheptsov --- src/network/kernel/qnetworkinformation.cpp | 36 ++++++++++++++++++++++++++++++ src/network/kernel/qnetworkinformation.h | 12 ++++++++++ src/network/kernel/qnetworkinformation_p.h | 12 ++++++++++ 3 files changed, 60 insertions(+) (limited to 'src/network/kernel') diff --git a/src/network/kernel/qnetworkinformation.cpp b/src/network/kernel/qnetworkinformation.cpp index c36b1c4698..c1e184f961 100644 --- a/src/network/kernel/qnetworkinformation.cpp +++ b/src/network/kernel/qnetworkinformation.cpp @@ -470,6 +470,21 @@ QNetworkInformationBackendFactory::~QNetworkInformationBackendFactory() \sa QNetworkInformation::reachability */ +/*! + \enum QNetworkInformation::TriState + \since 6.2 + + A bool with a 3rd, unknown, state. + + \value False + Known to be \c{false}. + \value True + Known to be \c{true}. + \value Unknown + The state cannot be determined at present, either because the query is + not supported on this platform or because the OS lacks the information. +*/ + /*! \internal ctor */ @@ -478,6 +493,9 @@ QNetworkInformation::QNetworkInformation(QNetworkInformationBackend *backend) { connect(backend, &QNetworkInformationBackend::reachabilityChanged, this, [this]() { emit reachabilityChanged(d_func()->backend->reachability()); }); + connect(backend, &QNetworkInformationBackend::behindCaptivePortalChanged, this, [this]() { + emit behindCaptivePortalChanged(d_func()->backend->behindCaptivePortal()); + }); } /*! @@ -504,6 +522,24 @@ QNetworkInformation::Reachability QNetworkInformation::reachability() const return d_func()->backend->reachability(); } +/*! + \property QNetworkInformation::behindCaptivePortal + \brief Lets you know if the user's device is behind a captive portal. + \since 6.2 + + This property indicates if the user's device is currently behind a captive + portal. This functionality relies on the operating system's detection of + captive portals and is not supported on systems that don't report this. + On systems where this is not supported this will always return + TriState::Unknown. + + \sa TriState +*/ +QNetworkInformation::TriState QNetworkInformation::behindCaptivePortal() const +{ + return d_func()->backend->behindCaptivePortal(); +} + /*! Returns the name of the currently loaded backend. */ diff --git a/src/network/kernel/qnetworkinformation.h b/src/network/kernel/qnetworkinformation.h index 07f4fe873d..6e700747bb 100644 --- a/src/network/kernel/qnetworkinformation.h +++ b/src/network/kernel/qnetworkinformation.h @@ -55,6 +55,7 @@ class Q_NETWORK_EXPORT QNetworkInformation : public QObject Q_OBJECT Q_DECLARE_PRIVATE(QNetworkInformation) Q_PROPERTY(Reachability reachability READ reachability NOTIFY reachabilityChanged) + Q_PROPERTY(TriState behindCaptivePortal READ behindCaptivePortal NOTIFY behindCaptivePortalChanged) public: enum class Reachability { Unknown, @@ -65,14 +66,24 @@ public: }; Q_ENUM(Reachability) + enum class TriState { + False, + True, + Unknown, + }; + Q_ENUM(TriState) + enum class Feature { Reachability = 0x1, + CaptivePortal = 0x2, }; Q_DECLARE_FLAGS(Features, Feature) Q_FLAG(Features) Reachability reachability() const; + TriState behindCaptivePortal() const; + QString backendName() const; bool supports(Features features) const; @@ -84,6 +95,7 @@ public: Q_SIGNALS: void reachabilityChanged(Reachability newReachability); + void behindCaptivePortalChanged(TriState state); private: friend struct QNetworkInformationDeleter; diff --git a/src/network/kernel/qnetworkinformation_p.h b/src/network/kernel/qnetworkinformation_p.h index 8cb31e6641..c71e7787a4 100644 --- a/src/network/kernel/qnetworkinformation_p.h +++ b/src/network/kernel/qnetworkinformation_p.h @@ -70,9 +70,11 @@ public: virtual QNetworkInformation::Features featuresSupported() const = 0; QNetworkInformation::Reachability reachability() const { return m_reachability; } + QNetworkInformation::TriState behindCaptivePortal() const { return m_behindCaptivePortal; } Q_SIGNALS: void reachabilityChanged(); + void behindCaptivePortalChanged(); protected: void setReachability(QNetworkInformation::Reachability reachability) @@ -83,8 +85,18 @@ protected: } } + void setBehindCaptivePortal(QNetworkInformation::TriState behindPortal) + { + if (m_behindCaptivePortal != behindPortal) { + m_behindCaptivePortal = behindPortal; + emit behindCaptivePortalChanged(); + } + } + private: QNetworkInformation::Reachability m_reachability = QNetworkInformation::Reachability::Unknown; + QNetworkInformation::TriState m_behindCaptivePortal = + QNetworkInformation::TriState::Unknown; Q_DISABLE_COPY_MOVE(QNetworkInformationBackend) friend class QNetworkInformation; -- cgit v1.2.3