Skip to content

Commit 5d7e075

Browse files
committed
bluetooth: fix defaultAdapter reactivity
Fixes #100
1 parent 87d99b8 commit 5d7e075

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/bluetooth/bluez.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Bluez* Bluez::instance() {
2626

2727
Bluez::Bluez() { this->init(); }
2828

29+
void Bluez::updateDefaultAdapter() {
30+
const auto& adapters = this->mAdapters.valueList();
31+
this->bDefaultAdapter = adapters.empty() ? nullptr : adapters.first();
32+
}
33+
2934
void Bluez::init() {
3035
qCDebug(logBluetooth) << "Connecting to BlueZ";
3136

@@ -95,6 +100,7 @@ void Bluez::onInterfacesAdded(
95100

96101
this->mAdapterMap.insert(path.path(), adapter);
97102
this->mAdapters.insertObject(adapter);
103+
this->updateDefaultAdapter();
98104
} else if (interfaces.contains("org.bluez.Device1")) {
99105
auto* device = new BluetoothDevice(path.path(), this);
100106

@@ -127,6 +133,7 @@ void Bluez::onInterfacesRemoved(const QDBusObjectPath& path, const QStringList&
127133

128134
this->mAdapterMap.remove(path.path());
129135
this->mAdapters.removeObject(adapter);
136+
this->updateDefaultAdapter();
130137
delete adapter;
131138
}
132139
} else if (auto* device = this->mDeviceMap.value(path.path())) {
@@ -148,9 +155,8 @@ void Bluez::onInterfacesRemoved(const QDBusObjectPath& path, const QStringList&
148155
}
149156
}
150157

151-
BluetoothAdapter* Bluez::defaultAdapter() const {
152-
const auto& adapters = this->mAdapters.valueList();
153-
return adapters.isEmpty() ? nullptr : adapters.first();
158+
BluezQml::BluezQml() {
159+
QObject::connect(Bluez::instance(), &Bluez::defaultAdapterChanged, this, &BluezQml::defaultAdapterChanged);
154160
}
155161

156162
} // namespace qs::bluetooth

src/bluetooth/bluez.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <qcontainerfwd.h>
44
#include <qhash.h>
55
#include <qobject.h>
6+
#include <qproperty.h>
67
#include <qqmlintegration.h>
78
#include <qtmetamacros.h>
89

@@ -22,18 +23,21 @@ class Bluez: public QObject {
2223
public:
2324
[[nodiscard]] ObjectModel<BluetoothAdapter>* adapters() { return &this->mAdapters; }
2425
[[nodiscard]] ObjectModel<BluetoothDevice>* devices() { return &this->mDevices; }
25-
[[nodiscard]] BluetoothAdapter* defaultAdapter() const;
2626

2727
[[nodiscard]] BluetoothAdapter* adapter(const QString& path) {
2828
return this->mAdapterMap.value(path);
2929
}
3030

3131
static Bluez* instance();
3232

33+
signals:
34+
void defaultAdapterChanged();
35+
3336
private slots:
3437
void
3538
onInterfacesAdded(const QDBusObjectPath& path, const DBusObjectManagerInterfaces& interfaces);
3639
void onInterfacesRemoved(const QDBusObjectPath& path, const QStringList& interfaces);
40+
void updateDefaultAdapter();
3741

3842
private:
3943
explicit Bluez();
@@ -44,14 +48,17 @@ private slots:
4448
QHash<QString, BluetoothDevice*> mDeviceMap;
4549
ObjectModel<BluetoothAdapter> mAdapters {this};
4650
ObjectModel<BluetoothDevice> mDevices {this};
51+
52+
public:
53+
Q_OBJECT_BINDABLE_PROPERTY(Bluez, BluetoothAdapter*, bDefaultAdapter, &Bluez::defaultAdapterChanged);
4754
};
4855

4956
///! Bluetooth manager
5057
/// Provides access to bluetooth devices and adapters.
5158
class BluezQml: public QObject {
5259
Q_OBJECT;
5360
/// The default bluetooth adapter. Usually there is only one.
54-
Q_PROPERTY(BluetoothAdapter* defaultAdapter READ defaultAdapter CONSTANT);
61+
Q_PROPERTY(BluetoothAdapter* defaultAdapter READ default NOTIFY defaultAdapterChanged BINDABLE bindableDefaultAdapter);
5562
QSDOC_TYPE_OVERRIDE(ObjectModel<qs::bluetooth::BluetoothAdapter>*);
5663
/// A list of all bluetooth adapters. See @@defaultAdapter for the default.
5764
Q_PROPERTY(UntypedObjectModel* adapters READ adapters CONSTANT);
@@ -62,8 +69,11 @@ class BluezQml: public QObject {
6269
QML_NAMED_ELEMENT(Bluetooth);
6370
QML_SINGLETON;
6471

72+
signals:
73+
void defaultAdapterChanged();
74+
6575
public:
66-
explicit BluezQml(QObject* parent = nullptr): QObject(parent) {}
76+
explicit BluezQml();
6777

6878
[[nodiscard]] static ObjectModel<BluetoothAdapter>* adapters() {
6979
return Bluez::instance()->adapters();
@@ -73,8 +83,8 @@ class BluezQml: public QObject {
7383
return Bluez::instance()->devices();
7484
}
7585

76-
[[nodiscard]] static BluetoothAdapter* defaultAdapter() {
77-
return Bluez::instance()->defaultAdapter();
86+
[[nodiscard]] static QBindable<BluetoothAdapter*> bindableDefaultAdapter() {
87+
return &Bluez::instance()->bDefaultAdapter;
7888
}
7989
};
8090

0 commit comments

Comments
 (0)