File indexing completed on 2024-12-22 04:14:00

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