From 4116504c3b5813d9fcbef25f43765303e88148cd Mon Sep 17 00:00:00 2001
From: con <qtc-committer@nokia.com>
Date: Tue, 11 Jan 2011 14:01:08 +0100
Subject: [PATCH] Allow optional plugin dependencies.

Adds a 'type' attribute to the 'dependency' tag, with possible values
'required' and 'optional'.
Optional dependencies may not be linked against. You'll need to use the
new dynamic methods in plugin manager (retrieving objects and calling
methods by name) if you want to access functionality of optional
dependencies.
---
 src/libs/extensionsystem/iplugin.cpp          |  5 +
 .../extensionsystem/plugindetailsview.cpp     |  2 +
 src/libs/extensionsystem/pluginmanager.cpp    |  8 +-
 src/libs/extensionsystem/pluginspec.cpp       | 97 ++++++++++++-------
 src/libs/extensionsystem/pluginspec.h         | 16 ++-
 src/libs/extensionsystem/pluginspec_p.h       |  6 +-
 src/libs/extensionsystem/pluginview.cpp       |  7 +-
 src/plugins/debugger/Debugger.pluginspec.in   |  2 +-
 8 files changed, 93 insertions(+), 50 deletions(-)

diff --git a/src/libs/extensionsystem/iplugin.cpp b/src/libs/extensionsystem/iplugin.cpp
index a15408a1d93..c53bda416ec 100644
--- a/src/libs/extensionsystem/iplugin.cpp
+++ b/src/libs/extensionsystem/iplugin.cpp
@@ -152,6 +152,11 @@
         \o version
         \o The version to which the plugin must be compatible to
            fill the dependency, in the form \c {"x.y.z_n"}.
+           Can be empty if the version does not matter.
+    \row
+        \o type
+        \o Value 'required' or 'optional'. Defines if the dependency is
+           a hard requirement or optional. Defaults to 'required'.
     \endtable
 
     \section2 Example \c plugin.xml
diff --git a/src/libs/extensionsystem/plugindetailsview.cpp b/src/libs/extensionsystem/plugindetailsview.cpp
index 2b43567fa6b..ddfcae3bae3 100644
--- a/src/libs/extensionsystem/plugindetailsview.cpp
+++ b/src/libs/extensionsystem/plugindetailsview.cpp
@@ -95,6 +95,8 @@ void PluginDetailsView::update(PluginSpec *spec)
         QString depString = dep.name;
         depString += QLatin1String(" (");
         depString += dep.version;
+        if (dep.type == PluginDependency::Optional)
+            depString += QLatin1String(", optional");
         depString += QLatin1Char(')');
         depStrings.append(depString);
     }
diff --git a/src/libs/extensionsystem/pluginmanager.cpp b/src/libs/extensionsystem/pluginmanager.cpp
index a8be0a37e49..456730302ea 100644
--- a/src/libs/extensionsystem/pluginmanager.cpp
+++ b/src/libs/extensionsystem/pluginmanager.cpp
@@ -996,7 +996,13 @@ void PluginManagerPrivate::loadPlugin(PluginSpec *spec, PluginSpec::State destSt
     default:
         break;
     }
