diff --git a/src/commands/revokekeycommand.cpp b/src/commands/revokekeycommand.cpp index cad0b9463..3a021ed50 100644 --- a/src/commands/revokekeycommand.cpp +++ b/src/commands/revokekeycommand.cpp @@ -1,257 +1,258 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/revokekeycommand.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2022 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "revokekeycommand.h" #include "command_p.h" #include "dialogs/revokekeydialog.h" #include #include #ifdef QGPGME_SUPPORTS_KEY_REVOCATION #include #endif #include "kleopatra_debug.h" +#include using namespace Kleo; using namespace GpgME; class RevokeKeyCommand::Private : public Command::Private { friend class ::RevokeKeyCommand; RevokeKeyCommand *q_func() const { return static_cast(q); } public: explicit Private(RevokeKeyCommand *qq, KeyListController *c = nullptr); ~Private() override; void start(); void cancel(); private: void ensureDialogCreated(); void onDialogAccepted(); void onDialogRejected(); #ifdef QGPGME_SUPPORTS_KEY_REVOCATION std::unique_ptr startJob(); #endif void onJobResult(const Error &err); void showError(const Error &err); private: Key key; QPointer dialog; #ifdef QGPGME_SUPPORTS_KEY_REVOCATION QPointer job; #endif }; RevokeKeyCommand::Private *RevokeKeyCommand::d_func() { return static_cast(d.get()); } const RevokeKeyCommand::Private *RevokeKeyCommand::d_func() const { return static_cast(d.get()); } #define d d_func() #define q q_func() RevokeKeyCommand::Private::Private(RevokeKeyCommand *qq, KeyListController *c) : Command::Private{qq, c} { } RevokeKeyCommand::Private::~Private() = default; namespace { Key getKey(const std::vector &keys) { if (keys.size() != 1) { qCWarning(KLEOPATRA_LOG) << "Expected exactly one key, but got" << keys.size(); return {}; } const Key key = keys.front(); if (key.protocol() != GpgME::OpenPGP) { qCWarning(KLEOPATRA_LOG) << "Expected OpenPGP key, but got" << Formatting::displayName(key.protocol()) << "key"; return {}; } return key; } } void RevokeKeyCommand::Private::start() { key = getKey(keys()); if (key.isNull()) { finished(); return; } if (key.isRevoked()) { information(i18nc("@info", "This key has already been revoked.")); finished(); return; } ensureDialogCreated(); Q_ASSERT(dialog); dialog->setKey(key); dialog->show(); } void RevokeKeyCommand::Private::cancel() { #ifdef QGPGME_SUPPORTS_KEY_REVOCATION if (job) { job->slotCancel(); } job.clear(); #endif } void RevokeKeyCommand::Private::ensureDialogCreated() { if (dialog) { return; } dialog = new RevokeKeyDialog; applyWindowID(dialog); dialog->setAttribute(Qt::WA_DeleteOnClose); connect(dialog, &QDialog::accepted, q, [this]() { onDialogAccepted(); }); connect(dialog, &QDialog::rejected, q, [this]() { onDialogRejected(); }); } void RevokeKeyCommand::Private::onDialogAccepted() { #ifdef QGPGME_SUPPORTS_KEY_REVOCATION auto revokeJob = startJob(); if (!revokeJob) { finished(); return; } job = revokeJob.release(); #endif } void RevokeKeyCommand::Private::onDialogRejected() { canceled(); } namespace { std::vector toStdStrings(const QStringList &l) { std::vector v; v.reserve(l.size()); std::transform(std::begin(l), std::end(l), std::back_inserter(v), std::mem_fn(&QString::toStdString)); return v; } auto descriptionToLines(const QString &description) { std::vector lines; if (!description.isEmpty()) { lines = toStdStrings(description.split(QLatin1Char('\n'))); } return lines; } } #ifdef QGPGME_SUPPORTS_KEY_REVOCATION std::unique_ptr RevokeKeyCommand::Private::startJob() { std::unique_ptr revokeJob{QGpgME::openpgp()->revokeKeyJob()}; Q_ASSERT(revokeJob); connect(revokeJob.get(), &QGpgME::RevokeKeyJob::result, q, [this](const GpgME::Error &err) { onJobResult(err); }); connect(revokeJob.get(), &QGpgME::Job::progress, q, &Command::progress); const auto description = descriptionToLines(dialog->description()); const GpgME::Error err = revokeJob->start(key, dialog->reason(), description); if (err) { showError(err); return {}; } Q_EMIT q->info(i18nc("@info:status", "Revoking key...")); return revokeJob; } #endif void RevokeKeyCommand::Private::onJobResult(const Error &err) { if (err) { showError(err); finished(); return; } if (!err.isCanceled()) { information(i18nc("@info", "The key was revoked successfully."), i18nc("@title:window", "Key Revoked")); } finished(); } void RevokeKeyCommand::Private::showError(const Error &err) { error(xi18nc("@info", "An error occurred during the revocation:" "%1", QString::fromLocal8Bit(err.asString())), i18nc("@title:window", "Revocation Failed")); } RevokeKeyCommand::RevokeKeyCommand(QAbstractItemView *v, KeyListController *c) : Command{v, new Private{this, c}} { } RevokeKeyCommand::RevokeKeyCommand(const GpgME::Key &key) : Command{key, new Private{this}} { } RevokeKeyCommand::~RevokeKeyCommand() = default; void RevokeKeyCommand::doStart() { d->start(); } void RevokeKeyCommand::doCancel() { d->cancel(); } #undef d #undef q #include "moc_revokekeycommand.cpp" diff --git a/src/commands/setprimaryuseridcommand.cpp b/src/commands/setprimaryuseridcommand.cpp index 68fafb89e..ab30e36d8 100644 --- a/src/commands/setprimaryuseridcommand.cpp +++ b/src/commands/setprimaryuseridcommand.cpp @@ -1,180 +1,181 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/setprimaryuseridcommand.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2022 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "setprimaryuseridcommand.h" #include "command_p.h" #include #ifdef QGPGME_SUPPORTS_SET_PRIMARY_UID #include #endif +#include #include #include "kleopatra_debug.h" using namespace Kleo; using namespace Kleo::Commands; using namespace GpgME; class SetPrimaryUserIDCommand::Private : public Command::Private { friend class ::Kleo::Commands::SetPrimaryUserIDCommand; SetPrimaryUserIDCommand *q_func() const { return static_cast(q); } public: explicit Private(SetPrimaryUserIDCommand *qq, const UserID &userId); ~Private() override; void startJob(); private: void createJob(); void slotResult(const Error &err); void showErrorDialog(const Error &error); void showSuccessDialog(); private: GpgME::UserID userId; #ifdef QGPGME_SUPPORTS_SET_PRIMARY_UID QPointer job; #endif }; SetPrimaryUserIDCommand::Private *SetPrimaryUserIDCommand::d_func() { return static_cast(d.get()); } const SetPrimaryUserIDCommand::Private *SetPrimaryUserIDCommand::d_func() const { return static_cast(d.get()); } #define d d_func() #define q q_func() SetPrimaryUserIDCommand::Private::Private(SetPrimaryUserIDCommand *qq, const UserID &userId) : Command::Private{qq} , userId{userId} { } SetPrimaryUserIDCommand::Private::~Private() = default; void Commands::SetPrimaryUserIDCommand::Private::startJob() { #ifdef QGPGME_SUPPORTS_SET_PRIMARY_UID createJob(); if (!job) { finished(); return; } job->start(userId); #else error(i18nc("@info", "The backend does not support this operation.")); #endif } void SetPrimaryUserIDCommand::Private::createJob() { #ifdef QGPGME_SUPPORTS_SET_PRIMARY_UID Q_ASSERT(!job); const auto backend = QGpgME::openpgp(); if (!backend) { return; } const auto j = backend->setPrimaryUserIDJob(); if (!j) { return; } connect(j, &QGpgME::Job::progress, q, &Command::progress); connect(j, &QGpgME::SetPrimaryUserIDJob::result, q, [this](const GpgME::Error &err) { slotResult(err); }); job = j; #endif } void SetPrimaryUserIDCommand::Private::slotResult(const Error &err) { if (err.isCanceled()) { } else if (err) { showErrorDialog(err); } else { showSuccessDialog(); } finished(); } void SetPrimaryUserIDCommand::Private::showErrorDialog(const Error &err) { error(xi18nc("@info", "An error occurred while trying to flag the user ID%1as the primary user ID." "%2", QString::fromUtf8(userId.id()), QString::fromLocal8Bit(err.asString()))); } void SetPrimaryUserIDCommand::Private::showSuccessDialog() { success(xi18nc("@info", "The user ID%1has been flagged successfully as the primary user ID.", QString::fromUtf8(userId.id()))); } SetPrimaryUserIDCommand::SetPrimaryUserIDCommand(const GpgME::UserID &userId) : Command{new Private{this, userId}} { } SetPrimaryUserIDCommand::~SetPrimaryUserIDCommand() { qCDebug(KLEOPATRA_LOG).nospace() << this << "::" << __func__; } void SetPrimaryUserIDCommand::doStart() { if (d->userId.isNull()) { d->finished(); return; } const auto key = d->userId.parent(); if (key.protocol() != GpgME::OpenPGP || !key.hasSecret()) { d->finished(); return; } d->startJob(); } void SetPrimaryUserIDCommand::doCancel() { qCDebug(KLEOPATRA_LOG).nospace() << this << "::" << __func__; #ifdef QGPGME_SUPPORTS_SET_PRIMARY_UID if (d->job) { d->job->slotCancel(); } #endif } #undef d #undef q