Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 0158097a authored by Daniel Vrátil's avatar Daniel Vrátil 🤖

Use NetworkManager to detect online status

Unliked Qt's NetworkConfiguration, NetworkManager gives us additional
information about connectivity (e.g. if we are behind a capture portal).
We only use QNetworkConfiguration as a fallback when NM is not available.
parent 091f8d5d
......@@ -67,6 +67,7 @@ find_package(KF5WidgetsAddons ${KF5_VERSION} CONFIG REQUIRED)
find_package(KF5WindowSystem ${KF5_VERSION} CONFIG REQUIRED)
find_package(KF5XmlGui ${KF5_VERSION} CONFIG REQUIRED)
find_package(KF5Crash ${KF5_VERSION} CONFIG REQUIRED)
find_package(KF5NetworkManagerQt ${KF5_VERSION} CONFIG REQUIRED)
find_package(Qt5Designer NO_MODULE)
set_package_properties(Qt5Designer PROPERTIES
......
......@@ -60,6 +60,7 @@ PRIVATE
KF5::AkonadiPrivate
KF5::DBusAddons
KF5::I18n
KF5::NetworkManagerQt
Qt5::Network
)
......
......@@ -46,10 +46,12 @@
#include <QSettings>
#include <QTimer>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QApplication>
#include <QCommandLineParser>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <NetworkManagerQt/NetworkManagerQt/Manager>
#include <signal.h>
#include <stdlib.h>
......@@ -834,10 +836,25 @@ void AgentBasePrivate::slotNetworkStatusChange(bool isOnline)
q->setOnlineInternal(mDesiredOnlineState);
}
void AgentBasePrivate::slotConnectivityChanged(NetworkManager::Connectivity connectivity)
{
Q_Q(AgentBase);
qCDebug(AKONADIAGENTBASE_LOG) << "NM Connectivity changed:" << connectivity;
if (connectivity == NetworkManager::Full) {
q->setOnlineInternal(mDesiredOnlineState);
} else {
q->setOnlineInternal(false);
}
}
void AgentBasePrivate::slotResumedFromSuspend()
{
if (mNeedsNetwork) {
slotNetworkStatusChange(mNetworkManager->isOnline());
if (mNetworkManager) {
slotNetworkStatusChange(mNetworkManager->isOnline());
} else {
slotConnectivityChanged(NetworkManager::connectivity());
}
}
}
......@@ -999,15 +1016,31 @@ void AgentBase::setNeedsNetwork(bool needsNetwork)
d->mNeedsNetwork = needsNetwork;
if (d->mNeedsNetwork) {
d->mNetworkManager = new QNetworkConfigurationManager(this);
connect(d->mNetworkManager, SIGNAL(onlineStateChanged(bool)),
this, SLOT(slotNetworkStatusChange(bool)),
Qt::UniqueConnection);
if (d->mNeedsNetwork) {
if (QDBusConnection::systemBus().interface()->isServiceRegistered(QStringLiteral("org.freedesktop.NetworkManager"))) {
connect(NetworkManager::notifier(), SIGNAL(connectivityChanged(NetworkManager::Connectivity)),
this, SLOT(slotConnectivityChanged(NetworkManager::Connectivity)),
Qt::UniqueConnection);
if (d->mDesiredOnlineState && NetworkManager::connectivity() == NetworkManager::Full) {
setOnlineInternal(d->mDesiredOnlineState);
}
} else {
d->mNetworkManager = new QNetworkConfigurationManager(this);
connect(d->mNetworkManager, SIGNAL(onlineStateChanged(bool)),
this, SLOT(slotNetworkStatusChange(bool)));
if (d->mDesiredOnlineState && d->mNetworkManager->isOnline()) {
setOnlineInternal(d->mDesiredOnlineState);
}
}
} else {
delete d->mNetworkManager;
d->mNetworkManager = nullptr;
if (d->mNetworkManager) {
delete d->mNetworkManager;
d->mNetworkManager = nullptr;
} else {
disconnect(NetworkManager::notifier(), SIGNAL(connectivityChanged(NetworkManager::Connectivity)),
this, SLOT(slotConnectivityChanged(NetworkManager::Connectivity)));
}
setOnlineInternal(d->mDesiredOnlineState);
}
}
......@@ -1044,7 +1077,8 @@ void AgentBase::setOnlineInternal(bool state)
{
Q_D(AgentBase);
if (state && d->mNeedsNetwork) {
if (!d->mNetworkManager->isOnline()) {
if ((d->mNetworkManager && !d->mNetworkManager->isOnline())
|| (NetworkManager::connectivity() != NetworkManager::Full)) {
//Don't go online if the resource needs network but there is none
state = false;
}
......
......@@ -790,6 +790,7 @@ private:
Q_PRIVATE_SLOT(d_func(), void slotNetworkStatusChange(bool))
Q_PRIVATE_SLOT(d_func(), void slotResumedFromSuspend())
Q_PRIVATE_SLOT(d_func(), void slotTemporaryOfflineTimeout())
Q_PRIVATE_SLOT(d_func(), void slotConnectivityChanged(NetworkManager::Connectivity))
//@endcond
};
......
......@@ -24,6 +24,8 @@
#include "agentbase.h"
#include "tracerinterface.h"
#include <NetworkManagerQt/Manager>
#include <klocalizedstring.h>
class QSettings;
......@@ -54,6 +56,7 @@ public:
void slotNetworkStatusChange(bool isOnline);
void slotResumedFromSuspend();
void slotTemporaryOfflineTimeout();
void slotConnectivityChanged(NetworkManager::Connectivity connectivity);
virtual void changeProcessed();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment