File indexing completed on 2024-10-06 06:38:17

0001 /*
0002  * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "agentmanager.h"
0008 
0009 #include <QDBusConnection>
0010 #include <QDBusMessage>
0011 #include <QDBusPendingCall>
0012 #include <QDebug>
0013 
0014 AgentManager::AgentManager(QObject *parent)
0015     : QDBusAbstractAdaptor(parent)
0016 {
0017     setName(QStringLiteral("org.bluez.AgentManager1"));
0018     setPath(QDBusObjectPath(QStringLiteral("/org/bluez")));
0019 }
0020 
0021 void AgentManager::runAction(const QString &actionName, const QVariantMap &properties)
0022 {
0023     if (actionName == QLatin1String("request-pincode")) {
0024         runRequestPinCodeAction(properties);
0025     } else if (actionName == QLatin1String("display-pincode")) {
0026         runDisplayPinCodeAction(properties);
0027     } else if (actionName == QLatin1String("request-passkey")) {
0028         runRequestPasskeyAction(properties);
0029     } else if (actionName == QLatin1String("display-passkey")) {
0030         runDisplayPasskeyAction(properties);
0031     } else if (actionName == QLatin1String("request-confirmation")) {
0032         runRequestConfirmationAction(properties);
0033     } else if (actionName == QLatin1String("request-authorization")) {
0034         runRequestAuthorizationAction(properties);
0035     } else if (actionName == QLatin1String("authorize-service")) {
0036         runAuthorizeServiceAction(properties);
0037     } else if (actionName == QLatin1String("cancel")) {
0038         runCancelAction();
0039     } else if (actionName == QLatin1String("release")) {
0040         runReleaseAction();
0041     }
0042 }
0043 
0044 void AgentManager::RegisterAgent(const QDBusObjectPath &path, const QString &capability, const QDBusMessage &msg)
0045 {
0046     m_agent = path;
0047     m_service = msg.service();
0048     m_capability = capability;
0049 }
0050 
0051 void AgentManager::UnregisterAgent(const QDBusObjectPath &path, const QDBusMessage &msg)
0052 {
0053     if (m_agent == path && m_service == msg.service()) {
0054         m_agent = QDBusObjectPath();
0055         m_service.clear();
0056         m_capability.clear();
0057     }
0058 }
0059 
0060 void AgentManager::RequestDefaultAgent(const QDBusObjectPath &path)
0061 {
0062     qDebug() << "RequestDefaultAgent" << path.path();
0063 }
0064 
0065 void AgentManager::runRequestPinCodeAction(const QVariantMap &properties)
0066 {
0067     QDBusMessage call = QDBusMessage::createMethodCall(m_service, m_agent.path(), QStringLiteral("org.bluez.Agent1"), QStringLiteral("RequestPinCode"));
0068 
0069     call << QVariant::fromValue(properties.value(QStringLiteral("Device")).value<QDBusObjectPath>());
0070     QDBusConnection::sessionBus().asyncCall(call);
0071 }
0072 
0073 void AgentManager::runDisplayPinCodeAction(const QVariantMap &properties)
0074 {
0075     QDBusMessage call = QDBusMessage::createMethodCall(m_service, m_agent.path(), QStringLiteral("org.bluez.Agent1"), QStringLiteral("DisplayPinCode"));
0076 
0077     call << QVariant::fromValue(properties.value(QStringLiteral("Device")).value<QDBusObjectPath>());
0078     call << properties.value(QStringLiteral("PinCode"));
0079     QDBusConnection::sessionBus().asyncCall(call);
0080 }
0081 
0082 void AgentManager::runRequestPasskeyAction(const QVariantMap &properties)
0083 {
0084     QDBusMessage call = QDBusMessage::createMethodCall(m_service, m_agent.path(), QStringLiteral("org.bluez.Agent1"), QStringLiteral("RequestPasskey"));
0085 
0086     call << QVariant::fromValue(properties.value(QStringLiteral("Device")).value<QDBusObjectPath>());
0087     QDBusConnection::sessionBus().asyncCall(call);
0088 }
0089 
0090 void AgentManager::runDisplayPasskeyAction(const QVariantMap &properties)
0091 {
0092     QDBusMessage call = QDBusMessage::createMethodCall(m_service, m_agent.path(), QStringLiteral("org.bluez.Agent1"), QStringLiteral("DisplayPasskey"));
0093 
0094     call << QVariant::fromValue(properties.value(QStringLiteral("Device")).value<QDBusObjectPath>());
0095     call << properties.value(QStringLiteral("Passkey"));
0096     call << properties.value(QStringLiteral("EnteredPasskey"));
0097     QDBusConnection::sessionBus().asyncCall(call);
0098 }
0099 
0100 void AgentManager::runRequestConfirmationAction(const QVariantMap &properties)
0101 {
0102     QDBusMessage call = QDBusMessage::createMethodCall(m_service, m_agent.path(), QStringLiteral("org.bluez.Agent1"), QStringLiteral("RequestConfirmation"));
0103 
0104     call << QVariant::fromValue(properties.value(QStringLiteral("Device")).value<QDBusObjectPath>());
0105     call << properties.value(QStringLiteral("Passkey"));
0106     QDBusConnection::sessionBus().asyncCall(call);
0107 }
0108 
0109 void AgentManager::runRequestAuthorizationAction(const QVariantMap &properties)
0110 {
0111     QDBusMessage call = QDBusMessage::createMethodCall(m_service, m_agent.path(), QStringLiteral("org.bluez.Agent1"), QStringLiteral("RequestAuthorization"));
0112 
0113     call << QVariant::fromValue(properties.value(QStringLiteral("Device")).value<QDBusObjectPath>());
0114     QDBusConnection::sessionBus().asyncCall(call);
0115 }
0116 
0117 void AgentManager::runAuthorizeServiceAction(const QVariantMap &properties)
0118 {
0119     QDBusMessage call = QDBusMessage::createMethodCall(m_service, m_agent.path(), QStringLiteral("org.bluez.Agent1"), QStringLiteral("AuthorizeService"));
0120 
0121     call << QVariant::fromValue(properties.value(QStringLiteral("Device")).value<QDBusObjectPath>());
0122     call << properties.value(QStringLiteral("UUID"));
0123     QDBusConnection::sessionBus().asyncCall(call);
0124 }
0125 
0126 void AgentManager::runCancelAction()
0127 {
0128     QDBusMessage call = QDBusMessage::createMethodCall(m_service, m_agent.path(), QStringLiteral("org.bluez.Agent1"), QStringLiteral("Cancel"));
0129     QDBusConnection::sessionBus().asyncCall(call);
0130 }
0131 
0132 void AgentManager::runReleaseAction()
0133 {
0134     QDBusMessage call = QDBusMessage::createMethodCall(m_service, m_agent.path(), QStringLiteral("org.bluez.Agent1"), QStringLiteral("Release"));
0135     QDBusConnection::sessionBus().asyncCall(call);
0136 }
0137 
0138 #include "moc_agentmanager.cpp"