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

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 QVariantHash &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     if (m_rx.indexIn(deviceUri) != -1) {
0072         maxrate = m_rx.cap(1).toInt();
0073     } else {
0074         maxrate = 19200;
0075     }
0076 
0077     ui->baudRateCB->clear();
0078     for (int i = 0; i < 10; i ++) {
0079         if (baudrates[i] > maxrate) {
0080             break;
0081         } else {
0082             ui->baudRateCB->addItem(QString::number(baudrates[i]));
0083         }
0084     }
0085     // Set the current index to the maxrate
0086     ui->baudRateCB->setCurrentIndex(ui->baudRateCB->count() - 1);
0087 }
0088 
0089 void ChooseSerial::load()
0090 {
0091 }
0092 
0093 QVariantHash ChooseSerial::values() const
0094 {
0095     QVariantHash ret = m_args;
0096     QString deviceUri = m_args[KCUPS_DEVICE_URI].toString();
0097     const int pos = deviceUri.indexOf(QLatin1Char('?'));
0098     const QString baudRate = ui->baudRateCB->currentText();
0099     const QString bits = ui->bitsCB->currentText();
0100     const QString parity = ui->baudRateCB->itemData(ui->baudRateCB->currentIndex()).toString();
0101     const QString flow = ui->flowCB->itemData(ui->flowCB->currentIndex()).toString();
0102     const QString replace = QString::fromLatin1("?baud=%1+bits=%2+parity=%3+flow=%4")
0103             .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"