File indexing completed on 2025-01-19 05:06:21

0001 /*
0002     SPDX-FileCopyrightText: 2010 Daniel Nicoletti <dantti12@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "ChooseSocket.h"
0008 #include "ui_ChooseSocket.h"
0009 
0010 #include <KCupsRequest.h>
0011 #include <KLocalizedString>
0012 
0013 #include <QPainter>
0014 #include <QUrl>
0015 
0016 ChooseSocket::ChooseSocket(QWidget *parent)
0017     : GenericPage(parent)
0018     , ui(new Ui::ChooseSocket)
0019 {
0020     ui->setupUi(this);
0021 
0022     // setup default options
0023     setWindowTitle(i18nc("@title:window", "Select a Printer to Add"));
0024 
0025     connect(ui->addressLE, &QLineEdit::textChanged, this, [this](const QString &) {
0026         Q_EMIT allowProceed(canProceed());
0027     });
0028 }
0029 
0030 ChooseSocket::~ChooseSocket()
0031 {
0032     delete ui;
0033 }
0034 
0035 void ChooseSocket::setValues(const QVariantMap &args)
0036 {
0037     if (m_args == args) {
0038         return;
0039     }
0040 
0041     m_args = args;
0042     ui->addressLE->clear();
0043     ui->portISB->setValue(9100);
0044     const QString deviceUri = args[KCUPS_DEVICE_URI].toString();
0045     QUrl url(deviceUri);
0046     if (url.scheme() == QLatin1String("socket")) {
0047         ui->addressLE->setText(url.host());
0048         ui->portISB->setValue(url.port(9100));
0049     }
0050     ui->addressLE->setFocus();
0051 
0052     m_isValid = true;
0053 }
0054 
0055 QVariantMap ChooseSocket::values() const
0056 {
0057     QVariantMap ret = m_args;
0058     QUrl url = QUrl(QLatin1String("socket://") + ui->addressLE->text());
0059     url.setPort(ui->portISB->value());
0060     ret[KCUPS_DEVICE_URI] = url.toDisplayString();
0061     return ret;
0062 }
0063 
0064 bool ChooseSocket::isValid() const
0065 {
0066     return m_isValid;
0067 }
0068 
0069 bool ChooseSocket::canProceed() const
0070 {
0071     return !ui->addressLE->text().isEmpty();
0072 }
0073 
0074 #include "moc_ChooseSocket.cpp"