BluezQt

devicesmodel.cpp
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
14namespace BluezQt
15{
16class DevicesModelPrivate : public QObject
17{
18public:
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
33DevicesModelPrivate::DevicesModelPrivate(DevicesModel *qq)
34 : QObject(qq)
35 , q(qq)
36 , m_manager(nullptr)
37{
38}
39
40void DevicesModelPrivate::init()
41{
42 m_devices = m_manager->devices();
43
44 connect(m_manager, &Manager::deviceAdded, this, &DevicesModelPrivate::deviceAdded);
45 connect(m_manager, &Manager::deviceRemoved, this, &DevicesModelPrivate::deviceRemoved);
46 connect(m_manager, &Manager::deviceChanged, this, &DevicesModelPrivate::deviceChanged);
47 connect(m_manager, &Manager::adapterChanged, this, &DevicesModelPrivate::adapterChanged);
48}
49
50void DevicesModelPrivate::deviceAdded(DevicePtr device)
51{
52 q->beginInsertRows(QModelIndex(), m_devices.size(), m_devices.size());
53 m_devices.append(device);
54 q->endInsertRows();
55}
56
57void DevicesModelPrivate::deviceRemoved(DevicePtr device)
58{
59 int offset = m_devices.indexOf(device);
60 Q_ASSERT(offset >= 0);
61
62 q->beginRemoveRows(QModelIndex(), offset, offset);
63 m_devices.removeAt(offset);
64 q->endRemoveRows();
65}
66
67void DevicesModelPrivate::deviceChanged(DevicePtr device)
68{
69 int offset = m_devices.indexOf(device);
70 Q_ASSERT(offset >= 0);
71
72 QModelIndex idx = q->createIndex(offset, 0);
73 Q_EMIT q->dataChanged(idx, idx);
74}
75
76void DevicesModelPrivate::adapterChanged(AdapterPtr adapter)
77{
78 const auto devices = adapter->devices();
79 for (const DevicePtr &device : devices) {
80 deviceChanged(device);
81 }
82}
83
84DevicesModel::DevicesModel(Manager *manager, QObject *parent)
85 : QAbstractListModel(parent)
86 , d(new DevicesModelPrivate(this))
87{
88 d->m_manager = manager;
89 d->init();
90}
91
93
95{
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
126int DevicesModel::rowCount(const QModelIndex &parent) const
127{
128 if (parent.isValid()) {
129 return 0;
130 }
131 return d->m_devices.size();
132}
133
134QVariant 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();
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();
181 return dev->adapter()->address();
183 return dev->adapter()->isPowered();
185 return dev->adapter()->isDiscoverable();
187 return dev->adapter()->isPairable();
189 return dev->adapter()->isDiscovering();
190 case AdapterUuidsRole:
191 return dev->adapter()->uuids();
192 default:
193 return QVariant();
194 }
195}
196
197QModelIndex DevicesModel::index(int row, int column, const QModelIndex &parent) const
198{
199 if (!hasIndex(row, column, parent)) {
200 return QModelIndex();
201 }
202 return createIndex(row, 0);
203}
204
206{
207 if (!index.isValid()) {
208 return DevicePtr();
209 }
210 return d->m_devices.at(index.row());
211}
212
213} // namespace BluezQt
214
215#include "moc_devicesmodel.cpp"
@ AdapterNameRole
Name of the associated adapter (QString)
@ LegacyPairingRole
Indicates whether the device has legacy pairing (bool)
@ NameRole
Name of the device (QString)
@ AdapterDiscoveringRole
Indicates whether the associated adapter is discovering (bool)
@ IconRole
Icon name of the device (QString)
@ AdapterPairableRole
Indicates whether the associated adapter is pairable (bool)
@ FriendlyNameRole
Friendly name of the device (QString)
@ RemoteNameRole
Remote name of the device (QString)
@ AdapterAddressRole
Address of the associated adapter (QString)
@ TypeRole
Type of the device (Device::Type)
@ UbiRole
UBI of the device (QString)
@ RssiRole
Received Signal Strength Indicator of the device (qint16)
@ ConnectedRole
Indicates whether the device is connected (bool)
@ PairedRole
Indicates whether the device is paired (bool)
@ ModaliasRole
Modalias of the device (QString)
@ AdapterDiscoverableRole
Indicates whether the associated adapter is discoverable (bool)
@ ClassRole
Class of the device (quint32)
@ AddressRole
Address of the device (QString)
@ AdapterUuidsRole
UUIDs of supported services by the associated adapter (QStringList)
@ UuidsRole
UUIDs of supported services by the device (QStringList)
@ AdapterPoweredRole
Indicates whether the associated adapter is powered (bool)
@ AppearanceRole
Appearance of the device (quint16)
@ TrustedRole
Indicates whether the device is trusted (bool)
@ BlockedRole
Indicates whether the device is blocked (bool)
~DevicesModel() override
Destroys a DevicesModel object.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Reimplemented from QAbstractListModel::index()
QVariant data(const QModelIndex &index, int role) const override
Reimplemented from QAbstractListModel::data()
QHash< int, QByteArray > roleNames() const override
Reimplemented from QAbstractListModel::roleNames()
DevicePtr device(const QModelIndex &index) const
Returns a device for specified index.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Reimplemented from QAbstractListModel::rowCount()
Bluetooth manager.
QModelIndex createIndex(int row, int column, const void *ptr) const const
bool hasIndex(int row, int column, const QModelIndex &parent) const const
virtual QHash< int, QByteArray > roleNames() const const
bool isValid() const const
int row() const const
QObject * parent() const const
DisplayRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.