ctkPluginFramework.cpp 4.5 KB

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