1/*
2 * BluezQt - Asynchronous Bluez wrapper library
3 *
4 * SPDX-FileCopyrightText: 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 "declarativeadapter.h"
10#include "declarativedevice.h"
11static qsizetype devicesCountDeclarativeAdapterFunction(QQmlListProperty<DeclarativeDevice> *property)
12{
13 Q_ASSERT(qobject_cast<DeclarativeAdapter *>(property->object));
14 DeclarativeAdapter *adapter = static_cast<DeclarativeAdapter *>(property->object);
15
16 return adapter->m_devices.count();
17}
18
19static DeclarativeDevice *devicesAtDeclarativeAdapterFunction(QQmlListProperty<DeclarativeDevice> *property, qsizetype index)
20{
21 Q_ASSERT(qobject_cast<DeclarativeAdapter *>(property->object));
22 DeclarativeAdapter *adapter = static_cast<DeclarativeAdapter *>(property->object);
23
24 return adapter->m_devices.values().at(i: index);
25}
26
27DeclarativeAdapter::DeclarativeAdapter(BluezQt::AdapterPtr adapter, QObject *parent)
28 : QObject(parent)
29 , m_adapter(adapter)
30{
31 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::nameChanged, context: this, slot: &DeclarativeAdapter::nameChanged);
32 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::systemNameChanged, context: this, slot: &DeclarativeAdapter::systemNameChanged);
33 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::adapterClassChanged, context: this, slot: &DeclarativeAdapter::adapterClassChanged);
34 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::poweredChanged, context: this, slot: &DeclarativeAdapter::poweredChanged);
35 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::discoverableChanged, context: this, slot: &DeclarativeAdapter::discoverableChanged);
36 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::discoverableTimeoutChanged, context: this, slot: &DeclarativeAdapter::discoverableTimeoutChanged);
37 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::pairableChanged, context: this, slot: &DeclarativeAdapter::pairableChanged);
38 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::pairableTimeoutChanged, context: this, slot: &DeclarativeAdapter::pairableTimeoutChanged);
39 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::discoveringChanged, context: this, slot: &DeclarativeAdapter::discoveringChanged);
40 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::uuidsChanged, context: this, slot: &DeclarativeAdapter::uuidsChanged);
41 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::modaliasChanged, context: this, slot: &DeclarativeAdapter::modaliasChanged);
42
43 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::deviceAdded, context: this, slot: &DeclarativeAdapter::slotDeviceAdded);
44 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::deviceRemoved, context: this, slot: &DeclarativeAdapter::slotDeviceRemoved);
45
46 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::adapterRemoved, context: this, slot: [this]() {
47 Q_EMIT adapterRemoved(adapter: this);
48 });
49
50 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::adapterChanged, context: this, slot: [this]() {
51 Q_EMIT adapterChanged(adapter: this);
52 });
53
54 connect(sender: m_adapter.data(), signal: &BluezQt::Adapter::deviceChanged, context: this, slot: [this](const BluezQt::DevicePtr &device) {
55 Q_EMIT deviceChanged(device: declarativeDeviceFromPtr(ptr: device));
56 });
57}
58
59QString DeclarativeAdapter::ubi() const
60{
61 return m_adapter->ubi();
62}
63
64QString DeclarativeAdapter::address() const
65{
66 return m_adapter->address();
67}
68
69QString DeclarativeAdapter::name() const
70{
71 return m_adapter->name();
72}
73
74void DeclarativeAdapter::setName(const QString &name)
75{
76 m_adapter->setName(name);
77}
78
79QString DeclarativeAdapter::systemName() const
80{
81 return m_adapter->systemName();
82}
83
84quint32 DeclarativeAdapter::adapterClass() const
85{
86 return m_adapter->adapterClass();
87}
88
89bool DeclarativeAdapter::isPowered() const
90{
91 return m_adapter->isPowered();
92}
93
94void DeclarativeAdapter::setPowered(bool powered)
95{
96 m_adapter->setPowered(powered);
97}
98
99bool DeclarativeAdapter::isDiscoverable() const
100{
101 return m_adapter->isDiscoverable();
102}
103
104void DeclarativeAdapter::setDiscoverable(bool discoverable)
105{
106 m_adapter->setDiscoverable(discoverable);
107}
108
109quint32 DeclarativeAdapter::discoverableTimeout() const
110{
111 return m_adapter->discoverableTimeout();
112}
113
114void DeclarativeAdapter::setDiscoverableTimeout(quint32 timeout)
115{
116 m_adapter->setDiscoverableTimeout(timeout);
117}
118
119bool DeclarativeAdapter::isPairable() const
120{
121 return m_adapter->isPairable();
122}
123
124void DeclarativeAdapter::setPairable(bool pairable)
125{
126 m_adapter->setPairable(pairable);
127}
128
129quint32 DeclarativeAdapter::pairableTimeout() const
130{
131 return m_adapter->pairableTimeout();
132}
133
134void DeclarativeAdapter::setPairableTimeout(quint32 timeout)
135{
136 m_adapter->setPairableTimeout(timeout);
137}
138
139bool DeclarativeAdapter::isDiscovering()
140{
141 return m_adapter->isDiscovering();
142}
143
144QStringList DeclarativeAdapter::uuids() const
145{
146 return m_adapter->uuids();
147}
148
149QString DeclarativeAdapter::modalias() const
150{
151 return m_adapter->modalias();
152}
153
154QQmlListProperty<DeclarativeDevice> DeclarativeAdapter::devices()
155{
156 return QQmlListProperty<DeclarativeDevice>(this, nullptr, devicesCountDeclarativeAdapterFunction, devicesAtDeclarativeAdapterFunction);
157}
158
159DeclarativeDevice *DeclarativeAdapter::deviceForAddress(const QString &address) const
160{
161 return declarativeDeviceFromPtr(ptr: m_adapter->deviceForAddress(address));
162}
163
164BluezQt::PendingCall *DeclarativeAdapter::startDiscovery()
165{
166 return m_adapter->startDiscovery();
167}
168
169BluezQt::PendingCall *DeclarativeAdapter::stopDiscovery()
170{
171 return m_adapter->stopDiscovery();
172}
173
174BluezQt::PendingCall *DeclarativeAdapter::removeDevice(DeclarativeDevice *device)
175{
176 return m_adapter->removeDevice(device: m_adapter->deviceForAddress(address: device->address()));
177}
178
179void DeclarativeAdapter::slotDeviceAdded(BluezQt::DevicePtr device)
180{
181 Q_EMIT deviceFound(device: declarativeDeviceFromPtr(ptr: device));
182 Q_EMIT devicesChanged(devices: devices());
183}
184
185void DeclarativeAdapter::slotDeviceRemoved(BluezQt::DevicePtr device)
186{
187 Q_EMIT deviceRemoved(device: declarativeDeviceFromPtr(ptr: device));
188 Q_EMIT devicesChanged(devices: devices());
189}
190
191DeclarativeDevice *DeclarativeAdapter::declarativeDeviceFromPtr(BluezQt::DevicePtr ptr) const
192{
193 if (!ptr) {
194 return nullptr;
195 }
196 return m_devices.value(key: ptr->ubi());
197}
198
199#include "moc_declarativeadapter.cpp"
200

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