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

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     commands/setprimaryuseridcommand.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 "setprimaryuseridcommand.h"
0014 
0015 #include "command_p.h"
0016 
0017 #include <Libkleo/Formatting>
0018 
0019 #include <KLocalizedString>
0020 
0021 #include <QGpgME/SetPrimaryUserIDJob>
0022 
0023 #include <QGpgME/Protocol>
0024 #include <gpgme++/key.h>
0025 
0026 #include "kleopatra_debug.h"
0027 
0028 using namespace Kleo;
0029 using namespace Kleo::Commands;
0030 using namespace GpgME;
0031 
0032 class SetPrimaryUserIDCommand::Private : public Command::Private
0033 {
0034     friend class ::Kleo::Commands::SetPrimaryUserIDCommand;
0035     SetPrimaryUserIDCommand *q_func() const
0036     {
0037         return static_cast<SetPrimaryUserIDCommand *>(q);
0038     }
0039 
0040 public:
0041     explicit Private(SetPrimaryUserIDCommand *qq, const UserID &userId);
0042     ~Private() override;
0043 
0044     void startJob();
0045 
0046 private:
0047     void createJob();
0048     void slotResult(const Error &err);
0049     void showErrorDialog(const Error &error);
0050     void showSuccessDialog();
0051 
0052 private:
0053     GpgME::UserID userId;
0054     QPointer<QGpgME::SetPrimaryUserIDJob> job;
0055 };
0056 
0057 SetPrimaryUserIDCommand::Private *SetPrimaryUserIDCommand::d_func()
0058 {
0059     return static_cast<Private *>(d.get());
0060 }
0061 const SetPrimaryUserIDCommand::Private *SetPrimaryUserIDCommand::d_func() const
0062 {
0063     return static_cast<const Private *>(d.get());
0064 }
0065 
0066 #define d d_func()
0067 #define q q_func()
0068 
0069 SetPrimaryUserIDCommand::Private::Private(SetPrimaryUserIDCommand *qq, const UserID &userId)
0070     : Command::Private{qq}
0071     , userId{userId}
0072 {
0073 }
0074 
0075 SetPrimaryUserIDCommand::Private::~Private() = default;
0076 
0077 void Commands::SetPrimaryUserIDCommand::Private::startJob()
0078 {
0079     createJob();
0080     if (!job) {
0081         finished();
0082         return;
0083     }
0084     job->start(userId);
0085 }
0086 
0087 void SetPrimaryUserIDCommand::Private::createJob()
0088 {
0089     Q_ASSERT(!job);
0090 
0091     const auto backend = QGpgME::openpgp();
0092     if (!backend) {
0093         return;
0094     }
0095 
0096     const auto j = backend->setPrimaryUserIDJob();
0097     if (!j) {
0098         return;
0099     }
0100 
0101     connect(j, &QGpgME::Job::jobProgress, q, &Command::progress);
0102     connect(j, &QGpgME::SetPrimaryUserIDJob::result, q, [this](const GpgME::Error &err) {
0103         slotResult(err);
0104     });
0105 
0106     job = j;
0107 }
0108 
0109 void SetPrimaryUserIDCommand::Private::slotResult(const Error &err)
0110 {
0111     if (err.isCanceled()) {
0112     } else if (err) {
0113         showErrorDialog(err);
0114     } else {
0115         showSuccessDialog();
0116     }
0117     finished();
0118 }
0119 
0120 void SetPrimaryUserIDCommand::Private::showErrorDialog(const Error &err)
0121 {
0122     error(xi18nc("@info",
0123                  "<para>An error occurred while trying to flag the user ID<nl/><emphasis>%1</emphasis><nl/>as the primary user ID.</para>"
0124                  "<para><message>%2</message></para>",
0125                  QString::fromUtf8(userId.id()),
0126                  Formatting::errorAsString(err)));
0127 }
0128 
0129 void SetPrimaryUserIDCommand::Private::showSuccessDialog()
0130 {
0131     success(xi18nc("@info",
0132                    "<para>The user ID<nl/><emphasis>%1</emphasis><nl/>has been flagged successfully as the primary user ID.</para>",
0133                    QString::fromUtf8(userId.id())));
0134 }
0135 
0136 SetPrimaryUserIDCommand::SetPrimaryUserIDCommand(const GpgME::UserID &userId)
0137     : Command{new Private{this, userId}}
0138 {
0139 }
0140 
0141 SetPrimaryUserIDCommand::~SetPrimaryUserIDCommand()
0142 {
0143     qCDebug(KLEOPATRA_LOG).nospace() << this << "::" << __func__;
0144 }
0145 
0146 void SetPrimaryUserIDCommand::doStart()
0147 {
0148     if (d->userId.isNull()) {
0149         d->finished();
0150         return;
0151     }
0152 
0153     const auto key = d->userId.parent();
0154     if (key.protocol() != GpgME::OpenPGP || !key.hasSecret()) {
0155         d->finished();
0156         return;
0157     }
0158 
0159     d->startJob();
0160 }
0161 
0162 void SetPrimaryUserIDCommand::doCancel()
0163 {
0164     qCDebug(KLEOPATRA_LOG).nospace() << this << "::" << __func__;
0165     if (d->job) {
0166         d->job->slotCancel();
0167     }
0168 }
0169 
0170 #undef d
0171 #undef q
0172 
0173 #include "moc_setprimaryuseridcommand.cpp"