diff --git a/src/commands/changeownertrustcommand.cpp b/src/commands/changeownertrustcommand.cpp index 68dbb02da..9cabfbd6e 100644 --- a/src/commands/changeownertrustcommand.cpp +++ b/src/commands/changeownertrustcommand.cpp @@ -1,306 +1,276 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/changeownertrustcommand.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB SPDX-FileCopyrightText: 2022 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "changeownertrustcommand.h" #include "command_p.h" #include #include #include #include #include #include #include #include "kleopatra_debug.h" using namespace Kleo; using namespace Kleo::Commands; using namespace GpgME; using namespace QGpgME; class ChangeOwnerTrustCommand::Private : public Command::Private { friend class ::Kleo::Commands::ChangeOwnerTrustCommand; ChangeOwnerTrustCommand *q_func() const { return static_cast(q); } public: - explicit Private(ChangeOwnerTrustCommand *qq, KeyListController *c); - ~Private() override; - - void init(); - -private: - void slotResult(const Error &err); + Private(ChangeOwnerTrustCommand *qq, KeyListController *c); private: void startJob(Key::OwnerTrust trust); void createJob(); + void slotResult(const Error &err); void showErrorDialog(const Error &error); void showSuccessDialog(); private: QPointer job; Key::OwnerTrust trustToSet = Key::OwnerTrust::Unknown; }; ChangeOwnerTrustCommand::Private *ChangeOwnerTrustCommand::d_func() { return static_cast(d.get()); } const ChangeOwnerTrustCommand::Private *ChangeOwnerTrustCommand::d_func() const { return static_cast(d.get()); } #define d d_func() #define q q_func() ChangeOwnerTrustCommand::Private::Private(ChangeOwnerTrustCommand *qq, KeyListController *c) - : Command::Private(qq, c), - job() + : Command::Private{qq, c} { - -} - -ChangeOwnerTrustCommand::Private::~Private() -{ - qCDebug(KLEOPATRA_LOG); -} - -ChangeOwnerTrustCommand::ChangeOwnerTrustCommand(KeyListController *c) - : Command(new Private(this, c)) -{ - d->init(); } ChangeOwnerTrustCommand::ChangeOwnerTrustCommand(QAbstractItemView *v, KeyListController *c) - : Command(v, new Private(this, c)) -{ - d->init(); -} - -ChangeOwnerTrustCommand::ChangeOwnerTrustCommand(const Key &key) - : Command(key, new Private(this, nullptr)) + : Command{v, new Private{this, c}} { - d->init(); -} - -void ChangeOwnerTrustCommand::Private::init() -{ - } ChangeOwnerTrustCommand::~ChangeOwnerTrustCommand() { - qCDebug(KLEOPATRA_LOG); + qCDebug(KLEOPATRA_LOG) << this << __func__; } void ChangeOwnerTrustCommand::doStart() { if (d->keys().size() != 1) { d->finished(); return; } const Key key = d->key(); if (key.protocol() != GpgME::OpenPGP) { d->finished(); return; } const auto keyInfo = Formatting::formatForComboBox(key); if (key.hasSecret()) { const auto answer = KMessageBox::questionYesNoCancel(d->parentWidgetOrView(), xi18nc("@info", "Is '%1' your own certificate?", keyInfo), i18nc("@title:window", "Mark Own Certificate"), KGuiItem(i18nc("@action:button", "Yes, it's mine")), KGuiItem(i18nc("@action:button", "No, it's not mine")), KStandardGuiItem::cancel()); switch (answer) { case KMessageBox::Yes: { if (key.ownerTrust() < Key::Ultimate) { d->startJob(Key::OwnerTrust::Ultimate); } return; } case KMessageBox::No: { if (key.ownerTrust() == Key::Ultimate) { d->startJob(Key::OwnerTrust::Unknown); return; } // else ask next question break; } case KMessageBox::Cancel: { d->canceled(); return; } default:; // cannot happen } } if (key.ownerTrust() < Key::OwnerTrust::Full) { const auto text = (DeVSCompliance::isCompliant() && Formatting::isKeyDeVs(key)) ? xi18nc("@info %1: a certificate, %2: name of a compliance mode", "Do you want to grant '%1' the power to mark certificates as %2 for you?" "This means that the owner of this certificate properly check fingerprints " "and confirms the identities of others.", keyInfo, DeVSCompliance::name()) : xi18nc("@info %1: a certificate", "Do you want to grant '%1' the power to mark certificates as valid for you?" "This means that the owner of this certificate properly check fingerprints " "and confirms the identities of others.", keyInfo); const auto answer = KMessageBox::questionYesNo(d->parentWidgetOrView(), text, i18nc("@title:window", "Grant Certification Power"), KGuiItem(i18nc("@action:button", "Grant Power")), KStandardGuiItem::cancel()); if (answer == KMessageBox::Yes) { d->startJob(Key::OwnerTrust::Full); } else { d->canceled(); } } else { const auto text = (DeVSCompliance::isCompliant() && Formatting::isKeyDeVs(key)) ? xi18nc("@info %1: a certificate, %2: name of a compliance mode", "The certificate '%1' is empowered to mark other certificates as %2 for you." "Do you want to revoke this power?", keyInfo, DeVSCompliance::name()) : xi18nc("@info %1: a certificate", "The certificate '%1' is empowered to mark other certificates as valid for you." "Do you want to revoke this power?", keyInfo); const auto answer = KMessageBox::questionYesNo(d->parentWidgetOrView(), text, i18nc("@title:window", "Revoke Certification Power"), KGuiItem(i18nc("@action:button", "Revoke Power")), KStandardGuiItem::cancel()); if (answer == KMessageBox::Yes) { d->startJob(Key::OwnerTrust::Unknown); } else { d->canceled(); } } } void ChangeOwnerTrustCommand::Private::startJob(Key::OwnerTrust trust) { trustToSet = trust; createJob(); Q_ASSERT(job); if (const Error err = job->start(key(), trust)) { showErrorDialog(err); finished(); } } void ChangeOwnerTrustCommand::Private::slotResult(const Error &err) { if (err.isCanceled()) ; else if (err) { showErrorDialog(err); } else { showSuccessDialog(); } finished(); } void ChangeOwnerTrustCommand::doCancel() { - qCDebug(KLEOPATRA_LOG); + qCDebug(KLEOPATRA_LOG) << this << __func__; if (d->job) { d->job->slotCancel(); } } void ChangeOwnerTrustCommand::Private::createJob() { Q_ASSERT(!job); ChangeOwnerTrustJob *const j = QGpgME::openpgp()->changeOwnerTrustJob(); if (!j) { return; } connect(j, &Job::progress, q, &Command::progress); connect(j, &ChangeOwnerTrustJob::result, q, [this](const GpgME::Error &result) { slotResult(result); }); job = j; } void ChangeOwnerTrustCommand::Private::showErrorDialog(const Error &err) { const auto keyInfo = Formatting::formatForComboBox(key()); switch (trustToSet) { case Key::OwnerTrust::Ultimate: error(xi18nc("@info", "An error occurred while marking certificate '%1' as your certificate." "%2", keyInfo, QString::fromUtf8(err.asString()))); break; case Key::OwnerTrust::Full: error(xi18nc("@info", "An error occurred while granting certification power to '%1'." "%2", keyInfo, QString::fromUtf8(err.asString()))); break; default: error(xi18nc("@info", "An error occurred while revoking the certification power of '%1'." "%2", keyInfo, QString::fromUtf8(err.asString()))); } } void ChangeOwnerTrustCommand::Private::showSuccessDialog() { auto updatedKey = key(); updatedKey.update(); KeyCache::mutableInstance()->insert(updatedKey); const auto keyInfo = Formatting::formatForComboBox(updatedKey); switch (updatedKey.ownerTrust()) { case Key::OwnerTrust::Ultimate: success(i18nc("@info", "Certificate '%1' was marked as your certificate.", keyInfo)); break; case Key::OwnerTrust::Full: success(i18nc("@info", "Certification power was granted to '%1'.", keyInfo)); break; default: success(i18nc("@info", "The certification power of '%1' was revoked.", keyInfo)); } } #undef d #undef q #include "moc_changeownertrustcommand.cpp" diff --git a/src/commands/changeownertrustcommand.h b/src/commands/changeownertrustcommand.h index 927e6d516..9e3ed4e75 100644 --- a/src/commands/changeownertrustcommand.h +++ b/src/commands/changeownertrustcommand.h @@ -1,45 +1,43 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/changeownertrustcommand.h This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include namespace Kleo { namespace Commands { class ChangeOwnerTrustCommand : public Command { Q_OBJECT public: - explicit ChangeOwnerTrustCommand(QAbstractItemView *view, KeyListController *parent); - explicit ChangeOwnerTrustCommand(KeyListController *parent); - explicit ChangeOwnerTrustCommand(const GpgME::Key &key); + ChangeOwnerTrustCommand(QAbstractItemView *view, KeyListController *parent); ~ChangeOwnerTrustCommand() override; /* reimp */ static Restrictions restrictions() { return OnlyOneKey | MustBeOpenPGP | MayOnlyBeSecretKeyIfOwnerTrustIsNotYetUltimate; } private: void doStart() override; void doCancel() override; private: class Private; inline Private *d_func(); inline const Private *d_func() const; }; } }