File indexing completed on 2024-04-14 04:51:46
0001 /** 0002 * SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "lockdeviceplugin.h" 0008 0009 #include <KLocalizedString> 0010 #include <KPluginFactory> 0011 0012 #include "plugin_lockdevice_debug.h" 0013 #include <QDebug> 0014 0015 #include <core/daemon.h> 0016 #include <core/device.h> 0017 #include <dbushelper.h> 0018 0019 K_PLUGIN_CLASS_WITH_JSON(LockDevicePlugin, "kdeconnect_lockdevice.json") 0020 0021 LockDevicePlugin::LockDevicePlugin(QObject *parent, const QVariantList &args) 0022 : KdeConnectPlugin(parent, args) 0023 , m_remoteLocked(false) 0024 , m_login1Interface(QStringLiteral("org.freedesktop.login1"), QStringLiteral("/org/freedesktop/login1/session/auto"), QDBusConnection::systemBus()) 0025 // Connect on all paths since the PropertiesChanged signal is only emitted 0026 // from /org/freedesktop/login1/session/<sessionId> and not /org/freedesktop/login1/session/auto 0027 , m_propertiesInterface(QStringLiteral("org.freedesktop.login1"), QString(), QDBusConnection::systemBus()) 0028 { 0029 if (!m_login1Interface.isValid()) { 0030 qCWarning(KDECONNECT_PLUGIN_LOCKDEVICE) << "Could not connect to logind interface" << m_login1Interface.lastError(); 0031 } 0032 0033 if (!m_propertiesInterface.isValid()) { 0034 qCWarning(KDECONNECT_PLUGIN_LOCKDEVICE) << "Could not connect to logind properties interface" << m_propertiesInterface.lastError(); 0035 } 0036 0037 connect(&m_propertiesInterface, 0038 &OrgFreedesktopDBusPropertiesInterface::PropertiesChanged, 0039 this, 0040 [this](const QString &interface, const QVariantMap &properties) { 0041 if (interface != QLatin1String("org.freedesktop.login1.Session")) { 0042 return; 0043 } 0044 0045 if (!properties.contains(QStringLiteral("LockedHint"))) { 0046 return; 0047 } 0048 0049 m_localLocked = properties.value(QStringLiteral("LockedHint")).toBool(); 0050 sendState(); 0051 }); 0052 0053 m_localLocked = m_login1Interface.lockedHint(); 0054 } 0055 0056 bool LockDevicePlugin::isLocked() const 0057 { 0058 return m_remoteLocked; 0059 } 0060 0061 void LockDevicePlugin::setLocked(bool locked) 0062 { 0063 NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("setLocked"), locked}}); 0064 sendPacket(np); 0065 } 0066 0067 void LockDevicePlugin::receivePacket(const NetworkPacket &np) 0068 { 0069 if (np.has(QStringLiteral("isLocked"))) { 0070 bool locked = np.get<bool>(QStringLiteral("isLocked")); 0071 if (m_remoteLocked != locked) { 0072 m_remoteLocked = locked; 0073 Q_EMIT lockedChanged(locked); 0074 } 0075 } 0076 0077 if (np.has(QStringLiteral("requestLocked"))) { 0078 sendState(); 0079 } 0080 0081 // Receiving result of setLocked 0082 if (np.has(QStringLiteral("lockResult"))) { 0083 bool lockSuccess = np.get<bool>(QStringLiteral("lockResult")); 0084 if (lockSuccess) { 0085 Daemon::instance()->sendSimpleNotification(QStringLiteral("remoteLockSuccess"), 0086 device()->name(), 0087 i18n("Remote lock successful"), 0088 QStringLiteral("lock")); 0089 } else { 0090 Daemon::instance()->sendSimpleNotification(QStringLiteral("remoteLockFailure"), 0091 device()->name(), 0092 i18n("Remote lock failed"), 0093 QStringLiteral("error")); 0094 Daemon::instance()->reportError(device()->name(), i18n("Remote lock failed")); 0095 } 0096 } 0097 0098 if (np.has(QStringLiteral("setLocked"))) { 0099 const bool lock = np.get<bool>(QStringLiteral("setLocked")); 0100 bool success = false; 0101 if (lock) { 0102 m_login1Interface.Lock(); 0103 success = m_login1Interface.lockedHint(); 0104 NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("lockResult"), success}}); 0105 sendPacket(np); 0106 } else { 0107 m_login1Interface.Unlock(); 0108 } 0109 0110 sendState(); 0111 } 0112 } 0113 0114 void LockDevicePlugin::sendState() 0115 { 0116 NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("isLocked"), m_localLocked}}); 0117 sendPacket(np); 0118 } 0119 0120 void LockDevicePlugin::connected() 0121 { 0122 NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("requestLocked"), QVariant()}}); 0123 sendPacket(np); 0124 } 0125 0126 QString LockDevicePlugin::dbusPath() const 0127 { 0128 return QLatin1String("/modules/kdeconnect/devices/%1/lockdevice").arg(device()->id()); 0129 } 0130 0131 #include "lockdeviceplugin.moc" 0132 #include "moc_lockdeviceplugin.cpp"