File indexing completed on 2024-06-23 05:18:31
0001 /* 0002 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com> 0003 0004 Based on KMail code by: 0005 Various authors. 0006 0007 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0008 */ 0009 0010 #include "attachmentfrompublickeyjob.h" 0011 0012 #include <KDialogJobUiDelegate> 0013 #include <KLocalizedString> 0014 0015 #include <QGpgME/ExportJob> 0016 #include <QGpgME/Protocol> 0017 0018 #include <Libkleo/ProgressDialog> 0019 0020 using namespace MessageComposer; 0021 using MessageCore::AttachmentPart; 0022 0023 class MessageComposer::AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate 0024 { 0025 public: 0026 AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob *qq); 0027 0028 void exportResult(const GpgME::Error &error, const QByteArray &keyData); // slot 0029 void emitGpgError(const GpgME::Error &error); 0030 0031 AttachmentFromPublicKeyJob *const q; 0032 QString fingerprint; 0033 QByteArray data; 0034 }; 0035 0036 AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob *qq) 0037 : q(qq) 0038 { 0039 } 0040 0041 void AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::exportResult(const GpgME::Error &error, const QByteArray &keyData) 0042 { 0043 if (error) { 0044 emitGpgError(error); 0045 return; 0046 } 0047 0048 // Create the AttachmentPart. 0049 AttachmentPart::Ptr part = AttachmentPart::Ptr(new AttachmentPart); 0050 part->setName(i18n("OpenPGP key 0x%1", fingerprint.right(8))); 0051 part->setFileName(QString::fromLatin1(QByteArray(QByteArray("0x") + fingerprint.toLatin1() + QByteArray(".asc")))); 0052 part->setMimeType("application/pgp-keys"); 0053 part->setData(keyData); 0054 0055 q->setAttachmentPart(part); 0056 q->emitResult(); // Success. 0057 } 0058 0059 void AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::emitGpgError(const GpgME::Error &error) 0060 { 0061 Q_ASSERT(error); 0062 const QString msg = i18n( 0063 "<p>An error occurred while trying to export " 0064 "the key from the backend:</p>" 0065 "<p><b>%1</b></p>", 0066 QString::fromLocal8Bit(error.asString())); 0067 q->setError(KJob::UserDefinedError); 0068 q->setErrorText(msg); 0069 q->emitResult(); 0070 } 0071 0072 AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJob(const QString &fingerprint, QObject *parent) 0073 : AttachmentLoadJob(parent) 0074 , d(new AttachmentFromPublicKeyJobPrivate(this)) 0075 { 0076 d->fingerprint = fingerprint; 0077 } 0078 0079 AttachmentFromPublicKeyJob::~AttachmentFromPublicKeyJob() = default; 0080 0081 QString AttachmentFromPublicKeyJob::fingerprint() const 0082 { 0083 return d->fingerprint; 0084 } 0085 0086 void AttachmentFromPublicKeyJob::setFingerprint(const QString &fingerprint) 0087 { 0088 d->fingerprint = fingerprint; 0089 } 0090 0091 void AttachmentFromPublicKeyJob::doStart() 0092 { 0093 QGpgME::ExportJob *job = QGpgME::openpgp()->publicKeyExportJob(true); 0094 Q_ASSERT(job); 0095 connect(job, &QGpgME::ExportJob::result, this, [this](const GpgME::Error &error, const QByteArray &ba) { 0096 d->exportResult(error, ba); 0097 }); 0098 0099 const GpgME::Error error = job->start(QStringList(d->fingerprint)); 0100 if (error) { 0101 d->emitGpgError(error); 0102 // TODO check autodeletion policy of Kleo::Jobs... 0103 return; 0104 } else if (uiDelegate()) { 0105 Q_ASSERT(dynamic_cast<KDialogJobUiDelegate *>(uiDelegate())); 0106 auto delegate = static_cast<KDialogJobUiDelegate *>(uiDelegate()); 0107 (void)new Kleo::ProgressDialog(job, i18n("Exporting key..."), delegate->window()); 0108 } 0109 } 0110 0111 #include "moc_attachmentfrompublickeyjob.cpp"