QNI: Add a convenience method for loading the default plugin

We have some official plugins, we may as well treat them as default
and give a convenient function which loads those.

Change-Id: I6251c77ac042b795bcf24b86e510e960ee4bab54
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Mårten Nordheim 2021-10-21 13:07:33 +02:00
parent 5f8e9c194e
commit bfe579613a
8 changed files with 94 additions and 18 deletions

View File

@ -603,12 +603,63 @@ QNetworkInformation::Features QNetworkInformation::supportedFeatures() const
return d_func()->backend->featuresSupported();
}
/*!
\since 6.3
Attempts to load the platform-default backend.
This platform-to-plugin mapping is as follows:
\table
\header
\li Platform
\li Plugin-name
\row
\li Windows
\li networklistmanager
\row
\li Apple (macOS/iOS)
\li scnetworkreachability
\row
\li Android
\li android
\row
\li Linux
\li networkmanager
\endtable
This function is provided for convenience where the default for a given
platform is good enough. If you are not using the default plugins you must
use one of the other load() overloads.
Returns \c true if it managed to load the backend or if it was already
loaded. Returns \c false otherwise.
\sa instance
*/
bool QNetworkInformation::load()
{
int index = -1;
#ifdef Q_OS_WIN
index = QNetworkInformationBackend::PluginNamesWindowsIndex;
#elif defined(Q_OS_DARWIN)
index = QNetworkInformationBackend::PluginNamesAppleIndex;
#elif defined(Q_OS_ANDROID)
index = QNetworkInformationBackend::PluginNamesAndroidIndex;
#elif defined(Q_OS_LINUX)
index = QNetworkInformationBackend::PluginNamesLinuxIndex;
#endif
if (index == -1)
return false;
return load(QNetworkInformationBackend::PluginNames[index]);
}
/*!
Attempts to load a backend whose name matches \a backend
(case insensitively).
Returns \c true if it managed to load the requested backend or
if it was already loaded. Returns \c false otherwise
if it was already loaded. Returns \c false otherwise.
\sa instance
*/
@ -622,7 +673,7 @@ bool QNetworkInformation::load(QStringView backend)
Load a backend which supports \a features.
Returns \c true if it managed to load the requested backend or
if it was already loaded. Returns \c false otherwise
if it was already loaded. Returns \c false otherwise.
\sa instance
*/

View File

@ -96,6 +96,7 @@ public:
bool supports(Features features) const;
Features supportedFeatures() const;
static bool load();
static bool load(QStringView backend);
static bool load(Features features);
static QStringList availableBackends();

View File

@ -67,6 +67,17 @@ class Q_NETWORK_EXPORT QNetworkInformationBackend : public QObject
using TransportMedium = QNetworkInformation::TransportMedium;
public:
static inline const char16_t PluginNames[4][22] = {
{ u"networklistmanager" },
{ u"scnetworkreachability" },
{ u"android" },
{ u"networkmanager" },
};
static constexpr int PluginNamesWindowsIndex = 0;
static constexpr int PluginNamesAppleIndex = 1;
static constexpr int PluginNamesAndroidIndex = 2;
static constexpr int PluginNamesLinuxIndex = 3;
QNetworkInformationBackend() = default;
~QNetworkInformationBackend() override;

View File

