Verified Commit cca5d3e8 authored by Andre Heinecke's avatar Andre Heinecke
Browse files

Extend exportdialogs for subkeys and flags

With latest GPGME this allows users to export
an OpenSSH key for authentication with Kleopatra.
parent ed83d62b
Pipeline #27422 passed with stage
in 14 minutes and 1 second
......@@ -38,6 +38,11 @@
#include <Libkleo/Formatting>
#include <gpgme++/gpgmepp_version.h>
#if GPGMEPP_VERSION >= 0x10E00 // 1.14.0
# define GPGME_HAS_EXPORT_FLAGS
#endif
using namespace Kleo;
class ExportWidget::Private
......@@ -50,8 +55,10 @@ public:
void setupUi();
GpgME::Key key;
GpgME::Subkey subkey;
QTextEdit *textEdit;
WaitWidget *waitWidget;
unsigned int flags;
private:
ExportWidget *const q;
};
......@@ -134,24 +141,53 @@ void ExportWidget::exportResult(const GpgME::Error &err, const QByteArray &data)
d->textEdit->setText(i18nc("%1 is error message", "Failed to export: '%1'", QString::fromLatin1(err.asString())));
}
d->textEdit->setText(injectComments(d->key, data));
if (!d->flags) {
d->textEdit->setText(injectComments(d->key, data));
} else {
d->textEdit->setText(QString::fromUtf8(data));
}
}
void ExportWidget::setKey(const GpgME::Key &key)
void ExportWidget::setKey(const GpgME::Subkey &key, unsigned int flags)
{
d->waitWidget->setVisible(true);
d->textEdit->setVisible(false);
d->key = key.parent();
d->subkey = key;
d->flags = flags;
auto protocol = d->key.protocol() == GpgME::CMS ?
QGpgME::smime() : QGpgME::openpgp();
auto job = protocol->publicKeyExportJob(true);
connect(job, &QGpgME::ExportJob::result,
this, &ExportWidget::exportResult);
#ifdef GPGME_HAS_EXPORT_FLAGS
job->setExportFlags(flags);
#endif
job->start(QStringList() << QLatin1String(key.fingerprint()) + QLatin1Char('!'));
}
void ExportWidget::setKey(const GpgME::Key &key, unsigned int flags)
{
d->waitWidget->setVisible(true);
d->textEdit->setVisible(false);
d->key = key;
d->flags = flags;
auto protocol = key.protocol() == GpgME::CMS ?
QGpgME::smime() : QGpgME::openpgp();
auto job = protocol->publicKeyExportJob(true);
/* New style connect does not work on Windows. */
connect(job, &QGpgME::ExportJob::result,
this, &ExportWidget::exportResult);
#ifdef GPGME_HAS_EXPORT_FLAGS
job->setExportFlags(flags);
#endif
job->start(QStringList() << QLatin1String(key.primaryFingerprint()));
}
......@@ -187,9 +223,14 @@ ExportDialog::~ExportDialog()
dialog.sync();
}
void ExportDialog::setKey(const GpgME::Key &key)
void ExportDialog::setKey(const GpgME::Key &key, unsigned int flags)
{
mWidget->setKey(key, flags);
}
void ExportDialog::setKey(const GpgME::Subkey &key, unsigned int flags)
{
mWidget->setKey(key);
mWidget->setKey(key, flags);
}
GpgME::Key ExportDialog::key() const
......
......@@ -23,6 +23,7 @@
namespace GpgME {
class Key;
class Subkey;
class Error;
}
......@@ -36,7 +37,8 @@ public:
explicit ExportWidget(QWidget *parent = nullptr);
~ExportWidget();
void setKey(const GpgME::Key &key);
void setKey(const GpgME::Key &key, unsigned int flags = 0);
void setKey(const GpgME::Subkey &key, unsigned int flags = 0);
GpgME::Key key() const;
private Q_SLOTS:
......@@ -56,7 +58,8 @@ public:
explicit ExportDialog(QWidget *parent = nullptr);
~ExportDialog();
void setKey(const GpgME::Key &key);
void setKey(const GpgME::Key &key, unsigned int flags = 0);
void setKey(const GpgME::Subkey &key, unsigned int flags = 0);
GpgME::Key key() const;
private:
......
......@@ -24,8 +24,10 @@
#include "commands/keytocardcommand.h"
#include "commands/importpaperkeycommand.h"
#include "exportdialog.h"
#include <gpgme++/key.h>
#include <gpgme++/context.h>
#include <KConfigGroup>
#include <KSharedConfig>
......@@ -34,6 +36,11 @@
#include <QTreeWidgetItem>
#include <QMenu>
#include <gpgme++/gpgmepp_version.h>
#if GPGMEPP_VERSION >= 0x10E00 // 1.14.0
# define GPGME_HAS_EXPORT_FLAGS
#endif
#include <Libkleo/Formatting>
Q_DECLARE_METATYPE(GpgME::Subkey)
......@@ -72,6 +79,19 @@ void SubKeysWidget::Private::tableContextMenuRequested(const QPoint &p)
bool hasActions = false;
#ifdef GPGME_HAS_EXPORT_FLAGS
if (subkey.parent().protocol() && subkey.canAuthenticate()) {
hasActions = true;
menu->addAction(QIcon::fromTheme(QStringLiteral("view-certificate-export")),
i18n("Export OpenSSH key"),
q, [this, subkey]() {
QScopedPointer<ExportDialog> dlg(new ExportDialog(q));
dlg->setKey(subkey, static_cast<unsigned int> (GpgME::Context::ExportSSH));
dlg->exec();
});
}
#endif // GPGME_HAS_EXPORT_FLAGS
if (!subkey.isSecret()) {
hasActions = true;
menu->addAction(QIcon::fromTheme(QStringLiteral("view-certificate-import")),
......
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