| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2012 David Faure <faure@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #include "khelpclient.h" |
| 9 | |
| 10 | #include <KDesktopFile> |
| 11 | |
| 12 | #include <QCoreApplication> |
| 13 | #include <QDesktopServices> |
| 14 | #include <QDirIterator> |
| 15 | #include <QUrl> |
| 16 | |
| 17 | void KHelpClient::invokeHelp(const QString &anchor, const QString &_appname) |
| 18 | { |
| 19 | QString appname; |
| 20 | if (_appname.isEmpty()) { |
| 21 | appname = QCoreApplication::instance()->applicationName(); |
| 22 | } else { |
| 23 | appname = _appname; |
| 24 | } |
| 25 | |
| 26 | // Look for the .desktop file of the application |
| 27 | |
| 28 | // was: |
| 29 | // KService::Ptr service(KService::serviceByDesktopName(appname)); |
| 30 | // if (service) |
| 31 | // docPath = service->docPath(); |
| 32 | // but we don't want to depend on KService here. |
| 33 | |
| 34 | QString docPath; |
| 35 | const QStringList desktopDirs = QStandardPaths::standardLocations(type: QStandardPaths::ApplicationsLocation); |
| 36 | for (const QString &dir : desktopDirs) { |
| 37 | QDirIterator it(dir, QStringList() << appname + QLatin1String(".desktop" ), QDir::NoFilter, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks); |
| 38 | while (it.hasNext()) { |
| 39 | const QString desktopPath(it.next()); |
| 40 | KDesktopFile desktopFile(desktopPath); |
| 41 | docPath = desktopFile.readDocPath(); |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // docPath could be a path or a full URL, I think. |
| 47 | |
| 48 | QUrl url; |
| 49 | if (!docPath.isEmpty()) { |
| 50 | url = QUrl(QStringLiteral("help:/" )).resolved(relative: QUrl(docPath)); |
| 51 | } else { |
| 52 | if (!anchor.isEmpty()) { |
| 53 | if (anchor.contains(c: QLatin1Char('#'))) { |
| 54 | url = QUrl(QStringLiteral("help:/%1/%2" ).arg(args&: appname, args: anchor)); |
| 55 | } else { |
| 56 | url = QUrl(QStringLiteral("help:/%1/%2.html" ).arg(args&: appname, args: anchor)); |
| 57 | } |
| 58 | } else { |
| 59 | url = QUrl(QStringLiteral("help:/%1/index.html" ).arg(a: appname)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // launch khelpcenter, or a browser for URIs not handled by khelpcenter |
| 64 | QDesktopServices::openUrl(url); |
| 65 | } |
| 66 | |