BluezQt

request.cpp
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(msg);
38 case OrgBluezObexAgent:
39 return DBusConnection::orgBluezObex().send(msg);
40
41 default:
42 return false;
43 }
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(val);
65 } else {
66 reply = m_message.createReply();
67 }
68
69 if (!sendMessage(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(interfaceName() % QStringLiteral(".Rejected"), QStringLiteral("Rejected"));
77 if (!sendMessage(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(interfaceName() % QStringLiteral(".Canceled"), QStringLiteral("Canceled"));
85 if (!sendMessage(reply)) {
86 qCWarning(BLUEZQT) << "Request: Failed to put reply on DBus queue";
87 }
88}
89
90// T
91template<typename T>
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>
109
110template<typename T>
112 : d(other.d)
113{
114}
115
116template<typename T>
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(returnValue);
129}
130
131template<typename T>
133{
134 d->rejectRequest();
135}
136
137template<typename T>
139{
140 d->cancelRequest();
141}
142
143// void
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
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(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
D-Bus request.
Definition request.h:39
void accept(T returnValue) const
Accepts the request.
Definition request.cpp:126
Request & operator=(const Request &other)
Copy assignment operator.
Definition request.cpp:117
void cancel() const
Cancels the request.
Definition request.cpp:138
void reject() const
Rejects the request.
Definition request.cpp:132
virtual ~Request()
Destroys a Request object.
Definition request.cpp:106
Request()
Creates a new Request object.
Definition request.cpp:92
QDBusMessage createErrorReply(const QString name, const QString &msg) const const
QDBusMessage createReply(const QList< QVariant > &arguments) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:39:12 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.