Debian Codegraph @

include/util/ddbussender.h

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #ifndef DDBUSSENDER_H
6 #define DDBUSSENDER_H
7
8 #include "dtkcore_global.h"
9
10 #include <QObject>
11 #include <QDBusConnection>
12 #include <QDBusPendingCall>
13 #include <QDBusInterface>
14
15 #include <memory>
16
17 class LIBDTKCORESHARED_EXPORT DDBusData
18 {
19 public:
20 DDBusData();
21 QDBusPendingCall asyncCallWithArguments(const QString &method, const QVariantList &arguments, const QString &iface = QString());
22
23 QString service;
24 QString path;
25 QString interface;
26 QString queryName;
27 QDBusConnection connection;
28 };
29
30 class LIBDTKCORESHARED_EXPORT DDBusCaller
31 {
32 friend class DDBusSender;
33
34 public:
35 QDBusPendingCall call();
36
37 template <typename T>
38 DDBusCaller arg(const T &argument);
39
40 private:
41 explicit DDBusCaller(const QString &method, std::shared_ptr<DDBusData> data);
42
43 private:
44 std::shared_ptr<DDBusData> m_dbusData;
45 QString m_methodName;
46 QVariantList m_arguments;
47 };
48
49 template <typename T>
50 DDBusCaller DDBusCaller::arg(const T &argument)
51 {
52 m_arguments << QVariant::fromValue(argument);
53
54 return *this;
55 }
56
57 class LIBDTKCORESHARED_EXPORT DDBusProperty
58 {
59 friend class DDBusSender;
60
61 public:
62 QDBusPendingCall get();
63 template <typename T>
64 QDBusPendingCall set(const T &value);
65
66 private:
67 explicit DDBusProperty(const QString &property, std::shared_ptr<DDBusData> data);
68
69 private:
70 std::shared_ptr<DDBusData> m_dbusData;
71 QString m_propertyName;
72 };
73
74 template <typename T>
75 QDBusPendingCall DDBusProperty::set(const T &value)
76 {
77 QVariantList args{QVariant::fromValue(m_dbusData->interface), QVariant::fromValue(m_propertyName), QVariant::fromValue(QDBusVariant(value))};
78 return m_dbusData->asyncCallWithArguments(QStringLiteral("Set"), args, QStringLiteral("org.freedesktop.DBus.Properties"));
79 }
80
81 class LIBDTKCORESHARED_EXPORT DDBusSender
82 {
83 public:
84 explicit DDBusSender();
85
86 DDBusSender service(const QString &service);
87 DDBusSender interface(const QString &interface);
88 DDBusSender path(const QString &path);
89 DDBusCaller method(const QString &method);
90 DDBusProperty property(const QString &property);
91
92 static DDBusSender system();
93 private:
94 DDBusSender type(const QDBusConnection::BusType busType);
95
96 private:
97 std::shared_ptr<DDBusData> m_dbusData;
98 };
99
100 #endif // DDBUSSENDER_H