2 qgpgmerefreshkeysjob.cpp
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 by Bundesamt für Sicherheit in der Informationstechnik
7 Software engineering by Intevation GmbH
9 QGpgME is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 QGpgME is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 In addition, as a special exception, the copyright holders give
24 permission to link the code of this program with any edition of
25 the Qt library by Trolltech AS, Norway (or with modified versions
26 of Qt that use the same license as Qt), and distribute linked
27 combinations including the two. You must obey the GNU General
28 Public License in all respects for all of the code used other than
29 Qt. If you modify this file, you may extend this exception to
30 your version of the file, but you are not obligated to do so. If
31 you do not wish to do so, delete this exception statement from
35 #define MAX_CMD_LENGTH 32768
41 #include "qgpgmerefreshkeysjob.h"
44 #include "qgpgme_debug.h"
49 #include <QStringList>
51 #include <gpg-error.h>
55 QGpgME::QGpgMERefreshKeysJob::QGpgMERefreshKeysJob()
56 : RefreshKeysJob(nullptr),
63 QGpgME::QGpgMERefreshKeysJob::~QGpgMERefreshKeysJob()
68 GpgME::Error QGpgME::QGpgMERefreshKeysJob::start(const QStringList &patterns)
70 assert(mPatternsToDo.empty());
72 mPatternsToDo = patterns;
73 if (mPatternsToDo.empty()) {
74 mPatternsToDo.push_back(QStringLiteral(" ")); // empty list means all -> mae
76 // sure to fail the first
77 // startAProcess() guard clause
79 return startAProcess();
82 #if MAX_CMD_LENGTH < 65 + 128
83 #error MAX_CMD_LENGTH is too low
86 GpgME::Error QGpgME::QGpgMERefreshKeysJob::startAProcess()
88 if (mPatternsToDo.empty()) {
89 return GpgME::Error();
91 // create and start gpgsm process:
92 mProcess = new QProcess(this);
93 mProcess->setObjectName(QStringLiteral("gpgsm -k --with-validation --force-crl-refresh --enable-crl-checks"));
95 // FIXME: obbtain the path to gpgsm from gpgme, so we use the same instance.
96 mProcess->setProgram(QStringLiteral("gpgsm"));
97 QStringList arguments;
98 arguments << QStringLiteral("-k")
99 << QStringLiteral("--with-validation")
100 << QStringLiteral("--force-crl-refresh")
101 << QStringLiteral("--enable-crl-checks");
102 unsigned int commandLineLength = MAX_CMD_LENGTH;
104 strlen("gpgsm") + 1 + strlen("-k") + 1 +
105 strlen("--with-validation") + 1 + strlen("--force-crl-refresh") + 1 +
106 strlen("--enable-crl-checks") + 1;
107 while (!mPatternsToDo.empty()) {
108 const QByteArray pat = mPatternsToDo.front().toUtf8().trimmed();
109 const unsigned int patLength = pat.length();
110 if (patLength >= commandLineLength) {
113 mPatternsToDo.pop_front();
117 arguments << QLatin1String(pat);
118 commandLineLength -= patLength + 1;
121 mProcess->setArguments(arguments);
123 connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)),
124 SLOT(slotProcessExited(int,QProcess::ExitStatus)));
125 connect(mProcess, SIGNAL(readyReadStandardOutput()),
127 connect(mProcess, &QProcess::readyReadStandardError,
128 this, &QGpgMERefreshKeysJob::slotStderr);
131 if (!mProcess->waitForStarted()) {
132 mError = GpgME::Error::fromCode(GPG_ERR_ENOENT, GPG_ERR_SOURCE_GPGSM); // what else?
136 return GpgME::Error();
140 void QGpgME::QGpgMERefreshKeysJob::slotCancel()
146 mError = GpgME::Error::fromCode(GPG_ERR_CANCELED, GPG_ERR_SOURCE_GPGSM);
149 void QGpgME::QGpgMERefreshKeysJob::slotStatus(QProcess *proc, const QString &type, const QStringList &args)
151 if (proc != mProcess) {
154 QStringList::const_iterator it = args.begin();
157 if (type == QLatin1String("ERROR")) {
159 if (args.size() < 2) {
160 qCDebug(QGPGME_LOG) << "not recognising ERROR with < 2 args!";
163 const int source = (*++it).toInt(&ok);
165 qCDebug(QGPGME_LOG) << "expected number for first ERROR arg, got something else";
169 const int code = (*++it).toInt(&ok);
171 qCDebug(QGPGME_LOG) << "expected number for second ERROR arg, got something else";
174 mError = GpgME::Error::fromCode(code, source);
176 } else if (type == QLatin1String("PROGRESS")) {
178 if (args.size() < 4) {
179 qCDebug(QGPGME_LOG) << "not recognising PROGRESS with < 4 args!";
182 const QString what = *++it;
186 qCDebug(QGPGME_LOG) << "expected number for \"type\", got something else";
190 const int cur = (*++it).toInt(&ok);
192 qCDebug(QGPGME_LOG) << "expected number for \"cur\", got something else";
196 const int total = (*++it).toInt(&ok);
198 qCDebug(QGPGME_LOG) << "expected number for \"total\", got something else";
202 Q_EMIT progress(QString(), cur, total);
207 void QGpgME::QGpgMERefreshKeysJob::slotStderr()
209 // implement? or not?
212 void QGpgME::QGpgMERefreshKeysJob::slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus)
214 if (!mError && !mPatternsToDo.empty()) {
215 if (const GpgME::Error err = startAProcess()) {
224 (exitStatus != QProcess::NormalExit || exitCode != 0)) {
225 mError = GpgME::Error::fromCode(GPG_ERR_GENERAL, GPG_ERR_SOURCE_GPGSM);
227 Q_EMIT result(mError);
230 #include "qgpgmerefreshkeysjob.moc"