DtkCore
DTK Core module
ddbussender.h
浏览该文件的文档.
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
17class LIBDTKCORESHARED_EXPORT DDBusData
18{
19public:
20 DDBusData();
21
22 QString service;
23 QString path;
24 QString interface;
25 QString queryName;
26 QDBusConnection connection;
27};
28
29class LIBDTKCORESHARED_EXPORT DDBusCaller
30{
31 friend class DDBusSender;
32
33public:
34 QDBusPendingCall call();
35
36 template <typename T>
37 DDBusCaller arg(const T &argument);
38
39private:
40 explicit DDBusCaller(const QString &method, std::shared_ptr<DDBusData> data);
41
42private:
43 std::shared_ptr<DDBusData> m_dbusData;
44 QString m_methodName;
45 QVariantList m_arguments;
46};
47
48template <typename T>
49DDBusCaller DDBusCaller::arg(const T &argument)
50{
51 m_arguments << QVariant::fromValue(argument);
52
53 return *this;
54}
55
56class LIBDTKCORESHARED_EXPORT DDBusProperty
57{
58 friend class DDBusSender;
59
60public:
61 QDBusPendingCall get();
62 template <typename T>
63 QDBusPendingCall set(const T &value);
64
65private:
66 explicit DDBusProperty(const QString &property, std::shared_ptr<DDBusData> data);
67
68private:
69 std::shared_ptr<DDBusData> m_dbusData;
70 QString m_propertyName;
71};
72
73template <typename T>
74QDBusPendingCall DDBusProperty::set(const T &value)
75{
76 QDBusInterface iface(m_dbusData->service, m_dbusData->path, QStringLiteral("org.freedesktop.DBus.Properties"), m_dbusData->connection);
77
78 const QVariantList args = { QVariant::fromValue(m_dbusData->interface), QVariant::fromValue(m_propertyName), QVariant::fromValue(QDBusVariant(value)) };
79
80 return iface.asyncCallWithArgumentList(QStringLiteral("Set"), args);
81}
82
83class LIBDTKCORESHARED_EXPORT DDBusSender
84{
85public:
86 explicit DDBusSender();
87
88 DDBusSender service(const QString &service);
89 DDBusSender interface(const QString &interface);
90 DDBusSender path(const QString &path);
91 DDBusCaller method(const QString &method);
92 DDBusProperty property(const QString &property);
93
94private:
95 DDBusSender type(const QDBusConnection::BusType busType);
96
97private:
98 std::shared_ptr<DDBusData> m_dbusData;
99};
100
101#endif // DDBUSSENDER_H
DBus接口调用工具类
Definition: ddbussender.h:30
QDBusPendingCall call()
发起实际调用
DDBusCaller arg(const T &argument)
添加调用参数
Definition: ddbussender.h:49
DBus数据存储类
Definition: ddbussender.h:18
QString service
请求调用服务名
Definition: ddbussender.h:22
QString path
请求调用对象路径
Definition: ddbussender.h:23
QDBusConnection connection
进行调用的维护的DBus连接
Definition: ddbussender.h:26
QString interface
请求调用接口名
Definition: ddbussender.h:24
QString queryName
请求调用函数名
Definition: ddbussender.h:25
DBus属性操作对象
Definition: ddbussender.h:57
QDBusPendingCall get()
获取属性值
QDBusPendingCall set(const T &value)
设置属性值
Definition: ddbussender.h:74
DBus请求工具类
Definition: ddbussender.h:84
DDBusCaller method(const QString &method)
设置请求方法名并获取请求调用对象
DDBusSender interface(const QString &interface)
设置请求接口名
DDBusProperty property(const QString &property)
设置访问的属性名并获得属性操作对象
DDBusSender service(const QString &service)
设置请求服务名
DDBusSender path(const QString &path)
设置请求对象路径