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
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 | Feature::Metered);
56 }
57
58 bool isValid() const;
59
60private:
61 Q_DISABLE_COPY_MOVE(QGlibNetworkInformationBackend)
62
63 static void updateConnectivity(QGlibNetworkInformationBackend *backend);
64 static void updateMetered(QGlibNetworkInformationBackend *backend);
65
66 GNetworkMonitor *networkMonitor = nullptr;
67 gulong connectivityHandlerId = 0;
68 gulong meteredHandlerId = 0;
69};
70
72{
76public:
79 QString name() const override { return backendName; }
80 QNetworkInformation::Features featuresSupported() const override
81 {
83 }
84
85 QNetworkInformationBackend *create(QNetworkInformation::Features requiredFeatures) const override
86 {
87 if ((requiredFeatures & featuresSupported()) != requiredFeatures)
88 return nullptr;
89 auto backend = new QGlibNetworkInformationBackend();
90 if (!backend->isValid())
91 delete std::exchange(backend, nullptr);
92 return backend;
93 }
94private:
95 Q_DISABLE_COPY_MOVE(QGlibNetworkInformationBackendFactory)
96};
97
99: networkMonitor(g_network_monitor_get_default())
100{
101 updateConnectivity(this);
102 updateMetered(this);
103
104 connectivityHandlerId = g_signal_connect_swapped(networkMonitor, "notify::connectivity",
105 G_CALLBACK(updateConnectivity), this);
106
107 meteredHandlerId = g_signal_connect_swapped(networkMonitor, "notify::network-metered",
108 G_CALLBACK(updateMetered), this);
109}
110
112{
113 g_signal_handler_disconnect(networkMonitor, meteredHandlerId);
114 g_signal_handler_disconnect(networkMonitor, connectivityHandlerId);
115}
116
118{
119 return QLatin1StringView(G_OBJECT_TYPE_NAME(networkMonitor)) != "GNetworkMonitorBase"_L1;
120}
121
122void QGlibNetworkInformationBackend::updateConnectivity(QGlibNetworkInformationBackend *backend)
123{
124 const auto connectivityState = g_network_monitor_get_connectivity(backend->networkMonitor);
125 const bool behindPortal = (connectivityState == G_NETWORK_CONNECTIVITY_PORTAL);
126 backend->setReachability(reachabilityFromGNetworkConnectivity(connectivityState));
127 backend->setBehindCaptivePortal(behindPortal);
128}
129
130void QGlibNetworkInformationBackend::updateMetered(QGlibNetworkInformationBackend *backend)
131{
132 backend->setMetered(g_network_monitor_get_network_metered(backend->networkMonitor));
133}
134
136
137#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:129
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)
QHostInfo info
[0]