File indexing completed on 2024-09-29 04:26:09
0001 /** 0002 * SPDX-FileCopyrightText: 2018 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 "remotecommandsmodel.h" 0008 #include "interfaces_debug.h" 0009 0010 #include <QDebug> 0011 0012 #include <dbushelper.h> 0013 0014 RemoteCommandsModel::RemoteCommandsModel(QObject *parent) 0015 : QAbstractListModel(parent) 0016 , m_dbusInterface(nullptr) 0017 { 0018 connect(this, &QAbstractItemModel::rowsInserted, this, &RemoteCommandsModel::rowsChanged); 0019 connect(this, &QAbstractItemModel::rowsRemoved, this, &RemoteCommandsModel::rowsChanged); 0020 0021 QDBusServiceWatcher *watcher = 0022 new QDBusServiceWatcher(DaemonDbusInterface::activatedService(), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this); 0023 connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &RemoteCommandsModel::refreshCommandList); 0024 connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &RemoteCommandsModel::clearCommands); 0025 } 0026 0027 QHash<int, QByteArray> RemoteCommandsModel::roleNames() const 0028 { 0029 // Role names for QML 0030 QHash<int, QByteArray> names = QAbstractItemModel::roleNames(); 0031 names.insert(KeyRole, "key"); 0032 names.insert(NameRole, "name"); 0033 names.insert(CommandRole, "command"); 0034 return names; 0035 } 0036 0037 RemoteCommandsModel::~RemoteCommandsModel() 0038 { 0039 } 0040 0041 QString RemoteCommandsModel::deviceId() const 0042 { 0043 return m_deviceId; 0044 } 0045 0046 void RemoteCommandsModel::setDeviceId(const QString &deviceId) 0047 { 0048 m_deviceId = deviceId; 0049 0050 if (m_dbusInterface) { 0051 delete m_dbusInterface; 0052 } 0053 0054 m_dbusInterface = new RemoteCommandsDbusInterface(deviceId, this); 0055 0056 connect(m_dbusInterface, &OrgKdeKdeconnectDeviceRemotecommandsInterface::commandsChanged, this, &RemoteCommandsModel::refreshCommandList); 0057 0058 refreshCommandList(); 0059 0060 Q_EMIT deviceIdChanged(deviceId); 0061 } 0062 0063 void RemoteCommandsModel::refreshCommandList() 0064 { 0065 if (!m_dbusInterface) { 0066 return; 0067 } 0068 0069 clearCommands(); 0070 0071 if (!m_dbusInterface->isValid()) { 0072 qCWarning(KDECONNECT_INTERFACES) << "dbus interface not valid"; 0073 return; 0074 } 0075 0076 const auto cmds = QJsonDocument::fromJson(m_dbusInterface->commands()).object(); 0077 0078 beginResetModel(); 0079 0080 for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it != itEnd; ++it) { 0081 const QJsonObject cont = it->toObject(); 0082 Command command; 0083 command.key = it.key(); 0084 command.name = cont.value(QStringLiteral("name")).toString(); 0085 command.command = cont.value(QStringLiteral("command")).toString(); 0086 m_commandList.append(command); 0087 } 0088 0089 endResetModel(); 0090 } 0091 0092 QVariant RemoteCommandsModel::data(const QModelIndex &index, int role) const 0093 { 0094 if (!index.isValid() || index.row() < 0 || index.row() >= m_commandList.count()) { 0095 return QVariant(); 0096 } 0097 0098 if (!m_dbusInterface || !m_dbusInterface->isValid()) { 0099 return QVariant(); 0100 } 0101 0102 Command command = m_commandList[index.row()]; 0103 0104 switch (role) { 0105 case KeyRole: 0106 return command.key; 0107 case NameRole: 0108 return command.name; 0109 case CommandRole: 0110 return command.command; 0111 default: 0112 return QVariant(); 0113 } 0114 } 0115 0116 int RemoteCommandsModel::rowCount(const QModelIndex &parent) const 0117 { 0118 if (parent.isValid()) { 0119 // Return size 0 if we are a child because this is not a tree 0120 return 0; 0121 } 0122 0123 return m_commandList.count(); 0124 } 0125 0126 void RemoteCommandsModel::clearCommands() 0127 { 0128 if (!m_commandList.isEmpty()) { 0129 beginRemoveRows(QModelIndex(), 0, m_commandList.size() - 1); 0130 m_commandList.clear(); 0131 endRemoveRows(); 0132 } 0133 } 0134 0135 #include "moc_remotecommandsmodel.cpp"