Changeset View
Changeset View
Standalone View
Standalone View
plugins/runcommand/runcommandplugin.cpp
Show All 23 Lines | |||||
24 | 24 | | |||
25 | #include <QDBusConnection> | 25 | #include <QDBusConnection> | ||
26 | #include <QProcess> | 26 | #include <QProcess> | ||
27 | #include <QDir> | 27 | #include <QDir> | ||
28 | #include <QLoggingCategory> | 28 | #include <QLoggingCategory> | ||
29 | #include <QSettings> | 29 | #include <QSettings> | ||
30 | #include <QJsonDocument> | 30 | #include <QJsonDocument> | ||
31 | #include <KShell> | 31 | #include <KShell> | ||
32 | #include <QUuid> | ||||
33 | #include <KNotification> | ||||
34 | #include <KLocalizedString> | ||||
32 | 35 | | |||
33 | #include <core/networkpackage.h> | 36 | #include <core/networkpackage.h> | ||
34 | #include <core/device.h> | 37 | #include <core/device.h> | ||
35 | 38 | | |||
36 | #define PACKAGE_TYPE_RUNCOMMAND QStringLiteral("kdeconnect.runcommand") | 39 | #define PACKAGE_TYPE_RUNCOMMAND QStringLiteral("kdeconnect.runcommand") | ||
40 | #define PACKAGE_TYPE_RUNCOMMAND_ADD QStringLiteral("kdeconnect.runcommand.add") | ||||
37 | 41 | | |||
38 | K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_runcommand.json", registerPlugin< RunCommandPlugin >(); ) | 42 | K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_runcommand.json", registerPlugin< RunCommandPlugin >(); ) | ||
39 | 43 | | |||
40 | Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_RUNCOMMAND, "kdeconnect.plugin.runcommand") | 44 | Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_RUNCOMMAND, "kdeconnect.plugin.runcommand") | ||
41 | 45 | | |||
42 | RunCommandPlugin::RunCommandPlugin(QObject* parent, const QVariantList& args) | 46 | RunCommandPlugin::RunCommandPlugin(QObject* parent, const QVariantList& args) | ||
43 | : KdeConnectPlugin(parent, args) | 47 | : KdeConnectPlugin(parent, args) | ||
44 | { | 48 | { | ||
45 | connect(config(), &KdeConnectPluginConfig::configChanged, this, &RunCommandPlugin::configChanged); | 49 | connect(config(), &KdeConnectPluginConfig::configChanged, this, &RunCommandPlugin::configChanged); | ||
46 | } | 50 | } | ||
47 | 51 | | |||
48 | RunCommandPlugin::~RunCommandPlugin() | 52 | RunCommandPlugin::~RunCommandPlugin() | ||
49 | { | 53 | { | ||
50 | } | 54 | } | ||
51 | 55 | | |||
52 | bool RunCommandPlugin::receivePackage(const NetworkPackage& np) | 56 | bool RunCommandPlugin::receivePackage(const NetworkPackage& np) | ||
53 | { | 57 | { | ||
58 | if(np.type() == PACKAGE_TYPE_RUNCOMMAND_ADD) { | ||||
59 | QString name = np.get<QString>("name"); | ||||
60 | QString command = np.get<QString>("command"); | ||||
61 | | ||||
62 | showConfirmDialog(name, command); | ||||
63 | return true; | ||||
thomasp: is a
```
return true;
```
missing here? | |||||
64 | } | ||||
65 | | ||||
54 | if (np.get<bool>(QStringLiteral("requestCommandList"), false)) { | 66 | if (np.get<bool>(QStringLiteral("requestCommandList"), false)) { | ||
55 | sendConfig(); | 67 | sendConfig(); | ||
56 | return true; | 68 | return true; | ||
57 | } | 69 | } | ||
58 | 70 | | |||
59 | if (np.has(QStringLiteral("key"))) { | 71 | if (np.has(QStringLiteral("key"))) { | ||
60 | QJsonDocument commandsDocument = QJsonDocument::fromJson(config()->get<QByteArray>(QStringLiteral("commands"), "{}")); | 72 | QJsonDocument commandsDocument = QJsonDocument::fromJson(config()->get<QByteArray>(QStringLiteral("commands"), "{}")); | ||
61 | QJsonObject commands = commandsDocument.object(); | 73 | QJsonObject commands = commandsDocument.object(); | ||
62 | QString key = np.get<QString>(QStringLiteral("key")); | 74 | QString key = np.get<QString>(QStringLiteral("key")); | ||
63 | QJsonValue value = commands[key]; | 75 | QJsonValue value = commands[key]; | ||
64 | if (value == QJsonValue::Undefined) { | 76 | if (value == QJsonValue::Undefined) { | ||
65 | qCWarning(KDECONNECT_PLUGIN_RUNCOMMAND) << key << "is not a configured command"; | 77 | qCWarning(KDECONNECT_PLUGIN_RUNCOMMAND) << key << "is not a configured command"; | ||
66 | } | 78 | } | ||
67 | const QJsonObject commandJson = value.toObject(); | 79 | const QJsonObject commandJson = value.toObject(); | ||
68 | qCInfo(KDECONNECT_PLUGIN_RUNCOMMAND) << "Running:" << "/bin/sh" << "-c" << commandJson[QStringLiteral("command")].toString(); | 80 | qCInfo(KDECONNECT_PLUGIN_RUNCOMMAND) << "Running:" << "/bin/sh" << "-c" << commandJson[QStringLiteral("command")].toString(); | ||
69 | QProcess::startDetached(QStringLiteral("/bin/sh"), QStringList()<< QStringLiteral("-c") << commandJson[QStringLiteral("command")].toString()); | 81 | QProcess::startDetached(QStringLiteral("/bin/sh"), QStringList()<< QStringLiteral("-c") << commandJson[QStringLiteral("command")].toString()); | ||
70 | return true; | 82 | return true; | ||
71 | } | 83 | } | ||
72 | 84 | | |||
73 | return false; | 85 | return false; | ||
74 | } | 86 | } | ||
75 | 87 | | |||
76 | void RunCommandPlugin::connected() | 88 | void RunCommandPlugin::connected() | ||
77 | { | 89 | { | ||
78 | | ||||
79 | sendConfig(); | 90 | sendConfig(); | ||
80 | } | 91 | } | ||
81 | 92 | | |||
82 | void RunCommandPlugin::sendConfig() | 93 | void RunCommandPlugin::sendConfig() | ||
83 | { | 94 | { | ||
84 | QString commands = config()->get<QString>(QStringLiteral("commands"),QStringLiteral("{}")); | 95 | QString commands = config()->get<QString>(QStringLiteral("commands"),QStringLiteral("{}")); | ||
85 | NetworkPackage np(PACKAGE_TYPE_RUNCOMMAND, {{"commandList", commands}}); | 96 | NetworkPackage np(PACKAGE_TYPE_RUNCOMMAND, {{"commandList", commands}}); | ||
97 | np.set<bool>(QStringLiteral("canAddCommand"), true); | ||||
86 | sendPackage(np); | 98 | sendPackage(np); | ||
87 | } | 99 | } | ||
88 | 100 | | |||
89 | void RunCommandPlugin::configChanged() { | 101 | void RunCommandPlugin::configChanged() { | ||
90 | sendConfig(); | 102 | sendConfig(); | ||
91 | } | 103 | } | ||
92 | 104 | | |||
105 | void RunCommandPlugin::addCommand(const QString& name, const QString& command) | ||||
106 | { | ||||
107 | QJsonDocument jsonDocument = QJsonDocument::fromJson(config()->get<QByteArray>(QStringLiteral("commands"), "{}")); | ||||
108 | QJsonObject jsonConfig = jsonDocument.object(); | ||||
109 | | ||||
110 | QString key = QUuid::createUuid().toString(); | ||||
111 | QJsonObject entry; | ||||
112 | entry[QStringLiteral("name")] = name; | ||||
113 | entry[QStringLiteral("command")] = command; | ||||
114 | jsonConfig[key] = entry; | ||||
115 | | ||||
116 | QJsonDocument document; | ||||
117 | document.setObject(jsonConfig); | ||||
118 | config()->set(QStringLiteral("commands"), document.toJson(QJsonDocument::Compact)); | ||||
119 | } | ||||
120 | | ||||
121 | void RunCommandPlugin::showConfirmDialog(const QString& name, const QString& command) | ||||
122 | { | ||||
123 | KNotification* notification = new KNotification("confirmAddCommand", KNotification::Persistent); | ||||
124 | notification->setIconName(QStringLiteral("dialog-information")); | ||||
125 | notification->setComponentName("kdeconnect"); | ||||
126 | notification->setText(i18n("%1 wants to add a command:\n%2", device()->name(), command)); | ||||
127 | notification->setActions(QStringList() << i18n("Accept") << i18n("Reject")); | ||||
128 | connect(notification, &KNotification::action1Activated, this, [this, name, command]{ | ||||
129 | addCommand(name, command); | ||||
config()->set(...) in addCommand already calls sendConfig() through the configChanged signal thomasp: `config()->set(...)` in `addCommand` already calls `sendConfig()` through the `configChanged`… | |||||
130 | }); | ||||
131 | notification->sendEvent(); | ||||
132 | } | ||||
133 | | ||||
93 | #include "runcommandplugin.moc" | 134 | #include "runcommandplugin.moc" |
is a
missing here?