/libs/widgetutils/config/khelpclient.cpp

https://github.com/KDE/krita · C++ · 81 lines · 40 code · 13 blank · 28 comment · 6 complexity · 188b6e97c5c48027af5a5e58340756ba MD5 · raw file

  1. /* This file is part of the KDE libraries
  2. * Copyright 2012 David Faure <faure@kde.org>
  3. *
  4. * This library is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License or ( at
  7. * your option ) version 3 or, at the discretion of KDE e.V. ( which shall
  8. * act as a proxy as in section 14 of the GPLv3 ), any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include "khelpclient.h"
  21. #include "kdesktopfile.h"
  22. #include <QCoreApplication>
  23. #include <QUrl>
  24. #include <QDirIterator>
  25. #include <QUrlQuery>
  26. #include <QStandardPaths>
  27. #include <QDesktopServices>
  28. void KHelpClient::invokeHelp(const QString &anchor, const QString &_appname)
  29. {
  30. QString appname;
  31. if (_appname.isEmpty()) {
  32. appname = QCoreApplication::instance()->applicationName();
  33. } else {
  34. appname = _appname;
  35. }
  36. // Look for the .desktop file of the application
  37. // was:
  38. //KService::Ptr service(KService::serviceByDesktopName(appname));
  39. //if (service)
  40. // docPath = service->docPath();
  41. // but we don't want to depend on KService here.
  42. QString docPath;
  43. const QStringList desktopDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
  44. Q_FOREACH (const QString &dir, desktopDirs) {
  45. QDirIterator it(dir, QStringList() << appname + QLatin1String(".desktop"), QDir::NoFilter, QDirIterator::Subdirectories);
  46. while (it.hasNext()) {
  47. const QString desktopPath(it.next());
  48. KDesktopFile desktopFile(desktopPath);
  49. docPath = desktopFile.readDocPath();
  50. // TODO: why explicit break in a loop?
  51. break;
  52. }
  53. }
  54. // docPath could be a path or a full URL, I think.
  55. QUrl url;
  56. if (!docPath.isEmpty()) {
  57. url = QUrl(QLatin1String("help:/")).resolved(QUrl(docPath));
  58. } else {
  59. url = QUrl(QString::fromLatin1("help:/%1/index.html").arg(appname));
  60. }
  61. if (!anchor.isEmpty()) {
  62. QUrlQuery query(url);
  63. query.addQueryItem(QString::fromLatin1("anchor"), anchor);
  64. url.setQuery(query);
  65. }
  66. // launch khelpcenter, or a browser for URIs not handled by khelpcenter
  67. QDesktopServices::openUrl(url);
  68. }