BluezQt

devicesmodel.cpp
1 /*
2  * BluezQt - Asynchronous Bluez wrapper library
3  *
4  * SPDX-FileCopyrightText: 2014 David Rosca <[email protected]>
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(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 
50 void 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 
57 void 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 
67 void 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 
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 
85  : QAbstractListModel(parent)
86  , d(new DevicesModelPrivate(this))
87 {
88  d->m_manager = manager;
89  d->init();
90 }
91 
93 {
94  delete d;
95 }
96 
98 {
100 
101  roles[UbiRole] = QByteArrayLiteral("Ubi");
102  roles[AddressRole] = QByteArrayLiteral("Address");
103  roles[NameRole] = QByteArrayLiteral("Name");
104  roles[FriendlyNameRole] = QByteArrayLiteral("FriendlyName");
105  roles[RemoteNameRole] = QByteArrayLiteral("RemoteName");
106  roles[ClassRole] = QByteArrayLiteral("Class");
107  roles[TypeRole] = QByteArrayLiteral("Type");
108  roles[AppearanceRole] = QByteArrayLiteral("Appearance");
109  roles[IconRole] = QByteArrayLiteral("Icon");
110  roles[PairedRole] = QByteArrayLiteral("Paired");
111  roles[TrustedRole] = QByteArrayLiteral("Trusted");
112  roles[BlockedRole] = QByteArrayLiteral("Blocked");
113  roles[LegacyPairingRole] = QByteArrayLiteral("LegacyPairing");
114  roles[RssiRole] = QByteArrayLiteral("Rssi");
115  roles[ConnectedRole] = QByteArrayLiteral("Connected");
116  roles[UuidsRole] = QByteArrayLiteral("Uuids");
117  roles[ModaliasRole] = QByteArrayLiteral("Modalias");
118  roles[AdapterNameRole] = QByteArrayLiteral("AdapterName");
119  roles[AdapterAddressRole] = QByteArrayLiteral("AdapterAddress");
120  roles[AdapterPoweredRole] = QByteArrayLiteral("AdapterPowered");
121  roles[AdapterDiscoverableRole] = QByteArrayLiteral("AdapterDiscoverable");
122  roles[AdapterPairableRole] = QByteArrayLiteral("AdapterPairable");
123  roles[AdapterDiscoveringRole] = QByteArrayLiteral("AdapterDiscovering");
124  roles[AdapterUuidsRole] = QByteArrayLiteral("AdapterUuids");
125 
126  return roles;
127 }
128 
129 int DevicesModel::rowCount(const QModelIndex &parent) const
130 {
131  if (parent.isValid()) {
132  return 0;
133  }
134  return d->m_devices.size();
135 }
136 
137 QVariant DevicesModel::data(const QModelIndex &index, int role) const
138 {
139  DevicePtr dev = device(index);
140  if (!dev) {
141  return QVariant();
142  }
143 
144  switch (role) {
145  case Qt::DisplayRole:
146  return dev->name();
147  case UbiRole:
148  return dev->ubi();
149  case AddressRole:
150  return dev->address();
151  case NameRole:
152  return dev->name();
153  case FriendlyNameRole:
154  return dev->friendlyName();
155  case RemoteNameRole:
156  return dev->remoteName();
157  case ClassRole:
158  return dev->deviceClass();
159  case TypeRole:
160  return dev->type();
161  case AppearanceRole:
162  return dev->appearance();
163  case IconRole:
164  return dev->icon();
165  case PairedRole:
166  return dev->isPaired();
167  case TrustedRole:
168  return dev->isTrusted();
169  case BlockedRole:
170  return dev->isBlocked();
171  case LegacyPairingRole:
172  return dev->hasLegacyPairing();
173  case RssiRole:
174  return dev->rssi();
175  case ConnectedRole:
176  return dev->isConnected();
177  case UuidsRole:
178  return dev->uuids();
179  case ModaliasRole:
180  return dev->modalias();
181  case AdapterNameRole:
182  return dev->adapter()->name();
183  case AdapterAddressRole:
184  return dev->adapter()->address();
185  case AdapterPoweredRole:
186  return dev->adapter()->isPowered();
188  return dev->adapter()->isDiscoverable();
189  case AdapterPairableRole:
190  return dev->adapter()->isPairable();
192  return dev->adapter()->isDiscovering();
193  case AdapterUuidsRole:
194  return dev->adapter()->uuids();
195  default:
196  return QVariant();
197  }
198 }
199 
200 QModelIndex DevicesModel::index(int row, int column, const QModelIndex &parent) const
201 {
202  if (!hasIndex(row, column, parent)) {
203  return QModelIndex();
204  }
205  return createIndex(row, 0);
206 }
207 
209 {
210  if (!index.isValid()) {
211  return DevicePtr();
212  }
213  return d->m_devices.at(index.row());
214 }
215 
216 } // namespace BluezQt
@ TrustedRole
Indicates whether the device is trusted (bool)
Definition: devicesmodel.h:71
@ ModaliasRole
Modalias of the device (QString)
Definition: devicesmodel.h:83
QHash< int, QByteArray > roleNames() const override
Reimplemented from QAbstractListModel::roleNames()
@ BlockedRole
Indicates whether the device is blocked (bool)
Definition: devicesmodel.h:73
DisplayRole
DevicePtr device(const QModelIndex &index) const
Returns a device for specified index.
@ IconRole
Icon name of the device (QString)
Definition: devicesmodel.h:67
QCA_EXPORT void init()
@ AdapterDiscoveringRole
Indicates whether the associated adapter is discovering (bool)
Definition: devicesmodel.h:95
@ UbiRole
UBI of the device (QString)
Definition: devicesmodel.h:51
@ LegacyPairingRole
Indicates whether the device has legacy pairing (bool)
Definition: devicesmodel.h:75
@ TypeRole
Type of the device (Device::Type)
Definition: devicesmodel.h:63
@ AdapterUuidsRole
UUIDs of supported services by the associated adapter (QStringList)
Definition: devicesmodel.h:97
bool hasIndex(int row, int column, const QModelIndex &parent) const const
void deviceChanged(DevicePtr device)
Indicates that at least one of the device's properties have changed.
@ ConnectedRole
Indicates whether the device is connected (bool)
Definition: devicesmodel.h:79
@ AdapterAddressRole
Address of the associated adapter (QString)
Definition: devicesmodel.h:87
@ AdapterPoweredRole
Indicates whether the associated adapter is powered (bool)
Definition: devicesmodel.h:89
virtual QHash< int, QByteArray > roleNames() const const
@ AdapterPairableRole
Indicates whether the associated adapter is pairable (bool)
Definition: devicesmodel.h:93
QModelIndex createIndex(int row, int column, void *ptr) const const
~DevicesModel() override
Destroys a DevicesModel object.
QVariant data(const QModelIndex &index, int role) const override
Reimplemented from QAbstractListModel::data()
@ NameRole
Name of the device (QString)
Definition: devicesmodel.h:55
void deviceRemoved(DevicePtr device)
Indicates that a device was removed.
@ UuidsRole
UUIDs of supported services by the device (QStringList)
Definition: devicesmodel.h:81
@ AddressRole
Address of the device (QString)
Definition: devicesmodel.h:53
void adapterChanged(AdapterPtr adapter)
Indicates that at least one of the adapter's properties have changed.
@ RssiRole
Received Signal Strength Indicator of the device (qint16)
Definition: devicesmodel.h:77
bool isValid() const const
int row() const const
@ AdapterNameRole
Name of the associated adapter (QString)
Definition: devicesmodel.h:85
DevicesModel(Manager *manager, QObject *parent=nullptr)
Creates a new DevicesModel object.
@ RemoteNameRole
Remote name of the device (QString)
Definition: devicesmodel.h:59
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Reimplemented from QAbstractListModel::rowCount()
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Reimplemented from QAbstractListModel::index()
@ ClassRole
Class of the device (quint32)
Definition: devicesmodel.h:61
@ AdapterDiscoverableRole
Indicates whether the associated adapter is discoverable (bool)
Definition: devicesmodel.h:91
@ AppearanceRole
Appearance of the device (quint16)
Definition: devicesmodel.h:65
@ FriendlyNameRole
Friendly name of the device (QString)
Definition: devicesmodel.h:57
@ PairedRole
Indicates whether the device is paired (bool)
Definition: devicesmodel.h:69
QObject * parent() const const
void deviceAdded(DevicePtr device)
Indicates that a new device was added (eg.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Sep 25 2022 04:19:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.