2015-07-22 02:37:34 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
|
2015-07-22 02:37:34 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2015-07-22 02:37:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lockdeviceplugin.h"
|
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
#include <KPluginFactory>
|
|
|
|
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "plugin_lock_debug.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QDebug>
|
2015-07-22 02:37:34 +01:00
|
|
|
|
2021-06-01 18:26:15 +01:00
|
|
|
#include <core/daemon.h>
|
2015-07-22 02:37:34 +01:00
|
|
|
#include <core/device.h>
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
2015-07-22 02:37:34 +01:00
|
|
|
|
2019-06-12 21:16:54 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(LockDevicePlugin, "kdeconnect_lockdevice.json")
|
2015-07-22 02:37:34 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
LockDevicePlugin::LockDevicePlugin(QObject *parent, const QVariantList &args)
|
2015-07-22 02:37:34 +01:00
|
|
|
: KdeConnectPlugin(parent, args)
|
|
|
|
, m_remoteLocked(false)
|
2020-08-19 23:47:29 +01:00
|
|
|
, 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/<sessionId> and not /org/freedesktop/login1/session/auto
|
|
|
|
, m_propertiesInterface(QStringLiteral("org.freedesktop.login1"), QString(), QDBusConnection::systemBus())
|
2015-07-22 02:37:34 +01:00
|
|
|
{
|
2020-08-19 23:47:29 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(&m_propertiesInterface,
|
|
|
|
&OrgFreedesktopDBusPropertiesInterface::PropertiesChanged,
|
|
|
|
this,
|
|
|
|
[this](const QString &interface, const QVariantMap &properties, QStringList invalidatedProperties) {
|
|
|
|
Q_UNUSED(invalidatedProperties);
|
2020-08-19 23:47:29 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (interface != QLatin1String("org.freedesktop.login1.Session")) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-19 23:47:29 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!properties.contains(QStringLiteral("LockedHint"))) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-19 23:47:29 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
m_localLocked = properties.value(QStringLiteral("LockedHint")).toBool();
|
|
|
|
sendState();
|
|
|
|
});
|
2020-08-19 23:47:29 +01:00
|
|
|
|
|
|
|
m_localLocked = m_login1Interface.lockedHint();
|
2015-07-22 02:37:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LockDevicePlugin::isLocked() const
|
|
|
|
{
|
|
|
|
return m_remoteLocked;
|
|
|
|
}
|
2020-08-19 23:47:29 +01:00
|
|
|
|
2015-07-22 02:37:34 +01:00
|
|
|
void LockDevicePlugin::setLocked(bool locked)
|
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("setLocked"), locked}});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-07-22 02:37:34 +01:00
|
|
|
}
|
|
|
|
|
2023-07-31 08:25:45 +01:00
|
|
|
void LockDevicePlugin::receivePacket(const NetworkPacket &np)
|
2015-07-22 02:37:34 +01:00
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("isLocked"))) {
|
|
|
|
bool locked = np.get<bool>(QStringLiteral("isLocked"));
|
2015-07-22 02:37:34 +01:00
|
|
|
if (m_remoteLocked != locked) {
|
|
|
|
m_remoteLocked = locked;
|
|
|
|
Q_EMIT lockedChanged(locked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:47:29 +01:00
|
|
|
if (np.has(QStringLiteral("requestLocked"))) {
|
|
|
|
sendState();
|
2015-07-22 02:37:34 +01:00
|
|
|
}
|
2020-08-19 23:47:29 +01:00
|
|
|
|
2021-06-01 18:26:15 +01:00
|
|
|
// Receiving result of setLocked
|
|
|
|
if (np.has(QStringLiteral("lockResult"))) {
|
|
|
|
bool lockSuccess = np.get<bool>(QStringLiteral("lockResult"));
|
|
|
|
if (lockSuccess) {
|
2022-09-10 22:23:52 +01:00
|
|
|
Daemon::instance()->sendSimpleNotification(QStringLiteral("remoteLockSuccess"),
|
|
|
|
device()->name(),
|
|
|
|
i18n("Remote lock successful"),
|
|
|
|
QStringLiteral("lock"));
|
2021-06-01 18:26:15 +01:00
|
|
|
} else {
|
2022-09-10 22:23:52 +01:00
|
|
|
Daemon::instance()->sendSimpleNotification(QStringLiteral("remoteLockFailure"),
|
|
|
|
device()->name(),
|
|
|
|
i18n("Remote lock failed"),
|
|
|
|
QStringLiteral("error"));
|
2021-06-01 18:26:15 +01:00
|
|
|
Daemon::instance()->reportError(device()->name(), i18n("Remote lock failed"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:47:29 +01:00
|
|
|
if (np.has(QStringLiteral("setLocked"))) {
|
|
|
|
const bool lock = np.get<bool>(QStringLiteral("setLocked"));
|
2021-06-01 18:26:15 +01:00
|
|
|
bool success = false;
|
2020-08-19 23:47:29 +01:00
|
|
|
if (lock) {
|
|
|
|
m_login1Interface.Lock();
|
2021-06-01 18:26:15 +01:00
|
|
|
success = m_login1Interface.lockedHint();
|
|
|
|
NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("lockResult"), success}});
|
|
|
|
sendPacket(np);
|
2022-09-10 22:23:52 +01:00
|
|
|
} else {
|
2020-08-19 23:47:29 +01:00
|
|
|
m_login1Interface.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
sendState();
|
2015-07-22 02:37:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:47:29 +01:00
|
|
|
void LockDevicePlugin::sendState()
|
2015-07-22 02:37:34 +01:00
|
|
|
{
|
2020-08-19 23:47:29 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("isLocked"), m_localLocked}});
|
|
|
|
sendPacket(np);
|
2015-07-22 02:37:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LockDevicePlugin::connected()
|
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("requestLocked"), QVariant()}});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-07-22 02:37:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString LockDevicePlugin::dbusPath() const
|
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/lockdevice");
|
2015-07-22 02:37:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "lockdeviceplugin.moc"
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_lockdeviceplugin.cpp"
|