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;