/*============================================================================= Library: CTK Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =============================================================================*/ #ifndef CTKPLUGINTRACKER_H #define CTKPLUGINTRACKER_H #include #include "ctkPluginFrameworkExport.h" #include "ctkPlugin.h" #include "ctkPluginTrackerCustomizer.h" class ctkPluginTrackerPrivate; /** * The ctkPluginTracker class simplifies tracking plugins much like * the ctkServiceTracker simplifies tracking services. *

* A ctkPluginTracker is constructed with state criteria and a * ctkPluginTrackerCustomizer object. A ctkPluginTracker can * use the ctkPluginTrackerCustomizer to select which plugins are * tracked and to create a customized object to be tracked with the plugin. The * ctkPluginTracker can then be opened to begin tracking all plugins * whose state matches the specified state criteria. *

* The getPlugins method can be called to get the * ctkPlugin objects of the plugins being tracked. The * getObject method can be called to get the customized object for * a tracked plugin. *

* The ctkPluginTracker class is thread-safe. It does not call a * ctkPluginTrackerCustomizer while holding any locks. * ctkPluginTrackerCustomizer implementations must also be * thread-safe. * * @ThreadSafe */ class CTK_PLUGINFW_EXPORT ctkPluginTracker : protected ctkPluginTrackerCustomizer { public: ~ctkPluginTracker(); /** * Create a ctkPluginTracker for plugins whose state is present in * the specified state mask. * *

* Plugins whose state is present on the specified state mask will be * tracked by this ctkPluginTracker. * * @param context The ctkPluginContext against which the tracking * is done. * @param stateMask The bit mask of the ORing of the plugin * states to be tracked. * @param customizer The customizer object to call when plugins are added, * modified, or removed in this ctkPluginTracker. If * customizer is null, then this * ctkPluginTracker will be used as the * ctkPluginTrackerCustomizer and this * ctkPluginTracker will call the * ctkPluginTrackerCustomizer methods on itself. If the * customizer is not null, this ctkPluginTracker * takes ownership of the customizer. * @see ctkPlugin#getState() */ ctkPluginTracker(ctkPluginContext* context, ctkPlugin::States stateMask, ctkPluginTrackerCustomizer* customizer = 0); /** * Open this ctkPluginTracker and begin tracking plugins. * *

* ctkPlugin's which match the state criteria specified when this * ctkPluginTracker was created are now tracked by this * ctkPluginTracker. * * @throws std::logic_error If the ctkPluginContext * with which this ctkPluginTracker was created is no * longer valid. */ void open(); /** * Close this ctkPluginTracker. * *

* This method should be called when this ctkPluginTracker should * end the tracking of plugins. * *

* This implementation calls getPlugins() to get the list of * tracked plugins to remove. */ void close(); /** * Return a list of ctkPlugins for all plugins being tracked by * this ctkPluginTracker. * * @return A list of ctkPlugins. */ QList getPlugins() const; /** * Returns the customized object for the specified ctkPlugin if * the specified plugin is being tracked by this ctkPluginTracker. * * @param plugin The ctkPlugin being tracked. * @return The customized object for the specified ctkPlugin or * null if the specified ctkPlugin is not * being tracked. */ QVariant getObject(ctkPlugin* plugin) const; /** * Remove a plugin from this ctkPluginTracker. * * The specified plugin will be removed from this ctkPluginTracker. * If the specified plugin was being tracked then the * ctkPluginTrackerCustomizer::removedPlugin method will be called * for that plugin. * * @param plugin The ctkPlugin to be removed. */ void remove(ctkPlugin* plugin); /** * Return the number of plugins being tracked by this * ctkPluginTracker. * * @return The number of plugins being tracked. */ int size() const; /** * Returns the tracking count for this ctkPluginTracker. * * The tracking count is initialized to 0 when this * ctkPluginTracker is opened. Every time a plugin is added, * modified or removed from this ctkPluginTracker the tracking * count is incremented. * *

* The tracking count can be used to determine if this * ctkPluginTracker has added, modified or removed a plugin by * comparing a tracking count value previously collected with the current * tracking count value. If the value has not changed, then no plugin has * been added, modified or removed from this ctkPluginTracker * since the previous tracking count was collected. * * @return The tracking count for this ctkPluginTracker or -1 if * this ctkPluginTracker is not open. */ int getTrackingCount() const; protected: /** * Default implementation of the * ctkPluginTrackerCustomizer::addingPlugin method. * *

* This method is only called when this ctkPluginTracker has been * constructed with a null ctkPluginTrackerCustomizer argument. * *

* This implementation simply returns the specified ctkPlugin* in * a QVariant. * *

* This method can be overridden in a subclass to customize the object to be * tracked for the plugin being added. * * @param plugin The ctkPlugin being added to this * ctkPluginTracker object. * @param event The plugin event which caused this customizer method to be * called or an invalid event if there is no plugin event associated * with the call to this method. * @return The specified plugin. * @see ctkPluginTrackerCustomizer::addingPlugin(ctkPlugin*, const ctkPluginEvent&) */ QVariant addingPlugin(ctkPlugin* plugin, const ctkPluginEvent& event); /** * Default implementation of the * ctkPluginTrackerCustomizer::modifiedPlugin method. * *

* This method is only called when this ctkPluginTracker has been * constructed with a null ctkPluginTrackerCustomizer argument. * *

* This implementation does nothing. * * @param plugin The ctkPlugin whose state has been modified. * @param event The plugin event which caused this customizer method to be * called or an invalid event if there is no plugin event associated * with the call to this method. * @param object The customized object for the specified ctkPlugin. * @see ctkPluginTrackerCustomizer::modifiedPlugin(ctkPlugin*, const ctkPluginEvent&, QVariant) */ void modifiedPlugin(ctkPlugin* plugin, const ctkPluginEvent& event, QVariant object); /** * Default implementation of the * ctkPluginTrackerCustomizer::removedPlugin method. * *

* This method is only called when this ctkPluginTracker has been * constructed with a null ctkPluginTrackerCustomizer argument. * *

* This implementation does nothing. * * @param plugin The ctkPlugin being removed. * @param event The plugin event which caused this customizer method to be * called or an invalid event if there is no plugin event associated * with the call to this method. * @param object The customized object for the specified plugin. * @see ctkPluginTrackerCustomizer::removedPlugin(ctkPlugin*, const ctkPluginEvent&, QVariant) */ void removedPlugin(ctkPlugin* plugin, const ctkPluginEvent& event, QVariant object); private: friend class ctkTrackedPlugin; Q_DECLARE_PRIVATE(ctkPluginTracker) const QScopedPointer d_ptr; }; #endif // CTKPLUGINTRACKER_H