File: commandsmodel.cpp

package info (click to toggle)
kdeconnect 25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: cpp: 22,922; xml: 520; python: 92; sh: 25; makefile: 5
file content (139 lines) | stat: -rw-r--r-- 3,675 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * 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>
#include <QJsonObject>
#include <QUuid>

#include <dbushelper.h>

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);
}

QHash<int, QByteArray> CommandsModel::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;
}

CommandsModel::~CommandsModel()
{
}

QString CommandsModel::deviceId() const
{
    return m_deviceId;
}

void CommandsModel::setDeviceId(const QString &deviceId)
{
    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();

    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();
}

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();
    }
}

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();
}

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));
}

void CommandsModel::addCommand(const QString &name, const QString &command)
{
    CommandEntry entry;
    QString key = QUuid::createUuid().toString(QUuid::WithoutBraces);
    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();
}

#include "moc_commandsmodel.cpp"