PageRenderTime 93ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/plasma/private/associatedapplicationmanager.cpp

https://github.com/vasi/kdelibs
C++ | 157 lines | 107 code | 31 blank | 19 comment | 11 complexity | 5bb045b2c3ae2b5c03daa843db889941 MD5 | raw file
  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. if (!d->applicationNames.contains(applet)) {
  85. connect(applet, SIGNAL(destroyed(QObject *)), this, SLOT(cleanupApplet(QObject *)));
  86. }
  87. }
  88. void AssociatedApplicationManager::setUrls(Plasma::Applet *applet, const KUrl::List &urls)
  89. {
  90. d->urlLists[applet] = urls;
  91. }
  92. KUrl::List AssociatedApplicationManager::urls(const Plasma::Applet *applet) const
  93. {
  94. return d->urlLists.value(applet);
  95. }
  96. void AssociatedApplicationManager::run(Plasma::Applet *applet)
  97. {
  98. if (d->applicationNames.contains(applet)) {
  99. #ifndef PLASMA_NO_KIO
  100. bool success = KRun::run(d->applicationNames.value(applet), d->urlLists.value(applet), 0);
  101. #else
  102. QString execCommand = d->applicationNames.value(applet);
  103. // Clean-up the %u and friends from the exec command (KRun expect them, not QProcess)
  104. execCommand = execCommand.replace(QRegExp("%[a-z]"), QString());
  105. execCommand = execCommand.trimmed();
  106. QStringList parameters = d->urlLists.value(applet).toStringList();
  107. bool success = QProcess::startDetached(execCommand, parameters);
  108. #endif
  109. if (!success) {
  110. applet->showMessage(KIcon("application-exit"), i18n("There was an error attempting to exec the associated application with this widget."), ButtonOk);
  111. }
  112. } else if (d->urlLists.contains(applet) && !d->urlLists.value(applet).isEmpty()) {
  113. #ifndef PLASMA_NO_KIO
  114. KRun *krun = new KRun(d->urlLists.value(applet).first(), 0);
  115. krun->setAutoDelete(true);
  116. #else
  117. QDesktopServices::openUrl(d->urlLists.value(applet).first());
  118. #endif
  119. }
  120. }
  121. bool AssociatedApplicationManager::appletHasValidAssociatedApplication(const Plasma::Applet *applet) const
  122. {
  123. return (d->applicationNames.contains(applet) || d->urlLists.contains(applet));
  124. }
  125. } // namespace Plasma
  126. #include <moc_associatedapplicationmanager_p.cpp>