File indexing completed on 2025-01-19 05:06:22

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 "PageAddPrinter.h"
0008 #include "ui_PageAddPrinter.h"
0009 
0010 #include <KCupsRequest.h>
0011 #include <KLocalizedString>
0012 
0013 #include <QDebug>
0014 #include <QPainter>
0015 #include <QPointer>
0016 #include <QRegularExpressionValidator>
0017 
0018 PageAddPrinter::PageAddPrinter(QWidget *parent)
0019     : GenericPage(parent)
0020     , ui(new Ui::PageAddPrinter)
0021 {
0022     ui->setupUi(this);
0023     setAttribute(Qt::WA_DeleteOnClose);
0024 
0025     // setup default options
0026     setWindowTitle(i18nc("@title:window", "Select a Printer to Add"));
0027 
0028     const int printerSize = 128;
0029     const int overlaySize = 48;
0030 
0031     QPixmap printerIcon = QIcon::fromTheme(QStringLiteral("printer")).pixmap(printerSize);
0032     const QPixmap preferencesIcon = QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(overlaySize);
0033 
0034     QPainter painter(&printerIcon);
0035 
0036     // bottom right corner
0037     const QPoint startPoint = QPoint(printerSize - overlaySize - 2, printerSize - overlaySize - 2);
0038     painter.drawPixmap(startPoint, preferencesIcon);
0039 
0040     ui->printerL->setPixmap(printerIcon);
0041 
0042     // May contain any printable characters except "/", "#", and space
0043     const QRegularExpression rx(QLatin1String("[^/#\\ ]*"));
0044     auto validator = new QRegularExpressionValidator(rx, this);
0045     ui->nameLE->setValidator(validator);
0046     connect(ui->nameLE, &QLineEdit::textChanged, this, [this](const QString &text) {
0047         Q_EMIT allowProceed(!text.isEmpty());
0048     });
0049 
0050     // Hide the message widget
0051     ui->messageWidget->setWordWrap(true);
0052     ui->messageWidget->setMessageType(KMessageWidget::Error);
0053     ui->messageWidget->hide();
0054 }
0055 
0056 PageAddPrinter::~PageAddPrinter()
0057 {
0058     delete ui;
0059 }
0060 
0061 void PageAddPrinter::setValues(const QVariantMap &args)
0062 {
0063     if (m_args != args) {
0064         QString name;
0065         if (!args[KCUPS_PRINTER_NAME].toString().isEmpty()) {
0066             name = args[KCUPS_PRINTER_NAME].toString();
0067         } else if (!args[KCUPS_DEVICE_MAKE_AND_MODEL].toString().isEmpty()) {
0068             name = args[KCUPS_DEVICE_MAKE_AND_MODEL].toString();
0069         } else if (!args[KCUPS_DEVICE_INFO].toString().isEmpty()) {
0070             name = args[KCUPS_DEVICE_INFO].toString();
0071         }
0072 
0073         if (!args[KCUPS_PRINTER_INFO].toString().isEmpty()) {
0074             ui->descriptionLE->setText(args[KCUPS_PRINTER_INFO].toString());
0075         } else {
0076             ui->descriptionLE->setText(name);
0077         }
0078 
0079         name.replace(QLatin1Char(' '), QLatin1Char('_'));
0080         name.replace(QLatin1Char('/'), QLatin1Char('-'));
0081         name.replace(QLatin1Char('#'), QLatin1Char('='));
0082         ui->nameLE->setText(name);
0083         ui->locationLE->setText(args[KCUPS_DEVICE_LOCATION].toString());
0084         ui->shareCB->setChecked(true);
0085         ui->shareCB->setVisible(args[ADDING_PRINTER].toBool());
0086 
0087         m_args = args;
0088     }
0089 }
0090 
0091 void PageAddPrinter::load()
0092 {
0093 }
0094 
0095 bool PageAddPrinter::canProceed() const
0096 {
0097     return !ui->nameLE->text().isEmpty();
0098 }
0099 
0100 bool PageAddPrinter::finishClicked()
0101 {
0102     bool ret = false;
0103     QVariantMap args = values();
0104     args[KCUPS_PRINTER_IS_ACCEPTING_JOBS] = true;
0105     args[KCUPS_PRINTER_STATE] = IPP_PRINTER_IDLE;
0106 
0107     // Check if it's a printer or a class that we are adding
0108     bool isClass = !args.take(ADDING_PRINTER).toBool();
0109     QString destName = args[KCUPS_PRINTER_NAME].toString();
0110     QString filename = args.take(FILENAME).toString();
0111 
0112     QPointer<KCupsRequest> request = new KCupsRequest;
0113     if (isClass) {
0114         request->addOrModifyClass(destName, args);
0115     } else {
0116         request->addOrModifyPrinter(destName, args, filename);
0117     }
0118     request->waitTillFinished();
0119     if (request) {
0120         if (request->hasError()) {
0121             qDebug() << request->error() << request->errorMsg();
0122             QString message;
0123             if (isClass) {
0124                 message = i18nc("@info", "Failed to add class: '%1'", request->errorMsg());
0125             } else {
0126                 message = i18nc("@info", "Failed to configure printer: '%1'", request->errorMsg());
0127             }
0128             ui->messageWidget->setText(message);
0129             ui->messageWidget->animatedShow();
0130         } else {
0131             ret = true;
0132         }
0133         request->deleteLater();
0134     }
0135 
0136     return ret;
0137 }
0138 
0139 QVariantMap PageAddPrinter::values() const
0140 {
0141     QVariantMap ret = m_args;
0142     ret[KCUPS_PRINTER_NAME] = ui->nameLE->text();
0143     ret[KCUPS_PRINTER_LOCATION] = ui->locationLE->text();
0144     ret[KCUPS_PRINTER_INFO] = ui->descriptionLE->text();
0145     if (ret[ADDING_PRINTER].toBool()) {
0146         ret[KCUPS_PRINTER_IS_SHARED] = ui->shareCB->isChecked();
0147     }
0148     return ret;
0149 }
0150 
0151 void PageAddPrinter::checkSelected()
0152 {
0153     //     Q_EMIT allowProceed(!devicesLV->selectionModel()->selection().isEmpty());
0154 }
0155 
0156 #include "moc_PageAddPrinter.cpp"