Libkleo

defaultkeygenerationjob.cpp
1 /* This file is part of Kleopatra, the KDE keymanager
2  SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include <config-libkleo.h>
8 
9 #include "defaultkeygenerationjob.h"
10 
11 #include <QGpgME/KeyGenerationJob>
12 #include <QGpgME/Protocol>
13 
14 #include <QEvent>
15 #include <QPointer>
16 
17 using namespace Kleo;
18 
19 namespace Kleo
20 {
21 
22 class DefaultKeyGenerationJob::DefaultKeyGenerationJobPrivate
23 {
24 public:
25  DefaultKeyGenerationJobPrivate()
26  {
27  }
28 
29  ~DefaultKeyGenerationJobPrivate()
30  {
31  if (job) {
32  job->deleteLater();
33  }
34  }
35 
36  QString passphrase;
38 };
39 }
40 
41 DefaultKeyGenerationJob::DefaultKeyGenerationJob(QObject *parent)
42  : Job(parent)
43  , d(new DefaultKeyGenerationJob::DefaultKeyGenerationJobPrivate())
44 {
45 }
46 
47 DefaultKeyGenerationJob::~DefaultKeyGenerationJob() = default;
48 
49 QString DefaultKeyGenerationJob::auditLogAsHtml() const
50 {
51  return d->job ? d->job->auditLogAsHtml() : QString();
52 }
53 
54 GpgME::Error DefaultKeyGenerationJob::auditLogError() const
55 {
56  return d->job ? d->job->auditLogError() : GpgME::Error();
57 }
58 
59 void DefaultKeyGenerationJob::slotCancel()
60 {
61  if (d->job) {
62  d->job->slotCancel();
63  }
64 }
65 
67 {
68  // null QString = ask for passphrase
69  // empty QString = empty passphrase
70  // non-empty QString = passphrase
71  d->passphrase = passphrase.isNull() ? QLatin1StringView("") : passphrase;
72 }
73 
74 namespace
75 {
76 QString passphraseParameter(const QString &passphrase)
77 {
78  if (passphrase.isNull()) {
79  return QStringLiteral("%ask-passphrase");
80  } else if (passphrase.isEmpty()) {
81  return QStringLiteral("%no-protection");
82  } else {
83  return QStringLiteral("passphrase: %1").arg(passphrase);
84  };
85 }
86 }
87 
88 GpgME::Error DefaultKeyGenerationJob::start(const QString &email, const QString &name)
89 {
90  const QString passphrase = passphraseParameter(d->passphrase);
91  const QString args = QStringLiteral(
92  "<GnupgKeyParms format=\"internal\">\n"
93  "key-type: RSA\n"
94  "key-length: 2048\n"
95  "key-usage: sign\n"
96  "subkey-type: RSA\n"
97  "subkey-length: 2048\n"
98  "subkey-usage: encrypt\n"
99  "%1\n"
100  "name-email: %2\n"
101  "name-real: %3\n"
102  "</GnupgKeyParms>")
103  .arg(passphrase, email, name);
104 
105  d->job = QGpgME::openpgp()->keyGenerationJob();
106  d->job->installEventFilter(this);
107  connect(d->job.data(), &QGpgME::KeyGenerationJob::result, this, &DefaultKeyGenerationJob::result);
108  connect(d->job.data(), &QGpgME::KeyGenerationJob::done, this, &DefaultKeyGenerationJob::done);
109  connect(d->job.data(), &QGpgME::KeyGenerationJob::done, this, &QObject::deleteLater);
110  return d->job->start(args);
111 }
112 
113 bool DefaultKeyGenerationJob::eventFilter(QObject *watched, QEvent *event)
114 {
115  // Intercept the KeyGenerationJob's deferred delete event. We want the job
116  // to live at least as long as we do so we can delegate calls to it. We will
117  // delete the job manually afterwards.
118  if (watched == d->job && event->type() == QEvent::DeferredDelete) {
119  return true;
120  }
121 
122  return Job::eventFilter(watched, event);
123 }
124 
125 #include "moc_defaultkeygenerationjob.cpp"
bool isNull() const const
void deleteLater()
Generates a PGP RSA/2048 bit key pair for given name and email address.
bool isEmpty() const const
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
void setPassphrase(const QString &passphrase)
Set key passphrase.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:56:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.