File indexing completed on 2020-07-15 10:49:47

0001 /*
0002     SPDX-FileCopyrightText: 2013 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <kguiaddons_debug.h>
0008 
0009 #include <QObject>
0010 #include <QUrl>
0011 #include <QStandardPaths>
0012 #include <QCoreApplication>
0013 #include <QProcess>
0014 #include <QDesktopServices>
0015 #include <QLocale>
0016 
0017 class UrlHandler : public QObject
0018 {
0019     Q_OBJECT
0020 public:
0021     explicit UrlHandler(QObject *parent = nullptr) : QObject(parent) {}
0022 
0023 public Q_SLOTS:
0024     void openHelp(const QUrl &url)
0025     {
0026         const QString appName = QCoreApplication::applicationName();
0027 
0028         QUrl u(url);
0029         if (u.path() == QLatin1Char('/')) {
0030             u.setPath(appName);
0031         }
0032 
0033         const QString helpcenter = QStandardPaths::findExecutable(QStringLiteral("khelpcenter"));
0034         if (!helpcenter.isEmpty()) { // use khelpcenter if it is available
0035             QProcess::startDetached(helpcenter, QStringList(u.toString()));
0036             return;
0037         }
0038 
0039         //if khelpcenter is not available and it's a KDE application, use docs.kde.org
0040         if (QCoreApplication::organizationDomain() == QLatin1String("kde.org")) {
0041             QString path = url.path();
0042             QString docPath;
0043             if (appName == QLatin1String("systemsettings") && path.startsWith(QLatin1String("/kcontrol"))) {
0044                 // special case for kcm modules
0045                 // e.g. "help:/kcontrol/fonts/index.html" >>> "&application=kcontorl/fonts"
0046                 docPath = path.remove(0, 1).remove(QLatin1String("/index.html"));
0047             } else { //e.g. "help:/okular", "help:/systemsettings"
0048                 docPath = appName + QStringLiteral("&path=") + path;
0049             }
0050             const QUrl httpUrl(QLatin1String("https://docs.kde.org/index.php?branch=stable5&language=")
0051                                + QLocale().name() + QLatin1String("&application=") + docPath);
0052             QDesktopServices::openUrl(httpUrl);
0053             return;
0054         }
0055 
0056         // not a KDE application
0057         qCWarning(KGUIADDONS_LOG) << "Could not find a suitable handler for " << u.toString();
0058     }
0059 };
0060 
0061 Q_GLOBAL_STATIC(UrlHandler, s_handler)
0062 
0063 static void initializeGlobalSettings()
0064 {
0065     QDesktopServices::setUrlHandler(QStringLiteral("help"), s_handler, "openHelp");
0066 }
0067 
0068 Q_COREAPP_STARTUP_FUNCTION(initializeGlobalSettings)
0069 
0070 #include "urlhandler.moc"