[OpenDocString] kdeconnect-kde (cpp)
remotecommandsmodel.cpp
RemoteCommandsModel::RemoteCommandsModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_dbusInterface(nullptr)
{
    connect(this, &QAbstractItemModel::rowsInserted, this, &RemoteCommandsModel::rowsChanged);
    connect(this, &QAbstractItemModel::rowsRemoved, this, &RemoteCommandsModel::rowsChanged);

    QDBusServiceWatcher *watcher =
        new QDBusServiceWatcher(DaemonDbusInterface::activatedService(), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
    connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &RemoteCommandsModel::refreshCommandList);
    connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &RemoteCommandsModel::clearCommands);
}
This constructor builds a remote commands model. It takes the parent object, sets up connections to the rowsInserted and rowsRemoved signal, and a QDBusServiceWatcher object to watch for the change.
QHash RemoteCommandsModel::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. The keys are the keys and the names are the names of the names of the items.
RemoteCommandsModel::~RemoteCommandsModel()
{
}
This implements the Model interface to manage remote commands.
QString RemoteCommandsModel::deviceId() const
{
    return m_deviceId;
}
Returns the device id stored in the internal list.
void RemoteCommandsModel::setDeviceId(const QString &deviceId)
{
    m_deviceId = deviceId;

    if (m_dbusInterface) {
        delete m_dbusInterface;
    }

    m_dbusInterface = new RemoteCommandsDbusInterface(deviceId, this);

    connect(m_dbusInterface, &OrgKdeKdeconnectDeviceRemotecommandsInterface::commandsChanged, this, &RemoteCommandsModel::refreshCommandList);

    refreshCommandList();

    Q_EMIT deviceIdChanged(deviceId);
}
Sets the device id, creates a DBus interface and connects the commandsChanged signal. It also connects the commandsChanged signal to the deviceIdChanged signal.
void RemoteCommandsModel::refreshCommandList()
{
    if (!m_dbusInterface) {
        return;
    }

    clearCommands();

    if (!m_dbusInterface->isValid()) {
        qCWarning(KDECONNECT_INTERFACES) << "dbus interface not valid";
        return;
    }

    const auto cmds = QJsonDocument::fromJson(m_dbusInterface->commands()).object();

    beginResetModel();

    for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it != itEnd; ++it) {
        const QJsonObject cont = it->toObject();
        Command 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 dbus interface.
QVariant RemoteCommandsModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() < 0 || index.row() >= m_commandList.count()) {
        return QVariant();
    }

    if (!m_dbusInterface || !m_dbusInterface->isValid()) {
        return QVariant();
    }

    Command 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 RemoteCommandsModel::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 RemoteCommandsModel::clearCommands()
{
    if (!m_commandList.isEmpty()) {
        beginRemoveRows(QModelIndex(), 0, m_commandList.size() - 1);
        m_commandList.clear();
        endRemoveRows();
    }
}
This removes all commands from the command list.