Changeset View
Changeset View
Standalone View
Standalone View
plugins/remotecommands/remotecommandsdbusinterface.cpp
- This file was added.
1 | /** | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Nicolas Fella <nicolas.fella@gmx.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "remotecommandsdbusinterface.h" | ||||
22 | | ||||
23 | #include <QDBusConnection> | ||||
24 | #include <QJsonArray> | ||||
25 | #include <QJsonDocument> | ||||
26 | #include <QJsonObject> | ||||
27 | | ||||
28 | #include "remotecommand.h" | ||||
29 | #include "remotecommandsplugin.h" | ||||
30 | #include "core/networkpacket.h" | ||||
31 | | ||||
32 | #define PACKET_TYPE_RUNCOMMAND_REQUEST QLatin1String("kdeconnect.runcommand.request") | ||||
33 | | ||||
34 | RemoteCommandsDbusInterface::RemoteCommandsDbusInterface(RemoteCommandsPlugin* plugin) | ||||
35 | : QDBusAbstractAdaptor(const_cast<Device*>(plugin->device())) | ||||
36 | , m_device(plugin->device()) | ||||
37 | , m_plugin(plugin) | ||||
38 | , m_commands() | ||||
39 | , m_lastId(0) | ||||
40 | , m_canAddCommand(false) | ||||
41 | { | ||||
42 | } | ||||
43 | | ||||
44 | RemoteCommandsDbusInterface::~RemoteCommandsDbusInterface() | ||||
45 | { | ||||
46 | } | ||||
47 | | ||||
48 | | ||||
49 | QStringList RemoteCommandsDbusInterface::commands() | ||||
50 | { | ||||
51 | return m_commands.keys(); | ||||
52 | } | ||||
53 | | ||||
54 | QString RemoteCommandsDbusInterface::deviceId() | ||||
55 | { | ||||
56 | return m_device->id(); | ||||
57 | } | ||||
58 | | ||||
59 | bool RemoteCommandsDbusInterface::canAddCommand() | ||||
60 | { | ||||
61 | return m_canAddCommand; | ||||
62 | } | ||||
63 | | ||||
64 | | ||||
65 | bool RemoteCommandsDbusInterface::receivePacket(const NetworkPacket& np) | ||||
66 | { | ||||
67 | if (np.has(QStringLiteral("commandList"))) { | ||||
68 | | ||||
69 | m_canAddCommand = np.get<bool>(QStringLiteral("canAddCommand")); | ||||
70 | | ||||
71 | qDeleteAll(m_commands); | ||||
72 | m_commands.clear(); | ||||
73 | QJsonDocument commands = QJsonDocument::fromJson(np.get<QByteArray>(QStringLiteral("commandList"))); | ||||
74 | | ||||
75 | for (QString& key : commands.object().keys()) { | ||||
76 | const QJsonObject entry = commands[key].toObject(); | ||||
77 | | ||||
78 | // key contains characters that are not suitable for DBus paths, create a public id instead | ||||
79 | QString id = QString::number(m_lastId++); | ||||
80 | | ||||
81 | Remotecommand* command = new Remotecommand(key, entry[QStringLiteral("name")].toString(), entry[QStringLiteral("command")].toString(), this); | ||||
82 | m_commands.insert(id, command); | ||||
83 | QDBusConnection::sessionBus().registerObject(m_device->dbusPath()+"/remotecommands/" + id, command, QDBusConnection::ExportScriptableContents); | ||||
84 | } | ||||
85 | | ||||
86 | } | ||||
87 | return true; | ||||
88 | } | ||||
89 | | ||||
90 | void RemoteCommandsDbusInterface::triggerCommand(const QString& id) | ||||
91 | { | ||||
92 | NetworkPacket np(PACKET_TYPE_RUNCOMMAND_REQUEST, {{ "key", m_commands[id]->key() }}); | ||||
93 | m_plugin->sendPacket(np); | ||||
94 | } | ||||
95 | | ||||
96 | | ||||
97 | void RemoteCommandsDbusInterface::editCommands() | ||||
98 | { | ||||
99 | NetworkPacket np(PACKET_TYPE_RUNCOMMAND_REQUEST, {{ "setup", true }}); | ||||
100 | m_plugin->sendPacket(np); | ||||
101 | } |