diff --git a/src/view/netkeywidget.cpp b/src/view/netkeywidget.cpp index 3f54e698d..767dcb94c 100644 --- a/src/view/netkeywidget.cpp +++ b/src/view/netkeywidget.cpp @@ -1,229 +1,229 @@ /* view/netkeywidget.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2017 Intevation GmbH SPDX-License-Identifier: GPL-2.0-or-later */ #include "netkeywidget.h" #include "nullpinwidget.h" #include "keytreeview.h" #include "kleopatraapplication.h" #include "systrayicon.h" #include "kleopatra_debug.h" #include "smartcard/netkeycard.h" #include "smartcard/readerstatus.h" #include "commands/changepincommand.h" #include "commands/createopenpgpkeyfromcardkeyscommand.h" #include "commands/learncardkeyscommand.h" #include "commands/detailscommand.h" #include #include #include #include #include #include #include #include #include using namespace Kleo; using namespace Kleo::SmartCard; using namespace Kleo::Commands; NetKeyWidget::NetKeyWidget(QWidget *parent) : QWidget(parent), mSerialNumberLabel(new QLabel(this)), mVersionLabel(new QLabel(this)), mLearnKeysLabel(new QLabel(this)), mErrorLabel(new QLabel(this)), - mNullPinWidget(new NullPinWidget()), + mNullPinWidget(new NullPinWidget(this)), mLearnKeysBtn(new QPushButton(this)), mChangeNKSPINBtn(new QPushButton(this)), mChangeSigGPINBtn(new QPushButton(this)), mTreeView(new KeyTreeView(this)), mArea(new QScrollArea) { auto vLay = new QVBoxLayout; // Set up the scroll are mArea->setFrameShape(QFrame::NoFrame); mArea->setWidgetResizable(true); auto mAreaWidget = new QWidget; mAreaWidget->setLayout(vLay); mArea->setWidget(mAreaWidget); auto scrollLay = new QVBoxLayout(this); scrollLay->setContentsMargins(0, 0, 0, 0); scrollLay->addWidget(mArea); // Add general widgets mVersionLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); vLay->addWidget(mVersionLabel, 0, Qt::AlignLeft); mSerialNumberLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); auto hLay1 = new QHBoxLayout; hLay1->addWidget(new QLabel(i18n("Serial number:"))); hLay1->addWidget(mSerialNumberLabel); hLay1->addStretch(1); vLay->addLayout(hLay1); vLay->addWidget(mNullPinWidget); auto line1 = new QFrame(); line1->setFrameShape(QFrame::HLine); vLay->addWidget(line1); vLay->addWidget(new QLabel(QStringLiteral("%1").arg(i18n("Certificates:"))), 0, Qt::AlignLeft); mLearnKeysLabel = new QLabel(i18n("There are unknown certificates on this card.")); mLearnKeysBtn->setText(i18nc("@action", "Load Certificates")); connect(mLearnKeysBtn, &QPushButton::clicked, this, [this] () { mLearnKeysBtn->setEnabled(false); auto cmd = new LearnCardKeysCommand(GpgME::CMS); cmd->setParentWidget(this); cmd->start(); auto icon = KleopatraApplication::instance()->sysTrayIcon(); if (icon) { icon->setLearningInProgress(true); } connect(cmd, &Command::finished, this, [icon] () { ReaderStatus::mutableInstance()->updateStatus(); icon->setLearningInProgress(false); }); }); auto hLay2 = new QHBoxLayout; hLay2->addWidget(mLearnKeysLabel); hLay2->addWidget(mLearnKeysBtn); hLay2->addStretch(1); vLay->addLayout(hLay2); mErrorLabel->setVisible(false); vLay->addWidget(mErrorLabel); // The certificate view mTreeView->setHierarchicalModel(AbstractKeyListModel::createHierarchicalKeyListModel(mTreeView)); mTreeView->setHierarchicalView(true); connect(mTreeView->view(), &QAbstractItemView::doubleClicked, this, [this] (const QModelIndex &idx) { const auto klm = dynamic_cast (mTreeView->view()->model()); if (!klm) { qCDebug(KLEOPATRA_LOG) << "Unhandled Model: " << mTreeView->view()->model()->metaObject()->className(); return; } auto cmd = new DetailsCommand(klm->key(idx), nullptr); cmd->setParentWidget(this); cmd->start(); }); vLay->addWidget(mTreeView); // The action area auto line2 = new QFrame(); line2->setFrameShape(QFrame::HLine); vLay->addWidget(line2); vLay->addWidget(new QLabel(QStringLiteral("%1").arg(i18n("Actions:"))), 0, Qt::AlignLeft); auto actionLayout = new QHBoxLayout(); if (CreateOpenPGPKeyFromCardKeysCommand::isSupported()) { mKeyForCardKeysButton = new QPushButton(this); mKeyForCardKeysButton->setText(i18nc("@action:button", "Create OpenPGP Key")); mKeyForCardKeysButton->setToolTip(i18nc("@info:tooltip", "Create an OpenPGP key for the keys stored on the card.")); actionLayout->addWidget(mKeyForCardKeysButton); connect(mKeyForCardKeysButton, &QPushButton::clicked, this, &NetKeyWidget::createKeyFromCardKeys); } mChangeNKSPINBtn->setText(i18nc("NKS is an identifier for a type of keys on a NetKey card", "Change NKS PIN")); mChangeSigGPINBtn->setText(i18nc("SigG is an identifier for a type of keys on a NetKey card", "Change SigG PIN")); connect(mChangeNKSPINBtn, &QPushButton::clicked, this, [this] () { doChangePin(NetKeyCard::nksPinKeyRef()); }); connect(mChangeSigGPINBtn, &QPushButton::clicked, this, [this] () { doChangePin(NetKeyCard::sigGPinKeyRef()); }); actionLayout->addWidget(mChangeNKSPINBtn); actionLayout->addWidget(mChangeSigGPINBtn); actionLayout->addStretch(1); vLay->addLayout(actionLayout); vLay->addStretch(1); } NetKeyWidget::~NetKeyWidget() { } void NetKeyWidget::setCard(const NetKeyCard* card) { mSerialNumber = card->serialNumber(); mVersionLabel->setText(i18nc("1 is a Version number", "NetKey v%1 Card", card->appVersion())); mSerialNumberLabel->setText(QString::fromStdString(mSerialNumber)); mNullPinWidget->setSerialNumber(mSerialNumber); /* According to users of NetKey Cards it is fairly uncommon * to use SigG Certificates at all. So it should be optional to set the pins. */ mNullPinWidget->setVisible(card->hasNKSNullPin() /*|| card->hasSigGNullPin()*/); mNullPinWidget->setSigGVisible(false/*card->hasSigGNullPin()*/); mNullPinWidget->setNKSVisible(card->hasNKSNullPin()); mChangeNKSPINBtn->setEnabled(!card->hasNKSNullPin()); if (card->hasSigGNullPin()) { mChangeSigGPINBtn->setText(i18nc("SigG is an identifier for a type of keys on a NetKey card", "Set SigG PIN")); } else { mChangeSigGPINBtn->setText(i18nc("SigG is an identifier for a type of keys on a NetKey card", "Change SigG PIN")); } mLearnKeysBtn->setEnabled(true); mLearnKeysBtn->setVisible(card->canLearnKeys()); mTreeView->setVisible(!card->canLearnKeys()); mLearnKeysLabel->setVisible(card->canLearnKeys()); const auto errMsg = card->errorMsg(); if (!errMsg.isEmpty()) { mErrorLabel->setText(QStringLiteral("%1: %2").arg(i18n("Error"), errMsg)); mErrorLabel->setVisible(true); } else { mErrorLabel->setVisible(false); } const auto keys = card->keys(); mTreeView->setKeys(keys); if (mKeyForCardKeysButton) { mKeyForCardKeysButton->setEnabled(!card->hasNKSNullPin() && card->hasSigningKey() && card->hasEncryptionKey()); } } void NetKeyWidget::doChangePin(const std::string &keyRef) { auto cmd = new ChangePinCommand(mSerialNumber, NetKeyCard::AppName, this); this->setEnabled(false); connect(cmd, &ChangePinCommand::finished, this, [this]() { this->setEnabled(true); }); cmd->setKeyRef(keyRef); cmd->start(); } void NetKeyWidget::createKeyFromCardKeys() { auto cmd = new CreateOpenPGPKeyFromCardKeysCommand(mSerialNumber, NetKeyCard::AppName, this); this->setEnabled(false); connect(cmd, &CreateOpenPGPKeyFromCardKeysCommand::finished, this, [this]() { this->setEnabled(true); }); cmd->start(); } diff --git a/src/view/nullpinwidget.cpp b/src/view/nullpinwidget.cpp index b6e3a7a62..aa1dc1c1e 100644 --- a/src/view/nullpinwidget.cpp +++ b/src/view/nullpinwidget.cpp @@ -1,137 +1,130 @@ /* view/nullpinwidget.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2017 Intevation GmbH SPDX-License-Identifier: GPL-2.0-or-later */ #include "nullpinwidget.h" #include "kleopatra_debug.h" #include "smartcard/netkeycard.h" #include "smartcard/readerstatus.h" #include #include #include #include #include #include #include using namespace Kleo; using namespace Kleo::SmartCard; NullPinWidget::NullPinWidget(QWidget *parent) : QWidget(parent) { const auto nullTitle = i18nc("NullPIN is a word that is used all over in the netkey " "documentation and should be understandable by Netkey cardholders", "The NullPIN is still active on this card."); const auto nullDescription = i18n("You need to set a PIN before you can use the certificates."); const auto descriptionLbl = new QLabel(QStringLiteral("%1
%2").arg(nullTitle, nullDescription)); auto vLay = new QVBoxLayout(this); vLay->addWidget(descriptionLbl, 0, Qt::AlignCenter); mNKSBtn = new QPushButton(i18nc("NKS is an identifier for a type of keys on a NetKey card", "Set NKS PIN")); mSigGBtn = new QPushButton(i18nc("SigG is an identifier for a type of keys on a NetKey card", "Set SigG PIN")); connect(mNKSBtn, &QPushButton::clicked, this, [this] () { - mNKSBtn->setEnabled(false); doChangePin(false); }); connect(mSigGBtn, &QPushButton::clicked, this, [this] () { - mSigGBtn->setEnabled(false); doChangePin(true); }); auto hLayBtn = new QHBoxLayout; hLayBtn->addStretch(1); hLayBtn->addWidget(mNKSBtn); hLayBtn->addWidget(mSigGBtn); hLayBtn->addStretch(1); vLay->addLayout(hLayBtn); } void NullPinWidget::setSerialNumber(const std::string &serialNumber) { mSerialNumber = serialNumber; } void NullPinWidget::doChangePin(bool sigG) { + parentWidget()->setEnabled(false); auto ret = KMessageBox::warningContinueCancel(this, i18n("Setting a PIN is required but can't be reverted.") + QStringLiteral("

