QNetworkInformation: Adjustments to captive portal API

Make it return bool since the TriState was really only used signify that
the property was unsupported but there is already a separate way to
check if it's supported. More importantly there is no different set of
actions available to a user if they're in the Unknown or False state.

Because of the change to bool, we also rename the property to have an
'is'-prefix.

Change-Id: Iaaaad5ac31e663c36e00223bf5b0e719f412fc69
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
This commit is contained in:
Mårten Nordheim 2021-06-03 13:27:56 +02:00
parent c34b596119
commit 14b74af060
8 changed files with 33 additions and 65 deletions

View File

@ -470,21 +470,6 @@ QNetworkInformationBackendFactory::~QNetworkInformationBackendFactory()
\sa QNetworkInformation::reachability \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 \internal ctor
*/ */
@ -494,7 +479,7 @@ QNetworkInformation::QNetworkInformation(QNetworkInformationBackend *backend)
connect(backend, &QNetworkInformationBackend::reachabilityChanged, this, connect(backend, &QNetworkInformationBackend::reachabilityChanged, this,
[this]() { emit reachabilityChanged(d_func()->backend->reachability()); }); [this]() { emit reachabilityChanged(d_func()->backend->reachability()); });
connect(backend, &QNetworkInformationBackend::behindCaptivePortalChanged, this, [this]() { connect(backend, &QNetworkInformationBackend::behindCaptivePortalChanged, this, [this]() {
emit behindCaptivePortalChanged(d_func()->backend->behindCaptivePortal()); emit isBehindCaptivePortalChanged(d_func()->backend->behindCaptivePortal());
}); });
} }
@ -523,19 +508,17 @@ QNetworkInformation::Reachability QNetworkInformation::reachability() const
} }
/*! /*!
\property QNetworkInformation::behindCaptivePortal \property QNetworkInformation::isBehindCaptivePortal
\brief Lets you know if the user's device is behind a captive portal. \brief Lets you know if the user's device is behind a captive portal.
\since 6.2 \since 6.2
This property indicates if the user's device is currently behind a captive This property indicates if the user's device is currently known to be
portal. This functionality relies on the operating system's detection of behind a captive portal. This functionality relies on the operating system's
captive portals and is not supported on systems that don't report this. detection of captive portals and is not supported on systems that don't
On systems where this is not supported this will always return report this. On systems where this is not supported this will always return
TriState::Unknown. \c{false}.
\sa TriState
*/ */
QNetworkInformation::TriState QNetworkInformation::behindCaptivePortal() const bool QNetworkInformation::isBehindCaptivePortal() const
{ {
return d_func()->backend->behindCaptivePortal(); return d_func()->backend->behindCaptivePortal();
} }

View File

