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 @@ ...@@ -38,6 +38,11 @@
#include <Libkleo/Formatting> #include <Libkleo/Formatting>
#include <gpgme++/gpgmepp_version.h>
#if GPGMEPP_VERSION >= 0x10E00 // 1.14.0
# define GPGME_HAS_EXPORT_FLAGS
#endif
using namespace Kleo; using namespace Kleo;
class ExportWidget::Private class ExportWidget::Private
...@@ -50,8 +55,10 @@ public: ...@@ -50,8 +55,10 @@ public:
void setupUi(); void setupUi();
GpgME::Key key; GpgME::Key key;
GpgME::Subkey subkey;
QTextEdit *textEdit; QTextEdit *textEdit;
WaitWidget *waitWidget; WaitWidget *waitWidget;
unsigned int flags;
private: private:
ExportWidget *const q; ExportWidget *const q;
}; };
...@@ -134,24 +141,53 @@ void ExportWidget::exportResult(const GpgME::Error &err, const QByteArray &data) ...@@ -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(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->waitWidget->setVisible(true);
d->textEdit->setVisible(false); d->textEdit->setVisible(false);
d->key = key; d->key = key;
d->flags = flags;
auto protocol = key.protocol() == GpgME::CMS ? auto protocol = key.protocol() == GpgME::CMS ?
QGpgME::smime() : QGpgME::openpgp(); QGpgME::smime() : QGpgME::openpgp();
auto job = protocol->publicKeyExportJob(true); auto job = protocol->publicKeyExportJob(true);
/* New style connect does not work on Windows. */
connect(job, &QGpgME::ExportJob::result, connect(job, &QGpgME::ExportJob::result,
this, &ExportWidget::exportResult); this, &ExportWidget::exportResult);
#ifdef GPGME_HAS_EXPORT_FLAGS
job->setExportFlags(flags);
#endif
job->start(QStringList() << QLatin1String(key.primaryFingerprint())); job->start(QStringList() << QLatin1String(key.primaryFingerprint()));
} }
...@@ -187,9 +223,14 @@ ExportDialog::~ExportDialog() ...@@ -187,9 +223,14 @@ ExportDialog::~ExportDialog()
dialog.sync(); 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 GpgME::Key ExportDialog::key() const
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
namespace GpgME { namespace GpgME {
class Key; class Key;
class Subkey;
class Error; class Error;
} }
...@@ -36,7 +37,8 @@ public: ...@@ -36,7 +37,8 @@ public:
explicit ExportWidget(QWidget *parent = nullptr); explicit ExportWidget(QWidget *parent = nullptr);
~ExportWidget(); ~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; GpgME::Key key() const;
private Q_SLOTS: private Q_SLOTS:
...@@ -56,7 +58,8 @@ public: ...@@ -56,7 +58,8 @@ public:
explicit ExportDialog(QWidget *parent = nullptr); explicit ExportDialog(QWidget *parent = nullptr);
~ExportDialog(); ~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; GpgME::Key key() const;
private: private:
......
...@@ -24,8 +24,10 @@ ...@@ -24,8 +24,10 @@
#include "commands/keytocardcommand.h" #include "commands/keytocardcommand.h"
#include "commands/importpaperkeycommand.h" #include "commands/importpaperkeycommand.h"
#include "exportdialog.h"
#include <gpgme++/key.h> #include <gpgme++/key.h>
#include <gpgme++/context.h>
#include <KConfigGroup> #include <KConfigGroup>
#include <KSharedConfig> #include <KSharedConfig>
...@@ -34,6 +36,11 @@ ...@@ -34,6 +36,11 @@
#include <QTreeWidgetItem> #include <QTreeWidgetItem>
#include <QMenu> #include <QMenu>
#include <gpgme++/gpgmepp_version.h>
#if GPGMEPP_VERSION >= 0x10E00 // 1.14.0
# define GPGME_HAS_EXPORT_FLAGS
#endif
#include <Libkleo/Formatting> #include <Libkleo/Formatting>
Q_DECLARE_METATYPE(GpgME::Subkey) Q_DECLARE_METATYPE(GpgME::Subkey)
...@@ -72,6 +79,19 @@ void SubKeysWidget::Private::tableContextMenuRequested(const QPoint &p) ...@@ -72,6 +79,19 @@ void SubKeysWidget::Private::tableContextMenuRequested(const QPoint &p)
bool hasActions = false; 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()) { if (!subkey.isSecret()) {
hasActions = true; hasActions = true;
menu->addAction(QIcon::fromTheme(QStringLiteral("view-certificate-import")), 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