Commit 5b3f1251 authored by Nicolas Fella's avatar Nicolas Fella
Browse files

[handler] Replace QDBusPendingCallWatcher with coroutines

Manipulating connections is done using async DBus calls. Currenly we use QDBusPendingCallWatcher to get notified when those finish

All calls trigger the same slot, which them determines what happens based on untyped data that was passed along.

This makes the code quite hard to follow.

By using coroutines we can write the code in a manner that makes the calls appear as if they were sychronous. This allows to reduce some boilerplate and break up the giant slot and move the relevant code to where it logically belongs

That way the code becomes a lot more easy to follow.

This makes use of the QCoro library (https://github.com/danvratil/qcoro) to add coroutine support for QDBusPendingCallWatcher
parent 1dfc47da
Pipeline #270204 passed with stage
in 2 minutes and 13 seconds
......@@ -9,7 +9,7 @@ set(QT_MIN_VERSION "5.15.2")
set(KF5_MIN_VERSION "5.98.0")
set(KDE_COMPILERSETTINGS_LEVEL "5.82")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
################# set KDE specific information #################
......@@ -83,6 +83,10 @@ pkg_check_modules(NETWORKMANAGER IMPORTED_TARGET libnm>1.4.0 REQUIRED)
pkg_check_modules(MOBILEBROADBANDPROVIDERINFO mobile-broadband-provider-info)
pkg_get_variable(BROADBANDPROVIDER_DATABASE mobile-broadband-provider-info database)
find_package(QCoro${QT_MAJOR_VERSION} REQUIRED COMPONENTS Core DBus)
qcoro_enable_coroutines()
add_definitions(
-DQT_DISABLE_DEPRECATED_BEFORE=0x050f00
-DQT_DEPRECATED_WARNINGS_SINCE=0x060000
......
......@@ -7,8 +7,7 @@
#include "connectivitymonitor.h"
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QDBusReply>
#include <QDesktopServices>
#include <KIO/OpenUrlJob>
......@@ -16,6 +15,9 @@
#include <NetworkManagerQt/ActiveConnection>
#include <QCoroCore>
#include <QCoroDBus>
#include "plasma_nm_kded.h"
#include <chrono>
......@@ -93,15 +95,11 @@ void ConnectivityMonitor::showLimitedConnectivityNotification()
m_notification->sendEvent();
}
void ConnectivityMonitor::checkConnectivity()
QCoro::Task<void> ConnectivityMonitor::checkConnectivity()
{
QDBusPendingReply<uint> pendingReply = NetworkManager::checkConnectivity();
auto callWatcher = new QDBusPendingCallWatcher(pendingReply);
connect(callWatcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<uint> reply = *watcher;
if (reply.isValid()) {
connectivityChanged((NetworkManager::Connectivity)reply.value());
}
watcher->deleteLater();
});
QDBusReply<uint> reply = co_await NetworkManager::checkConnectivity();
if (reply.isValid()) {
connectivityChanged((NetworkManager::Connectivity)reply.value());
}
}
......@@ -15,6 +15,8 @@
#include <QPointer>
#include <QTimer>
#include <QCoroCore>
class ConnectivityMonitor : public QObject
{
Q_OBJECT
......@@ -24,7 +26,7 @@ public:
private Q_SLOTS:
void connectivityChanged(NetworkManager::Connectivity connectivity);
void checkConnectivity();
QCoro::Task<void> checkConnectivity();
private:
void showLimitedConnectivityNotification();
......
......@@ -35,12 +35,14 @@ PUBLIC
KF5::NetworkManagerQt
plasmanm_editor
PkgConfig::NETWORKMANAGER
QCoro::Core
PRIVATE
KF5::I18n
KF5::Notifications
KF5::Service
KF5::Wallet
KF5::WindowSystem
QCoro::DBus
)
target_link_libraries(plasmanm_internal PUBLIC KF5::ModemManagerQt)
......
......@@ -19,6 +19,8 @@
#include <ModemManagerQt/Manager>
#include <QCoroDBus>
ConnectionIcon::ConnectionIcon(QObject *parent)
: QObject(parent)
, m_wirelessNetwork(nullptr)
......@@ -59,19 +61,20 @@ ConnectionIcon::ConnectionIcon(QObject *parent)
setIcons();
QDBusPendingReply<uint> pendingReply = NetworkManager::checkConnectivity();
auto callWatcher = new QDBusPendingCallWatcher(pendingReply);
connect(callWatcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<uint> reply = *watcher;
if (reply.isValid()) {
connectivityChanged((NetworkManager::Connectivity)reply.value());
}
watcher->deleteLater();
});
checkConnectivity();
}
ConnectionIcon::~ConnectionIcon() = default;
QCoro::Task<void> ConnectionIcon::checkConnectivity()
{
QDBusReply<uint> reply = co_await NetworkManager::checkConnectivity();
if (reply.isValid()) {
connectivityChanged((NetworkManager::Connectivity)reply.value());
}
}
bool ConnectionIcon::connecting() const
{
return m_connecting;
......
......@@ -13,6 +13,8 @@
#include <NetworkManagerQt/VpnConnection>
#include <NetworkManagerQt/WirelessNetwork>
#include <QCoroCore>
class ConnectionIcon : public QObject
{
Q_PROPERTY(bool connecting READ connecting NOTIFY connectingChanged)
......@@ -61,6 +63,7 @@ Q_SIGNALS:
void needsPortalChanged(bool needsPortal);
private:
QCoro::Task<void> checkConnectivity();
void addActiveConnection(const QString &activeConnection);
void setConnecting(bool connecting);
void setConnectionIcon(const QString &icon);
......
This diff is collapsed.
......@@ -16,22 +16,13 @@
#include <NetworkManagerQt/Settings>
#include <NetworkManagerQt/Utils>
#include <QCoroCore>
class Q_DECL_EXPORT Handler : public QObject
{
Q_OBJECT
public:
enum HandlerAction {
ActivateConnection,
AddAndActivateConnection,
AddConnection,
DeactivateConnection,
RemoveConnection,
RequestScan,
UpdateConnection,
CreateHotspot,
};
explicit Handler(QObject *parent = nullptr);
~Handler() override;
......@@ -86,7 +77,7 @@ public Q_SLOTS:
* Adds a new connection
* @map - NMVariantMapMap with connection settings
*/
void addConnection(const NMVariantMapMap &map);
QCoro::Task<void> addConnection(const NMVariantMapMap &map);
/**
* Deactivates given connection
* @connection - d-bus path of the connection you want to deactivate
......@@ -113,7 +104,7 @@ public Q_SLOTS:
* @connection - connection which should be updated
* @map - NMVariantMapMap with new connection settings
*/
void updateConnection(const NetworkManager::Connection::Ptr &connection, const NMVariantMapMap &map);
QCoro::Task<void> updateConnection(NetworkManager::Connection::Ptr connection, const NMVariantMapMap &map);
void requestScan(const QString &interface = QString());
void createHotspot();
......@@ -121,8 +112,6 @@ public Q_SLOTS:
private Q_SLOTS:
void secretAgentError(const QString &connectionPath, const QString &message);
void replyFinished(QDBusPendingCallWatcher *watcher);
void hotspotCreated(QDBusPendingCallWatcher *watcher);
void primaryConnectionTypeChanged(NetworkManager::ConnectionSettings::ConnectionType type);
void unlockRequiredChanged(MMModemLock modemLock);
......@@ -134,7 +123,13 @@ Q_SIGNALS:
void scanningChanged();
private:
void addAndActivateConnectionDBus(const NMVariantMapMap &map, const QString &device, const QString &specificObject);
QCoro::Task<void> addAndActivateConnectionDBus(const NMVariantMapMap &map, const QString &device, const QString &specificObject);
QCoro::Task<void> activateConnectionInternal(const QString &connection, const QString &device, const QString &specificParameter);
QCoro::Task<void> addAndActivateConnectionInternal(const QString &device, const QString &specificParameter, const QString &password = QString());
QCoro::Task<void> deactivateConnectionInternal(const QString &connection, const QString &device);
QCoro::Task<void> removeConnectionInternal(const QString &connection);
QCoro::Task<void> requestScanInternal(const QString &interface = QString());
QCoro::Task<void> createHotspotInternal();
bool m_hotspotSupported;
bool m_tmpWirelessEnabled;
......@@ -147,7 +142,7 @@ private:
QMap<QString, QTimer *> m_wirelessScanRetryTimer;
short m_ongoingScansCount = 0;
void enableBluetooth(bool enable);
QCoro::Task<void> enableBluetooth(bool enable);
void scanRequestFailed(const QString &interface);
bool checkRequestScanRateLimit(const NetworkManager::WirelessDevice::Ptr &wifiDevice);
bool checkHotspotSupported();
......
Supports Markdown
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