Debian Codegraph @

src/dbus/doc/snippets/code/src_qdbus_qdbusabstractinterface.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include <QString>
5 #include <QDBusMessage>
6 #include <QDBusReply>
7 #include <QDBusInterface>
8
9 using namespace Qt::StringLiterals;
10
11 class Abstract_DBus_Interface : public QObject
12 {
13 Q_OBJECT
14
15 public:
16 Abstract_DBus_Interface(QObject *parent = nullptr)
17 : QObject(parent) {
18 interface = new QDBusInterface("org.example.Interface", "/Example/Methods");
19 }
20
21 ~Abstract_DBus_Interface() { delete interface; }
22 void interfaceMain();
23 void asyncCall();
24 QString retrieveValue() { return QString(); }
25
26 public slots:
27 void callFinishedSlot();
28
29 private:
30 QDBusInterface *interface;
31 };
32
33 void Abstract_DBus_Interface::interfaceMain()
34 {
35 //! [0]
36 QString value = retrieveValue();
37 QDBusMessage reply;
38
39 QDBusReply<int> api = interface->call("GetAPIVersion"_L1);
40 if (api >= 14)
41 reply = interface->call("ProcessWorkUnicode"_L1, value);
42 else
43 reply = interface->call("ProcessWork"_L1, "UTF-8"_L1, value.toUtf8());
44 //! [0]
45 }
46
47 void Abstract_DBus_Interface::asyncCall()
48 {
49 //! [1]
50 QDBusPendingCall pcall = interface->asyncCall("GetAPIVersion"_L1);
51 auto watcher = new QDBusPendingCallWatcher(pcall, this);
52
53 QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this,
54 [&](QDBusPendingCallWatcher *w) {
55 QString value = retrieveValue();
56 QDBusPendingReply<int> reply(*w);
57 QDBusPendingCall pcall;
58 if (reply.argumentAt<0>() >= 14)
59 pcall = interface->asyncCall("ProcessWorkUnicode"_L1, value);
60 else
61 pcall = interface->asyncCall("ProcessWork"_L1, "UTF-8"_L1, value.toUtf8());
62
63 w = new QDBusPendingCallWatcher(pcall);
64 QObject::connect(w, &QDBusPendingCallWatcher::finished, this,
65 &Abstract_DBus_Interface::callFinishedSlot);
66 });
67 //! [1]
68 }