File indexing completed on 2024-04-21 04:56:49

0001 /**
0002  * SPDX-FileCopyrightText: 2021 Piyush Aggarwal <piyushaggarwal002@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "lockdeviceplugin-win.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KPluginFactory>
0011 #include <QDebug>
0012 
0013 #include <core/daemon.h>
0014 #include <core/device.h>
0015 #include <dbushelper.h>
0016 
0017 #include <Windows.h>
0018 #pragma comment(lib, "user32.lib")
0019 
0020 K_PLUGIN_CLASS_WITH_JSON(LockDevicePlugin, "kdeconnect_lockdevice.json")
0021 
0022 LockDevicePlugin::LockDevicePlugin(QObject *parent, const QVariantList &args)
0023     : KdeConnectPlugin(parent, args)
0024     , m_remoteLocked(false)
0025 {
0026 }
0027 
0028 bool LockDevicePlugin::isLocked() const
0029 {
0030     return m_remoteLocked; // Windows doesn't support monitoring lock status, m_remoteLocked is never updated
0031 }
0032 
0033 void LockDevicePlugin::setLocked(bool locked)
0034 {
0035     NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("setLocked"), locked}});
0036     sendPacket(np);
0037 }
0038 
0039 void LockDevicePlugin::receivePacket(const NetworkPacket &np)
0040 {
0041     if (np.has(QStringLiteral("isLocked"))) {
0042         bool locked = np.get<bool>(QStringLiteral("isLocked"));
0043         if (m_remoteLocked != locked) {
0044             m_remoteLocked = locked;
0045             Q_EMIT lockedChanged(locked);
0046         }
0047     }
0048 
0049     if (np.has(QStringLiteral("requestLocked"))) {
0050         sendState();
0051     }
0052 
0053     // Receiving result of setLocked
0054     if (np.has(QStringLiteral("lockResult"))) {
0055         bool lockSuccess = np.get<bool>(QStringLiteral("lockResult"));
0056         if (lockSuccess) {
0057             Daemon::instance()->sendSimpleNotification(QStringLiteral("remoteLockSuccess"),
0058                                                        device()->name(),
0059                                                        i18n("Remote lock successful"),
0060                                                        QStringLiteral("error"));
0061         } else {
0062             Daemon::instance()->sendSimpleNotification(QStringLiteral("remoteLockFail"), device()->name(), i18n("Remote lock failed"), QStringLiteral("error"));
0063             Daemon::instance()->reportError(device()->name(), i18n("Remote lock failed"));
0064         }
0065     }
0066 
0067     if (np.has(QStringLiteral("setLocked"))) {
0068         const bool lock = np.get<bool>(QStringLiteral("setLocked"));
0069         bool success = false;
0070         if (lock) {
0071             success = LockWorkStation();
0072             NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("lockResult"), success}});
0073             sendPacket(np);
0074         }
0075 
0076         sendState();
0077     }
0078 }
0079 
0080 void LockDevicePlugin::sendState()
0081 {
0082     NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("isLocked"), m_localLocked}});
0083     sendPacket(np);
0084 }
0085 
0086 void LockDevicePlugin::connected()
0087 {
0088     NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("requestLocked"), QVariant()}});
0089     sendPacket(np);
0090 }
0091 
0092 QString LockDevicePlugin::dbusPath() const
0093 {
0094     return QLatin1String("/modules/kdeconnect/devices/%1/lockdevice").arg(device()->id());
0095 }
0096 
0097 #include "lockdeviceplugin-win.moc"
0098 #include "moc_lockdeviceplugin-win.cpp"