File indexing completed on 2023-09-24 11:39:11

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2012 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "khelpclient.h"
0009 
0010 #include <KDesktopFile>
0011 
0012 #include <QCoreApplication>
0013 #include <QDesktopServices>
0014 #include <QDirIterator>
0015 #include <QUrl>
0016 
0017 void KHelpClient::invokeHelp(const QString &anchor, const QString &_appname)
0018 {
0019     QString appname;
0020     if (_appname.isEmpty()) {
0021         appname = QCoreApplication::instance()->applicationName();
0022     } else {
0023         appname = _appname;
0024     }
0025 
0026     // Look for the .desktop file of the application
0027 
0028     // was:
0029     // KService::Ptr service(KService::serviceByDesktopName(appname));
0030     // if (service)
0031     //    docPath = service->docPath();
0032     // but we don't want to depend on KService here.
0033 
0034     QString docPath;
0035     const QStringList desktopDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
0036     for (const QString &dir : desktopDirs) {
0037         QDirIterator it(dir, QStringList() << appname + QLatin1String(".desktop"), QDir::NoFilter, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
0038         while (it.hasNext()) {
0039             const QString desktopPath(it.next());
0040             KDesktopFile desktopFile(desktopPath);
0041             docPath = desktopFile.readDocPath();
0042             break;
0043         }
0044     }
0045 
0046     // docPath could be a path or a full URL, I think.
0047 
0048     QUrl url;
0049     if (!docPath.isEmpty()) {
0050         url = QUrl(QStringLiteral("help:/")).resolved(QUrl(docPath));
0051     } else {
0052         if (!anchor.isEmpty()) {
0053             if (anchor.contains(QLatin1Char('#'))) {
0054                 url = QUrl(QStringLiteral("help:/%1/%2").arg(appname, anchor));
0055             } else {
0056                 url = QUrl(QStringLiteral("help:/%1/%2.html").arg(appname, anchor));
0057             }
0058         } else {
0059             url = QUrl(QStringLiteral("help:/%1/index.html").arg(appname));
0060         }
0061     }
0062 
0063     // launch khelpcenter, or a browser for URIs not handled by khelpcenter
0064     QDesktopServices::openUrl(url);
0065 }