Line data Source code
1 : /*
2 : qgpgmesecretexportjob.cpp
3 :
4 : This file is part of qgpgme, the Qt API binding for gpgme
5 : Copyright (c) 2004 Klarävdalens Datakonsult AB
6 : Copyright (c) 2016 Intevation GmbH
7 :
8 : QGpgME is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License as
10 : published by the Free Software Foundation; either version 2 of the
11 : License, or (at your option) any later version.
12 :
13 : QGpgME is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 :
22 : In addition, as a special exception, the copyright holders give
23 : permission to link the code of this program with any edition of
24 : the Qt library by Trolltech AS, Norway (or with modified versions
25 : of Qt that use the same license as Qt), and distribute linked
26 : combinations including the two. You must obey the GNU General
27 : Public License in all respects for all of the code used other than
28 : Qt. If you modify this file, you may extend this exception to
29 : your version of the file, but you are not obligated to do so. If
30 : you do not wish to do so, delete this exception statement from
31 : your version.
32 : */
33 :
34 : #ifdef HAVE_CONFIG_H
35 : #include "config.h"
36 : #endif
37 :
38 : #include "qgpgmesecretkeyexportjob.h"
39 :
40 : #include <QDebug>
41 : #include "gpgme_backend_debug.h"
42 :
43 : #include "context.h"
44 : #include "data.h"
45 :
46 : #include <QStringList>
47 :
48 : #include <gpg-error.h>
49 :
50 : #include <string.h>
51 : #include <assert.h>
52 :
53 0 : QGpgME::QGpgMESecretKeyExportJob::QGpgMESecretKeyExportJob(bool armour, const QString &charset)
54 : : ExportJob(0),
55 : mProcess(0),
56 : mError(0),
57 : mArmour(armour),
58 0 : mCharset(charset)
59 : {
60 :
61 0 : }
62 :
63 0 : QGpgME::QGpgMESecretKeyExportJob::~QGpgMESecretKeyExportJob()
64 : {
65 :
66 0 : }
67 :
68 0 : GpgME::Error QGpgME::QGpgMESecretKeyExportJob::start(const QStringList &patterns)
69 : {
70 0 : assert(mKeyData.isEmpty());
71 :
72 0 : if (patterns.size() != 1 || patterns.front().isEmpty()) {
73 0 : deleteLater();
74 0 : return mError = GpgME::Error::fromCode(GPG_ERR_INV_VALUE, GPG_ERR_SOURCE_GPGSM);
75 : }
76 :
77 : // create and start gpgsm process:
78 0 : mProcess = new QProcess(this);
79 0 : mProcess->setObjectName(QStringLiteral("gpgsm --export-secret-key-p12"));
80 :
81 : // FIXME: obtain the path to gpgsm from gpgme, so we use the same instance.
82 0 : mProcess->setProgram("gpgsm");
83 0 : QStringList arguments;
84 0 : arguments << QStringLiteral("--export-secret-key-p12");
85 0 : if (mArmour) {
86 0 : arguments << QStringLiteral("--armor");
87 : }
88 0 : if (!mCharset.isEmpty()) {
89 0 : arguments << QStringLiteral("--p12-charset") << mCharset;
90 : }
91 0 : arguments << QLatin1String(patterns.front().toUtf8());
92 :
93 0 : mProcess->setArguments(arguments);
94 0 : connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)),
95 0 : SLOT(slotProcessExited(int,QProcess::ExitStatus)));
96 0 : connect(mProcess, &QProcess::readyReadStandardOutput,
97 0 : this, &QGpgMESecretKeyExportJob::slotStdout);
98 0 : connect(mProcess, &QProcess::readyReadStandardError,
99 0 : this, &QGpgMESecretKeyExportJob::slotStderr);
100 :
101 0 : mProcess->start();
102 0 : if (!mProcess->waitForStarted()) {
103 0 : mError = GpgME::Error::fromCode(GPG_ERR_ENOENT, GPG_ERR_SOURCE_GPGSM); // what else?
104 0 : deleteLater();
105 0 : return mError;
106 : } else {
107 0 : return GpgME::Error();
108 : }
109 : }
110 :
111 0 : void QGpgME::QGpgMESecretKeyExportJob::slotCancel()
112 : {
113 0 : if (mProcess) {
114 0 : mProcess->kill();
115 : }
116 0 : mProcess = 0;
117 0 : mError = GpgME::Error::fromCode(GPG_ERR_CANCELED, GPG_ERR_SOURCE_GPGSM);
118 0 : }
119 :
120 0 : void QGpgME::QGpgMESecretKeyExportJob::slotStdout()
121 : {
122 0 : QString line = QString::fromLocal8Bit(mProcess->readLine());
123 0 : if (!line.isEmpty()) {
124 0 : return;
125 : }
126 0 : const unsigned int oldlen = mKeyData.size();
127 0 : mKeyData.resize(oldlen + line.length());
128 0 : memcpy(mKeyData.data() + oldlen, line.toLatin1(), line.length());
129 : }
130 :
131 0 : void QGpgME::QGpgMESecretKeyExportJob::slotStderr()
132 : {
133 : // implement? or not?
134 0 : }
135 :
136 0 : void QGpgME::QGpgMESecretKeyExportJob::slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus)
137 : {
138 0 : Q_EMIT done();
139 0 : if (!mError &&
140 0 : (exitStatus != QProcess::NormalExit || exitCode != 0)) {
141 0 : mError = GpgME::Error::fromCode(GPG_ERR_GENERAL, GPG_ERR_SOURCE_GPGSM);
142 : }
143 0 : Q_EMIT result(mError, mKeyData);
144 0 : deleteLater();
145 0 : }
146 : #include "qgpgmesecretkeyexportjob.moc"
|