[OpenDocString] kdeconnect-kde (cpp)
runcommandplugin.cpp
RunCommandPlugin::RunCommandPlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
{
    connect(config(), &KdeConnectPluginConfig::configChanged, this, &RunCommandPlugin::configChanged);
}
Constructs a KdeConnectPlugin object and connects the configChanged signal to the configChanged signal.
RunCommandPlugin::~RunCommandPlugin()
{
}
This implements the functionality required to start a command plugin.
bool RunCommandPlugin::receivePacket(const NetworkPacket &np)
{
    if (np.get(QStringLiteral("requestCommandList"), false)) {
        sendConfig();
        return true;
    }

    if (np.has(QStringLiteral("key"))) {
        QJsonDocument commandsDocument = QJsonDocument::fromJson(config()->getByteArray(QStringLiteral("commands"), "{}"));
        QJsonObject commands = commandsDocument.object();
        QString key = np.get(QStringLiteral("key"));
        QJsonValue value = commands[key];
        if (value == QJsonValue::Undefined) {
            qCWarning(KDECONNECT_PLUGIN_RUNCOMMAND) << key << "is not a configured command";
        }
        const QJsonObject commandJson = value.toObject();
        qCInfo(KDECONNECT_PLUGIN_RUNCOMMAND) << "Running:" << COMMAND << ARGS << commandJson[QStringLiteral("command")].toString();
#ifdef Q_OS_WIN
        QProcess::startDetached(commandJson[QStringLiteral("command")].toString());
#else
        QProcess::startDetached(QStringLiteral(COMMAND), QStringList() << QStringLiteral(ARGS) << commandJson[QStringLiteral("command")].toString());
#endif
        return true;
    } else if (np.has(QStringLiteral("setup"))) {
        OpenConfig oc;
        oc.openConfiguration(device()->id(), QStringLiteral("kdeconnect_runcommand"));
    }

    return false;
}
This receives a NetworkPacket object np and processes it. It first checks if the packet is configured for a command, if it is it requests the command list from the configuration. Then it checks if the packet is configured for a command, if it is it starts the command using the QProcess::startDetached function. It returns true on the first successful attempt.
void RunCommandPlugin::connected()
{
    sendConfig();
}
This sends the configuration to the end of the process.
void RunCommandPlugin::sendConfig()
{
    QString commands = config()->getString(QStringLiteral("commands"), QStringLiteral("{}"));
    NetworkPacket np(PACKET_TYPE_RUNCOMMAND, {{QStringLiteral("commandList"), commands}});

#if KCMUTILS_VERSION >= QT_VERSION_CHECK(5, 45, 0)
    np.set(QStringLiteral("canAddCommand"), true);
#endif

    sendPacket(np);
}
This sends a network packet based on the current configuration.
void RunCommandPlugin::configChanged()
{
    sendConfig();
}
This sends the config change signal to the controller.