[OpenDocString] kdeconnect-kde (cpp)
lockdeviceplugin-win.cpp
LockDevicePlugin::LockDevicePlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
    , m_remoteLocked(false)
{
}
Constructs a KdeConnectPlugin object and locks its remote device.
LockDevicePlugin::~LockDevicePlugin()
{
}
This implements locking for the device plugin.
bool LockDevicePlugin::isLocked() const
{
    return m_remoteLocked; // Windows doesn't support monitoring lock status, m_remoteLocked is never updated
}
This implements checking if the remote device is locked. It is locked on the local machine only and is never updated.
void LockDevicePlugin::setLocked(bool locked)
{
    NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("setLocked"), locked}});
    sendPacket(np);
}
This sends a lock request to the controller.
bool LockDevicePlugin::receivePacket(const NetworkPacket &np)
{
    if (np.has(QStringLiteral("isLocked"))) {
        bool locked = np.get(QStringLiteral("isLocked"));
        if (m_remoteLocked != locked) {
            m_remoteLocked = locked;
            Q_EMIT lockedChanged(locked);
        }
    }

    if (np.has(QStringLiteral("requestLocked"))) {
        sendState();
    }

    // Receiving result of setLocked
    if (np.has(QStringLiteral("lockResult"))) {
        bool lockSuccess = np.get(QStringLiteral("lockResult"));
        if (lockSuccess) {
            Daemon::instance()->sendSimpleNotification(QStringLiteral("remoteLockSuccess"),
                                                       device()->name(),
                                                       i18n("Remote lock successful"),
                                                       QStringLiteral("error"));
        } else {
            Daemon::instance()->sendSimpleNotification(QStringLiteral("remoteLockFail"), device()->name(), i18n("Remote lock failed"), QStringLiteral("error"));
            Daemon::instance()->reportError(device()->name(), i18n("Remote lock failed"));
        }
    }

    if (np.has(QStringLiteral("setLocked"))) {
        const bool lock = np.get(QStringLiteral("setLocked"));
        bool success = false;
        if (lock) {
            success = LockWorkStation();
            NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("lockResult"), success}});
            sendPacket(np);
        }

        sendState();
    }

    return true;
}
This sends a network packet if it is not locked. If the user has requested that the device is locked, the method obtains the lock result from the network and sets the state to locked. Then it sends the network packet and returns true.
void LockDevicePlugin::sendState()
{
    NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("isLocked"), m_localLocked}});
    sendPacket(np);
}
This sends a network packet that sets the locked state of the device to false.
void LockDevicePlugin::connected()
{
    NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("requestLocked"), QVariant()}});
    sendPacket(np);
}
This sends a lock request packet to the LockDevicePlugin.
QString LockDevicePlugin::dbusPath() const
{
    return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/lockdevice");
}
Returns the dbus path as a QString.