Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
ClientHelpers.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
7#include <PhosphorProtocol/phosphorprotocol_export.h>
8
10
11#include <QDBusConnection>
12#include <QDBusPendingCall>
13#include <QDBusPendingCallWatcher>
14#include <QDBusPendingReply>
15#include <QLoggingCategory>
16#include <QObject>
17#include <QVariant>
18
19#include <utility>
20
21namespace PhosphorProtocol {
22
23PHOSPHORPROTOCOL_EXPORT const QLoggingCategory& lcPhosphorProtocol();
24
37{
38 return PhosphorDBus::Client(QDBusConnection::sessionBus(), Service::Name, Service::ObjectPath,
40}
41
48namespace ClientHelpers {
49
52inline void fireAndForget(QObject* parent, const QString& interface, const QString& method, const QVariantList& args,
53 const QString& logContext = {})
54{
55 daemonClient().fireAndForget(parent, interface, method, args, logContext);
56}
57
60inline void sendOneWay(const QString& interface, const QString& method, const QVariantList& args = {})
61{
62 daemonClient().sendOneWay(interface, method, args);
63}
64
67inline QDBusPendingCall asyncCall(const QString& interface, const QString& method, const QVariantList& args = {})
68{
69 return daemonClient().asyncCall(interface, method, args);
70}
71
75inline QDBusMessage syncCall(const QString& interface, const QString& method, const QVariantList& args = {})
76{
77 return daemonClient().syncCall(interface, method, args, Service::SyncCallTimeoutMs);
78}
79
99inline void reloadDaemonSettings(QObject* parent, const QString& logContext = {})
100{
101 fireAndForget(parent, Service::Interface::Settings, QStringLiteral("reloadSettings"), {}, logContext);
102}
103
123{
124 return syncCall(Service::Interface::Settings, QStringLiteral("reloadSettings")).type()
125 != QDBusMessage::ErrorMessage;
126}
127
139template<typename Fn>
140void loadSettingAsync(QObject* parent, const QString& name, Fn&& onValue)
141{
142 auto* watcher = new QDBusPendingCallWatcher(
143 daemonClient().asyncCall(Service::Interface::Settings, QStringLiteral("getSetting"), {name}), parent);
144 QObject::connect(watcher, &QDBusPendingCallWatcher::finished, parent,
145 [name, onValue = std::forward<Fn>(onValue)](QDBusPendingCallWatcher* w) {
146 w->deleteLater();
147 QDBusPendingReply<QVariant> reply = *w;
148 if (reply.isValid()) {
149 QVariant value = reply.value();
150 if (value.canConvert<QDBusVariant>()) {
151 value = value.value<QDBusVariant>().variant();
152 }
153 onValue(value);
154 qCDebug(lcPhosphorProtocol) << "Loaded" << name;
155 } else {
156 qCWarning(lcPhosphorProtocol)
157 << "loadSettingAsync: failed to load" << name << ":" << reply.error().message();
158 }
159 });
160}
161
162} // namespace ClientHelpers
163
164} // namespace PhosphorProtocol
A lightweight, service-agnostic D-Bus method-call client.
Definition Client.h:36
void sendOneWay(const QString &interface, const QString &method, const QVariantList &args={}) const
Send a one-way notification with no expected reply.
QDBusMessage syncCall(const QString &interface, const QString &method, const QVariantList &args={}, int timeoutMs=-1) const
Blocking method call with an explicit timeout.
QDBusPendingCall asyncCall(const QString &interface, const QString &method, const QVariantList &args={}) const
Issue an async method call and return the pending result.
void fireAndForget(QObject *parent, const QString &interface, const QString &method, const QVariantList &args, const QString &logContext={}) const
Fire-and-forget async call with error logging.
Daemon-bound convenience wrappers around PhosphorDBus::Client.
Definition ClientHelpers.h:48
void reloadDaemonSettings(QObject *parent, const QString &logContext={})
Ask the daemon to reparse config.json from disk.
Definition ClientHelpers.h:99
QDBusPendingCall asyncCall(const QString &interface, const QString &method, const QVariantList &args={})
Async method call to the daemon; caller attaches its own watcher.
Definition ClientHelpers.h:67
void sendOneWay(const QString &interface, const QString &method, const QVariantList &args={})
One-way notification to the daemon with no expected reply.
Definition ClientHelpers.h:60
void loadSettingAsync(QObject *parent, const QString &name, Fn &&onValue)
Async helper for loading a single daemon setting.
Definition ClientHelpers.h:140
void fireAndForget(QObject *parent, const QString &interface, const QString &method, const QVariantList &args, const QString &logContext={})
Fire-and-forget async call to the daemon, with error logging.
Definition ClientHelpers.h:52
bool reloadDaemonSettingsBlocking()
Blocking form of reloadDaemonSettings.
Definition ClientHelpers.h:122
QDBusMessage syncCall(const QString &interface, const QString &method, const QVariantList &args={})
Blocking daemon call bounded by Service::SyncCallTimeoutMs.
Definition ClientHelpers.h:75
constexpr QLatin1String Settings("org.phosphor_works.Placement.Settings")
constexpr QLatin1String ObjectPath("/org/phosphor_works/Placement")
constexpr int SyncCallTimeoutMs
Definition ServiceConstants.h:935
constexpr QLatin1String Name("org.phosphor_works.Placement")
The placement service's D-Bus identity.
D-Bus marshalling for the shared tiling-family value types (see AutotileTypes.h; the file names preda...
Definition AutotileMarshalling.h:19
PHOSPHORPROTOCOL_EXPORT const QLoggingCategory & lcPhosphorProtocol()
PhosphorDBus::Client daemonClient()
A PhosphorDBus::Client bound to the Phosphor daemon.
Definition ClientHelpers.h:36