diff --git a/src/commands/exportsecretkeycommand_old.cpp b/src/commands/exportsecretkeycommand_old.cpp index 56981a72f..738a5c878 100644 --- a/src/commands/exportsecretkeycommand_old.cpp +++ b/src/commands/exportsecretkeycommand_old.cpp @@ -1,181 +1,171 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/exportsecretkeycommand_old.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "exportsecretkeycommand_old.h" #include "fileoperationspreferences.h" #include "command_p.h" #include #include #include #include #include #include #include #include using namespace Kleo; using namespace Kleo::Commands; using namespace GpgME; namespace Kleo::Commands::Compat { -ExportSecretKeyCommand::ExportSecretKeyCommand(KeyListController *c) - : GnuPGProcessCommand(c) -{ -} - ExportSecretKeyCommand::ExportSecretKeyCommand(QAbstractItemView *v, KeyListController *c) : GnuPGProcessCommand(v, c) { } ExportSecretKeyCommand::ExportSecretKeyCommand(const Key &key) : GnuPGProcessCommand(key) { } ExportSecretKeyCommand::~ExportSecretKeyCommand() {} -void ExportSecretKeyCommand::setFileName(const QString &fileName) -{ - m_filename = fileName; -} - bool ExportSecretKeyCommand::preStartHook(QWidget *parent) const { if (!m_filename.isEmpty()) { return true; } const auto key = d->key(); const auto protocol = key.protocol(); QString proposedFileName; const bool usePGPFileExt = FileOperationsPreferences().usePGPFileExt(); auto name = Formatting::prettyName(key); if (name.isEmpty()) { name = Formatting::prettyEMail(key); } /* Not translated so it's better to use in tutorials etc. */ proposedFileName = QStringLiteral("%1_%2_SECRET.%3").arg(name).arg( Formatting::prettyKeyID(key.shortKeyID())).arg( QString::fromLatin1(outputFileExtension(protocol == OpenPGP ? Class::OpenPGP | Class::Ascii | Class::Certificate : Class::CMS | Class::Binary | Class::ExportedPSM, usePGPFileExt))); m_filename = FileDialog::getSaveFileNameEx(parent ? parent : d->parentWidgetOrView(), i18n("Backup Secret Key"), QStringLiteral("imp"), proposedFileName, protocol == GpgME::OpenPGP ? i18n("Secret Key Files") + QLatin1String(" (*.asc *.gpg *.pgp)") : i18n("Secret Key Files") + QLatin1String(" (*.p12)")); m_armor = m_filename.endsWith (QLatin1String (".asc")); return !m_filename.isEmpty (); } QStringList ExportSecretKeyCommand::arguments() const { const Key key = d->key(); QStringList result; if (key.protocol() == OpenPGP) { result << gpgPath() << QStringLiteral("--batch"); } else { result << gpgSmPath(); } result << QStringLiteral("--yes") << QStringLiteral("--output") << QStringLiteral("-"); if (m_armor) { result << QStringLiteral("--armor"); } if (key.protocol() == CMS) { result << QStringLiteral("--p12-charset") << QStringLiteral("utf-8"); } if (key.protocol() == OpenPGP) { result << QStringLiteral("--export-secret-key"); } else { result << QStringLiteral("--export-secret-key-p12"); } result << QLatin1String(key.primaryFingerprint()); return result; } QString ExportSecretKeyCommand::errorCaption() const { return i18nc("@title:window", "Secret Key Export Error"); } QString ExportSecretKeyCommand::successCaption() const { return i18nc("@title:window", "Secret Key Export Finished"); } QString ExportSecretKeyCommand::crashExitMessage(const QStringList &args) const { return xi18nc("@info", "The GPG or GpgSM process that tried to export the secret key " "ended prematurely because of an unexpected error." "Please check the output of %1 for details.", args.join(QLatin1Char(' '))); } QString ExportSecretKeyCommand::errorExitMessage(const QStringList &args) const { return xi18nc("@info", "An error occurred while trying to export the secret key. " "The output from %1 was: %2", args[0], errorString()); } QString ExportSecretKeyCommand::successMessage(const QStringList &) const { if (mHasError) { return QString(); } return i18nc("@info", "Secret key successfully exported."); } void ExportSecretKeyCommand::postSuccessHook(QWidget *) { Q_ASSERT (process()); const auto data = process()->readAllStandardOutput(); if (!data.size()) { d->error (i18nc("@info", "Possibly bad passphrase given."), errorCaption()); mHasError = true; return; } QFile file(m_filename); /* The filedialog already asked for replace ok. */ file.open(QIODevice::ReadWrite | QIODevice::Truncate); if (file.write(data) != data.size()) { d->error(i18nc("@info", "Failed to write data."), errorCaption()); mHasError = true; } file.close(); } } diff --git a/src/commands/exportsecretkeycommand_old.h b/src/commands/exportsecretkeycommand_old.h index 6c8541108..b15d7c739 100644 --- a/src/commands/exportsecretkeycommand_old.h +++ b/src/commands/exportsecretkeycommand_old.h @@ -1,65 +1,58 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/exportsecretkeycommand_old.h This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include #include namespace Kleo { namespace Commands { namespace Compat { class ExportSecretKeyCommand : public GnuPGProcessCommand { Q_OBJECT public: explicit ExportSecretKeyCommand(QAbstractItemView *view, KeyListController *parent); - explicit ExportSecretKeyCommand(KeyListController *parent); explicit ExportSecretKeyCommand(const GpgME::Key &key); ~ExportSecretKeyCommand() override; - void setFileName(const QString &fileName); - QString fileName() const - { - return m_filename; - } - /* reimp */ static Restrictions restrictions() { return OnlyOneKey | NeedSecretKey; } private: bool preStartHook(QWidget *) const override; void postSuccessHook(QWidget *) override; QStringList arguments() const override; QString errorCaption() const override; QString successCaption() const override; QString crashExitMessage(const QStringList &) const override; QString errorExitMessage(const QStringList &) const override; QString successMessage(const QStringList &) const override; private: mutable QString m_filename; mutable bool m_armor; bool mHasError = false; }; } } }