summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Vrátil <dvratil@kde.org>2016-08-10 13:09:30 (GMT)
committerDaniel Vrátil <dvratil@kde.org>2016-08-10 13:11:20 (GMT)
commitf49b7157e55a25da325eb4c35ac4134e20bdb2f1 (patch)
treef6962fa0a51f536cdc37ac4b11b262355ffca5a9
parentf93f14e544c8248612b91471600a99c6dc9bb3a5 (diff)
Introduce Kleo::DefaultKeyGenerationJob
The DefaultKeyGenerationJob takes only email and name, assembles the XML with additional arguments using default key type (RSA), key length (2048 bits) and passes it to the proper KeyGenerationJob. The purpose of this job is to avoid having to have to build the nasty XML everytime we want to generate a simple key pair. Would be nice to eventually have a full-featured job that would allow customizing more arguments, like key type, length, purpose etc.
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/kleo/defaultkeygenerationjob.cpp111
-rw-r--r--src/kleo/defaultkeygenerationjob.h67
3 files changed, 180 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ce08907..d5636cb 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -75,6 +75,7 @@ set(libkleo_core_SRCS
kleo/multideletejob.cpp
kleo/hierarchicalkeylistjob.cpp
kleo/defaultkeyfilter.cpp
+ kleo/defaultkeygenerationjob.cpp
kleo/kconfigbasedkeyfilter.cpp
kleo/keyfiltermanager.cpp
models/keycache.cpp
@@ -207,6 +208,7 @@ ecm_generate_headers(libkleo_CamelCase_HEADERS
AbstractImportJob
DefaultKeyFilter
KeyForMailboxJob
+ DefaultKeyGenerationJob
REQUIRED_HEADERS libkleo_HEADERS
PREFIX Libkleo
RELATIVE kleo
diff --git a/src/kleo/defaultkeygenerationjob.cpp b/src/kleo/defaultkeygenerationjob.cpp
new file mode 100644
index 0000000..7a274d6
--- /dev/null
+++ b/src/kleo/defaultkeygenerationjob.cpp
@@ -0,0 +1,111 @@
+/* This file is part of Kleopatra, the KDE keymanager
+ Copyright (c) 2016 Klarälvdalens Datakonsult AB
+
+ Kleopatra is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Kleopatra is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "defaultkeygenerationjob.h"
+#include "kleo/cryptobackendfactory.h"
+#include "kleo/keygenerationjob.h"
+
+#include <QPointer>
+#include <QEvent>
+
+using namespace Kleo;
+
+namespace Kleo {
+
+class DefaultKeyGenerationJob::Private
+{
+public:
+ Private()
+ {}
+
+ ~Private()
+ {
+ if (job) {
+ job->deleteLater();
+ }
+ }
+
+ QPointer<KeyGenerationJob> job;
+};
+}
+
+
+DefaultKeyGenerationJob::DefaultKeyGenerationJob(QObject* parent)
+ : Job(parent)
+ , d(new DefaultKeyGenerationJob::Private())
+{
+}
+
+DefaultKeyGenerationJob::~DefaultKeyGenerationJob()
+{
+ delete d;
+}
+
+QString DefaultKeyGenerationJob::auditLogAsHtml() const
+{
+ return d->job ? d->job->auditLogAsHtml() : QString();
+}
+
+GpgME::Error DefaultKeyGenerationJob::auditLogError() const
+{
+ return d->job ? d->job->auditLogError() : GpgME::Error();
+}
+
+void DefaultKeyGenerationJob::slotCancel()
+{
+ if (d->job) {
+ d->job->slotCancel();
+ }
+}
+
+GpgME::Error DefaultKeyGenerationJob::start(const QString &email, const QString &name)
+{
+ const QString args = QStringLiteral("<GnupgKeyParms format=\"internal\">\n"
+ "%ask-passphrase\n"
+ "key-type: RSA\n"
+ "key-length: 2048\n"
+ "key-usage: sign\n"
+ "subkey-type: RSA\n"
+ "subkey-length: 2048\n"
+ "subkey-usage: encrypt\n"
+ "name-email: %1\n"
+ "name-real: %2\n"
+ "</GnupgKeyParms>").arg(email, name);
+
+ d->job = CryptoBackendFactory::instance()->openpgp()->keyGenerationJob();
+ d->job->installEventFilter(this);
+ connect(d->job, &KeyGenerationJob::result,
+ this, &DefaultKeyGenerationJob::result);
+ connect(d->job, &KeyGenerationJob::done,
+ this, &DefaultKeyGenerationJob::done);
+ connect(d->job, &KeyGenerationJob::done,
+ this, &QObject::deleteLater);
+ return d->job->start(args);
+}
+
+bool DefaultKeyGenerationJob::eventFilter(QObject *watched, QEvent *event)
+{
+ // Intercept the KeyGenerationJob's deferred delete event. We want the job
+ // to live at least as long as we do so we can delegate calls to it. We will
+ // delete the job manually afterwards.
+ if (watched == d->job && event->type() == QEvent::DeferredDelete) {
+ return true;
+ }
+
+ return Job::eventFilter(watched, event);
+}
diff --git a/src/kleo/defaultkeygenerationjob.h b/src/kleo/defaultkeygenerationjob.h
new file mode 100644
index 0000000..ddff01a
--- /dev/null
+++ b/src/kleo/defaultkeygenerationjob.h
@@ -0,0 +1,67 @@
+/* This file is part of Kleopatra, the KDE keymanager
+ Copyright (c) 2016 Klarälvdalens Datakonsult AB
+
+ Kleopatra is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Kleopatra is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef LIBKLEO_DEFAULTKEYGENERATION_H
+#define LIBKLEO_DEFAULTKEYGENERATION_H
+
+#include "job.h"
+
+#include <kleo_export.h>
+
+namespace GpgME {
+class KeyGenerationResult;
+}
+
+namespace Kleo {
+
+/**
+ * Generates a PGP RSA/2048 bit key pair for given name and email address.
+ *
+ * @since 5.4
+ */
+class KLEO_EXPORT DefaultKeyGenerationJob : public Job
+{
+ Q_OBJECT
+public:
+ explicit DefaultKeyGenerationJob(QObject *parent = Q_NULLPTR);
+ ~DefaultKeyGenerationJob();
+
+ GpgME::Error start(const QString &email, const QString &name);
+
+ QString auditLogAsHtml() const Q_DECL_OVERRIDE;
+ GpgME::Error auditLogError() const Q_DECL_OVERRIDE;
+
+
+public Q_SLOTS:
+ void slotCancel() Q_DECL_OVERRIDE;
+
+Q_SIGNALS:
+ void result(const GpgME::KeyGenerationResult &result, const QByteArray &pubkeyData,
+ const QString &auditLogAsHtml, const GpgME::Error &auditLogError);
+
+protected:
+ bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE;
+
+private:
+ class Private;
+ Private * const d;
+};
+
+}
+
+#endif