Enable running commands from app

Summary:
The RemotecommandsPlugin lacks a graphical frontend.

Inlcudes a Dbus Interface for fetching the commands and a Model exposing them to QML. For this I oriented on the NotificatonsPlugin.

Test Plan:
Open command list in app, check available commands, trigger some. Do same for CLI.
Activate edit action, check KCM opening on remote device, add command, check for new command in list

Reviewers: #kde_connect, apol

Reviewed By: #kde_connect, apol

Subscribers: apol, kdeconnect, #kde_connect

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D13503
This commit is contained in:
Nicolas Fella 2018-06-18 21:01:29 +02:00
parent e40312bf5e
commit 17e1e1eced
9 changed files with 320 additions and 6 deletions

View file

@ -99,6 +99,13 @@ Kirigami.Page
} }
} }
PluginItem {
label: i18n("Run command")
interfaceFactory: RemoteCommandsDbusInterfaceFactory
component: "qrc:/qml/runcommand.qml"
pluginName: "remotecommands"
}
Item { Layout.fillHeight: true } Item { Layout.fillHeight: true }
} }
} }

53
app/qml/runcommand.qml Normal file
View file

@ -0,0 +1,53 @@
/*
* Copyright 2018 Nicolas Fella <nicolas.fella@gmx.de>
*
* 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/>.
*/
import QtQuick 2.2
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.1
import org.kde.kirigami 2.0 as Kirigami
import org.kde.kdeconnect 1.0
Kirigami.Page
{
id: root
title: i18n("Run command")
property QtObject pluginInterface
actions.main: Kirigami.Action {
icon.name: "document-edit"
text: i18n("Edit commands")
onTriggered: pluginInterface.editCommands()
}
ListView {
anchors.fill: parent
model: RemoteCommandsModel {
deviceId: pluginInterface.deviceId
}
delegate: Kirigami.BasicListItem {
width: ListView.view.width
label: name + "\n" + command
onClicked: pluginInterface.triggerCommand(key)
reserveSpaceForIcon: false
}
}
}

View file

@ -8,5 +8,6 @@
<file>qml/PluginItem.qml</file> <file>qml/PluginItem.qml</file>
<file>qml/DevicePage.qml</file> <file>qml/DevicePage.qml</file>
<file>qml/FindDevicesPage.qml</file> <file>qml/FindDevicesPage.qml</file>
<file>qml/runcommand.qml</file>
</qresource> </qresource>
</RCC> </RCC>

View file

@ -18,6 +18,7 @@ set(libkdeconnect_SRC
notificationsmodel.cpp notificationsmodel.cpp
devicessortproxymodel.cpp devicessortproxymodel.cpp
conversationmessage.cpp conversationmessage.cpp
remotecommandsmodel.cpp
# modeltest.cpp # modeltest.cpp
) )
@ -31,6 +32,7 @@ set(libkdeconnect_HEADERS
notificationsmodel.h notificationsmodel.h
conversationmessage.h conversationmessage.h
dbusinterfaces.h dbusinterfaces.h
remotecommandsmodel.h
${CMAKE_CURRENT_BINARY_DIR}/kdeconnectinterfaces_export.h ${CMAKE_CURRENT_BINARY_DIR}/kdeconnectinterfaces_export.h
) )

View file

