File indexing completed on 2024-03-24 17:00:24

0001 /*
0002  *   SPDX-FileCopyrightText: 2010 Alejandro Fiestas Olivares <alex@eyeos.org>
0003  *   SPDX-FileCopyrightText: 2010 Eduardo Robles Elvira <edulix@gmail.com>
0004  *   SPDX-FileCopyrightText: 2010 UFO Coders <info@ufocoders.com>
0005  *   SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
0006  *
0007  *   SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 
0010 #include "bluezagent.h"
0011 #include "bluedevil_kded.h"
0012 #include "helpers/requestauthorization.h"
0013 #include "helpers/requestconfirmation.h"
0014 #include "helpers/requestpin.h"
0015 
0016 #include <QDBusObjectPath>
0017 #include <QStandardPaths>
0018 
0019 #include <BluezQt/Device>
0020 
0021 BluezAgent::BluezAgent(QObject *parent)
0022     : BluezQt::Agent(parent)
0023 {
0024 }
0025 
0026 QDBusObjectPath BluezAgent::objectPath() const
0027 {
0028     return QDBusObjectPath(QStringLiteral("/modules/bluedevil/Agent"));
0029 }
0030 
0031 static void processAuthorizationRequest(BluezQt::DevicePtr device, const BluezQt::Request<> &request, RequestAuthorization::Result result)
0032 {
0033     switch (result) {
0034     case RequestAuthorization::Accept:
0035         qCDebug(BLUEDEVIL_KDED_LOG) << "Accepting request";
0036         request.accept();
0037         break;
0038 
0039     case RequestAuthorization::AcceptAndTrust:
0040         qCDebug(BLUEDEVIL_KDED_LOG) << "Accepting request and trusting device";
0041         request.accept();
0042         device->setTrusted(true);
0043         break;
0044 
0045     default:
0046         qCDebug(BLUEDEVIL_KDED_LOG) << "Rejecting request";
0047         request.reject();
0048         break;
0049     }
0050 }
0051 
0052 void BluezAgent::authorizeService(BluezQt::DevicePtr device, const QString &uuid, const BluezQt::Request<> &request)
0053 {
0054     // TODO: Show user the Service UUID
0055     qCDebug(BLUEDEVIL_KDED_LOG) << "AGENT-AuthorizeService" << device->name() << "Service:" << uuid;
0056 
0057     RequestAuthorization *helper = new RequestAuthorization(device, this);
0058     connect(helper, &RequestAuthorization::done, this, [device, request](RequestAuthorization::Result result) {
0059         processAuthorizationRequest(device, request, result);
0060     });
0061 }
0062 
0063 void BluezAgent::requestPinCode(BluezQt::DevicePtr device, const BluezQt::Request<QString> &request)
0064 {
0065     qCDebug(BLUEDEVIL_KDED_LOG) << "AGENT-RequestPinCode " << device->name();
0066 
0067     RequestPin *helper = new RequestPin(device, false, this);
0068     connect(helper, &RequestPin::done, this, [request](const QString &result) {
0069         if (!result.isEmpty()) {
0070             qCDebug(BLUEDEVIL_KDED_LOG) << "Introducing PIN...";
0071             request.accept(result);
0072             return;
0073         }
0074 
0075         qCDebug(BLUEDEVIL_KDED_LOG) << "No PIN introduced";
0076         request.reject();
0077     });
0078 }
0079 
0080 void BluezAgent::requestPasskey(BluezQt::DevicePtr device, const BluezQt::Request<quint32> &request)
0081 {
0082     qCDebug(BLUEDEVIL_KDED_LOG) << "AGENT-RequestPasskey " << device->name();
0083 
0084     RequestPin *helper = new RequestPin(device, true, this);
0085     connect(helper, &RequestPin::done, this, [request](const QString &result) {
0086         bool ok;
0087         quint32 passkey = result.toInt(&ok);
0088         if (ok) {
0089             qCDebug(BLUEDEVIL_KDED_LOG) << "Introducing PassKey...";
0090             request.accept(passkey);
0091             return;
0092         }
0093 
0094         qCDebug(BLUEDEVIL_KDED_LOG) << "No PassKey introduced";
0095         request.reject();
0096     });
0097 }
0098 
0099 void BluezAgent::requestConfirmation(BluezQt::DevicePtr device, const QString &passkey, const BluezQt::Request<> &request)
0100 {
0101     qCDebug(BLUEDEVIL_KDED_LOG) << "AGENT-RequestConfirmation " << device->name() << passkey;
0102 
0103     RequestConfirmation *helper = new RequestConfirmation(device, passkey, this);
0104     connect(helper, &RequestConfirmation::done, this, [request](RequestConfirmation::Result result) {
0105         if (result == RequestConfirmation::Accept) {
0106             qCDebug(BLUEDEVIL_KDED_LOG) << "Accepting request";
0107             request.accept();
0108             return;
0109         }
0110 
0111         qCDebug(BLUEDEVIL_KDED_LOG) << "Rejecting request";
0112         request.reject();
0113     });
0114 }
0115 
0116 void BluezAgent::requestAuthorization(BluezQt::DevicePtr device, const BluezQt::Request<> &request)
0117 {
0118     qCDebug(BLUEDEVIL_KDED_LOG) << "AGENT-RequestAuthorization";
0119 
0120     RequestAuthorization *helper = new RequestAuthorization(device, this);
0121     connect(helper, &RequestAuthorization::done, this, [device, request](RequestAuthorization::Result result) {
0122         processAuthorizationRequest(device, request, result);
0123     });
0124 }
0125 
0126 void BluezAgent::release()
0127 {
0128     qCDebug(BLUEDEVIL_KDED_LOG) << "AGENT-Release";
0129 
0130     Q_EMIT agentReleased();
0131 }
0132 
0133 void BluezAgent::cancel()
0134 {
0135     qCDebug(BLUEDEVIL_KDED_LOG) << "AGENT-Cancel";
0136 
0137     Q_EMIT agentCanceled();
0138 }