Line data Source code
1 : /* defaultkeygenerationjob.cpp
2 :
3 : Copyright (c) 2016 Klarälvdalens Datakonsult AB
4 :
5 : QGpgME is free software; you can redistribute it and/or
6 : modify it under the terms of the GNU General Public License as
7 : published by the Free Software Foundation; either version 2 of the
8 : License, or (at your option) any later version.
9 :
10 : QGpgME is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public License along
16 : with this program; if not, write to the Free Software Foundation, Inc.,
17 : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 :
19 : In addition, as a special exception, the copyright holders give
20 : permission to link the code of this program with any edition of
21 : the Qt library by Trolltech AS, Norway (or with modified versions
22 : of Qt that use the same license as Qt), and distribute linked
23 : combinations including the two. You must obey the GNU General
24 : Public License in all respects for all of the code used other than
25 : Qt. If you modify this file, you may extend this exception to
26 : your version of the file, but you are not obligated to do so. If
27 : you do not wish to do so, delete this exception statement from
28 : your version.
29 : */
30 :
31 : #ifdef HAVE_CONFIG_H
32 : #include "config.h"
33 : #endif
34 :
35 : #include "defaultkeygenerationjob.h"
36 : #include "protocol.h"
37 : #include "keygenerationjob.h"
38 :
39 : #include <QPointer>
40 : #include <QEvent>
41 :
42 : using namespace QGpgME;
43 :
44 : namespace QGpgME {
45 :
46 : class DefaultKeyGenerationJob::Private
47 : {
48 : public:
49 0 : Private()
50 0 : {}
51 :
52 0 : ~Private()
53 0 : {
54 0 : if (job) {
55 0 : job->deleteLater();
56 : }
57 0 : }
58 :
59 : QPointer<KeyGenerationJob> job;
60 : };
61 : }
62 :
63 :
64 0 : DefaultKeyGenerationJob::DefaultKeyGenerationJob(QObject* parent)
65 : : Job(parent)
66 0 : , d(new DefaultKeyGenerationJob::Private())
67 : {
68 0 : }
69 :
70 0 : DefaultKeyGenerationJob::~DefaultKeyGenerationJob()
71 : {
72 0 : delete d;
73 0 : }
74 :
75 0 : QString DefaultKeyGenerationJob::auditLogAsHtml() const
76 : {
77 0 : return d->job ? d->job->auditLogAsHtml() : QString();
78 : }
79 :
80 0 : GpgME::Error DefaultKeyGenerationJob::auditLogError() const
81 : {
82 0 : return d->job ? d->job->auditLogError() : GpgME::Error();
83 : }
84 :
85 0 : void DefaultKeyGenerationJob::slotCancel()
86 : {
87 0 : if (d->job) {
88 0 : d->job->slotCancel();
89 : }
90 0 : }
91 :
92 0 : GpgME::Error DefaultKeyGenerationJob::start(const QString &email, const QString &name)
93 : {
94 0 : const QString namePart = name.isEmpty() ? QString() :
95 0 : QStringLiteral("name-real: %1\n").arg(name);
96 0 : const QString mailPart = email.isEmpty() ? QString() :
97 0 : QStringLiteral("name-email: %1\n").arg(email);
98 :
99 0 : const QString args = QStringLiteral("<GnupgKeyParms format=\"internal\">\n"
100 : "%ask-passphrase\n"
101 : "key-type: RSA\n"
102 : "key-length: 2048\n"
103 : "key-usage: sign\n"
104 : "subkey-type: RSA\n"
105 : "subkey-length: 2048\n"
106 : "subkey-usage: encrypt\n"
107 : "%1"
108 : "%2"
109 0 : "</GnupgKeyParms>").arg(mailPart, namePart);
110 :
111 0 : d->job = openpgp()->keyGenerationJob();
112 0 : d->job->installEventFilter(this);
113 0 : connect(d->job.data(), &KeyGenerationJob::result,
114 0 : this, &DefaultKeyGenerationJob::result);
115 0 : connect(d->job.data(), &KeyGenerationJob::done,
116 0 : this, &DefaultKeyGenerationJob::done);
117 0 : connect(d->job.data(), &KeyGenerationJob::done,
118 0 : this, &QObject::deleteLater);
119 0 : return d->job->start(args);
120 : }
121 :
122 0 : bool DefaultKeyGenerationJob::eventFilter(QObject *watched, QEvent *event)
123 : {
124 : // Intercept the KeyGenerationJob's deferred delete event. We want the job
125 : // to live at least as long as we do so we can delegate calls to it. We will
126 : // delete the job manually afterwards.
127 0 : if (watched == d->job && event->type() == QEvent::DeferredDelete) {
128 0 : return true;
129 : }
130 :
131 0 : return Job::eventFilter(watched, event);
132 : }
133 :
134 : #include "defaultkeygenerationjob.moc"
|