Browse Source

Added convenience method for stopping plug-ins or the framework.

Sascha Zelzer 13 years ago
parent
commit
489a12d25c

+ 63 - 0
Libs/PluginFramework/ctkPluginFrameworkLauncher.cpp

@@ -163,6 +163,69 @@ bool ctkPluginFrameworkLauncher::start(const QString& symbolicName, ctkPlugin::S
 }
 
 //----------------------------------------------------------------------------
+bool ctkPluginFrameworkLauncher::stop(const QString& symbolicName,
+                                      ctkPlugin::StopOptions options, ctkPluginContext* context)
+{
+  if (d->fwFactory == 0) return true;
+
+  ctkPluginContext* pc = context ? context : getPluginContext();
+  if (pc == 0)
+  {
+    qWarning() << "No valid plug-in context available";
+    return false;
+  }
+
+  if(!symbolicName.isEmpty())
+  {
+    QString pluginPath = getPluginPath(symbolicName);
+    if (pluginPath.isEmpty()) return false;
+
+    try
+    {
+      QList<QSharedPointer<ctkPlugin> > plugins = pc->getPlugins();
+      foreach(QSharedPointer<ctkPlugin> plugin, plugins)
+      {
+        if (plugin->getSymbolicName() == symbolicName)
+        {
+          plugin->stop(options);
+          return true;
+        }
+      }
+      qWarning() << "Plug-in" << symbolicName << "not found";
+      return false;
+    }
+    catch (const ctkPluginException& exc)
+    {
+      qWarning() << "Failed to stop plug-in:" << exc;
+      return false;
+    }
+  }
+  else
+  {
+    // stop the framework
+    QSharedPointer<ctkPluginFramework> fw =
+        qSharedPointerCast<ctkPluginFramework>(pc->getPlugin(0));
+    try
+    {
+      fw->stop();
+      ctkPluginFrameworkEvent fe = fw->waitForStop(5000);
+      if (fe.getType() == ctkPluginFrameworkEvent::WAIT_TIMEDOUT)
+      {
+        qWarning() << "Stopping the plugin framework timed out";
+        return false;
+      }
+    }
+    catch (const ctkRuntimeException& e)
+    {
+      qWarning() << "Stopping the plugin framework failed: " << e;
+      return false;
+    }
+
+    return true;
+  }
+}
+
+//----------------------------------------------------------------------------
 ctkPluginContext* ctkPluginFrameworkLauncher::getPluginContext()
 {
   if (d->fwFactory == 0) return 0;

+ 22 - 1
Libs/PluginFramework/ctkPluginFrameworkLauncher.h

@@ -100,13 +100,34 @@ public:
    * \return <code>true</code> if the plugin was found and successfully
    *         installed, <code>false</code> otherwise.
    *
-   * \see ctkPlugin::StartOption
+   * \see ctkPlugin::StartOptions
    */
   static bool start(const QString& symbolicName = QString(),
                     ctkPlugin::StartOptions options = ctkPlugin::START_ACTIVATION_POLICY,
                     ctkPluginContext* context = 0);
 
   /**
+   * This method either stops the plug-in with the given <code>symbolicName</code> using
+   * the supplied stop options <code>options</code>
+   * or the complete framework (if <code>symbolicName</code> is empty).
+   *
+   * <p>
+   * If a plugin context is provided, this context is used to find the plug-in,
+   * otherwise the Plugin Framework context is used.
+   *
+   * \param symbolicName The symbolic name of the plugin to stop.
+   * \param options The options used to stop the plugin.
+   * \param context The plugin context to use for finding the plugin.
+   * \return <code>true</code> if the plugin was found and successfully
+   *         stopped or the complete framework was successfully stopped,
+   *         <code>false</code> otherwise.
+   *
+   * \see ctkPlugin::StopOptions
+   */
+  static bool stop(const QString& symbolicName = QString(),
+                    ctkPlugin::StopOptions options = 0, ctkPluginContext* context = 0);
+
+  /**
    * Get the plugin context for the Plugin Framework.
    *
    * \return The context associated to the Plugin Framework, or <code>null</code>