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.
This commit is contained in:
David Edmundson 2022-03-17 14:05:01 +00:00
parent 8feeda5c86
commit d1493295df
2 changed files with 16 additions and 71 deletions

View file

@ -9,8 +9,6 @@
#include "screenlockerwatcher.h" #include "screenlockerwatcher.h"
#include "wayland_server.h" #include "wayland_server.h"
#include <QFutureWatcher>
#include <QtConcurrentRun>
// dbus generated // dbus generated
#include "screenlocker_interface.h" #include "screenlocker_interface.h"
#include "kscreenlocker_interface.h" #include "kscreenlocker_interface.h"
@ -43,85 +41,33 @@ void ScreenLockerWatcher::initialize()
connect(m_serviceWatcher, &QDBusServiceWatcher::serviceOwnerChanged, this, &ScreenLockerWatcher::serviceOwnerChanged); connect(m_serviceWatcher, &QDBusServiceWatcher::serviceOwnerChanged, this, &ScreenLockerWatcher::serviceOwnerChanged);
m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange); m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange);
m_serviceWatcher->addWatchedService(SCREEN_LOCKER_SERVICE_NAME); m_serviceWatcher->addWatchedService(SCREEN_LOCKER_SERVICE_NAME);
// check whether service is registered
QFutureWatcher<QDBusReply<bool> > *watcher = new QFutureWatcher<QDBusReply<bool> >(this); m_interface = new OrgFreedesktopScreenSaverInterface(SCREEN_LOCKER_SERVICE_NAME, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this);
connect(watcher, &QFutureWatcher<QDBusReply<bool>>::finished, m_kdeInterface = new OrgKdeScreensaverInterface(SCREEN_LOCKER_SERVICE_NAME, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this);
this, &ScreenLockerWatcher::serviceRegisteredQueried); connect(m_interface, &OrgFreedesktopScreenSaverInterface::ActiveChanged,
connect(watcher, &QFutureWatcher<QDBusReply<bool>>::canceled, this, &ScreenLockerWatcher::setLocked);
watcher, &QFutureWatcher<QDBusReply<bool>>::deleteLater); connect(m_kdeInterface, &OrgKdeScreensaverInterface::AboutToLock, this, &ScreenLockerWatcher::aboutToLock);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
watcher->setFuture(QtConcurrent::run(QDBusConnection::sessionBus().interface(), queryActive();
&QDBusConnectionInterface::isServiceRegistered,
SCREEN_LOCKER_SERVICE_NAME));
#else
watcher->setFuture(QtConcurrent::run(&QDBusConnectionInterface::isServiceRegistered,
QDBusConnection::sessionBus().interface(),
SCREEN_LOCKER_SERVICE_NAME));
#endif
} }
void ScreenLockerWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner) void ScreenLockerWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
{ {
Q_UNUSED(oldOwner) Q_UNUSED(oldOwner)
if (serviceName != SCREEN_LOCKER_SERVICE_NAME) { Q_UNUSED(serviceName)
return;
}
delete m_interface;
m_interface = nullptr;
delete m_kdeInterface;
m_kdeInterface = nullptr;
m_locked = false; m_locked = false;
if (!newOwner.isEmpty()) { if (!newOwner.isEmpty()) {
m_interface = new OrgFreedesktopScreenSaverInterface(newOwner, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this); queryActive();
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);
} }
} }
void ScreenLockerWatcher::serviceRegisteredQueried() void ScreenLockerWatcher::queryActive()
{ {
QFutureWatcher<QDBusReply<bool> > *watcher = dynamic_cast<QFutureWatcher<QDBusReply<bool> > *>(sender()); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(m_interface->GetActive(), this);
if (!watcher) { connect(watcher, &QDBusPendingCallWatcher::finished,
return; this, &ScreenLockerWatcher::activeQueried);
}
const QDBusReply<bool> &reply = watcher->result();
if (reply.isValid() && reply.value()) {
QFutureWatcher<QDBusReply<QString> > *ownerWatcher = new QFutureWatcher<QDBusReply<QString> >(this);
connect(ownerWatcher, &QFutureWatcher<QDBusReply<QString>>::finished,
this, &ScreenLockerWatcher::serviceOwnerQueried);
connect(ownerWatcher, &QFutureWatcher<QDBusReply<QString>>::canceled,
ownerWatcher, &QFutureWatcher<QDBusReply<QString>>::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<QDBusReply<QString> > *watcher = dynamic_cast<QFutureWatcher<QDBusReply<QString> > *>(sender());
if (!watcher) {
return;
}
const QDBusReply<QString> reply = watcher->result();
if (reply.isValid()) {
serviceOwnerChanged(SCREEN_LOCKER_SERVICE_NAME, QString(), reply.value());
}
watcher->deleteLater();
} }
void ScreenLockerWatcher::activeQueried(QDBusPendingCallWatcher *watcher) void ScreenLockerWatcher::activeQueried(QDBusPendingCallWatcher *watcher)

View file

@ -36,10 +36,9 @@ private Q_SLOTS:
void setLocked(bool activated); void setLocked(bool activated);
void activeQueried(QDBusPendingCallWatcher *watcher); void activeQueried(QDBusPendingCallWatcher *watcher);
void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner); void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner);
void serviceRegisteredQueried();
void serviceOwnerQueried();
private: private:
void initialize(); void initialize();
void queryActive();
OrgFreedesktopScreenSaverInterface *m_interface = nullptr; OrgFreedesktopScreenSaverInterface *m_interface = nullptr;
OrgKdeScreensaverInterface *m_kdeInterface = nullptr; OrgKdeScreensaverInterface *m_kdeInterface = nullptr;
QDBusServiceWatcher *m_serviceWatcher; QDBusServiceWatcher *m_serviceWatcher;