Simplify secret key export

* Do not ask for "ASCII Armor" as users do not know what this means.
* Do not ask for passphrase-charset for S/MIME we use utf-8.
* Just use a file dialog with a default name.

This should make the secret key export a bit smoother. Especially
right after the keygen when we ask to save the key for a backup
we do not want to offer confusing options afterward.

GnuPG-Bug-Id: T4089
parent 8cec2258
......@@ -112,7 +112,6 @@ set(_kleopatra_SRCS
dialogs/ownertrustdialog.cpp
dialogs/selftestdialog.cpp
dialogs/certifycertificatedialog.cpp
dialogs/exportsecretkeydialog.cpp
dialogs/adduseriddialog.cpp
dialogs/exportcertificatesdialog.cpp
dialogs/deletecertificatesdialog.cpp
......@@ -264,7 +263,6 @@ ki18n_wrap_ui(_kleopatra_SRCS
dialogs/ownertrustdialog.ui
dialogs/selectchecklevelwidget.ui
dialogs/selftestdialog.ui
dialogs/exportsecretkeydialog.ui
dialogs/adduseriddialog.ui
dialogs/setinitialpindialog.ui
dialogs/certificatedetailswidget.ui
......
......@@ -34,11 +34,14 @@
#include "exportsecretkeycommand.h"
#include "command_p.h"
#include "fileoperationspreferences.h"
#include <dialogs/exportsecretkeydialog.h>
#include "command_p.h"
#include <utils/gnupg-helper.h>
#include <utils/filedialog.h>
#include <Libkleo/Classify>
#include <gpgme++/key.h>
......@@ -46,7 +49,6 @@
using namespace Kleo;
using namespace Kleo::Commands;
using namespace Kleo::Dialogs;
using namespace GpgME;
ExportSecretKeyCommand::ExportSecretKeyCommand(KeyListController *c)
......@@ -71,33 +73,37 @@ void ExportSecretKeyCommand::setFileName(const QString &fileName)
m_filename = fileName;
}
void ExportSecretKeyCommand::setPassphraseCharset(const QByteArray &charset)
{
m_charset = charset;
}
void ExportSecretKeyCommand::setUseArmor(bool armor)
{
m_armor = armor;
}
bool ExportSecretKeyCommand::preStartHook(QWidget *parent) const
{
if (!m_filename.isEmpty()) {
return true;
}
ExportSecretKeyDialog dlg(parent);
dlg.setKey(d->key());
if (!dlg.exec()) {
return false;
}
const auto key = d->key();
const auto protocol = key.protocol();
QString proposedFileName;
const bool usePGPFileExt = FileOperationsPreferences().usePGPFileExt();
proposedFileName
= QString::fromLatin1(key.primaryFingerprint())
+ QLatin1Char('.')
+ 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("Export Secret Key"),
QStringLiteral("imp"),
proposedFileName,
protocol == GpgME::OpenPGP
? i18n("Secret Key Files") + QLatin1String(" (*.asc *.gpg *.pgp)")
: i18n("Secret Key Files") + QLatin1String(" (*.p12)"));
m_filename = dlg.fileName();
m_armor = dlg.useArmor();
m_charset = dlg.charset();
m_armor = m_filename.endsWith (QLatin1String (".asc"));
return true;
return !m_filename.isEmpty ();
}
QStringList ExportSecretKeyCommand::arguments() const
......@@ -117,8 +123,8 @@ QStringList ExportSecretKeyCommand::arguments() const
result << QStringLiteral("--armor");
}
if (key.protocol() == CMS && !m_charset.isEmpty()) {
result << QStringLiteral("--p12-charset") << QLatin1String(m_charset);
if (key.protocol() == CMS) {
result << QStringLiteral("--p12-charset") << QLatin1String("utf-8");
}
if (key.protocol() == OpenPGP) {
......
......@@ -58,18 +58,6 @@ public:
return m_filename;
}
void setPassphraseCharset(const QByteArray &charset);
QByteArray passphraseCharset() const
{
return m_charset;
}
void setUseArmor(bool armor);
bool useArmor() const
{
return m_armor;
}
/* reimp */ static Restrictions restrictions()
{
return OnlyOneKey | NeedSecretKey;
......@@ -89,7 +77,6 @@ private:
private:
mutable QString m_filename;
mutable QByteArray m_charset;
mutable bool m_armor;
};
......
/* -*- mode: c++; c-basic-offset:4 -*-
dialogs/exportsecretkeydialog.cpp
This file is part of Kleopatra, the KDE keymanager
Copyright (c) 2008 Klarälvdalens Datakonsult AB
Kleopatra is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include <config-kleopatra.h>
#include "exportsecretkeydialog.h"
#include "ui_exportsecretkeydialog.h"
#include <Libkleo/Formatting>
#include <gpgme++/key.h>
#include <KMessageBox>
#include <KLocalizedString>
using namespace Kleo;
using namespace Kleo::Dialogs;
using namespace GpgME;
// This comes from gnupg's sources, agent/minip12.c
// In fact, any charset supported by iconv would work, but we don't link to iconv directly...
static const char *charsets[] = {
"utf8",
"iso-8859-1",
"iso-8859-15",
"iso-8859-2",
"iso-8859-3",
"iso-8859-4",
"iso-8859-5",
"iso-8859-6",
"iso-8859-7",
"iso-8859-8",
"iso-8859-9",
"koi8-r",
"ibm437",
"ibm850",
"euc-jp",
"big5",
};
static const unsigned int numCharsets = sizeof charsets / sizeof * charsets;
class ExportSecretKeyDialog::Private
{
friend class ::Kleo::Dialogs::ExportSecretKeyDialog;
ExportSecretKeyDialog *const q;
public:
explicit Private(ExportSecretKeyDialog *qq)
: q(qq),
ui(q)
{
}
private:
void updateWidgets()
{
const bool x509 = key.protocol() == CMS;
ui.charsetCB->setVisible(x509);
ui.charsetLB->setVisible(x509);
}
void updateFileName()
{
const bool x509 = key.protocol() == CMS;
const bool armor = q->useArmor();
static const char *extensions[] = {
".gpg", ".asc", ".p12", ".pem"
};
const unsigned int idx = 2 * x509 + armor;
const char *const extension = extensions[idx];
const QString nf = i18n("Secret Key Files") + QStringLiteral("(*%1 *%2 *%3 *%4 *.pgp)")
.arg(QLatin1String(extensions[idx]), QLatin1String(extensions[(idx + 1) % 4]), QLatin1String(extensions[(idx + 2) % 4]), QLatin1String(extensions[(idx + 3) % 4]));
ui.outputFileFR->setNameFilter(nf);
QString fn = q->fileName();
if (fn.isEmpty()) {
return;
}
bool found = false;
for (unsigned int i = 0; i < sizeof extensions / sizeof * extensions; ++i)
if (fn.endsWith(QLatin1String(extensions[i]), Qt::CaseInsensitive)) {
fn.chop(4);
found = true;
break;
}
if (found) {
q->setFileName(fn + QLatin1String(extension));
}
}
void updateLabel()
{
ui.descriptionLB->setText(i18nc("@info",
"Please select export options for %1:",
Formatting::formatForComboBox(key)));
}
private:
Key key;
struct UI : public Ui_ExportSecretKeyDialog {
explicit UI(Dialogs::ExportSecretKeyDialog *qq)
: Ui_ExportSecretKeyDialog()
{
setupUi(qq);
outputFileFR->setExistingOnly(false);
outputFileFR->setFilter(QDir::Files);
outputFileFR->setNameFilter(i18n("Secret Key Files (*.pem *.p12 *.gpg *.asc *.pgp)"));
for (unsigned int i = 0; i < numCharsets; ++i) {
charsetCB->addItem(QString::fromLatin1(charsets[i]));
}
charsetCB->setCurrentIndex(0);
}
} ui;
};
ExportSecretKeyDialog::ExportSecretKeyDialog(QWidget *p)
: QDialog(p), d(new Private(this))
{
}
ExportSecretKeyDialog::~ExportSecretKeyDialog() {}
void ExportSecretKeyDialog::setKey(const Key &key)
{
if (qstricmp(key.primaryFingerprint(), d->key.primaryFingerprint()) == 0) {
return;
}
d->key = key;
d->updateWidgets();
d->updateLabel();
d->updateFileName();
}
Key ExportSecretKeyDialog::key() const
{
return d->key;
}
void ExportSecretKeyDialog::setFileName(const QString &fileName)
{
d->ui.outputFileFR->setFileName(fileName);
}
QString ExportSecretKeyDialog::fileName() const
{
return d->ui.outputFileFR->fileName();
}
void ExportSecretKeyDialog::setCharset(const QByteArray &charset)
{
for (unsigned int i = 0; i < sizeof charsets / sizeof * charsets; ++i)
if (charset == charsets[i]) {
d->ui.charsetCB->setCurrentIndex(static_cast<int>(i));
return;
}
}
QByteArray ExportSecretKeyDialog::charset() const
{
if (d->ui.charsetCB->isVisible()) {
return d->ui.charsetCB->currentText().toLatin1();
} else {
return QByteArray();
}
}
void ExportSecretKeyDialog::setUseArmor(bool on)
{
d->ui.armorCB->setChecked(on);
}
bool ExportSecretKeyDialog::useArmor() const
{
return d->ui.armorCB->isChecked();
}
void ExportSecretKeyDialog::accept()
{
d->updateFileName();
const QString fn = fileName();
if (fn.isEmpty()) {
KMessageBox::information(this, i18nc("@info",
"You have to enter an output filename."),
i18nc("@title", "Incomplete data"));
d->ui.outputFileFR->setFocus();
return;
}
const QByteArray cs = charset();
if (d->key.protocol() == CMS && cs.isEmpty()) {
KMessageBox::information(this, i18nc("@info",
"You have to choose a passphrase character set."),
i18nc("@title", "Incomplete data"));
d->ui.charsetCB->setFocus();
return;
}
QDialog::accept();
}
#include "moc_exportsecretkeydialog.cpp"
/* -*- mode: c++; c-basic-offset:4 -*-
dialogs/exportsecretkeydialog.h
This file is part of Kleopatra, the KDE keymanager
Copyright (c) 2008 Klarälvdalens Datakonsult AB
Kleopatra is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#ifndef __KLEOPATRA_DIALOGS_EXPORTSECRETKEYDIALOG_H__
#define __KLEOPATRA_DIALOGS_EXPORTSECRETKEYDIALOG_H__
#include <QDialog>
#include <utils/pimpl_ptr.h>
namespace GpgME
{
class Key;
}
namespace Kleo
{
namespace Dialogs
{
class ExportSecretKeyDialog : public QDialog
{
Q_OBJECT
public:
explicit ExportSecretKeyDialog(QWidget *parent = nullptr);
~ExportSecretKeyDialog() override;
void setKey(const GpgME::Key &key);
GpgME::Key key() const;
void setFileName(const QString &fileName);
QString fileName() const;
void setCharset(const QByteArray &charset);
QByteArray charset() const;
void setUseArmor(bool armor);
bool useArmor() const;
protected Q_SLOTS:
void accept() override;
private:
class Private;
kdtools::pimpl_ptr<Private> d;
Q_PRIVATE_SLOT(d, void updateFileName())
};
}
}
#endif /* __KLEOPATRA_DIALOGS_EXPORTSECRETKEYDIALOG_H__ */
<ui version="4.0" >
<class>ExportSecretKeyDialog</class>
<widget class="QDialog" name="ExportSecretKeyDialog" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>372</width>
<height>194</height>
</rect>
</property>
<property name="windowTitle" >
<string>Export Secret Certificate</string>
</property>
<layout class="QGridLayout" name="gridLayout" >
<item row="0" column="0" colspan="2" >
<widget class="QLabel" name="descriptionLB" >
<property name="wordWrap" >
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0" >
<widget class="QLabel" name="outputFileLB" >
<property name="text" >
<string>Output file:</string>
</property>
<property name="buddy" >
<cstring>outputFileFR</cstring>
</property>
</widget>
</item>
<item row="3" column="0" >
<widget class="QLabel" name="charsetLB" >
<property name="text" >
<string>Passphrase charset:</string>
</property>
<property name="buddy" >
<cstring>charsetCB</cstring>
</property>
</widget>
</item>
<item row="3" column="1" >
<widget class="QComboBox" name="charsetCB" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Fixed" hsizetype="MinimumExpanding" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2" >
<widget class="QDialogButtonBox" name="buttonBox" >
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons" >
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2" >
<widget class="QCheckBox" name="armorCB" >
<property name="text" >
<string>ASCII armor</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2" >
<spacer name="verticalSpacer" >
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0" >
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1" >
<widget class="Kleo::FileNameRequester" native="1" name="outputFileFR" />
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>Kleo::FileNameRequester</class>
<extends>QWidget</extends>
<header>libkleo/filenamerequester.h</header>
<container>1</container>
<slots>
<signal>fileNameChanged(QString)</signal>
</slots>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ExportSecretKeyDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel" >
<x>227</x>
<y>165</y>
</hint>
<hint type="destinationlabel" >
<x>157</x>
<y>193</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>ExportSecretKeyDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel" >
<x>295</x>
<y>171</y>
</hint>
<hint type="destinationlabel" >
<x>286</x>
<y>193</y>
</hint>
</hints>
</connection>
<connection>
<sender>armorCB</sender>
<signal>toggled(bool)</signal>
<receiver>ExportSecretKeyDialog</receiver>
<slot>updateFileName()</slot>
<hints>
<hint type="sourcelabel" >
<x>77</x>
<y>83</y>
</hint>
<hint type="destinationlabel" >
<x>69</x>
<y>131</y>
</hint>
</hints>
</connection>
<connection>
<sender>outputFileFR</sender>
<signal>fileNameChanged(QString)</signal>
<receiver>ExportSecretKeyDialog</receiver>
<slot>updateFileName()</slot>
<hints>
<hint type="sourcelabel" >
<x>267</x>
<y>53</y>
</hint>
<hint type="destinationlabel" >
<x>159</x>
<y>131</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>updateFileName()</slot>
</slots>
</ui>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment