[OpenDocString] kdeconnect-kde (cpp)
lockdeviceplugin.cpp
LockDevicePlugin::LockDevicePlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
    , m_remoteLocked(false)
    , 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();
}
This constructor builds a KdeConnectPlugin object and connects to the logind interface and properties interface. It first connects to the logind interface and the properties interface, since the PropertiesChanged signal is only emitted. Then it sends the state change signal to the logind interface.
LockDevicePlugin::~LockDevicePlugin()
{
}
This implements locking for the device plugin.
bool LockDevicePlugin::isLocked() const
{
    return m_remoteLocked;
}
This implements checking if the device is locked.
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("lock"));
        } else {
            Daemon::instance()->sendSimpleNotification(QStringLiteral("remoteLockFailure"),
                                                       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) {
            m_login1Interface.Lock();
            success = m_login1Interface.lockedHint();
            NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("lockResult"), success}});
            sendPacket(np);
        } else {
            m_login1Interface.Unlock();
        }

        sendState();
    }

    return true;
}
This sends a network packet if it has the "isLocked" property. Then it checks if the remote device is locked. Then it checks if the request is locked, if it is successful, it sends a simple notification with the "lockSuccess" and "error" messages.
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.