From d7602df51e8c12112cbc16ceea9ceceb6900af60 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 20 Aug 2019 09:25:37 -0400 Subject: [PATCH] [libcalamares] Introduce networking service - The networking service is intended to wrap up use of QNetworkAccessManager and others for consumption within Calamares, and to provide some convenience functions for internet access. - Medium term, it may also monitor network access, so that we can respond to changes in network availability during installation. Currently very minimal and undocumented. --- src/libcalamares/CMakeLists.txt | 3 + src/libcalamares/network/Manager.cpp | 93 ++++++++++++++++++++++++++++ src/libcalamares/network/Manager.h | 55 ++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/libcalamares/network/Manager.cpp create mode 100644 src/libcalamares/network/Manager.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index f2063813c..ac9b2df08 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -36,6 +36,9 @@ set( libSources locale/Lookup.cpp locale/TranslatableConfiguration.cpp + # Network service + network/Manager.cpp + # Partition service partition/PartitionSize.cpp diff --git a/src/libcalamares/network/Manager.cpp b/src/libcalamares/network/Manager.cpp new file mode 100644 index 000000000..c229ab036 --- /dev/null +++ b/src/libcalamares/network/Manager.cpp @@ -0,0 +1,93 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "Manager.h" + +#include +#include +#include +#include + +namespace CalamaresUtils +{ +namespace Network +{ +struct Manager::Private +{ + std::unique_ptr< QNetworkAccessManager > m_nam; + QUrl m_hasInternetUrl; + bool m_hasInternet; + + Private(); +}; +} // namespace Network +} // namespace CalamaresUtils +CalamaresUtils::Network::Manager::Private::Private() + : m_nam( std::make_unique< QNetworkAccessManager >() ) + , m_hasInternet( false ) +{ +} + +CalamaresUtils::Network::Manager::Manager() + : d( std::make_unique< Private >() ) +{ +} + +CalamaresUtils::Network::Manager::~Manager() {} + +CalamaresUtils::Network::Manager& +CalamaresUtils::Network::Manager::instance() +{ + static auto* s_manager = new CalamaresUtils::Network::Manager(); + return *s_manager; +} + +bool +CalamaresUtils::Network::Manager::hasInternet() +{ + return d->m_hasInternet; +} + +bool +CalamaresUtils::Network::Manager::checkHasInternet() +{ + bool b = false; + if ( d->m_hasInternetUrl.isValid() ) + { + b = synchronousPing( d->m_hasInternetUrl ); + } + d->m_hasInternet = b; + return b; +} + +void +CalamaresUtils::Network::Manager::setCheckHasInternetUrl( const QUrl& url ) +{ + d->m_hasInternetUrl = url; +} + +bool +CalamaresUtils::Network::Manager::synchronousPing( const QUrl& url ) +{ + QNetworkRequest req = QNetworkRequest( url ); + QNetworkReply* reply = d->m_nam->get( req ); + QEventLoop loop; + connect( reply, &QNetworkReply::finished, &loop, &QEventLoop::quit ); + loop.exec(); + return reply->bytesAvailable(); +} diff --git a/src/libcalamares/network/Manager.h b/src/libcalamares/network/Manager.h new file mode 100644 index 000000000..a9903e115 --- /dev/null +++ b/src/libcalamares/network/Manager.h @@ -0,0 +1,55 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef LIBCALAMARES_NETWORK_MANAGER_H +#define LIBCALAMARES_NETWORK_MANAGER_H + +#include "DllMacro.h" + +#include +#include + +#include + +namespace CalamaresUtils +{ +namespace Network +{ +class DLLEXPORT Manager : QObject +{ + Q_OBJECT + + Manager(); + +public: + static Manager& instance(); + virtual ~Manager(); + + bool synchronousPing( const QUrl& url ); + + void setCheckHasInternetUrl( const QUrl& url ); + bool checkHasInternet(); + bool hasInternet(); + +private: + struct Private; + std::unique_ptr< Private > d; +}; +} // namespace Network +} // namespace CalamaresUtils +#endif // LIBCALAMARES_NETWORK_MANAGER_H