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:
parent
5f8e9c194e
commit
bfe579613a
@ -603,12 +603,63 @@ QNetworkInformation::Features QNetworkInformation::supportedFeatures() const
|
|||||||
return d_func()->backend->featuresSupported();
|
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
|
Attempts to load a backend whose name matches \a backend
|
||||||
(case insensitively).
|
(case insensitively).
|
||||||
|
|
||||||
Returns \c true if it managed to load the requested backend or
|
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
|
\sa instance
|
||||||
*/
|
*/
|
||||||
@ -622,7 +673,7 @@ bool QNetworkInformation::load(QStringView backend)
|
|||||||
Load a backend which supports \a features.
|
Load a backend which supports \a features.
|
||||||
|
|
||||||
Returns \c true if it managed to load the requested backend or
|
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
|
\sa instance
|
||||||
*/
|
*/
|
||||||
|
@ -96,6 +96,7 @@ public:
|
|||||||
bool supports(Features features) const;
|
bool supports(Features features) const;
|
||||||
Features supportedFeatures() const;
|
Features supportedFeatures() const;
|
||||||
|
|
||||||
|
static bool load();
|
||||||
static bool load(QStringView backend);
|
static bool load(QStringView backend);
|
||||||
static bool load(Features features);
|
static bool load(Features features);
|
||||||
static QStringList availableBackends();
|
static QStringList availableBackends();
|
||||||
|
@ -67,6 +67,17 @@ class Q_NETWORK_EXPORT QNetworkInformationBackend : public QObject
|
|||||||
using TransportMedium = QNetworkInformation::TransportMedium;
|
using TransportMedium = QNetworkInformation::TransportMedium;
|
||||||
|
|
||||||
public:
|
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() = default;
|
||||||
~QNetworkInformationBackend() override;
|
~QNetworkInformationBackend() override;
|
||||||
|
|
||||||
|
@ -47,7 +47,10 @@ QT_BEGIN_NAMESPACE
|
|||||||
Q_DECLARE_LOGGING_CATEGORY(lcNetInfoAndroid)
|
Q_DECLARE_LOGGING_CATEGORY(lcNetInfoAndroid)
|
||||||
Q_LOGGING_CATEGORY(lcNetInfoAndroid, "qt.network.info.android");
|
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
|
class QAndroidNetworkInformationBackend : public QNetworkInformationBackend
|
||||||
{
|
{
|
||||||
@ -56,7 +59,7 @@ public:
|
|||||||
QAndroidNetworkInformationBackend();
|
QAndroidNetworkInformationBackend();
|
||||||
~QAndroidNetworkInformationBackend() { m_valid = false; }
|
~QAndroidNetworkInformationBackend() { m_valid = false; }
|
||||||
|
|
||||||
QString name() const override { return backendName; }
|
QString name() const override { return backendName(); }
|
||||||
QNetworkInformation::Features featuresSupported() const override
|
QNetworkInformation::Features featuresSupported() const override
|
||||||
{
|
{
|
||||||
return featuresSupportedStatic();
|
return featuresSupportedStatic();
|
||||||
@ -88,7 +91,7 @@ class QAndroidNetworkInformationBackendFactory : public QNetworkInformationBacke
|
|||||||
public:
|
public:
|
||||||
QAndroidNetworkInformationBackendFactory() = default;
|
QAndroidNetworkInformationBackendFactory() = default;
|
||||||
~QAndroidNetworkInformationBackendFactory() = default;
|
~QAndroidNetworkInformationBackendFactory() = default;
|
||||||
QString name() const override { return backendName; }
|
QString name() const override { return backendName(); }
|
||||||
QNetworkInformation::Features featuresSupported() const override
|
QNetworkInformation::Features featuresSupported() const override
|
||||||
{
|
{
|
||||||
return QAndroidNetworkInformationBackend::featuresSupportedStatic();
|
return QAndroidNetworkInformationBackend::featuresSupportedStatic();
|
||||||
|
@ -50,7 +50,11 @@ QT_BEGIN_NAMESPACE
|
|||||||
// Declared in qnetworklistmanagerevents.h
|
// Declared in qnetworklistmanagerevents.h
|
||||||
Q_LOGGING_CATEGORY(lcNetInfoNLM, "qt.network.info.netlistmanager");
|
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 {
|
namespace {
|
||||||
bool testCONNECTIVITY(NLM_CONNECTIVITY connectivity, NLM_CONNECTIVITY flag)
|
bool testCONNECTIVITY(NLM_CONNECTIVITY connectivity, NLM_CONNECTIVITY flag)
|
||||||
@ -90,7 +94,7 @@ public:
|
|||||||
QNetworkListManagerNetworkInformationBackend();
|
QNetworkListManagerNetworkInformationBackend();
|
||||||
~QNetworkListManagerNetworkInformationBackend();
|
~QNetworkListManagerNetworkInformationBackend();
|
||||||
|
|
||||||
QString name() const override { return backendName; }
|
QString name() const override { return backendName(); }
|
||||||
QNetworkInformation::Features featuresSupported() const override
|
QNetworkInformation::Features featuresSupported() const override
|
||||||
{
|
{
|
||||||
return featuresSupportedStatic();
|
return featuresSupportedStatic();
|
||||||
@ -130,7 +134,7 @@ class QNetworkListManagerNetworkInformationBackendFactory : public QNetworkInfor
|
|||||||
public:
|
public:
|
||||||
QNetworkListManagerNetworkInformationBackendFactory() = default;
|
QNetworkListManagerNetworkInformationBackendFactory() = default;
|
||||||
~QNetworkListManagerNetworkInformationBackendFactory() = default;
|
~QNetworkListManagerNetworkInformationBackendFactory() = default;
|
||||||
QString name() const override { return backendName; }
|
QString name() const override { return backendName(); }
|
||||||
QNetworkInformation::Features featuresSupported() const override
|
QNetworkInformation::Features featuresSupported() const override
|
||||||
{
|
{
|
||||||
return QNetworkListManagerNetworkInformationBackend::featuresSupportedStatic();
|
return QNetworkListManagerNetworkInformationBackend::featuresSupportedStatic();
|
||||||
|
@ -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
|
class QNetworkManagerNetworkInformationBackend : public QNetworkInformationBackend
|
||||||
{
|
{
|
||||||
@ -129,7 +133,7 @@ public:
|
|||||||
QNetworkManagerNetworkInformationBackend();
|
QNetworkManagerNetworkInformationBackend();
|
||||||
~QNetworkManagerNetworkInformationBackend() = default;
|
~QNetworkManagerNetworkInformationBackend() = default;
|
||||||
|
|
||||||
QString name() const override { return backendName; }
|
QString name() const override { return backendName(); }
|
||||||
QNetworkInformation::Features featuresSupported() const override
|
QNetworkInformation::Features featuresSupported() const override
|
||||||
{
|
{
|
||||||
if (!isValid())
|
if (!isValid())
|
||||||
@ -160,7 +164,7 @@ class QNetworkManagerNetworkInformationBackendFactory : public QNetworkInformati
|
|||||||
public:
|
public:
|
||||||
QNetworkManagerNetworkInformationBackendFactory() = default;
|
QNetworkManagerNetworkInformationBackendFactory() = default;
|
||||||
~QNetworkManagerNetworkInformationBackendFactory() = default;
|
~QNetworkManagerNetworkInformationBackendFactory() = default;
|
||||||
QString name() const override { return backendName; }
|
QString name() const override { return backendName(); }
|
||||||
QNetworkInformation::Features featuresSupported() const override
|
QNetworkInformation::Features featuresSupported() const override
|
||||||
{
|
{
|
||||||
if (!QNetworkManagerInterfaceBase::networkManagerAvailable())
|
if (!QNetworkManagerInterfaceBase::networkManagerAvailable())
|
||||||
|
@ -48,8 +48,11 @@ QT_BEGIN_NAMESPACE
|
|||||||
Q_DECLARE_LOGGING_CATEGORY(lcNetInfoSCR)
|
Q_DECLARE_LOGGING_CATEGORY(lcNetInfoSCR)
|
||||||
Q_LOGGING_CATEGORY(lcNetInfoSCR, "qt.network.info.scnetworkreachability");
|
Q_LOGGING_CATEGORY(lcNetInfoSCR, "qt.network.info.scnetworkreachability");
|
||||||
|
|
||||||
|
static QString backendName()
|
||||||
static QString backendName = QStringLiteral("scnetworkreachability");
|
{
|
||||||
|
return QString::fromUtf16(QNetworkInformationBackend::PluginNames
|
||||||
|
[QNetworkInformationBackend::PluginNamesAppleIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
class QSCNetworkReachabilityNetworkInformationBackend : public QNetworkInformationBackend
|
class QSCNetworkReachabilityNetworkInformationBackend : public QNetworkInformationBackend
|
||||||
{
|
{
|
||||||
@ -58,7 +61,7 @@ public:
|
|||||||
QSCNetworkReachabilityNetworkInformationBackend();
|
QSCNetworkReachabilityNetworkInformationBackend();
|
||||||
~QSCNetworkReachabilityNetworkInformationBackend();
|
~QSCNetworkReachabilityNetworkInformationBackend();
|
||||||
|
|
||||||
QString name() const override { return backendName; }
|
QString name() const override { return backendName(); }
|
||||||
QNetworkInformation::Features featuresSupported() const override
|
QNetworkInformation::Features featuresSupported() const override
|
||||||
{
|
{
|
||||||
return featuresSupportedStatic();
|
return featuresSupportedStatic();
|
||||||
@ -87,7 +90,7 @@ class QSCNetworkReachabilityNetworkInformationBackendFactory : public QNetworkIn
|
|||||||
public:
|
public:
|
||||||
QSCNetworkReachabilityNetworkInformationBackendFactory() = default;
|
QSCNetworkReachabilityNetworkInformationBackendFactory() = default;
|
||||||
~QSCNetworkReachabilityNetworkInformationBackendFactory() = default;
|
~QSCNetworkReachabilityNetworkInformationBackendFactory() = default;
|
||||||
QString name() const override { return backendName; }
|
QString name() const override { return backendName(); }
|
||||||
QNetworkInformation::Features featuresSupported() const override
|
QNetworkInformation::Features featuresSupported() const override
|
||||||
{
|
{
|
||||||
return QSCNetworkReachabilityNetworkInformationBackend::featuresSupportedStatic();
|
return QSCNetworkReachabilityNetworkInformationBackend::featuresSupportedStatic();
|
||||||
|
@ -46,9 +46,8 @@ int main(int argc, char **argv)
|
|||||||
QCoreApplication app(argc, argv);
|
QCoreApplication app(argc, argv);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!QNetworkInformation::load(QNetworkInformation::Feature::Reachability
|
// Use the platform-default:
|
||||||
| QNetworkInformation::Feature::CaptivePortal
|
if (!QNetworkInformation::load()) {
|
||||||
| QNetworkInformation::Feature::TransportMedium)) {
|
|
||||||
qWarning("Failed to load any backend");
|
qWarning("Failed to load any backend");
|
||||||
qDebug() << "Backends available:" << QNetworkInformation::availableBackends().join(", ");
|
qDebug() << "Backends available:" << QNetworkInformation::availableBackends().join(", ");
|
||||||
return -1;
|
return -1;
|
||||||
|
Loading…
Reference in New Issue
Block a user