From a3b58a7844f77bd416fad8307f8333ff7c0efacf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Fri, 18 Dec 2020 15:35:49 +0100 Subject: NetworkListManager based backend for QNetworkInformation For Windows. Based on the code I wrote for QNetworkStatusMonitor. It also renames the netlistmgr feature, avoiding the abbreviation. Locally my MinGW fails the networklistmanager feature test so it may not be supported on MinGW, likely leaving it without a backend at all. Change-Id: I13bbe4127edc2a9c0bb91602c95f1cb206a85a69 Reviewed-by: Timur Pocheptsov --- tests/manual/CMakeLists.txt | 1 + tests/manual/qnetworkinformation/CMakeLists.txt | 8 +++ .../tst_qnetworkinformation.cpp | 63 ++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 tests/manual/qnetworkinformation/CMakeLists.txt create mode 100644 tests/manual/qnetworkinformation/tst_qnetworkinformation.cpp (limited to 'tests/manual') diff --git a/tests/manual/CMakeLists.txt b/tests/manual/CMakeLists.txt index 9f48f2832a..f9eb23cd5f 100644 --- a/tests/manual/CMakeLists.txt +++ b/tests/manual/CMakeLists.txt @@ -31,6 +31,7 @@ add_subdirectory(qlocale) add_subdirectory(qmimedatabase) add_subdirectory(qnetconmonitor) add_subdirectory(qnetworkaccessmanager/qget) +add_subdirectory(qnetworkinformation) #special case begin if (QT_FEATURE_openssl AND UNIX) add_subdirectory(qnetworkreply) diff --git a/tests/manual/qnetworkinformation/CMakeLists.txt b/tests/manual/qnetworkinformation/CMakeLists.txt new file mode 100644 index 0000000000..964d48c93f --- /dev/null +++ b/tests/manual/qnetworkinformation/CMakeLists.txt @@ -0,0 +1,8 @@ + +qt_internal_add_manual_test(qnetworkinformation + SOURCES + tst_qnetworkinformation.cpp + PUBLIC_LIBRARIES + Qt::Network + Qt::Test +) diff --git a/tests/manual/qnetworkinformation/tst_qnetworkinformation.cpp b/tests/manual/qnetworkinformation/tst_qnetworkinformation.cpp new file mode 100644 index 0000000000..6ef337fda9 --- /dev/null +++ b/tests/manual/qnetworkinformation/tst_qnetworkinformation.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +QByteArray nameOfEnumValue(QNetworkInformation::Reachability reachability) +{ + return QMetaEnum::fromType().valueToKey(int(reachability)); +} + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + QTextStream writer(stdout); + + if (!QNetworkInformation::load(QNetworkInformation::Feature::Reachability)) { + qWarning("Failed to load any backend"); + writer << "Backends available: " << QNetworkInformation::availableBackends().join(", ") << '\n'; + return -1; + } + QNetworkInformation *info = QNetworkInformation::instance(); + writer << "Backend loaded: " << info->backendName() << '\n'; + writer << "Now you can make changes to the current network connection. Qt should see the " + "changes and notify about it.\n"; + QObject::connect(info, &QNetworkInformation::reachabilityChanged, + [&writer](QNetworkInformation::Reachability newStatus) { + writer << "Updated: " << nameOfEnumValue(newStatus) << '\n'; + writer.flush(); + }); + + writer << "Initial: " << nameOfEnumValue(info->reachability()) << '\n'; + writer.flush(); + + return app.exec(); +} -- cgit v1.2.3