File indexing completed on 2024-06-23 05:13:40

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     commands/exportpaperkeycommand.cpp
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2016 Bundesamt für Sicherheit in der Informationstechnik
0006     SPDX-FileContributor: Intevation GmbH
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include <config-kleopatra.h>
0012 
0013 #include "exportpaperkeycommand.h"
0014 
0015 #include <Libkleo/GnuPG>
0016 
0017 #include <gpgme++/key.h>
0018 
0019 #include <KLocalizedString>
0020 #include <KMessageBox>
0021 
0022 #include <QFontDatabase>
0023 #include <QPrintDialog>
0024 #include <QPrinter>
0025 #include <QTextDocument>
0026 
0027 #include "command_p.h"
0028 #include "kleopatra_debug.h"
0029 
0030 using namespace Kleo;
0031 using namespace Kleo::Commands;
0032 using namespace GpgME;
0033 
0034 ExportPaperKeyCommand::ExportPaperKeyCommand(QAbstractItemView *v, KeyListController *c)
0035     : GnuPGProcessCommand(v, c)
0036     , mParent(v)
0037 {
0038     connect(&mPkProc, &QProcess::finished, this, &ExportPaperKeyCommand::pkProcFinished);
0039     mPkProc.setProgram(paperKeyInstallPath());
0040     mPkProc.setArguments(QStringList() << QStringLiteral("--output-type=base16"));
0041 
0042     process()->setStandardOutputProcess(&mPkProc);
0043     qCDebug(KLEOPATRA_LOG) << "Starting PaperKey process.";
0044     mPkProc.start();
0045     setAutoDelete(false);
0046 }
0047 
0048 QStringList ExportPaperKeyCommand::arguments() const
0049 {
0050     const Key key = d->key();
0051     QStringList result;
0052 
0053     result << gpgPath() << QStringLiteral("--batch");
0054     result << QStringLiteral("--export-secret-key");
0055     result << QLatin1StringView(key.primaryFingerprint());
0056 
0057     return result;
0058 }
0059 
0060 bool ExportPaperKeyCommand::preStartHook(QWidget *parent) const
0061 {
0062     if (paperKeyInstallPath().isNull()) {
0063         KMessageBox::error(parent,
0064                            xi18nc("@info",
0065                                   "<para><application>Kleopatra</application> uses "
0066                                   "<application>PaperKey</application> to create a minimized and"
0067                                   " printable version of your secret key.</para>"
0068                                   "<para>Please make sure it is installed.</para>"),
0069                            i18nc("@title", "Failed to find PaperKey executable."));
0070         return false;
0071     }
0072     return true;
0073 }
0074 
0075 void ExportPaperKeyCommand::pkProcFinished(int code, QProcess::ExitStatus status)
0076 {
0077     qCDebug(KLEOPATRA_LOG) << "Paperkey export finished: " << code << "status: " << status;
0078 
0079     if (status == QProcess::CrashExit || code) {
0080         qCDebug(KLEOPATRA_LOG) << "Aborting because paperkey failed";
0081         deleteLater();
0082         return;
0083     }
0084 
0085     QPrinter printer;
0086 
0087     const Key key = d->key();
0088     printer.setDocName(QStringLiteral("0x%1-sec").arg(QString::fromLatin1(key.shortKeyID())));
0089     QPrintDialog printDialog(&printer, mParent);
0090     printDialog.setWindowTitle(i18nc("@title:window", "Print Secret Key"));
0091 
0092     if (printDialog.exec() != QDialog::Accepted) {
0093         qCDebug(KLEOPATRA_LOG) << "Printing aborted.";
0094         deleteLater();
0095         return;
0096     }
0097 
0098     QTextDocument doc(QString::fromLatin1(mPkProc.readAllStandardOutput()));
0099     doc.setDefaultFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
0100     doc.print(&printer);
0101 
0102     deleteLater();
0103 }
0104 
0105 QString ExportPaperKeyCommand::errorCaption() const
0106 {
0107     return i18nc("@title:window", "Error printing secret key");
0108 }
0109 
0110 QString ExportPaperKeyCommand::crashExitMessage(const QStringList &args) const
0111 {
0112     return xi18nc("@info",
0113                   "<para>The GPG process that tried to export the secret key "
0114                   "ended prematurely because of an unexpected error.</para>"
0115                   "<para>Please check the output of <icode>%1</icode> for details.</para>",
0116                   args.join(QLatin1Char(' ')));
0117 }
0118 
0119 QString ExportPaperKeyCommand::errorExitMessage(const QStringList &args) const
0120 {
0121     return xi18nc("@info",
0122                   "<para>An error occurred while trying to export the secret key.</para> "
0123                   "<para>The output from <command>%1</command> was: <message>%2</message></para>",
0124                   args[0],
0125                   errorString());
0126 }
0127 
0128 #include "moc_exportpaperkeycommand.cpp"