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 "ChooseUri.h"
0008 #include "ui_ChooseUri.h"
0009 
0010 #include <KCupsRequest.h>
0011 #include <KLocalizedString>
0012 
0013 #include <QLoggingCategory>
0014 
0015 Q_DECLARE_LOGGING_CATEGORY(PM_ADD_PRINTER)
0016 
0017 ChooseUri::ChooseUri(QWidget *parent) :
0018     GenericPage(parent),
0019     ui(new Ui::ChooseUri)
0020 {
0021     ui->setupUi(this);
0022 
0023     ui->searchTB->setIcon(QIcon::fromTheme(QLatin1String("edit-find")));
0024 
0025     // setup default options
0026     setWindowTitle(i18nc("@title:window", "Select a Printer to Add"));
0027 
0028     connect(ui->addressLE, &QLineEdit::textChanged, this, &ChooseUri::checkSelected);
0029     connect(ui->addressLE, &QLineEdit::returnPressed, this, &ChooseUri::findPrinters);
0030     connect(ui->searchTB, &QToolButton::clicked, this, &ChooseUri::findPrinters);
0031 }
0032 
0033 ChooseUri::~ChooseUri()
0034 {
0035     delete ui;
0036 }
0037 
0038 void ChooseUri::setValues(const QVariantHash &args)
0039 {
0040     m_args = args;
0041     bool visible = false;
0042     const QUrl url(args[KCUPS_DEVICE_URI].toString());
0043     if (url.url() == QLatin1String("other")) {
0044         ui->addressLE->clear();
0045         visible = true;
0046     } else if (url.scheme().isEmpty() && url.authority().isEmpty()) {
0047         ui->addressLE->setText(url.url() + QLatin1String("://"));
0048     } else {
0049         ui->addressLE->setText(url.url());
0050     }
0051     ui->searchTB->setVisible(visible);
0052     ui->addressLE->setFocus();
0053 }
0054 
0055 QVariantHash ChooseUri::values() const
0056 {
0057     QVariantHash ret = m_args;
0058 
0059     ret[KCUPS_DEVICE_URI] = parsedURL(ui->addressLE->text()).toString();
0060 
0061     return ret;
0062 }
0063 
0064 bool ChooseUri::isValid() const
0065 {
0066     const QString urlDefault = m_args[KCUPS_DEVICE_URI].toString();
0067     const QVariantHash args = values();
0068     const QString deviceUri = args[KCUPS_DEVICE_URI].toString();
0069     QUrl url(deviceUri);
0070 //    qCDebug(PM_ADD_PRINTER) << url << url.isValid() << url.isEmpty() << url.scheme().isEmpty() << url.host() << url.toString();
0071     return (url.isValid() && !url.isEmpty() && !url.scheme().isEmpty() && !url.host().isEmpty())
0072             || urlDefault == deviceUri;
0073 }
0074 
0075 bool ChooseUri::canProceed() const
0076 {
0077     return isValid();
0078 }
0079 
0080 void ChooseUri::load()
0081 {
0082 }
0083 
0084 void ChooseUri::checkSelected()
0085 {
0086     Q_EMIT allowProceed(isValid());
0087 }
0088 
0089 void ChooseUri::on_addressLE_textChanged(const QString &text)
0090 {
0091     QUrl url = parsedURL(text);
0092 
0093     if (url.isValid() &&
0094             (url.scheme().isEmpty() ||
0095              url.scheme() == QLatin1String("http") ||
0096              url.scheme() == QLatin1String("https") ||
0097              url.scheme() == QLatin1String("ipp"))) {
0098         // TODO maybe cups library can connect to more protocols
0099         ui->searchTB->setEnabled(true);
0100     } else {
0101         ui->searchTB->setEnabled(false);
0102     }
0103 }
0104 
0105 void ChooseUri::findPrinters()
0106 {
0107     const QUrl url = parsedURL(ui->addressLE->text());
0108 
0109     auto conn = new KCupsConnection(url, this);
0110     auto request = new KCupsRequest(conn);
0111     connect(request, &KCupsRequest::finished, this, &ChooseUri::getPrintersFinished);
0112 
0113     request->setProperty("URI", url);
0114 
0115     Q_EMIT startWorking();
0116     request->getPrinters({
0117                              KCUPS_PRINTER_NAME,
0118                              KCUPS_PRINTER_STATE,
0119                              KCUPS_PRINTER_IS_SHARED,
0120                              KCUPS_PRINTER_IS_ACCEPTING_JOBS,
0121                              KCUPS_PRINTER_TYPE,
0122                              KCUPS_PRINTER_LOCATION,
0123                              KCUPS_PRINTER_INFO,
0124                              KCUPS_PRINTER_MAKE_AND_MODEL
0125                          });
0126 }
0127 
0128 void ChooseUri::getPrintersFinished(KCupsRequest *request)
0129 {
0130     QUrl uri = request->property("URI").value<QUrl>();
0131     QUrl url;
0132     url.setScheme(QStringLiteral("ipp"));
0133     url.setAuthority(uri.authority());
0134 
0135     KCupsPrinters printers = request->printers();
0136     if (request->hasError()) {
0137         Q_EMIT errorMessage(request->errorMsg());
0138     } else {
0139         Q_EMIT insertDevice(QLatin1String("network"),
0140                           url.authority(),
0141                           url.authority(),
0142                           QString(),
0143                           url.url(),
0144                           QString(),
0145                           printers);
0146     }
0147 
0148     request->deleteLater();
0149     request->connection()->deleteLater();
0150     Q_EMIT stopWorking();
0151 }
0152 
0153 QUrl ChooseUri::parsedURL(const QString &text) const
0154 {
0155     const QString urlDefault = m_args[KCUPS_DEVICE_URI].toString();
0156     QUrl url(QUrl::fromUserInput(text));
0157     if (url.host().isEmpty() && !text.contains(QLatin1String("://")) && urlDefault != text) {
0158         url = QUrl();
0159         // URI might be scsi, network on anything that didn't match before
0160         if (m_args[KCUPS_DEVICE_URI].toString() != QLatin1String("other")) {
0161             url.setScheme(m_args[KCUPS_DEVICE_URI].toString());
0162         }
0163         url.setAuthority(text);
0164     }
0165     return url;
0166 }
0167 
0168 #include "moc_ChooseUri.cpp"