File indexing completed on 2024-12-29 04:46:20

0001 /*
0002   SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
0003   SPDX-FileCopyrightText: 2005 Rafal Rzepecki <divide@users.sourceforge.net>
0004 
0005   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0006 */
0007 
0008 #include "urihandler.h"
0009 #include "calendarsupport_debug.h"
0010 
0011 #include <KIO/ApplicationLauncherJob>
0012 #include <KIO/OpenUrlJob>
0013 #include <KService>
0014 
0015 #include <QDesktopServices>
0016 #include <QUrl>
0017 #include <QUrlQuery>
0018 
0019 using namespace CalendarSupport;
0020 
0021 namespace
0022 {
0023 bool startService(const QString &desktopFileName, const QString &uri)
0024 {
0025     const auto service = KService::serviceByDesktopName(desktopFileName);
0026     if (!service) {
0027         qWarning() << "Desktop file not found:" << desktopFileName << ".desktop  -- please check your installation";
0028         return false;
0029     }
0030     auto job = new KIO::ApplicationLauncherJob(service);
0031     job->setUrls({QUrl{uri}});
0032     QObject::connect(job, &KJob::result, job, [desktopFileName](KJob *job) {
0033         if (job->error()) {
0034             qCWarning(CALENDARSUPPORT_LOG) << "Failed to start" << desktopFileName << ":" << job->errorText();
0035         }
0036     });
0037     job->start();
0038     return true;
0039 }
0040 
0041 bool startKOrganizer(const QString &uri)
0042 {
0043     return startService(QStringLiteral("korganizer-view"), uri);
0044 }
0045 
0046 bool startKMail(const QString &uri)
0047 {
0048     return startService(QStringLiteral("kmail_view"), uri);
0049 }
0050 
0051 bool startKAddressbook(const QString &uri)
0052 {
0053     return startService(QStringLiteral("kaddressbook-view"), uri);
0054 }
0055 
0056 } // namespace
0057 
0058 bool UriHandler::process(const QString &uri)
0059 {
0060     qCDebug(CALENDARSUPPORT_LOG) << uri;
0061 
0062     if (uri.startsWith(QLatin1StringView("kmail:"))) {
0063         // extract 'number' from 'kmail:<number>/<id>'
0064         const int start = uri.indexOf(QLatin1Char(':')) + 1;
0065         const int end = uri.indexOf(QLatin1Char('/'), start);
0066         const QString serialNumberStr = uri.mid(start, start - end);
0067         return startKMail(QStringLiteral("akonadi://?item=%1").arg(serialNumberStr));
0068     } else if (uri.startsWith(QLatin1StringView("mailto:"))) {
0069         return QDesktopServices::openUrl(QUrl(uri));
0070     } else if (uri.startsWith(QLatin1StringView("uid:"))) {
0071         const QString uid = uri.mid(4);
0072         return startKAddressbook(QStringLiteral("akonadi://?item=%1").arg(uid));
0073     } else if (uri.startsWith(QLatin1StringView("urn:x-ical"))) {
0074         const QString uid = QUrl::fromPercentEncoding(uri.toLatin1()).mid(11);
0075         return startKOrganizer(QStringLiteral("akonadi://?item=%1").arg(uid));
0076     } else if (uri.startsWith(QLatin1StringView("akonadi:"))) {
0077         const QString mimeType = QUrlQuery(QUrl(uri)).queryItemValue(QStringLiteral("type")).toLower();
0078         if (mimeType == QLatin1StringView("message/rfc822")) {
0079             return startKMail(uri);
0080         } else if (mimeType == QLatin1StringView("text/calendar")) {
0081             return startKOrganizer(uri);
0082         } else if (mimeType == QLatin1StringView("text/directory")) {
0083             return startKAddressbook(uri);
0084         }
0085     } else { // no special URI, let KDE handle it
0086         auto job = new KIO::OpenUrlJob(QUrl(uri));
0087         job->start();
0088     }
0089 
0090     return false;
0091 }