diff --git a/src/commands/exportpaperkeycommand.cpp b/src/commands/exportpaperkeycommand.cpp index a2f180bc8..53cf6b3ae 100644 --- a/src/commands/exportpaperkeycommand.cpp +++ b/src/commands/exportpaperkeycommand.cpp @@ -1,186 +1,182 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/exportpaperkeycommand.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2016 Bundesamt für Sicherheit in der Informationstechnik SPDX-FileContributor: Intevation GmbH SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "exportpaperkeycommand.h" #include #include #include #include #include #include #include #include #include #include #include #include "command_p.h" #include "kleopatra_debug.h" using namespace Kleo; using namespace Kleo::Commands; using namespace GpgME; class ExportPaperKeyCommand::Private : public Command::Private { friend class ::ExportPaperKeyCommand; ExportPaperKeyCommand *q_func() const { return static_cast(q); } public: explicit Private(ExportPaperKeyCommand *qq, KeyListController *c); ~Private() override; void startPaperKey(const QByteArray &data); private: QProcess pkProc; QPointer job; }; ExportPaperKeyCommand::Private::Private(ExportPaperKeyCommand *qq, KeyListController *c) : Command::Private(qq, c) { } ExportPaperKeyCommand::Private::~Private() { } ExportPaperKeyCommand::Private *ExportPaperKeyCommand::d_func() { return static_cast(d.get()); } const ExportPaperKeyCommand::Private *ExportPaperKeyCommand::d_func() const { return static_cast(d.get()); } #define d d_func() #define q q_func() ExportPaperKeyCommand::ExportPaperKeyCommand(QAbstractItemView *v, KeyListController *c) : Command(v, new Private(this, c)) { } void ExportPaperKeyCommand::doStart() { if (paperKeyInstallPath().isNull()) { KMessageBox::error(d->parentWidgetOrView(), xi18nc("@info", "Kleopatra uses " "PaperKey to create a minimized and" " printable version of your secret key." "Please make sure it is installed."), i18nc("@title", "Failed to find PaperKey executable.")); finished(); return; } const auto key = d->key(); if (key.isNull()) { finished(); return; } std::unique_ptr exportJob{QGpgME::openpgp()->secretKeyExportJob(false)}; connect(exportJob.get(), &QGpgME::ExportJob::result, this, [this](const GpgME::Error &err, const QByteArray &keyData) { if (err.isCanceled()) { finished(); return; } if (err) { d->error(xi18nc("@info", "An error occurred during export of the secret key:" "%1", Formatting::errorAsString(err))); finished(); return; } d->startPaperKey(keyData); }); const GpgME::Error err = exportJob->start({QLatin1StringView{key.primaryFingerprint()}}); if (err) { d->error(xi18nc("@info", "An error occurred during export of the secret key:" "%1", Formatting::errorAsString(err))); finished(); return; } d->job = exportJob.release(); } void ExportPaperKeyCommand::Private::startPaperKey(const QByteArray &data) { pkProc.setProgram(paperKeyInstallPath()); pkProc.setArguments({QStringLiteral("--output-type=base16")}); qCDebug(KLEOPATRA_LOG) << "Starting PaperKey process."; pkProc.start(); pkProc.write(data); pkProc.closeWriteChannel(); - q->setAutoDelete(false); pkProc.waitForFinished(); qCDebug(KLEOPATRA_LOG) << "Paperkey export finished: " << pkProc.exitCode() << "status: " << pkProc.exitStatus(); if (pkProc.exitStatus() == QProcess::CrashExit || pkProc.exitCode()) { error(xi18nc("@info", "PaperKey failed with error" "%1", pkProc.errorString())); finished(); - q->deleteLater(); return; } QPrinter printer; const auto key = this->key(); printer.setDocName(QStringLiteral("0x%1-sec").arg(QString::fromLatin1(key.shortKeyID()))); QPrintDialog printDialog(&printer, parentWidgetOrView()); printDialog.setWindowTitle(i18nc("@title:window", "Print Secret Key")); if (printDialog.exec() != QDialog::Accepted) { qCDebug(KLEOPATRA_LOG) << "Printing aborted."; finished(); - q->deleteLater(); return; } QTextDocument doc(QString::fromLatin1(pkProc.readAllStandardOutput())); doc.setDefaultFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); doc.print(&printer); - - q->deleteLater(); + finished(); } void ExportPaperKeyCommand::doCancel() { if (d->job) { d->job->slotCancel(); } d->job.clear(); } #include "moc_exportpaperkeycommand.cpp"