diff --git a/src/kded/bluezagent.cpp b/src/kded/bluezagent.cpp index f1b3081e..5f7c9783 100644 --- a/src/kded/bluezagent.cpp +++ b/src/kded/bluezagent.cpp @@ -1,144 +1,151 @@ /*************************************************************************** * Copyright (C) 2010 Alejandro Fiestas Olivares * * Copyright (C) 2010 Eduardo Robles Elvira * * Copyright (C) 2010 UFO Coders * * Copyright (C) 2014-2015 David Rosca * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ #include "bluezagent.h" #include "debug_p.h" #include "helpers/requestauthorization.h" #include "helpers/requestconfirmation.h" #include "helpers/requestpin.h" #include #include #include BluezAgent::BluezAgent(QObject *parent) : BluezQt::Agent(parent) { } QDBusObjectPath BluezAgent::objectPath() const { return QDBusObjectPath(QStringLiteral("/modules/bluedevil/Agent")); } static void processAuthorizationRequest(BluezQt::DevicePtr device, const BluezQt::Request<> &request, RequestAuthorization::Result result) { switch (result) { case RequestAuthorization::Accept: qCDebug(BLUEDAEMON) << "Accepting request"; request.accept(); break; case RequestAuthorization::AcceptAndTrust: qCDebug(BLUEDAEMON) << "Accepting request and trusting device"; request.accept(); device->setTrusted(true); break; default: qCDebug(BLUEDAEMON) << "Rejecting request"; request.reject(); break; } } void BluezAgent::authorizeService(BluezQt::DevicePtr device, const QString &uuid, const BluezQt::Request<> &request) { // TODO: Show user the Service UUID qCDebug(BLUEDAEMON) << "AGENT-AuthorizeService" << device->name() << "Service:" << uuid; RequestAuthorization *helper = new RequestAuthorization(device, this); connect(helper, &RequestAuthorization::done, this, [this, device, request](RequestAuthorization::Result result) { processAuthorizationRequest(device, request, result); }); } void BluezAgent::requestPinCode(BluezQt::DevicePtr device, const BluezQt::Request &request) { qCDebug(BLUEDAEMON) << "AGENT-RequestPinCode " << device->name(); RequestPin *helper = new RequestPin(device, false, this); connect(helper, &RequestPin::done, this, [this, request](const QString &result) { if (!result.isEmpty()) { qCDebug(BLUEDAEMON) << "Introducing PIN..."; request.accept(result); return; } qCDebug(BLUEDAEMON) << "No PIN introduced"; request.reject(); }); } void BluezAgent::requestPasskey(BluezQt::DevicePtr device, const BluezQt::Request &request) { qCDebug(BLUEDAEMON) << "AGENT-RequestPasskey " << device->name(); RequestPin *helper = new RequestPin(device, true, this); connect(helper, &RequestPin::done, this, [this, request](const QString &result) { bool ok; quint32 passkey = result.toInt(&ok); if (ok) { qCDebug(BLUEDAEMON) << "Introducing PassKey..."; request.accept(passkey); return; } qCDebug(BLUEDAEMON) << "No PassKey introduced"; request.reject(); }); } void BluezAgent::requestConfirmation(BluezQt::DevicePtr device, const QString &passkey, const BluezQt::Request<> &request) { qCDebug(BLUEDAEMON) << "AGENT-RequestConfirmation " << device->name() << passkey; RequestConfirmation *helper = new RequestConfirmation(device, passkey, this); connect(helper, &RequestConfirmation::done, this, [this, request](RequestConfirmation::Result result) { if (result == RequestConfirmation::Accept) { qCDebug(BLUEDAEMON) << "Accepting request"; request.accept(); return; } qCDebug(BLUEDAEMON) << "Rejecting request"; request.reject(); }); } void BluezAgent::requestAuthorization(BluezQt::DevicePtr device, const BluezQt::Request<> &request) { qCDebug(BLUEDAEMON) << "AGENT-RequestAuthorization"; RequestAuthorization *helper = new RequestAuthorization(device, this); connect(helper, &RequestAuthorization::done, this, [this, device, request](RequestAuthorization::Result result) { processAuthorizationRequest(device, request, result); }); } void BluezAgent::release() { qCDebug(BLUEDAEMON) << "AGENT-Release"; Q_EMIT agentReleased(); } + +void BluezAgent::cancel() +{ + qCDebug(BLUEDAEMON) << "AGENT-Cancel"; + + Q_EMIT agentCanceled(); +} diff --git a/src/kded/bluezagent.h b/src/kded/bluezagent.h index 0dd55d99..35225a37 100644 --- a/src/kded/bluezagent.h +++ b/src/kded/bluezagent.h @@ -1,49 +1,51 @@ /*************************************************************************** * Copyright (C) 2010 Alejandro Fiestas Olivares * * Copyright (C) 2010 Eduardo Robles Elvira * * Copyright (C) 2010 UFO Coders * * Copyright (C) 2014-2015 David Rosca * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ #ifndef BLUEZAGENT_H #define BLUEZAGENT_H #include class BluezAgent : public BluezQt::Agent { Q_OBJECT public: explicit BluezAgent(QObject *parent); QDBusObjectPath objectPath() const override; void authorizeService(BluezQt::DevicePtr device, const QString &uuid, const BluezQt::Request<> &request) override; void requestPinCode(BluezQt::DevicePtr device, const BluezQt::Request &request) override; void requestPasskey(BluezQt::DevicePtr device, const BluezQt::Request &request) override; void requestConfirmation(BluezQt::DevicePtr device, const QString &passkey, const BluezQt::Request<> &request) override; void requestAuthorization(BluezQt::DevicePtr device, const BluezQt::Request<> &request) override; void release() override; + void cancel() override; Q_SIGNALS: void agentReleased(); + void agentCanceled(); }; #endif // BLUEZAGENT_H diff --git a/src/kded/helpers/requestauthorization.cpp b/src/kded/helpers/requestauthorization.cpp index 7d0e9788..eaf84210 100644 --- a/src/kded/helpers/requestauthorization.cpp +++ b/src/kded/helpers/requestauthorization.cpp @@ -1,83 +1,84 @@ /*************************************************************************** * Copyright (C) 2010 Alejandro Fiestas Olivares * * Copyright (C) 2010 Eduardo Robles Elvira * * Copyright (C) 2010 UFO Coders * * Copyright (C) 2014-2015 David Rosca * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ #include "requestauthorization.h" #include "debug_p.h" #include #include #include RequestAuthorization::RequestAuthorization(BluezQt::DevicePtr device, QObject *parent) : QObject(parent) , m_device(device) { KNotification *notification = new KNotification(QStringLiteral("Authorize"), KNotification::Persistent, this); notification->setComponentName(QStringLiteral("bluedevil")); notification->setTitle(QStringLiteral("%1 (%2)").arg(m_device->name(), m_device->address())); notification->setText(i18nc("Show a notification asking to authorize or deny access to this computer from Bluetooth." "The %1 is the name of the bluetooth device", "%1 is requesting access to this computer", m_device->name())); QStringList actions; actions.append(i18nc("Button to trust a bluetooth remote device and authorize it to connect", "Trust && Authorize")); actions.append(i18nc("Button to authorize a bluetooth remote device to connect", "Authorize Only")); actions.append(i18nc("Deny access to a remote bluetooth device", "Deny")); notification->setActions(actions); connect(notification, &KNotification::action1Activated, this, &RequestAuthorization::authorizeAndTrust); connect(notification, &KNotification::action2Activated, this, &RequestAuthorization::authorize); connect(notification, &KNotification::action3Activated, this, &RequestAuthorization::deny); connect(notification, &KNotification::closed, this, &RequestAuthorization::deny); connect(notification, &KNotification::ignored, this, &RequestAuthorization::deny); + connect(parent, SIGNAL(agentCanceled()), this, SLOT(deny())); notification->sendEvent(); } void RequestAuthorization::authorizeAndTrust() { qCDebug(BLUEDAEMON) << "Authorization accepted and trusted:" << m_device->name() << m_device->address(); deleteLater(); Q_EMIT done(AcceptAndTrust); } void RequestAuthorization::authorize() { qCDebug(BLUEDAEMON) << "Authorization accepted:" << m_device->name() << m_device->address(); deleteLater(); Q_EMIT done(Accept); } void RequestAuthorization::deny() { qCDebug(BLUEDAEMON) << "Authorization denied:" << m_device->name() << m_device->address(); deleteLater(); Q_EMIT done(Deny); } diff --git a/src/kded/helpers/requestconfirmation.cpp b/src/kded/helpers/requestconfirmation.cpp index eb491f92..3c4ecbaf 100644 --- a/src/kded/helpers/requestconfirmation.cpp +++ b/src/kded/helpers/requestconfirmation.cpp @@ -1,71 +1,72 @@ /*************************************************************************** * Copyright (C) 2010 Alejandro Fiestas Olivares * * Copyright (C) 2010 Eduardo Robles Elvira * * Copyright (C) 2010 UFO Coders * * Copyright (C) 2014-2015 David Rosca * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ #include "requestconfirmation.h" #include "debug_p.h" #include #include RequestConfirmation::RequestConfirmation(BluezQt::DevicePtr device, const QString &pin, QObject *parent) : QObject(parent) , m_device(device) , m_pin(pin) { KNotification *notification = new KNotification(QStringLiteral("RequestConfirmation"), KNotification::Persistent, this); notification->setComponentName(QStringLiteral("bluedevil")); notification->setTitle(QStringLiteral("%1 (%2)").arg(m_device->name(), m_device->address())); notification->setText(i18nc("The text is shown in a notification to know if the PIN is correct," "%1 is the remote bluetooth device and %2 is the pin", "%1 is asking if the PIN is correct: %2", m_device->name(), m_pin)); QStringList actions; actions.append(i18nc("Notification button to know if the pin is correct or not", "PIN correct")); actions.append(i18nc("Notification button to say that the PIN is wrong", "PIN incorrect")); notification->setActions(actions); connect(notification, &KNotification::action1Activated, this, &RequestConfirmation::pinCorrect); connect(notification, &KNotification::action2Activated, this, &RequestConfirmation::pinWrong); connect(notification, &KNotification::closed, this, &RequestConfirmation::pinWrong); connect(notification, &KNotification::ignored, this, &RequestConfirmation::pinWrong); + connect(parent, SIGNAL(agentCanceled()), this, SLOT(pinWrong())); notification->sendEvent(); } void RequestConfirmation::pinCorrect() { qCDebug(BLUEDAEMON) << "PIN correct:" << m_device->name() << m_device->address(); deleteLater(); Q_EMIT done(Accept); } void RequestConfirmation::pinWrong() { qCDebug(BLUEDAEMON) << "PIN wrong:" << m_device->name() << m_device->address(); deleteLater(); Q_EMIT done(Deny); } diff --git a/src/kded/helpers/requestpin.cpp b/src/kded/helpers/requestpin.cpp index d115714d..61ce46ba 100644 --- a/src/kded/helpers/requestpin.cpp +++ b/src/kded/helpers/requestpin.cpp @@ -1,133 +1,134 @@ /*************************************************************************** * Copyright (C) 2010 Alejandro Fiestas Olivares * * Copyright (C) 2010 Eduardo Robles Elvira * * Copyright (C) 2010 UFO Coders * * Copyright (C) 2014-2015 David Rosca * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ #include "requestpin.h" #include "ui_requestpin.h" #include "debug_p.h" #include #include #include #include #include #include #include RequestPin::RequestPin(BluezQt::DevicePtr device, bool numeric, QObject *parent) : QObject(parent) , m_dialogWidget(nullptr) , m_device(device) , m_numeric(numeric) { m_notification = new KNotification(QStringLiteral("RequestPin"), KNotification::Persistent, this); m_notification->setComponentName(QStringLiteral("bluedevil")); m_notification->setTitle(QStringLiteral("%1 (%2)").arg(m_device->name(), m_device->address())); m_notification->setText(i18nc("Shown in a notification to announce that a PIN is needed to accomplish a pair action," "%1 is the name of the bluetooth device", "PIN needed to pair with %1", m_device->name())); QStringList actions; actions.append(i18nc("Notification button which once clicked, a dialog to introduce the PIN will be shown", "Introduce PIN")); m_notification->setActions(actions); connect(m_notification, &KNotification::action1Activated,this, &RequestPin::introducePin); connect(m_notification, &KNotification::closed, this, &RequestPin::quit); connect(m_notification, &KNotification::ignored, this, &RequestPin::quit); + connect(parent, SIGNAL(agentCanceled()), this, SLOT(quit())); m_notification->sendEvent(); } void RequestPin::introducePin() { m_notification->disconnect(); m_notification->close(); m_notification->deleteLater(); QDialog *dialog = new QDialog; dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->setWindowIcon(QIcon::fromTheme(QStringLiteral("preferences-system-bluetooth"))); dialog->setWindowTitle(i18nc("Shown in the caption of a dialog where the user introduce the PIN", "Introduce PIN")); m_dialogWidget = new Ui::DialogWidget; m_dialogWidget->setupUi(dialog); m_dialogWidget->descLabel->setText(i18nc("Shown in a dialog which asks to introduce a PIN that will be used to pair a Bluetooth device," "%1 is the name of the Bluetooth device", "In order to pair this computer with %1, you have to enter a PIN. Please do it below.", m_device->name())); m_dialogWidget->pixmap->setPixmap(QIcon::fromTheme(QStringLiteral("preferences-system-bluetooth")).pixmap(64)); m_dialogWidget->pin->setFocus(Qt::ActiveWindowFocusReason); if (m_numeric) { QRegularExpression rx(QStringLiteral("[0-9]{1,6}")); m_dialogWidget->pin->setValidator(new QRegularExpressionValidator(rx, this)); } else { QRegularExpression rx(QStringLiteral("[A-Za-z0-9]{1,16}")); m_dialogWidget->pin->setValidator(new QRegularExpressionValidator(rx, this)); } m_dialogWidget->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); dialog->setFixedSize(dialog->sizeHint()); connect(dialog, &QDialog::finished, this, &RequestPin::dialogFinished); connect(m_dialogWidget->pin, &QLineEdit::textChanged, this, &RequestPin::checkPin); connect(m_dialogWidget->buttonBox, &QDialogButtonBox::accepted, dialog, &QDialog::accept); connect(m_dialogWidget->buttonBox, &QDialogButtonBox::rejected, dialog, &QDialog::reject); dialog->show(); KWindowSystem::setState(dialog->winId(), NET::KeepAbove); KWindowSystem::forceActiveWindow(dialog->winId()); } void RequestPin::checkPin(const QString &pin) { m_dialogWidget->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!pin.isEmpty()); } void RequestPin::dialogFinished(int result) { deleteLater(); if (!result) { qCDebug(BLUEDAEMON) << "PIN dialog rejected:" << m_device->name() << m_device->address(); Q_EMIT done(QString()); return; } qCDebug(BLUEDAEMON) << "PIN dialog accepted:" << m_device->name() << m_device->address(); Q_EMIT done(m_dialogWidget->pin->text().toLatin1().constData()); } void RequestPin::quit() { qCDebug(BLUEDAEMON) << "Rejected to introduce PIN:" << m_device->name() << m_device->address(); deleteLater(); Q_EMIT done(QString()); }