File indexing completed on 2024-04-14 04:51:43

0001 /**
0002  * SPDX-FileCopyrightText: 2019 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 #ifndef COMMANDSMODEL_H
0008 #define COMMANDSMODEL_H
0009 
0010 #include "core/kdeconnectpluginconfig.h"
0011 #include "kdeconnectinterfaces_export.h"
0012 #include <QAbstractListModel>
0013 
0014 struct CommandEntry {
0015     QString key;
0016     QString name;
0017     QString command;
0018 };
0019 
0020 class KDECONNECTINTERFACES_EXPORT CommandsModel : public QAbstractListModel
0021 {
0022     Q_OBJECT
0023     Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged)
0024 
0025 public:
0026     enum ModelRoles { KeyRole, NameRole, CommandRole };
0027 
0028     explicit CommandsModel(QObject *parent = nullptr);
0029     ~CommandsModel() override;
0030 
0031     QString deviceId() const;
0032     void setDeviceId(const QString &deviceId);
0033 
0034     QVariant data(const QModelIndex &index, int role) const override;
0035     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0036 
0037     QHash<int, QByteArray> roleNames() const override;
0038 
0039     Q_SCRIPTABLE void removeCommand(int index);
0040     Q_SCRIPTABLE void addCommand(const QString &name, const QString &command);
0041 
0042 private Q_SLOTS:
0043     void refreshCommandList();
0044 
0045 Q_SIGNALS:
0046     void deviceIdChanged(const QString &value);
0047     void rowsChanged();
0048 
0049 private:
0050     void saveCommands();
0051 
0052     QVector<CommandEntry> m_commandList;
0053     QString m_deviceId;
0054     KdeConnectPluginConfig m_config;
0055 };
0056 
0057 #endif // DEVICESMODEL_H