BluezQt

declarativemanager.cpp
1 /*
2  * BluezQt - Asynchronous Bluez wrapper library
3  *
4  * SPDX-FileCopyrightText: 2014-2015 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 "declarativemanager.h"
10 #include "adapter.h"
11 #include "declarativeadapter.h"
12 #include "declarativedevice.h"
13 #include "device.h"
14 #include "initmanagerjob.h"
15 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
16 static qsizetype adaptersCountFunction(QQmlListProperty<DeclarativeAdapter> *property)
17 #else
18 static int adaptersCountFunction(QQmlListProperty<DeclarativeAdapter> *property)
19 #endif
20 {
21  Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
22  DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
23 
24  return manager->m_adapters.count();
25 }
26 
27 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
28 static DeclarativeAdapter *adaptersAtFunction(QQmlListProperty<DeclarativeAdapter> *property, qsizetype index)
29 #else
30 static DeclarativeAdapter *adaptersAtFunction(QQmlListProperty<DeclarativeAdapter> *property, int index)
31 #endif
32 {
33  Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
34  DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
35 
36  return manager->m_adapters.values().at(index);
37 }
38 
39 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
40 static qsizetype devicesCountFunction(QQmlListProperty<DeclarativeDevice> *property)
41 #else
42 static int devicesCountFunction(QQmlListProperty<DeclarativeDevice> *property)
43 #endif
44 {
45  Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
46  DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
47 
48  return manager->m_devices.count();
49 }
50 
51 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
52 static DeclarativeDevice *devicesAtFunction(QQmlListProperty<DeclarativeDevice> *property, qsizetype index)
53 #else
54 static DeclarativeDevice *devicesAtFunction(QQmlListProperty<DeclarativeDevice> *property, int index)
55 #endif
56 {
57  Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
58  DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
59 
60  return manager->m_devices.values().at(index);
61 }
62 
63 DeclarativeManager::DeclarativeManager(QObject *parent)
64  : BluezQt::Manager(parent)
65 {
67  job->start();
68  connect(job, &BluezQt::InitManagerJob::result, this, &DeclarativeManager::initJobResult);
69 
70  connect(this, &BluezQt::Manager::adapterAdded, this, &DeclarativeManager::slotAdapterAdded);
71  connect(this, &BluezQt::Manager::adapterRemoved, this, &DeclarativeManager::slotAdapterRemoved);
72  connect(this, &BluezQt::Manager::usableAdapterChanged, this, &DeclarativeManager::slotUsableAdapterChanged);
73  connect(this, &BluezQt::Manager::deviceAdded, this, &DeclarativeManager::slotDeviceAdded);
74  connect(this, &BluezQt::Manager::deviceRemoved, this, &DeclarativeManager::slotDeviceRemoved);
75 
76  connect(this, &BluezQt::Manager::adapterChanged, this, [this](const BluezQt::AdapterPtr &adapter) {
77  Q_EMIT adapterChanged(declarativeAdapterFromPtr(adapter));
78  });
79 
80  connect(this, &BluezQt::Manager::deviceChanged, this, [this](const BluezQt::DevicePtr &device) {
81  Q_EMIT deviceChanged(declarativeDeviceFromPtr(device));
82  });
83 }
84 
85 DeclarativeAdapter *DeclarativeManager::usableAdapter() const
86 {
87  return declarativeAdapterFromPtr(BluezQt::Manager::usableAdapter());
88 }
89 
90 QQmlListProperty<DeclarativeAdapter> DeclarativeManager::declarativeAdapters()
91 {
92  return QQmlListProperty<DeclarativeAdapter>(this, nullptr, adaptersCountFunction, adaptersAtFunction);
93 }
94 
95 QQmlListProperty<DeclarativeDevice> DeclarativeManager::declarativeDevices()
96 {
97  return QQmlListProperty<DeclarativeDevice>(this, nullptr, devicesCountFunction, devicesAtFunction);
98 }
99 
100 DeclarativeAdapter *DeclarativeManager::declarativeAdapterFromPtr(BluezQt::AdapterPtr ptr) const
101 {
102  if (!ptr) {
103  return nullptr;
104  }
105  return m_adapters.value(ptr->ubi());
106 }
107 
108 DeclarativeDevice *DeclarativeManager::declarativeDeviceFromPtr(BluezQt::DevicePtr ptr) const
109 {
110  if (!ptr) {
111  return nullptr;
112  }
113  return m_devices.value(ptr->ubi());
114 }
115 
116 DeclarativeAdapter *DeclarativeManager::adapterForAddress(const QString &address) const
117 {
118  return declarativeAdapterFromPtr(BluezQt::Manager::adapterForAddress(address));
119 }
120 
121 DeclarativeAdapter *DeclarativeManager::adapterForUbi(const QString &ubi) const
122 {
123  return declarativeAdapterFromPtr(BluezQt::Manager::adapterForUbi(ubi));
124 }
125 
126 DeclarativeDevice *DeclarativeManager::deviceForAddress(const QString &address) const
127 {
128  return declarativeDeviceFromPtr(BluezQt::Manager::deviceForAddress(address));
129 }
130 
131 DeclarativeDevice *DeclarativeManager::deviceForUbi(const QString &ubi) const
132 {
133  return declarativeDeviceFromPtr(BluezQt::Manager::deviceForUbi(ubi));
134 }
135 
136 void DeclarativeManager::initJobResult(BluezQt::InitManagerJob *job)
137 {
138  if (job->error()) {
139  Q_EMIT initError(job->errorText());
140  return;
141  }
142 
143  Q_EMIT initFinished();
144 }
145 
146 void DeclarativeManager::slotAdapterAdded(BluezQt::AdapterPtr adapter)
147 {
148  DeclarativeAdapter *dAdapter = new DeclarativeAdapter(adapter, this);
149  m_adapters[adapter->ubi()] = dAdapter;
150 
151  Q_EMIT adapterAdded(dAdapter);
152  Q_EMIT adaptersChanged(declarativeAdapters());
153 }
154 
155 void DeclarativeManager::slotAdapterRemoved(BluezQt::AdapterPtr adapter)
156 {
157  DeclarativeAdapter *dAdapter = m_adapters.take(adapter->ubi());
158  dAdapter->deleteLater();
159 
160  Q_EMIT adapterRemoved(dAdapter);
161  Q_EMIT adaptersChanged(declarativeAdapters());
162 }
163 
164 void DeclarativeManager::slotDeviceAdded(BluezQt::DevicePtr device)
165 {
166  DeclarativeAdapter *dAdapter = declarativeAdapterFromPtr(device->adapter());
167  DeclarativeDevice *dDevice = new DeclarativeDevice(device, dAdapter);
168  m_devices[device->ubi()] = dDevice;
169  dAdapter->m_devices[device->ubi()] = dDevice;
170 
171  Q_EMIT deviceAdded(dDevice);
172  Q_EMIT devicesChanged(declarativeDevices());
173 }
174 
175 void DeclarativeManager::slotDeviceRemoved(BluezQt::DevicePtr device)
176 {
177  DeclarativeDevice *dDevice = m_devices.take(device->ubi());
178  dDevice->adapter()->m_devices.take(device->ubi());
179  dDevice->deleteLater();
180 
181  Q_EMIT deviceRemoved(dDevice);
182  Q_EMIT devicesChanged(declarativeDevices());
183 }
184 
185 void DeclarativeManager::slotUsableAdapterChanged(BluezQt::AdapterPtr adapter)
186 {
187  Q_EMIT usableAdapterChanged(declarativeAdapterFromPtr(adapter));
188 }
void result(InitManagerJob *job)
Indicates that the job have finished.
Q_EMITQ_EMIT
QCA_EXPORT void init()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void deviceChanged(DevicePtr device)
Indicates that at least one of the device's properties have changed.
void adapterAdded(AdapterPtr adapter)
Indicates that adapter was added.
void deviceRemoved(DevicePtr device)
Indicates that a device was removed.
AdapterPtr adapterForAddress(const QString &address) const
Returns an adapter for specified address.
Definition: manager.cpp:96
DevicePtr deviceForAddress(const QString &address) const
Returns a device for specified address.
Definition: manager.cpp:111
void adapterRemoved(AdapterPtr adapter)
Indicates that adapter was removed.
void adapterChanged(AdapterPtr adapter)
Indicates that at least one of the adapter's properties have changed.
void start()
Starts the job asynchronously.
Definition: job.cpp:38
DevicePtr deviceForUbi(const QString &ubi) const
Returns a device for specified UBI.
Definition: manager.cpp:132
AdapterPtr adapterForUbi(const QString &ubi) const
Returns an adapter for specified UBI.
Definition: manager.cpp:106
void usableAdapterChanged(AdapterPtr adapter)
Indicates that usable adapter have changed.
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.