From 36fb1124be8b0a215e0eb88418bf042bf3861792 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 23 Jul 2020 12:57:01 +0200 Subject: [PATCH] [libcalamares] Export network status as Q_PROPERTY and to QML --- src/libcalamares/network/Manager.cpp | 8 ++++++-- src/libcalamares/network/Manager.h | 25 ++++++++++++++++++------- src/libcalamaresui/utils/Qml.cpp | 5 +++++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/libcalamares/network/Manager.cpp b/src/libcalamares/network/Manager.cpp index d70988a0a..9d7534e99 100644 --- a/src/libcalamares/network/Manager.cpp +++ b/src/libcalamares/network/Manager.cpp @@ -1,5 +1,5 @@ /* === This file is part of Calamares - === - * + * * SPDX-FileCopyrightText: 2019 Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify @@ -168,7 +168,11 @@ Manager::checkHasInternet() { hasInternet = synchronousPing( d->m_hasInternetUrl ); } - d->m_hasInternet = hasInternet; + if ( hasInternet != d->m_hasInternet ) + { + d->m_hasInternet = hasInternet; + emit hasInternetChanged( hasInternet ); + } return hasInternet; } diff --git a/src/libcalamares/network/Manager.h b/src/libcalamares/network/Manager.h index 1ba3eb411..8673d340b 100644 --- a/src/libcalamares/network/Manager.h +++ b/src/libcalamares/network/Manager.h @@ -1,5 +1,5 @@ /* === This file is part of Calamares - === - * + * * SPDX-FileCopyrightText: 2019 Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify @@ -98,9 +98,10 @@ struct RequestStatus QDebug& operator<<( QDebug& s, const RequestStatus& e ); -class DLLEXPORT Manager : QObject +class DLLEXPORT Manager : public QObject { Q_OBJECT + Q_PROPERTY( bool hasInternet READ hasInternet NOTIFY hasInternetChanged FINAL ) Manager(); @@ -133,6 +134,16 @@ public: /// @brief Set the URL which is used for the general "is there internet" check. void setCheckHasInternetUrl( const QUrl& url ); + + /** @brief Do a network request asynchronously. + * + * Returns a pointer to the reply-from-the-request. + * This may be a nullptr if an error occurs immediately. + * The caller is responsible for cleaning up the reply (eventually). + */ + QNetworkReply* asynchronousGet( const QUrl& url, const RequestOptions& options = RequestOptions() ); + +public Q_SLOTS: /** @brief Do an explicit check for internet connectivity. * * This **may** do a ping to the configured check URL, but can also @@ -148,13 +159,13 @@ public: */ bool hasInternet(); - /** @brief Do a network request asynchronously. +signals: + /** @brief Indicates that internet connectivity status has changed * - * Returns a pointer to the reply-from-the-request. - * This may be a nullptr if an error occurs immediately. - * The caller is responsible for cleaning up the reply (eventually). + * The value is that returned from hasInternet() -- @c true when there + * is connectivity, @c false otherwise. */ - QNetworkReply* asynchronousGet( const QUrl& url, const RequestOptions& options = RequestOptions() ); + void hasInternetChanged( bool ); private: class Private; diff --git a/src/libcalamaresui/utils/Qml.cpp b/src/libcalamaresui/utils/Qml.cpp index 4f53aa317..1f1152fa2 100644 --- a/src/libcalamaresui/utils/Qml.cpp +++ b/src/libcalamaresui/utils/Qml.cpp @@ -23,6 +23,7 @@ #include "JobQueue.h" #include "Settings.h" #include "ViewManager.h" +#include "network/Manager.h" #include "utils/Dirs.h" #include "utils/Logger.h" @@ -242,6 +243,10 @@ registerQmlModels() "io.calamares.core", 1, 0, "Global", []( QQmlEngine*, QJSEngine* ) -> QObject* { return Calamares::JobQueue::instance()->globalStorage(); } ); + qmlRegisterSingletonType< CalamaresUtils::Network::Manager >( + "io.calamares.core", 1, 0, "Network", []( QQmlEngine*, QJSEngine* ) -> QObject* { + return &CalamaresUtils::Network::Manager::instance(); + } ); } }