From 714f7656a6a6968f919e6727995f6ccfdba440bb Mon Sep 17 00:00:00 2001
From: Christian Kandeler <christian.kandeler@nokia.com>
Date: Wed, 24 Nov 2010 15:51:48 +0100
Subject: [PATCH] Maemo: Fix desktop file in binary package.

Ensure that the desktop files we deploy contain the right path
to the executable.

Reviewed-by: kh1
---
 .../qt-maemo/maemopackagecreationstep.cpp     | 78 +++++++++++++------
 .../qt-maemo/maemopackagecreationstep.h       |  7 +-
 2 files changed, 62 insertions(+), 23 deletions(-)

diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.cpp
index 2e63c08db94..1023548d047 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.cpp
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.cpp
@@ -245,10 +245,6 @@ bool MaemoPackageCreationStep::copyDebianFiles(bool inSourceBuild)
         ->debianDirPath(buildConfiguration()->target()->project());
     QDir templatesDir(templatesDirPath);
     const QStringList &files = templatesDir.entryList(QDir::Files);
-    const bool harmattanWorkaroundNeeded
-        = maemoToolChain()->version() == MaemoToolChain::Maemo6
-            && !qt4BuildConfiguration()->qt4Target()->qt4Project()
-                   ->applicationProFiles().isEmpty();
     foreach (const QString &fileName, files) {
         const QString srcFile
                 = templatesDirPath + QLatin1Char('/') + fileName;
@@ -261,9 +257,8 @@ bool MaemoPackageCreationStep::copyDebianFiles(bool inSourceBuild)
             return false;
         }
 
-        // Workaround for Harmattan icon bug
-        if (harmattanWorkaroundNeeded && fileName == QLatin1String("rules"))
-            addWorkaroundForHarmattanBug(destFile);
+        if (fileName == QLatin1String("rules"))
+            updateDesktopFiles(destFile);
     }
 
     QFile magicFile(magicFilePath);
@@ -580,7 +575,7 @@ QString MaemoPackageCreationStep::packageFileName(const ProjectExplorer::Project
         % QLatin1String("_armel.deb");
 }
 
