2017-06-06 16:48:30 +01:00
|
|
|
/*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
|
2017-06-06 16:48:30 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2017-06-06 16:48:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DBUSHELPERS_H
|
|
|
|
#define DBUSHELPERS_H
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KLocalizedString>
|
2017-06-06 16:48:30 +01:00
|
|
|
#include <QDBusPendingReply>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
template<typename T>
|
2017-06-06 16:48:30 +01:00
|
|
|
Q_REQUIRED_RESULT T blockOnReply(QDBusPendingReply<T> reply)
|
|
|
|
{
|
|
|
|
reply.waitForFinished();
|
|
|
|
if (reply.isError()) {
|
2022-10-17 22:17:49 +01:00
|
|
|
QTextStream(stderr) << i18n("error: ") << reply.error().message() << Qt::endl;
|
2017-06-06 16:48:30 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return reply.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void blockOnReply(QDBusPendingReply<void> reply)
|
|
|
|
{
|
|
|
|
reply.waitForFinished();
|
|
|
|
if (reply.isError()) {
|
2022-10-17 22:17:49 +01:00
|
|
|
QTextStream(stderr) << i18n("error: ") << reply.error().message() << Qt::endl;
|
2017-06-06 16:48:30 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 15:53:57 +01:00
|
|
|
template<typename T, typename W>
|
|
|
|
static void setWhenAvailable(const QDBusPendingReply<T> &pending, W func, QObject *parent)
|
|
|
|
{
|
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pending, parent);
|
|
|
|
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, parent, [func](QDBusPendingCallWatcher *watcher) {
|
|
|
|
watcher->deleteLater();
|
|
|
|
QDBusPendingReply<T> reply = *watcher;
|
2024-09-09 12:31:31 +01:00
|
|
|
func(reply.isError(), reply.value());
|
2023-07-23 15:53:57 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 16:48:30 +01:00
|
|
|
#endif
|