File indexing completed on 2025-09-14 05:20:22

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