File indexing completed on 2022-09-27 16:37:09

0001 /*
0002     SPDX-FileCopyrightText: 2010-2018 Daniel Nicoletti <dantti12@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "AddPrinterAssistant.h"
0008 
0009 #include "PageDestinations.h"
0010 #include "PageChoosePrinters.h"
0011 #include "PageChoosePPD.h"
0012 #include "PageAddPrinter.h"
0013 
0014 #include <KCupsRequest.h>
0015 
0016 #include <QHostInfo>
0017 #include <QPushButton>
0018 
0019 #include <KLocalizedString>
0020 #include <KPixmapSequence>
0021 #include <KIconLoader>
0022 #include <KSharedConfig>
0023 #include <KWindowConfig>
0024 
0025 AddPrinterAssistant::AddPrinterAssistant()
0026 {
0027     setWindowTitle(i18nc("@title:window", "Add a New Printer"));
0028     setWindowIcon(QIcon::fromTheme(QLatin1String("printer")));
0029     buttonBox()->removeButton(buttonBox()->button(QDialogButtonBox::Cancel));
0030     // Needed so we have our dialog size saved
0031     setAttribute(Qt::WA_DeleteOnClose);
0032 
0033     QPushButton *helpButton = buttonBox()->addButton(QDialogButtonBox::Help);
0034     // Configure the help button to be flat, disabled and empty
0035     helpButton->setFlat(true);
0036     helpButton->setEnabled(false);
0037     helpButton->setIcon(QIcon());
0038     helpButton->setText(QString());
0039 
0040     // Setup the busy cursor
0041     m_busySeq = new KPixmapSequenceOverlayPainter(this);
0042     m_busySeq->setSequence(KIconLoader::global()->loadPixmapSequence(QLatin1String("process-working"), KIconLoader::SizeSmallMedium));
0043     m_busySeq->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
0044     m_busySeq->setWidget(helpButton);
0045 
0046     connect(finishButton(), &QPushButton::clicked, this, &AddPrinterAssistant::slotFinishButtonClicked);
0047 
0048     // Restore the dialog size
0049     KConfigGroup configGroup(KSharedConfig::openConfig(QLatin1String("print-manager")), "AddPrinterAssistant");
0050     KWindowConfig::restoreWindowSize(windowHandle(), configGroup);
0051 }
0052 
0053 AddPrinterAssistant::~AddPrinterAssistant()
0054 {
0055     KConfigGroup configGroup(KSharedConfig::openConfig(QLatin1String("print-manager")), "AddPrinterAssistant");
0056     KWindowConfig::saveWindowSize(windowHandle(), configGroup);
0057 }
0058 
0059 void AddPrinterAssistant::initAddPrinter(const QString &printer, const QString &deviceId)
0060 {
0061     // setup our hash args with the information if we are
0062     // adding a new printer or a class
0063     QVariantHash args({
0064                           {ADDING_PRINTER, true}
0065                       });
0066 
0067     KPageWidgetItem *currentPage;
0068     if (deviceId.isEmpty()) {
0069         m_devicesPage = new KPageWidgetItem(new PageDestinations(args), i18nc("@title:window", "Select a Printer to Add"));
0070         addPage(m_devicesPage);
0071         currentPage = m_devicesPage;
0072 
0073         m_choosePPDPage = new KPageWidgetItem(new PageChoosePPD, i18nc("@title:window", "Pick a Driver"));
0074         addPage(m_choosePPDPage);
0075     } else {
0076         args[KCUPS_DEVICE_URI] = printer;
0077         args[KCUPS_DEVICE_ID] = deviceId;
0078         args[KCUPS_DEVICE_LOCATION] = QHostInfo::localHostName();
0079 
0080         m_choosePPDPage = new KPageWidgetItem(new PageChoosePPD(args), i18nc("@title:window", "Pick a Driver"));
0081         addPage(m_choosePPDPage);
0082         currentPage = m_choosePPDPage;
0083     }
0084 
0085     m_addPrinterPage = new KPageWidgetItem(new PageAddPrinter, i18nc("@title:window", "Please describe your printer"));
0086     addPage(m_addPrinterPage);
0087 
0088     // Set this later so that all m_*Pages are created
0089     setCurrentPage(currentPage);
0090 }
0091 
0092 void AddPrinterAssistant::initAddClass()
0093 {
0094     // setup our hash args with the information if we are
0095     // adding a new printer or a class
0096     const QVariantHash args({
0097                                 {ADDING_PRINTER, false},
0098                                 {KCUPS_DEVICE_LOCATION, QHostInfo::localHostName()}
0099                             });
0100 
0101     KPageWidgetItem *currentPage;
0102     m_chooseClassPage = new KPageWidgetItem(new PageChoosePrinters(args), i18nc("@title:window", "Configure your connection"));
0103     addPage(m_chooseClassPage);
0104     currentPage = m_chooseClassPage;
0105 
0106     m_addPrinterPage = new KPageWidgetItem(new PageAddPrinter, i18nc("@title:window", "Please describe your printer"));
0107     addPage(m_addPrinterPage);
0108 
0109     // Set this later so that all m_*Pages are created
0110     setCurrentPage(currentPage);
0111 }
0112 
0113 void AddPrinterAssistant::initChangePPD(const QString &printer, const QString &deviceUri, const QString &makeAndModel)
0114 {
0115     // setup our hash args with the information if we are
0116     // adding a new printer or a class
0117     const QVariantHash args({
0118                                 {ADDING_PRINTER, true},
0119                                 {KCUPS_DEVICE_URI, deviceUri},
0120                                 {KCUPS_PRINTER_NAME, printer},
0121                                 {KCUPS_PRINTER_MAKE_AND_MODEL, makeAndModel}
0122                             });
0123 
0124     m_choosePPDPage = new KPageWidgetItem(new PageChoosePPD(args), i18nc("@title:window", "Pick a Driver"));
0125     addPage(m_choosePPDPage);
0126     setCurrentPage(m_choosePPDPage);
0127 }
0128 
0129 void AddPrinterAssistant::back()
0130 {
0131     KAssistantDialog::back();
0132     auto currPage = qobject_cast<GenericPage*>(currentPage()->widget());
0133     enableNextButton(currPage->canProceed());
0134     if (!qobject_cast<GenericPage*>(currentPage()->widget())->isValid()) {
0135         back();
0136     }
0137 }
0138 
0139 void AddPrinterAssistant::next()
0140 {
0141     next(currentPage());
0142 }
0143 
0144 void AddPrinterAssistant::next(KPageWidgetItem *currentPage)
0145 {
0146     // Each page has all it's settings and previous pages
0147     // settings stored, so when going backwards
0148     // we don't set (or even unset values),
0149     // and we only call setValues on the next page if
0150     // the currentPage() has changes.
0151     const QVariantHash args = qobject_cast<GenericPage*>(currentPage->widget())->values();
0152     if (currentPage == m_devicesPage) {
0153         qobject_cast<GenericPage*>(m_choosePPDPage->widget())->setValues(args);
0154         setCurrentPage(m_choosePPDPage);
0155     } else if (currentPage == m_chooseClassPage ||currentPage == m_choosePPDPage) {
0156         qobject_cast<GenericPage*>(m_addPrinterPage->widget())->setValues(args);
0157         setCurrentPage(m_addPrinterPage);
0158     }
0159 }
0160 
0161 void AddPrinterAssistant::setCurrentPage(KPageWidgetItem *page)
0162 {
0163     // if after setting the values the page is still valid show
0164     // it up, if not call next with it so we can find the next page
0165     if (qobject_cast<GenericPage*>(page->widget())->isValid()) {
0166         KAssistantDialog::setCurrentPage(page);
0167         auto currPage = qobject_cast<GenericPage*>(currentPage()->widget());
0168         auto nextPage = qobject_cast<GenericPage*>(page->widget());
0169         // Disconnect the current page slots
0170         disconnect(currPage, &GenericPage::allowProceed, this, &AddPrinterAssistant::enableNextButton);
0171         disconnect(currPage, &GenericPage::allowProceed, this, &AddPrinterAssistant::enableFinishButton);
0172         disconnect(currPage, &GenericPage::proceed, this, static_cast<void(AddPrinterAssistant::*)()>(&AddPrinterAssistant::next));
0173         disconnect(currPage, &GenericPage::startWorking, m_busySeq, &KPixmapSequenceOverlayPainter::start);
0174         disconnect(currPage, &GenericPage::stopWorking, m_busySeq, &KPixmapSequenceOverlayPainter::stop);
0175 
0176         // Connect next page signals
0177         connect(nextPage, &GenericPage::startWorking, m_busySeq, &KPixmapSequenceOverlayPainter::start);
0178         connect(nextPage, &GenericPage::stopWorking, m_busySeq, &KPixmapSequenceOverlayPainter::stop);
0179         connect(nextPage, &GenericPage::proceed, this, static_cast<void(AddPrinterAssistant::*)()>(&AddPrinterAssistant::next));
0180 
0181         // check the working property
0182         if (nextPage->isWorking()) {
0183             m_busySeq->start();
0184         } else {
0185             m_busySeq->stop();
0186         }
0187 
0188         // When ChangePPD() is called addPrinterPage is zero
0189         if (page == m_addPrinterPage || m_addPrinterPage == nullptr) {
0190             connect(nextPage, &GenericPage::allowProceed, this, &AddPrinterAssistant::enableFinishButton);
0191             enableNextButton(false);
0192             enableFinishButton(nextPage->canProceed());
0193         } else {
0194             connect(nextPage, &GenericPage::allowProceed, this, &AddPrinterAssistant::enableNextButton);
0195             enableNextButton(nextPage->canProceed());
0196         }
0197     } else {
0198         // In case page is not valid try the next one
0199         next(page);
0200     }
0201 }
0202 
0203 void AddPrinterAssistant::showEvent(QShowEvent *event)
0204 {
0205     KAssistantDialog::showEvent(event);
0206     enableNextButton(false);
0207     enableFinishButton(false);
0208 }
0209 
0210 void AddPrinterAssistant::slotFinishButtonClicked()
0211 {
0212     auto page = qobject_cast<GenericPage*>(currentPage()->widget());
0213     enableFinishButton(false);
0214     if (page->finishClicked()) {
0215         //KAssistantDialog::slotButtonClicked(button); // FIXME next() really?
0216         next();
0217     } else {
0218         enableFinishButton(true);
0219     }
0220 }
0221 
0222 void AddPrinterAssistant::enableNextButton(bool enable)
0223 {
0224     nextButton()->setEnabled(enable);
0225 }
0226 
0227 void AddPrinterAssistant::enableFinishButton(bool enable)
0228 {
0229     finishButton()->setEnabled(enable);
0230 }
0231 
0232 #include "moc_AddPrinterAssistant.cpp"