From 77dc72d3e5fb0142e876dbfd13d44bf44e49d01d Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@digia.com>
Date: Tue, 12 Feb 2013 14:02:07 +0100
Subject: [PATCH] Library-Wizard: Write plugins compatible with Qt 4 / 5.

Add json-file and #ifdef plugin export for use with Qt 4 and 5.

Change-Id: Ib04f2e56be08634f00c8aa2cde45837b9a138cbe
Reviewed-by: hjk <hjk121@nokiamail.com>
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
---
 .../wizards/libraryparameters.cpp             | 23 +++++++++++++---
 .../wizards/libraryparameters.h               |  1 +
 .../wizards/librarywizard.cpp                 | 17 +++++++++++-
 .../wizards/librarywizarddialog.cpp           | 26 +++++++++++++------
 .../wizards/librarywizarddialog.h             |  2 ++
 5 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/src/plugins/qt4projectmanager/wizards/libraryparameters.cpp b/src/plugins/qt4projectmanager/wizards/libraryparameters.cpp
index 4c1926203cc..28ef629b4bf 100644
--- a/src/plugins/qt4projectmanager/wizards/libraryparameters.cpp
+++ b/src/plugins/qt4projectmanager/wizards/libraryparameters.cpp
@@ -28,6 +28,7 @@
 ****************************************************************************/
 
 #include "libraryparameters.h"
+#include "librarywizarddialog.h"
 
 #include <utils/codegeneration.h>
 
@@ -61,6 +62,7 @@ void LibraryParameters::generateCode(QtProjectParameters:: Type t,
                                      const QString &headerName,
                                      const QString &sharedHeader,
                                      const QString &exportMacro,
+                                     const QString &pluginJsonFileName,
                                      int indentation,
                                      QString *header,
                                      QString *source) const
