Commit 48d19596 authored by Kai Koehne's avatar Kai Koehne
Browse files

QmlProjectFiles: Dramatically reduce number of watched fs entities

Only watch directories, and not single files. Also, share one
FileSystemWatcher between projects & FileFilter items.

This fixes the issue that the process runs out of file handles e.g.
when loading a .qmlproject file containing all of qt examples & demos.

Basic implementation of FileSystemWatcher was copied from
ProjectExplorer::FileWatcher - however the FileWatcher is only
for files (and not directories), and also monitors the last
change date etc.
parent c01122fa
......@@ -8,8 +8,7 @@ FileFilterBaseItem::FileFilterBaseItem(QObject *parent) :
QmlProjectContentItem(parent),
m_recurse(RecurseDefault)
{
connect(&m_fsWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(updateFileList()));
connect(&m_fsWatcher, SIGNAL(fileChanged(QString)), this, SLOT(updateFileList()));
connect(&m_dirWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(updateFileList()));
}
QString FileFilterBaseItem::directory() const
......@@ -127,9 +126,8 @@ bool FileFilterBaseItem::matchesFile(const QString &filePath) const
if (!regexMatches)
return false;
const QStringList watchedDirectories = m_fsWatcher.directories();
const QDir fileDir = QFileInfo(filePath).absoluteDir();
foreach (const QString &watchedDirectory, watchedDirectories) {
foreach (const QString &watchedDirectory, m_dirWatcher.directories()) {
if (QDir(watchedDirectory) == fileDir)
return true;
}
......@@ -171,28 +169,19 @@ void FileFilterBaseItem::updateFileList()
newFiles += filesInSubTree(QDir(m_defaultDir), QDir(projectDir), &dirsToBeWatched);
if (newFiles != m_files) {
// update watched files
const QSet<QString> unwatchFiles = QSet<QString>(m_files - newFiles);
const QSet<QString> watchFiles = QSet<QString>(newFiles - m_files);
if (!unwatchFiles.isEmpty())
m_fsWatcher.removePaths(unwatchFiles.toList());
if (!watchFiles.isEmpty())
m_fsWatcher.addPaths(QSet<QString>(newFiles - m_files).toList());
m_files = newFiles;
emit filesChanged();
}
// update watched directories
const QSet<QString> watchedDirectories = m_fsWatcher.directories().toSet();
const QSet<QString> unwatchDirs = watchedDirectories - dirsToBeWatched;
const QSet<QString> watchDirs = dirsToBeWatched - watchedDirectories;
const QSet<QString> oldDirs = m_dirWatcher.directories().toSet();
const QSet<QString> unwatchDirs = oldDirs - dirsToBeWatched;
const QSet<QString> watchDirs = dirsToBeWatched - oldDirs;
if (!unwatchDirs.isEmpty())
m_fsWatcher.removePaths(unwatchDirs.toList());
m_dirWatcher.removeDirectories(unwatchDirs.toList());
if (!watchDirs.isEmpty())
m_fsWatcher.addPaths(watchDirs.toList());
m_dirWatcher.addDirectories(watchDirs.toList());
}
QSet<QString> FileFilterBaseItem::filesInSubTree(const QDir &rootDir, const QDir &dir, QSet<QString> *parsedDirs)
......
......@@ -8,6 +8,7 @@
#include <QFileSystemWatcher>
#include "qmlprojectitem.h"
#include "filesystemwatcher.h"
namespace QmlProjectManager {
......@@ -72,9 +73,9 @@ private:
QStringList m_explicitFiles;
QFileSystemWatcher m_fsWatcher;
QSet<QString> m_files;
FileSystemWatcher m_dirWatcher;
friend class ProjectItem;
};
......
HEADERS += $$PWD/qmlprojectitem.h \
$$PWD/filefilteritems.h \
$$PWD/qmlprojectfileformat.h
$$PWD/qmlprojectfileformat.h \
$$PWD/filesystemwatcher.h
SOURCES += $$PWD/qmlprojectitem.cpp \
$$PWD/filefilteritems.cpp \
$$PWD/qmlprojectfileformat.cpp
$$PWD/qmlprojectfileformat.cpp \
$$PWD/filesystemwatcher.cpp
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "filesystemwatcher.h"
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QTimer>
enum { debug = false };
namespace QmlProjectManager {
int FileSystemWatcher::m_objectCount = 0;
QHash<QString,int> FileSystemWatcher::m_fileCount;
QHash<QString,int> FileSystemWatcher::m_directoryCount;
QFileSystemWatcher *FileSystemWatcher::m_watcher = 0;
FileSystemWatcher::FileSystemWatcher(QObject *parent) :
QObject(parent)
{
if (!m_watcher)
m_watcher = new QFileSystemWatcher();
++m_objectCount;
connect(m_watcher, SIGNAL(fileChanged(QString)),
this, SLOT(slotFileChanged(QString)));
connect(m_watcher, SIGNAL(directoryChanged(QString)),
this, SLOT(slotDirectoryChanged(QString)));
}
FileSystemWatcher::~FileSystemWatcher()
{
removeFiles(files());
removeDirectories(directories());
if (--m_objectCount == 0) {
delete m_watcher;
m_watcher = 0;
}
}
void FileSystemWatcher::addFile(const QString &file)
{
addFiles(QStringList(file));
}
void FileSystemWatcher::addFiles(const QStringList &files)
{
QStringList toAdd;
if (debug)
qDebug() << Q_FUNC_INFO << files.count();
foreach (const QString &file, files) {
if (m_files.contains(file)) {
qWarning() << "FileSystemWatcher: File" << file << "is already being watched";
continue;
}
m_files.append(file);
const int count = ++m_fileCount[file];
Q_ASSERT(count > 0);
if (count == 1)
toAdd << file;
}
if (!toAdd.isEmpty())
m_watcher->addPaths(toAdd);
}
void FileSystemWatcher::removeFile(const QString &file)
{
removeFiles(QStringList(file));
}
void FileSystemWatcher::removeFiles(const QStringList &files)
{
QStringList toRemove;
if (debug)
qDebug() << Q_FUNC_INFO << files.count();
foreach (const QString &file, files) {
if (!m_files.contains(file)) {
qWarning() << "FileSystemWatcher: File" << file << "is not watched";
continue;
}
m_files.removeOne(file);
const int count = --m_fileCount[file];
Q_ASSERT(count >= 0);
if (!count) {
toRemove << file;
}
}
if (!toRemove.isEmpty())
m_watcher->removePaths(toRemove);
}
QStringList FileSystemWatcher::files() const
{
return m_files;
}
void FileSystemWatcher::addDirectory(const QString &directory)
{
addDirectories(QStringList(directory));
}
void FileSystemWatcher::addDirectories(const QStringList &directories)
{
QStringList toAdd;
if (debug)
qDebug() << Q_FUNC_INFO << directories.count();
foreach (const QString &directory, directories) {
if (m_directories.contains(directory)) {
qWarning() << "Directory" << directory << "is already being watched";
continue;
}
m_directories.append(directory);
const int count = ++m_directoryCount[directory];
Q_ASSERT(count > 0);
if (count == 1)
toAdd << directory;
}
if (!toAdd.isEmpty())
m_watcher->addPaths(toAdd);
}
void FileSystemWatcher::removeDirectory(const QString &directory)
{
removeDirectories(QStringList(directory));
}
void FileSystemWatcher::removeDirectories(const QStringList &directories)
{
QStringList toRemove;
if (debug)
qDebug() << Q_FUNC_INFO << directories.count();
foreach (const QString &directory, directories) {
if (!m_directories.contains(directory)) {
qWarning() << "FileSystemWatcher: Directory" << directory << "is not watched";
continue;
}
m_directories.removeOne(directory);
const int count = --m_directoryCount[directory];
Q_ASSERT(count >= 0);
if (!count) {
toRemove << directory;
}
}
if (!toRemove.isEmpty())
m_watcher->removePaths(toRemove);
}
QStringList FileSystemWatcher::directories() const
{
return m_directories;
}
void FileSystemWatcher::slotFileChanged(const QString &path)
{
if (m_files.contains(path))
emit fileChanged(path);
}
void FileSystemWatcher::slotDirectoryChanged(const QString &path)
{
if (m_directories.contains(path))
emit directoryChanged(path);
}
} // namespace QmlProjectManager
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef FSWATCHER_H
#define FSWATCHER_H
#include <QtCore/QDateTime>
#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QStringList>
#include <QtCore/QMap>
class QTimer;
class QFileSystemWatcher;
namespace QmlProjectManager {
class FileSystemWatcher : public QObject
{
Q_DISABLE_COPY(FileSystemWatcher)
Q_OBJECT
public:
explicit FileSystemWatcher(QObject *parent = 0);
virtual ~FileSystemWatcher();
void addFile(const QString &file);
void addFiles(const QStringList &files);
void removeFile(const QString &file);
void removeFiles(const QStringList &files);
QStringList files() const;
void addDirectory(const QString &file);
void addDirectories(const QStringList &files);
void removeDirectory(const QString &file);
void removeDirectories(const QStringList &files);
QStringList directories() const;
private slots:
void slotFileChanged(const QString &path);
void slotDirectoryChanged(const QString &path);
signals:
void fileChanged(const QString &path);
void directoryChanged(const QString &path);
private:
QStringList m_files;
QStringList m_directories;
static int m_objectCount;
static QHash<QString, int> m_fileCount;
static QHash<QString, int> m_directoryCount;
static QFileSystemWatcher *m_watcher;
};
} // namespace QmlProjectManager
#endif // FSWATCHER_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment