From a9b4a25a68df7f23ba263e045aaa8df145c98f13 Mon Sep 17 00:00:00 2001
From: Christian Kamm <christian.d.kamm@nokia.com>
Date: Fri, 15 Oct 2010 10:06:59 +0200
Subject: [PATCH] QmlJS: Simplify document and library updating.

It is now guaranteed that the ModelManager's snapshot will be updated
before the documentChanged/libraryInfoChanged signals are emitted.
Whether the updated document/library is already in the snapshot or not
was undefined before.

Task-number: QTCREATORBUG-2640
Reviewed-by: Roberto Raggi
---
 src/plugins/qmljseditor/qmljsmodelmanager.cpp | 39 ++++++++-----------
 src/plugins/qmljseditor/qmljsmodelmanager.h   |  7 +---
 2 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/src/plugins/qmljseditor/qmljsmodelmanager.cpp b/src/plugins/qmljseditor/qmljsmodelmanager.cpp
index 379431fe2ba..27cf5c1ced2 100644
--- a/src/plugins/qmljseditor/qmljsmodelmanager.cpp
+++ b/src/plugins/qmljseditor/qmljsmodelmanager.cpp
@@ -69,11 +69,6 @@ ModelManager::ModelManager(QObject *parent):
     qRegisterMetaType<QmlJS::Document::Ptr>("QmlJS::Document::Ptr");
     qRegisterMetaType<QmlJS::LibraryInfo>("QmlJS::LibraryInfo");
 
-    connect(this, SIGNAL(documentUpdated(QmlJS::Document::Ptr)),
-            this, SLOT(onDocumentUpdated(QmlJS::Document::Ptr)));
-    connect(this, SIGNAL(libraryInfoUpdated(QString,QmlJS::LibraryInfo)),
-            this, SLOT(onLibraryInfoUpdated(QString,QmlJS::LibraryInfo)));
-
     loadQmlTypeDescriptions();
 
     m_defaultImportPaths << environmentImportPaths();
@@ -215,24 +210,22 @@ void ModelManager::updateProjectInfo(const ProjectInfo &pinfo)
 void ModelManager::emitDocumentChangedOnDisk(Document::Ptr doc)
 { emit documentChangedOnDisk(doc); }
 
-void ModelManager::emitDocumentUpdated(Document::Ptr doc)
-{ emit documentUpdated(doc); }
-
-void ModelManager::onDocumentUpdated(Document::Ptr doc)
+void ModelManager::updateDocument(Document::Ptr doc)
 {
-    QMutexLocker locker(&m_mutex);
-
-    _snapshot.insert(doc);
+    {
+        QMutexLocker locker(&m_mutex);
+        _snapshot.insert(doc);
+    }
+    emit documentUpdated(doc);
 }
 
-void ModelManager::emitLibraryInfoUpdated(const QString &path, const LibraryInfo &info)
-{ emit libraryInfoUpdated(path, info); }
-
-void ModelManager::onLibraryInfoUpdated(const QString &path, const LibraryInfo &info)
+void ModelManager::updateLibraryInfo(const QString &path, const LibraryInfo &info)
 {
-    QMutexLocker locker(&m_mutex);
-
-    _snapshot.insertLibraryInfo(path, info);
+    {
+        QMutexLocker locker(&m_mutex);
+        _snapshot.insertLibraryInfo(path, info);
+    }
+    emit libraryInfoUpdated(path, info);
 }
 
 static QStringList qmlFilesInDirectory(const QString &path)
@@ -318,7 +311,7 @@ static void findNewLibraryImports(const Document::Ptr &doc, const Snapshot &snap
                 qmldirParser.setSource(qmldirData);
                 qmldirParser.parse();
 
-                modelManager->emitLibraryInfoUpdated(QFileInfo(qmldirFile).absolutePath(),
+                modelManager->updateLibraryInfo(QFileInfo(qmldirFile).absolutePath(),
                                                      LibraryInfo(qmldirParser));
 
                 // scan the qml files in the library
@@ -405,7 +398,7 @@ void ModelManager::parse(QFutureInterface<void> &future,
                 files.append(file);
         }
 
-        modelManager->emitDocumentUpdated(doc);
+        modelManager->updateDocument(doc);
         if (emitDocChangedOnDisk)
             modelManager->emitDocumentChangedOnDisk(doc);
     }
@@ -554,7 +547,7 @@ void ModelManager::qmlPluginTypeDumpDone(int exitCode)
     }
 
     if (!libraryPath.isEmpty())
-        emitLibraryInfoUpdated(libraryPath, libraryInfo);
+        updateLibraryInfo(libraryPath, libraryInfo);
 }
 
 void ModelManager::qmlPluginTypeDumpError(QProcess::ProcessError)
@@ -572,6 +565,6 @@ void ModelManager::qmlPluginTypeDumpError(QProcess::ProcessError)
     if (!libraryPath.isEmpty()) {
         LibraryInfo libraryInfo = _snapshot.libraryInfo(libraryPath);
         libraryInfo.setDumped(true);
-        emitLibraryInfoUpdated(libraryPath, libraryInfo);
+        updateLibraryInfo(libraryPath, libraryInfo);
     }
 }
diff --git a/src/plugins/qmljseditor/qmljsmodelmanager.h b/src/plugins/qmljseditor/qmljsmodelmanager.h
index bc829e60109..bda8bd647a4 100644
--- a/src/plugins/qmljseditor/qmljsmodelmanager.h
+++ b/src/plugins/qmljseditor/qmljsmodelmanager.h
@@ -65,8 +65,8 @@ public:
     virtual ProjectInfo projectInfo(ProjectExplorer::Project *project) const;
     virtual void updateProjectInfo(const ProjectInfo &pinfo);
 
-    void emitDocumentUpdated(QmlJS::Document::Ptr doc);
-    void emitLibraryInfoUpdated(const QString &path, const QmlJS::LibraryInfo &info);
+    void updateDocument(QmlJS::Document::Ptr doc);
+    void updateLibraryInfo(const QString &path, const QmlJS::LibraryInfo &info);
     void emitDocumentChangedOnDisk(QmlJS::Document::Ptr doc);
 
     virtual QStringList importPaths() const;
@@ -78,9 +78,6 @@ Q_SIGNALS:
     void libraryInfoUpdated(const QString &path, const QmlJS::LibraryInfo &info);
 
 private Q_SLOTS:
-    // this should be executed in the GUI thread.
-    void onDocumentUpdated(QmlJS::Document::Ptr doc);
-    void onLibraryInfoUpdated(const QString &path, const QmlJS::LibraryInfo &info);
     void onLoadPluginTypes(const QString &libraryPath, const QString &importPath, const QString &importUri);
     void qmlPluginTypeDumpDone(int exitCode);
     void qmlPluginTypeDumpError(QProcess::ProcessError error);
-- 
GitLab