revokeuseridcommand.cpp 3.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* -*- mode: c++; c-basic-offset:4 -*-
    commands/revokeuseridcommand.cpp

    This file is part of Kleopatra, the KDE keymanager
    SPDX-FileCopyrightText: 2022 g10 Code GmbH
    SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include <config-kleopatra.h>

#include "revokeuseridcommand.h"

#include "command_p.h"

#include <Libkleo/GnuPG>

#include <KLocalizedString>

#include <QGpgME/Protocol>
#include <QGpgME/QuickJob>

#include <gpgme++/key.h>

#include "kleopatra_debug.h"

using namespace Kleo;
using namespace Kleo::Commands;
using namespace GpgME;

class RevokeUserIDCommand::Private : public Command::Private
{
    friend class ::Kleo::Commands::RevokeUserIDCommand;
    RevokeUserIDCommand *q_func() const
    {
        return static_cast<RevokeUserIDCommand *>(q);
    }
public:
    explicit Private(RevokeUserIDCommand *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;
    QPointer<QGpgME::QuickJob> job;
};

RevokeUserIDCommand::Private *RevokeUserIDCommand::d_func()
{
    return static_cast<Private *>(d.get());
}
const RevokeUserIDCommand::Private *RevokeUserIDCommand::d_func() const
{
    return static_cast<const Private *>(d.get());
}

#define d d_func()
#define q q_func()

RevokeUserIDCommand::Private::Private(RevokeUserIDCommand *qq, const UserID &userId)
    : Command::Private{qq}
    , userId{userId}
{
}

RevokeUserIDCommand::Private::~Private() = default;

void Commands::RevokeUserIDCommand::Private::startJob()
{
    createJob();
    if (!job) {
        finished();
        return;
    }
83
    const QString uidToRevoke = QString::fromUtf8(engineIsVersion(2, 3, 7) ? userId.uidhash() : userId.id());
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    job->startRevUid(userId.parent(), uidToRevoke);
}

void RevokeUserIDCommand::Private::createJob()
{
    Q_ASSERT(!job);

    const auto backend = QGpgME::openpgp();
    if (!backend) {
        return;
    }

    const auto j = backend->quickJob();
    if (!j) {
        return;
    }

    connect(j, &QGpgME::Job::progress,
            q, &Command::progress);
    connect(j, &QGpgME::QuickJob::result,
            q, [this](const GpgME::Error &err) { slotResult(err); });

    job = j;
}

void RevokeUserIDCommand::Private::slotResult(const Error &err)
{
    if (err.isCanceled()) {
    } else if (err) {
        showErrorDialog(err);
    } else {
        showSuccessDialog();
    }
    finished();
}

void RevokeUserIDCommand::Private::showErrorDialog(const Error &err)
{
    error(xi18nc("@info",
                 "<para>An error occurred while trying to revoke the user ID<nl/><emphasis>%1</emphasis>.</para>"
                 "<para><message>%2</message></para>",
                 QString::fromUtf8(userId.id()), QString::fromLocal8Bit(err.asString())),
          i18nc("@title:window", "Revocation Failed"));
}

void RevokeUserIDCommand::Private::showSuccessDialog()
{
    information(xi18nc("@info", "<para>The user ID<nl/><emphasis>%1</emphasis><nl/>has been revoked successfully.</para>",
                       QString::fromUtf8(userId.id())),
                i18nc("@title:window", "Revocation Succeeded"));
}

RevokeUserIDCommand::RevokeUserIDCommand(const GpgME::UserID &userId)
    : Command{new Private{this, userId}}
{
}

RevokeUserIDCommand::~RevokeUserIDCommand()
{
}

void RevokeUserIDCommand::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 RevokeUserIDCommand::doCancel()
{
    if (d->job) {
        d->job->slotCancel();
    }
}

#undef d
#undef q

#include "moc_revokeuseridcommand.cpp"