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
16namespace BluezQt
17{
18class RequestPrivate
19{
20public:
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
32bool 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
46QString 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
60void 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
74void 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
82void 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
91template<typename T>
92Request<T>::Request()
93 : d(new RequestPrivate)
94{
95}
96
97template<typename T>
98Request<T>::Request(RequestOriginatingType type, const QDBusMessage &message)
99 : d(new RequestPrivate)
100{
101 d->m_type = type;
102 d->m_message = message;
103}
104
105template<typename T>
106Request<T>::~Request()
107{
108}
109
110template<typename T>
111Request<T>::Request(const Request &other)
112 : d(other.d)
113{
114}
115
116template<typename T>
117Request<T> &Request<T>::operator=(const Request<T> &other)
118{
119 if (d != other.d) {
120 d = other.d;
121 }
122 return *this;
123}
124
125template<typename T>
126void Request<T>::accept(T returnValue) const
127{
128 d->acceptRequest(val: returnValue);
129}
130
131template<typename T>
132void Request<T>::reject() const
133{
134 d->rejectRequest();
135}
136
137template<typename T>
138void Request<T>::cancel() const
139{
140 d->cancelRequest();
141}
142
143// void
144Request<void>::Request()
145 : d(new RequestPrivate)
146{
147}
148
149Request<void>::Request(RequestOriginatingType type, const QDBusMessage &message)
150 : d(new RequestPrivate)
151{
152 d->m_type = type;
153 d->m_message = message;
154}
155
156Request<void>::~Request()
157{
158}
159
160Request<void>::Request(const Request &other)
161 : d(other.d)
162{
163}
164
165Request<void> &Request<void>::operator=(const Request<void> &other)
166{
167 if (d != other.d) {
168 d = other.d;
169 }
170 return *this;
171}
172
173void Request<void>::accept() const
174{
175 d->acceptRequest(val: QVariant());
176}
177
178void Request<void>::reject() const
179{
180 d->rejectRequest();
181}
182
183void Request<void>::cancel() const
184{
185 d->cancelRequest();
186}
187
188// Generate classes
189template class Request<void>;
190template class Request<quint32>;
191template class Request<QString>;
192template class Request<QByteArray>;
193
194} // namespace BluezQt
195

source code of bluez-qt/src/request.cpp