PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/kdelibs-4.8.97/plasma/private/associatedapplicationmanager.cpp

#
C++ | 154 lines | 104 code | 31 blank | 19 comment | 10 complexity | a3657ce6942104b68eccade77f16a5a0 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, LGPL-2.0, LGPL-3.0
  1. /*
  2. * Copyright 2009 Marco Martin <notmart@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Library General Public License as
  6. * published by the Free Software Foundation; either version 2, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this program; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "associatedapplicationmanager_p.h"
  20. #include "config-plasma.h"
  21. #include <QHash>
  22. #include <QFile>
  23. #include <kstandarddirs.h>
  24. #include <kicon.h>
  25. #ifndef PLASMA_NO_KIO
  26. #include <krun.h>
  27. #else
  28. #include <QProcess>
  29. #include <QDesktopServices>
  30. #endif
  31. #include "plasma/applet.h"
  32. namespace Plasma
  33. {
  34. class AssociatedApplicationManagerPrivate
  35. {
  36. public:
  37. AssociatedApplicationManagerPrivate()
  38. {
  39. }
  40. ~AssociatedApplicationManagerPrivate()
  41. {
  42. }
  43. void cleanupApplet(QObject *obj)
  44. {
  45. Plasma::Applet *applet = static_cast<Plasma::Applet *>(obj);
  46. applicationNames.remove(applet);
  47. urlLists.remove(applet);
  48. }
  49. QHash<const Plasma::Applet *, QString> applicationNames;
  50. QHash<const Plasma::Applet *, KUrl::List> urlLists;
  51. };
  52. class AssociatedApplicationManagerSingleton
  53. {
  54. public:
  55. AssociatedApplicationManager self;
  56. };
  57. K_GLOBAL_STATIC(AssociatedApplicationManagerSingleton, privateAssociatedApplicationManagerSelf)
  58. AssociatedApplicationManager::AssociatedApplicationManager(QObject *parent)
  59. : QObject(parent),
  60. d(new AssociatedApplicationManagerPrivate())
  61. {
  62. }
  63. AssociatedApplicationManager::~AssociatedApplicationManager()
  64. {
  65. delete d;
  66. }
  67. AssociatedApplicationManager *AssociatedApplicationManager::self()
  68. {
  69. return &privateAssociatedApplicationManagerSelf->self;
  70. }
  71. void AssociatedApplicationManager::setApplication(Plasma::Applet *applet, const QString &application)
  72. {
  73. KService::Ptr service = KService::serviceByDesktopName(application);
  74. if (service || !KStandardDirs::findExe(application).isNull() || QFile::exists(application)) {
  75. d->applicationNames[applet] = application;
  76. if (!d->urlLists.contains(applet)) {
  77. connect(applet, SIGNAL(destroyed(QObject*)), this, SLOT(cleanupApplet(QObject*)));
  78. }
  79. }
  80. }
  81. QString AssociatedApplicationManager::application(const Plasma::Applet *applet) const
  82. {
  83. return d->applicationNames.value(applet);
  84. }
  85. void AssociatedApplicationManager::setUrls(Plasma::Applet *applet, const KUrl::List &urls)
  86. {
  87. d->urlLists[applet] = urls;
  88. }
  89. KUrl::List AssociatedApplicationManager::urls(const Plasma::Applet *applet) const
  90. {
  91. return d->urlLists.value(applet);
  92. }
  93. void AssociatedApplicationManager::run(Plasma::Applet *applet)
  94. {
  95. if (d->applicationNames.contains(applet)) {
  96. #ifndef PLASMA_NO_KIO
  97. bool success = KRun::run(d->applicationNames.value(applet), d->urlLists.value(applet), 0);
  98. #else
  99. QString execCommand = d->applicationNames.value(applet);
  100. // Clean-up the %u and friends from the exec command (KRun expect them, not QProcess)
  101. execCommand = execCommand.replace(QRegExp("%[a-z]"), QString());
  102. execCommand = execCommand.trimmed();
  103. QStringList parameters = d->urlLists.value(applet).toStringList();
  104. bool success = QProcess::startDetached(execCommand, parameters);
  105. #endif
  106. if (!success) {
  107. applet->showMessage(KIcon("application-exit"), i18n("There was an error attempting to exec the associated application with this widget."), ButtonOk);
  108. }
  109. } else if (d->urlLists.contains(applet) && !d->urlLists.value(applet).isEmpty()) {
  110. #ifndef PLASMA_NO_KIO
  111. KRun *krun = new KRun(d->urlLists.value(applet).first(), 0);
  112. krun->setAutoDelete(true);
  113. #else
  114. QDesktopServices::openUrl(d->urlLists.value(applet).first());
  115. #endif
  116. }
  117. }
  118. bool AssociatedApplicationManager::appletHasValidAssociatedApplication(const Plasma::Applet *applet) const
  119. {
  120. return (d->applicationNames.contains(applet) || d->urlLists.contains(applet));
  121. }
  122. } // namespace Plasma
  123. #include <moc_associatedapplicationmanager_p.cpp>