2015-09-12 11:38:44 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "runcommandplugin.h"
|
|
|
|
|
|
|
|
#include <KPluginFactory>
|
|
|
|
|
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QJsonDocument>
|
2015-09-12 12:45:02 +01:00
|
|
|
#include <KShell>
|
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>
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
#define PACKET_TYPE_RUNCOMMAND QStringLiteral("kdeconnect.runcommand")
|
2015-09-12 11:38:44 +01:00
|
|
|
|
|
|
|
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_runcommand.json", registerPlugin< RunCommandPlugin >(); )
|
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_RUNCOMMAND, "kdeconnect.plugin.runcommand")
|
|
|
|
|
|
|
|
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"))) {
|
|
|
|
QJsonDocument commandsDocument = QJsonDocument::fromJson(config()->get<QByteArray>(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();
|
2017-07-24 15:47:21 +01:00
|
|
|
qCInfo(KDECONNECT_PLUGIN_RUNCOMMAND) << "Running:" << "/bin/sh" << "-c" << commandJson[QStringLiteral("command")].toString();
|
2016-11-26 14:38:08 +00:00
|
|
|
QProcess::startDetached(QStringLiteral("/bin/sh"), QStringList()<< QStringLiteral("-c") << commandJson[QStringLiteral("command")].toString());
|
2015-09-12 11:38:44 +01:00
|
|
|
return true;
|
2018-04-19 01:28:02 +01:00
|
|
|
} else if (np.has("setup")) {
|
|
|
|
QProcess::startDetached(QStringLiteral("kcmshell5"), {QStringLiteral("kdeconnect"), QStringLiteral("--args"), QString(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()
|
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
QString commands = config()->get<QString>(QStringLiteral("commands"),QStringLiteral("{}"));
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_RUNCOMMAND, {{"commandList", commands}});
|
2018-04-19 01:28:02 +01:00
|
|
|
np.set<bool>(QStringLiteral("canAddCommand"), true);
|
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"
|