Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qnetworkmanagernetworkinformationbackend.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtNetwork/private/qnetworkinformation_p.h>
5
7
8#include <QtCore/qglobal.h>
9#include <QtCore/private/qobject_p.h>
10
11#include <QtDBus/qdbusmessage.h>
12
15Q_LOGGING_CATEGORY(lcNetInfoNM, "qt.network.info.networkmanager");
16
17namespace {
19{
20 switch (state) {
25 case QNetworkManagerInterface::NM_STATE_DISCONNECTING: // No point in starting new connections:
34 }
36}
37
39transportMediumFromDeviceType(QNetworkManagerInterface::NMDeviceType type)
40{
41 switch (type) {
50
79 break;
80 }
81 // While the list is exhaustive of the enum there can be additional
82 // entries added in NetworkManager that isn't listed here
84}
85
86bool isMeteredFromNMMetered(QNetworkManagerInterface::NMMetered metered)
87{
88 switch (metered) {
91 return true;
95 return false;
96 }
97 Q_UNREACHABLE_RETURN(false);
98}
99} // unnamed namespace
100
102{
105}
106
108{
110public:
113
114 QString name() const override { return backendName(); }
115 QNetworkInformation::Features featuresSupported() const override
116 {
117 if (!isValid())
118 return {};
120 }
121
122 static QNetworkInformation::Features featuresSupportedStatic()
123 {
124 using Feature = QNetworkInformation::Feature;
125 return QNetworkInformation::Features(Feature::Reachability | Feature::CaptivePortal
126 | Feature::TransportMedium | Feature::Metered);
127 }
128
129 bool isValid() const { return iface.isValid(); }
130
131private:
133
135};
136
138{
142public:
145 QString name() const override { return backendName(); }
146 QNetworkInformation::Features featuresSupported() const override
147 {
149 return {};
151 }
152
153 QNetworkInformationBackend *create(QNetworkInformation::Features requiredFeatures) const override
154 {
155 if ((requiredFeatures & featuresSupported()) != requiredFeatures)
156 return nullptr;
158 return nullptr;
159 auto backend = new QNetworkManagerNetworkInformationBackend();
160 if (!backend->isValid())
161 delete std::exchange(backend, nullptr);
162 return backend;
163 }
164private:
166};
167
169{
170 auto updateReachability = [this](QNetworkManagerInterface::NMState newState) {
171 setReachability(reachabilityFromNMState(newState));
172 };
173 updateReachability(iface.state());
174 connect(&iface, &QNetworkManagerInterface::stateChanged, this, std::move(updateReachability));
175
176 auto updateBehindCaptivePortal = [this](QNetworkManagerInterface::NMConnectivityState state) {
177 const bool behindPortal = (state == QNetworkManagerInterface::NM_CONNECTIVITY_PORTAL);
178 setBehindCaptivePortal(behindPortal);
179 };
180 updateBehindCaptivePortal(iface.connectivityState());
182 std::move(updateBehindCaptivePortal));
183
184 auto updateTransportMedium = [this](QNetworkManagerInterface::NMDeviceType newDevice) {
185 setTransportMedium(transportMediumFromDeviceType(newDevice));
186 };
187 updateTransportMedium(iface.deviceType());
189 std::move(updateTransportMedium));
190
191 auto updateMetered = [this](QNetworkManagerInterface::NMMetered metered) {
192 setMetered(isMeteredFromNMMetered(metered));
193 };
194 updateMetered(iface.meteredState());
195 connect(&iface, &QNetworkManagerInterface::meteredChanged, this, std::move(updateMetered));
196}
197
199
200#include "qnetworkmanagernetworkinformationbackend.moc"
bool isValid() const
Returns true if this is a valid reference to a remote object.
QNetworkInformationBackendFactory provides the interface for creating instances of QNetworkInformatio...
QNetworkInformationBackend provides the interface with which QNetworkInformation does all of its actu...
static constexpr int PluginNamesLinuxIndex
void setTransportMedium(TransportMedium medium)
static const char16_t PluginNames[4][22]
void setBehindCaptivePortal(bool behindPortal)
void setReachability(QNetworkInformation::Reachability reachability)
Call this when reachability has changed.
Reachability
\value Unknown If this value is returned then we may be connected but the OS has still not confirmed ...
Feature
Lists all of the features that a plugin may currently support.
void stateChanged(NMState)
NMConnectivityState connectivityState() const
void meteredChanged(NMMetered)
void connectivityChanged(NMConnectivityState)
void deviceTypeChanged(NMDeviceType)
QString name() const override
Backend name, return the same in QNetworkInformationBackend::name().
QNetworkInformationBackend * create(QNetworkInformation::Features requiredFeatures) const override
Create and return an instance of QNetworkInformationBackend.
QNetworkInformation::Features featuresSupported() const override
Features supported, return the same in QNetworkInformationBackend::featuresSupported().
QString name() const override
Backend name, return the same in QNetworkInformationBackendFactory::name().
QNetworkInformation::Features featuresSupported() const override
Features supported, return the same in QNetworkInformationBackendFactory::featuresSupported().
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromUtf16(const char16_t *, qsizetype size=-1)
Definition qstring.cpp:5883
else opt state
[0]
void newState(QList< State > &states, const char *token, const char *lexem, bool pre)
Combined button and popup list for selecting options.
#define Q_LOGGING_CATEGORY(name,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
#define QNetworkInformationBackendFactory_iid
GLenum type
#define Q_OBJECT
#define Q_PLUGIN_METADATA(x)
#define Q_INTERFACES(x)
QFileInfo info(fileName)
[8]