ctkPlugin.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "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: Register Qt Mobility service xml files
  63. //only register if we are not already in the STARTING state
  64. if (d->state != STARTING)
  65. {
  66. QByteArray serviceDescriptor = getResource("servicedescriptor.xml");
  67. if (!serviceDescriptor.isEmpty())
  68. {
  69. d->fwCtx->services.registerService(d, serviceDescriptor);
  70. }
  71. }
  72. //6: Eager?
  73. if ((options & START_ACTIVATION_POLICY) && !d->eagerActivation )
  74. {
  75. if (STARTING == d->state) return;
  76. d->state = STARTING;
  77. d->pluginContext = new ctkPluginContext(this->d_func());
  78. ctkPluginEvent pluginEvent(ctkPluginEvent::LAZY_ACTIVATION, this);
  79. d->fwCtx->listeners.emitPluginChanged(pluginEvent);
  80. }
  81. else
  82. {
  83. d->finalizeActivation();
  84. }
  85. }
  86. void ctkPlugin::stop(const StopOptions& options)
  87. {
  88. Q_D(ctkPlugin);
  89. const std::exception* savedException = 0;
  90. //1:
  91. if (d->state == UNINSTALLED)
  92. {
  93. throw std::logic_error("Plugin is uninstalled");
  94. }
  95. //2: If activating or deactivating, wait a litle
  96. // We don't support threaded start/stop methods, so we don't need to wait
  97. //waitOnActivation(fwCtx.packages, "Plugin::stop", false);
  98. //3:
  99. if ((options & STOP_TRANSIENT) == 0)
  100. {
  101. d->ignoreAutostartSetting();
  102. }
  103. bool wasStarted = false;
  104. switch (d->state)
  105. {
  106. case INSTALLED:
  107. case RESOLVED:
  108. case STOPPING:
  109. case UNINSTALLED:
  110. //4:
  111. return;
  112. case ACTIVE:
  113. wasStarted = true;
  114. case STARTING: // Lazy start...
  115. try
  116. {
  117. d->stop0(wasStarted);
  118. }
  119. catch (const std::exception* exc)
  120. {
  121. savedException = exc;
  122. }
  123. break;
  124. };
  125. if (d->state != UNINSTALLED)
  126. {
  127. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPED, this));
  128. }
  129. if (savedException)
  130. {
  131. if (const ctkPluginException* pluginExc = dynamic_cast<const ctkPluginException*>(savedException))
  132. {
  133. throw pluginExc;
  134. }
  135. else
  136. {
  137. throw dynamic_cast<const std::logic_error*>(savedException);
  138. }
  139. }
  140. }
  141. ctkPluginContext* ctkPlugin::getPluginContext() const
  142. {
  143. //TODO security checks
  144. Q_D(const ctkPlugin);
  145. return d->pluginContext;
  146. }
  147. long ctkPlugin::getPluginId() const
  148. {
  149. Q_D(const ctkPlugin);
  150. return d->id;
  151. }
  152. QString ctkPlugin::getLocation() const
  153. {
  154. //TODO security
  155. Q_D(const ctkPlugin);
  156. return d->location;
  157. }
  158. QHash<QString, QString> ctkPlugin::getHeaders()
  159. {
  160. //TODO security
  161. Q_D(ctkPlugin);
  162. if (d->cachedRawHeaders.empty())
  163. {
  164. d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  165. }
  166. if (d->state == UNINSTALLED)
  167. {
  168. return d->cachedHeaders;
  169. }
  170. //TODO use the embedded .qm files to localize header values
  171. return d->cachedRawHeaders;
  172. }
  173. QString ctkPlugin::getSymbolicName() const
  174. {
  175. Q_D(const ctkPlugin);
  176. return d->symbolicName;
  177. }
  178. QStringList ctkPlugin::getResourceList(const QString& path) const
  179. {
  180. Q_D(const ctkPlugin);
  181. return d->archive->findResourcesPath(path);
  182. }
  183. QByteArray ctkPlugin::getResource(const QString& path) const
  184. {
  185. Q_D(const ctkPlugin);
  186. return d->archive->getPluginResource(path);
  187. }
  188. ctkVersion ctkPlugin::getVersion() const
  189. {
  190. Q_D(const ctkPlugin);
  191. return d->version;
  192. }