1 | /* |
2 | * BluezQt - Asynchronous Bluez wrapper library |
3 | * |
4 | * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com> |
5 | * |
6 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
7 | */ |
8 | |
9 | #include "devicesmodel.h" |
10 | #include "adapter.h" |
11 | #include "device.h" |
12 | #include "manager.h" |
13 | |
14 | namespace BluezQt |
15 | { |
16 | class DevicesModelPrivate : public QObject |
17 | { |
18 | public: |
19 | explicit DevicesModelPrivate(DevicesModel *qq); |
20 | |
21 | void init(); |
22 | |
23 | void deviceAdded(DevicePtr device); |
24 | void deviceRemoved(DevicePtr device); |
25 | void deviceChanged(DevicePtr device); |
26 | void adapterChanged(AdapterPtr adapter); |
27 | |
28 | DevicesModel *q; |
29 | Manager *m_manager; |
30 | QList<DevicePtr> m_devices; |
31 | }; |
32 | |
33 | DevicesModelPrivate::DevicesModelPrivate(DevicesModel *qq) |
34 | : QObject(qq) |
35 | , q(qq) |
36 | , m_manager(nullptr) |
37 | { |
38 | } |
39 | |
40 | void DevicesModelPrivate::init() |
41 | { |
42 | m_devices = m_manager->devices(); |
43 | |
44 | connect(sender: m_manager, signal: &Manager::deviceAdded, context: this, slot: &DevicesModelPrivate::deviceAdded); |
45 | connect(sender: m_manager, signal: &Manager::deviceRemoved, context: this, slot: &DevicesModelPrivate::deviceRemoved); |
46 | connect(sender: m_manager, signal: &Manager::deviceChanged, context: this, slot: &DevicesModelPrivate::deviceChanged); |
47 | connect(sender: m_manager, signal: &Manager::adapterChanged, context: this, slot: &DevicesModelPrivate::adapterChanged); |
48 | } |
49 | |
50 | void DevicesModelPrivate::deviceAdded(DevicePtr device) |
51 | { |
52 | q->beginInsertRows(parent: QModelIndex(), first: m_devices.size(), last: m_devices.size()); |
53 | m_devices.append(t: device); |
54 | q->endInsertRows(); |
55 | } |
56 | |
57 | void DevicesModelPrivate::deviceRemoved(DevicePtr device) |
58 | { |
59 | int offset = m_devices.indexOf(t: device); |
60 | Q_ASSERT(offset >= 0); |
61 | |
62 | q->beginRemoveRows(parent: QModelIndex(), first: offset, last: offset); |
63 | m_devices.removeAt(i: offset); |
64 | q->endRemoveRows(); |
65 | } |
66 | |
67 | void DevicesModelPrivate::deviceChanged(DevicePtr device) |
68 | { |
69 | int offset = m_devices.indexOf(t: device); |
70 | Q_ASSERT(offset >= 0); |
71 | |
72 | QModelIndex idx = q->createIndex(arow: offset, acolumn: 0); |
73 | Q_EMIT q->dataChanged(topLeft: idx, bottomRight: idx); |
74 | } |
75 | |
76 | void DevicesModelPrivate::adapterChanged(AdapterPtr adapter) |
77 | { |
78 | const auto devices = adapter->devices(); |
79 | for (const DevicePtr &device : devices) { |
80 | deviceChanged(device); |
81 | } |
82 | } |
83 | |
84 | DevicesModel::DevicesModel(Manager *manager, QObject *parent) |
85 | : QAbstractListModel(parent) |
86 | , d(new DevicesModelPrivate(this)) |
87 | { |
88 | d->m_manager = manager; |
89 | d->init(); |
90 | } |
91 | |
92 | DevicesModel::~DevicesModel() = default; |
93 | |
94 | QHash<int, QByteArray> DevicesModel::roleNames() const |
95 | { |
96 | QHash<int, QByteArray> roles = QAbstractListModel::roleNames(); |
97 | |
98 | roles[UbiRole] = QByteArrayLiteral("Ubi" ); |
99 | roles[AddressRole] = QByteArrayLiteral("Address" ); |
100 | roles[NameRole] = QByteArrayLiteral("Name" ); |
101 | roles[FriendlyNameRole] = QByteArrayLiteral("FriendlyName" ); |
102 | roles[RemoteNameRole] = QByteArrayLiteral("RemoteName" ); |
103 | roles[ClassRole] = QByteArrayLiteral("Class" ); |
104 | roles[TypeRole] = QByteArrayLiteral("Type" ); |
105 | roles[AppearanceRole] = QByteArrayLiteral("Appearance" ); |
106 | roles[IconRole] = QByteArrayLiteral("Icon" ); |
107 | roles[PairedRole] = QByteArrayLiteral("Paired" ); |
108 | roles[TrustedRole] = QByteArrayLiteral("Trusted" ); |
109 | roles[BlockedRole] = QByteArrayLiteral("Blocked" ); |
110 | roles[LegacyPairingRole] = QByteArrayLiteral("LegacyPairing" ); |
111 | roles[RssiRole] = QByteArrayLiteral("Rssi" ); |
112 | roles[ConnectedRole] = QByteArrayLiteral("Connected" ); |
113 | roles[UuidsRole] = QByteArrayLiteral("Uuids" ); |
114 | roles[ModaliasRole] = QByteArrayLiteral("Modalias" ); |
115 | roles[AdapterNameRole] = QByteArrayLiteral("AdapterName" ); |
116 | roles[AdapterAddressRole] = QByteArrayLiteral("AdapterAddress" ); |
117 | roles[AdapterPoweredRole] = QByteArrayLiteral("AdapterPowered" ); |
118 | roles[AdapterDiscoverableRole] = QByteArrayLiteral("AdapterDiscoverable" ); |
119 | roles[AdapterPairableRole] = QByteArrayLiteral("AdapterPairable" ); |
120 | roles[AdapterDiscoveringRole] = QByteArrayLiteral("AdapterDiscovering" ); |
121 | roles[AdapterUuidsRole] = QByteArrayLiteral("AdapterUuids" ); |
122 | |
123 | return roles; |
124 | } |
125 | |
126 | int DevicesModel::rowCount(const QModelIndex &parent) const |
127 | { |
128 | if (parent.isValid()) { |
129 | return 0; |
130 | } |
131 | return d->m_devices.size(); |
132 | } |
133 | |
134 | QVariant DevicesModel::data(const QModelIndex &index, int role) const |
135 | { |
136 | DevicePtr dev = device(index); |
137 | if (!dev) { |
138 | return QVariant(); |
139 | } |
140 | |
141 | switch (role) { |
142 | case Qt::DisplayRole: |
143 | return dev->name(); |
144 | case UbiRole: |
145 | return dev->ubi(); |
146 | case AddressRole: |
147 | return dev->address(); |
148 | case NameRole: |
149 | return dev->name(); |
150 | case FriendlyNameRole: |
151 | return dev->friendlyName(); |
152 | case RemoteNameRole: |
153 | return dev->remoteName(); |
154 | case ClassRole: |
155 | return dev->deviceClass(); |
156 | case TypeRole: |
157 | return dev->type(); |
158 | case AppearanceRole: |
159 | return dev->appearance(); |
160 | case IconRole: |
161 | return dev->icon(); |
162 | case PairedRole: |
163 | return dev->isPaired(); |
164 | case TrustedRole: |
165 | return dev->isTrusted(); |
166 | case BlockedRole: |
167 | return dev->isBlocked(); |
168 | case LegacyPairingRole: |
169 | return dev->hasLegacyPairing(); |
170 | case RssiRole: |
171 | return dev->rssi(); |
172 | case ConnectedRole: |
173 | return dev->isConnected(); |
174 | case UuidsRole: |
175 | return dev->uuids(); |
176 | case ModaliasRole: |
177 | return dev->modalias(); |
178 | case AdapterNameRole: |
179 | return dev->adapter()->name(); |
180 | case AdapterAddressRole: |
181 | return dev->adapter()->address(); |
182 | case AdapterPoweredRole: |
183 | return dev->adapter()->isPowered(); |
184 | case AdapterDiscoverableRole: |
185 | return dev->adapter()->isDiscoverable(); |
186 | case AdapterPairableRole: |
187 | return dev->adapter()->isPairable(); |
188 | case AdapterDiscoveringRole: |
189 | return dev->adapter()->isDiscovering(); |
190 | case AdapterUuidsRole: |
191 | return dev->adapter()->uuids(); |
192 | default: |
193 | return QVariant(); |
194 | } |
195 | } |
196 | |
197 | QModelIndex DevicesModel::index(int row, int column, const QModelIndex &parent) const |
198 | { |
199 | if (!hasIndex(row, column, parent)) { |
200 | return QModelIndex(); |
201 | } |
202 | return createIndex(arow: row, acolumn: 0); |
203 | } |
204 | |
205 | DevicePtr DevicesModel::device(const QModelIndex &index) const |
206 | { |
207 | if (!index.isValid()) { |
208 | return DevicePtr(); |
209 | } |
210 | return d->m_devices.at(i: index.row()); |
211 | } |
212 | |
213 | } // namespace BluezQt |
214 | |
215 | #include "moc_devicesmodel.cpp" |
216 | |