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 7c5632ef5f
Signed by untrusted user: 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,
const std::function<void(bool)>& callback
);
}