ctkPluginTracker.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #ifndef CTKPLUGINTRACKER_H
  16. #define CTKPLUGINTRACKER_H
  17. #include <QScopedPointer>
  18. #include "CTKPluginFrameworkExport.h"
  19. #include "ctkPlugin.h"
  20. #include "ctkPluginTrackerCustomizer.h"
  21. class ctkPluginTrackerPrivate;
  22. /**
  23. * The <code>ctkPluginTracker</code> class simplifies tracking plugins much like
  24. * the <code>ctkServiceTracker</code> simplifies tracking services.
  25. * <p>
  26. * A <code>ctkPluginTracker</code> is constructed with state criteria and a
  27. * <code>ctkPluginTrackerCustomizer</code> object. A <code>ctkPluginTracker</code> can
  28. * use the <code>ctkPluginTrackerCustomizer</code> to select which plugins are
  29. * tracked and to create a customized object to be tracked with the plugin. The
  30. * <code>ctkPluginTracker</code> can then be opened to begin tracking all plugins
  31. * whose state matches the specified state criteria.
  32. * <p>
  33. * The <code>getPlugins</code> method can be called to get the
  34. * <code>ctkPlugin</code> objects of the plugins being tracked. The
  35. * <code>getObject</code> method can be called to get the customized object for
  36. * a tracked plugin.
  37. * <p>
  38. * The <code>ctkPluginTracker</code> class is thread-safe. It does not call a
  39. * <code>ctkPluginTrackerCustomizer</code> while holding any locks.
  40. * <code>ctkPluginTrackerCustomizer</code> implementations must also be
  41. * thread-safe.
  42. *
  43. * @ThreadSafe
  44. */
  45. class CTK_PLUGINFW_EXPORT ctkPluginTracker : protected ctkPluginTrackerCustomizer
  46. {
  47. public:
  48. ~ctkPluginTracker();
  49. /**
  50. * Create a <code>ctkPluginTracker</code> for plugins whose state is present in
  51. * the specified state mask.
  52. *
  53. * <p>
  54. * Plugins whose state is present on the specified state mask will be
  55. * tracked by this <code>ctkPluginTracker</code>.
  56. *
  57. * @param context The <code>ctkPluginContext</code> against which the tracking
  58. * is done.
  59. * @param stateMask The bit mask of the <code>OR</code>ing of the plugin
  60. * states to be tracked.
  61. * @param customizer The customizer object to call when plugins are added,
  62. * modified, or removed in this <code>ctkPluginTracker</code>. If
  63. * customizer is <code>null</code>, then this
  64. * <code>ctkPluginTracker</code> will be used as the
  65. * <code>ctkPluginTrackerCustomizer</code> and this
  66. * <code>ctkPluginTracker</code> will call the
  67. * <code>ctkPluginTrackerCustomizer</code> methods on itself. If the
  68. * customizer is not <code>null</code>, this <code>ctkPluginTracker</code>
  69. * takes ownership of the customizer.
  70. * @see ctkPlugin#getState()
  71. */
  72. ctkPluginTracker(ctkPluginContext* context, ctkPlugin::States stateMask,
  73. ctkPluginTrackerCustomizer* customizer = 0);
  74. /**
  75. * Open this <code>ctkPluginTracker</code> and begin tracking plugins.
  76. *
  77. * <p>
  78. * ctkPlugin's which match the state criteria specified when this
  79. * <code>ctkPluginTracker</code> was created are now tracked by this
  80. * <code>ctkPluginTracker</code>.
  81. *
  82. * @throws std::logic_error If the <code>ctkPluginContext</code>
  83. * with which this <code>ctkPluginTracker</code> was created is no
  84. * longer valid.
  85. */
  86. void open();
  87. /**
  88. * Close this <code>ctkPluginTracker</code>.
  89. *
  90. * <p>
  91. * This method should be called when this <code>ctkPluginTracker</code> should
  92. * end the tracking of plugins.
  93. *
  94. * <p>
  95. * This implementation calls getPlugins() to get the list of
  96. * tracked plugins to remove.
  97. */
  98. void close();
  99. /**
  100. * Return a list of <code>ctkPlugin</code>s for all plugins being tracked by
  101. * this <code>ctkPluginTracker</code>.
  102. *
  103. * @return A list of <code>ctkPlugin</code>s.
  104. */
  105. QList<ctkPlugin*> getPlugins() const;
  106. /**
  107. * Returns the customized object for the specified <code>ctkPlugin</code> if
  108. * the specified plugin is being tracked by this <code>ctkPluginTracker</code>.
  109. *
  110. * @param plugin The <code>ctkPlugin</code> being tracked.
  111. * @return The customized object for the specified <code>ctkPlugin</code> or
  112. * <code>null</code> if the specified <code>ctkPlugin</code> is not
  113. * being tracked.
  114. */
  115. QVariant getObject(ctkPlugin* plugin) const;
  116. /**
  117. * Remove a plugin from this <code>ctkPluginTracker</code>.
  118. *
  119. * The specified plugin will be removed from this <code>ctkPluginTracker</code>.
  120. * If the specified plugin was being tracked then the
  121. * <code>ctkPluginTrackerCustomizer::removedPlugin</code> method will be called
  122. * for that plugin.
  123. *
  124. * @param plugin The <code>ctkPlugin</code> to be removed.
  125. */
  126. void remove(ctkPlugin* plugin);
  127. /**
  128. * Return the number of plugins being tracked by this
  129. * <code>ctkPluginTracker</code>.
  130. *
  131. * @return The number of plugins being tracked.
  132. */
  133. int size() const;
  134. /**
  135. * Returns the tracking count for this <code>ctkPluginTracker</code>.
  136. *
  137. * The tracking count is initialized to 0 when this
  138. * <code>ctkPluginTracker</code> is opened. Every time a plugin is added,
  139. * modified or removed from this <code>ctkPluginTracker</code> the tracking
  140. * count is incremented.
  141. *
  142. * <p>
  143. * The tracking count can be used to determine if this
  144. * <code>ctkPluginTracker</code> has added, modified or removed a plugin by
  145. * comparing a tracking count value previously collected with the current
  146. * tracking count value. If the value has not changed, then no plugin has
  147. * been added, modified or removed from this <code>ctkPluginTracker</code>
  148. * since the previous tracking count was collected.
  149. *
  150. * @return The tracking count for this <code>ctkPluginTracker</code> or -1 if
  151. * this <code>ctkPluginTracker</code> is not open.
  152. */
  153. int getTrackingCount() const;
  154. protected:
  155. /**
  156. * Default implementation of the
  157. * <code>ctkPluginTrackerCustomizer::addingPlugin</code> method.
  158. *
  159. * <p>
  160. * This method is only called when this <code>ctkPluginTracker</code> has been
  161. * constructed with a <code>null</code> ctkPluginTrackerCustomizer argument.
  162. *
  163. * <p>
  164. * This implementation simply returns the specified <code>ctkPlugin*</code> in
  165. * a QVariant.
  166. *
  167. * <p>
  168. * This method can be overridden in a subclass to customize the object to be
  169. * tracked for the plugin being added.
  170. *
  171. * @param plugin The <code>ctkPlugin</code> being added to this
  172. * <code>ctkPluginTracker</code> object.
  173. * @param event The plugin event which caused this customizer method to be
  174. * called or an invalid event if there is no plugin event associated
  175. * with the call to this method.
  176. * @return The specified plugin.
  177. * @see ctkPluginTrackerCustomizer::addingPlugin(ctkPlugin*, const ctkPluginEvent&)
  178. */
  179. QVariant addingPlugin(ctkPlugin* plugin, const ctkPluginEvent& event);
  180. /**
  181. * Default implementation of the
  182. * <code>ctkPluginTrackerCustomizer::modifiedPlugin</code> method.
  183. *
  184. * <p>
  185. * This method is only called when this <code>ctkPluginTracker</code> has been
  186. * constructed with a <code>null</code> ctkPluginTrackerCustomizer argument.
  187. *
  188. * <p>
  189. * This implementation does nothing.
  190. *
  191. * @param plugin The <code>ctkPlugin</code> whose state has been modified.
  192. * @param event The plugin event which caused this customizer method to be
  193. * called or an invalid event if there is no plugin event associated
  194. * with the call to this method.
  195. * @param object The customized object for the specified ctkPlugin.
  196. * @see ctkPluginTrackerCustomizer::modifiedPlugin(ctkPlugin*, const ctkPluginEvent&, QVariant)
  197. */
  198. void modifiedPlugin(ctkPlugin* plugin, const ctkPluginEvent& event, QVariant object);
  199. /**
  200. * Default implementation of the
  201. * <code>ctkPluginTrackerCustomizer::removedPlugin</code> method.
  202. *
  203. * <p>
  204. * This method is only called when this <code>ctkPluginTracker</code> has been
  205. * constructed with a <code>null</code> ctkPluginTrackerCustomizer argument.
  206. *
  207. * <p>
  208. * This implementation does nothing.
  209. *
  210. * @param plugin The <code>ctkPlugin</code> being removed.
  211. * @param event The plugin event which caused this customizer method to be
  212. * called or an invalid event if there is no plugin event associated
  213. * with the call to this method.
  214. * @param object The customized object for the specified plugin.
  215. * @see ctkPluginTrackerCustomizer::removedPlugin(ctkPlugin*, const ctkPluginEvent&, QVariant)
  216. */
  217. void removedPlugin(ctkPlugin* plugin, const ctkPluginEvent& event, QVariant object);
  218. private:
  219. friend class ctkTrackedPlugin;
  220. Q_DECLARE_PRIVATE(ctkPluginTracker)
  221. const QScopedPointer<ctkPluginTrackerPrivate> d_ptr;
  222. };
  223. #endif // CTKPLUGINTRACKER_H