Brainstorming: Detecting network state
jarolrod opened this issue · 6 comments
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 |
---|---|
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.
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.
Hi!
We have some limitations for QProcess in some platforms such as Android and iOS.
Have you tried the ping method?