Debian Codegraph @

src/util/ddbussender.cpp

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "ddbussender.h"
6
7 #include <QDBusInterface>
8 #include <QDebug>
9
10 DDBusSender::DDBusSender()
11 : m_dbusData(std::make_shared<DDBusData>())
12 {
13 }
14
15 DDBusSender DDBusSender::service(const QString &service)
16 {
17 m_dbusData->service = service;
18
19 return *this;
20 }
21
22 DDBusSender DDBusSender::interface(const QString &interface)
23 {
24 m_dbusData->interface = interface;
25
26 return *this;
27 }
28
29 DDBusCaller DDBusSender::method(const QString &method)
30 {
31 return DDBusCaller(method, m_dbusData);
32 }
33
34 DDBusProperty DDBusSender::property(const QString &property)
35 {
36 return DDBusProperty(property, m_dbusData);
37 }
38
39 DDBusSender DDBusSender::system()
40 {
41 DDBusSender self;
42 self.type(QDBusConnection::SystemBus);
43 return self;
44 }
45
46 DDBusSender DDBusSender::path(const QString &path)
47 {
48 m_dbusData->path = path;
49
50 return *this;
51 }
52
53 DDBusSender DDBusSender::type(const QDBusConnection::BusType busType)
54 {
55 switch (busType)
56 {
57 case QDBusConnection::SessionBus:
58 m_dbusData->connection = QDBusConnection::sessionBus();
59 break;
60
61 case QDBusConnection::SystemBus:
62 m_dbusData->connection = QDBusConnection::systemBus();
63 break;
64
65 default:
66 Q_UNREACHABLE_IMPL();
67 }
68
69 return *this;
70 }
71
72 DDBusData::DDBusData()
73 : connection(QDBusConnection::sessionBus())
74 {
75
76 }
77
78 QDBusPendingCall DDBusData::asyncCallWithArguments(const QString &method, const QVariantList &arguments, const QString &iface)
79 {
80 // When creating a QDBusAbstractInterface, Qt will try to invoke introspection into this dbus path;
81 // This is costing in some cases when introspection is not ready;
82 // Cause this is an asynchronous method, it'd be better not to wait for anything, just leave this to caller;
83 // Use QDBusMessage to invoke directly instead of creating a QDBusInterface.
84 const QString calledInterface = iface.isEmpty() ? interface : iface;
85 QDBusMessage methodCall = QDBusMessage::createMethodCall(service, path, calledInterface, method);
86 methodCall.setArguments(arguments);
87 return connection.asyncCall(methodCall);
88 }
89
90 QDBusPendingCall DDBusCaller::call()
91 {
92 return m_dbusData->asyncCallWithArguments(m_methodName, m_arguments);
93 }
94
95 DDBusCaller::DDBusCaller(const QString &method, std::shared_ptr<DDBusData> data)
96 : m_dbusData(data)
97 , m_methodName(method)
98 {
99 }
100
101 QDBusPendingCall DDBusProperty::get()
102 {
103 QVariantList args{QVariant::fromValue(m_dbusData->interface), QVariant::fromValue(m_propertyName)};
104 return m_dbusData->asyncCallWithArguments(QStringLiteral("Get"), args, QStringLiteral("org.freedesktop.DBus.Properties"));
105 }
106
107 DDBusProperty::DDBusProperty(const QString &property, std::shared_ptr<DDBusData> data)
108 : m_dbusData(data)
109 , m_propertyName(property)
110 {
111 }