File indexing completed on 2024-12-22 05:05:21

0001 // SPDX-FileCopyrightText: 2023 g10 Code GmbH
0002 // SPDX-FileContributor: Carl Schwan <carl.schwan@gnupg.com>
0003 // SPDX-License-Identifier: LGPL-2.0-or-later
0004 
0005 #include "urlhandler_p.h"
0006 
0007 #include "mimetreeparser_widgets_debug.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KMessageBox>
0011 
0012 #include <QCoreApplication>
0013 #include <QProcess>
0014 #include <QStandardPaths>
0015 #include <QUrl>
0016 
0017 UrlHandler::UrlHandler(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 bool UrlHandler::handleClick(const QUrl &url, QWindow *window)
0023 {
0024     if (!url.hasFragment()) {
0025         return false;
0026     }
0027     QString displayName;
0028     QString libName;
0029     QString keyId;
0030     if (!foundSMIMEData(url.path() + QLatin1Char('#') + QUrl::fromPercentEncoding(url.fragment().toLatin1()), displayName, libName, keyId)) {
0031         return false;
0032     }
0033     QStringList lst;
0034     lst << QStringLiteral("--parent-windowid") << QString::number(static_cast<qlonglong>(window->winId())) << QStringLiteral("--query") << keyId;
0035 #ifdef Q_OS_WIN
0036     QString exec = QStandardPaths::findExecutable(QStringLiteral("kleopatra.exe"), {QCoreApplication::applicationDirPath()});
0037     if (exec.isEmpty()) {
0038         exec = QStandardPaths::findExecutable(QStringLiteral("kleopatra.exe"));
0039     }
0040 #else
0041     const QString exec = QStandardPaths::findExecutable(QStringLiteral("kleopatra"));
0042 #endif
0043     if (exec.isEmpty()) {
0044         qCWarning(MIMETREEPARSER_WIDGET_LOG) << "Could not find kleopatra executable in PATH";
0045         KMessageBox::errorWId(window->winId(),
0046                               i18n("Could not start certificate manager. "
0047                                    "Please check your installation."),
0048                               i18n("KMail Error"));
0049         return false;
0050     }
0051     QProcess::startDetached(exec, lst);
0052     return true;
0053 }
0054 
0055 bool UrlHandler::foundSMIMEData(const QString &aUrl, QString &displayName, QString &libName, QString &keyId)
0056 {
0057     static QString showCertMan(QStringLiteral("showCertificate#"));
0058     displayName.clear();
0059     libName.clear();
0060     keyId.clear();
0061     int i1 = aUrl.indexOf(showCertMan);
0062     if (-1 < i1) {
0063         i1 += showCertMan.length();
0064         int i2 = aUrl.indexOf(QLatin1StringView(" ### "), i1);
0065         if (i1 < i2) {
0066             displayName = aUrl.mid(i1, i2 - i1);
0067             i1 = i2 + 5;
0068             i2 = aUrl.indexOf(QLatin1StringView(" ### "), i1);
0069             if (i1 < i2) {
0070                 libName = aUrl.mid(i1, i2 - i1);
0071                 i2 += 5;
0072 
0073                 keyId = aUrl.mid(i2);
0074             }
0075         }
0076     }
0077     return !keyId.isEmpty();
0078 }