Messagelib

attachmentfrompublickeyjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Constantin Berzan <[email protected]>
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;
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 
32  QString fingerprint;
33  QByteArray data;
34 };
35 
36 AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJobPrivate::AttachmentFromPublicKeyJobPrivate(AttachmentFromPublicKeyJob *qq)
37  : q(qq)
38 {
39 }
40 
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.
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 
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 
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 
81 QString AttachmentFromPublicKeyJob::fingerprint() const
82 {
83  return d->fingerprint;
84 }
85 
86 void AttachmentFromPublicKeyJob::setFingerprint(const QString &fingerprint)
87 {
88  d->fingerprint = fingerprint;
89 }
90 
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"
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
A class that encapsulates an attachment.
QString i18n(const char *text, const TYPE &arg...)
QString fromLocal8Bit(const char *str, int size)
QSharedPointer< AttachmentPart > Ptr
Defines a pointer to an attachment object.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString fromLatin1(const char *str, int size)
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Mar 22 2023 04:07:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.