File indexing completed on 2025-03-09 05:06:32

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 "ConfigureDialog.h"
0008 #include "PrinterPage.h"
0009 
0010 #include "PrinterBehavior.h"
0011 #include "PrinterOptions.h"
0012 
0013 #include "Debug.h"
0014 #include "KCupsRequest.h"
0015 
0016 #include <KConfigGroup>
0017 #include <KLocalizedString>
0018 #include <KMessageBox>
0019 #include <KSharedConfig>
0020 #include <KWindowConfig>
0021 #include <kwidgetsaddons_version.h>
0022 
0023 #include <QCloseEvent>
0024 #include <QList>
0025 #include <QPointer>
0026 #include <QPushButton>
0027 
0028 Q_DECLARE_METATYPE(QList<int>)
0029 
0030 ConfigureDialog::ConfigureDialog(const QString &destName, bool isClass, QWidget *parent)
0031     : KPageDialog(parent)
0032 {
0033     setFaceType(List);
0034     setModal(false);
0035     setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply);
0036     setWindowTitle(destName);
0037     setWindowIcon(QIcon::fromTheme(QLatin1String("configure")));
0038     enableButtonApply(false);
0039     // Needed so we have our dialog size saved
0040     setAttribute(Qt::WA_DeleteOnClose);
0041 
0042     QStringList attr;
0043     KPageWidgetItem *page;
0044 
0045     auto printerBehavior = new PrinterBehavior(destName, isClass, this);
0046     attr << printerBehavior->neededValues();
0047     attr << KCUPS_PRINTER_TYPE; // needed to know if it's a remote printer
0048     attr << KCUPS_PRINTER_MAKE_AND_MODEL;
0049     attr << KCUPS_PRINTER_INFO;
0050     attr << KCUPS_PRINTER_LOCATION;
0051     attr << KCUPS_DEVICE_URI;
0052 
0053     KCupsPrinter printer;
0054     QPointer<KCupsRequest> request = new KCupsRequest;
0055     request->getPrinterAttributes(destName, isClass, attr);
0056     request->waitTillFinished();
0057     if (!request) {
0058         return;
0059     }
0060     if (!request->hasError() && !request->printers().isEmpty()) {
0061         printer = request->printers().first();
0062         setWindowTitle(printer.info());
0063     }
0064     //    qCDebug(PM_CONFIGURE_PRINTER) << "VALUES" << printer.argument();
0065     //    qCDebug(PM_CONFIGURE_PRINTER) << "marker" << values["marker-levels"].value<QList<int> >();
0066 
0067     request->deleteLater();
0068 
0069     //     qCDebug(PM_CONFIGURE_PRINTER) << values;
0070     if (printer.type() & CUPS_PRINTER_LOCAL) {
0071         qCDebug(PM_CONFIGURE_PRINTER) << "CUPS_PRINTER_LOCAL";
0072     }
0073     isClass = printer.isClass();
0074     bool isRemote = false;
0075     if (printer.type() & CUPS_PRINTER_REMOTE) {
0076         qCDebug(PM_CONFIGURE_PRINTER) << "CUPS_PRINTER_REMOTE";
0077         isRemote = true;
0078     }
0079     if (printer.type() & CUPS_PRINTER_BW) {
0080         qCDebug(PM_CONFIGURE_PRINTER) << "CUPS_PRINTER_BW";
0081     }
0082     if (printer.type() & CUPS_PRINTER_COLOR) {
0083         qCDebug(PM_CONFIGURE_PRINTER) << "CUPS_PRINTER_COLOR";
0084     }
0085     if (printer.type() & CUPS_PRINTER_MFP) {
0086         qCDebug(PM_CONFIGURE_PRINTER) << "CUPS_PRINTER_MFP";
0087     }
0088 
0089     if (!isClass) {
0090         // At least on localhost:631 modify printer does not show printer options
0091         // for classes
0092         printerOptions = new PrinterOptions(destName, isClass, isRemote, this);
0093         page = new KPageWidgetItem(printerOptions, i18n("Media Settings"));
0094         page->setHeader(i18n("Media Settings"));
0095         page->setIcon(QIcon::fromTheme(QLatin1String("view-pim-tasks")));
0096         addPage(page);
0097     }
0098 
0099     printerBehavior->setRemote(isRemote);
0100     printerBehavior->setValues(printer);
0101     page = new KPageWidgetItem(printerBehavior, i18n("Banners, Policies and Allowed Users"));
0102     page->setHeader(i18n("Banners, Policies and Allowed Users"));
0103     page->setIcon(QIcon::fromTheme(QLatin1String("feed-subscribe")));
0104     addPage(page);
0105 
0106     // connect this after ALL pages were added, otherwise the slot will be called
0107     connect(this, &ConfigureDialog::currentPageChanged, this, &ConfigureDialog::currentPageChangedSlot);
0108 
0109     KConfigGroup group(KSharedConfig::openConfig(QLatin1String("print-manager")), QStringLiteral("ConfigureDialog"));
0110     KWindowConfig::restoreWindowSize(windowHandle(), group);
0111 
0112     connect(buttonBox(), &QDialogButtonBox::clicked, this, &ConfigureDialog::slotButtonClicked);
0113 }
0114 
0115 void ConfigureDialog::ppdChanged()
0116 {
0117     printerOptions->reloadPPD();
0118 }
0119 
0120 ConfigureDialog::~ConfigureDialog()
0121 {
0122     KConfigGroup group(KSharedConfig::openConfig(QLatin1String("print-manager")), QStringLiteral("ConfigureDialog"));
0123     KWindowConfig::saveWindowSize(windowHandle(), group);
0124 }
0125 
0126 void ConfigureDialog::currentPageChangedSlot(KPageWidgetItem *current, KPageWidgetItem *before)
0127 {
0128     auto currentPage = qobject_cast<PrinterPage *>(current->widget());
0129     auto beforePage = qobject_cast<PrinterPage *>(before->widget());
0130 
0131     qCDebug(PM_CONFIGURE_PRINTER) << "currentPageChanged" << beforePage << currentPage;
0132     // Check if the before page has changes
0133     savePage(beforePage);
0134     if (beforePage) {
0135         disconnect(beforePage, &PrinterPage::changed, this, &ConfigureDialog::enableButtonApply);
0136     }
0137 
0138     // connect the changed signal to the new page and check if it has changes
0139     connect(currentPage, &PrinterPage::changed, this, &ConfigureDialog::enableButtonApply);
0140     enableButtonApply(currentPage->hasChanges());
0141 }
0142 
0143 void ConfigureDialog::enableButtonApply(bool enable)
0144 {
0145     qDebug() << Q_FUNC_INFO << enable << sender();
0146     button(QDialogButtonBox::QDialogButtonBox::Apply)->setEnabled(enable);
0147 }
0148 
0149 void ConfigureDialog::slotButtonClicked(QAbstractButton *pressedButton)
0150 {
0151     auto page = qobject_cast<PrinterPage *>(currentPage()->widget());
0152     if (pressedButton == button(QDialogButtonBox::Ok) || pressedButton == button(QDialogButtonBox::Apply)) {
0153         page->save();
0154     }
0155 }
0156 
0157 void ConfigureDialog::closeEvent(QCloseEvent *event)
0158 {
0159     auto page = qobject_cast<PrinterPage *>(currentPage()->widget());
0160     if (savePage(page)) {
0161         event->accept();
0162     } else {
0163         event->ignore();
0164     }
0165 }
0166 
0167 bool ConfigureDialog::savePage(PrinterPage *page)
0168 {
0169     if (page->hasChanges()) {
0170         const int ret = KMessageBox::warningTwoActionsCancel(this,
0171                                                              i18n("The current page has changes.\n"
0172                                                                   "Do you want to save them?"),
0173                                                              i18n("Save"),
0174                                                              KStandardGuiItem::save(),
0175                                                              KStandardGuiItem::discard());
0176         if (ret == KMessageBox::PrimaryAction) {
0177             page->save();
0178         } else if (ret == KMessageBox::Cancel) {
0179             return false;
0180         }
0181     }
0182     return true;
0183 }
0184 
0185 #include "moc_ConfigureDialog.cpp"