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"
15static qsizetype adaptersCountFunction(QQmlListProperty<DeclarativeAdapter> *property)
16{
17 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
18 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
19
20 return manager->m_adapters.count();
21}
22
23static DeclarativeAdapter *adaptersAtFunction(QQmlListProperty<DeclarativeAdapter> *property, qsizetype index)
24{
25 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
26 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
27
28 return manager->m_adapters.values().at(i: index);
29}
30
31static qsizetype devicesCountFunction(QQmlListProperty<DeclarativeDevice> *property)
32{
33 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
34 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
35
36 return manager->m_devices.count();
37}
38
39static DeclarativeDevice *devicesAtFunction(QQmlListProperty<DeclarativeDevice> *property, qsizetype index)
40{
41 Q_ASSERT(qobject_cast<DeclarativeManager *>(property->object));
42 DeclarativeManager *manager = static_cast<DeclarativeManager *>(property->object);
43
44 return manager->m_devices.values().at(i: index);
45}
46
47DeclarativeManager::DeclarativeManager(QObject *parent)
48 : BluezQt::Manager(parent)
49{
50 BluezQt::InitManagerJob *job = init();
51 job->start();
52 connect(sender: job, signal: &BluezQt::InitManagerJob::result, context: this, slot: &DeclarativeManager::initJobResult);
53
54 connect(sender: this, signal: &BluezQt::Manager::adapterAdded, context: this, slot: &DeclarativeManager::slotAdapterAdded);
55 connect(sender: this, signal: &BluezQt::Manager::adapterRemoved, context: this, slot: &DeclarativeManager::slotAdapterRemoved);
56 connect(sender: this, signal: &BluezQt::Manager::usableAdapterChanged, context: this, slot: &DeclarativeManager::slotUsableAdapterChanged);
57 connect(sender: this, signal: &BluezQt::Manager::deviceAdded, context: this, slot: &DeclarativeManager::slotDeviceAdded);
58 connect(sender: this, signal: &BluezQt::Manager::deviceRemoved, context: this, slot: &DeclarativeManager::slotDeviceRemoved);
59
60 connect(sender: this, signal: &BluezQt::Manager::adapterChanged, context: this, slot: [this](const BluezQt::AdapterPtr &adapter) {
61 Q_EMIT adapterChanged(adapter: declarativeAdapterFromPtr(ptr: adapter));
62 });
63
64 connect(sender: this, signal: &BluezQt::Manager::deviceChanged, context: this, slot: [this](const BluezQt::DevicePtr &device) {
65 Q_EMIT deviceChanged(device: declarativeDeviceFromPtr(ptr: device));
66 });
67}
68
69DeclarativeAdapter *DeclarativeManager::usableAdapter() const
70{
71 return declarativeAdapterFromPtr(ptr: BluezQt::Manager::usableAdapter());
72}
73
74QQmlListProperty<DeclarativeAdapter> DeclarativeManager::declarativeAdapters()
75{
76 return QQmlListProperty<DeclarativeAdapter>(this, nullptr, adaptersCountFunction, adaptersAtFunction);
77}
78
79QQmlListProperty<DeclarativeDevice> DeclarativeManager::declarativeDevices()
80{
81 return QQmlListProperty<DeclarativeDevice>(this, nullptr, devicesCountFunction, devicesAtFunction);
82}
83
84DeclarativeAdapter *DeclarativeManager::declarativeAdapterFromPtr(BluezQt::AdapterPtr ptr) const
85{
86 if (!ptr) {
87 return nullptr;
88 }
89 return m_adapters.value(key: ptr->ubi());
90}
91
92DeclarativeDevice *DeclarativeManager::declarativeDeviceFromPtr(BluezQt::DevicePtr ptr) const
93{
94 if (!ptr) {
95 return nullptr;
96 }
97 return m_devices.value(key: ptr->ubi());
98}
99
100DeclarativeAdapter *DeclarativeManager::adapterForAddress(const QString &address) const
101{
102 return declarativeAdapterFromPtr(ptr: BluezQt::Manager::adapterForAddress(address));
103}
104
105DeclarativeAdapter *DeclarativeManager::adapterForUbi(const QString &ubi) const
106{
107 return declarativeAdapterFromPtr(ptr: BluezQt::Manager::adapterForUbi(ubi));
108}
109
110DeclarativeDevice *DeclarativeManager::deviceForAddress(const QString &address) const
111{
112 return declarativeDeviceFromPtr(ptr: BluezQt::Manager::deviceForAddress(address));
113}
114
115DeclarativeDevice *DeclarativeManager::deviceForUbi(const QString &ubi) const
116{
117 return declarativeDeviceFromPtr(ptr: BluezQt::Manager::deviceForUbi(ubi));
118}
119
120void DeclarativeManager::initJobResult(BluezQt::InitManagerJob *job)
121{
122 if (job->error()) {
123 Q_EMIT initError(errorText: job->errorText());
124 return;
125 }
126
127 Q_EMIT initFinished();
128}
129
130void DeclarativeManager::slotAdapterAdded(BluezQt::AdapterPtr adapter)
131{
132 DeclarativeAdapter *dAdapter = new DeclarativeAdapter(adapter, this);
133 m_adapters[adapter->ubi()] = dAdapter;
134
135 Q_EMIT adapterAdded(adapter: dAdapter);
136 Q_EMIT adaptersChanged(adapters: declarativeAdapters());
137}
138
139void DeclarativeManager::slotAdapterRemoved(BluezQt::AdapterPtr adapter)
140{
141 DeclarativeAdapter *dAdapter = m_adapters.take(key: adapter->ubi());
142 dAdapter->deleteLater();
143
144 Q_EMIT adapterRemoved(adapter: dAdapter);
145 Q_EMIT adaptersChanged(adapters: declarativeAdapters());
146}
147
148void DeclarativeManager::slotDeviceAdded(BluezQt::DevicePtr device)
149{
150 DeclarativeAdapter *dAdapter = declarativeAdapterFromPtr(ptr: device->adapter());
151 DeclarativeDevice *dDevice = new DeclarativeDevice(device, dAdapter);
152 m_devices[device->ubi()] = dDevice;
153 dAdapter->m_devices[device->ubi()] = dDevice;
154
155 Q_EMIT deviceAdded(device: dDevice);
156 Q_EMIT devicesChanged(devices: declarativeDevices());
157}
158
159void DeclarativeManager::slotDeviceRemoved(BluezQt::DevicePtr device)
160{
161 DeclarativeDevice *dDevice = m_devices.take(key: device->ubi());
162 dDevice->adapter()->m_devices.take(key: device->ubi());
163 dDevice->deleteLater();
164
165 Q_EMIT deviceRemoved(device: dDevice);
166 Q_EMIT devicesChanged(devices: declarativeDevices());
167}
168
169void DeclarativeManager::slotUsableAdapterChanged(BluezQt::AdapterPtr adapter)
170{
171 Q_EMIT usableAdapterChanged(adapter: declarativeAdapterFromPtr(ptr: adapter));
172}
173
174#include "moc_declarativemanager.cpp"
175

source code of bluez-qt/src/imports/declarativemanager.cpp