11
11
#include < qloggingcategory.h>
12
12
#include < qobject.h>
13
13
#include < qqmllist.h>
14
- #include < qtmetamacros.h>
15
14
16
15
#include " ../../core/model.hpp"
17
16
#include " ../../dbus/bus.hpp"
@@ -53,15 +52,23 @@ UPower::UPower() {
53
52
}
54
53
55
54
void UPower::init () {
55
+ QObject::connect (this ->service , &DBusUPowerService::DeviceAdded, this , &UPower::onDeviceAdded);
56
+
57
+ QObject::connect (
58
+ this ->service ,
59
+ &DBusUPowerService::DeviceRemoved,
60
+ this ,
61
+ &UPower::onDeviceRemoved
62
+ );
63
+
56
64
this ->serviceProperties .setInterface (this ->service );
57
65
this ->serviceProperties .updateAllViaGetAll ();
58
66
59
- this ->registerExisting ();
67
+ this ->registerDisplayDevice ();
68
+ this ->registerDevices ();
60
69
}
61
70
62
- void UPower::registerExisting () {
63
- this ->registerDevice (" /org/freedesktop/UPower/devices/DisplayDevice" );
64
-
71
+ void UPower::registerDevices () {
65
72
auto pending = this ->service ->EnumerateDevices ();
66
73
auto * call = new QDBusPendingCallWatcher (pending, this );
67
74
@@ -82,34 +89,46 @@ void UPower::registerExisting() {
82
89
QObject::connect (call, &QDBusPendingCallWatcher::finished, this , responseCallback);
83
90
}
84
91
85
- void UPower::onDeviceReady () {
86
- auto * device = qobject_cast<UPowerDevice*>(this ->sender ());
92
+ void UPower::registerDisplayDevice () {
93
+ auto pending = this ->service ->GetDisplayDevice ();
94
+ auto * call = new QDBusPendingCallWatcher (pending, this );
87
95
88
- if (device->path () == " /org/freedesktop/UPower/devices/DisplayDevice" ) {
89
- this ->mDisplayDevice = device;
90
- emit this ->displayDeviceChanged ();
91
- qCDebug (logUPower) << " Display UPowerDevice" << device->path () << " ready" ;
92
- return ;
93
- }
96
+ auto responseCallback = [this ](QDBusPendingCallWatcher* call) {
97
+ const QDBusPendingReply<QDBusObjectPath> reply = *call;
94
98
95
- this ->readyDevices .insertObject (device);
96
- qCDebug (logUPower) << " UPowerDevice" << device->path () << " ready" ;
99
+ if (reply.isError ()) {
100
+ qCWarning (logUPower) << " Failed to get default device:" << reply.error ().message ();
101
+ } else {
102
+ qCDebug (logUPower) << " UPower default device registered at" << reply.value ().path ();
103
+ this ->mDisplayDevice .init (reply.value ().path ());
104
+ }
105
+
106
+ delete call;
107
+ };
108
+
109
+ QObject::connect (call, &QDBusPendingCallWatcher::finished, this , responseCallback);
97
110
}
98
111
99
- void UPower::onDeviceDestroyed (QObject* object) {
100
- auto * device = static_cast <UPowerDevice*>(object); // NOLINT
112
+ void UPower::onDeviceAdded (const QDBusObjectPath& path) { this ->registerDevice (path.path ()); }
101
113
102
- this ->mDevices .remove (device->path ());
114
+ void UPower::onDeviceRemoved (const QDBusObjectPath& path) {
115
+ auto iter = this ->mDevices .find (path.path ());
103
116
104
- if (device == this ->mDisplayDevice ) {
105
- this ->mDisplayDevice = nullptr ;
106
- emit this ->displayDeviceChanged ();
107
- qCDebug (logUPower) << " Display UPowerDevice" << device->path () << " destroyed" ;
108
- return ;
117
+ if (iter == this ->mDevices .end ()) {
118
+ qCWarning (logUPower) << " UPower service sent removal signal for" << path.path ()
119
+ << " which is not registered." ;
120
+ } else {
121
+ auto * device = iter.value ();
122
+ this ->mDevices .erase (iter);
123
+ this ->readyDevices .removeObject (device);
124
+ qCDebug (logUPower) << " UPowerDevice" << device->path () << " removed." ;
109
125
}
126
+ }
127
+
128
+ void UPower::onDeviceReady () {
129
+ auto * device = qobject_cast<UPowerDevice*>(this ->sender ());
110
130
111
- this ->readyDevices .removeObject (device);
112
- qCDebug (logUPower) << " UPowerDevice" << device->path () << " destroyed" ;
131
+ this ->readyDevices .insertObject (device);
113
132
}
114
133
115
134
void UPower::registerDevice (const QString& path) {
@@ -118,21 +137,22 @@ void UPower::registerDevice(const QString& path) {
118
137
return ;
119
138
}
120
139
121
- auto * device = new UPowerDevice (path, this );
140
+ auto * device = new UPowerDevice (this );
141
+ device->init (path);
142
+
122
143
if (!device->isValid ()) {
123
144
qCWarning (logUPower) << " Ignoring invalid UPowerDevice registration of" << path;
124
145
delete device;
125
146
return ;
126
147
}
127
148
128
149
this ->mDevices .insert (path, device);
129
- QObject::connect (device, &UPowerDevice::ready, this , &UPower::onDeviceReady);
130
- QObject::connect (device, &QObject::destroyed, this , &UPower::onDeviceDestroyed);
150
+ QObject::connect (device, &UPowerDevice::readyChanged, this , &UPower::onDeviceReady);
131
151
132
152
qCDebug (logUPower) << " Registered UPowerDevice" << path;
133
153
}
134
154
135
- UPowerDevice* UPower::displayDevice () { return this ->mDisplayDevice ; }
155
+ UPowerDevice* UPower::displayDevice () { return & this ->mDisplayDevice ; }
136
156
137
157
ObjectModel<UPowerDevice>* UPower::devices () { return &this ->readyDevices ; }
138
158
@@ -142,12 +162,6 @@ UPower* UPower::instance() {
142
162
}
143
163
144
164
UPowerQml::UPowerQml (QObject* parent): QObject(parent) {
145
- QObject::connect (
146
- UPower::instance (),
147
- &UPower::displayDeviceChanged,
148
- this ,
149
- &UPowerQml::displayDeviceChanged
150
- );
151
165
QObject::connect (
152
166
UPower::instance (),
153
167
&UPower::onBatteryChanged,
0 commit comments