ctkPlugin.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "ctkPlugin.h"
  16. #include "ctkPluginPrivate_p.h"
  17. #include "ctkPluginArchive_p.h"
  18. #include "ctkPluginFrameworkContextPrivate_p.h"
  19. #include <QStringList>
  20. namespace ctk {
  21. Plugin::Plugin(PluginFrameworkContextPrivate* fw,
  22. PluginArchive* pa)
  23. : d_ptr(new PluginPrivate(*this, fw, pa))
  24. {
  25. }
  26. Plugin::Plugin(PluginPrivate& dd)
  27. : d_ptr(&dd)
  28. {
  29. }
  30. Plugin::~Plugin()
  31. {
  32. delete d_ptr;
  33. }
  34. Plugin::State Plugin::getState() const
  35. {
  36. Q_D(const Plugin);
  37. return d->state;
  38. }
  39. void Plugin::start(const StartOptions& options)
  40. {
  41. Q_D(Plugin);
  42. if (d->state == UNINSTALLED)
  43. {
  44. throw std::logic_error("Plugin is uninstalled");
  45. }
  46. // Initialize the activation; checks initialization of lazy
  47. // activation.
  48. //TODO 1: If activating or deactivating, wait a litle
  49. // we don't use mutliple threads to start plugins for now
  50. //waitOnActivation(lock, "Plugin::start", false);
  51. //2: start() is idempotent, i.e., nothing to do when already started
  52. if (d->state == ACTIVE)
  53. {
  54. return;
  55. }
  56. //3: Record non-transient start requests.
  57. if ((options & START_TRANSIENT) == 0)
  58. {
  59. d->setAutostartSetting(options);
  60. }
  61. //4: Resolve plugin (if needed)
  62. d->getUpdatedState();
  63. //5: Eager?
  64. if ((options & START_ACTIVATION_POLICY) && d->eagerActivation )
  65. {
  66. d->finalizeActivation();
  67. }
  68. else
  69. {
  70. if (STARTING == d->state) return;
  71. d->state = STARTING;
  72. d->pluginContext = new PluginContext(this->d_func());
  73. PluginEvent pluginEvent(PluginEvent::LAZY_ACTIVATION, this);
  74. d->fwCtx->listeners.emitPluginChanged(pluginEvent);
  75. }
  76. }
  77. void Plugin::stop(const StopOptions& options)
  78. {
  79. }
  80. PluginContext* Plugin::getPluginContext() const
  81. {
  82. //TODO security checks
  83. Q_D(const Plugin);
  84. return d->pluginContext;
  85. }
  86. long Plugin::getPluginId() const
  87. {
  88. Q_D(const Plugin);
  89. return d->id;
  90. }
  91. QString Plugin::getLocation() const
  92. {
  93. //TODO security
  94. Q_D(const Plugin);
  95. return d->location;
  96. }
  97. QHash<QString, QString> Plugin::getHeaders()
  98. {
  99. //TODO security
  100. Q_D(Plugin);
  101. if (d->cachedRawHeaders.empty())
  102. {
  103. d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  104. }
  105. if (d->state == UNINSTALLED)
  106. {
  107. return d->cachedHeaders;
  108. }
  109. //TODO use the embedded .qm files to localize header values
  110. return d->cachedRawHeaders;
  111. }
  112. QString Plugin::getSymbolicName() const
  113. {
  114. Q_D(const Plugin);
  115. return d->symbolicName;
  116. }
  117. QStringList Plugin::getResourceList(const QString& path) const
  118. {
  119. Q_D(const Plugin);
  120. return d->archive->findResourcesPath(path);
  121. }
  122. QByteArray Plugin::getResource(const QString& path) const
  123. {
  124. Q_D(const Plugin);
  125. return d->archive->getPluginResource(path);
  126. }
  127. Version Plugin::getVersion() const
  128. {
  129. Q_D(const Plugin);
  130. return d->version;
  131. }
  132. }