ctkPlugin.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_UNUSED(options)
  89. }
  90. ctkPluginContext* ctkPlugin::getPluginContext() const
  91. {
  92. //TODO security checks
  93. Q_D(const ctkPlugin);
  94. return d->pluginContext;
  95. }
  96. long ctkPlugin::getPluginId() const
  97. {
  98. Q_D(const ctkPlugin);
  99. return d->id;
  100. }
  101. QString ctkPlugin::getLocation() const
  102. {
  103. //TODO security
  104. Q_D(const ctkPlugin);
  105. return d->location;
  106. }
  107. QHash<QString, QString> ctkPlugin::getHeaders()
  108. {
  109. //TODO security
  110. Q_D(ctkPlugin);
  111. if (d->cachedRawHeaders.empty())
  112. {
  113. d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  114. }
  115. if (d->state == UNINSTALLED)
  116. {
  117. return d->cachedHeaders;
  118. }
  119. //TODO use the embedded .qm files to localize header values
  120. return d->cachedRawHeaders;
  121. }
  122. QString ctkPlugin::getSymbolicName() const
  123. {
  124. Q_D(const ctkPlugin);
  125. return d->symbolicName;
  126. }
  127. QStringList ctkPlugin::getResourceList(const QString& path) const
  128. {
  129. Q_D(const ctkPlugin);
  130. return d->archive->findResourcesPath(path);
  131. }
  132. QByteArray ctkPlugin::getResource(const QString& path) const
  133. {
  134. Q_D(const ctkPlugin);
  135. return d->archive->getPluginResource(path);
  136. }
  137. ctkVersion ctkPlugin::getVersion() const
  138. {
  139. Q_D(const ctkPlugin);
  140. return d->version;
  141. }