Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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)
76#endif
77 );
78 }
79
80 [[nodiscard]] bool start();
81 void stop();
82
83private:
84 bool event(QEvent *event) override;
85 void setConnectivity(NLM_CONNECTIVITY newConnectivity);
86 void checkCaptivePortal();
87
88 QComHelper comHelper;
89
90 ComPtr<QNetworkListManagerEvents> managerEvents;
91
92 NLM_CONNECTIVITY connectivity = NLM_CONNECTIVITY_DISCONNECTED;
93
94 bool monitoring = false;
95};
96
98{
102public:
105 QString name() const override { return backendName(); }
106 QNetworkInformation::Features featuresSupported() const override
107 {
109 }
110
112 create(QNetworkInformation::Features requiredFeatures) const override
113 {
114 if ((requiredFeatures & featuresSupported()) != requiredFeatures)
115 return nullptr;
117 if (!backend->start()) {
118 qCWarning(lcNetInfoNLM) << "Failed to start listening to events";
119 delete backend;
120 backend = nullptr;
121 }
122 return backend;
123 }
124};
125
127{
128 if (!comHelper.isValid())
129 return;
130
131 managerEvents = new QNetworkListManagerEvents();
133 &QNetworkListManagerNetworkInformationBackend::setConnectivity);
134
137
138 connect(managerEvents.Get(), &QNetworkListManagerEvents::isMeteredChanged, this,
140}
141
146
147void QNetworkListManagerNetworkInformationBackend::setConnectivity(NLM_CONNECTIVITY newConnectivity)
148{
149 if (reachabilityFromNLM_CONNECTIVITY(connectivity)
150 != reachabilityFromNLM_CONNECTIVITY(newConnectivity)) {
151 connectivity = newConnectivity;
152 setReachability(reachabilityFromNLM_CONNECTIVITY(newConnectivity));
153
154 // @future: only check if signal is connected
155 checkCaptivePortal();
156 }
157}
158
159void QNetworkListManagerNetworkInformationBackend::checkCaptivePortal()
160{
161 setBehindCaptivePortal(managerEvents->checkBehindCaptivePortal());
162}
163
165{
166 if (event->type() == QEvent::ThreadChange)
167 qFatal("Moving QNetworkListManagerNetworkInformationBackend to different thread is not supported");
168
169 return QObject::event(event);
170}
171
173{
174 Q_ASSERT(!monitoring);
175
176 if (!comHelper.isValid())
177 return false;
178
179 if (!managerEvents)
180 managerEvents = new QNetworkListManagerEvents();
181
182 if (managerEvents->start())
183 monitoring = true;
184 return monitoring;
185}
186
188{
189 if (monitoring) {
190 Q_ASSERT(managerEvents);
191 managerEvents->stop();
192 monitoring = false;
193 managerEvents.Reset();
194 }
195}
196
198
199#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:2960
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:1389
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf16(const char16_t *, qsizetype size=-1)
Definition qstring.cpp:6045
Combined button and popup list for selecting options.
#define qFatal
Definition qlogging.h:168
#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)