1 | /* |
---|---|
2 | * BluezQt - Asynchronous Bluez wrapper library |
3 | * |
4 | * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com> |
5 | * |
6 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
7 | */ |
8 | |
9 | #include "request.h" |
10 | #include "debug.h" |
11 | #include "utils.h" |
12 | |
13 | #include <QDBusConnection> |
14 | #include <QDBusMessage> |
15 | |
16 | namespace BluezQt |
17 | { |
18 | class RequestPrivate |
19 | { |
20 | public: |
21 | RequestOriginatingType m_type; |
22 | QDBusMessage m_message; |
23 | |
24 | bool sendMessage(const QDBusMessage &msg); |
25 | QString interfaceName(); |
26 | |
27 | void acceptRequest(const QVariant &val); |
28 | void rejectRequest(); |
29 | void cancelRequest(); |
30 | }; |
31 | |
32 | bool RequestPrivate::sendMessage(const QDBusMessage &msg) |
33 | { |
34 | switch (m_type) { |
35 | case OrgBluezAgent: |
36 | case OrgBluezProfile: |
37 | return DBusConnection::orgBluez().send(message: msg); |
38 | case OrgBluezObexAgent: |
39 | return DBusConnection::orgBluezObex().send(message: msg); |
40 | |
41 | default: |
42 | return false; |
43 | } |
44 | } |
45 | |
46 | QString RequestPrivate::interfaceName() |
47 | { |
48 | switch (m_type) { |
49 | case OrgBluezAgent: |
50 | return QStringLiteral("org.bluez.Agent1"); |
51 | case OrgBluezProfile: |
52 | return QStringLiteral("org.bluez.Profile1"); |
53 | case OrgBluezObexAgent: |
54 | return QStringLiteral("org.bluez.obex.Agent1"); |
55 | default: |
56 | return QString(); |
57 | } |
58 | } |
59 | |
60 | void RequestPrivate::acceptRequest(const QVariant &val) |
61 | { |
62 | QDBusMessage reply; |
63 | if (val.isValid()) { |
64 | reply = m_message.createReply(argument: val); |
65 | } else { |
66 | reply = m_message.createReply(); |
67 | } |
68 | |
69 | if (!sendMessage(msg: reply)) { |
70 | qCWarning(BLUEZQT) << "Request: Failed to put reply on DBus queue"; |
71 | } |
72 | } |
73 | |
74 | void RequestPrivate::rejectRequest() |
75 | { |
76 | const QDBusMessage &reply = m_message.createErrorReply(name: interfaceName() % QStringLiteral(".Rejected"), QStringLiteral( "Rejected")); |
77 | if (!sendMessage(msg: reply)) { |
78 | qCWarning(BLUEZQT) << "Request: Failed to put reply on DBus queue"; |
79 | } |
80 | } |
81 | |
82 | void RequestPrivate::cancelRequest() |
83 | { |
84 | const QDBusMessage &reply = m_message.createErrorReply(name: interfaceName() % QStringLiteral(".Canceled"), QStringLiteral( "Canceled")); |
85 | if (!sendMessage(msg: reply)) { |
86 | qCWarning(BLUEZQT) << "Request: Failed to put reply on DBus queue"; |
87 | } |
88 | } |
89 | |
90 | // T |
91 | template<typename T> |
92 | Request<T>::Request() |
93 | : d(new RequestPrivate) |
94 | { |
95 | } |
96 | |
97 | template<typename T> |
98 | Request<T>::Request(RequestOriginatingType type, const QDBusMessage &message) |
99 | : d(new RequestPrivate) |
100 | { |
101 | d->m_type = type; |
102 | d->m_message = message; |
103 | } |
104 | |
105 | template<typename T> |
106 | Request<T>::~Request() |
107 | { |
108 | } |
109 | |
110 | template<typename T> |
111 | Request<T>::Request(const Request &other) |
112 | : d(other.d) |
113 | { |
114 | } |
115 | |
116 | template<typename T> |
117 | Request<T> &Request<T>::operator=(const Request<T> &other) |
118 | { |
119 | if (d != other.d) { |
120 | d = other.d; |
121 | } |
122 | return *this; |
123 | } |
124 | |
125 | template<typename T> |
126 | void Request<T>::accept(T returnValue) const |
127 | { |
128 | d->acceptRequest(val: returnValue); |
129 | } |
130 | |
131 | template<typename T> |
132 | void Request<T>::reject() const |
133 | { |
134 | d->rejectRequest(); |
135 | } |
136 | |
137 | template<typename T> |
138 | void Request<T>::cancel() const |
139 | { |
140 | d->cancelRequest(); |
141 | } |
142 | |
143 | // void |
144 | Request<void>::Request() |
145 | : d(new RequestPrivate) |
146 | { |
147 | } |
148 | |
149 | Request<void>::Request(RequestOriginatingType type, const QDBusMessage &message) |
150 | : d(new RequestPrivate) |
151 | { |
152 | d->m_type = type; |
153 | d->m_message = message; |
154 | } |
155 | |
156 | Request<void>::~Request() |
157 | { |
158 | } |
159 | |
160 | Request<void>::Request(const Request &other) |
161 | : d(other.d) |
162 | { |
163 | } |
164 | |
165 | Request<void> &Request<void>::operator=(const Request<void> &other) |
166 | { |
167 | if (d != other.d) { |
168 | d = other.d; |
169 | } |
170 | return *this; |
171 | } |
172 | |
173 | void Request<void>::accept() const |
174 | { |
175 | d->acceptRequest(val: QVariant()); |
176 | } |
177 | |
178 | void Request<void>::reject() const |
179 | { |
180 | d->rejectRequest(); |
181 | } |
182 | |
183 | void Request<void>::cancel() const |
184 | { |
185 | d->cancelRequest(); |
186 | } |
187 | |
188 | // Generate classes |
189 | template class Request<void>; |
190 | template class Request<quint32>; |
191 | template class Request<QString>; |
192 | template class Request<QByteArray>; |
193 | |
194 | } // namespace BluezQt |
195 |