ctkCMPluginManager.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCMPluginManager_p.h"
  16. #include <ctkServiceTracker.h>
  17. #include <service/cm/ctkConfigurationPlugin.h>
  18. #include <QStringList>
  19. #include <set>
  20. struct _PluginTrackerCompare
  21. {
  22. bool operator() (const ctkServiceReference& l, const ctkServiceReference& r) const
  23. {
  24. return getRank(l) < getRank(r);
  25. }
  26. int getRank(const ctkServiceReference& ref) const
  27. {
  28. QVariant ranking = ref.getProperty(ctkConfigurationPlugin::CM_RANKING);
  29. if (!ranking.isValid() || !(ranking.canConvert<int>()))
  30. return 0;
  31. return ranking.toInt();
  32. }
  33. };
  34. class _PluginTracker : public ctkServiceTracker<ctkConfigurationPlugin*>
  35. {
  36. mutable QMutex mutex;
  37. typedef std::set<ctkServiceReference, _PluginTrackerCompare> ServiceRefContainer;
  38. ServiceRefContainer serviceReferences;
  39. ctkPluginContext* context;
  40. public:
  41. _PluginTracker(ctkPluginContext* context)
  42. : ctkServiceTracker<ctkConfigurationPlugin*>(context),
  43. context(context)
  44. {
  45. }
  46. QList<ctkServiceReference> getServiceReferences() const
  47. {
  48. QMutexLocker lock(&mutex);
  49. QList<ctkServiceReference> refs;
  50. ServiceRefContainer::const_iterator refEnd = serviceReferences.end();
  51. for (ServiceRefContainer::const_iterator it = serviceReferences.begin();
  52. it != refEnd; ++it)
  53. {
  54. refs.push_back(*it);
  55. }
  56. return refs;
  57. }
  58. ctkConfigurationPlugin* addingService(const ctkServiceReference& reference)
  59. {
  60. QMutexLocker lock(&mutex);
  61. serviceReferences.insert(reference);
  62. return context->getService<ctkConfigurationPlugin>(reference);
  63. }
  64. void modifiedService(const ctkServiceReference& reference, ctkConfigurationPlugin* service)
  65. {
  66. Q_UNUSED(reference)
  67. Q_UNUSED(service)
  68. // nothing to do
  69. }
  70. void removedService(const ctkServiceReference& reference, ctkConfigurationPlugin* service)
  71. {
  72. Q_UNUSED(service)
  73. QMutexLocker lock(&mutex);
  74. serviceReferences.erase(reference);
  75. context->ungetService(reference);
  76. }
  77. };
  78. ctkCMPluginManager::ctkCMPluginManager(ctkPluginContext* context)
  79. {
  80. pluginTracker.reset(new _PluginTracker(context));
  81. }
  82. ctkCMPluginManager::~ctkCMPluginManager()
  83. {
  84. }
  85. void ctkCMPluginManager::start()
  86. {
  87. pluginTracker->open();
  88. }
  89. void ctkCMPluginManager::stop()
  90. {
  91. pluginTracker->close();
  92. }
  93. void ctkCMPluginManager::modifyConfiguration(const ctkServiceReference& managedReference, ctkDictionary& properties)
  94. {
  95. if (properties.empty())
  96. return;
  97. QList<ctkServiceReference> references = pluginTracker->getServiceReferences();
  98. foreach(ctkServiceReference ref, references)
  99. {
  100. QVariant pids = ref.getProperty(ctkConfigurationPlugin::CM_TARGET);
  101. if (pids.isValid() && pids.canConvert<QStringList>())
  102. {
  103. QString pid = properties.value(ctkPluginConstants::SERVICE_PID).toString();
  104. if (!pids.toStringList().contains(pid))
  105. continue;
  106. }
  107. ctkConfigurationPlugin* plugin = pluginTracker->getService(ref);
  108. if (plugin != 0)
  109. plugin->modifyConfiguration(managedReference, properties);
  110. }
  111. }