BluezQt

declarativemanager.cpp
1/*
2 * BluezQt - Asynchronous Bluez wrapper library
3 *
4 * SPDX-FileCopyrightText: 2014-2015 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 "declarativemanager.h"
10#include "adapter.h"
11#include "declarativeadapter.h"
12#include "declarativedevice.h"
13#include "device.h"
14#include "initmanagerjob.h"
15#include <algorithm>
16static qsizetype adaptersCountFunction(QQmlListProperty<DeclarativeAdapter> *property)
17{
18 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
19 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
20
21 return manager->m_adapters.count();
22}
23
24static DeclarativeAdapter *adaptersAtFunction(QQmlListProperty<DeclarativeAdapter> *property, qsizetype index)
25{
26 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
27 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
28
29 return manager->m_adapters.values().at(index);
30}
31
32static qsizetype devicesCountFunction(QQmlListProperty<DeclarativeDevice> *property)
33{
34 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
35 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
36
37 return manager->m_devices.count();
38}
39
40static DeclarativeDevice *devicesAtFunction(QQmlListProperty<DeclarativeDevice> *property, qsizetype index)
41{
42 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
43 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
44
45 return manager->m_devices.values().at(index);
46}
47
48static qsizetype connectedDevicesCountFunction(QQmlListProperty<DeclarativeDevice> *property)
49{
50 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
51 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
52
53 return std::ranges::count_if(manager->m_devices.values(), [](DeclarativeDevice *device) {
54 return device->isConnected();
55 });
56}
57
58static DeclarativeDevice *connectedDevicesAtFunction(QQmlListProperty<DeclarativeDevice> *property, qsizetype index)
59{
60 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
61 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
62
63 int i = 0;
64 for (const auto device : manager->m_devices) {
65 if (device->isConnected()) {
66 if (index == i) {
67 return device;
68 }
69 i++;
70 }
71 }
72 return nullptr;
73}
74
75DeclarativeManager::DeclarativeManager(QObject *parent)
76 : BluezQt::Manager(parent)
77{
79 job->start();
80 connect(job, &BluezQt::InitManagerJob::result, this, &DeclarativeManager::initJobResult);
81
82 connect(this, &BluezQt::Manager::adapterAdded, this, &DeclarativeManager::slotAdapterAdded);
83 connect(this, &BluezQt::Manager::adapterRemoved, this, &DeclarativeManager::slotAdapterRemoved);
84 connect(this, &BluezQt::Manager::usableAdapterChanged, this, &DeclarativeManager::slotUsableAdapterChanged);
85 connect(this, &BluezQt::Manager::deviceAdded, this, &DeclarativeManager::slotDeviceAdded);
86 connect(this, &BluezQt::Manager::deviceRemoved, this, &DeclarativeManager::slotDeviceRemoved);
87
88 connect(this, &BluezQt::Manager::adapterChanged, this, [this](const BluezQt::AdapterPtr &adapter) {
89 Q_EMIT adapterChanged(declarativeAdapterFromPtr(adapter));
90 });
91
92 connect(this, &BluezQt::Manager::deviceChanged, this, [this](const BluezQt::DevicePtr &device) {
93 Q_EMIT deviceChanged(declarativeDeviceFromPtr(device));
94 });
95}
96
97DeclarativeAdapter *DeclarativeManager::usableAdapter() const
98{
99 return declarativeAdapterFromPtr(BluezQt::Manager::usableAdapter());
100}
101
102QQmlListProperty<DeclarativeAdapter> DeclarativeManager::declarativeAdapters()
103{
104 return QQmlListProperty<DeclarativeAdapter>(this, nullptr, adaptersCountFunction, adaptersAtFunction);
105}
106
107QQmlListProperty<DeclarativeDevice> DeclarativeManager::declarativeDevices()
108{
109 return QQmlListProperty<DeclarativeDevice>(this, nullptr, devicesCountFunction, devicesAtFunction);
110}
111
112QQmlListProperty<DeclarativeDevice> DeclarativeManager::declarativeConnectedDevices()
113{
114 return QQmlListProperty<DeclarativeDevice>(this, nullptr, connectedDevicesCountFunction, connectedDevicesAtFunction);
115}
116
117DeclarativeAdapter *DeclarativeManager::declarativeAdapterFromPtr(BluezQt::AdapterPtr ptr) const
118{
119 if (!ptr) {
120 return nullptr;
121 }
122 return m_adapters.value(ptr->ubi());
123}
124
125DeclarativeDevice *DeclarativeManager::declarativeDeviceFromPtr(BluezQt::DevicePtr ptr) const
126{
127 if (!ptr) {
128 return nullptr;
129 }
130 return m_devices.value(ptr->ubi());
131}
132
133DeclarativeAdapter *DeclarativeManager::adapterForAddress(const QString &address) const
134{
135 return declarativeAdapterFromPtr(BluezQt::Manager::adapterForAddress(address));
136}
137
138DeclarativeAdapter *DeclarativeManager::adapterForUbi(const QString &ubi) const
139{
140 return declarativeAdapterFromPtr(BluezQt::Manager::adapterForUbi(ubi));
141}
142
143DeclarativeDevice *DeclarativeManager::deviceForAddress(const QString &address) const
144{
145 return declarativeDeviceFromPtr(BluezQt::Manager::deviceForAddress(address));
146}
147
148DeclarativeDevice *DeclarativeManager::deviceForUbi(const QString &ubi) const
149{
150 return declarativeDeviceFromPtr(BluezQt::Manager::deviceForUbi(ubi));
151}
152
153void DeclarativeManager::initJobResult(BluezQt::InitManagerJob *job)
154{
155 if (job->error()) {
156 Q_EMIT initError(job->errorText());
157 return;
158 }
159
160 Q_EMIT initFinished();
161}
162
163void DeclarativeManager::slotAdapterAdded(BluezQt::AdapterPtr adapter)
164{
165 DeclarativeAdapter *dAdapter = new DeclarativeAdapter(adapter, this);
166 m_adapters[adapter->ubi()] = dAdapter;
167
168 Q_EMIT adapterAdded(dAdapter);
169 Q_EMIT adaptersChanged(declarativeAdapters());
170}
171
172void DeclarativeManager::slotAdapterRemoved(BluezQt::AdapterPtr adapter)
173{
174 DeclarativeAdapter *dAdapter = m_adapters.take(adapter->ubi());
175 dAdapter->deleteLater();
176
177 Q_EMIT adapterRemoved(dAdapter);
178 Q_EMIT adaptersChanged(declarativeAdapters());
179}
180
181void DeclarativeManager::slotDeviceAdded(BluezQt::DevicePtr device)
182{
183 DeclarativeAdapter *dAdapter = declarativeAdapterFromPtr(device->adapter());
184 DeclarativeDevice *dDevice = new DeclarativeDevice(device, dAdapter);
185 m_devices[device->ubi()] = dDevice;
186 dAdapter->m_devices[device->ubi()] = dDevice;
187
188 Q_EMIT deviceAdded(dDevice);
189 Q_EMIT devicesChanged(declarativeDevices());
190 connect(device.get(), &BluezQt::Device::connectedChanged, this, &DeclarativeManager::slotDeviceConnectedChanged);
191 if (device->isConnected()) {
192 Q_EMIT connectedDevicesChanged(declarativeConnectedDevices());
193 }
194}
195
196void DeclarativeManager::slotDeviceRemoved(BluezQt::DevicePtr device)
197{
198 DeclarativeDevice *dDevice = m_devices.take(device->ubi());
199 dDevice->adapter()->m_devices.take(device->ubi());
200 dDevice->deleteLater();
201
202 Q_EMIT deviceRemoved(dDevice);
203 Q_EMIT devicesChanged(declarativeDevices());
204 disconnect(device.get(), &BluezQt::Device::connectedChanged, this, &DeclarativeManager::slotDeviceConnectedChanged);
205 if (device->isConnected()) {
206 Q_EMIT connectedDevicesChanged(declarativeConnectedDevices());
207 }
208}
209
210void DeclarativeManager::slotDeviceConnectedChanged()
211{
212 Q_EMIT connectedDevicesChanged(declarativeConnectedDevices());
213}
214
215void DeclarativeManager::slotUsableAdapterChanged(BluezQt::AdapterPtr adapter)
216{
217 Q_EMIT usableAdapterChanged(declarativeAdapterFromPtr(adapter));
218}
219
220#include "moc_declarativemanager.cpp"
void connectedChanged(bool connected)
Indicates that device's connected state have changed.
Init manager job.
void result(InitManagerJob *job)
Indicates that the job have finished.
void start()
Starts the job asynchronously.
Definition job.cpp:35
void deviceAdded(DevicePtr device)
Indicates that a new device was added (eg.
void adapterChanged(AdapterPtr adapter)
Indicates that at least one of the adapter's properties have changed.
DevicePtr deviceForAddress(const QString &address) const
Returns a device for specified address.
Definition manager.cpp:110
DevicePtr deviceForUbi(const QString &ubi) const
Returns a device for specified UBI.
Definition manager.cpp:131
void adapterRemoved(AdapterPtr adapter)
Indicates that adapter was removed.
AdapterPtr adapterForUbi(const QString &ubi) const
Returns an adapter for specified UBI.
Definition manager.cpp:105
AdapterPtr adapterForAddress(const QString &address) const
Returns an adapter for specified address.
Definition manager.cpp:95
void usableAdapterChanged(AdapterPtr adapter)
Indicates that usable adapter have changed.
void deviceChanged(DevicePtr device)
Indicates that at least one of the device's properties have changed.
void deviceRemoved(DevicePtr device)
Indicates that a device was removed.
void adapterAdded(AdapterPtr adapter)
Indicates that adapter was added.
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
T take(const Key &key)
T value(const Key &key) const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool disconnect(const QMetaObject::Connection &connection)
T * get() const const
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 Fri Nov 29 2024 11:46:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.