File indexing completed on 2024-04-28 13:18:39

0001 /*
0002     SPDX-FileCopyrightText: 2022 Aditya Mehra <aix.m@outlook.com>
0003     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #ifndef DEVICESMODEL_H
0007 #define DEVICESMODEL_H
0008 
0009 #include <QAbstractListModel>
0010 #include <QObject>
0011 #include <QDBusInterface>
0012 
0013 class ControllerManagerDBusInterface : public QDBusInterface
0014 {
0015     Q_OBJECT
0016 
0017 public:
0018     ControllerManagerDBusInterface(const QString &service, const QString &path, const char *interface, const QDBusConnection &connection, QObject *parent = nullptr)
0019         : QDBusInterface(service, path, interface, connection, parent)
0020     {
0021     }
0022 
0023 signals:
0024     void deviceConnected(const QString &deviceName);
0025     void deviceDisconnected(const QString &deviceName);
0026 };
0027 
0028 class DevicesModel : public QAbstractListModel
0029 {
0030     Q_OBJECT
0031     Q_PROPERTY(int count READ count NOTIFY countChanged)
0032 
0033 public:
0034     enum DeviceRoles {
0035         DeviceTypeRole = Qt::UserRole + 1,
0036         DeviceNameRole,
0037         DeviceUniqueIdentifierRole,
0038         DeviceIconNameRole
0039     };
0040 
0041     Q_ENUM(DeviceRoles)
0042 
0043     explicit DevicesModel(QObject *parent = nullptr);
0044     ~DevicesModel();
0045     QHash<int, QByteArray> roleNames() const override;
0046 
0047     int count() const;
0048     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0049     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0050     QModelIndex indexOf(const QString &uniqueIdentifier) const;
0051     Q_INVOKABLE QVariantMap get(int index) const;
0052     Q_INVOKABLE void load();
0053 
0054     // Dbus interface implementation
0055     QStringList connectedDevices();
0056     QString deviceName(const QString &uniqueIdentifier);
0057     int deviceType(const QString &uniqueIdentifier);
0058     QString deviceIconName(const QString &uniqueIdentifier);
0059 
0060 public Q_SLOTS:
0061     void deviceConnected(const QString &uniqueIdentifier);
0062     void deviceDisconnected(const QString &uniqueIdentifier);
0063 
0064 Q_SIGNALS:
0065     void devicesChanged();
0066     void countChanged();
0067 
0068 private:
0069     QHash<int, QByteArray> m_roleNames;
0070     QList<QVariantMap> m_devices;
0071 };
0072 
0073 
0074 #endif // DEVICESMODEL_H