@ -55,7 +55,7 @@ class Q_NETWORK_EXPORT QNetworkInformation : public QObject
Q_OBJECT Q_OBJECT
Q_DECLARE_PRIVATE(QNetworkInformation) Q_DECLARE_PRIVATE(QNetworkInformation)
Q_PROPERTY(Reachability reachability READ reachability NOTIFY reachabilityChanged) Q_PROPERTY(Reachability reachability READ reachability NOTIFY reachabilityChanged)
Q_PROPERTY(TriState behindCaptivePortal READ behindCaptivePortal NOTIFY behindCaptivePortalChanged) Q_PROPERTY(bool isBehindCaptivePortal READ isBehindCaptivePortal NOTIFY isBehindCaptivePortalChanged)
public: public:
enum class Reachability { enum class Reachability {
Unknown, Unknown,
@ -66,13 +66,6 @@ public:
}; };
Q_ENUM(Reachability) Q_ENUM(Reachability)
enum class TriState {
False,
True,
Unknown,
};
Q_ENUM(TriState)
enum class Feature { enum class Feature {
Reachability = 0x1, Reachability = 0x1,
CaptivePortal = 0x2, CaptivePortal = 0x2,
@ -82,7 +75,7 @@ public:
Reachability reachability() const; Reachability reachability() const;
TriState behindCaptivePortal() const; bool isBehindCaptivePortal() const;
QString backendName() const; QString backendName() const;
@ -95,7 +88,7 @@ public:
Q_SIGNALS: Q_SIGNALS:
void reachabilityChanged(Reachability newReachability); void reachabilityChanged(Reachability newReachability);
void behindCaptivePortalChanged(TriState state); void isBehindCaptivePortalChanged(bool state);
private: private:
friend struct QNetworkInformationDeleter; friend struct QNetworkInformationDeleter;

View File

@ -70,7 +70,7 @@ public:
virtual QNetworkInformation::Features featuresSupported() const = 0; virtual QNetworkInformation::Features featuresSupported() const = 0;
QNetworkInformation::Reachability reachability() const { return m_reachability; } QNetworkInformation::Reachability reachability() const { return m_reachability; }
QNetworkInformation::TriState behindCaptivePortal() const { return m_behindCaptivePortal; } bool behindCaptivePortal() const { return m_behindCaptivePortal; }
Q_SIGNALS: Q_SIGNALS:
void reachabilityChanged(); void reachabilityChanged();
@ -85,7 +85,7 @@ protected:
} }
} }
void setBehindCaptivePortal(QNetworkInformation::TriState behindPortal) void setBehindCaptivePortal(bool behindPortal)
{ {
if (m_behindCaptivePortal != behindPortal) { if (m_behindCaptivePortal != behindPortal) {
m_behindCaptivePortal = behindPortal; m_behindCaptivePortal = behindPortal;
@ -95,8 +95,7 @@ protected:
private: private:
QNetworkInformation::Reachability m_reachability = QNetworkInformation::Reachability::Unknown; QNetworkInformation::Reachability m_reachability = QNetworkInformation::Reachability::Unknown;
QNetworkInformation::TriState m_behindCaptivePortal = bool m_behindCaptivePortal = false;
QNetworkInformation::TriState::Unknown;
Q_DISABLE_COPY_MOVE(QNetworkInformationBackend) Q_DISABLE_COPY_MOVE(QNetworkInformationBackend)
friend class QNetworkInformation; friend class QNetworkInformation;

View File

@ -128,10 +128,7 @@ QAndroidNetworkInformationBackend::QAndroidNetworkInformationBackend()
}); });
connect(conman, &AndroidConnectivityManager::captivePortalChanged, this, connect(conman, &AndroidConnectivityManager::captivePortalChanged, this,
[this](bool state) { &QAndroidNetworkInformationBackend::setBehindCaptivePortal);
using TriState = QNetworkInformation::TriState;
setBehindCaptivePortal(state ? TriState::True : TriState::False);
});
} }
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -365,10 +365,7 @@ void QNetworkListManagerNetworkInformationBackend::setConnectivity(NLM_CONNECTIV
void QNetworkListManagerNetworkInformationBackend::checkCaptivePortal() void QNetworkListManagerNetworkInformationBackend::checkCaptivePortal()
{ {
const bool behindPortal = managerEvents->checkBehindCaptivePortal(); setBehindCaptivePortal(managerEvents->checkBehindCaptivePortal());
using TriState = QNetworkInformation::TriState;
const auto triState = behindPortal ? TriState::True : TriState::False;
setBehindCaptivePortal(triState);
} }
bool QNetworkListManagerNetworkInformationBackend::event(QEvent *event) bool QNetworkListManagerNetworkInformationBackend::event(QEvent *event)

View File

@ -144,16 +144,15 @@ QNetworkManagerNetworkInformationBackend::QNetworkManagerNetworkInformationBacke
}); });
using ConnectivityState = QNetworkManagerInterface::NMConnectivityState; using ConnectivityState = QNetworkManagerInterface::NMConnectivityState;
using TriState = QNetworkInformation::TriState;
const auto connectivityState = iface.connectivityState(); const auto connectivityState = iface.connectivityState();
const bool behindPortal = (connectivityState == ConnectivityState::NM_CONNECTIVITY_PORTAL); const bool behindPortal = (connectivityState == ConnectivityState::NM_CONNECTIVITY_PORTAL);
setBehindCaptivePortal(behindPortal ? TriState::True : TriState::False); setBehindCaptivePortal(behindPortal);
connect(&iface, &QNetworkManagerInterface::connectivityChanged, this, connect(&iface, &QNetworkManagerInterface::connectivityChanged, this,
[this](ConnectivityState state) { [this](ConnectivityState state) {
const bool behindPortal = (state == ConnectivityState::NM_CONNECTIVITY_PORTAL); const bool behindPortal = (state == ConnectivityState::NM_CONNECTIVITY_PORTAL);
setBehindCaptivePortal(behindPortal ? TriState::True : TriState::False); setBehindCaptivePortal(behindPortal);
}); });
} }

