File indexing completed on 2024-09-29 07:46:22

0001 /*
0002     SPDX-FileCopyrightText: 2010 UFO Coders <info@ufocoders.com>
0003 
0004     SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "bluewizard.h"
0008 #include "bluedevil_wizard.h"
0009 #include "pages/connect.h"
0010 #include "pages/discover.h"
0011 #include "pages/fail.h"
0012 #include "pages/pairing.h"
0013 #include "pages/success.h"
0014 #include "wizardagent.h"
0015 
0016 #include <QApplication>
0017 #include <QDBusConnection>
0018 #include <QProcess>
0019 #include <QPushButton>
0020 #include <QString>
0021 
0022 #include <KLocalizedString>
0023 #include <KStandardGuiItem>
0024 
0025 #include <BluezQt/InitManagerJob>
0026 
0027 BlueWizard::BlueWizard()
0028     : QWizard()
0029     , m_agent(new WizardAgent(this))
0030 {
0031     setOption(QWizard::IndependentPages, true);
0032 
0033     setPage(Discover, new DiscoverPage(this));
0034     setPage(Connect, new ConnectPage(this));
0035     setPage(Pairing, new PairingPage(this));
0036     setPage(Success, new SuccessPage(this));
0037     setPage(Fail, new FailPage(this));
0038 
0039     QPushButton *backButton = new QPushButton(this);
0040     KGuiItem::assign(backButton, KStandardGuiItem::back(KStandardGuiItem::UseRTL));
0041 
0042     QPushButton *forwardButton = new QPushButton(this);
0043     KGuiItem::assign(forwardButton, KStandardGuiItem::forward(KStandardGuiItem::UseRTL));
0044 
0045     QPushButton *applyButton = new QPushButton(this);
0046     KGuiItem::assign(applyButton, KStandardGuiItem::apply());
0047 
0048     QPushButton *cancelButton = new QPushButton(this);
0049     KGuiItem::assign(cancelButton, KStandardGuiItem::cancel());
0050 
0051     setButton(QWizard::BackButton, backButton);
0052     setButton(QWizard::NextButton, forwardButton);
0053     setButton(QWizard::FinishButton, applyButton);
0054     setButton(QWizard::CancelButton, cancelButton);
0055 
0056     // We do not want "Forward" as text
0057     setButtonText(QWizard::NextButton, i18nc("Action to go to the next page on the wizard", "Next"));
0058     setButtonText(QWizard::FinishButton, i18nc("Action to finish the wizard", "Finish"));
0059 
0060     // Initialize BluezQt
0061     m_manager = new BluezQt::Manager(this);
0062     BluezQt::InitManagerJob *initJob = m_manager->init();
0063     initJob->start();
0064     connect(initJob, &BluezQt::InitManagerJob::result, this, &BlueWizard::initJobResult);
0065 
0066     // When Finished page is opened, close wizard automatically
0067     const auto onCurrentIdChanged = [this](int id) {
0068         if (id == Success) {
0069             done(QDialog::Accepted);
0070         }
0071         // Sending notification in SuccessPage is asynchronous, so this needs to be queued.
0072     };
0073     connect(this, &QWizard::currentIdChanged, this, onCurrentIdChanged, Qt::QueuedConnection);
0074 }
0075 
0076 BluezQt::DevicePtr BlueWizard::device() const
0077 {
0078     return m_device;
0079 }
0080 
0081 void BlueWizard::setDevice(BluezQt::DevicePtr device)
0082 {
0083     m_device = device;
0084 }
0085 
0086 WizardAgent *BlueWizard::agent() const
0087 {
0088     return m_agent;
0089 }
0090 
0091 BluezQt::Manager *BlueWizard::manager() const
0092 {
0093     return m_manager;
0094 }
0095 
0096 void BlueWizard::initJobResult(BluezQt::InitManagerJob *job)
0097 {
0098     if (job->error()) {
0099         qCWarning(BLUEDEVIL_WIZARD_LOG) << "Error initializing manager:" << job->errorText();
0100         qApp->exit(1);
0101         return;
0102     }
0103 
0104     qCDebug(BLUEDEVIL_WIZARD_LOG) << "Manager initialized";
0105 
0106     // Make sure to register agent when bluetoothd starts
0107     operationalChanged(m_manager->isOperational());
0108     connect(m_manager, &BluezQt::Manager::operationalChanged, this, &BlueWizard::operationalChanged);
0109 
0110     // Only show wizard after init is completed
0111     show();
0112 }
0113 
0114 void BlueWizard::operationalChanged(bool operational)
0115 {
0116     if (operational) {
0117         m_manager->registerAgent(m_agent);
0118     } else {
0119         // Attempt to start bluetoothd
0120         BluezQt::Manager::startService();
0121     }
0122 }
0123 
0124 void BlueWizard::done(int result)
0125 {
0126     qCDebug(BLUEDEVIL_WIZARD_LOG) << "Wizard done:" << result;
0127 
0128     QWizard::done(result);
0129     qApp->exit(result);
0130 }
0131 
0132 #include "moc_bluewizard.cpp"