2015-09-12 11:38:44 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2015-09-12 11:38:44 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2015-09-12 11:38:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "runcommandplugin.h"
|
|
|
|
|
|
|
|
#include <KPluginFactory>
|
|
|
|
|
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QJsonDocument>
|
2020-03-01 20:00:40 +00:00
|
|
|
|
|
|
|
#ifdef SAILFISHOS
|
|
|
|
#define KCMUTILS_VERSION 0
|
|
|
|
#else
|
2015-09-12 12:45:02 +01:00
|
|
|
#include <KShell>
|
2018-05-10 17:36:06 +01:00
|
|
|
#include <kcmutils_version.h>
|
2020-03-01 20:00:40 +00:00
|
|
|
#endif
|
2015-09-12 11:38:44 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
#include <core/networkpacket.h>
|
2015-09-12 11:38:44 +01:00
|
|
|
#include <core/device.h>
|
2020-08-22 15:12:46 +01:00
|
|
|
#include <core/daemon.h>
|
2022-05-12 02:59:56 +01:00
|
|
|
#include <core/openconfig.h>
|
2015-09-12 11:38:44 +01:00
|
|
|
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "plugin_runcommand_debug.h"
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
#define PACKET_TYPE_RUNCOMMAND QStringLiteral("kdeconnect.runcommand")
|
2015-09-12 11:38:44 +01:00
|
|
|
|
2018-11-08 13:55:18 +00:00
|
|
|
#define COMMAND "/bin/sh"
|
|
|
|
#define ARGS "-c"
|
|
|
|
|
2019-06-12 21:16:54 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(RunCommandPlugin, "kdeconnect_runcommand.json")
|
2015-09-12 11:38:44 +01:00
|
|
|
|
|
|
|
RunCommandPlugin::RunCommandPlugin(QObject* parent, const QVariantList& args)
|
|
|
|
: KdeConnectPlugin(parent, args)
|
|
|
|
{
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(config(), &KdeConnectPluginConfig::configChanged, this, &RunCommandPlugin::configChanged);
|
2015-09-12 11:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RunCommandPlugin::~RunCommandPlugin()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
bool RunCommandPlugin::receivePacket(const NetworkPacket& np)
|
2015-09-12 11:38:44 +01:00
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.get<bool>(QStringLiteral("requestCommandList"), false)) {
|
2015-09-12 11:38:44 +01:00
|
|
|
sendConfig();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("key"))) {
|
2019-10-27 17:08:51 +00:00
|
|
|
QJsonDocument commandsDocument = QJsonDocument::fromJson(config()->getByteArray(QStringLiteral("commands"), "{}"));
|
2015-09-12 11:38:44 +01:00
|
|
|
QJsonObject commands = commandsDocument.object();
|
2016-11-26 14:38:08 +00:00
|
|
|
QString key = np.get<QString>(QStringLiteral("key"));
|
2015-09-12 11:38:44 +01:00
|
|
|
QJsonValue value = commands[key];
|
|
|
|
if (value == QJsonValue::Undefined) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_RUNCOMMAND) << key << "is not a configured command";
|
|
|
|
}
|
2016-06-01 11:19:28 +01:00
|
|
|
const QJsonObject commandJson = value.toObject();
|
2018-11-08 13:55:18 +00:00
|
|
|
qCInfo(KDECONNECT_PLUGIN_RUNCOMMAND) << "Running:" << COMMAND << ARGS << commandJson[QStringLiteral("command")].toString();
|
2021-05-09 00:41:41 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
QProcess::startDetached(commandJson[QStringLiteral("command")].toString());
|
|
|
|
#else
|
2018-11-08 13:55:18 +00:00
|
|
|
QProcess::startDetached(QStringLiteral(COMMAND), QStringList()<< QStringLiteral(ARGS) << commandJson[QStringLiteral("command")].toString());
|
2021-05-09 00:41:41 +01:00
|
|
|
#endif
|
2015-09-12 11:38:44 +01:00
|
|
|
return true;
|
2019-06-10 15:40:28 +01:00
|
|
|
} else if (np.has(QStringLiteral("setup"))) {
|
2022-05-12 02:59:56 +01:00
|
|
|
OpenConfig oc;
|
|
|
|
oc.openConfiguration(device()->id(), QStringLiteral("kdeconnect_runcommand"));
|
2015-09-12 11:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunCommandPlugin::connected()
|
|
|
|
{
|
|
|
|
|
2016-06-02 11:17:24 +01:00
|
|
|
sendConfig();
|
2015-09-12 11:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RunCommandPlugin::sendConfig()
|
|
|
|
{
|
2019-10-27 17:08:51 +00:00
|
|
|
QString commands = config()->getString(QStringLiteral("commands"),QStringLiteral("{}"));
|
2019-06-10 15:40:28 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_RUNCOMMAND, {{QStringLiteral("commandList"), commands}});
|
2018-05-10 17:36:06 +01:00
|
|
|
|
|
|
|
#if KCMUTILS_VERSION >= QT_VERSION_CHECK(5, 45, 0)
|
|
|
|
np.set<bool>(QStringLiteral("canAddCommand"), true);
|
|
|
|
#endif
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-09-12 11:38:44 +01:00
|
|
|
}
|
|
|
|
|
2015-09-12 12:50:09 +01:00
|
|
|
void RunCommandPlugin::configChanged() {
|
|
|
|
sendConfig();
|
|
|
|
}
|
2015-09-12 11:38:44 +01:00
|
|
|
|
|
|
|
#include "runcommandplugin.moc"
|