From d1493295dfaa429c3b607d1af647d4a8cc75eb92 Mon Sep 17 00:00:00 2001 From: David Edmundson Date: Thu, 17 Mar 2022 14:05:01 +0000 Subject: [PATCH] Simplify ScreenLockerWatcher ScreenLockerWatcher was written whislt overthinking DBus. An interface does not have to have the same lifespan as the service the other end. DBus connections works in terms of established "match rules", these can be set up before a service is created the other side and they remain if a service closes. The only part that needs doing is re-fetch any cached properties. There's also need to check the intial service names at startup. We can just let our call to GetActive fail. It's far faster. This class was getting convoluted in both ifdefs and Qt6 porting all to solve something that isn't real. There should be no behavioural differences. --- src/screenlockerwatcher.cpp | 84 +++++++------------------------------ src/screenlockerwatcher.h | 3 +- 2 files changed, 16 insertions(+), 71 deletions(-) diff --git a/src/screenlockerwatcher.cpp b/src/screenlockerwatcher.cpp index 214c19ae81..7932631ab7 100644 --- a/src/screenlockerwatcher.cpp +++ b/src/screenlockerwatcher.cpp @@ -9,8 +9,6 @@ #include "screenlockerwatcher.h" #include "wayland_server.h" -#include -#include // dbus generated #include "screenlocker_interface.h" #include "kscreenlocker_interface.h" @@ -43,85 +41,33 @@ void ScreenLockerWatcher::initialize() connect(m_serviceWatcher, &QDBusServiceWatcher::serviceOwnerChanged, this, &ScreenLockerWatcher::serviceOwnerChanged); m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange); m_serviceWatcher->addWatchedService(SCREEN_LOCKER_SERVICE_NAME); - // check whether service is registered - QFutureWatcher > *watcher = new QFutureWatcher >(this); - connect(watcher, &QFutureWatcher>::finished, - this, &ScreenLockerWatcher::serviceRegisteredQueried); - connect(watcher, &QFutureWatcher>::canceled, - watcher, &QFutureWatcher>::deleteLater); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - watcher->setFuture(QtConcurrent::run(QDBusConnection::sessionBus().interface(), - &QDBusConnectionInterface::isServiceRegistered, - SCREEN_LOCKER_SERVICE_NAME)); -#else - watcher->setFuture(QtConcurrent::run(&QDBusConnectionInterface::isServiceRegistered, - QDBusConnection::sessionBus().interface(), - SCREEN_LOCKER_SERVICE_NAME)); -#endif + + m_interface = new OrgFreedesktopScreenSaverInterface(SCREEN_LOCKER_SERVICE_NAME, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this); + m_kdeInterface = new OrgKdeScreensaverInterface(SCREEN_LOCKER_SERVICE_NAME, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this); + connect(m_interface, &OrgFreedesktopScreenSaverInterface::ActiveChanged, + this, &ScreenLockerWatcher::setLocked); + connect(m_kdeInterface, &OrgKdeScreensaverInterface::AboutToLock, this, &ScreenLockerWatcher::aboutToLock); + + queryActive(); } void ScreenLockerWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner) { Q_UNUSED(oldOwner) - if (serviceName != SCREEN_LOCKER_SERVICE_NAME) { - return; - } - delete m_interface; - m_interface = nullptr; - delete m_kdeInterface; - m_kdeInterface = nullptr; + Q_UNUSED(serviceName) m_locked = false; + if (!newOwner.isEmpty()) { - m_interface = new OrgFreedesktopScreenSaverInterface(newOwner, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this); - m_kdeInterface = new OrgKdeScreensaverInterface(newOwner, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this); - connect(m_interface, &OrgFreedesktopScreenSaverInterface::ActiveChanged, - this, &ScreenLockerWatcher::setLocked); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(m_interface->GetActive(), this); - connect(watcher, &QDBusPendingCallWatcher::finished, - this, &ScreenLockerWatcher::activeQueried); - connect(m_kdeInterface, &OrgKdeScreensaverInterface::AboutToLock, this, &ScreenLockerWatcher::aboutToLock); + queryActive(); } } -void ScreenLockerWatcher::serviceRegisteredQueried() +void ScreenLockerWatcher::queryActive() { - QFutureWatcher > *watcher = dynamic_cast > *>(sender()); - if (!watcher) { - return; - } - const QDBusReply &reply = watcher->result(); - if (reply.isValid() && reply.value()) { - QFutureWatcher > *ownerWatcher = new QFutureWatcher >(this); - connect(ownerWatcher, &QFutureWatcher>::finished, - this, &ScreenLockerWatcher::serviceOwnerQueried); - connect(ownerWatcher, &QFutureWatcher>::canceled, - ownerWatcher, &QFutureWatcher>::deleteLater); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - ownerWatcher->setFuture(QtConcurrent::run(QDBusConnection::sessionBus().interface(), - &QDBusConnectionInterface::serviceOwner, - SCREEN_LOCKER_SERVICE_NAME)); -#else - ownerWatcher->setFuture(QtConcurrent::run(&QDBusConnectionInterface::serviceOwner, - QDBusConnection::sessionBus().interface(), - SCREEN_LOCKER_SERVICE_NAME)); -#endif - } - watcher->deleteLater(); -} - -void ScreenLockerWatcher::serviceOwnerQueried() -{ - QFutureWatcher > *watcher = dynamic_cast > *>(sender()); - if (!watcher) { - return; - } - const QDBusReply reply = watcher->result(); - if (reply.isValid()) { - serviceOwnerChanged(SCREEN_LOCKER_SERVICE_NAME, QString(), reply.value()); - } - - watcher->deleteLater(); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(m_interface->GetActive(), this); + connect(watcher, &QDBusPendingCallWatcher::finished, + this, &ScreenLockerWatcher::activeQueried); } void ScreenLockerWatcher::activeQueried(QDBusPendingCallWatcher *watcher) diff --git a/src/screenlockerwatcher.h b/src/screenlockerwatcher.h index 0c4f83c99a..158253a5a4 100644 --- a/src/screenlockerwatcher.h +++ b/src/screenlockerwatcher.h @@ -36,10 +36,9 @@ private Q_SLOTS: void setLocked(bool activated); void activeQueried(QDBusPendingCallWatcher *watcher); void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner); - void serviceRegisteredQueried(); - void serviceOwnerQueried(); private: void initialize(); + void queryActive(); OrgFreedesktopScreenSaverInterface *m_interface = nullptr; OrgKdeScreensaverInterface *m_kdeInterface = nullptr; QDBusServiceWatcher *m_serviceWatcher;