@@ -107,7 +109,19 @@ void LibraryParameters::generateCode(QtProjectParameters:: Type t,
     const bool inheritsQObject = t == QtProjectParameters::Qt4Plugin;
     if (inheritsQObject)
         headerStr << namespaceIndent << indent << "Q_OBJECT\n";
-    headerStr << namespaceIndent << "public:\n";
+    if (t == QtProjectParameters::Qt4Plugin) { // Write Qt 5 plugin meta data.
+        const QString qt5InterfaceName = LibraryWizardDialog::pluginInterface(baseClassName);
+        if (!qt5InterfaceName.isEmpty()) {
+            headerStr << "#if QT_VERSION >= 0x050000\n"
+                      << namespaceIndent << indent << "Q_PLUGIN_METADATA(IID \""
+                      << qt5InterfaceName << '"';
+            if (!pluginJsonFileName.isEmpty())
+                headerStr << " FILE \"" << pluginJsonFileName << '"';
+            headerStr << ")\n#endif // QT_VERSION >= 0x050000\n";
+        }
+    }
+
+    headerStr << namespaceIndent << "\npublic:\n";
     if (inheritsQObject)
         headerStr << namespaceIndent << indent << unqualifiedClassName << "(QObject *parent = 0);\n";
     else
@@ -134,8 +148,11 @@ void LibraryParameters::generateCode(QtProjectParameters:: Type t,
 
     Utils::writeClosingNameSpaces(namespaceList, indent, sourceStr);
 
-    if (t == QtProjectParameters::Qt4Plugin)
-        sourceStr << '\n' << "Q_EXPORT_PLUGIN2(" << projectTarget << ", " << className << ")\n";
+    if (t == QtProjectParameters::Qt4Plugin) { // Qt 4 plugin export
+        sourceStr << "\n#if QT_VERSION < 0x050000\n"
+                  << "Q_EXPORT_PLUGIN2(" << projectTarget << ", " << className << ")\n"
+                  << "#endif // QT_VERSION < 0x050000\n";
+    }
 }
 
 QString  LibraryParameters::generateSharedHeader(const QString &globalHeaderFileName,
diff --git a/src/plugins/qt4projectmanager/wizards/libraryparameters.h b/src/plugins/qt4projectmanager/wizards/libraryparameters.h
index 2253f146880..0fe2471fb6b 100644
--- a/src/plugins/qt4projectmanager/wizards/libraryparameters.h
+++ b/src/plugins/qt4projectmanager/wizards/libraryparameters.h
@@ -47,6 +47,7 @@ struct LibraryParameters {
                       const QString &headerName,
                       const QString &sharedHeader,
                       const QString &exportMacro,
+                      const QString &pluginJsonFileName,
                       int indentation,
                       QString *header,
                       QString *source) const;
diff --git a/src/plugins/qt4projectmanager/wizards/librarywizard.cpp b/src/plugins/qt4projectmanager/wizards/librarywizard.cpp
index 3740515c2ec..fd98474c4c1 100644
--- a/src/plugins/qt4projectmanager/wizards/librarywizard.cpp
+++ b/src/plugins/qt4projectmanager/wizards/librarywizard.cpp
@@ -95,6 +95,13 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w,
 
     const QString headerFileFullName = buildFileName(projectPath, params.headerFileName, headerSuffix());
     const QString headerFileName = QFileInfo(headerFileFullName).fileName();
+    QString pluginJsonFileFullName;
+    QString pluginJsonFileName;
+    if (projectParams.type == QtProjectParameters::Qt4Plugin) {
+        pluginJsonFileFullName = buildFileName(projectPath, projectParams.fileName, QLatin1String("json"));
+        pluginJsonFileName = QFileInfo(pluginJsonFileFullName).fileName();
+    }
+
     Core::GeneratedFile header(headerFileFullName);
 
     // Create files: global header for shared libs
@@ -111,7 +118,7 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w,
     // Generate code
     QString headerContents, sourceContents;
     params.generateCode(projectParams.type, projectParams.fileName,  headerFileName,
-                        globalHeaderFileName, sharedLibExportMacro,
+                        globalHeaderFileName, sharedLibExportMacro, pluginJsonFileName,
                         /* indentation*/ 4, &headerContents, &sourceContents);
 
     source.setContents(CppTools::AbstractEditorSupport::licenseTemplate(sourceFileName, params.className)
@@ -133,11 +140,19 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w,
                << "\n\nHEADERS += " << headerFileName;
         if (!globalHeaderFileName.isEmpty())
             proStr << "\\\n        " << globalHeaderFileName << '\n';
+        if (!pluginJsonFileName.isEmpty())
+            proStr << "\nOTHER_FILES += " << pluginJsonFileName << '\n';
         if (mobileParams.type)
             mobileParams.writeProFile(proStr);
     }
     profile.setContents(profileContents);
     rc.push_back(profile);
+
+    if (!pluginJsonFileName.isEmpty()) {
+        Core::GeneratedFile jsonFile(pluginJsonFileFullName);
+        jsonFile.setContents(QLatin1String("{\n    \"Keys\" : [ ]\n}\n"));
+        rc.push_back(jsonFile);
+    }
     return rc;
 }
 
diff --git a/src/plugins/qt4projectmanager/wizards/librarywizarddialog.cpp b/src/plugins/qt4projectmanager/wizards/librarywizarddialog.cpp
index edd278eac0b..8f799f107ac 100644
--- a/src/plugins/qt4projectmanager/wizards/librarywizarddialog.cpp
+++ b/src/plugins/qt4projectmanager/wizards/librarywizarddialog.cpp
@@ -58,18 +58,20 @@ struct PluginBaseClasses {
     // blank separated list or 0
     const char *dependentModules;
     const char *targetDirectory;
+    const char *pluginInterface;
 };
 
 static const PluginBaseClasses pluginBaseClasses[] =
 {
-    { "QAccessiblePlugin", "QtGui", "QtCore", "accessible" },
-    { "QDecorationPlugin", "QtGui", "QtCore", 0},
-    { "QIconEnginePluginV2", "QtGui", "QtCore", "imageformats" },
-    { "QImageIOPlugin", "QtGui", "QtCore", "imageformats" },
-    { "QScriptExtensionPlugin", "QtScript", "QtCore", 0 },
-    { "QSqlDriverPlugin", "QtSql", "QtCore", "sqldrivers" },
-    { "QStylePlugin", "QtGui", "QtCore", "styles" },
-    { "QTextCodecPlugin", "QtCore", 0, "codecs" }
+    { "QAccessiblePlugin", "QtGui", "QtCore", "accessible", "QAccessibleFactoryInterface" },
+    { "QDecorationPlugin", "QtGui", "QtCore", 0, 0 }, // Qt 4 only.
+    { "QIconEnginePluginV2", "QtGui", "QtCore", "imageformats", 0 }, // Qt 4 only.
+    { "QIconEnginePlugin", "QtGui", "QtCore", "imageformats", "QIconEngineFactoryInterface" },
+    { "QImageIOPlugin", "QtGui", "QtCore", "imageformats",  "QImageIOHandlerFactoryInterface" },
+    { "QScriptExtensionPlugin", "QtScript", "QtCore", 0, "QScriptExtensionInterface" },
+    { "QSqlDriverPlugin", "QtSql", "QtCore", "sqldrivers", "QSqlDriverFactoryInterface" },
+    { "QStylePlugin", "QtGui", "QtCore", "styles", "QStyleFactoryInterface" },
+    { "QTextCodecPlugin", "QtCore", 0, "codecs", 0 } // Qt 4 only.
 };
 
 enum { defaultPluginBaseClass = 6 };
@@ -357,6 +359,14 @@ LibraryParameters LibraryWizardDialog::libraryParameters() const
     return rc;
 }
 
+QString LibraryWizardDialog::pluginInterface(const QString &baseClass)
+{
+    if (const PluginBaseClasses *plb = findPluginBaseClass(baseClass))
+        if (plb->pluginInterface)
+            return QLatin1String("org.qt-project.Qt.") + QLatin1String(plb->pluginInterface);
+    return QString();
+}
+
 MobileLibraryParameters LibraryWizardDialog::mobileLibraryParameters() const
 {
     MobileLibraryParameters mlp;
diff --git a/src/plugins/qt4projectmanager/wizards/librarywizarddialog.h b/src/plugins/qt4projectmanager/wizards/librarywizarddialog.h
index 452f6a2cd59..43a2702e6a2 100644
--- a/src/plugins/qt4projectmanager/wizards/librarywizarddialog.h
+++ b/src/plugins/qt4projectmanager/wizards/librarywizarddialog.h
@@ -61,6 +61,8 @@ public:
     LibraryParameters libraryParameters() const;
     MobileLibraryParameters mobileLibraryParameters() const;
 
+    static QString pluginInterface(const QString &baseClass);
+
     virtual int nextId() const;
 
 protected:
-- 
GitLab