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 "ChooseSerial.h"
0008 #include "ui_ChooseSerial.h"
0009 
0010 #include <KCupsRequest.h>
0011 #include <KLocalizedString>
0012 
0013 #include <QPainter>
0014 
0015 ChooseSerial::ChooseSerial(QWidget *parent)
0016     : GenericPage(parent)
0017     , ui(new Ui::ChooseSerial)
0018     , m_rx(QLatin1String("\\?baud=(\\d+)"))
0019 {
0020     ui->setupUi(this);
0021 
0022     // setup default options
0023     setWindowTitle(i18nc("@title:window", "Select a Printer to Add"));
0024 
0025     ui->parityCB->addItem(i18nc("@label:listbox", "None"), QLatin1String("none"));
0026     ui->parityCB->addItem(i18nc("@label:listbox", "Even"), QLatin1String("even"));
0027     ui->parityCB->addItem(i18nc("@label:listbox", "Odd"), QLatin1String("odd"));
0028 
0029     ui->flowCB->addItem(i18nc("@label:listbox", "None"), QLatin1String("none"));
0030     ui->flowCB->addItem(i18nc("@label:listbox", "XON/XOFF (Software)"), QLatin1String("soft"));
0031     ui->flowCB->addItem(i18nc("@label:listbox", "RTS/CTS (Hardware)"), QLatin1String("hard"));
0032     ui->flowCB->addItem(i18nc("@label:listbox", "DTR/DSR (Hardware)"), QLatin1String("dtrdsr"));
0033 }
0034 
0035 ChooseSerial::~ChooseSerial()
0036 {
0037     delete ui;
0038 }
0039 
0040 bool ChooseSerial::isValid() const
0041 {
0042     return m_isValid;
0043 }
0044 
0045 void ChooseSerial::setValues(const QVariantMap &args)
0046 {
0047     m_args = args;
0048     const QString deviceUri = args[KCUPS_DEVICE_URI].toString();
0049     if (!deviceUri.startsWith(QLatin1String("serial:"))) {
0050         m_isValid = false;
0051         return;
0052     }
0053     m_isValid = true;
0054 
0055     static int baudrates[] = /* Baud rates */
0056         {
0057             1200,
0058             2400,
0059             4800,
0060             9600,
0061             19200,
0062             38400,
0063             57600,
0064             115200,
0065             230400,
0066             460800,
0067         };
0068 
0069     // Find out the max baud rate
0070     int maxrate;
0071     const auto match = m_rx.match(deviceUri);
0072     if (match.hasMatch()) {
0073         maxrate = match.captured(1).toInt();
0074     } else {
0075         maxrate = 19200;
0076     }
0077 
0078     ui->baudRateCB->clear();
0079     for (int i = 0; i < 10; i++) {
0080         if (baudrates[i] > maxrate) {
0081             break;
0082         } else {
0083             ui->baudRateCB->addItem(QString::number(baudrates[i]));
0084         }
0085     }
0086     // Set the current index to the maxrate
0087     ui->baudRateCB->setCurrentIndex(ui->baudRateCB->count() - 1);
0088 }
0089 
0090 void ChooseSerial::load()
0091 {
0092 }
0093 
0094 QVariantMap ChooseSerial::values() const
0095 {
0096     QVariantMap ret = m_args;
0097     QString deviceUri = m_args[KCUPS_DEVICE_URI].toString();
0098     const int pos = deviceUri.indexOf(QLatin1Char('?'));
0099     const QString baudRate = ui->baudRateCB->currentText();
0100     const QString bits = ui->bitsCB->currentText();
0101     const QString parity = ui->baudRateCB->itemData(ui->baudRateCB->currentIndex()).toString();
0102     const QString flow = ui->flowCB->itemData(ui->flowCB->currentIndex()).toString();
0103     const QString replace = QString::fromLatin1("?baud=%1+bits=%2+parity=%3+flow=%4").arg(baudRate, bits, parity, flow);
0104     deviceUri.replace(pos, deviceUri.size() - pos, replace);
0105     ret[KCUPS_DEVICE_URI] = deviceUri;
0106     return ret;
0107 }
0108 
0109 #include "moc_ChooseSerial.cpp"