From 6e4aa3f8b9838a46d238a95c389579885590044f Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@nokia.com>
Date: Tue, 6 Mar 2012 12:26:18 +0100
Subject: [PATCH] WelcomePage: add ids to IWelcomePage and allow replacing

This patch allows replacing specific pages by id() and priority().

Change-Id: Ief0b01e6a43bdc187625c0b7aa97745588322e5e
Reviewed-by: hjk <qthjk@ovi.com>
---
 src/libs/utils/iwelcomepage.h                 |  9 ++++
 .../projectexplorer/projectwelcomepage.cpp    |  7 +++
 .../projectexplorer/projectwelcomepage.h      |  1 +
 .../qtsupport/gettingstartedwelcomepage.cpp   | 14 +++++-
 .../qtsupport/gettingstartedwelcomepage.h     |  2 +
 src/plugins/welcome/welcomeplugin.cpp         | 43 ++++++++++++++++++-
 6 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/src/libs/utils/iwelcomepage.h b/src/libs/utils/iwelcomepage.h
index 38a973bb7cb..67b2de93b5b 100644
--- a/src/libs/utils/iwelcomepage.h
+++ b/src/libs/utils/iwelcomepage.h
@@ -54,6 +54,14 @@ class QTCREATOR_UTILS_EXPORT IWelcomePage : public QObject
     Q_PROPERTY(bool hasSearchBar READ hasSearchBar CONSTANT)
 
 public:
+    enum Id {
+        GettingStarted = 0,
+        Develop = 1,
+        Examples = 2,
+        Tutorials = 3,
+        UserDefined = 32
+    };
+
     IWelcomePage();
     virtual ~IWelcomePage();
 
@@ -62,6 +70,7 @@ public:
     virtual int priority() const { return 0; }
     virtual void facilitateQml(QDeclarativeEngine *) {}
     virtual bool hasSearchBar() const { return false; }
+    virtual Id id() const = 0;
 
 private:
     // not used atm
diff --git a/src/plugins/projectexplorer/projectwelcomepage.cpp b/src/plugins/projectexplorer/projectwelcomepage.cpp
index ee4c436d5a7..f26e4d2e458 100644
--- a/src/plugins/projectexplorer/projectwelcomepage.cpp
+++ b/src/plugins/projectexplorer/projectwelcomepage.cpp
@@ -32,6 +32,8 @@
 
 #include "projectwelcomepage.h"
 
+#include "projectexplorerconstants.h"
+
 #include <utils/stringutils.h>
 
 #include <QDeclarativeEngine>
@@ -216,6 +218,11 @@ void ProjectWelcomePage::facilitateQml(QDeclarativeEngine *engine)
     ctx->setContextProperty(QLatin1String("projectWelcomePage"), this);
 }
 
+ProjectWelcomePage::Id ProjectWelcomePage::id() const
+{
+    return Develop;
+}
+
 void ProjectWelcomePage::reloadWelcomeScreenData()
 {
     if (m_sessionModel)
diff --git a/src/plugins/projectexplorer/projectwelcomepage.h b/src/plugins/projectexplorer/projectwelcomepage.h
index 2e85551b2ef..5c0d963a3b5 100644
--- a/src/plugins/projectexplorer/projectwelcomepage.h
+++ b/src/plugins/projectexplorer/projectwelcomepage.h
@@ -101,6 +101,7 @@ public:
     QWidget *page() { return 0; }
     QString title() const { return tr("Develop"); }
     int priority() const { return 20; }
+    Id id() const;
 
     void reloadWelcomeScreenData();
 
diff --git a/src/plugins/qtsupport/gettingstartedwelcomepage.cpp b/src/plugins/qtsupport/gettingstartedwelcomepage.cpp
index 5b112daa2c0..496094ee923 100644
--- a/src/plugins/qtsupport/gettingstartedwelcomepage.cpp
+++ b/src/plugins/qtsupport/gettingstartedwelcomepage.cpp
@@ -35,6 +35,8 @@
 #include "exampleslistmodel.h"
 #include "screenshotcropper.h"
 
+#include "qtsupportconstants.h"
+
 #include <utils/pathchooser.h>
 #include <utils/fileutils.h>
 
@@ -210,7 +212,7 @@ QString GettingStartedWelcomePage::title() const
 
 int GettingStartedWelcomePage::priority() const
 {
-    return 0;
+    return 4;
 }
 
 void GettingStartedWelcomePage::facilitateQml(QDeclarativeEngine *engine)
@@ -218,6 +220,11 @@ void GettingStartedWelcomePage::facilitateQml(QDeclarativeEngine *engine)
     m_engine = engine;
 }
 
