diff --git a/plugins/kdeconnect.notifyrc b/plugins/kdeconnect.notifyrc --- a/plugins/kdeconnect.notifyrc +++ b/plugins/kdeconnect.notifyrc @@ -726,5 +726,7 @@ Comment[zh_TW]=接收檔案 Action=Popup - - +[Event/confirmAddCommand] +Name=Confirm to add a command +Comment=A connected device wants to add a command. Please confirm if you intended this +Action=Popup diff --git a/plugins/runcommand/CMakeLists.txt b/plugins/runcommand/CMakeLists.txt --- a/plugins/runcommand/CMakeLists.txt +++ b/plugins/runcommand/CMakeLists.txt @@ -1,3 +1,5 @@ +find_package(KF5 REQUIRED COMPONENTS Notifications) + set(kdeconnect_runcommand_SRCS runcommandplugin.cpp ) @@ -7,7 +9,8 @@ target_link_libraries(kdeconnect_runcommand kdeconnectcore Qt5::DBus - KF5::I18n) + KF5::I18n + KF5::Notifications) #---------------------- @@ -19,7 +22,6 @@ KF5::I18n KF5::CoreAddons KF5::ConfigWidgets - ) install(TARGETS kdeconnect_runcommand_config DESTINATION ${PLUGIN_INSTALL_DIR} ) diff --git a/plugins/runcommand/kdeconnect_runcommand.json b/plugins/runcommand/kdeconnect_runcommand.json --- a/plugins/runcommand/kdeconnect_runcommand.json +++ b/plugins/runcommand/kdeconnect_runcommand.json @@ -102,6 +102,10 @@ "kdeconnect.runcommand" ], "X-KdeConnect-SupportedPacketType": [ - "kdeconnect.runcommand.request" + "kdeconnect.runcommand.request", + "kdeconnect.runcommand.add" + ], + "X-KdeConnect-IngoingPacketType": [ + "kdeconnect.runcommand.add" ] } diff --git a/plugins/runcommand/runcommandplugin.h b/plugins/runcommand/runcommandplugin.h --- a/plugins/runcommand/runcommandplugin.h +++ b/plugins/runcommand/runcommandplugin.h @@ -47,6 +47,8 @@ private: void sendConfig(); + void showConfirmDialog(const QString& name, const QString& command); + void addCommand(const QString& name, const QString& command); }; diff --git a/plugins/runcommand/runcommandplugin.cpp b/plugins/runcommand/runcommandplugin.cpp --- a/plugins/runcommand/runcommandplugin.cpp +++ b/plugins/runcommand/runcommandplugin.cpp @@ -29,11 +29,15 @@ #include #include #include +#include +#include +#include #include #include #define PACKET_TYPE_RUNCOMMAND QStringLiteral("kdeconnect.runcommand") +#define PACKET_TYPE_RUNCOMMAND_ADD QStringLiteral("kdeconnect.runcommand.add") K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_runcommand.json", registerPlugin< RunCommandPlugin >(); ) @@ -51,6 +55,14 @@ bool RunCommandPlugin::receivePacket(const NetworkPacket& np) { + if(np.type() == PACKET_TYPE_RUNCOMMAND_ADD) { + QString name = np.get("name"); + QString command = np.get("command"); + + showConfirmDialog(name, command); + return true; + } + if (np.get(QStringLiteral("requestCommandList"), false)) { sendConfig(); return true; @@ -75,19 +87,48 @@ void RunCommandPlugin::connected() { - sendConfig(); } void RunCommandPlugin::sendConfig() { QString commands = config()->get(QStringLiteral("commands"),QStringLiteral("{}")); NetworkPacket np(PACKET_TYPE_RUNCOMMAND, {{"commandList", commands}}); + np.set(QStringLiteral("canAddCommand"), true); sendPacket(np); } void RunCommandPlugin::configChanged() { sendConfig(); } +void RunCommandPlugin::addCommand(const QString& name, const QString& command) +{ + QJsonDocument jsonDocument = QJsonDocument::fromJson(config()->get(QStringLiteral("commands"), "{}")); + QJsonObject jsonConfig = jsonDocument.object(); + + QString key = QUuid::createUuid().toString(); + QJsonObject entry; + entry[QStringLiteral("name")] = name; + entry[QStringLiteral("command")] = command; + jsonConfig[key] = entry; + + QJsonDocument document; + document.setObject(jsonConfig); + config()->set(QStringLiteral("commands"), document.toJson(QJsonDocument::Compact)); +} + +void RunCommandPlugin::showConfirmDialog(const QString& name, const QString& command) +{ + KNotification* notification = new KNotification("confirmAddCommand", KNotification::Persistent); + notification->setIconName(QStringLiteral("dialog-information")); + notification->setComponentName("kdeconnect"); + notification->setText(i18n("%1 wants to add a command:\n%2", device()->name(), command)); + notification->setActions(QStringList() << i18n("Accept") << i18n("Reject")); + connect(notification, &KNotification::action1Activated, this, [this, name, command]{ + addCommand(name, command); + }); + notification->sendEvent(); +} + #include "runcommandplugin.moc"