File: dbushelpers.h

package info (click to toggle)
kdeconnect 25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: cpp: 22,922; xml: 520; python: 92; sh: 25; makefile: 5
file content (45 lines) | stat: -rw-r--r-- 1,265 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
 *
 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */

#ifndef DBUSHELPERS_H
#define DBUSHELPERS_H

#include <KLocalizedString>
#include <QDBusPendingReply>
#include <QTextStream>

template<typename T>
Q_REQUIRED_RESULT T blockOnReply(QDBusPendingReply<T> reply)
{
    reply.waitForFinished();
    if (reply.isError()) {
        QTextStream(stderr) << i18n("error: ") << reply.error().message() << Qt::endl;
        exit(1);
    }
    return reply.value();
}

void blockOnReply(QDBusPendingReply<void> reply)
{
    reply.waitForFinished();
    if (reply.isError()) {
        QTextStream(stderr) << i18n("error: ") << reply.error().message() << Qt::endl;
        exit(1);
    }
}

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;
        func(reply.isError(), reply.value());
    });
}

#endif