File indexing completed on 2024-11-03 04:57:05

0001 /*
0002  * This file is part of the KDE project
0003  *
0004  * SPDX-FileCopyrightText: 2015 David Rosca <nowrep@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 #include "pairing.h"
0010 #include "../bluewizard.h"
0011 #include "../wizardagent.h"
0012 #include "bluedevil_wizard.h"
0013 
0014 #include <QPushButton>
0015 
0016 #include <KLocalizedString>
0017 #include <KStandardGuiItem>
0018 
0019 #include <BluezQt/Adapter>
0020 #include <BluezQt/PendingCall>
0021 
0022 PairingPage::PairingPage(BlueWizard *parent)
0023     : QWizardPage(parent)
0024     , m_wizard(parent)
0025     , m_success(false)
0026 {
0027     setupUi(this);
0028 
0029     QFont font(pinNumber->font());
0030     font.setPointSize(42);
0031     font.setBold(true);
0032     pinNumber->setFont(font);
0033 }
0034 
0035 int PairingPage::nextId() const
0036 {
0037     if (m_success) {
0038         return BlueWizard::Connect;
0039     }
0040     return BlueWizard::Fail;
0041 }
0042 
0043 void PairingPage::initializePage()
0044 {
0045     qCDebug(BLUEDEVIL_WIZARD_LOG) << "Initialize Pairing Page";
0046 
0047     m_device = m_wizard->device();
0048     m_wizard->setButtonLayout(wizardButtonsLayout());
0049 
0050     QPushButton *cancel = new QPushButton(this);
0051     KGuiItem::assign(cancel, KStandardGuiItem::cancel());
0052     connect(cancel, &QPushButton::clicked, this, &PairingPage::cancelClicked);
0053     wizard()->setButton(QWizard::CustomButton1, cancel);
0054 
0055     frame->hide();
0056     progressBar->show();
0057     connectingLbl->show();
0058     confirmLbl->clear();
0059 
0060     connectingLbl->setText(i18n("Connecting to %1…", m_device->name()));
0061 
0062     connect(m_wizard->agent(), &WizardAgent::pinRequested, this, &PairingPage::pinRequested);
0063     connect(m_wizard->agent(), &WizardAgent::confirmationRequested, this, &PairingPage::confirmationRequested);
0064 
0065     BluezQt::PendingCall *pairCall = m_device->pair();
0066     connect(pairCall, &BluezQt::PendingCall::finished, this, &PairingPage::pairingFinished);
0067 }
0068 
0069 void PairingPage::pairingFinished(BluezQt::PendingCall *call)
0070 {
0071     qCDebug(BLUEDEVIL_WIZARD_LOG) << "Pairing finished:";
0072     qCDebug(BLUEDEVIL_WIZARD_LOG) << "\t error     : " << (bool)call->error();
0073     qCDebug(BLUEDEVIL_WIZARD_LOG) << "\t errorText : " << call->errorText();
0074 
0075     m_success = !call->error();
0076     m_wizard->next();
0077 }
0078 
0079 void PairingPage::pinRequested(const QString &pin)
0080 {
0081     // Don't ask user to introduce the PIN if it was used from database
0082     if (m_wizard->agent()->isFromDatabase()) {
0083         return;
0084     }
0085 
0086     frame->show();
0087     connectingLbl->hide();
0088     progressBar->hide();
0089 
0090     if (m_device->type() == BluezQt::Device::Keyboard) {
0091         confirmLbl->setText(i18n("Please introduce the PIN in your keyboard when it appears and press Enter"));
0092     } else {
0093         confirmLbl->setText(i18n("Please introduce the PIN in your device when it appears"));
0094     }
0095 
0096     pinNumber->setText(pin);
0097 }
0098 
0099 void PairingPage::confirmationRequested(const QString &passkey, const BluezQt::Request<> &req)
0100 {
0101     m_req = req;
0102 
0103     frame->show();
0104     connectingLbl->hide();
0105     progressBar->hide();
0106 
0107     QPushButton *matches = new QPushButton(this);
0108     KGuiItem::assign(matches, KStandardGuiItem::apply());
0109     matches->setText(i18n("Matches"));
0110 
0111     QPushButton *notMatch = new QPushButton(this);
0112     KGuiItem::assign(notMatch, KStandardGuiItem::cancel());
0113     notMatch->setText(i18n("Does not match"));
0114 
0115     connect(matches, &QPushButton::clicked, this, &PairingPage::matchesClicked);
0116     connect(notMatch, &QPushButton::clicked, this, &PairingPage::notMatchClicked);
0117 
0118     m_wizard->setButton(QWizard::CustomButton1, matches);
0119     m_wizard->setButton(QWizard::CustomButton2, notMatch);
0120 
0121     QList<QWizard::WizardButton> list;
0122     list << QWizard::Stretch;
0123     list << QWizard::CustomButton1;
0124     list << QWizard::CustomButton2;
0125     m_wizard->setButtonLayout(list);
0126 
0127     pinNumber->setText(passkey);
0128     confirmLbl->setText(i18n("Please, confirm that the PIN displayed on %1 matches the wizard one.", m_wizard->device()->name()));
0129 }
0130 
0131 void PairingPage::matchesClicked()
0132 {
0133     wizard()->button(QWizard::CustomButton1)->setEnabled(false);
0134     wizard()->button(QWizard::CustomButton2)->setEnabled(false);
0135 
0136     m_req.accept();
0137 }
0138 
0139 void PairingPage::notMatchClicked()
0140 {
0141     m_req.reject();
0142 }
0143 
0144 void PairingPage::cancelClicked()
0145 {
0146     m_device->cancelPairing();
0147 }
0148 
0149 QList<QWizard::WizardButton> PairingPage::wizardButtonsLayout() const
0150 {
0151     QList<QWizard::WizardButton> list;
0152     list << QWizard::Stretch;
0153     list << QWizard::CustomButton1;
0154     return list;
0155 }
0156 
0157 #include "moc_pairing.cpp"