bitcoin-core / gui-qml

Bitcoin GUI (experimental QML-based fork)
MIT License
111 stars 40 forks source link

Brainstorming: Detecting network state #348

Closed jarolrod closed 1 week ago

jarolrod commented 1 year ago

We need to have the ability to detect network state to allow for states such as the following (specified in the design file):

Paused: No Wifi Peers: No Internet
Mobile Node - Peers

We have two options to go down through, at least with platforms aside from android.

1. Enable the Qt Network API

This gives us access to the QNetworkConfigurationManager class. Here we have access to the following signals and functions that give us the information we need:

It should be noted that with these functions, Qt checks that the computer is connected to any signal, and not if that signal really has an internet connection.

2. Use system specific terminal commands in a Process

For example, on macOS, this could be something like the following:


#include <QProcess>

class NetworkManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool isOnline READ isOnline NOTIFY isOnlineChanged)

public:
    explicit NetworkManager(QObject *parent = nullptr)
        : QObject(parent), m_isOnline(false)
    {
        checkNetworkAccess();
    }

    bool isOnline() const { return m_isOnline; }

public slots:
    void checkNetworkAccess() {
        QProcess process;
        process.start("scutil --nwi");
        process.waitForFinished();

        QString output = process.readAllStandardOutput();

        // check if output contains 'No network connection'
        m_isOnline = !output.contains("No network");

        emit isOnlineChanged(m_isOnline);
    }

signals:
    void isOnlineChanged(bool isOnline);

private:
    bool m_isOnline;
};

There should be available commands for macOS, linux, and Windows; but this approach won't work for Android, where we'll just plug into some android api for this information.

jarolrod commented 1 year ago

cc @johnny9 @hebasto @promag

jarolrod commented 1 year ago

related: https://github.com/bitcoin-core/gui-qml/issues/324

johnny9 commented 1 year ago

We can try out the QNetworkInterface class https://doc.qt.io/qt-5/qnetworkinterface.html with the QNetwork module. Qt 6.1 has QNetworkInformation https://doc.qt.io/qt-6/qnetworkinformation.html. I think we should avoid trying to implement platform specific network tests ourselves.

TheCompez commented 1 year ago

Hi!

We have some limitations for QProcess in some platforms such as Android and iOS. Have you tried the ping method?

jarolrod commented 1 week ago

We don't need Qt API or terminal commands, we can use platform specific api's that don't ping any server to know if the device has internet access

jarolrod commented 1 week ago

Closing as brainstorming is not needed, and we can move discussion into https://github.com/bitcoin-core/gui-qml/issues/324 and subsequent designs on the no internet access state