Messagelib

attachmentfrompublickeyjob.cpp
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/Formatting>
19#include <Libkleo/ProgressDialog>
20
21using namespace MessageComposer;
23
24class MessageComposer::AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate
25{
26public:
27 AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob *qq);
28
29 void exportResult(const GpgME::Error &error, const QByteArray &keyData); // slot
30 void emitGpgError(const GpgME::Error &error);
31
33 QString fingerprint;
34 QByteArray data;
35};
36
37AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob *qq)
38 : q(qq)
39{
40}
41
42void AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::exportResult(const GpgME::Error &error, const QByteArray &keyData)
43{
44 if (error) {
45 emitGpgError(error);
46 return;
47 }
48
49 // Create the AttachmentPart.
50 AttachmentPart::Ptr part = AttachmentPart::Ptr(new AttachmentPart);
51 part->setName(i18n("OpenPGP key 0x%1", fingerprint.right(8)));
52 part->setFileName(QString::fromLatin1(QByteArray(QByteArray("0x") + fingerprint.toLatin1() + QByteArray(".asc"))));
53 part->setMimeType("application/pgp-keys");
54 part->setData(keyData);
55
56 q->setAttachmentPart(part);
57 q->emitResult(); // Success.
58}
59
60void AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::emitGpgError(const GpgME::Error &error)
61{
62 Q_ASSERT(error);
63 const QString msg = i18n(
64 "<p>An error occurred while trying to export "
65 "the key from the backend:</p>"
66 "<p><b>%1</b></p>",
67 Kleo::Formatting::errorAsString(error));
68 q->setError(KJob::UserDefinedError);
69 q->setErrorText(msg);
70 q->emitResult();
71}
72
73AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJob(const QString &fingerprint, QObject *parent)
75 , d(new AttachmentFromPublicKeyJobPrivate(this))
76{
77 d->fingerprint = fingerprint;
78}
79
80AttachmentFromPublicKeyJob::~AttachmentFromPublicKeyJob() = default;
81
82QString AttachmentFromPublicKeyJob::fingerprint() const
83{
84 return d->fingerprint;
85}
86
87void AttachmentFromPublicKeyJob::setFingerprint(const QString &fingerprint)
88{
89 d->fingerprint = fingerprint;
90}
91
92void AttachmentFromPublicKeyJob::doStart()
93{
94 QGpgME::ExportJob *job = QGpgME::openpgp()->publicKeyExportJob(true);
95 Q_ASSERT(job);
96 connect(job, &QGpgME::ExportJob::result, this, [this](const GpgME::Error &error, const QByteArray &ba) {
97 d->exportResult(error, ba);
98 });
99
100 const GpgME::Error error = job->start(QStringList(d->fingerprint));
101 if (error) {
102 d->emitGpgError(error);
103 // TODO check autodeletion policy of Kleo::Jobs...
104 return;
105 } else if (uiDelegate()) {
106 Q_ASSERT(dynamic_cast<KDialogJobUiDelegate *>(uiDelegate()));
107 auto delegate = static_cast<KDialogJobUiDelegate *>(uiDelegate());
108 (void)new Kleo::ProgressDialog(job, i18n("Exporting key..."), delegate->window());
109 }
110}
111
112#include "moc_attachmentfrompublickeyjob.cpp"
void emitResult()
int error() const
KJobUiDelegate * uiDelegate() const
void setAttachmentPart(const AttachmentPart::Ptr &part)
Subclasses use this method to set the loaded part.
AttachmentLoadJob(QObject *parent=nullptr)
Creates a new attachment load job.
A class that encapsulates an attachment.
QString i18n(const char *text, const TYPE &arg...)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QString fromLatin1(QByteArrayView str)
QString right(qsizetype n) const const
QByteArray toLatin1() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.