[OpenDocString] kdeconnect-kde (cpp)
commandsmodel.cpp
CommandsModel::CommandsModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_config()
{
    m_config.setPluginName(QStringLiteral("kdeconnect_runcommand"));
    connect(this, &QAbstractItemModel::rowsInserted, this, &CommandsModel::rowsChanged);
    connect(this, &QAbstractItemModel::rowsRemoved, this, &CommandsModel::rowsChanged);
}
Constructs a commands model. The parent object is the parent object, sets the plugin name to kdeconnect_runcommand, and sets up signal/slot connections for row inserted and row removed events.
QHash CommandsModel::roleNames() const
{
    // Role names for QML
    QHash names = QAbstractItemModel::roleNames();
    names.insert(KeyRole, "key");
    names.insert(NameRole, "name");
    names.insert(CommandRole, "command");
    return names;
}
Returns a hash of the role names.
CommandsModel::~CommandsModel()
{
}
This implements the actions necessary to manage the commands model.
QString CommandsModel::deviceId() const
{
    return m_deviceId;
}
Returns the device id stored in the internal list.
void CommandsModel::setDeviceId(const QString &deviceId)
{
    m_deviceId = deviceId;
    m_config.setDeviceId(deviceId);

    refreshCommandList();

    Q_EMIT deviceIdChanged(deviceId);
}
Sets the device id by storing it in the m_deviceId variable, and refreshes the command list.
void CommandsModel::refreshCommandList()
{
    const auto cmds = QJsonDocument::fromJson(m_config.getByteArray(QStringLiteral("commands"), QByteArray())).object();

    beginResetModel();
    m_commandList.clear();

    for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it != itEnd; ++it) {
        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();
}
This refreshes the command list from the internal list.
QVariant CommandsModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() < 0 || index.row() >= m_commandList.count()) {
        return QVariant();
    }

    CommandEntry 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();
    }
}
Returns the data for the given index and role.
int CommandsModel::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();
}
Returns the row count for the given parent index. If the parent is not a tree, it returns 0.
void CommandsModel::removeCommand(int index)
{
    beginRemoveRows(QModelIndex(), index, index);
    m_commandList.remove(index);
    endRemoveRows();
    saveCommands();
}
This removes the command at the given index. It first removes the rows of the command list, then saves the changes to the commands model.
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));
}
Saves the commands to the config.
void CommandsModel::addCommand(const QString &name, const QString &command)
{
    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();
}
Adds a command to the list of commands. The function creates a new key, sets name and command in the list of commands. It filters characters that cannot be exported, and writes the command to the command list.