diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index fbf2fd5bf0b..e771f185160 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -1047,10 +1047,11 @@ void EditorManager::addEditor(IEditor *editor, bool isDuplicate) m_d->m_editorModel->addEditor(editor, isDuplicate); if (!isDuplicate) { - m_d->m_core->fileManager()->addFile(editor->file()); - if (!editor->isTemporary()) { + const bool isTemporary = editor->isTemporary(); + const bool addWatcher = !isTemporary; + m_d->m_core->fileManager()->addFile(editor->file(), addWatcher); + if (!isTemporary) m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName()); - } } emit editorOpened(editor); } diff --git a/src/plugins/coreplugin/filemanager.cpp b/src/plugins/coreplugin/filemanager.cpp index a81002bcace..3f4271bf558 100644 --- a/src/plugins/coreplugin/filemanager.cpp +++ b/src/plugins/coreplugin/filemanager.cpp @@ -99,15 +99,17 @@ struct FileStateItem struct FileState { + FileState() : watched(false) {} QMap lastUpdatedState; FileStateItem expected; + bool watched; }; - struct FileManagerPrivate { explicit FileManagerPrivate(QObject *q, QMainWindow *mw); + typedef QMap NameFileStateMap; - QMap m_states; + NameFileStateMap m_states; QStringList m_changedFiles; QStringList m_recentFiles; @@ -183,7 +185,7 @@ FileManager::~FileManager() Returns true if the file specified by \a files have not been yet part of the file list. */ -bool FileManager::addFiles(const QList &files) +bool FileManager::addFiles(const QList &files, bool addWatcher) { bool filesAdded = false; foreach (IFile *file, files) { @@ -196,13 +198,14 @@ bool FileManager::addFiles(const QList &files) connect(file, SIGNAL(destroyed(QObject *)), this, SLOT(fileDestroyed(QObject *))); filesAdded = true; - addFileInfo(file); + addFileInfo(file, addWatcher); } return filesAdded; } -void FileManager::addFileInfo(IFile *file) +void FileManager::addFileInfo(IFile *file, bool addWatcher) { + typedef Internal::FileManagerPrivate::NameFileStateMap NameFileStateMap; // We do want to insert the IFile into d->m_states even if the filename is empty // Such that m_states always contains all IFiles @@ -214,13 +217,18 @@ void FileManager::addFileInfo(IFile *file) item.permissions = fi.permissions(); } - if (!d->m_states.contains(fixedname)) { - d->m_states.insert(fixedname, Internal::FileState()); - if (!fixedname.isEmpty()) + // Ensure entry + NameFileStateMap::iterator it = d->m_states.find(fixedname); + if (it == d->m_states.end()) { + Internal::FileState state; + state.watched = addWatcher; + it = d->m_states.insert(fixedname, state); + if (addWatcher && !fixedname.isEmpty()) { d->m_fileWatcher->addPath(fixedname); + } } - d->m_states[fixedname].lastUpdatedState.insert(file, item); + it.value().lastUpdatedState.insert(file, item); } void FileManager::updateFileInfo(IFile *file) @@ -243,10 +251,10 @@ void FileManager::updateFileInfo(IFile *file) /// with renamed files and deleted files void FileManager::removeFileInfo(IFile *file) { + typedef Internal::FileManagerPrivate::NameFileStateMap NameFileStateMap; QString fileName; - QMap::const_iterator it, end; - end = d->m_states.constEnd(); - for (it = d->m_states.constBegin(); it != end; ++it) { + const NameFileStateMap::const_iterator end = d->m_states.constEnd(); + for (NameFileStateMap::const_iterator it = d->m_states.constBegin(); it != end; ++it) { if (it.value().lastUpdatedState.contains(file)) { fileName = it.key(); break; @@ -261,13 +269,19 @@ void FileManager::removeFileInfo(IFile *file) void FileManager::removeFileInfo(const QString &fileName, IFile *file) { + typedef Internal::FileManagerPrivate::NameFileStateMap NameFileStateMap; const QString &fixedName = fixFileName(fileName); - d->m_states[fixedName].lastUpdatedState.remove(file); - if (d->m_states.value(fixedName).lastUpdatedState.isEmpty()) { - d->m_states.remove(fixedName); - if (!fixedName.isEmpty()) + NameFileStateMap::iterator it = d->m_states.find(fixedName); + QTC_ASSERT(it != d->m_states.end(), return); + + it.value().lastUpdatedState.remove(file); + + if (it.value().lastUpdatedState.isEmpty()) { + if (!fixedName.isEmpty() && it.value().watched) { d->m_fileWatcher->removePath(fixedName); + } + d->m_states.erase(it); } } @@ -278,9 +292,9 @@ void FileManager::removeFileInfo(const QString &fileName, IFile *file) Returns true if the file specified by \a file has not been yet part of the file list. */ -bool FileManager::addFile(IFile *file) +bool FileManager::addFile(IFile *file, bool addWatcher) { - return addFiles(QList() << file); + return addFiles(QList() << file, addWatcher); } void FileManager::fileDestroyed(QObject *obj) @@ -309,16 +323,16 @@ bool FileManager::removeFile(IFile *file) return true; } - - void FileManager::checkForNewFileName() { + typedef Internal::FileManagerPrivate::NameFileStateMap NameFileStateMap; IFile *file = qobject_cast(sender()); QTC_ASSERT(file, return); const QString &fileName = fixFileName(file->fileName()); // check if the IFile is in the map - if (d->m_states[fileName].lastUpdatedState.contains(file)) { + const NameFileStateMap::const_iterator nfit = d->m_states.constFind(fileName); + if (nfit != d->m_states.constEnd() && nfit.value().lastUpdatedState.contains(file)) { // Should checkForNewFileName also call updateFileInfo if the name didn't change? updateFileInfo(file); return; @@ -327,7 +341,8 @@ void FileManager::checkForNewFileName() // Probably the name has changed... // This also updates the state to the on disk state removeFileInfo(file); - addFileInfo(file); + const bool wasWatched = nfit != d->m_states.constEnd() && nfit.value().watched; + addFileInfo(file, wasWatched); } // TODO Rename to nativeFileName diff --git a/src/plugins/coreplugin/filemanager.h b/src/plugins/coreplugin/filemanager.h index 8e9c830164a..55e30409c86 100644 --- a/src/plugins/coreplugin/filemanager.h +++ b/src/plugins/coreplugin/filemanager.h @@ -57,8 +57,8 @@ public: virtual ~FileManager(); // file pool to monitor - bool addFiles(const QList &files); - bool addFile(IFile *file); + bool addFiles(const QList &files, bool addWatcher = true); + bool addFile(IFile *file, bool addWatcher = true); bool removeFile(IFile *file); bool isFileManaged(const QString &fileName) const; QList modifiedFiles() const; @@ -128,7 +128,7 @@ private slots: void syncWithEditor(Core::IContext *context); private: - void addFileInfo(IFile *file); + void addFileInfo(IFile *file, bool addWatcher = true); void removeFileInfo(IFile *file); void removeFileInfo(const QString &fileName, IFile *file);