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 : #include "qgpgmesecretkeyexportjob.h"
35 :
36 : #include <QDebug>
37 : #include "gpgme_backend_debug.h"
38 :
39 : #include "context.h"
40 : #include "data.h"
41 :
42 : #include <QStringList>
43 :
44 : #include <gpg-error.h>
45 :
46 : #include <string.h>
47 : #include <assert.h>
48 :
49 0 : QGpgME::QGpgMESecretKeyExportJob::QGpgMESecretKeyExportJob(bool armour, const QString &charset)
50 : : ExportJob(0),
51 : mProcess(0),
52 : mError(0),
53 : mArmour(armour),
54 0 : mCharset(charset)
55 : {
56 :
57 0 : }
58 :
59 0 : QGpgME::QGpgMESecretKeyExportJob::~QGpgMESecretKeyExportJob()
60 : {
61 :
62 0 : }
63 :
64 0 : GpgME::Error QGpgME::QGpgMESecretKeyExportJob::start(const QStringList &patterns)
65 : {
66 0 : assert(mKeyData.isEmpty());
67 :
68 0 : if (patterns.size() != 1 || patterns.front().isEmpty()) {
69 0 : deleteLater();
70 0 : return mError = GpgME::Error::fromCode(GPG_ERR_INV_VALUE, GPG_ERR_SOURCE_GPGSM);
71 : }
72 :
73 : // create and start gpgsm process:
74 0 : mProcess = new QProcess(this);
75 0 : mProcess->setObjectName(QStringLiteral("gpgsm --export-secret-key-p12"));
76 :
77 : // FIXME: obtain the path to gpgsm from gpgme, so we use the same instance.
78 0 : mProcess->setProgram("gpgsm");
79 0 : QStringList arguments;
80 0 : arguments << QStringLiteral("--export-secret-key-p12");
81 0 : if (mArmour) {
82 0 : arguments << QStringLiteral("--armor");
83 : }
84 0 : if (!mCharset.isEmpty()) {
85 0 : arguments << QStringLiteral("--p12-charset") << mCharset;
86 : }
87 0 : arguments << QLatin1String(patterns.front().toUtf8());
88 :
89 0 : mProcess->setArguments(arguments);
90 : connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)),
91 0 : SLOT(slotProcessExited(int,QProcess::ExitStatus)));
92 : connect(mProcess, &QProcess::readyReadStandardOutput,
93 0 : this, &QGpgMESecretKeyExportJob::slotStdout);
94 : connect(mProcess, &QProcess::readyReadStandardError,
95 0 : this, &QGpgMESecretKeyExportJob::slotStderr);
96 :
97 0 : mProcess->start();
98 0 : if (!mProcess->waitForStarted()) {
99 0 : mError = GpgME::Error::fromCode(GPG_ERR_ENOENT, GPG_ERR_SOURCE_GPGSM); // what else?
100 0 : deleteLater();
101 0 : return mError;
102 : } else {
103 0 : return GpgME::Error();
104 0 : }
105 : }
106 :
107 0 : void QGpgME::QGpgMESecretKeyExportJob::slotCancel()
108 : {
109 0 : if (mProcess) {
110 0 : mProcess->kill();
111 : }
112 0 : mProcess = 0;
113 0 : mError = GpgME::Error::fromCode(GPG_ERR_CANCELED, GPG_ERR_SOURCE_GPGSM);
114 0 : }
115 :
116 0 : void QGpgME::QGpgMESecretKeyExportJob::slotStdout()
117 : {
118 0 : QString line = QString::fromLocal8Bit(mProcess->readLine());
119 0 : if (!line.isEmpty()) {
120 0 : return;
121 : }
122 0 : const unsigned int oldlen = mKeyData.size();
123 0 : mKeyData.resize(oldlen + line.length());
124 0 : memcpy(mKeyData.data() + oldlen, line.toLatin1(), line.length());
125 : }
126 :
127 0 : void QGpgME::QGpgMESecretKeyExportJob::slotStderr()
128 : {
129 : // implement? or not?
130 0 : }
131 :
132 0 : void QGpgME::QGpgMESecretKeyExportJob::slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus)
133 : {
134 0 : Q_EMIT done();
135 0 : if (!mError &&
136 0 : (exitStatus != QProcess::NormalExit || exitCode != 0)) {
137 0 : mError = GpgME::Error::fromCode(GPG_ERR_GENERAL, GPG_ERR_SOURCE_GPGSM);
138 : }
139 0 : Q_EMIT result(mError, mKeyData);
140 0 : deleteLater();
141 0 : }
142 : #include "qgpgmesecretkeyexportjob.moc"
|