[OpenDocString] kdeconnect-kde (cpp)
clipboardplugin.cpp
ClipboardPlugin::ClipboardPlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
{
    connect(ClipboardListener::instance(), &ClipboardListener::clipboardChanged, this, &ClipboardPlugin::propagateClipboard);
}
This constructor builds a KdeConnectPlugin object and connects the clipboard changed signal to the listener.
void ClipboardPlugin::connected()
{
    sendConnectPacket();
}
This sends a connect packet to the plugin.
void ClipboardPlugin::propagateClipboard(const QString &content, ClipboardListener::ClipboardContentType contentType)
{
    if (contentType == ClipboardListener::ClipboardContentTypeUnknown) {
        if (!config()->getBool(QStringLiteral("sendUnknown"), true)) {
            return;
        }
    } else if (contentType == ClipboardListener::ClipboardContentTypePassword) {
        if (!config()->getBool(QStringLiteral("sendPassword"), true)) {
            return;
        }
    } else {
        return;
    }

    NetworkPacket np(PACKET_TYPE_CLIPBOARD, {{QStringLiteral("content"), content}});
    sendPacket(np);
}
This sends a clipboard message of the given content type to the network if the config is enabled.
void ClipboardPlugin::sendConnectPacket()
{
    NetworkPacket np(PACKET_TYPE_CLIPBOARD_CONNECT,
                     {{QStringLiteral("content"), ClipboardListener::instance()->currentContent()},
                      {QStringLiteral("timestamp"), ClipboardListener::instance()->updateTimestamp()}});
    sendPacket(np);
}
This sends a network packet that represents the connect to the clipboard. It uses the ClipboardListener singleton instance to update the timestamp.
bool ClipboardPlugin::receivePacket(const NetworkPacket &np)
{
    QString content = np.get(QStringLiteral("content"));
    if (np.type() == PACKET_TYPE_CLIPBOARD) {
        ClipboardListener::instance()->setText(content);
        return true;
    } else if (np.type() == PACKET_TYPE_CLIPBOARD_CONNECT) {
        qint64 packetTime = np.get(QStringLiteral("timestamp"));
        // If the packetTime is 0, it means the timestamp is unknown (so do nothing).
        if (packetTime == 0 || packetTime < ClipboardListener::instance()->updateTimestamp()) {
            return false;
        }

        ClipboardListener::instance()->setText(content);
        return true;
    }
    return false;
}
It receives a NetworkPacket object np and sets the text content of the plugin to the clipboard listener. It returns true on the first successful attempt. If the packet is not a CLIPBOARD or CONNECT, it returns false.