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