Commit 32c6f48b authored by Ingo Klöcker's avatar Ingo Klöcker

Derive all card commands from CardCommand and CardCommand from Command

Command::Private:
* Add two constructors for commands that are independent of
  KeyListController, but that may set a parent widget (e.g. for dialogs
  shown by the commands)
* Simplify member initialization

CardCommand:
* Remove methods initially duplicated from Command
* Remove empty default implementation of doCancel()

AuthenticatePIVCardApplicationCommand, ChangePinCommand,
PIVGenerateCardKeyCommand, SetPIVCardApplicationAdministrationKeyCommand:
* Implement doCancel()
* Adjust some calls of methods of super class

CertificateToPIVCardCommand, KeyToCardCommand:
* Derive from CardCommand instead of Command
* Remove serialNumber member, get serial number from super class

ImportCertificateFromDataCommand:
* Add mId member to allow overriding the hard-coded id ("Notepad")

ImportCertificateFromPIVCardCommand:
* Derive from CardCommand instead of ImportCertificatesCommand
* Use ImportCertificateFromDataCommand internally
* Implement doCancel()

GnuPG-bug-id: 5066
parent b1fd4390
......@@ -106,6 +106,10 @@ void AuthenticatePIVCardApplicationCommand::doStart()
d->authenticate(QByteArray::fromHex("010203040506070801020304050607080102030405060708"));
}
void AuthenticatePIVCardApplicationCommand::doCancel()
{
}
void AuthenticatePIVCardApplicationCommand::Private::authenticate(const QByteArray& adminKey)
{
qCDebug(KLEOPATRA_LOG) << "AuthenticatePIVCardApplicationCommand::authenticate()";
......@@ -147,7 +151,7 @@ void AuthenticatePIVCardApplicationCommand::Private::ensureDialogCreated()
return;
}
dialog = new PIVCardApplicationAdministrationKeyInputDialog(parentWidget());
dialog = new PIVCardApplicationAdministrationKeyInputDialog(parentWidgetOrView());
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setLabelText(prompt.isEmpty() ?
i18n("Please enter the PIV Card Application Administration Key in hex-encoded form.") :
......
......@@ -32,6 +32,7 @@ public:
private:
void doStart() override;
void doCancel() override;
private:
class Private;
......
......@@ -15,53 +15,41 @@
using namespace Kleo;
CardCommand::Private::Private(CardCommand *qq, const std::string &serialNumber, QWidget *parent)
: q(qq),
autoDelete(true),
serialNumber_(serialNumber),
parentWidget_(parent)
CardCommand::Private *CardCommand::d_func()
{
return static_cast<Private *>(d.get());
}
CardCommand::Private::~Private()
const CardCommand::Private *CardCommand::d_func() const
{
return static_cast<const Private *>(d.get());
}
CardCommand::CardCommand(const std::string &serialNumber, QWidget *parent)
: QObject(parent), d(new Private(this, serialNumber, parent))
{
}
CardCommand::CardCommand(Private *pp)
: QObject(pp->parentWidget_), d(pp)
{
}
#define d d_func()
#define q q_func()
CardCommand::~CardCommand()
CardCommand::Private::Private(CardCommand *qq, const std::string &serialNumber, QWidget *parent)
: Command::Private(qq, parent)
, serialNumber_(serialNumber)
{
}
void CardCommand::setAutoDelete(bool on)
CardCommand::Private::~Private()
{
d->autoDelete = on;
}
bool CardCommand::autoDelete() const
CardCommand::CardCommand(const std::string &serialNumber, QWidget *parent)
: Command(new Private(this, serialNumber, parent))
{
return d->autoDelete;
}
void CardCommand::start()
CardCommand::CardCommand(Private *pp)
: Command(pp)
{
doStart();
}
void CardCommand::cancel()
CardCommand::~CardCommand()
{
doCancel();
Q_EMIT canceled();
}
void CardCommand::doCancel()
{
}
#undef q_func
#undef d_func
......@@ -11,40 +11,23 @@
#ifndef __KLEOPATRA_COMMANDS_CARDCOMMAND_H__
#define __KLEOPATRA_COMMANDS_CARDCOMMAND_H__
#include <QObject>
class QWidget;
#include <utils/pimpl_ptr.h>
#include "command.h"
namespace Kleo
{
class CardCommand : public QObject
class CardCommand : public Command
{
Q_OBJECT
public:
explicit CardCommand(const std::string &serialNumber, QWidget *parent);
~CardCommand() override;
void setAutoDelete(bool on);
bool autoDelete() const;
public Q_SLOTS:
void start();
void cancel();
Q_SIGNALS:
void finished();
void canceled();
private:
virtual void doStart() = 0;
virtual void doCancel();
protected:
class Private;
kdtools::pimpl_ptr<Private> d;
inline Private *d_func();
inline const Private *d_func() const;
protected:
explicit CardCommand(Private *pp);
};
......
......@@ -12,58 +12,26 @@
#define __KLEOPATRA_COMMANDS_CARDCOMMAND_P_H__
#include "cardcommand.h"
#include "command_p.h"
#include <QPointer>
#include <KMessageBox>
class Kleo::CardCommand::Private
class Kleo::CardCommand::Private : public Command::Private
{
friend class ::Kleo::CardCommand;
protected:
CardCommand *const q;
Kleo::CardCommand *q_func() const
{
return static_cast<Kleo::CardCommand *>(q);
}
public:
explicit Private(CardCommand *qq, const std::string &serialNumber, QWidget *parent);
virtual ~Private();
~Private();
std::string serialNumber() const
{
return serialNumber_;
}
QWidget *parentWidget() const
{
return parentWidget_;
}
void finished()
{
Q_EMIT q->finished();
if (autoDelete) {
q->deleteLater();
}
}
void canceled()
{
Q_EMIT q->canceled();
finished();
}
void error(const QString &text, const QString &caption = QString(), KMessageBox::Options options = KMessageBox::Notify) const
{
KMessageBox::error(parentWidget(), text, caption, options);
}
void information(const QString &text, const QString &caption = QString(), const QString &dontShowAgainName = QString(), KMessageBox::Options options = KMessageBox::Notify) const
{
KMessageBox::information(parentWidget(), text, caption, dontShowAgainName, options);
}
private:
bool autoDelete : 1;
std::string serialNumber_;
QPointer<QWidget> parentWidget_;
};
#endif /* __KLEOPATRA_COMMANDS_CARDCOMMAND_P_H__ */
......@@ -11,7 +11,7 @@
#include "certificatetopivcardcommand.h"
#include "command_p.h"
#include "cardcommand_p.h"
#include "commands/authenticatepivcardapplicationcommand.h"
......@@ -37,7 +37,7 @@ using namespace Kleo::Commands;
using namespace Kleo::SmartCard;
using namespace GpgME;
class CertificateToPIVCardCommand::Private : public Command::Private
class CertificateToPIVCardCommand::Private : public CardCommand::Private
{
friend class ::Kleo::Commands::CertificateToPIVCardCommand;
CertificateToPIVCardCommand *q_func() const
......@@ -57,7 +57,6 @@ private:
void authenticationCanceled();
private:
std::string serialNumber;
std::string cardSlot;
Key certificate;
bool hasBeenCanceled = false;
......@@ -77,8 +76,7 @@ const CertificateToPIVCardCommand::Private *CertificateToPIVCardCommand::d_func(
CertificateToPIVCardCommand::Private::Private(CertificateToPIVCardCommand *qq, const std::string &slot, const std::string &serialno)
: Command::Private(qq, nullptr)
, serialNumber(serialno)
: CardCommand::Private(qq, serialno, nullptr)
, cardSlot(slot)
{
}
......@@ -112,9 +110,9 @@ void CertificateToPIVCardCommand::Private::start()
{
qCDebug(KLEOPATRA_LOG) << "CertificateToPIVCardCommand::Private::start()";
const auto pivCard = SmartCard::ReaderStatus::instance()->getCard<PIVCard>(serialNumber);
const auto pivCard = SmartCard::ReaderStatus::instance()->getCard<PIVCard>(serialNumber());
if (!pivCard) {
error(i18n("Failed to find the PIV card with the serial number: %1", QString::fromStdString(serialNumber)));
error(i18n("Failed to find the PIV card with the serial number: %1", QString::fromStdString(serialNumber())));
finished();
return;
}
......@@ -134,7 +132,7 @@ void CertificateToPIVCardCommand::Private::start()
"@info %1 name of card slot, %2 serial number of card",
"<p>Please confirm that you want to write the following certificate to the %1 slot of card %2:</p>"
"<center>%3</center>",
PIVCard::keyDisplayName(cardSlot), QString::fromStdString(serialNumber), certificateInfo);
PIVCard::keyDisplayName(cardSlot), QString::fromStdString(serialNumber()), certificateInfo);
auto confirmButton = KStandardGuiItem::yes();
confirmButton.setText(i18nc("@action:button", "Write certificate"));
confirmButton.setToolTip(QString());
......@@ -175,7 +173,7 @@ void CertificateToPIVCardCommand::Private::authenticate()
{
qCDebug(KLEOPATRA_LOG) << "CertificateToPIVCardCommand::authenticate()";
auto cmd = new AuthenticatePIVCardApplicationCommand(serialNumber, parentWidgetOrView());
auto cmd = new AuthenticatePIVCardApplicationCommand(serialNumber(), parentWidgetOrView());
connect(cmd, &AuthenticatePIVCardApplicationCommand::finished,
q, [this]() { authenticationFinished(); });
connect(cmd, &AuthenticatePIVCardApplicationCommand::canceled,
......@@ -199,7 +197,7 @@ void CertificateToPIVCardCommand::Private::authenticationCanceled()
}
CertificateToPIVCardCommand::CertificateToPIVCardCommand(const std::string& cardSlot, const std::string &serialno)
: Command(new Private(this, cardSlot, serialno))
: CardCommand(new Private(this, cardSlot, serialno))
{
}
......
......@@ -10,7 +10,7 @@
#ifndef __KLEOPATRA_COMMANDS_CERTIFICATETOPIVCARDCOMMAND_H__
#define __KLEOPATRA_COMMANDS_CERTIFICATETOPIVCARDCOMMAND_H__
#include <commands/command.h>
#include <commands/cardcommand.h>
namespace GpgME
{
......@@ -22,7 +22,7 @@ namespace Kleo
namespace Commands
{
class CertificateToPIVCardCommand : public Command
class CertificateToPIVCardCommand : public CardCommand
{
Q_OBJECT
public:
......
......@@ -98,6 +98,10 @@ void ChangePinCommand::doStart()
d->changePin();
}
void ChangePinCommand::doCancel()
{
}
void ChangePinCommand::Private::changePin()
{
qCDebug(KLEOPATRA_LOG) << "ChangePinCommand::changePin()";
......
......@@ -32,6 +32,7 @@ public:
private:
void doStart() override;
void doCancel() override;
private:
class Private;
......
......@@ -35,16 +35,27 @@ using namespace Kleo;
using namespace Kleo::Commands;
using namespace GpgME;
Command::Private::Private(Command *qq)
: q(qq),
autoDelete(true),
warnWhenRunningAtShutdown(true)
{
}
Command::Private::Private(Command *qq, KeyListController *controller)
: q(qq),
autoDelete(true),
warnWhenRunningAtShutdown(true),
indexes_(),
view_(),
parentWId(0),
controller_(controller)
{
}
Command::Private::Private(Command *qq, QWidget *parent)
: q(qq),
autoDelete(true),
warnWhenRunningAtShutdown(true),
parentWidget_(parent)
{
}
Command::Private::~Private()
......@@ -91,13 +102,13 @@ Command::Command(QAbstractItemView *v, Private *pp)
}
Command::Command(const GpgME::Key &key)
: QObject(nullptr), d(new Private(this, nullptr))
: QObject(nullptr), d(new Private(this))
{
d->keys_ = std::vector<Key>(1, key);
}
Command::Command(const std::vector<GpgME::Key> &keys)
: QObject(nullptr), d(new Private(this, nullptr))
: QObject(nullptr), d(new Private(this))
{
d->keys_ = keys;
}
......
......@@ -33,7 +33,9 @@ class Kleo::Command::Private
protected:
Command *const q;
public:
explicit Private(Command *qq);
explicit Private(Command *qq, KeyListController *controller);
explicit Private(Command *qq, QWidget *parent);
virtual ~Private();
QAbstractItemView *view() const
......@@ -114,7 +116,7 @@ private:
QList<QPersistentModelIndex> indexes_;
QPointer<QAbstractItemView> view_;
QPointer<QWidget> parentWidget_;
WId parentWId;
WId parentWId = 0;
QPointer<KeyListController> controller_;
};
......
......@@ -29,12 +29,13 @@ class ImportCertificateFromDataCommand::Private : public ImportCertificatesComma
}
public:
explicit Private(ImportCertificateFromDataCommand *qq,
const QByteArray &data, GpgME::Protocol proto);
const QByteArray &data, GpgME::Protocol proto, const QString &id);
~Private();
private:
QByteArray mData;
GpgME::Protocol mProto;
QString mId;
};
ImportCertificateFromDataCommand::Private *ImportCertificateFromDataCommand::d_func()
......@@ -48,8 +49,9 @@ const ImportCertificateFromDataCommand::Private *ImportCertificateFromDataComman
ImportCertificateFromDataCommand::Private::Private(ImportCertificateFromDataCommand *qq,
const QByteArray &data,
GpgME::Protocol proto)
: ImportCertificatesCommand::Private(qq, nullptr), mData(data), mProto(proto)
GpgME::Protocol proto,
const QString &id)
: ImportCertificatesCommand::Private(qq, nullptr), mData(data), mProto(proto), mId(id)
{
}
......@@ -59,8 +61,9 @@ ImportCertificateFromDataCommand::Private::~Private() {}
#define q q_func()
ImportCertificateFromDataCommand::ImportCertificateFromDataCommand(const QByteArray &data,
GpgME::Protocol proto)
: ImportCertificatesCommand(new Private(this, data, proto))
GpgME::Protocol proto,
const QString &id)
: ImportCertificatesCommand(new Private(this, data, proto, id))
{
}
......@@ -68,7 +71,7 @@ ImportCertificateFromDataCommand::~ImportCertificateFromDataCommand() {}
void ImportCertificateFromDataCommand::doStart()
{
d->startImport(d->mProto, d->mData, i18n("Notepad"));
d->startImport(d->mProto, d->mData, d->mId.isEmpty() ? i18n("Notepad") : d->mId);
}
#undef d
......
......@@ -23,7 +23,8 @@ class ImportCertificateFromDataCommand : public ImportCertificatesCommand
Q_OBJECT
public:
explicit ImportCertificateFromDataCommand(const QByteArray &data,
GpgME::Protocol proto);
GpgME::Protocol proto,
const QString &id = QString());
~ImportCertificateFromDataCommand() override;
private:
......
......@@ -9,11 +9,13 @@
#include "importcertificatefrompivcardcommand.h"
#include "importcertificatescommand_p.h"
#include "cardcommand_p.h"
#include "smartcard/pivcard.h"
#include "smartcard/readerstatus.h"
#include "commands/importcertificatefromdatacommand.h"
#include <KLocalizedString>
#include "kleopatra_debug.h"
......@@ -22,7 +24,7 @@ using namespace Kleo;
using namespace Kleo::Commands;
using namespace Kleo::SmartCard;
class ImportCertificateFromPIVCardCommand::Private : public ImportCertificatesCommand::Private
class ImportCertificateFromPIVCardCommand::Private : public CardCommand::Private
{
friend class ::Kleo::Commands::ImportCertificateFromPIVCardCommand;
ImportCertificateFromPIVCardCommand *q_func() const
......@@ -36,10 +38,12 @@ public:
private:
void start();
void importFinished();
void importCanceled();
private:
std::string serialNumber;
std::string cardSlot;
bool hasBeenCanceled = false;
};
ImportCertificateFromPIVCardCommand::Private *ImportCertificateFromPIVCardCommand::d_func()
......@@ -57,8 +61,7 @@ const ImportCertificateFromPIVCardCommand::Private *ImportCertificateFromPIVCard
ImportCertificateFromPIVCardCommand::Private::Private(ImportCertificateFromPIVCardCommand *qq,
const std::string &slot, const std::string &serialno)
: ImportCertificatesCommand::Private(qq, nullptr)
, serialNumber(serialno)
: CardCommand::Private(qq, serialno, nullptr)
, cardSlot(slot)
{
}
......@@ -71,9 +74,9 @@ void ImportCertificateFromPIVCardCommand::Private::start()
{
qCDebug(KLEOPATRA_LOG) << "ImportCertificateFromPIVCardCommand::Private::start()";
const auto pivCard = ReaderStatus::instance()->getCard<PIVCard>(serialNumber);
const auto pivCard = ReaderStatus::instance()->getCard<PIVCard>(serialNumber());
if (!pivCard) {
error(i18n("Failed to find the PIV card with the serial number: %1", QString::fromStdString(serialNumber)));
error(i18n("Failed to find the PIV card with the serial number: %1", QString::fromStdString(serialNumber())));
finished();
return;
}
......@@ -85,11 +88,31 @@ void ImportCertificateFromPIVCardCommand::Private::start()
return;
}
startImport(GpgME::CMS, QByteArray::fromStdString(certificateData), i18n("Card Certificate"));
auto cmd = new ImportCertificateFromDataCommand(QByteArray::fromStdString(certificateData), GpgME::CMS, i18n("Card Certificate"));
connect(cmd, &ImportCertificateFromDataCommand::finished,
q, [this]() { importFinished(); });
connect(cmd, &ImportCertificateFromDataCommand::canceled,
q, [this]() { importCanceled(); });
cmd->start();
}
void ImportCertificateFromPIVCardCommand::Private::importFinished()
{
qCDebug(KLEOPATRA_LOG) << "ImportCertificateFromPIVCardCommand::importFinished()";
if (!hasBeenCanceled) {
finished();
}
}
void ImportCertificateFromPIVCardCommand::Private::importCanceled()
{
qCDebug(KLEOPATRA_LOG) << "ImportCertificateFromPIVCardCommand::importCanceled()";
hasBeenCanceled = true;
canceled();
}
ImportCertificateFromPIVCardCommand::ImportCertificateFromPIVCardCommand(const std::string& cardSlot, const std::string &serialno)
: ImportCertificatesCommand(new Private(this, cardSlot, serialno))
: CardCommand(new Private(this, cardSlot, serialno))
{
}
......@@ -105,5 +128,9 @@ void ImportCertificateFromPIVCardCommand::doStart()
d->start();
}
void ImportCertificateFromPIVCardCommand::doCancel()
{
}
#undef q_func
#undef d_func
......@@ -10,14 +10,14 @@
#ifndef KLEOPATRA_COMMANDS_IMPORTCERTIFICATEFROMPIVCARDCOMMAND_H
#define KLEOPATRA_COMMANDS_IMPORTCERTIFICATEFROMPIVCARDCOMMAND_H
#include "importcertificatescommand.h"
#include "cardcommand.h"
namespace Kleo
{
namespace Commands
{
class ImportCertificateFromPIVCardCommand : public ImportCertificatesCommand
class ImportCertificateFromPIVCardCommand : public CardCommand
{
Q_OBJECT
public:
......@@ -26,6 +26,7 @@ public:
private:
void doStart() override;
void doCancel() override;
private:
class Private;
......
......@@ -13,7 +13,7 @@
#include "keytocardcommand.h"
#include "command_p.h"
#include "cardcommand_p.h"
#include "commands/authenticatepivcardapplicationcommand.h"
......@@ -42,7 +42,7 @@ using namespace Kleo::Dialogs;
using namespace Kleo::SmartCard;
using namespace GpgME;
class KeyToCardCommand::Private : public Command::Private
class KeyToCardCommand::Private : public CardCommand::Private
{
friend class ::Kleo::Commands::KeyToCardCommand;
KeyToCardCommand *q_func() const
......@@ -67,7 +67,6 @@ private:
void authenticationCanceled();
private:
std::string serialNumber;
GpgME::Subkey subkey;
std::string cardSlot;
bool overwriteExistingAlreadyApproved = false;
......@@ -90,15 +89,13 @@ const KeyToCardCommand::Private *KeyToCardCommand::d_func() const
KeyToCardCommand::Private::Private(KeyToCardCommand *qq,
const GpgME::Subkey &subkey_,
const std::string &serialno)
: Command::Private(qq, nullptr),
serialNumber(serialno),
: CardCommand::Private(qq, serialno, nullptr),
subkey(subkey_)
{
}
KeyToCardCommand::Private::