diff options
author | Ilya Fedin <fedin-ilja2010@ya.ru> | 2021-10-22 14:44:23 +0400 |
---|---|---|
committer | Ilya Fedin <fedin-ilja2010@ya.ru> | 2021-10-24 18:22:46 +0400 |
commit | 6bf41058a64ad86b8f3a54652bf2132c08a336e0 (patch) | |
tree | c2fc9df0edc6a87fa36f242ba2c338e2af6c100e /src/plugins/networkinformation | |
parent | 8896ef999b386bcdd20aa5e8f2ebfd11d3706221 (diff) |
Add a QNetworkInformation backend based on GNetworkMonitor
GNetworkMonitor can get information from 3 sources:
* org.freedesktop.portal.NetworkMonitor
* NetworkManager
* netlink
Change-Id: Icfafe6af42148f26360449f1262093ffc6b0613a
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/plugins/networkinformation')
3 files changed, 175 insertions, 0 deletions
diff --git a/src/plugins/networkinformation/CMakeLists.txt b/src/plugins/networkinformation/CMakeLists.txt index 142031c62a..ddb907d1e7 100644 --- a/src/plugins/networkinformation/CMakeLists.txt +++ b/src/plugins/networkinformation/CMakeLists.txt @@ -13,3 +13,7 @@ endif() if(ANDROID) add_subdirectory(android) endif() + +if(QT_FEATURE_glib) + add_subdirectory(glib) +endif() diff --git a/src/plugins/networkinformation/glib/CMakeLists.txt b/src/plugins/networkinformation/glib/CMakeLists.txt new file mode 100644 index 0000000000..17a16a15c0 --- /dev/null +++ b/src/plugins/networkinformation/glib/CMakeLists.txt @@ -0,0 +1,14 @@ +qt_internal_add_plugin(QGlibNetworkInformationPlugin + OUTPUT_NAME qglib + CLASS_NAME QGlibNetworkInformationBackendFactory + PLUGIN_TYPE networkinformation + DEFAULT_IF LINUX + SOURCES + qglibnetworkinformationbackend.cpp + LIBRARIES + Qt::NetworkPrivate + GLIB2::GOBJECT + GLIB2::GIO + DEFINES + QT_NO_SIGNALS_SLOTS_KEYWORDS +) diff --git a/src/plugins/networkinformation/glib/qglibnetworkinformationbackend.cpp b/src/plugins/networkinformation/glib/qglibnetworkinformationbackend.cpp new file mode 100644 index 0000000000..f26079b041 --- /dev/null +++ b/src/plugins/networkinformation/glib/qglibnetworkinformationbackend.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** Copyright (C) 2021 Ilya Fedin <fedin-ilja2010@ya.ru> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtNetwork/private/qnetworkinformation_p.h> + +#include <QtCore/qglobal.h> +#include <QtCore/private/qobject_p.h> + +#include <gio/gio.h> + +QT_BEGIN_NAMESPACE +Q_DECLARE_LOGGING_CATEGORY(lcNetInfoGlib) +Q_LOGGING_CATEGORY(lcNetInfoGlib, "qt.network.info.glib"); + +namespace { +QNetworkInformation::Reachability reachabilityFromGNetworkConnectivity(GNetworkConnectivity connectivity) +{ + switch (connectivity) { + case G_NETWORK_CONNECTIVITY_LOCAL: + return QNetworkInformation::Reachability::Disconnected; + case G_NETWORK_CONNECTIVITY_LIMITED: + case G_NETWORK_CONNECTIVITY_PORTAL: + return QNetworkInformation::Reachability::Site; + case G_NETWORK_CONNECTIVITY_FULL: + return QNetworkInformation::Reachability::Online; + } + return QNetworkInformation::Reachability::Unknown; +} +} + +static QString backendName = QStringLiteral("glib"); + +class QGlibNetworkInformationBackend : public QNetworkInformationBackend +{ + Q_OBJECT +public: + QGlibNetworkInformationBackend(); + ~QGlibNetworkInformationBackend(); + + QString name() const override { return backendName; } + QNetworkInformation::Features featuresSupported() const override + { + if (!isValid()) + return {}; + return featuresSupportedStatic(); + } + + static QNetworkInformation::Features featuresSupportedStatic() + { + using Feature = QNetworkInformation::Feature; + return QNetworkInformation::Features(Feature::Reachability | Feature::CaptivePortal); + } + + bool isValid() const; + +private: + Q_DISABLE_COPY_MOVE(QGlibNetworkInformationBackend) + + static void updateInformation(QGlibNetworkInformationBackend *backend); + + GNetworkMonitor *networkMonitor = nullptr; + gulong handlerId = 0; +}; + +class QGlibNetworkInformationBackendFactory : public QNetworkInformationBackendFactory +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID QNetworkInformationBackendFactory_iid) + Q_INTERFACES(QNetworkInformationBackendFactory) +public: + QGlibNetworkInformationBackendFactory() = default; + ~QGlibNetworkInformationBackendFactory() = default; + QString name() const override { return backendName; } + QNetworkInformation::Features featuresSupported() const override + { + return QGlibNetworkInformationBackend::featuresSupportedStatic(); + } + + QNetworkInformationBackend *create(QNetworkInformation::Features requiredFeatures) const override + { + if ((requiredFeatures & featuresSupported()) != requiredFeatures) + return nullptr; + auto backend = new QGlibNetworkInformationBackend(); + if (!backend->isValid()) + delete std::exchange(backend, nullptr); + return backend; + } +private: + Q_DISABLE_COPY_MOVE(QGlibNetworkInformationBackendFactory) +}; + +QGlibNetworkInformationBackend::QGlibNetworkInformationBackend() +: networkMonitor(g_network_monitor_get_default()) +{ + updateInformation(this); + + handlerId = g_signal_connect_swapped(networkMonitor, "notify::connectivity", + G_CALLBACK(updateInformation), this); +} + +QGlibNetworkInformationBackend::~QGlibNetworkInformationBackend() +{ + g_signal_handler_disconnect(networkMonitor, handlerId); +} + +bool QGlibNetworkInformationBackend::isValid() const +{ + return G_OBJECT_TYPE_NAME(networkMonitor) != QLatin1String("GNetworkMonitorBase"); +} + +void QGlibNetworkInformationBackend::updateInformation(QGlibNetworkInformationBackend *backend) +{ + const auto connectivityState = g_network_monitor_get_connectivity(backend->networkMonitor); + const bool behindPortal = (connectivityState == G_NETWORK_CONNECTIVITY_PORTAL); + backend->setReachability(reachabilityFromGNetworkConnectivity(connectivityState)); + backend->setBehindCaptivePortal(behindPortal); +} + +QT_END_NAMESPACE + +#include "qglibnetworkinformationbackend.moc" |