1 /*
2   SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3 
4   Based on KMail code by:
5   Various authors.
6 
7   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
8 */
9 
10 #include "attachmentfrompublickeyjob.h"
11 
12 #include <KDialogJobUiDelegate>
13 #include <KLocalizedString>
14 
15 #include <QGpgME/ExportJob>
16 #include <QGpgME/Protocol>
17 
18 #include <Libkleo/ProgressDialog>
19 
20 using namespace MessageComposer;
21 using MessageCore::AttachmentPart;
22 
23 class MessageComposer::AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate
24 {
25 public:
26     AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob *qq);
27 
28     void exportResult(const GpgME::Error &error, const QByteArray &keyData); // slot
29     void emitGpgError(const GpgME::Error &error);
30 
31     AttachmentFromPublicKeyJob *const q;
32     QString fingerprint;
33     QByteArray data;
34 };
35 
AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob * qq)36 AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob *qq)
37     : q(qq)
38 {
39 }
40 
exportResult(const GpgME::Error & error,const QByteArray & keyData)41 void AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::exportResult(const GpgME::Error &error, const QByteArray &keyData)
42 {
43     if (error) {
44         emitGpgError(error);
45         return;
46     }
47 
48     // Create the AttachmentPart.
49     AttachmentPart::Ptr part = AttachmentPart::Ptr(new AttachmentPart);
50     part->setName(i18n("OpenPGP key 0x%1", fingerprint.right(8)));
51     part->setFileName(QString::fromLatin1(QByteArray(QByteArray("0x") + fingerprint.toLatin1() + QByteArray(".asc"))));
52     part->setMimeType("application/pgp-keys");
53     part->setData(keyData);
54 
55     q->setAttachmentPart(part);
56     q->emitResult(); // Success.
57 }
58 
emitGpgError(const GpgME::Error & error)59 void AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::emitGpgError(const GpgME::Error &error)
60 {
61     Q_ASSERT(error);
62     const QString msg = i18n(
63         "<p>An error occurred while trying to export "
64         "the key from the backend:</p>"
65         "<p><b>%1</b></p>",
66         QString::fromLocal8Bit(error.asString()));
67     q->setError(KJob::UserDefinedError);
68     q->setErrorText(msg);
69     q->emitResult();
70 }
71 
AttachmentFromPublicKeyJob(const QString & fingerprint,QObject * parent)72 AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJob(const QString &fingerprint, QObject *parent)
73     : AttachmentLoadJob(parent)
74     , d(new AttachmentFromPublicKeyJobPrivate(this))
75 {
76     d->fingerprint = fingerprint;
77 }
78 
79 AttachmentFromPublicKeyJob::~AttachmentFromPublicKeyJob() = default;
80 
fingerprint() const81 QString AttachmentFromPublicKeyJob::fingerprint() const
82 {
83     return d->fingerprint;
84 }
85 
setFingerprint(const QString & fingerprint)86 void AttachmentFromPublicKeyJob::setFingerprint(const QString &fingerprint)
87 {
88     d->fingerprint = fingerprint;
89 }
90 
doStart()91 void AttachmentFromPublicKeyJob::doStart()
92 {
93     QGpgME::ExportJob *job = QGpgME::openpgp()->publicKeyExportJob(true);
94     Q_ASSERT(job);
95     connect(job, &QGpgME::ExportJob::result, this, [this](const GpgME::Error &error, const QByteArray &ba) {
96         d->exportResult(error, ba);
97     });
98 
99     const GpgME::Error error = job->start(QStringList(d->fingerprint));
100     if (error) {
101         d->emitGpgError(error);
102         // TODO check autodeletion policy of Kleo::Jobs...
103         return;
104     } else if (uiDelegate()) {
105         Q_ASSERT(dynamic_cast<KDialogJobUiDelegate *>(uiDelegate()));
106         auto delegate = static_cast<KDialogJobUiDelegate *>(uiDelegate());
107         (void)new Kleo::ProgressDialog(job, i18n("Exporting key..."), delegate->window());
108     }
109 }
110 
111 #include "moc_attachmentfrompublickeyjob.cpp"
112