Verified Commit e5cc992f authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

Import UriHandler code from KdepimDBusInterfaces

Summary:
The code has been largely adapted and improved to better handle all URLs
and to not rely on DBus interfaces and/or directly linking against some
other PIM libraries. Instead, invocation happens through
KRun using the "view" services of KMail, KAddressbook and KOrganizer.

The code is used by calendaring apps to handle opening links to attachments.

Reviewers: #kde_pim, winterz

Reviewed By: #kde_pim, winterz

Subscribers: winterz, kde-pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D29543
parent 4d7520fc
cmake_minimum_required(VERSION 3.5)
set(PIM_VERSION "5.14.41")
set(PIM_VERSION "5.14.42")
project(calendarsupport VERSION ${PIM_VERSION})
......@@ -25,7 +25,6 @@ include(ECMAddTests)
set(CALENDARSUPPORT_LIB_VERSION ${PIM_VERSION})
set(AKONADI_MIMELIB_VERSION "5.14.40")
set(KDEPIM_LIB_VERSION "5.14.40")
set(QT_REQUIRED_VERSION "5.12.0")
set(KMIME_LIB_VERSION "5.14.40")
set(CALENDARUTILS_LIB_VERSION "5.14.40")
......@@ -48,7 +47,6 @@ find_package(KF5IdentityManagement ${IDENTITYMANAGEMENT_LIB_VERSION} CONFIG REQU
find_package(KF5Holidays ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(KF5AkonadiCalendar ${AKONADICALENDAR_LIB_VERSION} CONFIG REQUIRED)
find_package(KF5PimCommon ${PIMCOMMON_LIB_VERSION} CONFIG REQUIRED)
find_package(KF5KdepimDBusInterfaces ${KDEPIM_LIB_VERSION} CONFIG REQUIRED)
find_package(KF5AkonadiNotes ${AKONADI_NOTES_VERSION} CONFIG REQUIRED)
ecm_setup_version(PROJECT VARIABLE_PREFIX CALENDARSUPPORT
......
......@@ -21,6 +21,7 @@ set(calendarsupport_LIB_SRCS
utils.cpp
tagcache.cpp
plugin.cpp
urihandler.cpp
printing/calprintpluginbase.cpp
printing/calprintdefaultplugins.cpp
......@@ -77,7 +78,6 @@ PRIVATE
KF5::AkonadiCore
KF5::CalendarCore
KF5::CalendarUtils
KF5::KdepimDBusInterfaces
KF5::PimCommon
)
......@@ -107,6 +107,7 @@ ecm_generate_headers(CalendarSupport_CamelCase_HEADERS
MessageWidget
ArchiveDialog
NoteEditDialog
UriHandler
REQUIRED_HEADERS CalendarSupport_HEADERS
PREFIX CalendarSupport
)
......
......@@ -22,8 +22,8 @@
#include "incidenceviewer_p.h"
#include "attachmenthandler.h"
#include "utils.h"
#include "urihandler.h"
#include <KdepimDBusInterfaces/UriHandler>
#include "incidenceattachmentmodel.h"
#include <CollectionFetchJob>
......
/*
Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#include "urihandler.h"
#include "calendarsupport_debug.h"
#include <KRun>
#include <KService>
#include <KDesktopFile>
#include <QUrl>
#include <QUrlQuery>
#include <QDesktopServices>
using namespace CalendarSupport;
namespace {
bool startService(const QString &desktopFileName, const QString &uri)
{
KDesktopFile desktopFile(QStandardPaths::ApplicationsLocation, desktopFileName);
const KService service{&desktopFile};
if (!KRun::run(service.exec(), {QUrl{uri}}, nullptr)) {
qCWarning(CALENDARSUPPORT_LOG) << "Failed to start" << desktopFileName;
return false;
}
return true;
}
bool startKOrganizer(const QString &uri)
{
return startService(QStringLiteral("korganizer-view.desktop"), uri);
}
bool startKMail(const QString &uri)
{
return startService(QStringLiteral("kmail_view.desktop"), uri);
}
bool startKAddressbook(const QString &uri)
{
return startService(QStringLiteral("kaddressbook-view.desktop"), uri);
}
} // namespace
bool UriHandler::process(const QString &uri)
{
qCDebug(CALENDARSUPPORT_LOG) << uri;
if (uri.startsWith(QLatin1String("kmail:"))) {
// extract 'number' from 'kmail:<number>/<id>'
const int start = uri.indexOf(QLatin1Char(':')) + 1;
const int end = uri.indexOf(QLatin1Char('/'), start);
const QString serialNumberStr = uri.mid(start, start - end);
return startKMail(QStringLiteral("akonadi://?item=%1").arg(serialNumberStr));
} else if (uri.startsWith(QLatin1String("mailto:"))) {
return QDesktopServices::openUrl(QUrl(uri));
} else if (uri.startsWith(QLatin1String("uid:"))) {
const QString uid = uri.mid(4);
return startKAddressbook(QStringLiteral("akonadi://?item=%1").arg(uid));
} else if (uri.startsWith(QLatin1String("urn:x-ical"))) {
const QString uid = QUrl::fromPercentEncoding(uri.toLatin1()).mid(11);
return startKOrganizer(QStringLiteral("akonadi://?item=%1").arg(uid));
} else if (uri.startsWith(QLatin1String("akonadi:"))) {
const QString mimeType = QUrlQuery(QUrl(uri)).queryItemValue(QStringLiteral("type"));
if (mimeType.toLower() == QLatin1String("message/rfc822")) {
return startKMail(uri);
} else if (mimeType.toLower() == QLatin1String("text/calendar")) {
return startKOrganizer(uri);
} else if (mimeType.toLower() == QLatin1String("text/directory")) {
return startKAddressbook(uri);
}
} else { // no special URI, let KDE handle it
new KRun(QUrl(uri), nullptr);
}
return false;
}
/*
Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#ifndef URIHANDLER_H
#define URIHANDLER_H
#include "calendarsupport_export.h"
class QString;
namespace CalendarSupport {
/** URI handler. */
class CALENDARSUPPORT_EXPORT UriHandler
{
public:
/**
Process URI (e.g. open mailer, open browser, open incidence viewer etc.).
@return true if handler handled the URI, otherwise false.
@param uri The URI of the link that should be handled.
*/
static bool process(const QString &uri);
};
} // namespace
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment