File indexing completed on 2025-01-19 05:06:21
0001 /* 0002 SPDX-FileCopyrightText: 2010 Daniel Nicoletti <dantti12@gmail.com> 0003 SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "ChooseSamba.h" 0009 #include "ui_ChooseSamba.h" 0010 0011 #include <KCupsRequest.h> 0012 #include <KLocalizedString> 0013 0014 #include <QDebug> 0015 #include <QFileDialog> 0016 #include <QPainter> 0017 #include <QUrl> 0018 0019 ChooseSamba::ChooseSamba(QWidget *parent) 0020 : GenericPage(parent) 0021 , ui(new Ui::ChooseSamba) 0022 { 0023 // Opt into printer listing in the KIO SMB worker. 0024 qputenv("KIO_SMB_PRINTERS", QByteArrayLiteral("1")); 0025 0026 ui->setupUi(this); 0027 0028 // setup default options 0029 setWindowTitle(i18nc("@title:window", "Select a Printer to Add")); 0030 0031 connect(ui->addressLE, &QLineEdit::textChanged, this, &ChooseSamba::checkSelected); 0032 connect(ui->usernameLE, &QLineEdit::textChanged, this, &ChooseSamba::checkSelected); 0033 connect(ui->passwordLE, &QLineEdit::textChanged, this, &ChooseSamba::checkSelected); 0034 connect(ui->browsePB, &QPushButton::clicked, this, [this] { 0035 auto dialog = new QFileDialog(this); 0036 dialog->setDirectoryUrl(QUrl(QStringLiteral("smb://"))); 0037 dialog->setMimeTypeFilters({QStringLiteral("inode/vnd.kde.kio.smb.printer")}); 0038 dialog->setSupportedSchemes({QStringLiteral("smb")}); 0039 connect(dialog, &QFileDialog::accepted, this, [dialog, this] { 0040 dialog->hide(); 0041 dialog->deleteLater(); 0042 const QList<QUrl> urls = dialog->selectedUrls(); 0043 if (urls.isEmpty()) { 0044 return; 0045 } 0046 QUrl url = urls.constFirst(); 0047 url.setQuery(QString()); // clear kio-smb query artifacts such as ?kio-printer=true 0048 ui->addressLE->setText(url.toString()); 0049 }); 0050 dialog->show(); 0051 }); 0052 ui->browsePB->setEnabled(true); 0053 } 0054 0055 ChooseSamba::~ChooseSamba() 0056 { 0057 delete ui; 0058 } 0059 0060 void ChooseSamba::setValues(const QVariantMap &args) 0061 { 0062 m_args = args; 0063 ui->addressLE->setFocus(); 0064 } 0065 0066 QVariantMap ChooseSamba::values() const 0067 { 0068 QVariantMap ret = m_args; 0069 0070 const QString address = ui->addressLE->text().trimmed(); 0071 QUrl url; 0072 if (address.startsWith(QLatin1String("//"))) { 0073 url = QUrl(QLatin1String("smb:") + address); 0074 } else if (address.startsWith(QLatin1String("/"))) { 0075 url = QUrl(QLatin1String("smb:/") + address); 0076 } else if (address.startsWith(QLatin1String("://"))) { 0077 url = QUrl(QLatin1String("smb") + address); 0078 } else if (address.startsWith(QLatin1String("smb://"))) { 0079 url = QUrl(address); 0080 } else if (!QUrl::fromUserInput(address).scheme().isEmpty() && QUrl::fromUserInput(address).scheme() != QStringLiteral("smb")) { 0081 url = QUrl::fromUserInput(address); 0082 url.setScheme(QStringLiteral("smb")); 0083 } else { 0084 url = QUrl(QStringLiteral("smb://") + address); 0085 } 0086 0087 qDebug() << 1 << url; 0088 if (!ui->usernameLE->text().isEmpty()) { 0089 url.setUserName(ui->usernameLE->text()); 0090 } 0091 0092 if (!ui->passwordLE->text().isEmpty()) { 0093 url.setPassword(ui->passwordLE->text()); 0094 } 0095 0096 qDebug() << 2 << url; 0097 qDebug() << 3 << url.url() << url.path().section(QLatin1Char('/'), -1, -1); // same as url.fileName() 0098 qDebug() << 4 << url.fileName(); 0099 qDebug() << 5 << url.host() << url.url().section(QLatin1Char('/'), 3, 3).toLower(); 0100 0101 ret[KCUPS_DEVICE_URI] = url.url(); 0102 ret[KCUPS_DEVICE_INFO] = url.fileName(); 0103 0104 // if there is 4 '/' means the url is like 0105 // smb://group/host/printer, so the location is at a different place 0106 if (url.url().count(QLatin1Char('/')) == 4) { 0107 ret[KCUPS_DEVICE_LOCATION] = url.url().section(QLatin1Char('/'), 3, 3).toLower(); 0108 } else { 0109 ret[KCUPS_DEVICE_LOCATION] = url.host(); 0110 } 0111 0112 return ret; 0113 } 0114 0115 bool ChooseSamba::isValid() const 0116 { 0117 const QVariantMap args = values(); 0118 const QUrl url(args[KCUPS_DEVICE_URI].toString()); 0119 0120 // clang-format off 0121 return url.isValid() && 0122 !url.isEmpty() && 0123 !url.scheme().isEmpty() && 0124 !url.host().isEmpty() && 0125 !url.path().isEmpty() && 0126 !url.fileName().isEmpty() && 0127 url.url().count(QLatin1Char('/')) <= 4; 0128 // clang-format on 0129 } 0130 0131 bool ChooseSamba::canProceed() const 0132 { 0133 return isValid(); 0134 } 0135 0136 void ChooseSamba::load() 0137 { 0138 } 0139 0140 void ChooseSamba::checkSelected() 0141 { 0142 Q_EMIT allowProceed(isValid()); 0143 } 0144 0145 #include "moc_ChooseSamba.cpp"