File indexing completed on 2024-12-08 12:15:37
0001 /* 0002 * BluezQt - Asynchronous Bluez wrapper library 0003 * 0004 * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com> 0005 * 0006 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "request.h" 0010 #include "debug.h" 0011 #include "utils.h" 0012 0013 #include <QDBusConnection> 0014 #include <QDBusMessage> 0015 0016 namespace BluezQt 0017 { 0018 class RequestPrivate 0019 { 0020 public: 0021 RequestOriginatingType m_type; 0022 QDBusMessage m_message; 0023 0024 bool sendMessage(const QDBusMessage &msg); 0025 QString interfaceName(); 0026 0027 void acceptRequest(const QVariant &val); 0028 void rejectRequest(); 0029 void cancelRequest(); 0030 }; 0031 0032 bool RequestPrivate::sendMessage(const QDBusMessage &msg) 0033 { 0034 switch (m_type) { 0035 case OrgBluezAgent: 0036 case OrgBluezProfile: 0037 return DBusConnection::orgBluez().send(msg); 0038 case OrgBluezObexAgent: 0039 return DBusConnection::orgBluezObex().send(msg); 0040 0041 default: 0042 return false; 0043 } 0044 } 0045 0046 QString RequestPrivate::interfaceName() 0047 { 0048 switch (m_type) { 0049 case OrgBluezAgent: 0050 return QStringLiteral("org.bluez.Agent1"); 0051 case OrgBluezProfile: 0052 return QStringLiteral("org.bluez.Profile1"); 0053 case OrgBluezObexAgent: 0054 return QStringLiteral("org.bluez.obex.Agent1"); 0055 default: 0056 return QString(); 0057 } 0058 } 0059 0060 void RequestPrivate::acceptRequest(const QVariant &val) 0061 { 0062 QDBusMessage reply; 0063 if (val.isValid()) { 0064 reply = m_message.createReply(val); 0065 } else { 0066 reply = m_message.createReply(); 0067 } 0068 0069 if (!sendMessage(reply)) { 0070 qCWarning(BLUEZQT) << "Request: Failed to put reply on DBus queue"; 0071 } 0072 } 0073 0074 void RequestPrivate::rejectRequest() 0075 { 0076 const QDBusMessage &reply = m_message.createErrorReply(interfaceName() % QStringLiteral(".Rejected"), QStringLiteral("Rejected")); 0077 if (!sendMessage(reply)) { 0078 qCWarning(BLUEZQT) << "Request: Failed to put reply on DBus queue"; 0079 } 0080 } 0081 0082 void RequestPrivate::cancelRequest() 0083 { 0084 const QDBusMessage &reply = m_message.createErrorReply(interfaceName() % QStringLiteral(".Canceled"), QStringLiteral("Canceled")); 0085 if (!sendMessage(reply)) { 0086 qCWarning(BLUEZQT) << "Request: Failed to put reply on DBus queue"; 0087 } 0088 } 0089 0090 // T 0091 template<typename T> 0092 Request<T>::Request() 0093 : d(new RequestPrivate) 0094 { 0095 } 0096 0097 template<typename T> 0098 Request<T>::Request(RequestOriginatingType type, const QDBusMessage &message) 0099 : d(new RequestPrivate) 0100 { 0101 d->m_type = type; 0102 d->m_message = message; 0103 } 0104 0105 template<typename T> 0106 Request<T>::~Request() 0107 { 0108 } 0109 0110 template<typename T> 0111 Request<T>::Request(const Request &other) 0112 : d(other.d) 0113 { 0114 } 0115 0116 template<typename T> 0117 Request<T> &Request<T>::operator=(const Request<T> &other) 0118 { 0119 if (d != other.d) { 0120 d = other.d; 0121 } 0122 return *this; 0123 } 0124 0125 template<typename T> 0126 void Request<T>::accept(T returnValue) const 0127 { 0128 d->acceptRequest(returnValue); 0129 } 0130 0131 template<typename T> 0132 void Request<T>::reject() const 0133 { 0134 d->rejectRequest(); 0135 } 0136 0137 template<typename T> 0138 void Request<T>::cancel() const 0139 { 0140 d->cancelRequest(); 0141 } 0142 0143 // void 0144 Request<void>::Request() 0145 : d(new RequestPrivate) 0146 { 0147 } 0148 0149 Request<void>::Request(RequestOriginatingType type, const QDBusMessage &message) 0150 : d(new RequestPrivate) 0151 { 0152 d->m_type = type; 0153 d->m_message = message; 0154 } 0155 0156 Request<void>::~Request() 0157 { 0158 } 0159 0160 Request<void>::Request(const Request &other) 0161 : d(other.d) 0162 { 0163 } 0164 0165 Request<void> &Request<void>::operator=(const Request<void> &other) 0166 { 0167 if (d != other.d) { 0168 d = other.d; 0169 } 0170 return *this; 0171 } 0172 0173 void Request<void>::accept() const 0174 { 0175 d->acceptRequest(QVariant()); 0176 } 0177 0178 void Request<void>::reject() const 0179 { 0180 d->rejectRequest(); 0181 } 0182 0183 void Request<void>::cancel() const 0184 { 0185 d->cancelRequest(); 0186 } 0187 0188 // Generate classes 0189 template class Request<void>; 0190 template class Request<quint32>; 0191 template class Request<QString>; 0192 template class Request<QByteArray>; 0193 0194 } // namespace BluezQt