summaryrefslogtreecommitdiffstats
path: root/src/dbus/doc/snippets/code/src_qdbus_qdbusabstractinterface.cpp
blob: 92b9dea909c1911bb7d6b4ca245d576896273ede (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include <QString>
#include <QDBusMessage>
#include <QDBusReply>
#include <QDBusInterface>

using namespace Qt::StringLiterals;

class Abstract_DBus_Interface : public QObject
{
    Q_OBJECT

public:
    Abstract_DBus_Interface(QObject *parent = nullptr)
    : QObject(parent) {
        interface = new QDBusInterface("org.example.Interface", "/Example/Methods");
    }

    ~Abstract_DBus_Interface() {  delete interface; }
    void interfaceMain();
    void asyncCall();
    QString retrieveValue() { return QString(); }

public slots:
    void callFinishedSlot();

private:
    QDBusInterface *interface;
};

void Abstract_DBus_Interface::interfaceMain()
{
//! [0]
QString value = retrieveValue();
QDBusMessage reply;

QDBusReply<int> api = interface->call("GetAPIVersion"_L1);
if (api >= 14)
  reply = interface->call("ProcessWorkUnicode"_L1, value);
else
  reply = interface->call("ProcessWork"_L1, "UTF-8"_L1, value.toUtf8());
//! [0]
}

void Abstract_DBus_Interface::asyncCall()
{
//! [1]
QDBusPendingCall pcall = interface->asyncCall("GetAPIVersion"_L1);
auto watcher = new QDBusPendingCallWatcher(pcall, this);

QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this,
                 [&](QDBusPendingCallWatcher *w) {
    QString value = retrieveValue();
    QDBusPendingReply<int> reply(*w);
    QDBusPendingCall pcall;
    if (reply.argumentAt<0>() >= 14)
        pcall = interface->asyncCall("ProcessWorkUnicode"_L1, value);
    else
        pcall = interface->asyncCall("ProcessWork"_L1, "UTF-8"_L1, value.toUtf8());

    w = new QDBusPendingCallWatcher(pcall);
    QObject::connect(w,  &QDBusPendingCallWatcher::finished, this,
                     &Abstract_DBus_Interface::callFinishedSlot);
});
//! [1]
}