%1

%2

").arg( i18n("If you proceed you will be asked to enter a new PIN " "and later to repeat that PIN.")).arg( i18n("It will not be possible to recover the " "card if the PIN has been entered wrongly more than 2 times.")), i18n("Set initial PIN"), KStandardGuiItem::cont(), KStandardGuiItem::cancel()); if (ret != KMessageBox::Continue) { + parentWidget()->setEnabled(true); return; } const auto nksCard = ReaderStatus::instance()->getCard(mSerialNumber); if (!nksCard) { KMessageBox::error(this, i18n("Failed to find the NetKey card with the serial number: %1", QString::fromStdString(mSerialNumber))); + parentWidget()->setEnabled(true); return; } if (sigG) { ReaderStatus::mutableInstance()->startSimpleTransaction( nksCard, "SCD PASSWD --nullpin PW1.CH.SIG", this, "setSigGPinSettingResult"); } else { ReaderStatus::mutableInstance()->startSimpleTransaction( nksCard, "SCD PASSWD --nullpin PW1.CH", this, "setNksPinSettingResult"); } } -void NullPinWidget::handleResult(const GpgME::Error &err, QPushButton *btn) +void NullPinWidget::handleResult(const GpgME::Error &err) { - btn->setEnabled(true); - if (err.isCanceled()) { - return; - } if (err) { KMessageBox::error(this, i18nc("@info", "Failed to set PIN: %1", QString::fromLatin1(err.asString())), i18nc("@title", "Error")); - return; - } - btn->setVisible(false); - - if (!mNKSBtn->isVisible() && !mSigGBtn->isVisible()) { - // Both pins are set, we can hide. - setVisible(false); + } else if (!err.isCanceled()) { + ReaderStatus::mutableInstance()->updateStatus(); } + parentWidget()->setEnabled(true); } void NullPinWidget::setSigGVisible(bool val) { mSigGBtn->setVisible(val); } void NullPinWidget::setNKSVisible(bool val) { mNKSBtn->setVisible(val); } void NullPinWidget::setSigGPinSettingResult(const GpgME::Error &err) { - handleResult(err, mSigGBtn); + handleResult(err); } void NullPinWidget::setNksPinSettingResult(const GpgME::Error &err) { - handleResult(err, mNKSBtn); + handleResult(err); } diff --git a/src/view/nullpinwidget.h b/src/view/nullpinwidget.h index fa681b70d..c117963cd 100644 --- a/src/view/nullpinwidget.h +++ b/src/view/nullpinwidget.h @@ -1,49 +1,49 @@ #ifndef VIEW_NULLPINWIDGET_H #define VIEW_NULLPINWIDGET_H /* view/nullpinwidget.h This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2017 Intevation GmbH SPDX-License-Identifier: GPL-2.0-or-later */ #include class QPushButton; namespace GpgME { class Error; } // namespace GpgME namespace Kleo { class NullPinWidget: public QWidget { Q_OBJECT public: explicit NullPinWidget(QWidget *parent = nullptr); void setSerialNumber(const std::string &serialNumber); void setSigGVisible(bool val); void setNKSVisible(bool val); private: void doChangePin(bool sigG); - void handleResult(const GpgME::Error &err, QPushButton *btn); + void handleResult(const GpgME::Error &err); private Q_SLOTS: void setSigGPinSettingResult(const GpgME::Error &err); void setNksPinSettingResult(const GpgME::Error &err); private: std::string mSerialNumber; QPushButton *mNKSBtn, *mSigGBtn; }; } // namespace Kleo #endif // VIEW_NULLPINWIDGET_H