2019-10-27 17:08:51 +00:00
|
|
|
/**
|
|
|
|
* SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "commandsmodel.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QJsonDocument>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QJsonObject>
|
2019-10-27 17:08:51 +00:00
|
|
|
#include <QUuid>
|
|
|
|
|
|
|
|
#include <dbushelper.h>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
CommandsModel::CommandsModel(QObject *parent)
|
2019-10-27 17:08:51 +00:00
|
|
|
: QAbstractListModel(parent)
|
|
|
|
, m_config()
|
|
|
|
{
|
|
|
|
m_config.setPluginName(QStringLiteral("kdeconnect_runcommand"));
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(this, &QAbstractItemModel::rowsInserted, this, &CommandsModel::rowsChanged);
|
|
|
|
connect(this, &QAbstractItemModel::rowsRemoved, this, &CommandsModel::rowsChanged);
|
2019-10-27 17:08:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> CommandsModel::roleNames() const
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// Role names for QML
|
2019-10-27 17:08:51 +00:00
|
|
|
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
|
|
|
|
names.insert(KeyRole, "key");
|
|
|
|
names.insert(NameRole, "name");
|
|
|
|
names.insert(CommandRole, "command");
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandsModel::~CommandsModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CommandsModel::deviceId() const
|
|
|
|
{
|
|
|
|
return m_deviceId;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void CommandsModel::setDeviceId(const QString &deviceId)
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
|
|
|
m_deviceId = deviceId;
|
|
|
|
m_config.setDeviceId(deviceId);
|
|
|
|
|
|
|
|
refreshCommandList();
|
|
|
|
|
|
|
|
Q_EMIT deviceIdChanged(deviceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandsModel::refreshCommandList()
|
|
|
|
{
|
|
|
|
const auto cmds = QJsonDocument::fromJson(m_config.getByteArray(QStringLiteral("commands"), QByteArray())).object();
|
|
|
|
|
|
|
|
beginResetModel();
|
|
|
|
m_commandList.clear();
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it != itEnd; ++it) {
|
2019-10-27 17:08:51 +00:00
|
|
|
const QJsonObject cont = it->toObject();
|
|
|
|
CommandEntry command;
|
|
|
|
command.key = it.key();
|
|
|
|
command.name = cont.value(QStringLiteral("name")).toString();
|
|
|
|
command.command = cont.value(QStringLiteral("command")).toString();
|
|
|
|
m_commandList.append(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QVariant CommandsModel::data(const QModelIndex &index, int role) const
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_commandList.count()) {
|
2019-10-27 17:08:51 +00:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandEntry command = m_commandList[index.row()];
|
|
|
|
|
|
|
|
switch (role) {
|
2022-09-10 22:23:52 +01:00
|
|
|
case KeyRole:
|
|
|
|
return command.key;
|
|
|
|
case NameRole:
|
|
|
|
return command.name;
|
|
|
|
case CommandRole:
|
|
|
|
return command.command;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2019-10-27 17:08:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
int CommandsModel::rowCount(const QModelIndex &parent) const
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
|
|
|
if (parent.isValid()) {
|
2022-09-10 22:23:52 +01:00
|
|
|
// Return size 0 if we are a child because this is not a tree
|
2019-10-27 17:08:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_commandList.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandsModel::removeCommand(int index)
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
m_commandList.remove(index);
|
|
|
|
endRemoveRows();
|
|
|
|
saveCommands();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandsModel::saveCommands()
|
|
|
|
{
|
|
|
|
QJsonObject jsonConfig;
|
|
|
|
for (const CommandEntry &command : m_commandList) {
|
|
|
|
QJsonObject entry;
|
|
|
|
entry[QStringLiteral("name")] = command.name;
|
|
|
|
entry[QStringLiteral("command")] = command.command;
|
|
|
|
jsonConfig[command.key] = entry;
|
|
|
|
}
|
|
|
|
QJsonDocument document;
|
|
|
|
document.setObject(jsonConfig);
|
|
|
|
m_config.set(QStringLiteral("commands"), document.toJson(QJsonDocument::Compact));
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void CommandsModel::addCommand(const QString &name, const QString &command)
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
|
|
|
CommandEntry entry;
|
|
|
|
QString key = QUuid::createUuid().toString();
|
|
|
|
DBusHelper::filterNonExportableCharacters(key);
|
|
|
|
entry.key = key;
|
|
|
|
entry.name = name;
|
|
|
|
entry.command = command;
|
|
|
|
beginInsertRows(QModelIndex(), m_commandList.size(), m_commandList.size());
|
|
|
|
m_commandList.append(entry);
|
|
|
|
endInsertRows();
|
|
|
|
saveCommands();
|
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_commandsmodel.cpp"
|