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

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 QVariantHash &args)
0061 {
0062     m_args = args;
0063     ui->addressLE->setFocus();
0064 }
0065 
0066 QVariantHash ChooseSamba::values() const
0067 {
0068     QVariantHash 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() &&
0081                QUrl::fromUserInput(address).scheme() != QStringLiteral("smb")) {
0082         url = QUrl::fromUserInput(address);
0083         url.setScheme(QStringLiteral("smb"));
0084     } else {
0085         url = QUrl(QStringLiteral("smb://") + address);
0086     }
0087 
0088     qDebug() << 1 << url;
0089     if (!ui->usernameLE->text().isEmpty()) {
0090         url.setUserName(ui->usernameLE->text());
0091     }
0092 
0093     if (!ui->passwordLE->text().isEmpty()) {
0094         url.setPassword(ui->passwordLE->text());
0095     }
0096 
0097     qDebug() << 2 << url;
0098     qDebug() << 3 << url.url() << url.path().section(QLatin1Char('/'), -1, -1);// same as url.fileName()
0099     qDebug() << 4 << url.fileName();
0100     qDebug() << 5 << url.host() << url.url().section(QLatin1Char('/'), 3, 3).toLower();
0101 
0102     ret[KCUPS_DEVICE_URI] = url.url();
0103     ret[KCUPS_DEVICE_INFO] = url.fileName();
0104 
0105     // if there is 4 '/' means the url is like
0106     // smb://group/host/printer, so the location is at a different place
0107     if (url.url().count(QLatin1Char('/')) == 4) {
0108         ret[KCUPS_DEVICE_LOCATION] = url.url().section(QLatin1Char('/'), 3, 3).toLower();
0109     } else {
0110         ret[KCUPS_DEVICE_LOCATION] = url.host();
0111     }
0112 
0113     return ret;
0114 }
0115 
0116 bool ChooseSamba::isValid() const
0117 {
0118     const QVariantHash args = values();
0119     const QUrl url(args[KCUPS_DEVICE_URI].toString());
0120 
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 }
0129 
0130 bool ChooseSamba::canProceed() const
0131 {
0132     return isValid();
0133 }
0134 
0135 void ChooseSamba::load()
0136 {
0137 }
0138 
0139 void ChooseSamba::checkSelected()
0140 {
0141     Q_EMIT allowProceed(isValid());
0142 }
0143 
0144 #include "moc_ChooseSamba.cpp"