File indexing completed on 2024-03-24 17:10:48

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