service/upower: start upower dbus service if inactive

This commit is contained in:
outfoxxed 2024-07-12 13:44:09 -07:00
parent d630cc7f76
commit e823b0adae
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
5 changed files with 94 additions and 4 deletions

View file

@ -9,6 +9,7 @@ qt_add_dbus_interface(DBUS_INTERFACES
qt_add_library(quickshell-dbus STATIC
properties.cpp
bus.cpp
${DBUS_INTERFACES}
)

57
src/dbus/bus.cpp Normal file
View file

@ -0,0 +1,57 @@
#include "bus.hpp" // NOLINT
#include <functional>
#include <qcontainerfwd.h>
#include <qdbusconnection.h>
#include <qdbusmessage.h>
#include <qdbuspendingcall.h>
#include <qdbuspendingreply.h>
#include <qlogging.h>
#include <qloggingcategory.h>
#include <qobject.h>
#include <qtypes.h>
#include <qvariant.h>
namespace qs::dbus {
Q_LOGGING_CATEGORY(logDbus, "quickshell.dbus", QtWarningMsg);
void tryLaunchService(
QObject* parent,
QDBusConnection& connection,
const QString& serviceName,
const std::function<void(bool)>& callback
) {
qCDebug(logDbus) << "Attempting to launch service" << serviceName;
auto message = QDBusMessage::createMethodCall(
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"StartServiceByName"
);
message << serviceName << 0u;
auto pendingCall = connection.asyncCall(message);
auto* call = new QDBusPendingCallWatcher(pendingCall, parent);
auto responseCallback = [callback, serviceName](QDBusPendingCallWatcher* call) {
const QDBusPendingReply<quint32> reply = *call;
if (reply.isError()) {
qCWarning(logDbus).noquote().nospace()
<< "Could not launch service " << serviceName << ": " << reply.error();
callback(false);
} else {
qCDebug(logDbus) << "Service launch successful for" << serviceName;
callback(true);
}
delete call;
};
QObject::connect(call, &QDBusPendingCallWatcher::finished, parent, responseCallback);
}
} // namespace qs::dbus

18
src/dbus/bus.hpp Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include <functional>
#include <qcontainerfwd.h>
#include <qdbusconnection.h>
#include <qobject.h>
namespace qs::dbus {
void tryLaunchService(
QObject* parent,
QDBusConnection& connection,
const QString& serviceName,
std::function<void(bool)> callback
);
}

View file

@ -14,6 +14,7 @@
#include <qtmetamacros.h>
#include "../../core/model.hpp"
#include "../../dbus/bus.hpp"
#include "../../dbus/properties.hpp"
#include "dbus_service.h"
#include "device.hpp"
@ -23,7 +24,7 @@ namespace qs::service::upower {
Q_LOGGING_CATEGORY(logUPower, "quickshell.service.upower", QtWarningMsg);
UPower::UPower() {
qCDebug(logUPower) << "Starting UPower";
qCDebug(logUPower) << "Starting UPower Service";
auto bus = QDBusConnection::systemBus();
@ -36,10 +37,22 @@ UPower::UPower() {
new DBusUPowerService("org.freedesktop.UPower", "/org/freedesktop/UPower", bus, this);
if (!this->service->isValid()) {
qCWarning(logUPower) << "Cannot connect to the UPower service.";
return;
}
qCDebug(logUPower) << "UPower service is not currently running, attempting to start it.";
dbus::tryLaunchService(this, bus, "org.freedesktop.UPower", [this](bool success) {
if (success) {
qCDebug(logUPower) << "Successfully launched UPower service.";
this->init();
} else {
qCWarning(logUPower) << "Could not start UPower. The UPower service will not work.";
}
});
} else {
this->init();
}
}
void UPower::init() {
QObject::connect(
&this->pOnBattery,
&dbus::AbstractDBusProperty::changed,

View file

@ -35,6 +35,7 @@ private slots:
private:
explicit UPower();
void init();
void registerExisting();
void registerDevice(const QString& path);