File indexing completed on 2024-04-28 05:26:12

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 "requestauthorization.h"
0011 #include "bluedevil_kded.h"
0012 
0013 #include <QIcon>
0014 
0015 #include <KLocalizedString>
0016 #include <KNotification>
0017 
0018 RequestAuthorization::RequestAuthorization(BluezQt::DevicePtr device, QObject *parent)
0019     : QObject(parent)
0020     , m_device(device)
0021 {
0022     KNotification *notification = new KNotification(QStringLiteral("Authorize"), KNotification::Persistent, this);
0023 
0024     notification->setComponentName(QStringLiteral("bluedevil"));
0025     notification->setTitle(QStringLiteral("%1 (%2)").arg(m_device->name().toHtmlEscaped(), m_device->address().toHtmlEscaped()));
0026     notification->setText(
0027         i18nc("Show a notification asking to authorize or deny access to this computer from Bluetooth."
0028               "The %1 is the name of the bluetooth device",
0029               "%1 is requesting access to this computer",
0030               m_device->name().toHtmlEscaped()));
0031 
0032     auto trustAndAuthorizeAction =
0033         notification->addAction(i18nc("Button to trust a bluetooth remote device and authorize it to connect", "Trust and Authorize"));
0034     auto authorizeAction = notification->addAction(i18nc("Button to authorize a bluetooth remote device to connect", "Authorize Only"));
0035     auto denyAction = notification->addAction(i18nc("Deny access to a remote bluetooth device", "Deny"));
0036 
0037     connect(trustAndAuthorizeAction, &KNotificationAction::activated, this, &RequestAuthorization::authorizeAndTrust);
0038     connect(authorizeAction, &KNotificationAction::activated, this, &RequestAuthorization::authorize);
0039     connect(denyAction, &KNotificationAction::activated, this, &RequestAuthorization::deny);
0040     connect(notification, &KNotification::closed, this, &RequestAuthorization::deny);
0041     connect(notification, &KNotification::ignored, this, &RequestAuthorization::deny);
0042     connect(parent, SIGNAL(agentCanceled()), this, SLOT(deny()));
0043 
0044     notification->sendEvent();
0045 }
0046 
0047 void RequestAuthorization::authorizeAndTrust()
0048 {
0049     qCDebug(BLUEDEVIL_KDED_LOG) << "Authorization accepted and trusted:" << m_device->name() << m_device->address();
0050 
0051     deleteLater();
0052     Q_EMIT done(AcceptAndTrust);
0053 }
0054 
0055 void RequestAuthorization::authorize()
0056 {
0057     qCDebug(BLUEDEVIL_KDED_LOG) << "Authorization accepted:" << m_device->name() << m_device->address();
0058 
0059     deleteLater();
0060     Q_EMIT done(Accept);
0061 }
0062 
0063 void RequestAuthorization::deny()
0064 {
0065     qCDebug(BLUEDEVIL_KDED_LOG) << "Authorization denied:" << m_device->name() << m_device->address();
0066 
0067     deleteLater();
0068     Q_EMIT done(Deny);
0069 }
0070 
0071 #include "moc_requestauthorization.cpp"