-    foreach (const PluginSpec *depSpec, spec->dependencySpecs()) {
+    // check if dependencies have loaded without error
+    QHashIterator<PluginDependency, PluginSpec *> it(spec->dependencySpecs());
+    while (it.hasNext()) {
+        it.next();
+        if (it.key().type == PluginDependency::Optional)
+            continue;
+        PluginSpec *depSpec = it.value();
         if (depSpec->state() != destState) {
             spec->d->hasError = true;
             spec->d->errorString =
diff --git a/src/libs/extensionsystem/pluginspec.cpp b/src/libs/extensionsystem/pluginspec.cpp
index 7b18c1a0aae..b4d968367a6 100644
--- a/src/libs/extensionsystem/pluginspec.cpp
+++ b/src/libs/extensionsystem/pluginspec.cpp
@@ -87,6 +87,23 @@
     Version string that a plugin must match to fill this dependency.
 */
 
+/*!
+    \variable ExtensionSystem::PluginDependency::type
+    Defines whether the dependency is required or optional.
+    \sa ExtensionSystem::PluginDependency::Type
+*/
+
+/*!
+    \enum ExtensionSystem::PluginDependency::Type
+    Whether the dependency is required or optional.
+    \value Required
+           Dependency needs to be there.
+    \value Optional
+           Dependency is not necessarily needed. You need to make sure that
+           the plugin is able to load without this dependency installed, so
+           for example you may not link to the dependency's library.
+*/
+
 /*!
     \class ExtensionSystem::PluginSpec
     \brief Contains the information of the plugins xml description file and
@@ -127,16 +144,26 @@
     \value Deleted
             The plugin instance has been deleted.
 */
+
 using namespace ExtensionSystem;
 using namespace ExtensionSystem::Internal;
 
+/*!
+    \fn uint qHash(const ExtensionSystem::PluginDependency &value)
+    \internal
+*/
+uint ExtensionSystem::qHash(const ExtensionSystem::PluginDependency &value)
+{
+    return qHash(value.name);
+}
+
 /*!
     \fn bool PluginDependency::operator==(const PluginDependency &other)
     \internal
 */
-bool PluginDependency::operator==(const PluginDependency &other)
+bool PluginDependency::operator==(const PluginDependency &other) const
 {
-    return name == other.name && version == other.version;
+    return name == other.name && version == other.version && type == other.type;
 }
 
 /*!
@@ -395,22 +422,11 @@ IPlugin *PluginSpec::plugin() const
 
     \sa PluginSpec::dependencies()
 */
-QList<PluginSpec *> PluginSpec::dependencySpecs() const
+QHash<PluginDependency, PluginSpec *> PluginSpec::dependencySpecs() const
 {
     return d->dependencySpecs;
 }
 
-/*!
-    \fn QList<PluginSpec *> PluginSpec::providesForSpecs() const
-    Returns the list of plugins that depend on this one.
-
-    \sa PluginSpec::dependencySpecs()
-*/
-QList<PluginSpec *> PluginSpec::providesForSpecs() const
-{
-    return d->providesSpecs;
-}
-
 //==========PluginSpecPrivate==================
 
 namespace {
@@ -429,6 +445,9 @@ namespace {
     const char * const DEPENDENCY = "dependency";
     const char * const DEPENDENCY_NAME = "name";
     const char * const DEPENDENCY_VERSION = "version";
+    const char * const DEPENDENCY_TYPE = "type";
+    const char * const DEPENDENCY_TYPE_SOFT = "optional";
+    const char * const DEPENDENCY_TYPE_HARD = "required";
     const char * const ARGUMENTLIST = "argumentList";
     const char * const ARGUMENT = "argument";
     const char * const ARGUMENT_NAME = "name";
@@ -726,6 +745,18 @@ void PluginSpecPrivate::readDependencyEntry(QXmlStreamReader &reader)
         reader.raiseError(msgInvalidFormat(DEPENDENCY_VERSION));
         return;
     }
+    dep.type = PluginDependency::Required;
+    if (reader.attributes().hasAttribute(DEPENDENCY_TYPE)) {
+        QString typeValue = reader.attributes().value(DEPENDENCY_TYPE).toString();
+        if (typeValue == QLatin1String(DEPENDENCY_TYPE_HARD)) {
+            dep.type = PluginDependency::Required;
+        } else if (typeValue == QLatin1String(DEPENDENCY_TYPE_SOFT)) {
+            dep.type = PluginDependency::Optional;
+        } else {
+            reader.raiseError(msgInvalidFormat(DEPENDENCY_TYPE));
+            return;
+        }
+    }
     dependencies.append(dep);
     reader.readNext();
     if (reader.tokenType() != QXmlStreamReader::EndElement)
@@ -801,26 +832,27 @@ bool PluginSpecPrivate::resolveDependencies(const QList<PluginSpec *> &specs)
         hasError = true;
         return false;
     }
-    QList<PluginSpec *> resolvedDependencies;
+    QHash<PluginDependency, PluginSpec *> resolvedDependencies;
     foreach (const PluginDependency &dependency, dependencies) {
         PluginSpec *found = 0;
 
         foreach (PluginSpec *spec, specs) {
             if (spec->provides(dependency.name, dependency.version)) {
                 found = spec;
-                spec->d->addProvidesForPlugin(q);
                 break;
             }
         }
         if (!found) {
-            hasError = true;
-            if (!errorString.isEmpty())
-                errorString.append(QLatin1Char('\n'));
-            errorString.append(QCoreApplication::translate("PluginSpec", "Could not resolve dependency '%1(%2)'")
-                .arg(dependency.name).arg(dependency.version));
+            if (dependency.type == PluginDependency::Required) {
+                hasError = true;
+                if (!errorString.isEmpty())
+                    errorString.append(QLatin1Char('\n'));
+                errorString.append(QCoreApplication::translate("PluginSpec", "Could not resolve dependency '%1(%2)'")
+                    .arg(dependency.name).arg(dependency.version));
+            }
             continue;
         }
-        resolvedDependencies.append(found);
+        resolvedDependencies.insert(dependency, found);
     }
     if (hasError)
         return false;
@@ -840,7 +872,12 @@ void PluginSpecPrivate::disableIndirectlyIfDependencyDisabled()
     if (disabledIndirectly)
         return;
 
-    foreach (PluginSpec *dependencySpec, dependencySpecs) {
+    QHashIterator<PluginDependency, PluginSpec *> it(dependencySpecs);
+    while (it.hasNext()) {
+        it.next();
+        if (it.key().type == PluginDependency::Optional)
+            continue;
+        PluginSpec *dependencySpec = it.value();
         if (dependencySpec->isDisabledIndirectly() || !dependencySpec->isEnabled()) {
             disabledIndirectly = true;
             break;
@@ -984,17 +1021,3 @@ void PluginSpecPrivate::kill()
     plugin = 0;
     state = PluginSpec::Deleted;
 }
-
-/*!
-    \fn void PluginSpecPrivate::addProvidesForPlugin(PluginSpec *dependent)
-    \internal
-*/
-void PluginSpecPrivate::addProvidesForPlugin(PluginSpec *dependent)
-{
-    providesSpecs.append(dependent);
-}
-
-void PluginSpecPrivate::removeProvidesForPlugin(PluginSpec *dependent)
-{
-    providesSpecs.removeOne(dependent);
-}
diff --git a/src/libs/extensionsystem/pluginspec.h b/src/libs/extensionsystem/pluginspec.h
index 39958441412..c9cc380b894 100644
--- a/src/libs/extensionsystem/pluginspec.h
+++ b/src/libs/extensionsystem/pluginspec.h
@@ -38,6 +38,7 @@
 
 #include <QtCore/QString>
 #include <QtCore/QList>
+#include <QtCore/QHash>
 
 QT_BEGIN_NAMESPACE
 class QStringList;
@@ -54,11 +55,19 @@ class IPlugin;
 
 struct EXTENSIONSYSTEM_EXPORT PluginDependency
 {
+    enum Type {
+        Required,
+        Optional
+    };
+
     QString name;
     QString version;
-    bool operator==(const PluginDependency &other);
+    Type type;
+    bool operator==(const PluginDependency &other) const;
 };
 
+uint qHash(const ExtensionSystem::PluginDependency &value);
+
 struct EXTENSIONSYSTEM_EXPORT PluginArgumentDescription
 {
     QString name;
@@ -105,10 +114,7 @@ public:
     bool provides(const QString &pluginName, const QString &version) const;
 
     // dependency specs, valid after 'Resolved' state is reached
-    QList<PluginSpec *> dependencySpecs() const;
-
-    // list of plugins that depend on this - e.g. this plugins provides for them
-    QList<PluginSpec *> providesForSpecs() const;
+    QHash<PluginDependency, PluginSpec *> dependencySpecs() const;
 
     // linked plugin instance, valid after 'Loaded' state is reached
     IPlugin *plugin() const;
diff --git a/src/libs/extensionsystem/pluginspec_p.h b/src/libs/extensionsystem/pluginspec_p.h
index 767620e4dc1..921e40432e6 100644
--- a/src/libs/extensionsystem/pluginspec_p.h
+++ b/src/libs/extensionsystem/pluginspec_p.h
@@ -82,8 +82,7 @@ public:
     QString filePath;
     QStringList arguments;
 
-    QList<PluginSpec *> providesSpecs;
-    QList<PluginSpec *> dependencySpecs;
+    QHash<PluginDependency, PluginSpec *> dependencySpecs;
     PluginSpec::PluginArgumentDescriptions argumentDescriptions;
     IPlugin *plugin;
 
@@ -94,9 +93,6 @@ public:
     static bool isValidVersion(const QString &version);
     static int versionCompare(const QString &version1, const QString &version2);
 
-    // add/remove from providesSpecs
-    void addProvidesForPlugin(PluginSpec *dependent);
-    void removeProvidesForPlugin(PluginSpec *dependent);
     void disableIndirectlyIfDependencyDisabled();
 
 
diff --git a/src/libs/extensionsystem/pluginview.cpp b/src/libs/extensionsystem/pluginview.cpp
index 38d9d8b8240..2fa382706bd 100644
--- a/src/libs/extensionsystem/pluginview.cpp
+++ b/src/libs/extensionsystem/pluginview.cpp
@@ -351,7 +351,12 @@ void PluginView::updatePluginDependencies()
         if (m_whitelist.contains(spec->name()))
             continue;
 
-        foreach(const PluginSpec *depSpec, spec->dependencySpecs()) {
+        QHashIterator<PluginDependency, PluginSpec *> it(spec->dependencySpecs());
+        while (it.hasNext()) {
+            it.next();
+            if (it.key().type == PluginDependency::Optional)
+                continue;
+            PluginSpec *depSpec = it.value();
             if (!depSpec->isEnabled() || depSpec->isDisabledIndirectly()) {
                 disableIndirectly = true;
                 break;
diff --git a/src/plugins/debugger/Debugger.pluginspec.in b/src/plugins/debugger/Debugger.pluginspec.in
index 28099c5cb42..e7dff0dd6fe 100644
--- a/src/plugins/debugger/Debugger.pluginspec.in
+++ b/src/plugins/debugger/Debugger.pluginspec.in
@@ -18,7 +18,7 @@ Alternatively, this plugin may be used under the terms of the GNU Lesser General
         <dependency name=\"Core\" version=\"$$QTCREATOR_VERSION\"/>
         <dependency name=\"Find\" version=\"$$QTCREATOR_VERSION\"/>
         <!-- Debugger plugin adds items to the editor's context menu -->
-        <dependency name=\"CppEditor\" version=\"$$QTCREATOR_VERSION\" type=\"soft\"/>
+        <dependency name=\"CppEditor\" version=\"$$QTCREATOR_VERSION\" type=\"optional\"/>
     </dependencyList>
     <argumentList>
         <argument name=\"-disable-cdb\">Disable Cdb debugger engine</argument>
-- 
GitLab