-void MaemoPackageCreationStep::addWorkaroundForHarmattanBug(const QString &rulesFilePath)
+void MaemoPackageCreationStep::updateDesktopFiles(const QString &rulesFilePath)
 {
     QFile rulesFile(rulesFilePath);
     if (!rulesFile.open(QIODevice::ReadWrite)) {
@@ -597,6 +592,8 @@ void MaemoPackageCreationStep::addWorkaroundForHarmattanBug(const QString &rules
     QString desktopFileDir = QFileInfo(rulesFile).dir().path()
         + QLatin1Char('/') + projectName()
         + QLatin1String("/usr/share/applications/");
+    if (maemoToolChain()->version() == MaemoToolChain::Maemo5)
+        desktopFileDir += QLatin1String("hildon/");
 #ifdef Q_OS_WIN
     desktopFileDir.remove(QLatin1Char(':'));
     desktopFileDir.prepend(QLatin1Char('/'));
@@ -606,27 +603,64 @@ void MaemoPackageCreationStep::addWorkaroundForHarmattanBug(const QString &rules
     int insertPos = makeInstallEol + 1;
     foreach (const Qt4ProFileNode * const proFile, proFiles) {
         const QString appName = proFile->targetInformation().target;
-        const QByteArray lineBefore("Icon=" + appName.toUtf8());
-        const QByteArray lineAfter("Icon=/usr/share/icons/hicolor/64x64/apps/"
-            + appName.toUtf8() + ".png");
+        if (maemoToolChain()->version() == MaemoToolChain::Maemo6) {
+            addWorkaroundForHarmattanBug(content, insertPos,
+                appName, desktopFileDir);
+        }
+
+        QString executableFilePath;
+        for (int i = 0; i < deployStep()->deployables()->modelCount(); ++i) {
+            const MaemoDeployableListModel * const model
+                = deployStep()->deployables()->modelAt(i);
+            if (model->proFilePath() == proFile->path()) {
+                executableFilePath = model->remoteExecutableFilePath();
+                break;
+            }
+        }
+        if (executableFilePath.isEmpty()) {
+            qDebug("%s: Skipping subproject %s with missing deployment information.",
+                Q_FUNC_INFO, qPrintable(proFile->path()));
+            continue;
+        }
+        const QByteArray lineBefore("Exec=.*");
+        const QByteArray lineAfter("Exec=" + executableFilePath.toUtf8());
         const QString desktopFilePath
             = desktopFileDir + appName + QLatin1String(".desktop");
-        const QString tmpFile
-            = desktopFileDir + appName + QLatin1String(".sed");
-        const QByteArray sedCmd = "\tsed 's:" + lineBefore + ':' + lineAfter
-            + ":' " + desktopFilePath.toLocal8Bit() + " > "
-            + tmpFile.toLocal8Bit() + " || echo -n\n";
-        const QByteArray mvCmd = "\tmv " + tmpFile.toLocal8Bit() + ' '
-            + desktopFilePath.toLocal8Bit() + " || echo -n\n";
-        content.insert(insertPos, sedCmd);
-        insertPos += sedCmd.length();
-        content.insert(insertPos, mvCmd);
-        insertPos += mvCmd.length();
+        addSedCmdToRulesFile(content, insertPos, desktopFilePath, lineBefore,
+            lineAfter);
     }
     rulesFile.resize(0);
     rulesFile.write(content);
 }
 
+void MaemoPackageCreationStep::addWorkaroundForHarmattanBug(QByteArray &rulesFileContent,
+    int &insertPos, const QString &appName, const QString &desktopFileDir)
+{
+    const QByteArray lineBefore("Icon=" + appName.toUtf8());
+    const QByteArray lineAfter("Icon=/usr/share/icons/hicolor/64x64/apps/"
+        + appName.toUtf8() + ".png");
+    const QString desktopFilePath
+        = desktopFileDir + appName + QLatin1String(".desktop");
+    addSedCmdToRulesFile(rulesFileContent, insertPos, desktopFilePath,
+        lineBefore, lineAfter);
+}
+
+void MaemoPackageCreationStep::addSedCmdToRulesFile(QByteArray &rulesFileContent,
+    int &insertPos, const QString &desktopFilePath, const QByteArray &oldString,
+    const QByteArray &newString)
+{
+    const QString tmpFilePath = desktopFilePath + QLatin1String(".sed");
+    const QByteArray sedCmd = "\tsed 's:" + oldString + ':' + newString
+        + ":' " + desktopFilePath.toLocal8Bit() + " > "
+        + tmpFilePath.toLocal8Bit() + " || echo -n\n";
+    const QByteArray mvCmd = "\tmv " + tmpFilePath.toLocal8Bit() + ' '
+        + desktopFilePath.toLocal8Bit() + " || echo -n\n";
+    rulesFileContent.insert(insertPos, sedCmd);
+    insertPos += sedCmd.length();
+    rulesFileContent.insert(insertPos, mvCmd);
+    insertPos += mvCmd.length();
+}
+
 const QLatin1String MaemoPackageCreationStep::CreatePackageId("Qt4ProjectManager.MaemoPackageCreationStep");
 
 } // namespace Internal
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.h b/src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.h
index fc1bc581f42..0201c9f21d8 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.h
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.h
@@ -124,7 +124,12 @@ private:
     const Qt4BuildConfiguration *qt4BuildConfiguration() const;
     MaemoDeployStep * deployStep() const;
     void checkProjectName();
-    void addWorkaroundForHarmattanBug(const QString &rulesFilePath);
+    void updateDesktopFiles(const QString &rulesFilePath);
+    void addWorkaroundForHarmattanBug(QByteArray &rulesFileContent,
+        int &insertPos, const QString &appName, const QString &desktopFileDir);
+    void addSedCmdToRulesFile(QByteArray &rulesFileContent, int &insertPos,
+        const QString &desktopFilePath, const QByteArray &oldString,
+        const QByteArray &newString);
 
     static const QLatin1String CreatePackageId;
 
-- 
GitLab