File indexing completed on 2024-02-25 05:06:50

0001 /*
0002  * SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #ifndef DBUSHELPERS_H
0008 #define DBUSHELPERS_H
0009 
0010 #include <KLocalizedString>
0011 #include <QDBusPendingReply>
0012 #include <QTextStream>
0013 
0014 template<typename T>
0015 Q_REQUIRED_RESULT T blockOnReply(QDBusPendingReply<T> reply)
0016 {
0017     reply.waitForFinished();
0018     if (reply.isError()) {
0019         QTextStream(stderr) << i18n("error: ") << reply.error().message() << Qt::endl;
0020         exit(1);
0021     }
0022     return reply.value();
0023 }
0024 
0025 void blockOnReply(QDBusPendingReply<void> reply)
0026 {
0027     reply.waitForFinished();
0028     if (reply.isError()) {
0029         QTextStream(stderr) << i18n("error: ") << reply.error().message() << Qt::endl;
0030         exit(1);
0031     }
0032 }
0033 
0034 template<typename T, typename W>
0035 static void setWhenAvailable(const QDBusPendingReply<T> &pending, W func, QObject *parent)
0036 {
0037     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pending, parent);
0038     QObject::connect(watcher, &QDBusPendingCallWatcher::finished, parent, [func](QDBusPendingCallWatcher *watcher) {
0039         watcher->deleteLater();
0040         QDBusPendingReply<T> reply = *watcher;
0041         func(reply.value());
0042     });
0043 }
0044 
0045 #endif