Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qglibnetworkinformationbackend.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 Ilya Fedin <fedin-ilja2010@ya.ru>
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
6#include <QtCore/qglobal.h>
7#include <QtCore/private/qobject_p.h>
8
9#include <gio/gio.h>
10
12
13using namespace Qt::StringLiterals;
14
15Q_DECLARE_LOGGING_CATEGORY(lcNetInfoGlib)
16Q_LOGGING_CATEGORY(lcNetInfoGlib, "qt.network.info.glib");
17
18namespace {
19QNetworkInformation::Reachability reachabilityFromGNetworkConnectivity(GNetworkConnectivity connectivity)
20{
21 switch (connectivity) {
22 case G_NETWORK_CONNECTIVITY_LOCAL:
24 case G_NETWORK_CONNECTIVITY_LIMITED:
25 case G_NETWORK_CONNECTIVITY_PORTAL:
27 case G_NETWORK_CONNECTIVITY_FULL:
29 }
31}
32}
33
35
37{
39public:
42
43 QString name() const override { return backendName; }
44 QNetworkInformation::Features featuresSupported() const override
45 {
46 if (!isValid())
47 return {};
49 }
50
51 static QNetworkInformation::Features featuresSupportedStatic()
52 {
53 using Feature = QNetworkInformation::Feature;
54 return QNetworkInformation::Features(Feature::Reachability | Feature::CaptivePortal);
55 }
56
57 bool isValid() const;
58
59private:
60 Q_DISABLE_COPY_MOVE(QGlibNetworkInformationBackend)
61
62 static void updateInformation(QGlibNetworkInformationBackend *backend);
63
64 GNetworkMonitor *networkMonitor = nullptr;
65 gulong handlerId = 0;
66};
67
69{
73public:
76 QString name() const override { return backendName; }
77 QNetworkInformation::Features featuresSupported() const override
78 {
80 }
81
82 QNetworkInformationBackend *create(QNetworkInformation::Features requiredFeatures) const override
83 {
84 if ((requiredFeatures & featuresSupported()) != requiredFeatures)
85 return nullptr;
86 auto backend = new QGlibNetworkInformationBackend();
87 if (!backend->isValid())
88 delete std::exchange(backend, nullptr);
89 return backend;
90 }
91private:
92 Q_DISABLE_COPY_MOVE(QGlibNetworkInformationBackendFactory)
93};
94
96: networkMonitor(g_network_monitor_get_default())
97{
98 updateInformation(this);
99
100 handlerId = g_signal_connect_swapped(networkMonitor, "notify::connectivity",
101 G_CALLBACK(updateInformation), this);
102}
103
105{
106 g_signal_handler_disconnect(networkMonitor, handlerId);
107}
108
110{
111 return G_OBJECT_TYPE_NAME(networkMonitor) != "GNetworkMonitorBase"_L1;
112}
113
114void QGlibNetworkInformationBackend::updateInformation(QGlibNetworkInformationBackend *backend)
115{
116 const auto connectivityState = g_network_monitor_get_connectivity(backend->networkMonitor);
117 const bool behindPortal = (connectivityState == G_NETWORK_CONNECTIVITY_PORTAL);
118 backend->setReachability(reachabilityFromGNetworkConnectivity(connectivityState));
119 backend->setBehindCaptivePortal(behindPortal);
120}
121
123
124#include "qglibnetworkinformationbackend.moc"
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().
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 QNetworkInformationBackendFactory::featuresSupported().
static QNetworkInformation::Features featuresSupportedStatic()
QString name() const override
Backend name, return the same in QNetworkInformationBackendFactory::name().
QNetworkInformationBackendFactory provides the interface for creating instances of QNetworkInformatio...
QNetworkInformationBackend provides the interface with which QNetworkInformation does all of its actu...
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.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
Combined button and popup list for selecting options.
static QString backendName
#define Q_LOGGING_CATEGORY(name,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
#define QNetworkInformationBackendFactory_iid
#define QStringLiteral(str)
#define Q_OBJECT
#define Q_PLUGIN_METADATA(x)
#define Q_INTERFACES(x)
QFileInfo info(fileName)
[8]