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>
This commit is contained in:
parent
8896ef999b
commit
6bf41058a6
cmake/3rdparty/extra-cmake-modules/find-modules
src/plugins/networkinformation
@ -71,6 +71,30 @@ find_library(GTHREAD2_LIBRARIES
|
||||
HINTS ${PC_GTHREAD2_LIBDIR}
|
||||
)
|
||||
|
||||
pkg_check_modules(PC_GOBJECT QUIET gobject-2.0)
|
||||
|
||||
find_path(GOBJECT_INCLUDE_DIRS
|
||||
NAMES glib-object.h
|
||||
HINTS ${PC_GOBJECT_INCLUDEDIR}
|
||||
PATH_SUFFIXES glib-2.0)
|
||||
|
||||
find_library(GOBJECT_LIBRARIES
|
||||
NAMES gobject-2.0
|
||||
HINTS ${PC_GOBJECT_LIBDIR}
|
||||
)
|
||||
|
||||
pkg_check_modules(PC_GIO QUIET gio-2.0)
|
||||
|
||||
find_path(GIO_INCLUDE_DIRS
|
||||
NAMES gio/gio.h
|
||||
HINTS ${PC_GIO_INCLUDEDIR}
|
||||
PATH_SUFFIXES glib-2.0)
|
||||
|
||||
find_library(GIO_LIBRARIES
|
||||
NAMES gio-2.0
|
||||
HINTS ${PC_GIO_LIBDIR}
|
||||
)
|
||||
|
||||
# search the glibconfig.h include dir under the same root where the library is found
|
||||
get_filename_component(glib2LibDir "${GLIB2_LIBRARIES}" PATH)
|
||||
|
||||
@ -82,6 +106,8 @@ find_path(GLIB2_INTERNAL_INCLUDE_DIR glibconfig.h
|
||||
# for now it is optional
|
||||
if(GLIB2_INTERNAL_INCLUDE_DIR)
|
||||
list(APPEND GLIB2_INCLUDE_DIRS "${GLIB2_INTERNAL_INCLUDE_DIR}")
|
||||
list(APPEND GOBJECT_INCLUDE_DIRS "${GLIB2_INTERNAL_INCLUDE_DIR}")
|
||||
list(APPEND GIO_INCLUDE_DIRS "${GLIB2_INTERNAL_INCLUDE_DIR}")
|
||||
endif()
|
||||
|
||||
# Deprecated synonyms
|
||||
@ -90,6 +116,8 @@ set(GLIB2_LIBRARY "${GLIB2_LIBRARIES}")
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(GLIB2 DEFAULT_MSG GLIB2_LIBRARIES GTHREAD2_LIBRARIES GLIB2_INCLUDE_DIRS)
|
||||
find_package_handle_standard_args(GOBJECT DEFAULT_MSG GOBJECT_LIBRARIES GOBJECT_INCLUDE_DIRS)
|
||||
find_package_handle_standard_args(GIO DEFAULT_MSG GIO_LIBRARIES GIO_INCLUDE_DIRS)
|
||||
|
||||
if(GLIB2_FOUND AND NOT TARGET GLIB2::GLIB2)
|
||||
add_library(GLIB2::GLIB2 UNKNOWN IMPORTED)
|
||||
@ -99,8 +127,26 @@ if(GLIB2_FOUND AND NOT TARGET GLIB2::GLIB2)
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${GLIB2_INCLUDE_DIRS}")
|
||||
endif()
|
||||
|
||||
if(GOBJECT_FOUND AND NOT TARGET GLIB2::GOBJECT)
|
||||
add_library(GLIB2::GOBJECT UNKNOWN IMPORTED)
|
||||
set_target_properties(GLIB2::GOBJECT PROPERTIES
|
||||
IMPORTED_LOCATION "${GOBJECT_LIBRARIES}"
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${GOBJECT_INCLUDE_DIRS}")
|
||||
endif()
|
||||
|
||||
if(GIO_FOUND AND NOT TARGET GLIB2::GIO)
|
||||
add_library(GLIB2::GIO UNKNOWN IMPORTED)
|
||||
set_target_properties(GLIB2::GIO PROPERTIES
|
||||
IMPORTED_LOCATION "${GIO_LIBRARIES}"
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${GIO_INCLUDE_DIRS}")
|
||||
endif()
|
||||
|
||||
mark_as_advanced(GLIB2_INCLUDE_DIRS GLIB2_INCLUDE_DIR
|
||||
GLIB2_LIBRARIES GLIB2_LIBRARY)
|
||||
GLIB2_LIBRARIES GLIB2_LIBRARY
|
||||
GOBJECT_INCLUDE_DIRS GOBJECT_INCLUDE_DIR
|
||||
GOBJECT_LIBRARIES GOBJECT_LIBRARY
|
||||
GIO_INCLUDE_DIRS GIO_INCLUDE_DIR
|
||||
GIO_LIBRARIES GIO_LIBRARY)
|
||||
|
||||
include(FeatureSummary)
|
||||
set_package_properties(GLIB2 PROPERTIES
|
||||
|
@ -13,3 +13,7 @@ endif()
|
||||
if(ANDROID)
|
||||
add_subdirectory(android)
|
||||
endif()
|
||||
|
||||
if(QT_FEATURE_glib)
|
||||
add_subdirectory(glib)
|
||||
endif()
|
||||
|
14
src/plugins/networkinformation/glib/CMakeLists.txt
Normal file
14
src/plugins/networkinformation/glib/CMakeLists.txt
Normal file
@ -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
|
||||
)
|
@ -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"
|
Loading…
Reference in New Issue
Block a user