Blame lang/qt/src/qgpgmesecretkeyexportjob.cpp

Packit Service 672cf4
/*
Packit Service 672cf4
    qgpgmesecretexportjob.cpp
Packit Service 672cf4
Packit Service 672cf4
    This file is part of qgpgme, the Qt API binding for gpgme
Packit Service 672cf4
    Copyright (c) 2004 Klarävdalens Datakonsult AB
Packit Service 672cf4
    Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
Packit Service 672cf4
    Software engineering by Intevation GmbH
Packit Service 672cf4
Packit Service 672cf4
    QGpgME is free software; you can redistribute it and/or
Packit Service 672cf4
    modify it under the terms of the GNU General Public License as
Packit Service 672cf4
    published by the Free Software Foundation; either version 2 of the
Packit Service 672cf4
    License, or (at your option) any later version.
Packit Service 672cf4
Packit Service 672cf4
    QGpgME is distributed in the hope that it will be useful,
Packit Service 672cf4
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 672cf4
    General Public License for more details.
Packit Service 672cf4
Packit Service 672cf4
    You should have received a copy of the GNU General Public License
Packit Service 672cf4
    along with this program; if not, write to the Free Software
Packit Service 672cf4
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit Service 672cf4
Packit Service 672cf4
    In addition, as a special exception, the copyright holders give
Packit Service 672cf4
    permission to link the code of this program with any edition of
Packit Service 672cf4
    the Qt library by Trolltech AS, Norway (or with modified versions
Packit Service 672cf4
    of Qt that use the same license as Qt), and distribute linked
Packit Service 672cf4
    combinations including the two.  You must obey the GNU General
Packit Service 672cf4
    Public License in all respects for all of the code used other than
Packit Service 672cf4
    Qt.  If you modify this file, you may extend this exception to
Packit Service 672cf4
    your version of the file, but you are not obligated to do so.  If
Packit Service 672cf4
    you do not wish to do so, delete this exception statement from
Packit Service 672cf4
    your version.
Packit Service 672cf4
*/
Packit Service 672cf4
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
 #include "config.h"
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include "qgpgmesecretkeyexportjob.h"
Packit Service 672cf4
Packit Service 672cf4
#include <QDebug>
Packit Service 672cf4
#include "gpgme_backend_debug.h"
Packit Service 672cf4
Packit Service 672cf4
#include "context.h"
Packit Service 672cf4
#include "data.h"
Packit Service 672cf4
Packit Service 672cf4
#include <QStringList>
Packit Service 672cf4
Packit Service 672cf4
#include <gpg-error.h>
Packit Service 672cf4
Packit Service 672cf4
#include <string.h>
Packit Service 672cf4
#include <assert.h>
Packit Service 672cf4
Packit Service 672cf4
QGpgME::QGpgMESecretKeyExportJob::QGpgMESecretKeyExportJob(bool armour, const QString &charset)
Packit Service 6c01f9
    : ExportJob(0),
Packit Service 6c01f9
      mProcess(0),
Packit Service 672cf4
      mError(0),
Packit Service 672cf4
      mArmour(armour),
Packit Service 672cf4
      mCharset(charset)