@ -47,7 +47,10 @@ QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(lcNetInfoAndroid)
Q_LOGGING_CATEGORY(lcNetInfoAndroid, "qt.network.info.android");
static const QString backendName = QStringLiteral("android");
static QString backendName() {
return QString::fromUtf16(QNetworkInformationBackend::PluginNames
[QNetworkInformationBackend::PluginNamesAndroidIndex]);
}
class QAndroidNetworkInformationBackend : public QNetworkInformationBackend
{
@ -56,7 +59,7 @@ public:
QAndroidNetworkInformationBackend();
~QAndroidNetworkInformationBackend() { m_valid = false; }
QString name() const override { return backendName; }
QString name() const override { return backendName(); }
QNetworkInformation::Features featuresSupported() const override
{
return featuresSupportedStatic();
@ -88,7 +91,7 @@ class QAndroidNetworkInformationBackendFactory : public QNetworkInformationBacke
public:
QAndroidNetworkInformationBackendFactory() = default;
~QAndroidNetworkInformationBackendFactory() = default;
QString name() const override { return backendName; }
QString name() const override { return backendName(); }
QNetworkInformation::Features featuresSupported() const override
{
return QAndroidNetworkInformationBackend::featuresSupportedStatic();

View File

@ -50,7 +50,11 @@ QT_BEGIN_NAMESPACE
// Declared in qnetworklistmanagerevents.h
Q_LOGGING_CATEGORY(lcNetInfoNLM, "qt.network.info.netlistmanager");
static const QString backendName = QStringLiteral("networklistmanager");
static QString backendName()
{
return QString::fromUtf16(QNetworkInformationBackend::PluginNames
[QNetworkInformationBackend::PluginNamesWindowsIndex]);
}
namespace {
bool testCONNECTIVITY(NLM_CONNECTIVITY connectivity, NLM_CONNECTIVITY flag)
@ -90,7 +94,7 @@ public:
QNetworkListManagerNetworkInformationBackend();
~QNetworkListManagerNetworkInformationBackend();
QString name() const override { return backendName; }
QString name() const override { return backendName(); }
QNetworkInformation::Features featuresSupported() const override
{
return featuresSupportedStatic();
@ -130,7 +134,7 @@ class QNetworkListManagerNetworkInformationBackendFactory : public QNetworkInfor
public:
QNetworkListManagerNetworkInformationBackendFactory() = default;
~QNetworkListManagerNetworkInformationBackendFactory() = default;
QString name() const override { return backendName; }
QString name() const override { return backendName(); }
QNetworkInformation::Features featuresSupported() const override
{
return QNetworkListManagerNetworkInformationBackend::featuresSupportedStatic();

View File

@ -120,7 +120,11 @@ transportMediumFromDeviceType(QNetworkManagerInterface::NMDeviceType type)
}
}
static QString backendName = QStringLiteral("networkmanager");
static QString backendName()
{
return QString::fromUtf16(QNetworkInformationBackend::PluginNames
[QNetworkInformationBackend::PluginNamesLinuxIndex]);
}
class QNetworkManagerNetworkInformationBackend : public QNetworkInformationBackend
{
@ -129,7 +133,7 @@ public:
QNetworkManagerNetworkInformationBackend();
~QNetworkManagerNetworkInformationBackend() = default;
QString name() const override { return backendName; }
QString name() const override { return backendName(); }
QNetworkInformation::Features featuresSupported() const override
{
if (!isValid())
@ -160,7 +164,7 @@ class QNetworkManagerNetworkInformationBackendFactory : public QNetworkInformati
public:
QNetworkManagerNetworkInformationBackendFactory() = default;
~QNetworkManagerNetworkInformationBackendFactory() = default;
QString name() const override { return backendName; }
QString name() const override { return backendName(); }
QNetworkInformation::Features featuresSupported() const override
{
if (!QNetworkManagerInterfaceBase::networkManagerAvailable())

View File

@ -48,8 +48,11 @@ QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(lcNetInfoSCR)
Q_LOGGING_CATEGORY(lcNetInfoSCR, "qt.network.info.scnetworkreachability");
static QString backendName = QStringLiteral("scnetworkreachability");
static QString backendName()
{
return QString::fromUtf16(QNetworkInformationBackend::PluginNames
[QNetworkInformationBackend::PluginNamesAppleIndex]);
}
class QSCNetworkReachabilityNetworkInformationBackend : public QNetworkInformationBackend
{
@ -58,7 +61,7 @@ public:
QSCNetworkReachabilityNetworkInformationBackend();
~QSCNetworkReachabilityNetworkInformationBackend();
QString name() const override { return backendName; }
QString name() const override { return backendName(); }
QNetworkInformation::Features featuresSupported() const override
{
return featuresSupportedStatic();
@ -87,7 +90,7 @@ class QSCNetworkReachabilityNetworkInformationBackendFactory : public QNetworkIn
public:
QSCNetworkReachabilityNetworkInformationBackendFactory() = default;
~QSCNetworkReachabilityNetworkInformationBackendFactory() = default;
QString name() const override { return backendName; }
QString name() const override { return backendName(); }
QNetworkInformation::Features featuresSupported() const override
{
return QSCNetworkReachabilityNetworkInformationBackend::featuresSupportedStatic();

View File

@ -46,9 +46,8 @@ int main(int argc, char **argv)
QCoreApplication app(argc, argv);
#endif
if (!QNetworkInformation::load(QNetworkInformation::Feature::Reachability
| QNetworkInformation::Feature::CaptivePortal
| QNetworkInformation::Feature::TransportMedium)) {
// Use the platform-default:
if (!QNetworkInformation::load()) {
qWarning("Failed to load any backend");
qDebug() << "Backends available:" << QNetworkInformation::availableBackends().join(", ");
return -1;