Warning, file /utilities/print-manager/add-printer/PageChoosePPD.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 "PageChoosePPD.h"
0008 #include "ui_PageChoosePPD.h"
0009 
0010 #include "DevicesModel.h"
0011 
0012 #include <SelectMakeModel.h>
0013 #include <KCupsRequest.h>
0014 #include <KLocalizedString>
0015 
0016 #include <QFileInfo>
0017 #include <QFile>
0018 #include <QDebug>
0019 #include <QUrl>
0020 #include <QTemporaryFile>
0021 
0022 PageChoosePPD::PageChoosePPD(const QVariantHash &args, QWidget *parent) :
0023     GenericPage(parent),
0024     ui(new Ui::PageChoosePPD)
0025 {
0026     ui->setupUi(this);
0027     setAttribute(Qt::WA_DeleteOnClose);
0028 
0029     // setup default options
0030     setWindowTitle(i18nc("@title:window", "Select a Printer to Add"));
0031 
0032     m_layout = new QStackedLayout;
0033     m_layout->setContentsMargins(0, 0, 0, 0);
0034     ui->gridLayout->addLayout(m_layout, 1, 3);
0035     m_selectMM = new SelectMakeModel(this);
0036     connect(m_selectMM, &SelectMakeModel::changed, this, &PageChoosePPD::checkSelected);
0037     m_layout->addWidget(m_selectMM);
0038 
0039     // Setup the busy cursor
0040     connect(m_selectMM, &SelectMakeModel::changed, this, &PageChoosePPD::notWorking);
0041 
0042     if (!args.isEmpty()) {
0043         // set our args
0044         setValues(args);
0045     }
0046 }
0047 
0048 PageChoosePPD::~PageChoosePPD()
0049 {
0050     removeTempPPD();
0051 
0052     delete ui;
0053 }
0054 
0055 void PageChoosePPD::setValues(const QVariantHash &args)
0056 {
0057     m_args = args;
0058 
0059     if (args[ADDING_PRINTER].toBool()) {
0060 
0061         qDebug() << args;
0062         working();
0063         removeTempPPD();
0064         const QString deviceId = args[KCUPS_DEVICE_ID].toString();
0065         QString make;
0066         QString makeAndModel = args[KCUPS_DEVICE_MAKE_AND_MODEL].toString();
0067         const QString deviceURI = args[KCUPS_DEVICE_URI].toString();
0068 
0069         // If
0070         QUrl url(deviceURI + QLatin1String(".ppd"));
0071         if (url.scheme() == QLatin1String("ipp")) {
0072             auto tempFile = new QTemporaryFile;
0073             tempFile->setFileTemplate(QLatin1String("print-manager-XXXXXX.ppd"));
0074             tempFile->open();
0075             url.setScheme(QLatin1String("http"));
0076             if (url.port() < 0) {
0077                 url.setPort(631);
0078             }
0079             qDebug() << deviceURI << url;
0080             KJob *job = KIO::file_copy(url,
0081                                       QUrl::fromLocalFile(tempFile->fileName()),
0082                                       -1,
0083                                       KIO::Overwrite | KIO::HideProgressInfo);
0084             job->setProperty("URI", deviceURI);
0085             connect(job, &KJob::result, this, &PageChoosePPD::resultJob);
0086         }
0087 
0088         // Get the make from the device id
0089         for (const QString &pair : deviceId.split(QLatin1Char(';'))) {
0090             if (pair.startsWith(QLatin1String("MFG:"))) {
0091                 make = pair.section(QLatin1Char(':'), 1);
0092                 break;
0093             }
0094         }
0095 
0096         if (makeAndModel.isEmpty()) {
0097             // Get the model  from the device id
0098             for (const QString &pair : deviceId.split(QLatin1Char(';'))) {
0099                 if (pair.startsWith(QLatin1String("MDL:"))) {
0100                     // Build the make and model string
0101                     if (make.isNull()) {
0102                         makeAndModel = pair.section(QLatin1Char(':'), 1);
0103                     } else {
0104                         makeAndModel = make + QLatin1Char(' ') + pair.section(QLatin1Char(':'), 1);
0105                     }
0106                     break;
0107                 }
0108             }
0109         }
0110 
0111         // if the device info is empty use the make and model
0112         // so we can have a nice name for the new printer on the next page
0113         if (!args.contains(KCUPS_DEVICE_INFO) && !makeAndModel.isEmpty()) {
0114             m_args[KCUPS_DEVICE_INFO] = makeAndModel;
0115         }
0116 
0117         m_selectMM->setDeviceInfo(deviceId, make, makeAndModel, deviceURI);
0118         m_isValid = true;
0119     } else {
0120         m_isValid = false;
0121     }
0122 }
0123 
0124 bool PageChoosePPD::isValid() const
0125 {
0126     return m_isValid;
0127 }
0128 
0129 QVariantHash PageChoosePPD::values() const
0130 {
0131     if (!isValid()) {
0132         return m_args;
0133     }
0134 
0135     QVariantHash ret = m_args;
0136     if (canProceed()) {
0137         if (!m_ppdFile.isNull()) {
0138             ret[FILENAME] = m_ppdFile;
0139         } else if (m_selectMM->isFileSelected()) {
0140             ret[FILENAME] = m_selectMM->selectedPPDFileName();
0141         } else {
0142             ret[PPD_NAME] = m_selectMM->selectedPPDName();
0143         }
0144     }
0145     return ret;
0146 }
0147 
0148 bool PageChoosePPD::canProceed() const
0149 {
0150     // It can proceed if a PPD file (local or not) is provided    bool changed = false;
0151     bool allow = false;
0152 
0153     if (m_selectMM->isFileSelected()) {
0154         allow = !m_selectMM->selectedPPDFileName().isNull();
0155     } else if (!m_ppdFile.isNull()) {
0156         allow = true;
0157     } else {
0158         allow = !m_selectMM->selectedPPDName().isNull();
0159     }
0160 
0161     qDebug() << allow;
0162     return allow;
0163 }
0164 
0165 void PageChoosePPD::checkSelected()
0166 {
0167     Q_EMIT allowProceed(canProceed());
0168 }
0169 
0170 void PageChoosePPD::selectDefault()
0171 {
0172 }
0173 
0174 void PageChoosePPD::resultJob(KJob *job)
0175 {
0176     if (!job->error() && job->property("URI").toString() == m_args[KCUPS_DEVICE_URI].toString()) {
0177         auto fileCopyJob = qobject_cast<KIO::FileCopyJob*>(job);
0178 
0179         // Make sure this job is for the current device
0180         m_ppdFile = fileCopyJob->destUrl().toLocalFile();
0181         m_isValid = false;
0182         Q_EMIT proceed();
0183     }
0184 }
0185 
0186 void PageChoosePPD::removeTempPPD()
0187 {
0188     if (!m_ppdFile.isEmpty()) {
0189         QFile::remove(m_ppdFile);
0190         m_ppdFile.clear();
0191     }
0192 }
0193 
0194 #include "moc_PageChoosePPD.cpp"