First implementation of runcommand plugin

This commit is contained in:
Albert Vaca 2015-09-12 03:38:44 -07:00
parent b7eefa0388
commit cf4a9b8639
8 changed files with 401 additions and 0 deletions

View file

@ -8,6 +8,7 @@ add_subdirectory(telephony)
add_subdirectory(share)
add_subdirectory(notifications)
add_subdirectory(battery)
add_subdirectory(runcommand)
if(NOT WIN32)
add_subdirectory(pausemusic)
add_subdirectory(mpriscontrol)

View file

@ -0,0 +1,26 @@
set(kdeconnect_runcommand_SRCS
runcommandplugin.cpp
)
kdeconnect_add_plugin(kdeconnect_runcommand JSON kdeconnect_runcommand.json SOURCES ${kdeconnect_runcommand_SRCS})
target_link_libraries(kdeconnect_runcommand
kdeconnectcore
Qt5::DBus
KF5::I18n)
#----------------------
set( kdeconnect_runcommand_config_SRCS runcommand_config.cpp )
add_library(kdeconnect_runcommand_config MODULE ${kdeconnect_runcommand_config_SRCS} )
target_link_libraries( kdeconnect_runcommand_config
kdeconnectpluginkcm
KF5::I18n
KF5::CoreAddons
KF5::ConfigWidgets
)
install(TARGETS kdeconnect_runcommand_config DESTINATION ${PLUGIN_INSTALL_DIR} )
install(FILES kdeconnect_runcommand_config.desktop DESTINATION ${SERVICES_INSTALL_DIR} )

View file

@ -0,0 +1,32 @@
{
"Encoding": "UTF-8",
"KPlugin": {
"Authors": [
{
"Email": "albertvaka@gmail.com",
"Name": "Albert Vaca"
},
{
"Email": "david@davidedmundson.co.uk",
"Name": "David Edmundson"
}
],
"Description": "Execute console commands remotely",
"EnabledByDefault": true,
"Icon": "system-run",
"Id": "kdeconnect_runcommand",
"License": "GPL",
"Name": "Run commands",
"ServiceTypes": [
"KdeConnect/Plugin"
],
"Version": "0.1",
"Website": "http://albertvaka.wordpress.com"
},
"X-KdeConnect-OutgoingPackageType": [
"kdeconnect.runcommand"
],
"X-KdeConnect-SupportedPackageType": [
"kdeconnect.runcommand"
]
}

View file

@ -0,0 +1,10 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=KCModule
X-KDE-Library=kdeconnect_runcommand_config
X-KDE-ParentComponents=kdeconnect_runcommand
Name=Run Command plugin settings
Categories=Qt;KDE;X-KDE-settings-kdeconnect;

View file