View File

@ -57,7 +57,7 @@ public:
Q_ASSERT(!instance); Q_ASSERT(!instance);
instance = this; instance = this;
setReachability(QNetworkInformation::Reachability::Online); setReachability(QNetworkInformation::Reachability::Online);
setNewBehindCaptivePortal(QNetworkInformation::TriState::False); setNewBehindCaptivePortal(false);
} }
~MockBackend() { instance = nullptr; } ~MockBackend() { instance = nullptr; }
@ -74,7 +74,7 @@ public:
instance->setReachability(value); instance->setReachability(value);
} }
static void setNewBehindCaptivePortal(QNetworkInformation::TriState value) static void setNewBehindCaptivePortal(bool value)
{ {
Q_ASSERT(instance); Q_ASSERT(instance);
instance->setBehindCaptivePortal(value); instance->setBehindCaptivePortal(value);
@ -164,25 +164,25 @@ void tst_QNetworkInformation::reachability()
void tst_QNetworkInformation::behindCaptivePortal() void tst_QNetworkInformation::behindCaptivePortal()
{ {
auto info = QNetworkInformation::instance(); auto info = QNetworkInformation::instance();
QNetworkInformation::TriState behindPortal = QNetworkInformation::TriState::Unknown; bool behindPortal = false;
bool signalEmitted = false; bool signalEmitted = false;
connect(info, &QNetworkInformation::behindCaptivePortalChanged, this, connect(info, &QNetworkInformation::isBehindCaptivePortalChanged, this,
[&, info](QNetworkInformation::TriState state) { [&, info](bool state) {
signalEmitted = true; signalEmitted = true;
QCOMPARE(state, info->behindCaptivePortal()); QCOMPARE(state, info->isBehindCaptivePortal());
behindPortal = info->behindCaptivePortal(); behindPortal = info->isBehindCaptivePortal();
}); });
QCOMPARE(info->behindCaptivePortal(), QNetworkInformation::TriState::False); QVERIFY(!info->isBehindCaptivePortal());
MockBackend::setNewBehindCaptivePortal(QNetworkInformation::TriState::True); MockBackend::setNewBehindCaptivePortal(true);
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QVERIFY(signalEmitted); QVERIFY(signalEmitted);
QCOMPARE(info->behindCaptivePortal(), QNetworkInformation::TriState::True); QVERIFY(info->isBehindCaptivePortal());
QCOMPARE(behindPortal, QNetworkInformation::TriState::True); QVERIFY(behindPortal);
// Set the same value again, signal should not be emitted again // Set the same value again, signal should not be emitted again
signalEmitted = false; signalEmitted = false;
MockBackend::setNewBehindCaptivePortal(QNetworkInformation::TriState::True); MockBackend::setNewBehindCaptivePortal(true);
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QVERIFY(!signalEmitted); QVERIFY(!signalEmitted);
} }

View File

@ -48,13 +48,13 @@ int main(int argc, char **argv)
qDebug() << "Updated:" << newStatus; qDebug() << "Updated:" << newStatus;
}); });
QObject::connect(info, &QNetworkInformation::behindCaptivePortalChanged, QObject::connect(info, &QNetworkInformation::isBehindCaptivePortalChanged,
[](QNetworkInformation::TriState status) { [](bool status) {
qDebug() << "Updated, behind captive portal:" << status; qDebug() << "Updated, behind captive portal:" << status;
}); });
qDebug() << "Initial reachability:" << info->reachability(); qDebug() << "Initial reachability:" << info->reachability();
qDebug() << "Behind captive portal:" << info->behindCaptivePortal(); qDebug() << "Behind captive portal:" << info->isBehindCaptivePortal();
return app.exec(); return app.exec();
} }