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
*/
/*!
\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
*/
@ -494,7 +479,7 @@ 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());
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.
\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
This property indicates if the user's device is currently known to be
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
\c{false}.
*/
QNetworkInformation::TriState QNetworkInformation::behindCaptivePortal() const
bool QNetworkInformation::isBehindCaptivePortal() const
{
return d_func()->backend->behindCaptivePortal();
}

View File

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

View File

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

View File

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

View File

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

View File

@ -144,16 +144,15 @@ QNetworkManagerNetworkInformationBackend::QNetworkManagerNetworkInformationBacke
});
using ConnectivityState = QNetworkManagerInterface::NMConnectivityState;
using TriState = QNetworkInformation::TriState;
const auto connectivityState = iface.connectivityState();
const bool behindPortal = (connectivityState == ConnectivityState::NM_CONNECTIVITY_PORTAL);
setBehindCaptivePortal(behindPortal ? TriState::True : TriState::False);
setBehindCaptivePortal(behindPortal);
connect(&iface, &QNetworkManagerInterface::connectivityChanged, this,
[this](ConnectivityState state) {
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);
instance = this;
setReachability(QNetworkInformation::Reachability::Online);
setNewBehindCaptivePortal(QNetworkInformation::TriState::False);
setNewBehindCaptivePortal(false);
}
~MockBackend() { instance = nullptr; }
@ -74,7 +74,7 @@ public:
instance->setReachability(value);
}
static void setNewBehindCaptivePortal(QNetworkInformation::TriState value)
static void setNewBehindCaptivePortal(bool value)
{
Q_ASSERT(instance);
instance->setBehindCaptivePortal(value);
@ -164,25 +164,25 @@ void tst_QNetworkInformation::reachability()
void tst_QNetworkInformation::behindCaptivePortal()
{
auto info = QNetworkInformation::instance();
QNetworkInformation::TriState behindPortal = QNetworkInformation::TriState::Unknown;
bool behindPortal = false;
bool signalEmitted = false;
connect(info, &QNetworkInformation::behindCaptivePortalChanged, this,
[&, info](QNetworkInformation::TriState state) {
connect(info, &QNetworkInformation::isBehindCaptivePortalChanged, this,
[&, info](bool state) {
signalEmitted = true;
QCOMPARE(state, info->behindCaptivePortal());
behindPortal = info->behindCaptivePortal();
QCOMPARE(state, info->isBehindCaptivePortal());
behindPortal = info->isBehindCaptivePortal();
});
QCOMPARE(info->behindCaptivePortal(), QNetworkInformation::TriState::False);
MockBackend::setNewBehindCaptivePortal(QNetworkInformation::TriState::True);
QVERIFY(!info->isBehindCaptivePortal());
MockBackend::setNewBehindCaptivePortal(true);
QCoreApplication::processEvents();
QVERIFY(signalEmitted);
QCOMPARE(info->behindCaptivePortal(), QNetworkInformation::TriState::True);
QCOMPARE(behindPortal, QNetworkInformation::TriState::True);
QVERIFY(info->isBehindCaptivePortal());
QVERIFY(behindPortal);
// Set the same value again, signal should not be emitted again
signalEmitted = false;
MockBackend::setNewBehindCaptivePortal(QNetworkInformation::TriState::True);
MockBackend::setNewBehindCaptivePortal(true);
QCoreApplication::processEvents();
QVERIFY(!signalEmitted);
}

View File

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