1111#include < qloggingcategory.h>
1212#include < qobject.h>
1313#include < qqmllist.h>
14- #include < qtmetamacros.h>
1514
1615#include " ../../core/model.hpp"
1716#include " ../../dbus/bus.hpp"
@@ -53,15 +52,23 @@ UPower::UPower() {
5352}
5453
5554void 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+
5664 this ->serviceProperties .setInterface (this ->service );
5765 this ->serviceProperties .updateAllViaGetAll ();
5866
59- this ->registerExisting ();
67+ this ->registerDisplayDevice ();
68+ this ->registerDevices ();
6069}
6170
62- void UPower::registerExisting () {
63- this ->registerDevice (" /org/freedesktop/UPower/devices/DisplayDevice" );
64-
71+ void UPower::registerDevices () {
6572 auto pending = this ->service ->EnumerateDevices ();
6673 auto * call = new QDBusPendingCallWatcher (pending, this );
6774
@@ -82,34 +89,46 @@ void UPower::registerExisting() {
8289 QObject::connect (call, &QDBusPendingCallWatcher::finished, this , responseCallback);
8390}
8491
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 );
8795
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;
9498
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);
97110}
98111
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 ()); }
101113
102- this ->mDevices .remove (device->path ());
114+ void UPower::onDeviceRemoved (const QDBusObjectPath& path) {
115+ auto iter = this ->mDevices .find (path.path ());
103116
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." ;
109125 }
126+ }
127+
128+ void UPower::onDeviceReady () {
129+ auto * device = qobject_cast<UPowerDevice*>(this ->sender ());
110130
111- this ->readyDevices .removeObject (device);
112- qCDebug (logUPower) << " UPowerDevice" << device->path () << " destroyed" ;
131+ this ->readyDevices .insertObject (device);
113132}
114133
115134void UPower::registerDevice (const QString& path) {
@@ -118,21 +137,22 @@ void UPower::registerDevice(const QString& path) {
118137 return ;
119138 }
120139
121- auto * device = new UPowerDevice (path, this );
140+ auto * device = new UPowerDevice (this );
141+ device->init (path);
142+
122143 if (!device->isValid ()) {
123144 qCWarning (logUPower) << " Ignoring invalid UPowerDevice registration of" << path;
124145 delete device;
125146 return ;
126147 }
127148
128149 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);
131151
132152 qCDebug (logUPower) << " Registered UPowerDevice" << path;
133153}
134154
135- UPowerDevice* UPower::displayDevice () { return this ->mDisplayDevice ; }
155+ UPowerDevice* UPower::displayDevice () { return & this ->mDisplayDevice ; }
136156
137157ObjectModel<UPowerDevice>* UPower::devices () { return &this ->readyDevices ; }
138158
@@ -142,12 +162,6 @@ UPower* UPower::instance() {
142162}
143163
144164UPowerQml::UPowerQml (QObject* parent): QObject(parent) {
145- QObject::connect (
146- UPower::instance (),
147- &UPower::displayDeviceChanged,
148- this ,
149- &UPowerQml::displayDeviceChanged
150- );
151165 QObject::connect (
152166 UPower::instance (),
153167 &UPower::onBatteryChanged,
0 commit comments