1 | /* |
2 | SPDX-FileCopyrightText: 2024 Nicolas Fella <nicolas.fella@gmx.de> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-or-later |
5 | */ |
6 | |
7 | #ifndef DEVICESMODEL_H |
8 | #define DEVICESMODEL_H |
9 | |
10 | #include <QAbstractListModel> |
11 | |
12 | #include <QQmlEngine> |
13 | |
14 | #include <memory> |
15 | #include <vector> |
16 | |
17 | class QDBusPendingCallWatcher; |
18 | class OrgKdeKdeconnectDaemonInterface; |
19 | class OrgKdeKdeconnectDeviceInterface; |
20 | |
21 | class DevicesModel : public QAbstractListModel |
22 | { |
23 | Q_OBJECT |
24 | QML_ELEMENT |
25 | |
26 | public: |
27 | enum Roles { |
28 | NameModelRole = Qt::DisplayRole, |
29 | IconNameRole = Qt::DecorationRole, |
30 | IdRole = Qt::UserRole + 1, |
31 | }; |
32 | |
33 | explicit DevicesModel(QObject *parent = nullptr); |
34 | ~DevicesModel() override; |
35 | |
36 | QVariant data(const QModelIndex &index, int role) const override; |
37 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
38 | |
39 | QHash<int, QByteArray> roleNames() const override; |
40 | int rowForDevice(const QString &id) const; |
41 | |
42 | private Q_SLOTS: |
43 | void loadDeviceList(); |
44 | void gotDeviceList(QDBusPendingCallWatcher *watcher); |
45 | |
46 | void deviceAdded(const QString &id); |
47 | void deviceRemoved(const QString &id); |
48 | void deviceUpdated(const QString &id); |
49 | |
50 | private: |
51 | struct DeviceInterface { |
52 | QString id; |
53 | std::unique_ptr<OrgKdeKdeconnectDeviceInterface> interface; |
54 | }; |
55 | |
56 | void clearDevices(); |
57 | void addDevice(DeviceInterface &&dev); |
58 | |
59 | OrgKdeKdeconnectDaemonInterface *m_daemonInterface; |
60 | std::vector<DeviceInterface> m_devices; |
61 | }; |
62 | |
63 | #endif |
64 | |