Packit Service 672cf4
{
Packit Service 672cf4
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
QGpgME::QGpgMESecretKeyExportJob::~QGpgMESecretKeyExportJob()
Packit Service 672cf4
{
Packit Service 672cf4
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Error QGpgME::QGpgMESecretKeyExportJob::start(const QStringList &patterns)
Packit Service 672cf4
{
Packit Service 672cf4
    assert(mKeyData.isEmpty());
Packit Service 672cf4
Packit Service 672cf4
    if (patterns.size() != 1 || patterns.front().isEmpty()) {
Packit Service 672cf4
        deleteLater();
Packit Service 672cf4
        return mError = GpgME::Error::fromCode(GPG_ERR_INV_VALUE, GPG_ERR_SOURCE_GPGSM);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    // create and start gpgsm process:
Packit Service 672cf4
    mProcess = new QProcess(this);
Packit Service 672cf4
    mProcess->setObjectName(QStringLiteral("gpgsm --export-secret-key-p12"));
Packit Service 672cf4
Packit Service 672cf4
    // FIXME: obtain the path to gpgsm from gpgme, so we use the same instance.
Packit Service 672cf4
    mProcess->setProgram("gpgsm");
Packit Service 672cf4
    QStringList arguments;
Packit Service 672cf4
    arguments << QStringLiteral("--export-secret-key-p12");
Packit Service 672cf4
    if (mArmour) {
Packit Service 672cf4
        arguments << QStringLiteral("--armor");
Packit Service 672cf4
    }
Packit Service 672cf4
    if (!mCharset.isEmpty()) {
Packit Service 672cf4
        arguments << QStringLiteral("--p12-charset") << mCharset;
Packit Service 672cf4
    }
Packit Service 672cf4
    arguments << QLatin1String(patterns.front().toUtf8());
Packit Service 672cf4
Packit Service 672cf4
    mProcess->setArguments(arguments);
Packit Service 672cf4
    connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)),
Packit Service 672cf4
            SLOT(slotProcessExited(int,QProcess::ExitStatus)));
Packit Service 672cf4
    connect(mProcess, &QProcess::readyReadStandardOutput,
Packit Service 672cf4
            this, &QGpgMESecretKeyExportJob::slotStdout);
Packit Service 672cf4
    connect(mProcess, &QProcess::readyReadStandardError,
Packit Service 672cf4
            this, &QGpgMESecretKeyExportJob::slotStderr);
Packit Service 672cf4
Packit Service 672cf4
    mProcess->start();
Packit Service 672cf4
    if (!mProcess->waitForStarted()) {
Packit Service 672cf4
        mError = GpgME::Error::fromCode(GPG_ERR_ENOENT, GPG_ERR_SOURCE_GPGSM);   // what else?
Packit Service 672cf4
        deleteLater();
Packit Service 672cf4
        return mError;
Packit Service 672cf4
    } else {
Packit Service 672cf4
        return GpgME::Error();
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void QGpgME::QGpgMESecretKeyExportJob::slotCancel()
Packit Service 672cf4
{
Packit Service 672cf4
    if (mProcess) {
Packit Service 672cf4
        mProcess->kill();
Packit Service 672cf4
    }
Packit Service 6c01f9
    mProcess = 0;
Packit Service 672cf4
    mError = GpgME::Error::fromCode(GPG_ERR_CANCELED, GPG_ERR_SOURCE_GPGSM);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void QGpgME::QGpgMESecretKeyExportJob::slotStdout()
Packit Service 672cf4
{
Packit Service 672cf4
    QString line = QString::fromLocal8Bit(mProcess->readLine());
Packit Service 672cf4
    if (!line.isEmpty()) {
Packit Service 672cf4
        return;
Packit Service 672cf4
    }
Packit Service 672cf4
    const unsigned int oldlen = mKeyData.size();
Packit Service 672cf4
    mKeyData.resize(oldlen + line.length());
Packit Service 672cf4
    memcpy(mKeyData.data() + oldlen, line.toLatin1(), line.length());
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void QGpgME::QGpgMESecretKeyExportJob::slotStderr()
Packit Service 672cf4
{
Packit Service 672cf4
    // implement? or not?
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void QGpgME::QGpgMESecretKeyExportJob::slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus)
Packit Service 672cf4
{
Packit Service 672cf4
    Q_EMIT done();
Packit Service 672cf4
    if (!mError &&
Packit Service 672cf4
            (exitStatus != QProcess::NormalExit || exitCode != 0)) {
Packit Service 672cf4
        mError = GpgME::Error::fromCode(GPG_ERR_GENERAL, GPG_ERR_SOURCE_GPGSM);
Packit Service 672cf4
    }
Packit Service 672cf4
    Q_EMIT result(mError, mKeyData);
Packit Service 672cf4
    deleteLater();
Packit Service 672cf4
}
Packit Service 672cf4
#include "qgpgmesecretkeyexportjob.moc"