Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qnetworklistmanagernetworkinformationbackend.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#include <QtCore/qscopeguard.h>
11
12#include <QtCore/private/qfunctions_win_p.h>
13
15
16// Declared in qnetworklistmanagerevents.h
17Q_LOGGING_CATEGORY(lcNetInfoNLM, "qt.network.info.netlistmanager");
18
20{
23}
24
25namespace {
26bool testCONNECTIVITY(NLM_CONNECTIVITY connectivity, NLM_CONNECTIVITY flag)
27{
28 return (connectivity & flag) == flag;
29}
30
31QNetworkInformation::Reachability reachabilityFromNLM_CONNECTIVITY(NLM_CONNECTIVITY connectivity)
32{
33 if (connectivity == NLM_CONNECTIVITY_DISCONNECTED)
35 if (testCONNECTIVITY(connectivity, NLM_CONNECTIVITY_IPV6_INTERNET)
36 || testCONNECTIVITY(connectivity, NLM_CONNECTIVITY_IPV4_INTERNET)) {
38 }
39 if (testCONNECTIVITY(connectivity, NLM_CONNECTIVITY_IPV6_SUBNET)
40 || testCONNECTIVITY(connectivity, NLM_CONNECTIVITY_IPV4_SUBNET)) {
42 }
43 if (testCONNECTIVITY(connectivity, NLM_CONNECTIVITY_IPV6_LOCALNETWORK)
44 || testCONNECTIVITY(connectivity, NLM_CONNECTIVITY_IPV4_LOCALNETWORK)) {
46 }
47 if (testCONNECTIVITY(connectivity, NLM_CONNECTIVITY_IPV6_NOTRAFFIC)
48 || testCONNECTIVITY(connectivity, NLM_CONNECTIVITY_IPV4_NOTRAFFIC)) {
50 }
51
53}
54}
55
57{
59public:
62
63 QString name() const override { return backendName(); }
64 QNetworkInformation::Features featuresSupported() const override
65 {
67 }
68
69 static QNetworkInformation::Features featuresSupportedStatic()
70 {
71 return QNetworkInformation::Features(QNetworkInformation::Feature::Reachability
73#if QT_CONFIG(cpp_winrt)
75#endif
76 );
77 }
78
79 [[nodiscard]] bool start();
80 void stop();
81
82private:
83 bool event(QEvent *event) override;
84 void setConnectivity(NLM_CONNECTIVITY newConnectivity);
85 void checkCaptivePortal();
86
87 QComHelper comHelper;
88
90
91 NLM_CONNECTIVITY connectivity = NLM_CONNECTIVITY_DISCONNECTED;
92
93 bool monitoring = false;
94};
95
97{
101public:
104 QString name() const override { return backendName(); }
105 QNetworkInformation::Features featuresSupported() const override
106 {
108 }
109
111 create(QNetworkInformation::Features requiredFeatures) const override
112 {
113 if ((requiredFeatures & featuresSupported()) != requiredFeatures)
114 return nullptr;
116 if (!backend->start()) {
117 qCWarning(lcNetInfoNLM) << "Failed to start listening to events";
118 delete backend;
119 backend = nullptr;
120 }
121 return backend;
122 }
123};
124
126{
127 if (!comHelper.isValid())
128 return;
129
130 managerEvents = new QNetworkListManagerEvents();
132 &QNetworkListManagerNetworkInformationBackend::setConnectivity);
133
136
137 connect(managerEvents.Get(), &QNetworkListManagerEvents::isMeteredChanged, this,
139}
140
142{
143 stop();
144}
145
146void QNetworkListManagerNetworkInformationBackend::setConnectivity(NLM_CONNECTIVITY newConnectivity)
147{
148 if (reachabilityFromNLM_CONNECTIVITY(connectivity)
149 != reachabilityFromNLM_CONNECTIVITY(newConnectivity)) {
150 connectivity = newConnectivity;
151 setReachability(reachabilityFromNLM_CONNECTIVITY(newConnectivity));
152
153 // @future: only check if signal is connected
154 checkCaptivePortal();
155 }
156}
157
158void QNetworkListManagerNetworkInformationBackend::checkCaptivePortal()
159{
160 setBehindCaptivePortal(managerEvents->checkBehindCaptivePortal());
161}
162
164{
165 if (event->type() == QEvent::ThreadChange)
166 qFatal("Moving QNetworkListManagerNetworkInformationBackend to different thread is not supported");
167
168 return QObject::event(event);
169}
170
172{
173 Q_ASSERT(!monitoring);
174
175 if (!comHelper.isValid())
176 return false;
177
178 if (!managerEvents)
179 managerEvents = new QNetworkListManagerEvents();
180
181 if (managerEvents->start())
182 monitoring = true;
183 return monitoring;
184}
185
187{
188 if (monitoring) {
189 Q_ASSERT(managerEvents);
190 managerEvents->stop();
191 monitoring = false;
192 managerEvents.Reset();
193 }
194}
195
197
198#include "qnetworklistmanagernetworkinformationbackend.moc"
\inmodule QtCore
Definition qcoreevent.h:45
@ ThreadChange
Definition qcoreevent.h:82
QNetworkInformationBackendFactory provides the interface for creating instances of QNetworkInformatio...
QNetworkInformationBackend provides the interface with which QNetworkInformation does all of its actu...
static constexpr int PluginNamesWindowsIndex
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 ...
void transportMediumChanged(QNetworkInformation::TransportMedium)
void connectivityChanged(NLM_CONNECTIVITY)
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 QNetworkInformationBackend::name().
QString name() const override
Backend name, return the same in QNetworkInformationBackendFactory::name().
bool event(QEvent *event) override
This virtual function receives events to an object and should return true if the event e was recogniz...
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
virtual bool event(QEvent *event)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition qobject.cpp:1363
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromUtf16(const char16_t *, qsizetype size=-1)
Definition qstring.cpp:5883
Combined button and popup list for selecting options.
#define qFatal
Definition qlogging.h:164
#define Q_LOGGING_CATEGORY(name,...)
#define qCWarning(category,...)
#define QNetworkInformationBackendFactory_iid
struct _cl_event * event
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QT_CONFIG(feature)
#define Q_OBJECT
#define Q_PLUGIN_METADATA(x)
#define Q_INTERFACES(x)