[OpenDocString] kdeconnect-kde (cpp)
remotekeyboardplugin.cpp
RemoteKeyboardPlugin::RemoteKeyboardPlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
    , m_remoteState(false)
{
}
Constructs a KdeConnectPlugin object and sets the remote state to false.
RemoteKeyboardPlugin::~RemoteKeyboardPlugin()
{
}
This removes the keyboard plugin from the list of plugins.
bool RemoteKeyboardPlugin::receivePacket(const NetworkPacket &np)
{
    if (np.type() == PACKET_TYPE_MOUSEPAD_ECHO) {
        if (!np.has(QStringLiteral("isAck")) || !np.has(QStringLiteral("key"))) {
            qCWarning(KDECONNECT_PLUGIN_REMOTEKEYBOARD) << "Invalid packet of type" << PACKET_TYPE_MOUSEPAD_ECHO;
            return false;
        }
        //        qCWarning(KDECONNECT_PLUGIN_REMOTEKEYBOARD) << "Received keypress" << np;
        Q_EMIT keyPressReceived(np.get(QStringLiteral("key")),
                                np.get(QStringLiteral("specialKey"), 0),
                                np.get(QStringLiteral("shift"), false),
                                np.get(QStringLiteral("ctrl"), false),
                                np.get(QStringLiteral("alt"), 0));
        return true;
    } else if (np.type() == PACKET_TYPE_MOUSEPAD_KEYBOARDSTATE) {
        //        qCWarning(KDECONNECT_PLUGIN_REMOTEKEYBOARD) << "Received keyboardstate" << np;
        if (m_remoteState != np.get(QStringLiteral("state"))) {
            m_remoteState = np.get(QStringLiteral("state"));
            Q_EMIT remoteStateChanged(m_remoteState);
        }
        return true;
    }
    return false;
}
It receives a NetworkPacket object np and returns true if the packet is OK. If the packet is a MOUSEPAD_ECHO packet, the method obtains the key press value, the special key shift and alt values from the network, and emits a signal remoteStateChanged. If the packet is a MOUSEPAD_KEYBOARDSTATE packet, the method returns true.
void RemoteKeyboardPlugin::sendKeyPress(const QString &key, int specialKey, bool shift, bool ctrl, bool alt, bool sendAck) const
{
    NetworkPacket np(PACKET_TYPE_MOUSEPAD_REQUEST,
                     {{QStringLiteral("key"), key},
                      {QStringLiteral("specialKey"), specialKey},
                      {QStringLiteral("shift"), shift},
                      {QStringLiteral("ctrl"), ctrl},
                      {QStringLiteral("alt"), alt},
                      {QStringLiteral("sendAck"), sendAck}});
    sendPacket(np);
}
This sends a network packet to the remote host using a key.
void RemoteKeyboardPlugin::sendQKeyEvent(const QVariantMap &keyEvent, bool sendAck) const
{
    if (!keyEvent.contains(QStringLiteral("key")))
        return;
    int k = translateQtKey(keyEvent.value(QStringLiteral("key")).toInt());
    int modifiers = keyEvent.value(QStringLiteral("modifiers")).toInt();
    sendKeyPress(keyEvent.value(QStringLiteral("text")).toString(),
                 k,
                 modifiers & Qt::ShiftModifier,
                 modifiers & Qt::ControlModifier,
                 modifiers & Qt::AltModifier,
                 sendAck);
}
Translates a QString object to a QKey object, and sends it to the remote host.
int RemoteKeyboardPlugin::translateQtKey(int qtKey) const
{
    return specialKeysMap.value(qtKey, 0);
}
Returns the corresponding key code in the specialKeysMap.
void RemoteKeyboardPlugin::connected()
{
}
This implements the connected method of the RemoteKeyboardPlugin object.
QString RemoteKeyboardPlugin::dbusPath() const
{
    return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/remotekeyboard");
}
Returns the dbus path as a QString.