diff --git a/plugins/lockdevice/CMakeLists.txt b/plugins/lockdevice/CMakeLists.txt index a6c25076b5a932803b1c17d8daa59430e180c34a..61ac67e2ed2e61d815c49100d23198973111e471 100644 --- a/plugins/lockdevice/CMakeLists.txt +++ b/plugins/lockdevice/CMakeLists.txt @@ -1,4 +1,5 @@ -qt5_add_dbus_interface(lockdevice_SRCS org.freedesktop.ScreenSaver.xml screensaverdbusinterface) +qt5_add_dbus_interface(lockdevice_SRCS org.freedesktop.login1.xml login1dbusinterface) +qt5_add_dbus_interface(lockdevice_SRCS org.freedesktop.DBus.Properties.xml propertiesdbusinterface) set(debug_file_SRCS) ecm_qt_declare_logging_category( diff --git a/plugins/lockdevice/lockdeviceplugin.cpp b/plugins/lockdevice/lockdeviceplugin.cpp index aa9d0b4b356a0df9f61619daf51caed329e1ccbc..d24a9e3904aa8afd46772b457d1f7bda8efe9012 100644 --- a/plugins/lockdevice/lockdeviceplugin.cpp +++ b/plugins/lockdevice/lockdeviceplugin.cpp @@ -10,7 +10,6 @@ #include #include -#include "screensaverdbusinterface.h" #include "plugin_lock_debug.h" #include @@ -21,19 +20,48 @@ K_PLUGIN_CLASS_WITH_JSON(LockDevicePlugin, "kdeconnect_lockdevice.json") LockDevicePlugin::LockDevicePlugin(QObject* parent, const QVariantList& args) : KdeConnectPlugin(parent, args) , m_remoteLocked(false) - , m_iface(nullptr) + , m_login1Interface(QStringLiteral("org.freedesktop.login1"), QStringLiteral("/org/freedesktop/login1/session/auto"), QDBusConnection::systemBus()) + // Connect on all paths since the PropertiesChanged signal is only emitted + // from /org/freedesktop/login1/session/ and not /org/freedesktop/login1/session/auto + , m_propertiesInterface(QStringLiteral("org.freedesktop.login1"), QString(), QDBusConnection::systemBus()) { + + if (!m_login1Interface.isValid()) { + qCWarning(KDECONNECT_PLUGIN_LOCKREMOTE) << "Could not connect to logind interface" << m_login1Interface.lastError(); + } + + if (!m_propertiesInterface.isValid()) { + qCWarning(KDECONNECT_PLUGIN_LOCKREMOTE) << "Could not connect to logind properties interface" << m_propertiesInterface.lastError(); + } + + connect(&m_propertiesInterface, &OrgFreedesktopDBusPropertiesInterface::PropertiesChanged, this, [this](const QString& interface, const QVariantMap& properties, QStringList invalidatedProperties ) { + + Q_UNUSED(invalidatedProperties); + + if (interface != QLatin1String("org.freedesktop.login1.Session")) { + return; + } + + if (!properties.contains(QStringLiteral("LockedHint"))) { + return; + } + + m_localLocked = properties.value(QStringLiteral("LockedHint")).toBool(); + sendState(); + }); + + m_localLocked = m_login1Interface.lockedHint(); } LockDevicePlugin::~LockDevicePlugin() { - delete m_iface; } bool LockDevicePlugin::isLocked() const { return m_remoteLocked; } + void LockDevicePlugin::setLocked(bool locked) { NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("setLocked"), locked}}); @@ -50,27 +78,29 @@ bool LockDevicePlugin::receivePacket(const NetworkPacket & np) } } - bool sendState = np.has(QStringLiteral("requestLocked")); - if (np.has(QStringLiteral("setLocked"))) { - iface()->SetActive(np.get(QStringLiteral("setLocked"))); - sendState = true; + if (np.has(QStringLiteral("requestLocked"))) { + sendState(); } - if (sendState) { - NetworkPacket np(PACKET_TYPE_LOCK, QVariantMap {{QStringLiteral("isLocked"), QVariant::fromValue(iface()->GetActive())}}); - sendPacket(np); + + if (np.has(QStringLiteral("setLocked"))) { + const bool lock = np.get(QStringLiteral("setLocked")); + + if (lock) { + m_login1Interface.Lock(); + } else { + m_login1Interface.Unlock(); + } + + sendState(); } return true; } -OrgFreedesktopScreenSaverInterface* LockDevicePlugin::iface() +void LockDevicePlugin::sendState() { - if (!m_iface) { - m_iface = new OrgFreedesktopScreenSaverInterface(QStringLiteral("org.freedesktop.ScreenSaver"), QStringLiteral("/org/freedesktop/ScreenSaver"), DBusHelper::sessionBus()); - if(!m_iface->isValid()) - qCWarning(KDECONNECT_PLUGIN_LOCKREMOTE) << "Couldn't connect to the ScreenSaver interface"; - } - return m_iface; + NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("isLocked"), m_localLocked}}); + sendPacket(np); } void LockDevicePlugin::connected() diff --git a/plugins/lockdevice/lockdeviceplugin.h b/plugins/lockdevice/lockdeviceplugin.h index 46fb799ffeeaca1ddbaa0a73bd12017a9958396a..d3b502e2be33cf3d7e05b9ad3ffc33e35797f61a 100644 --- a/plugins/lockdevice/lockdeviceplugin.h +++ b/plugins/lockdevice/lockdeviceplugin.h @@ -11,7 +11,8 @@ #include -class OrgFreedesktopScreenSaverInterface; +#include "login1dbusinterface.h" +#include "propertiesdbusinterface.h" #define PACKET_TYPE_LOCK QStringLiteral("kdeconnect.lock") #define PACKET_TYPE_LOCK_REQUEST QStringLiteral("kdeconnect.lock.request") @@ -35,14 +36,17 @@ public: bool receivePacket(const NetworkPacket & np) override; Q_SIGNALS: - void lockedChanged(bool locked); + Q_SCRIPTABLE void lockedChanged(bool locked); private: - bool m_remoteLocked; - OrgFreedesktopScreenSaverInterface* iface(); + void sendState(); + + bool m_remoteLocked; + bool m_localLocked = false; - OrgFreedesktopScreenSaverInterface* m_iface; + OrgFreedesktopLogin1SessionInterface m_login1Interface; + OrgFreedesktopDBusPropertiesInterface m_propertiesInterface; }; #endif diff --git a/plugins/lockdevice/org.freedesktop.DBus.Properties.xml b/plugins/lockdevice/org.freedesktop.DBus.Properties.xml new file mode 100644 index 0000000000000000000000000000000000000000..684cdc072abe1df6cb53bf0182c8e8838f454451 --- /dev/null +++ b/plugins/lockdevice/org.freedesktop.DBus.Properties.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/lockdevice/org.freedesktop.login1.xml b/plugins/lockdevice/org.freedesktop.login1.xml new file mode 100644 index 0000000000000000000000000000000000000000..a74eab7f67a7b51db45440827a77acf6b8a893dd --- /dev/null +++ b/plugins/lockdevice/org.freedesktop.login1.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + +