From 7dc88639dc453803dad19467b9853e31a5f74d1e Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@nokia.com>
Date: Tue, 11 Oct 2011 15:21:00 +0200
Subject: [PATCH] QmlProfiler: Allow user to specify a sysroot for "Attach to
 Port"

Change-Id: I943255e5a501610195e3758f9fbb42d127f15524
Reviewed-on: http://codereview.qt-project.org/6451
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Aurindam Jana <aurindam.jana@nokia.com>
---
 src/libs/utils/fileinprojectfinder.cpp        | 32 ++++++++++++++++---
 src/libs/utils/fileinprojectfinder.h          |  2 ++
 src/plugins/debugger/attachtoqmlportdialog.ui | 28 +++++++++++++---
 src/plugins/debugger/debuggerdialogs.cpp      | 10 ++++++
 src/plugins/debugger/debuggerdialogs.h        |  3 ++
 src/plugins/debugger/debuggerplugin.cpp       |  6 ++++
 src/plugins/debugger/qml/qmlengine.cpp        | 10 +++---
 7 files changed, 76 insertions(+), 15 deletions(-)

diff --git a/src/libs/utils/fileinprojectfinder.cpp b/src/libs/utils/fileinprojectfinder.cpp
index d0f3b82f8c6..8fcaca31d59 100644
--- a/src/libs/utils/fileinprojectfinder.cpp
+++ b/src/libs/utils/fileinprojectfinder.cpp
@@ -86,15 +86,27 @@ void FileInProjectFinder::setProjectFiles(const QStringList &projectFiles)
     m_cache.clear();
 }
 
+void FileInProjectFinder::setSysroot(const QString &sysroot)
+{
+    QString newsys = sysroot;
+    while (newsys.endsWith(QLatin1Char('/')))
+        newsys.remove(newsys.length() - 1, 1);
+
+    if (m_sysroot == newsys)
+        return;
+
+    m_sysroot = newsys;
+    m_cache.clear();
+}
+
 /**
   Returns the best match for the given file url in the project directory.
 
   The method first checks whether the file inside the project directory exists.
   If not, the leading directory in the path is stripped, and the - now shorter - path is
-  checked for existence. This continues until either the file is found, or the relative path
-  does not contain any directories any more: In this case the path of the url is returned.
-
-  Second, we walk the list of project files, and search for a file name match there.
+  checked for existence, and so on. Second, it tries to locate the file in the sysroot
+  folder specified. Third, we walk the list of project files, and search for a file name match
+  there. If all fails, it returns the original path from the file url.
   */
 QString FileInProjectFinder::findFile(const QUrl &fileUrl, bool *success) const
 {
@@ -167,6 +179,18 @@ QString FileInProjectFinder::findFile(const QUrl &fileUrl, bool *success) const
         }
     }
 
+    // check if absolute path is found in sysroot
+    if (!m_sysroot.isEmpty()) {
+        const QString sysrootPath = m_sysroot + QLatin1Char('/') + originalPath;
+        if (QFileInfo(sysrootPath).exists() && QFileInfo(sysrootPath).isFile()) {
+            if (success)
+                *success = true;
+            m_cache.insert(originalPath, sysrootPath);
+            return sysrootPath;
+        }
+    }
+
+    // finally, find solely by filename in project files
     const QString fileName = QFileInfo(originalPath).fileName();
     foreach (const QString &f, m_projectFiles) {
         if (QFileInfo(f).fileName() == fileName) {
diff --git a/src/libs/utils/fileinprojectfinder.h b/src/libs/utils/fileinprojectfinder.h
index b8941b80d76..5930cf57cfc 100644
--- a/src/libs/utils/fileinprojectfinder.h
+++ b/src/libs/utils/fileinprojectfinder.h
@@ -51,11 +51,13 @@ public:
     QString projectDirectory() const;
 
     void setProjectFiles(const QStringList &projectFiles);
+    void setSysroot(const QString &sysroot);
 
     QString findFile(const QUrl &fileUrl, bool *success = 0) const;
 
 private:
     QString m_projectDir;
+    QString m_sysroot;
     QStringList m_projectFiles;
     mutable QHash<QString,QString> m_cache;
 };
diff --git a/src/plugins/debugger/attachtoqmlportdialog.ui b/src/plugins/debugger/attachtoqmlportdialog.ui
index 45feeb73ef0..fdcc849cf32 100644
--- a/src/plugins/debugger/attachtoqmlportdialog.ui
+++ b/src/plugins/debugger/attachtoqmlportdialog.ui
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>185</width>
-    <height>115</height>
+    <width>212</width>
+    <height>136</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -16,9 +16,6 @@
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <layout class="QFormLayout" name="formLayout">
-     <property name="fieldGrowthPolicy">
-      <enum>QFormLayout::AllNonFixedFieldsGrow</enum>
-     </property>
      <item row="0" column="0">
       <widget class="QLabel" name="hostLabel">
        <property name="text">
@@ -56,6 +53,19 @@
        </property>
       </widget>
      </item>
+     <item row="2" column="0">
+      <widget class="QLabel" name="sysrootLabel">
+       <property name="text">
+        <string>Sys&amp;root:</string>
+       </property>
+       <property name="buddy">
+        <cstring>sysRootChooser</cstring>
+       </property>
+      </widget>
+     </item>
+     <item row="2" column="1">
+      <widget class="Utils::PathChooser" name="sysRootChooser" native="true"/>
+     </item>
     </layout>
    </item>
    <item>
@@ -70,6 +80,14 @@
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>Utils::PathChooser</class>
+   <extends>QWidget</extends>
+   <header location="global">utils/pathchooser.h</header>
+   <container>1</container>
+  </customwidget>
+ </customwidgets>
  <resources/>
  <connections/>
 </ui>
diff --git a/src/plugins/debugger/debuggerdialogs.cpp b/src/plugins/debugger/debuggerdialogs.cpp
index e13502c351f..4d69e61631a 100644
--- a/src/plugins/debugger/debuggerdialogs.cpp
+++ b/src/plugins/debugger/debuggerdialogs.cpp
@@ -864,6 +864,16 @@ int AttachToQmlPortDialog::port() const
     return m_ui->portSpinBox->value();
 }
 
+QString AttachToQmlPortDialog::sysroot() const
+{
+    return m_ui->sysRootChooser->path();
+}
+
+void AttachToQmlPortDialog::setSysroot(const QString &sysroot)
+{
+    m_ui->sysRootChooser->setPath(sysroot);
+}
+
 // --------- StartRemoteCdbDialog
 static inline QString cdbRemoteHelp()
 {
diff --git a/src/plugins/debugger/debuggerdialogs.h b/src/plugins/debugger/debuggerdialogs.h
index 1f1bf735a28..639759bd41d 100644
--- a/src/plugins/debugger/debuggerdialogs.h
+++ b/src/plugins/debugger/debuggerdialogs.h
@@ -234,6 +234,9 @@ public:
     int port() const;
     void setPort(const int port);
 
+    QString sysroot() const;
+    void setSysroot(const QString &sysroot);
+
 private:
     Ui::AttachToQmlPortDialog *m_ui;
 };
diff --git a/src/plugins/debugger/debuggerplugin.cpp b/src/plugins/debugger/debuggerplugin.cpp
index bb06bb951b3..7066e906423 100644
--- a/src/plugins/debugger/debuggerplugin.cpp
+++ b/src/plugins/debugger/debuggerplugin.cpp
@@ -1673,14 +1673,20 @@ void DebuggerPluginPrivate::attachToQmlPort()
         dlg.setPort(sp.qmlServerPort);
     }
 
+    const QVariant sysrootPath = configValue(_("LastSysroot"));
+    if (sysrootPath.isValid())
+        dlg.setSysroot(sysrootPath.toString());
+
     if (dlg.exec() != QDialog::Accepted)
         return;
 
     setConfigValue(_("LastQmlServerAddress"), dlg.host());
     setConfigValue(_("LastQmlServerPort"), dlg.port());
+    setConfigValue(_("LastSysroot"), dlg.sysroot());
 
     sp.qmlServerAddress = dlg.host();
     sp.qmlServerPort = dlg.port();
+    sp.sysroot = dlg.sysroot();
 
     sp.startMode = AttachToQmlPort;
     if (RunControl *rc = createDebugger(sp))
diff --git a/src/plugins/debugger/qml/qmlengine.cpp b/src/plugins/debugger/qml/qmlengine.cpp
index c64f77d6adf..a3d32ae3bbd 100644
--- a/src/plugins/debugger/qml/qmlengine.cpp
+++ b/src/plugins/debugger/qml/qmlengine.cpp
@@ -755,12 +755,10 @@ unsigned QmlEngine::debuggerCapabilities() const
 
 QString QmlEngine::toFileInProject(const QUrl &fileUrl)
 {
-    if (startParameters().startMode != AttachToQmlPort) {
-        if (d->fileFinder.projectDirectory().isEmpty()) {
-            d->fileFinder.setProjectDirectory(startParameters().projectSourceDirectory);
-            d->fileFinder.setProjectFiles(startParameters().projectSourceFiles);
-        }
-    }
+    // make sure file finder is properly initialized
+    d->fileFinder.setProjectDirectory(startParameters().projectSourceDirectory);
+    d->fileFinder.setProjectFiles(startParameters().projectSourceFiles);
+    d->fileFinder.setSysroot(startParameters().sysroot);
 
     return d->fileFinder.findFile(fileUrl);
 }
-- 
GitLab