@ -0,0 +1,153 @@
/**
* Copyright 2018 Nicolas Fella <nicolas.fella@gmx.de>
*
* 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 "remotecommandsmodel.h"
#include "interfaces_debug.h"
#include <QDebug>
#include <QDBusInterface>
RemoteCommandsModel::RemoteCommandsModel(QObject* parent)
: QAbstractListModel(parent)
, m_dbusInterface(nullptr)
{
connect(this, &QAbstractItemModel::rowsInserted,
this, &RemoteCommandsModel::rowsChanged);
connect(this, &QAbstractItemModel::rowsRemoved,
this, &RemoteCommandsModel::rowsChanged);
QDBusServiceWatcher* watcher = new QDBusServiceWatcher(DaemonDbusInterface::activatedService(),
QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &RemoteCommandsModel::refreshCommandList);
connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &RemoteCommandsModel::clearCommands);
}
QHash<int, QByteArray> RemoteCommandsModel::roleNames() const
{
//Role names for QML
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
names.insert(KeyRole, "key");
names.insert(NameRole, "name");
names.insert(CommandRole, "command");
return names;
}
RemoteCommandsModel::~RemoteCommandsModel()
{
}
QString RemoteCommandsModel::deviceId() const
{
return m_deviceId;
}
void RemoteCommandsModel::setDeviceId(const QString& deviceId)
{
m_deviceId = deviceId;
if (m_dbusInterface) {
delete m_dbusInterface;
}
m_dbusInterface = new RemoteCommandsDbusInterface(deviceId, this);
connect(m_dbusInterface, &OrgKdeKdeconnectDeviceRemotecommandsInterface::commandsChanged,
this, &RemoteCommandsModel::refreshCommandList);
refreshCommandList();
Q_EMIT deviceIdChanged(deviceId);
}
void RemoteCommandsModel::refreshCommandList()
{
if (!m_dbusInterface) {
return;
}
clearCommands();
if (!m_dbusInterface->isValid()) {
qCWarning(KDECONNECT_INTERFACES) << "dbus interface not valid";
return;
}
const auto cmds = QJsonDocument::fromJson(m_dbusInterface->commands()).object();
beginResetModel();
for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it!=itEnd; ++it) {
const QJsonObject cont = it->toObject();
Command command;
command.key = it.key();
command.name = cont.value(QStringLiteral("name")).toString();
command.command = cont.value(QStringLiteral("command")).toString();
m_commandList.append(command);
}
endResetModel();
}
QVariant RemoteCommandsModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()
|| index.row() < 0
|| index.row() >= m_commandList.count())
{
return QVariant();
}
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
return QVariant();
}
Command command = m_commandList[index.row()];
switch (role) {
case KeyRole:
return command.key;
case NameRole:
return command.name;
case CommandRole:
return command.command;
default:
return QVariant();
}
}
int RemoteCommandsModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid()) {
//Return size 0 if we are a child because this is not a tree
return 0;
}
return m_commandList.count();
}
void RemoteCommandsModel::clearCommands()
{
if (!m_commandList.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, m_commandList.size() - 1);
m_commandList.clear();
endRemoveRows();
}
}

View file

@ -0,0 +1,72 @@
/**
* Copyright 2018 Nicolas Fella <nicolas.fella@gmx.de>
*
* 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/>.
*/
#ifndef REMOTECOMMANDSMODEL_H
#define REMOTECOMMANDSMODEL_H
#include <QAbstractListModel>
#include "interfaces/dbusinterfaces.h"
struct Command {
QString key;
QString name;
QString command;
};
class KDECONNECTINTERFACES_EXPORT RemoteCommandsModel
: public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged)
public:
enum ModelRoles {
KeyRole,
NameRole,
CommandRole
};
explicit RemoteCommandsModel(QObject* parent = nullptr);
~RemoteCommandsModel() override;
QString deviceId() const;
void setDeviceId(const QString& deviceId);
QVariant data(const QModelIndex& index, int role) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
private Q_SLOTS:
void refreshCommandList();
void clearCommands();
Q_SIGNALS:
void deviceIdChanged(const QString& value);
void rowsChanged();
private:
RemoteCommandsDbusInterface* m_dbusInterface;
QVector<Command> m_commandList;
QString m_deviceId;
};
#endif // DEVICESMODEL_H

View file

