CalendarSupport

urihandler.cpp
1/*
2 SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
3 SPDX-FileCopyrightText: 2005 Rafal Rzepecki <divide@users.sourceforge.net>
4
5 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
6*/
7
8#include "urihandler.h"
9
10#include "calendarsupport_debug.h"
11
12#include <KIO/ApplicationLauncherJob>
13#include <KIO/OpenUrlJob>
14#include <KService>
15
16#include <QDesktopServices>
17#include <QUrl>
18#include <QUrlQuery>
19
20using namespace CalendarSupport;
21using namespace Qt::Literals::StringLiterals;
22namespace
23{
24bool startService(const QString &desktopFileName, const QString &uri)
25{
26 const auto service = KService::serviceByDesktopName(desktopFileName);
27 if (!service) {
28 qWarning() << "Desktop file not found:" << desktopFileName << ".desktop -- please check your installation";
29 return false;
30 }
31 auto job = new KIO::ApplicationLauncherJob(service);
32 job->setUrls({QUrl{uri}});
33 QObject::connect(job, &KJob::result, job, [desktopFileName](KJob *job) {
34 if (job->error()) {
35 qCWarning(CALENDARSUPPORT_LOG) << "Failed to start" << desktopFileName << ":" << job->errorText();
36 }
37 });
38 job->start();
39 return true;
40}
41
42bool startKOrganizer(const QString &uri)
43{
44 return startService(QStringLiteral("korganizer-view"), uri);
45}
46
47bool startKMail(const QString &uri)
48{
49 return startService(QStringLiteral("kmail_view"), uri);
50}
51
52bool startKAddressbook(const QString &uri)
53{
54 return startService(QStringLiteral("kaddressbook-view"), uri);
55}
56
57} // namespace
58
60{
61 qCDebug(CALENDARSUPPORT_LOG) << uri;
62
63 if (uri.startsWith("kmail:"_L1)) {
64 // extract 'number' from 'kmail:<number>/<id>'
65 const int start = uri.indexOf(QLatin1Char(':')) + 1;
66 const int end = uri.indexOf(QLatin1Char('/'), start);
67 const QString serialNumberStr = uri.mid(start, start - end);
68 return startKMail(QStringLiteral("akonadi://?item=%1").arg(serialNumberStr));
69 } else if (uri.startsWith("mailto:"_L1)) {
71 } else if (uri.startsWith("uid:"_L1)) {
72 const QString uid = uri.mid(4);
73 return startKAddressbook(QStringLiteral("akonadi://?item=%1").arg(uid));
74 } else if (uri.startsWith("urn:x-ical"_L1)) {
75 const QString uid = QUrl::fromPercentEncoding(uri.toLatin1()).mid(11);
76 return startKOrganizer(QStringLiteral("akonadi://?item=%1").arg(uid));
77 } else if (uri.startsWith("akonadi:"_L1)) {
78 const QString mimeType = QUrlQuery(QUrl(uri)).queryItemValue(QStringLiteral("type")).toLower();
79 if (mimeType == "message/rfc822"_L1) {
80 return startKMail(uri);
81 } else if (mimeType == "text/calendar"_L1) {
82 return startKOrganizer(uri);
83 } else if (mimeType == "text/directory"_L1) {
84 return startKAddressbook(uri);
85 }
86 } else { // no special URI, let KDE handle it
87 auto job = new KIO::OpenUrlJob(QUrl(uri));
88 job->start();
89 }
90
91 return false;
92}
static bool process(const QString &uri)
Process URI (e.g.
int error() const
void result(KJob *job)
virtual Q_SCRIPTABLE void start()=0
QString errorText() const
static Ptr serviceByDesktopName(const QString &_name)
Q_SCRIPTABLE Q_NOREPLY void start()
bool openUrl(const QUrl &url)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString mid(qsizetype position, qsizetype n) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QByteArray toLatin1() const const
QString toLower() const const
QString fromPercentEncoding(const QByteArray &input)
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.