2018-06-18 20:01:29 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nicolas Fella <nicolas.fella@gmx.de>
|
2018-06-18 20:01:29 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2018-06-18 20:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "remotecommandsmodel.h"
|
|
|
|
#include "interfaces_debug.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
RemoteCommandsModel::RemoteCommandsModel(QObject *parent)
|
2018-06-18 20:01:29 +01:00
|
|
|
: QAbstractListModel(parent)
|
|
|
|
, m_dbusInterface(nullptr)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(this, &QAbstractItemModel::rowsInserted, this, &RemoteCommandsModel::rowsChanged);
|
|
|
|
connect(this, &QAbstractItemModel::rowsRemoved, this, &RemoteCommandsModel::rowsChanged);
|
2018-06-18 20:01:29 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QDBusServiceWatcher *watcher =
|
|
|
|
new QDBusServiceWatcher(DaemonDbusInterface::activatedService(), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
|
2018-06-18 20:01:29 +01:00
|
|
|
connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &RemoteCommandsModel::refreshCommandList);
|
|
|
|
connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &RemoteCommandsModel::clearCommands);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> RemoteCommandsModel::roleNames() const
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// Role names for QML
|
2018-06-18 20:01:29 +01:00
|
|
|
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
|
|
|
|
names.insert(KeyRole, "key");
|
|
|
|
names.insert(NameRole, "name");
|
|
|
|
names.insert(CommandRole, "command");
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteCommandsModel::~RemoteCommandsModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RemoteCommandsModel::deviceId() const
|
|
|
|
{
|
|
|
|
return m_deviceId;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void RemoteCommandsModel::setDeviceId(const QString &deviceId)
|
2018-06-18 20:01:29 +01:00
|
|
|
{
|
|
|
|
m_deviceId = deviceId;
|
|
|
|
|
|
|
|
if (m_dbusInterface) {
|
|
|
|
delete m_dbusInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dbusInterface = new RemoteCommandsDbusInterface(deviceId, this);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(m_dbusInterface, &OrgKdeKdeconnectDeviceRemotecommandsInterface::commandsChanged, this, &RemoteCommandsModel::refreshCommandList);
|
2018-06-18 20:01:29 +01:00
|
|
|
|
|
|
|
refreshCommandList();
|
|
|
|
|
|
|
|
Q_EMIT deviceIdChanged(deviceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it != itEnd; ++it) {
|
2018-06-18 20:01:29 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QVariant RemoteCommandsModel::data(const QModelIndex &index, int role) const
|
2018-06-18 20:01:29 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_commandList.count()) {
|
2018-06-18 20:01:29 +01:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Command 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();
|
2018-06-18 20:01:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
int RemoteCommandsModel::rowCount(const QModelIndex &parent) const
|
2018-06-18 20:01:29 +01: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
|
2018-06-18 20:01:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_commandList.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteCommandsModel::clearCommands()
|
|
|
|
{
|
|
|
|
if (!m_commandList.isEmpty()) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_commandList.size() - 1);
|
|
|
|
m_commandList.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_remotecommandsmodel.cpp"
|