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/ProgressDialog>
19
20using namespace MessageComposer;
22
23class MessageComposer::AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate
24{
25public:
26 AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob *qq);
27
28 void exportResult(const GpgME::Error &error, const QByteArray &keyData); // slot
29 void emitGpgError(const GpgME::Error &error);
30
32 QString fingerprint;
33 QByteArray data;
34};
35
36AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob *qq)
37 : q(qq)
38{
39}
40
41void 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
59void 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
72AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJob(const QString &fingerprint, QObject *parent)
74 , d(new AttachmentFromPublicKeyJobPrivate(this))
75{
76 d->fingerprint = fingerprint;
77}
78
79AttachmentFromPublicKeyJob::~AttachmentFromPublicKeyJob() = default;
80
81QString AttachmentFromPublicKeyJob::fingerprint() const
82{
83 return d->fingerprint;
84}
85
86void AttachmentFromPublicKeyJob::setFingerprint(const QString &fingerprint)
87{
88 d->fingerprint = fingerprint;
89}
90
91void 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"
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 fromLocal8Bit(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 Fri Jul 26 2024 11:54:19 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.