Commit df53ad6c authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Use RefreshOpenPGPKeysJob for OpenPGP and show detailed result

GnuPG-bug-id: 5903
parent 6674f375
......@@ -19,8 +19,11 @@
#include <QGpgME/Protocol>
#ifdef QGPGME_SUPPORTS_KEY_REFRESH
#include <QGpgME/RefreshKeysJob>
#include <QGpgME/RefreshOpenPGPKeysJob>
#endif
#include <gpgme++/importresult.h>
#include "kleopatra_debug.h"
using namespace Kleo;
......@@ -41,15 +44,17 @@ public:
void cancel();
#ifdef QGPGME_SUPPORTS_KEY_REFRESH
std::unique_ptr<QGpgME::RefreshKeysJob> startJob();
std::unique_ptr<QGpgME::RefreshOpenPGPKeysJob> startOpenPGPJob();
std::unique_ptr<QGpgME::RefreshKeysJob> startSMIMEJob();
#endif
void onJobResult(const Error &err);
void onOpenPGPJobResult(const ImportResult &result);
void onSMIMEJobResult(const Error &err);
void showError(const Error &err);
private:
Key key;
#ifdef QGPGME_SUPPORTS_KEY_REFRESH
QPointer<QGpgME::RefreshKeysJob> job;
QPointer<QGpgME::Job> job;
#endif
};
......@@ -98,7 +103,17 @@ void RefreshCertificateCommand::Private::start()
}
#ifdef QGPGME_SUPPORTS_KEY_REFRESH
auto refreshJob = startJob();
std::unique_ptr<QGpgME::Job> refreshJob;
switch (key.protocol()) {
case GpgME::OpenPGP:
refreshJob = startOpenPGPJob();
break;
case GpgME::CMS:
refreshJob = startSMIMEJob();
break;
default:
; // cannot happen ;-)
}
if (!refreshJob) {
finished();
return;
......@@ -121,16 +136,36 @@ void RefreshCertificateCommand::Private::cancel()
}
#ifdef QGPGME_SUPPORTS_KEY_REFRESH
std::unique_ptr<QGpgME::RefreshKeysJob> RefreshCertificateCommand::Private::startJob()
std::unique_ptr<QGpgME::RefreshOpenPGPKeysJob> RefreshCertificateCommand::Private::startOpenPGPJob()
{
std::unique_ptr<QGpgME::RefreshOpenPGPKeysJob> refreshJob{QGpgME::openpgp()->refreshOpenPGPKeysJob()};
Q_ASSERT(refreshJob);
connect(refreshJob.get(), &QGpgME::RefreshOpenPGPKeysJob::result,
q, [this](const GpgME::ImportResult &result) {
onOpenPGPJobResult(result);
});
connect(refreshJob.get(), &QGpgME::Job::progress,
q, &Command::progress);
const GpgME::Error err = refreshJob->start({key});
if (err) {
showError(err);
return {};
}
Q_EMIT q->info(i18nc("@info:status", "Refreshing key..."));
return refreshJob;
}
std::unique_ptr<QGpgME::RefreshKeysJob> RefreshCertificateCommand::Private::startSMIMEJob()
{
auto jobFactory = key.protocol() == GpgME::OpenPGP ? QGpgME::openpgp() : QGpgME::smime();
Q_ASSERT(jobFactory);
std::unique_ptr<QGpgME::RefreshKeysJob> refreshJob{jobFactory->refreshKeysJob()};
std::unique_ptr<QGpgME::RefreshKeysJob> refreshJob{QGpgME::smime()->refreshKeysJob()};
Q_ASSERT(refreshJob);
connect(refreshJob.get(), &QGpgME::RefreshKeysJob::result,
q, [this](const GpgME::Error &err) {
onJobResult(err);
onSMIMEJobResult(err);
});
connect(refreshJob.get(), &QGpgME::Job::progress,
q, &Command::progress);
......@@ -146,7 +181,72 @@ std::unique_ptr<QGpgME::RefreshKeysJob> RefreshCertificateCommand::Private::star
}
#endif
void RefreshCertificateCommand::Private::onJobResult(const Error &err)
namespace
{
static auto informationOnChanges(const ImportResult &result)
{
QString text;
// if additional keys have been retrieved via WKD, then most of the below
// details are just a guess and may concern the additional keys instead of
// the refresh keys; this could only be clarified by a thorough comparison of
// unrefreshed and refreshed key
if (result.numUnchanged() == result.numConsidered()) {
// if numUnchanged < numConsidered, then it is not clear whether the refreshed key
// hasn't changed or whether another key retrieved via WKD hasn't changed
text = i18n("The key hasn't changed.");
} else if (result.newRevocations() > 0) {
// it is possible that a revoked key has been newly imported via WKD,
// but it is much more likely that the refreshed key was revoked
text = i18n("The key has been revoked.");
} else {
// it doesn't make much sense to list below details if the key has been revoked
text = i18n("The key has been updated.");
QStringList details;
if (result.newUserIDs() > 0) {
details.push_back(i18n("New user IDs: %1", result.newUserIDs()));
}
if (result.newSubkeys() > 0) {
details.push_back(i18n("New subkeys: %1", result.newSubkeys()));
}
if (result.newSignatures() > 0) {
details.push_back(i18n("New signatures: %1", result.newSignatures()));
}
if (!details.empty()) {
text += QLatin1String{"<br><br>"} + details.join(QLatin1String{"<br>"});
}
}
text = QLatin1String{"<p>"} + text + QLatin1String{"</p>"};
if (result.numImported() > 0) {
text += QLatin1String{"<p>"} + i18np("Additionally, one new key has been retrieved.",
"Additionally, %1 new keys have been retrieved.",
result.numImported()) + QLatin1String{"</p>"};
}
return text;
}
}
void RefreshCertificateCommand::Private::onOpenPGPJobResult(const ImportResult &result)
{
if (result.error()) {
showError(result.error());
finished();
return;
}
if (!result.error().isCanceled()) {
information(informationOnChanges(result),
i18nc("@title:window", "Key Refreshed"));
}
finished();
}
void RefreshCertificateCommand::Private::onSMIMEJobResult(const Error &err)
{
if (err) {
showError(err);
......
Supports Markdown
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