ctkPlugin.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "ctkPlugin.h"
  16. #include "ctkPluginPrivate_p.h"
  17. #include "ctkPluginArchive_p.h"
  18. #include "ctkPluginFrameworkContext_p.h"
  19. #include <QStringList>
  20. ctkPlugin::ctkPlugin(ctkPluginFrameworkContext* fw,
  21. ctkPluginArchive* pa)
  22. : d_ptr(new ctkPluginPrivate(*this, fw, pa))
  23. {
  24. }
  25. ctkPlugin::ctkPlugin(ctkPluginPrivate& dd)
  26. : d_ptr(&dd)
  27. {
  28. }
  29. ctkPlugin::~ctkPlugin()
  30. {
  31. delete d_ptr;
  32. }
  33. ctkPlugin::State ctkPlugin::getState() const
  34. {
  35. Q_D(const ctkPlugin);
  36. return d->state;
  37. }
  38. void ctkPlugin::start(const StartOptions& options)
  39. {
  40. Q_D(ctkPlugin);
  41. if (d->state == UNINSTALLED)
  42. {
  43. throw std::logic_error("ctkPlugin is uninstalled");
  44. }
  45. // Initialize the activation; checks initialization of lazy
  46. // activation.
  47. //TODO 1: If activating or deactivating, wait a litle
  48. // we don't use mutliple threads to start plugins for now
  49. //waitOnActivation(lock, "ctkPlugin::start", false);
  50. //2: start() is idempotent, i.e., nothing to do when already started
  51. if (d->state == ACTIVE)
  52. {
  53. return;
  54. }
  55. //3: Record non-transient start requests.
  56. if ((options & START_TRANSIENT) == 0)
  57. {
  58. d->setAutostartSetting(options);
  59. }
  60. //4: Resolve plugin (if needed)
  61. d->getUpdatedState();
  62. //5: Eager?
  63. if ((options & START_ACTIVATION_POLICY) && !d->eagerActivation )
  64. {
  65. if (STARTING == d->state) return;
  66. d->state = STARTING;
  67. d->pluginContext = new ctkPluginContext(this->d_func());
  68. ctkPluginEvent pluginEvent(ctkPluginEvent::LAZY_ACTIVATION, this);
  69. d->fwCtx->listeners.emitPluginChanged(pluginEvent);
  70. }
  71. else
  72. {
  73. d->finalizeActivation();
  74. }
  75. //6: Register Qt Mobility service xml files
  76. //only register if we are not already in the STARTING
  77. //or ACTIVE state
  78. if (d->state & STARTING || d->state & ACTIVE)
  79. {
  80. QByteArray serviceDescriptor = getResource("servicedescriptor.xml");
  81. if (!serviceDescriptor.isEmpty())
  82. {
  83. d->fwCtx->services.registerService(d, serviceDescriptor);
  84. }
  85. }
  86. }
  87. void ctkPlugin::stop(const StopOptions& options)
  88. {
  89. Q_D(ctkPlugin);
  90. const std::exception* savedException = 0;
  91. //1:
  92. if (d->state == UNINSTALLED)
  93. {
  94. throw std::logic_error("Plugin is uninstalled");
  95. }
  96. //2: If activating or deactivating, wait a litle
  97. // We don't support threaded start/stop methods, so we don't need to wait
  98. //waitOnActivation(fwCtx.packages, "Plugin::stop", false);
  99. //3:
  100. if ((options & STOP_TRANSIENT) == 0)
  101. {
  102. d->ignoreAutostartSetting();
  103. }
  104. bool wasStarted = false;
  105. switch (d->state)
  106. {
  107. case INSTALLED:
  108. case RESOLVED:
  109. case STOPPING:
  110. case UNINSTALLED:
  111. //4:
  112. return;
  113. case ACTIVE:
  114. wasStarted = true;
  115. case STARTING: // Lazy start...
  116. try
  117. {
  118. d->stop0(wasStarted);
  119. }
  120. catch (const std::exception* exc)
  121. {
  122. savedException = exc;
  123. }
  124. break;
  125. };
  126. if (d->state != UNINSTALLED)
  127. {
  128. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPED, this));
  129. }
  130. if (savedException)
  131. {
  132. if (const ctkPluginException* pluginExc = dynamic_cast<const ctkPluginException*>(savedException))
  133. {
  134. throw pluginExc;
  135. }
  136. else
  137. {
  138. throw dynamic_cast<const std::logic_error*>(savedException);
  139. }
  140. }
  141. }
  142. ctkPluginContext* ctkPlugin::getPluginContext() const
  143. {
  144. //TODO security checks
  145. Q_D(const ctkPlugin);
  146. return d->pluginContext;
  147. }
  148. long ctkPlugin::getPluginId() const
  149. {
  150. Q_D(const ctkPlugin);
  151. return d->id;
  152. }
  153. QString ctkPlugin::getLocation() const
  154. {
  155. //TODO security
  156. Q_D(const ctkPlugin);
  157. return d->location;
  158. }
  159. QHash<QString, QString> ctkPlugin::getHeaders()
  160. {
  161. //TODO security
  162. Q_D(ctkPlugin);
  163. if (d->cachedRawHeaders.empty())
  164. {
  165. d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  166. }
  167. if (d->state == UNINSTALLED)
  168. {
  169. return d->cachedHeaders;
  170. }
  171. //TODO use the embedded .qm files to localize header values
  172. return d->cachedRawHeaders;
  173. }
  174. QString ctkPlugin::getSymbolicName() const
  175. {
  176. Q_D(const ctkPlugin);
  177. return d->symbolicName;
  178. }
  179. QStringList ctkPlugin::getResourceList(const QString& path) const
  180. {
  181. Q_D(const ctkPlugin);
  182. return d->archive->findResourcesPath(path);
  183. }
  184. QByteArray ctkPlugin::getResource(const QString& path) const
  185. {
  186. Q_D(const ctkPlugin);
  187. return d->archive->getPluginResource(path);
  188. }
  189. ctkVersion ctkPlugin::getVersion() const
  190. {
  191. Q_D(const ctkPlugin);
  192. return d->version;
  193. }