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

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