File indexing completed on 2025-01-19 04:46:50

0001 /*
0002   SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "pgpkeyurlhandler.h"
0008 
0009 #include <QString>
0010 #include <QUrlQuery>
0011 
0012 #include <KLocalizedString>
0013 
0014 #include <MessageViewer/Viewer>
0015 #include <MimeTreeParser/BodyPart>
0016 #include <MimeTreeParser/NodeHelper>
0017 
0018 #include <QGpgME/ImportJob>
0019 #include <QGpgME/Protocol>
0020 #include <gpgme++/error.h>
0021 #include <gpgme++/importresult.h>
0022 
0023 #include <KMessageBox>
0024 
0025 using namespace MimeTreeParser::Interface;
0026 
0027 QUrlQuery ApplicationPgpKeyUrlHandler::decodePath(const QString &path) const
0028 {
0029     if (!path.startsWith(QLatin1StringView("pgpkey?"))) {
0030         return {};
0031     }
0032 
0033     return QUrlQuery(path.mid(sizeof("pgpkey?") - 1));
0034 }
0035 
0036 bool ApplicationPgpKeyUrlHandler::handleContextMenuRequest(BodyPart *, const QString &, const QPoint &) const
0037 {
0038     return false;
0039 }
0040 
0041 QString ApplicationPgpKeyUrlHandler::statusBarMessage(BodyPart *part, const QString &path) const
0042 {
0043     Q_UNUSED(part)
0044     const QUrlQuery q = decodePath(path);
0045     if (q.queryItemValue(QStringLiteral("action")) == QLatin1StringView("import")) {
0046         return i18n("Import the key");
0047     }
0048 
0049     return {};
0050 }
0051 
0052 QString ApplicationPgpKeyUrlHandler::name() const
0053 {
0054     return QStringLiteral("ApplicationPgpKeyUrlHandler");
0055 }
0056 
0057 bool ApplicationPgpKeyUrlHandler::handleClick(MessageViewer::Viewer *v, BodyPart *part, const QString &path) const
0058 {
0059     const QUrlQuery q = decodePath(path);
0060     if (q.queryItemValue(QStringLiteral("action")) == QLatin1StringView("import")) {
0061         auto job = QGpgME::openpgp()->importJob();
0062         auto res = job->exec(part->content()->decodedContent());
0063         if (res.error()) {
0064             KMessageBox::detailedError(v,
0065                                        i18n("An error occurred while importing the key."),
0066                                        QString::fromUtf8(res.error().asString()),
0067                                        i18nc("@title:window", "Import error"));
0068         } else if (res.numConsidered() == 0) {
0069             KMessageBox::error(v, i18n("No keys to import where found."), i18nc("@title:window", "Import error"));
0070         } else {
0071             KMessageBox::information(v, i18n("The key has been successfully imported."), i18nc("@title:window", "Import finished"));
0072         }
0073 
0074         return true;
0075     }
0076 
0077     return false;
0078 }