File indexing completed on 2024-06-16 04:56:16

0001 /*  view/nullpinwidget.cpp
0002 
0003     This file is part of Kleopatra, the KDE keymanager
0004     SPDX-FileCopyrightText: 2017 Intevation GmbH
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "nullpinwidget.h"
0010 
0011 #include "kleopatra_debug.h"
0012 
0013 #include "smartcard/netkeycard.h"
0014 
0015 #include "commands/changepincommand.h"
0016 
0017 #include <QHBoxLayout>
0018 #include <QLabel>
0019 #include <QPushButton>
0020 #include <QVBoxLayout>
0021 
0022 #include <KLocalizedString>
0023 #include <KMessageBox>
0024 
0025 using namespace Kleo;
0026 using namespace Kleo::Commands;
0027 using namespace Kleo::SmartCard;
0028 
0029 NullPinWidget::NullPinWidget(QWidget *parent)
0030     : QWidget(parent)
0031 {
0032     const auto nullTitle = i18nc(
0033         "NullPIN is a word that is used all over in the netkey "
0034         "documentation and should be understandable by Netkey cardholders",
0035         "The NullPIN is still active on this card.");
0036     const auto nullDescription = i18n("You need to set a PIN before you can use the certificates.");
0037     const auto descriptionLbl = new QLabel(QStringLiteral("<b>%1</b><br/>%2").arg(nullTitle, nullDescription));
0038 
0039     auto vLay = new QVBoxLayout(this);
0040     vLay->addWidget(descriptionLbl, 0, Qt::AlignCenter);
0041 
0042     mNKSBtn = new QPushButton(i18nc("NKS is an identifier for a type of keys on a NetKey card", "Set NKS PIN"));
0043     mSigGBtn = new QPushButton(i18nc("SigG is an identifier for a type of keys on a NetKey card", "Set SigG PIN"));
0044 
0045     connect(mNKSBtn, &QPushButton::clicked, this, [this]() {
0046         doChangePin(NetKeyCard::nksPinKeyRef());
0047     });
0048     connect(mSigGBtn, &QPushButton::clicked, this, [this]() {
0049         doChangePin(NetKeyCard::sigGPinKeyRef());
0050     });
0051 
0052     auto hLayBtn = new QHBoxLayout;
0053     hLayBtn->addStretch(1);
0054     hLayBtn->addWidget(mNKSBtn);
0055     hLayBtn->addWidget(mSigGBtn);
0056     hLayBtn->addStretch(1);
0057 
0058     vLay->addLayout(hLayBtn);
0059 }
0060 
0061 void NullPinWidget::setSerialNumber(const std::string &serialNumber)
0062 {
0063     mSerialNumber = serialNumber;
0064 }
0065 
0066 void NullPinWidget::doChangePin(const std::string &keyRef)
0067 {
0068     parentWidget()->setEnabled(false);
0069     auto ret = KMessageBox::warningContinueCancel(this,
0070                                                   i18n("Setting a PIN is required but <b>can't be reverted</b>.")
0071                                                       + QStringLiteral("<p>%1</p><p>%2</p>")
0072                                                             .arg(i18n("If you proceed you will be asked to enter a new PIN "
0073                                                                       "and later to repeat that PIN."))
0074                                                             .arg(i18n("It will <b>not be possible</b> to recover the "
0075                                                                       "card if the PIN has been entered wrongly more than 2 times.")),
0076                                                   i18nc("@title:window", "Set initial PIN"),
0077                                                   KStandardGuiItem::cont(),
0078                                                   KStandardGuiItem::cancel());
0079 
0080     if (ret != KMessageBox::Continue) {
0081         parentWidget()->setEnabled(true);
0082         return;
0083     }
0084 
0085     auto cmd = new ChangePinCommand(mSerialNumber, NetKeyCard::AppName, this);
0086     connect(cmd, &ChangePinCommand::finished, this, [this]() {
0087         this->parentWidget()->setEnabled(true);
0088     });
0089     cmd->setKeyRef(keyRef);
0090     cmd->setMode(ChangePinCommand::NullPinMode);
0091     cmd->start();
0092 }
0093 
0094 void NullPinWidget::setSigGVisible(bool val)
0095 {
0096     mSigGBtn->setVisible(val);
0097 }
0098 
0099 void NullPinWidget::setNKSVisible(bool val)
0100 {
0101     mNKSBtn->setVisible(val);
0102 }
0103 
0104 #include "moc_nullpinwidget.cpp"