ctkPluginFramework.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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 "ctkPluginFramework.h"
  16. #include "ctkPluginFrameworkPrivate_p.h"
  17. #include "ctkPluginPrivate_p.h"
  18. #include "ctkPluginFrameworkContext_p.h"
  19. #include "ctkPluginConstants.h"
  20. #include "ctkPluginArchive_p.h"
  21. namespace ctk {
  22. PluginFramework::PluginFramework(PluginFrameworkContext* fw)
  23. : Plugin(*new PluginFrameworkPrivate(*this, fw))
  24. {
  25. qRegisterMetaType<PluginFrameworkEvent>("PluginFrameworkEvent");
  26. }
  27. void PluginFramework::init()
  28. {
  29. Q_D(PluginFramework);
  30. QMutexLocker sync(&d->lock);
  31. // waitOnActivation(d->lock, "Framework.init", true);
  32. switch (d->state)
  33. {
  34. case Plugin::INSTALLED:
  35. case Plugin::RESOLVED:
  36. break;
  37. case Plugin::STARTING:
  38. case Plugin::ACTIVE:
  39. return;
  40. default:
  41. throw std::logic_error("INTERNAL ERROR, Illegal state");
  42. }
  43. d->init();
  44. }
  45. void PluginFramework::start(const Plugin::StartOptions& options)
  46. {
  47. Q_UNUSED(options);
  48. Q_D(PluginFramework);
  49. QStringList pluginsToStart;
  50. {
  51. QMutexLocker sync(&d->lock);
  52. // TODO: parallel start
  53. //waitOnActivation(lock, "PluginFramework::start", true);
  54. switch (d->state)
  55. {
  56. case INSTALLED:
  57. case RESOLVED:
  58. d->init();
  59. case STARTING:
  60. d->activating = true;
  61. break;
  62. case ACTIVE:
  63. return;
  64. default:
  65. throw std::logic_error("INTERNAL ERROR, Illegal state");
  66. }
  67. pluginsToStart = d->fwCtx->storage.getStartOnLaunchPlugins();
  68. }
  69. // Start plugins according to their autostart setting.
  70. QStringListIterator i(pluginsToStart);
  71. while (i.hasNext())
  72. {
  73. Plugin* p = d->fwCtx->plugins->getPlugin(i.next());
  74. try {
  75. const int autostartSetting = p->d_func()->archive->getAutostartSetting();
  76. // Launch must not change the autostart setting of a plugin
  77. StartOptions option = Plugin::START_TRANSIENT;
  78. if (Plugin::START_ACTIVATION_POLICY == autostartSetting)
  79. {
  80. // Transient start according to the plugins activation policy.
  81. option |= Plugin::START_ACTIVATION_POLICY;
  82. }
  83. p->start(option);
  84. }
  85. catch (const PluginException& pe)
  86. {
  87. d->fwCtx->listeners.frameworkError(p, pe);
  88. }
  89. }
  90. {
  91. QMutexLocker sync(&d->lock);
  92. d->state = ACTIVE;
  93. d->activating = false;
  94. d->fwCtx->listeners.emitFrameworkEvent(
  95. PluginFrameworkEvent(PluginFrameworkEvent::STARTED, this));
  96. }
  97. }
  98. QStringList PluginFramework::getResourceList(const QString& path) const
  99. {
  100. QString resourcePath = QString(":/") + PluginConstants::SYSTEM_PLUGIN_SYMBOLICNAME;
  101. if (path.startsWith('/'))
  102. resourcePath += path;
  103. else
  104. resourcePath += QString("/") + path;
  105. QStringList paths;
  106. QFileInfoList entryInfoList = QDir(resourcePath).entryInfoList();
  107. QListIterator<QFileInfo> infoIter(entryInfoList);
  108. while (infoIter.hasNext())
  109. {
  110. const QFileInfo& resInfo = infoIter.next();
  111. QString entry = resInfo.canonicalFilePath().mid(resourcePath.size());
  112. if (resInfo.isDir())
  113. entry += "/";
  114. paths << entry;
  115. }
  116. return paths;
  117. }
  118. QByteArray PluginFramework::getResource(const QString& path) const
  119. {
  120. QString resourcePath = QString(":/") + PluginConstants::SYSTEM_PLUGIN_SYMBOLICNAME;
  121. if (path.startsWith('/'))
  122. resourcePath += path;
  123. else
  124. resourcePath += QString("/") + path;
  125. QFile resourceFile(resourcePath);
  126. resourceFile.open(QIODevice::ReadOnly);
  127. return resourceFile.readAll();
  128. }
  129. QHash<QString, QString> PluginFramework::getHeaders()
  130. {
  131. //TODO security
  132. Q_D(PluginFramework);
  133. return d->systemHeaders;
  134. }
  135. }