File indexing completed on 2024-06-23 05:13:45

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     commands/revokeuseridcommand.cpp
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2022 g10 Code GmbH
0006     SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include <config-kleopatra.h>
0012 
0013 #include "revokeuseridcommand.h"
0014 
0015 #include "command_p.h"
0016 
0017 #include <Libkleo/Formatting>
0018 #include <Libkleo/GnuPG>
0019 
0020 #include <KLocalizedString>
0021 
0022 #include <QGpgME/Protocol>
0023 #include <QGpgME/QuickJob>
0024 
0025 #include <gpgme++/key.h>
0026 
0027 #include "kleopatra_debug.h"
0028 
0029 using namespace Kleo;
0030 using namespace Kleo::Commands;
0031 using namespace GpgME;
0032 
0033 class RevokeUserIDCommand::Private : public Command::Private
0034 {
0035     friend class ::Kleo::Commands::RevokeUserIDCommand;
0036     RevokeUserIDCommand *q_func() const
0037     {
0038         return static_cast<RevokeUserIDCommand *>(q);
0039     }
0040 
0041 public:
0042     explicit Private(RevokeUserIDCommand *qq, const UserID &userId);
0043     ~Private() override;
0044 
0045     void startJob();
0046 
0047 private:
0048     void createJob();
0049     void slotResult(const Error &err);
0050     void showErrorDialog(const Error &error);
0051     void showSuccessDialog();
0052 
0053 private:
0054     GpgME::UserID userId;
0055     QPointer<QGpgME::QuickJob> job;
0056 };
0057 
0058 RevokeUserIDCommand::Private *RevokeUserIDCommand::d_func()
0059 {
0060     return static_cast<Private *>(d.get());
0061 }
0062 const RevokeUserIDCommand::Private *RevokeUserIDCommand::d_func() const
0063 {
0064     return static_cast<const Private *>(d.get());
0065 }
0066 
0067 #define d d_func()
0068 #define q q_func()
0069 
0070 RevokeUserIDCommand::Private::Private(RevokeUserIDCommand *qq, const UserID &userId)
0071     : Command::Private{qq}
0072     , userId{userId}
0073 {
0074 }
0075 
0076 RevokeUserIDCommand::Private::~Private() = default;
0077 
0078 void Commands::RevokeUserIDCommand::Private::startJob()
0079 {
0080     createJob();
0081     if (!job) {
0082         finished();
0083         return;
0084     }
0085     const QString uidToRevoke = QString::fromUtf8(engineIsVersion(2, 3, 7) ? userId.uidhash() : userId.id());
0086     job->startRevUid(userId.parent(), uidToRevoke);
0087 }
0088 
0089 void RevokeUserIDCommand::Private::createJob()
0090 {
0091     Q_ASSERT(!job);
0092 
0093     const auto backend = QGpgME::openpgp();
0094     if (!backend) {
0095         return;
0096     }
0097 
0098     const auto j = backend->quickJob();
0099     if (!j) {
0100         return;
0101     }
0102 
0103     connect(j, &QGpgME::Job::jobProgress, q, &Command::progress);
0104     connect(j, &QGpgME::QuickJob::result, q, [this](const GpgME::Error &err) {
0105         slotResult(err);
0106     });
0107 
0108     job = j;
0109 }
0110 
0111 void RevokeUserIDCommand::Private::slotResult(const Error &err)
0112 {
0113     if (err.isCanceled()) {
0114     } else if (err) {
0115         showErrorDialog(err);
0116     } else {
0117         showSuccessDialog();
0118     }
0119     finished();
0120 }
0121 
0122 void RevokeUserIDCommand::Private::showErrorDialog(const Error &err)
0123 {
0124     error(xi18nc("@info",
0125                  "<para>An error occurred while trying to revoke the user ID<nl/><emphasis>%1</emphasis>.</para>"
0126                  "<para><message>%2</message></para>",
0127                  QString::fromUtf8(userId.id()),
0128                  Formatting::errorAsString(err)),
0129           i18nc("@title:window", "Revocation Failed"));
0130 }
0131 
0132 void RevokeUserIDCommand::Private::showSuccessDialog()
0133 {
0134     information(xi18nc("@info", "<para>The user ID<nl/><emphasis>%1</emphasis><nl/>has been revoked successfully.</para>", QString::fromUtf8(userId.id())),
0135                 i18nc("@title:window", "Revocation Succeeded"));
0136 }
0137 
0138 RevokeUserIDCommand::RevokeUserIDCommand(const GpgME::UserID &userId)
0139     : Command{new Private{this, userId}}
0140 {
0141 }
0142 
0143 RevokeUserIDCommand::~RevokeUserIDCommand()
0144 {
0145 }
0146 
0147 void RevokeUserIDCommand::doStart()
0148 {
0149     if (d->userId.isNull()) {
0150         d->finished();
0151         return;
0152     }
0153 
0154     const auto key = d->userId.parent();
0155     if (key.protocol() != GpgME::OpenPGP || !key.hasSecret()) {
0156         d->finished();
0157         return;
0158     }
0159 
0160     d->startJob();
0161 }
0162 
0163 void RevokeUserIDCommand::doCancel()
0164 {
0165     if (d->job) {
0166         d->job->slotCancel();
0167     }
0168 }
0169 
0170 #undef d
0171 #undef q
0172 
0173 #include "moc_revokeuseridcommand.cpp"