diff --git a/src/commands/exportopenpgpcerttoprovidercommand.cpp b/src/commands/exportopenpgpcerttoprovidercommand.cpp index 2043a072d..dee767f25 100644 --- a/src/commands/exportopenpgpcerttoprovidercommand.cpp +++ b/src/commands/exportopenpgpcerttoprovidercommand.cpp @@ -1,170 +1,162 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/exportopenpgpcerttoprovidercommand.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB SPDX-FileCopyrightText: 2019-2022 Felix Tiede SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "exportopenpgpcerttoprovidercommand.h" #include "command_p.h" #include #include #include #include #include #include #include #include using namespace Kleo; using namespace Kleo::Commands; using namespace GpgME; const KIdentityManagement::IdentityManager *ExportOpenPGPCertToProviderCommand::mailIdManager = new KIdentityManagement::IdentityManager(true); ExportOpenPGPCertToProviderCommand::ExportOpenPGPCertToProviderCommand(QAbstractItemView *v, KeyListController *c) : GnuPGProcessCommand(v, c) { wksMail.open(); wksMail.close(); } ExportOpenPGPCertToProviderCommand::ExportOpenPGPCertToProviderCommand(const UserID &uid) : GnuPGProcessCommand(uid.parent()), uid(uid) { wksMail.open(); wksMail.close(); } ExportOpenPGPCertToProviderCommand::~ExportOpenPGPCertToProviderCommand() {} bool ExportOpenPGPCertToProviderCommand::preStartHook(QWidget *parent) const { - QString sender; - if (uid.isNull()) - sender = QString::fromLatin1(d->keys().at(0).userID(0).addrSpec().data()); - else - sender = QString::fromLatin1(uid.addrSpec().data()); + QString sender = senderAddress(); KIdentityManagement::Identity identity = ExportOpenPGPCertToProviderCommand::mailIdManager->identityForAddress(sender); if (identity.isNull()) identity = ExportOpenPGPCertToProviderCommand::mailIdManager->defaultIdentity(); if (identity.transport().isEmpty()) { KMessageBox::error(parent, xi18nc("@warning", "%1 has no usable transport for mailing a key available, " "WKS upload not possible.", sender), i18nc("@title:window", "OpenPGP Certificate Export")); return false; } return KMessageBox::warningContinueCancel(parent, xi18nc("@info", "Not every mail provider supports WKS, so any key being " "exported this way may fail individually.If exported, " "a confirmation request mail will be sent to %1 " "which needs to be acknowledged with a mail program to complete the " "export process.KMail " "can handle these mails, but not all mail programs can." "Once exported, the standard does not (yet) allow for " "automated removal of a published key." "Are you sure you want to continue?", sender), i18nc("@title:window", "OpenPGP Certificate Export"), KStandardGuiItem::cont(), KStandardGuiItem::cancel(), QStringLiteral("warn-export-openpgp-wks-unsupported")) == KMessageBox::Continue; } void ExportOpenPGPCertToProviderCommand::postSuccessHook(QWidget *parent) { - QString sender; - if (uid.isNull()) - sender = QString::fromLatin1(d->keys().at(0).userID(0).addrSpec().data()); - else - sender = QString::fromLatin1(uid.addrSpec().data()); + QString sender = senderAddress(); KIdentityManagement::Identity identity = ExportOpenPGPCertToProviderCommand::mailIdManager->identityForAddress(sender); if (identity.isNull()) identity = ExportOpenPGPCertToProviderCommand::mailIdManager->defaultIdentity(); MailTransport::Transport *transport = MailTransport::TransportManager::self()->transportByName( identity.transport()); if (!transport) return; wksMail.open(); KMime::Message *msg = new KMime::Message(); msg->setContent(KMime::CRLFtoLF(wksMail.readAll())); msg->parse(); wksMail.close(); MailTransport::MessageQueueJob *job = new MailTransport::MessageQueueJob(parent); job->transportAttribute().setTransportId(transport->id()); job->addressAttribute().setFrom(msg->from()->asUnicodeString()); job->addressAttribute().setTo(msg->to()->displayNames()); job->setMessage(KMime::Message::Ptr(msg)); job->start(); } QStringList ExportOpenPGPCertToProviderCommand::arguments() const { QStringList result; result << gpgWksClientPath(); result << QStringLiteral("--output") << wksMail.fileName(); result << QStringLiteral("--create"); - Q_FOREACH (const Key &key, d->keys()) { - result << QLatin1String(key.primaryFingerprint()); - if (!uid.isNull()) { - result << QLatin1String(uid.addrSpec().data()); - } - else { - result << QLatin1String(key.userID(0).email()); - } - } + result << QString::fromUtf8(d->keys().at(0).primaryFingerprint()); + result << senderAddress(); return result; } QString ExportOpenPGPCertToProviderCommand::errorCaption() const { return i18nc("@title:window", "OpenPGP Certificate Export Error"); } QString ExportOpenPGPCertToProviderCommand::successCaption() const { return i18nc("@title:window", "OpenPGP Certificate Export Finished"); } QString ExportOpenPGPCertToProviderCommand::crashExitMessage(const QStringList &args) const { return xi18nc("@info", "The GPG process that tried to export OpenPGP certificates " "ended prematurely because of an unexpected error." "Please check the output of %1 for details.", args.join(QLatin1Char(' '))); } QString ExportOpenPGPCertToProviderCommand::errorExitMessage(const QStringList &args) const { return xi18nc("@info", "An error occurred while trying to export OpenPGP certificates. " "The output from %1 was: %2", args[0], errorString()); } QString ExportOpenPGPCertToProviderCommand::successMessage(const QStringList&) const { return i18nc("@info", "OpenPGP certificates exported successfully."); } +QString ExportOpenPGPCertToProviderCommand::senderAddress() const +{ + if (uid.isNull()) + return QString::fromUtf8(d->keys().at(0).userID(0).addrSpec().data()); + else + return QString::fromUtf8(uid.addrSpec().data()); +} diff --git a/src/commands/exportopenpgpcerttoprovidercommand.h b/src/commands/exportopenpgpcerttoprovidercommand.h index 7f162f327..a77564360 100644 --- a/src/commands/exportopenpgpcerttoprovidercommand.h +++ b/src/commands/exportopenpgpcerttoprovidercommand.h @@ -1,60 +1,62 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/exportopenpgpcertstoservercommand.h This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB SPDX-FileCopyrightText: 2019 Felix Tiede SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include #include #include #include namespace Kleo { namespace Commands { class ExportOpenPGPCertToProviderCommand : public GnuPGProcessCommand { Q_OBJECT public: explicit ExportOpenPGPCertToProviderCommand(QAbstractItemView *view, KeyListController *parent); explicit ExportOpenPGPCertToProviderCommand(const GpgME::UserID &uid); ~ExportOpenPGPCertToProviderCommand() override; static Restrictions restrictions() { return OnlyOneKey | NeedSecretKey | MustBeOpenPGP; } 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; + QString senderAddress() const; + GpgME::UserID uid; QTemporaryFile wksMail; static const KIdentityManagement::IdentityManager *mailIdManager; }; } }