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 "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::textChanged); 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 QVariantMap &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 QVariantMap ChooseUri::values() const 0056 { 0057 QVariantMap 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 QVariantMap 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()) || urlDefault == deviceUri; 0072 } 0073 0074 bool ChooseUri::canProceed() const 0075 { 0076 return isValid(); 0077 } 0078 0079 void ChooseUri::load() 0080 { 0081 } 0082 0083 void ChooseUri::checkSelected() 0084 { 0085 Q_EMIT allowProceed(isValid()); 0086 } 0087 0088 void ChooseUri::textChanged(const QString &text) 0089 { 0090 QUrl url = parsedURL(text); 0091 0092 if (url.isValid() 0093 && (url.scheme().isEmpty() || url.scheme() == QLatin1String("http") || url.scheme() == QLatin1String("https") 0094 || url.scheme() == QLatin1String("ipp"))) { 0095 // TODO maybe cups library can connect to more protocols 0096 ui->searchTB->setEnabled(true); 0097 } else { 0098 ui->searchTB->setEnabled(false); 0099 } 0100 0101 checkSelected(); 0102 } 0103 0104 void ChooseUri::findPrinters() 0105 { 0106 const QUrl url = parsedURL(ui->addressLE->text()); 0107 0108 auto conn = new KCupsConnection(url, this); 0109 auto request = new KCupsRequest(conn); 0110 connect(request, &KCupsRequest::finished, this, &ChooseUri::getPrintersFinished); 0111 0112 request->setProperty("URI", url); 0113 0114 Q_EMIT startWorking(); 0115 request->getPrinters({KCUPS_PRINTER_NAME, 0116 KCUPS_PRINTER_STATE, 0117 KCUPS_PRINTER_IS_SHARED, 0118 KCUPS_PRINTER_IS_ACCEPTING_JOBS, 0119 KCUPS_PRINTER_TYPE, 0120 KCUPS_PRINTER_LOCATION, 0121 KCUPS_PRINTER_INFO, 0122 KCUPS_PRINTER_MAKE_AND_MODEL}); 0123 } 0124 0125 void ChooseUri::getPrintersFinished(KCupsRequest *request) 0126 { 0127 QUrl uri = request->property("URI").value<QUrl>(); 0128 QUrl url; 0129 url.setScheme(QStringLiteral("ipp")); 0130 url.setAuthority(uri.authority()); 0131 0132 KCupsPrinters printers = request->printers(); 0133 if (request->hasError()) { 0134 Q_EMIT errorMessage(request->errorMsg()); 0135 } else { 0136 Q_EMIT insertDevice(QLatin1String("network"), url.authority(), url.authority(), QString(), url.url(), QString(), printers); 0137 } 0138 0139 request->deleteLater(); 0140 request->connection()->deleteLater(); 0141 Q_EMIT stopWorking(); 0142 } 0143 0144 QUrl ChooseUri::parsedURL(const QString &text) const 0145 { 0146 const QString urlDefault = m_args[KCUPS_DEVICE_URI].toString(); 0147 QUrl url(QUrl::fromUserInput(text)); 0148 if (url.host().isEmpty() && !text.contains(QLatin1String("://")) && urlDefault != text) { 0149 url = QUrl(); 0150 // URI might be scsi, network on anything that didn't match before 0151 if (m_args[KCUPS_DEVICE_URI].toString() != QLatin1String("other")) { 0152 url.setScheme(m_args[KCUPS_DEVICE_URI].toString()); 0153 } 0154 url.setAuthority(text); 0155 } 0156 return url; 0157 } 0158 0159 #include "moc_ChooseUri.cpp"