+GettingStartedWelcomePage::Id GettingStartedWelcomePage::id() const
+{
+    return GettingStarted;
+}
+
 ExamplesWelcomePage::ExamplesWelcomePage()
     : m_engine(0),  m_showExamples(false)
 {
@@ -281,6 +288,11 @@ void ExamplesWelcomePage::facilitateQml(QDeclarativeEngine *engine)
     rootContenxt->setContextProperty(QLatin1String("gettingStarted"), this);
 }
 
+ExamplesWelcomePage::Id ExamplesWelcomePage::id() const
+{
+    return m_showExamples ? Examples : Tutorials;
+}
+
 void ExamplesWelcomePage::openSplitHelp(const QUrl &help)
 {
     Core::ICore::helpManager()->handleHelpRequest(help.toString()+QLatin1String("?view=split"));
diff --git a/src/plugins/qtsupport/gettingstartedwelcomepage.h b/src/plugins/qtsupport/gettingstartedwelcomepage.h
index aefa4fdeeea..678b9bea02d 100644
--- a/src/plugins/qtsupport/gettingstartedwelcomepage.h
+++ b/src/plugins/qtsupport/gettingstartedwelcomepage.h
@@ -58,6 +58,7 @@ public:
     QString title() const;
     int priority() const;
     void facilitateQml(QDeclarativeEngine *);
+    Id id() const;
 
 private:
     QDeclarativeEngine *m_engine;
@@ -76,6 +77,7 @@ public:
     int priority() const;
     bool hasSearchBar() const;
     void facilitateQml(QDeclarativeEngine *);
+    Id id() const;
     Q_INVOKABLE QStringList tagList() const;
     Q_INVOKABLE void openUrl(const QUrl &url);
 
diff --git a/src/plugins/welcome/welcomeplugin.cpp b/src/plugins/welcome/welcomeplugin.cpp
index b34e36deee9..37e23f27092 100644
--- a/src/plugins/welcome/welcomeplugin.cpp
+++ b/src/plugins/welcome/welcomeplugin.cpp
@@ -208,8 +208,30 @@ void WelcomeMode::initPlugins()
     QDeclarativeContext *ctx = m_welcomePage->rootContext();
     ctx->setContextProperty(QLatin1String("welcomeMode"), this);
 
-    QList<Utils::IWelcomePage*> plugins = PluginManager::instance()->getObjects<Utils::IWelcomePage>();
-    qSort(plugins.begin(), plugins.end(), &sortFunction);
+    QList<Utils::IWelcomePage*> duplicatePlugins = PluginManager::instance()->getObjects<Utils::IWelcomePage>();
+    qSort(duplicatePlugins.begin(), duplicatePlugins.end(), &sortFunction);
+
+    QList<Utils::IWelcomePage*> plugins;
+    QHash<Utils::IWelcomePage::Id, Utils::IWelcomePage*> pluginHash;
+
+    //avoid duplicate ids - choose by priority
+    foreach (Utils::IWelcomePage* plugin, duplicatePlugins) {
+        if (pluginHash.contains(plugin->id())) {
+            Utils::IWelcomePage* pluginOther = pluginHash.value(plugin->id());
+
+            if (pluginOther->priority() > plugin->priority()) {
+                plugins.removeAll(pluginOther);
+                pluginHash.remove(pluginOther->id());
+                plugins << plugin;
+                pluginHash.insert(plugin->id(), plugin);
+            }
+
+        } else {
+            plugins << plugin;
+            pluginHash.insert(plugin->id(), plugin);
+        }
+    }
+
 
     QDeclarativeEngine *engine = m_welcomePage->engine();
     if (!debug)
@@ -252,7 +274,24 @@ QString WelcomeMode::platform() const
 
 void WelcomeMode::welcomePluginAdded(QObject *obj)
 {
+    QHash<Utils::IWelcomePage::Id, Utils::IWelcomePage*> pluginHash;
+
+    foreach (QObject *obj, m_pluginList) {
+        Utils::IWelcomePage *plugin = qobject_cast<Utils::IWelcomePage*>(obj);
+        pluginHash.insert(plugin->id(), plugin);
+    }
     if (Utils::IWelcomePage *plugin = qobject_cast<Utils::IWelcomePage*>(obj)) {
+        //check for duplicated id
+        if (pluginHash.contains(plugin->id())) {
+            Utils::IWelcomePage* pluginOther = pluginHash.value(plugin->id());
+
+            if (pluginOther->priority() > plugin->priority()) {
+                m_pluginList.removeAll(pluginOther);
+            } else {
+                return;
+            }
+        }
+
         int insertPos = 0;
         foreach (Utils::IWelcomePage* p, PluginManager::instance()->getObjects<Utils::IWelcomePage>()) {
             if (plugin->priority() < p->priority())
-- 
GitLab