File indexing completed on 2024-04-21 09:24:33

0001 /*
0002     SPDX-FileCopyrightText: 2011 Lamarque Souza <lamarque@kde.org>
0003     SPDX-FileCopyrightText: 2013 Lukas Tinkl <ltinkl@redhat.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "pindialog.h"
0009 
0010 #include <QIcon>
0011 #include <QIntValidator>
0012 #include <QScreen>
0013 #include <QStringBuilder>
0014 
0015 #include <KLocalizedString>
0016 #include <KWindowSystem>
0017 #include <KX11Extras>
0018 
0019 #include <ModemManagerQt/Manager>
0020 
0021 PinDialog::PinDialog(ModemManager::Modem *modem, const Type type, QWidget *parent)
0022     : QDialog(parent)
0023     , m_type(type)
0024 {
0025     if (modem) {
0026         m_udi = modem->uni();
0027         m_name = modem->device();
0028         for (const Solid::Device &d : Solid::Device::allDevices()) {
0029             if (d.udi().contains(m_name, Qt::CaseInsensitive)) {
0030                 m_name = d.product();
0031                 if (!m_name.startsWith(d.vendor())) {
0032                     m_name = d.vendor() % ' ' % m_name;
0033                 }
0034                 break;
0035             }
0036         }
0037     }
0038 
0039     ui = new Ui::PinWidget();
0040     ui->setupUi(this);
0041     ui->pin->setEchoMode(QLineEdit::Password);
0042 
0043     auto validator = new QIntValidator(this);
0044     validator->setRange(1000, 99999999);
0045     ui->pin->setValidator(validator);
0046     ui->pin2->setValidator(validator);
0047 
0048     auto validator2 = new QIntValidator(this);
0049     validator2->setRange(10000000, 99999999);
0050     ui->puk->setValidator(validator2);
0051 
0052     ui->errorMessage->setHidden(true);
0053     const QRect desktop = screen()->availableGeometry();
0054     // QRect desktop = QApplication::desktop()->screenGeometry(topLevelWidget());
0055     setMinimumWidth(360); // width of the PinePhone display
0056 
0057     pixmapLabel = new QLabel(this);
0058     pixmapLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
0059     ui->gridLayout->addWidget(pixmapLabel, 0, 0);
0060     pixmapLabel->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-password")).pixmap(32));
0061 
0062     connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &PinDialog::accept);
0063     connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &PinDialog::reject);
0064 
0065     if (isPukDialog()) {
0066         QString pukType;
0067         if (m_type == PinDialog::SimPuk) {
0068             pukType = i18n("SIM PUK");
0069         } else if (m_type == PinDialog::SimPuk2) {
0070             pukType = i18n("SIM PUK2");
0071         } else if (m_type == PinDialog::ModemServiceProviderPuk) {
0072             pukType = i18n("Service provider PUK");
0073         } else if (m_type == PinDialog::ModemNetworkPuk) {
0074             pukType = i18n("Network PUK");
0075         } else if (m_type == PinDialog::ModemCorporatePuk) {
0076             pukType = i18n("Corporate PUK");
0077         } else if (m_type == PinDialog::ModemPhFsimPuk) {
0078             pukType = i18n("PH-FSIM PUK");
0079         } else {
0080             pukType = i18n("Network Subset PUK");
0081         }
0082 
0083         setWindowTitle(i18n("%1 unlock required", pukType));
0084         ui->title->setText(i18n("%1 Unlock Required", pukType));
0085         ui->prompt->setText(i18n("The mobile broadband device '%1' requires a %2 code before it can be used.", m_name, pukType));
0086         ui->pukLabel->setText(i18n("%1 code:", pukType));
0087         ui->pinLabel->setText(i18n("New PIN code:"));
0088         ui->pin2Label->setText(i18n("Re-enter new PIN code:"));
0089         ui->chkShowPass->setText(i18n("Show PIN/PUK code"));
0090 
0091         ui->puk->setFocus();
0092         ui->pukLabel->show();
0093         ui->puk->show();
0094         ui->pin2Label->show();
0095         ui->pin2->show();
0096     } else if (isPinDialog()) {
0097         QString pinType;
0098         if (m_type == PinDialog::SimPin) {
0099             pinType = i18n("SIM PIN");
0100         } else if (m_type == PinDialog::SimPin2) {
0101             pinType = i18n("SIM PIN2");
0102         } else if (m_type == PinDialog::ModemServiceProviderPin) {
0103             pinType = i18n("Service provider PIN");
0104         } else if (m_type == PinDialog::ModemNetworkPin) {
0105             pinType = i18n("Network PIN");
0106         } else if (m_type == PinDialog::ModemPin) {
0107             pinType = i18n("PIN");
0108         } else if (m_type == PinDialog::ModemCorporatePin) {
0109             pinType = i18n("Corporate PIN");
0110         } else if (m_type == PinDialog::ModemPhFsimPin) {
0111             pinType = i18n("PH-FSIM PIN");
0112         } else {
0113             pinType = i18n("Network Subset PIN");
0114         }
0115         setWindowTitle(i18n("%1 unlock required", pinType));
0116         ui->title->setText(i18n("%1 Unlock Required", pinType));
0117         ui->prompt->setText(i18n("The mobile broadband device '%1' requires a %2 code before it can be used.", m_name, pinType));
0118         ui->pinLabel->setText(i18n("%1 code:", pinType));
0119         ui->chkShowPass->setText(i18n("Show PIN code"));
0120 
0121         ui->pin->setFocus();
0122         ui->pukLabel->hide();
0123         ui->puk->hide();
0124         ui->pin2Label->hide();
0125         ui->pin2->hide();
0126     }
0127 
0128     ui->puk->clear();
0129     ui->pin->clear();
0130     ui->pin2->clear();
0131     ui->puk->setCursorPosition(0);
0132     ui->pin->setCursorPosition(0);
0133     ui->pin2->setCursorPosition(0);
0134 
0135     if (KWindowSystem::isPlatformX11()) {
0136         KX11Extras::setState(winId(), NET::KeepAbove);
0137     }
0138 
0139     move((desktop.width() - width()) / 2, (desktop.height() - height()) / 2);
0140     connect(ui->chkShowPass, &QCheckBox::toggled, this, &PinDialog::chkShowPassToggled);
0141     connect(ModemManager::notifier(), &ModemManager::Notifier::modemRemoved, this, &PinDialog::modemRemoved);
0142 }
0143 
0144 PinDialog::~PinDialog()
0145 {
0146     delete ui;
0147 }
0148 
0149 void PinDialog::chkShowPassToggled(bool on)
0150 {
0151     ui->pin->setEchoMode(!on ? QLineEdit::Password : QLineEdit::Normal);
0152     ui->pin2->setEchoMode(!on ? QLineEdit::Password : QLineEdit::Normal);
0153     ui->puk->setEchoMode(!on ? QLineEdit::Password : QLineEdit::Normal);
0154 
0155     ui->puk->setCursorPosition(0);
0156     ui->pin->setCursorPosition(0);
0157     ui->pin2->setCursorPosition(0);
0158 
0159     if (isPukDialog()) {
0160         ui->puk->setFocus();
0161     } else {
0162         ui->pin->setFocus();
0163     }
0164 }
0165 
0166 void PinDialog::modemRemoved(const QString &udi)
0167 {
0168     if (udi == m_udi) {
0169         reject();
0170     }
0171 }
0172 
0173 PinDialog::Type PinDialog::type() const
0174 {
0175     return m_type;
0176 }
0177 
0178 QString PinDialog::pin() const
0179 {
0180     return ui->pin->text();
0181 }
0182 
0183 QString PinDialog::pin2() const
0184 {
0185     return ui->pin2->text();
0186 }
0187 
0188 QString PinDialog::puk() const
0189 {
0190     return ui->puk->text();
0191 }
0192 
0193 void PinDialog::showErrorMessage(const PinDialog::ErrorCode error)
0194 {
0195     QString msg;
0196     QFont bold = font();
0197     ui->pinLabel->setFont(bold);
0198     ui->pin2Label->setFont(bold);
0199     ui->pukLabel->setFont(bold);
0200     bold.setBold(true);
0201     switch (error) {
0202     case PinCodeTooShort:
0203         msg = i18n("PIN code too short. It should be at least 4 digits.");
0204         ui->pin->setFocus();
0205         ui->pinLabel->setFont(bold);
0206         break;
0207     case PinCodesDoNotMatch:
0208         msg = i18n("The two PIN codes do not match");
0209         ui->pin2->setFocus();
0210         ui->pin2Label->setFont(bold);
0211         break;
0212     case PukCodeTooShort:
0213         msg = i18n("PUK code too short. It should be 8 digits.");
0214         ui->puk->setFocus();
0215         ui->pukLabel->setFont(bold);
0216         break;
0217     default:
0218         msg = i18n("Unknown Error");
0219     }
0220     ui->errorMessage->setText(msg, KTitleWidget::ErrorMessage);
0221     adjustSize();
0222 }
0223 
0224 void PinDialog::accept()
0225 {
0226     if (isPukDialog()) {
0227         if (pin() != pin2()) {
0228             showErrorMessage(PinCodesDoNotMatch);
0229             return;
0230         } else if (puk().length() < 8) {
0231             showErrorMessage(PukCodeTooShort);
0232             return;
0233         }
0234     }
0235 
0236     if (pin().length() < 4) {
0237         showErrorMessage(PinCodeTooShort);
0238         return;
0239     }
0240 
0241     QDialog::accept();
0242 }
0243 
0244 bool PinDialog::isPinDialog() const
0245 {
0246     return (m_type == PinDialog::SimPin || //
0247             m_type == PinDialog::SimPin2 || //
0248             m_type == PinDialog::ModemServiceProviderPin || //
0249             m_type == PinDialog::ModemNetworkPin || //
0250             m_type == PinDialog::ModemPin || //
0251             m_type == PinDialog::ModemCorporatePin || //
0252             m_type == PinDialog::ModemPhFsimPin || //
0253             m_type == PinDialog::ModemNetworkSubsetPin);
0254 }
0255 
0256 bool PinDialog::isPukDialog() const
0257 {
0258     return !isPinDialog();
0259 }
0260 
0261 #include "moc_pindialog.cpp"