@ -0,0 +1,139 @@
/**
* Copyright 2015 David Edmundson <davidedmundson@kde.org>
*
* 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 "runcommand_config.h"
#include <QStandardPaths>
#include <QTableView>
#include <QHBoxLayout>
#include <QStandardItemModel>
#include <QDebug>
#include <QUuid>
#include <QJsonDocument>
#include <KLocalizedString>
#include <KPluginFactory>
K_PLUGIN_FACTORY(ShareConfigFactory, registerPlugin<RunCommandConfig>();)
RunCommandConfig::RunCommandConfig(QWidget *parent, const QVariantList& args)
: KdeConnectPluginKcm(parent, args, "kdeconnect_runcommand_config")
{
QTableView *table = new QTableView(this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(table);
setLayout(layout);
m_entriesModel = new QStandardItemModel(this);
table->setModel(m_entriesModel);
m_entriesModel->setHorizontalHeaderLabels(QStringList() << i18n("Name") << i18n("Command"));
}
RunCommandConfig::~RunCommandConfig()
{
}
void RunCommandConfig::defaults()
{
KCModule::defaults();
m_entriesModel->clear();
Q_EMIT changed(true);
}
void RunCommandConfig::load()
{
KCModule::load();
QJsonDocument jsonDocument = QJsonDocument::fromJson(config()->get<QByteArray>("commands", "{}"));
QJsonObject jsonConfig = jsonDocument.object();
foreach(const QString &key, jsonConfig.keys()) {
const QJsonObject entry = jsonConfig[key].toObject();
const QString name = entry["name"].toString();
const QString command = entry["command"].toString();
QStandardItem *newName = new QStandardItem(name);
newName->setEditable(true);
newName->setData(key);
QStandardItem *newCommand = new QStandardItem(command);
newName->setEditable(true);
m_entriesModel->appendRow(QList<QStandardItem*>() << newName << newCommand);
}
insertEmptyRow();
connect(m_entriesModel, &QAbstractItemModel::dataChanged, this, &RunCommandConfig::onDataChanged);
Q_EMIT changed(false);
}
void RunCommandConfig::save()
{
QJsonObject jsonConfig;
for (int i=0;i < m_entriesModel->rowCount(); i++) {
QString key = m_entriesModel->item(i, 0)->data().toString();
const QString name = m_entriesModel->item(i, 0)->text();
const QString command = m_entriesModel->item(i, 1)->text();
if (name.isEmpty() || command.isEmpty()) {
continue;
}
if (key.isEmpty()) {
key = QUuid::createUuid().toString();
}
QJsonObject entry;
entry["name"] = name;
entry["command"] = command;
jsonConfig[key] = entry;
}
QJsonDocument document;
document.setObject(jsonConfig);
config()->set("commands", document.toJson());
KCModule::save();
Q_EMIT changed(false);
}
void RunCommandConfig::insertEmptyRow()
{
QStandardItem *newName = new QStandardItem;
newName->setEditable(true);
QStandardItem *newCommand = new QStandardItem;
newName->setEditable(true);
m_entriesModel->appendRow(QList<QStandardItem*>() << newName << newCommand);
}
void RunCommandConfig::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
changed(true);
Q_UNUSED(topLeft);
if (bottomRight.row() == m_entriesModel->rowCount() - 1) {
//TODO check both entries are still empty
insertEmptyRow();
}
}
#include "runcommand_config.moc"

View file

@ -0,0 +1,50 @@
/**
* Copyright 2015 David Edmundson <davidedmundson@kde.org>
*
* 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 SHARE_CONFIG_H
#define SHARE_CONFIG_H
#include "kcmplugin/kdeconnectpluginkcm.h"
class QStandardItemModel;
class RunCommandConfig
: public KdeConnectPluginKcm
{
Q_OBJECT
public:
RunCommandConfig(QWidget *parent, const QVariantList&);
virtual ~RunCommandConfig();
public Q_SLOTS:
virtual void save() Q_DECL_OVERRIDE;
virtual void load();
virtual void defaults();
private Q_SLOTS:
void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
private:
void insertEmptyRow();
QStandardItemModel *m_entriesModel;
};
#endif

View file

@ -0,0 +1,91 @@
/**
* 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 <qt/QtCore/qjsondocument.h>
#include <QJsonDocument>
#include <core/networkpackage.h>
#include <core/device.h>
#define PACKAGE_TYPE_RUNCOMMAND QLatin1String("kdeconnect.runcommand")
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)
{
}
RunCommandPlugin::~RunCommandPlugin()
{
}
bool RunCommandPlugin::receivePackage(const NetworkPackage& np)
{
if (np.get<bool>("requestCommandList", false)) {
sendConfig();
return true;
}
if (np.has("key")) {
QJsonDocument commandsDocument = QJsonDocument::fromJson(config()->get<QByteArray>("commands", "{}"));
QJsonObject commands = commandsDocument.object();
QString key = np.get<QString>("key");
QJsonValue value = commands[key];
if (value == QJsonValue::Undefined) {
qCWarning(KDECONNECT_PLUGIN_RUNCOMMAND) << key << "is not a configured command";
}
QJsonObject command = value.toObject();
QString name = command["name"].toString();
QString commandLine = command["command"].toString();
QProcess::execute(commandLine);
return true;
}
return false;
}
void RunCommandPlugin::connected()
{
}
void RunCommandPlugin::sendConfig()
{
QString commands = config()->get<QString>("commands","{}");
NetworkPackage np(PACKAGE_TYPE_RUNCOMMAND);
np.set("commandList", commands);
qDebug() << "SENT" << np.serialize();
sendPackage(np);
}
#include "runcommandplugin.moc"

View file

@ -0,0 +1,52 @@
/**
* Copyright 2015 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/>.
*/
#ifndef RUNCOMMANDPLUGIN_H
#define RUNCOMMANDPLUGIN_H
#include <QObject>
#include <core/kdeconnectplugin.h>
#include <QFile>
#include <QFileSystemWatcher>
#include <QMap>
#include <QPair>
#include <QString>
class Q_DECL_EXPORT RunCommandPlugin
: public KdeConnectPlugin
{
Q_OBJECT
public:
explicit RunCommandPlugin(QObject *parent, const QVariantList &args);
virtual ~RunCommandPlugin();
public Q_SLOTS:
virtual bool receivePackage(const NetworkPackage& np);
virtual void connected();
private:
void sendConfig();
};
#endif