File indexing completed on 2021-06-15 12:03:43

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