@ -32,6 +32,7 @@
#include "interfaces/devicessortproxymodel.h" #include "interfaces/devicessortproxymodel.h"
#include "interfaces/devicesmodel.h" #include "interfaces/devicesmodel.h"
#include "interfaces/notificationsmodel.h" #include "interfaces/notificationsmodel.h"
#include <remotecommandsmodel.h>
QObject* createDeviceDbusInterface(const QVariant& deviceId) QObject* createDeviceDbusInterface(const QVariant& deviceId)
{ {
@ -84,10 +85,16 @@ QObject* createDBusResponse()
return new DBusAsyncResponse(); return new DBusAsyncResponse();
} }
QObject* createRemoteCommandsInterface(const QVariant& deviceId)
{
return new RemoteCommandsDbusInterface(deviceId.toString());
}
void KdeConnectDeclarativePlugin::registerTypes(const char* uri) void KdeConnectDeclarativePlugin::registerTypes(const char* uri)
{ {
qmlRegisterType<DevicesModel>(uri, 1, 0, "DevicesModel"); qmlRegisterType<DevicesModel>(uri, 1, 0, "DevicesModel");
qmlRegisterType<NotificationsModel>(uri, 1, 0, "NotificationsModel"); qmlRegisterType<NotificationsModel>(uri, 1, 0, "NotificationsModel");
qmlRegisterType<RemoteCommandsModel>(uri, 1, 0, "RemoteCommandsModel");
qmlRegisterType<DBusAsyncResponse>(uri, 1, 0, "DBusAsyncResponse"); qmlRegisterType<DBusAsyncResponse>(uri, 1, 0, "DBusAsyncResponse");
qmlRegisterType<DevicesSortProxyModel>(uri, 1, 0, "DevicesSortProxyModel"); qmlRegisterType<DevicesSortProxyModel>(uri, 1, 0, "DevicesSortProxyModel");
qmlRegisterUncreatableType<MprisDbusInterface>(uri, 1, 0, "MprisDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess")); qmlRegisterUncreatableType<MprisDbusInterface>(uri, 1, 0, "MprisDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess"));
@ -95,6 +102,7 @@ void KdeConnectDeclarativePlugin::registerTypes(const char* uri)
qmlRegisterUncreatableType<FindMyPhoneDeviceDbusInterface>(uri, 1, 0, "FindMyPhoneDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess")); qmlRegisterUncreatableType<FindMyPhoneDeviceDbusInterface>(uri, 1, 0, "FindMyPhoneDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess"));
qmlRegisterUncreatableType<RemoteKeyboardDbusInterface>(uri, 1, 0, "RemoteKeyboardDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess")); qmlRegisterUncreatableType<RemoteKeyboardDbusInterface>(uri, 1, 0, "RemoteKeyboardDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess"));
qmlRegisterUncreatableType<DeviceDbusInterface>(uri, 1, 0, "DeviceDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess")); qmlRegisterUncreatableType<DeviceDbusInterface>(uri, 1, 0, "DeviceDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess"));
qmlRegisterUncreatableType<DeviceDbusInterface>(uri, 1, 0, "RemoteCommandsDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess"));
qmlRegisterSingletonType<DaemonDbusInterface>(uri, 1, 0, "DaemonDbusInterface", qmlRegisterSingletonType<DaemonDbusInterface>(uri, 1, 0, "DaemonDbusInterface",
[](QQmlEngine*, QJSEngine*) -> QObject* { [](QQmlEngine*, QJSEngine*) -> QObject* {
return new DaemonDbusInterface; return new DaemonDbusInterface;
@ -105,13 +113,13 @@ void KdeConnectDeclarativePlugin::registerTypes(const char* uri)
void KdeConnectDeclarativePlugin::initializeEngine(QQmlEngine* engine, const char* uri) void KdeConnectDeclarativePlugin::initializeEngine(QQmlEngine* engine, const char* uri)
{ {
QQmlExtensionPlugin::initializeEngine(engine, uri); QQmlExtensionPlugin::initializeEngine(engine, uri);
engine->rootContext()->setContextProperty(QStringLiteral("DeviceDbusInterfaceFactory") engine->rootContext()->setContextProperty(QStringLiteral("DeviceDbusInterfaceFactory")
, new ObjectFactory(engine, createDeviceDbusInterface)); , new ObjectFactory(engine, createDeviceDbusInterface));
engine->rootContext()->setContextProperty(QStringLiteral("DeviceBatteryDbusInterfaceFactory") engine->rootContext()->setContextProperty(QStringLiteral("DeviceBatteryDbusInterfaceFactory")
, new ObjectFactory(engine, createDeviceBatteryDbusInterface)); , new ObjectFactory(engine, createDeviceBatteryDbusInterface));
engine->rootContext()->setContextProperty(QStringLiteral("FindMyPhoneDbusInterfaceFactory") engine->rootContext()->setContextProperty(QStringLiteral("FindMyPhoneDbusInterfaceFactory")
, new ObjectFactory(engine, createFindMyPhoneInterface)); , new ObjectFactory(engine, createFindMyPhoneInterface));
@ -132,10 +140,13 @@ void KdeConnectDeclarativePlugin::initializeEngine(QQmlEngine* engine, const cha
engine->rootContext()->setContextProperty(QStringLiteral("TelephonyDbusInterfaceFactory") engine->rootContext()->setContextProperty(QStringLiteral("TelephonyDbusInterfaceFactory")
, new ObjectFactory(engine, createTelephonyInterface)); , new ObjectFactory(engine, createTelephonyInterface));
engine->rootContext()->setContextProperty(QStringLiteral("DBusResponseFactory") engine->rootContext()->setContextProperty(QStringLiteral("DBusResponseFactory")
, new ObjectFactory(engine, createDBusResponse)); , new ObjectFactory(engine, createDBusResponse));
engine->rootContext()->setContextProperty(QStringLiteral("DBusResponseWaiter") engine->rootContext()->setContextProperty(QStringLiteral("DBusResponseWaiter")
, DBusResponseWaiter::instance()); , DBusResponseWaiter::instance());
engine->rootContext()->setContextProperty(QStringLiteral("RemoteCommandsDbusInterfaceFactory")
, new ObjectFactory(engine, createRemoteCommandsInterface));
} }

View file

@ -42,6 +42,7 @@ Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_REMOTECOMMANDS, "kdeconnect.plugin.remoteco
RemoteCommandsPlugin::RemoteCommandsPlugin(QObject* parent, const QVariantList& args) RemoteCommandsPlugin::RemoteCommandsPlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args) : KdeConnectPlugin(parent, args)
, m_commands("{}") , m_commands("{}")
, m_canAddCommand(false)
{ {
} }
@ -50,6 +51,7 @@ RemoteCommandsPlugin::~RemoteCommandsPlugin() = default;
bool RemoteCommandsPlugin::receivePacket(const NetworkPacket& np) bool RemoteCommandsPlugin::receivePacket(const NetworkPacket& np)
{ {
if (np.has(QStringLiteral("commandList"))) { if (np.has(QStringLiteral("commandList"))) {
m_canAddCommand = np.get<bool>(QStringLiteral("canAddCommand"));
setCommands(np.get<QByteArray>(QStringLiteral("commandList"))); setCommands(np.get<QByteArray>(QStringLiteral("commandList")));
return true; return true;
} }
@ -82,4 +84,10 @@ void RemoteCommandsPlugin::triggerCommand(const QString& key)
sendPacket(np); sendPacket(np);
} }
void RemoteCommandsPlugin::editCommands()
{
NetworkPacket np(PACKET_TYPE_RUNCOMMAND_REQUEST, {{ "setup", true }});
sendPacket(np);
}
#include "remotecommandsplugin.moc" #include "remotecommandsplugin.moc"

View file

@ -36,13 +36,19 @@ class Q_DECL_EXPORT RemoteCommandsPlugin
Q_OBJECT Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.remotecommands") Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.remotecommands")
Q_PROPERTY(QByteArray commands READ commands NOTIFY commandsChanged) Q_PROPERTY(QByteArray commands READ commands NOTIFY commandsChanged)
Q_PROPERTY(QString deviceId READ deviceId CONSTANT)
Q_PROPERTY(bool canAddCommand READ canAddCommand CONSTANT)
public: public:
explicit RemoteCommandsPlugin(QObject* parent, const QVariantList& args); explicit RemoteCommandsPlugin(QObject* parent, const QVariantList& args);
~RemoteCommandsPlugin() override; ~RemoteCommandsPlugin() override;
Q_SCRIPTABLE void triggerCommand(const QString& key); Q_SCRIPTABLE void triggerCommand(const QString& key);
Q_SCRIPTABLE void editCommands();
QByteArray commands() const { return m_commands; } QByteArray commands() const { return m_commands; }
QString deviceId() const { return device()->id(); }
bool canAddCommand() const { return m_canAddCommand; }
bool receivePacket(const NetworkPacket& np) override; bool receivePacket(const NetworkPacket& np) override;
void connected() override; void connected() override;
@ -55,6 +61,7 @@ private:
void setCommands(const QByteArray& commands); void setCommands(const QByteArray& commands);
QByteArray m_commands; QByteArray m_commands;
bool m_canAddCommand;
}; };
#endif #endif