File indexing completed on 2024-04-21 04:56:47

0001 /**
0002  * SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "commandsmodel.h"
0008 
0009 #include <QDebug>
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QUuid>
0013 
0014 #include <dbushelper.h>
0015 
0016 CommandsModel::CommandsModel(QObject *parent)
0017     : QAbstractListModel(parent)
0018     , m_config()
0019 {
0020     m_config.setPluginName(QStringLiteral("kdeconnect_runcommand"));
0021     connect(this, &QAbstractItemModel::rowsInserted, this, &CommandsModel::rowsChanged);
0022     connect(this, &QAbstractItemModel::rowsRemoved, this, &CommandsModel::rowsChanged);
0023 }
0024 
0025 QHash<int, QByteArray> CommandsModel::roleNames() const
0026 {
0027     // Role names for QML
0028     QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
0029     names.insert(KeyRole, "key");
0030     names.insert(NameRole, "name");
0031     names.insert(CommandRole, "command");
0032     return names;
0033 }
0034 
0035 CommandsModel::~CommandsModel()
0036 {
0037 }
0038 
0039 QString CommandsModel::deviceId() const
0040 {
0041     return m_deviceId;
0042 }
0043 
0044 void CommandsModel::setDeviceId(const QString &deviceId)
0045 {
0046     m_deviceId = deviceId;
0047     m_config.setDeviceId(deviceId);
0048 
0049     refreshCommandList();
0050 
0051     Q_EMIT deviceIdChanged(deviceId);
0052 }
0053 
0054 void CommandsModel::refreshCommandList()
0055 {
0056     const auto cmds = QJsonDocument::fromJson(m_config.getByteArray(QStringLiteral("commands"), QByteArray())).object();
0057 
0058     beginResetModel();
0059     m_commandList.clear();
0060 
0061     for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it != itEnd; ++it) {
0062         const QJsonObject cont = it->toObject();
0063         CommandEntry command;
0064         command.key = it.key();
0065         command.name = cont.value(QStringLiteral("name")).toString();
0066         command.command = cont.value(QStringLiteral("command")).toString();
0067         m_commandList.append(command);
0068     }
0069 
0070     endResetModel();
0071 }
0072 
0073 QVariant CommandsModel::data(const QModelIndex &index, int role) const
0074 {
0075     if (!index.isValid() || index.row() < 0 || index.row() >= m_commandList.count()) {
0076         return QVariant();
0077     }
0078 
0079     CommandEntry command = m_commandList[index.row()];
0080 
0081     switch (role) {
0082     case KeyRole:
0083         return command.key;
0084     case NameRole:
0085         return command.name;
0086     case CommandRole:
0087         return command.command;
0088     default:
0089         return QVariant();
0090     }
0091 }
0092 
0093 int CommandsModel::rowCount(const QModelIndex &parent) const
0094 {
0095     if (parent.isValid()) {
0096         // Return size 0 if we are a child because this is not a tree
0097         return 0;
0098     }
0099 
0100     return m_commandList.count();
0101 }
0102 
0103 void CommandsModel::removeCommand(int index)
0104 {
0105     beginRemoveRows(QModelIndex(), index, index);
0106     m_commandList.remove(index);
0107     endRemoveRows();
0108     saveCommands();
0109 }
0110 
0111 void CommandsModel::saveCommands()
0112 {
0113     QJsonObject jsonConfig;
0114     for (const CommandEntry &command : m_commandList) {
0115         QJsonObject entry;
0116         entry[QStringLiteral("name")] = command.name;
0117         entry[QStringLiteral("command")] = command.command;
0118         jsonConfig[command.key] = entry;
0119     }
0120     QJsonDocument document;
0121     document.setObject(jsonConfig);
0122     m_config.set(QStringLiteral("commands"), document.toJson(QJsonDocument::Compact));
0123 }
0124 
0125 void CommandsModel::addCommand(const QString &name, const QString &command)
0126 {
0127     CommandEntry entry;
0128     QString key = QUuid::createUuid().toString();
0129     DBusHelper::filterNonExportableCharacters(key);
0130     entry.key = key;
0131     entry.name = name;
0132     entry.command = command;
0133     beginInsertRows(QModelIndex(), m_commandList.size(), m_commandList.size());
0134     m_commandList.append(entry);
0135     endInsertRows();
0136     saveCommands();
0137 }
0138 
0139 #include "moc_commandsmodel.cpp"