Utils: Introduce FileSystemWatcher.

Remove duplicated classes ProjectExplorer::FileWatcher
and QmlProjectManager::FileSystemWatcher, create
Utils::FileSystemWatcher from them, merging the functionality.

Also use in HelpManager/Maemo, reducing the number
of QFileSystemWatcher instances (and thus, shutdown time).
This commit is contained in:
Friedemann Kleint
2011-04-15 15:55:11 +02:00
parent 72ae03ba80
commit f5cbf87965
19 changed files with 532 additions and 508 deletions

View File

@@ -32,6 +32,9 @@
#include "filefilteritems.h"
#include <utils/filesystemwatcher.h>
#include <utils/qtcassert.h>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtGui/QImageReader>
@@ -40,14 +43,30 @@ namespace QmlProjectManager {
FileFilterBaseItem::FileFilterBaseItem(QObject *parent) :
QmlProjectContentItem(parent),
m_recurse(RecurseDefault)
m_recurse(RecurseDefault),
m_dirWatcher(0)
{
m_updateFileListTimer.setSingleShot(true);
m_updateFileListTimer.setInterval(50);
connect(&m_dirWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(updateFileList()));
connect(&m_updateFileListTimer, SIGNAL(timeout()), this, SLOT(updateFileListNow()));
}
Utils::FileSystemWatcher *FileFilterBaseItem::dirWatcher()
{
if (!m_dirWatcher) {
m_dirWatcher = new Utils::FileSystemWatcher(1, this); // Separate id, might exceed OS limits.
m_dirWatcher->setObjectName(QLatin1String("FileFilterBaseItemWatcher"));
connect(m_dirWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(updateFileList()));
}
return m_dirWatcher;
}
QStringList FileFilterBaseItem::watchedDirectories() const
{
return m_dirWatcher ? m_dirWatcher->directories() : QStringList();
}
QString FileFilterBaseItem::directory() const
{
return m_rootDir;
@@ -170,7 +189,7 @@ bool FileFilterBaseItem::matchesFile(const QString &filePath) const
return false;
const QDir fileDir = QFileInfo(filePath).absoluteDir();
foreach (const QString &watchedDirectory, m_dirWatcher.directories()) {
foreach (const QString &watchedDirectory, watchedDirectories()) {
if (QDir(watchedDirectory) == fileDir)
return true;
}
@@ -233,14 +252,16 @@ void FileFilterBaseItem::updateFileListNow()
}
// update watched directories
const QSet<QString> oldDirs = m_dirWatcher.directories().toSet();
const QSet<QString> oldDirs = watchedDirectories().toSet();
const QSet<QString> unwatchDirs = oldDirs - dirsToBeWatched;
const QSet<QString> watchDirs = dirsToBeWatched - oldDirs;
if (!unwatchDirs.isEmpty())
m_dirWatcher.removeDirectories(unwatchDirs.toList());
if (!unwatchDirs.isEmpty()) {
QTC_ASSERT(m_dirWatcher, return ; )
m_dirWatcher->removeDirectories(unwatchDirs.toList());
}
if (!watchDirs.isEmpty())
m_dirWatcher.addDirectories(watchDirs.toList());
dirWatcher()->addDirectories(watchDirs.toList(), Utils::FileSystemWatcher::WatchAllChanges);
}
bool FileFilterBaseItem::fileMatches(const QString &fileName) const

View File

@@ -34,7 +34,6 @@
#define FILEFILTERITEMS_H
#include "qmlprojectitem.h"
#include "filesystemwatcher.h"
#include <QtCore/QObject>
#include <QtCore/QSet>
@@ -44,6 +43,10 @@
QT_FORWARD_DECLARE_CLASS(QDir)
namespace Utils {
class FileSystemWatcher;
}
namespace QmlProjectManager {
class FileFilterBaseItem : public QmlProjectContentItem {
@@ -91,6 +94,8 @@ private:
bool fileMatches(const QString &fileName) const;
QSet<QString> filesInSubTree(const QDir &rootDir, const QDir &dir, QSet<QString> *parsedDirs = 0);
Utils::FileSystemWatcher *dirWatcher();
QStringList watchedDirectories() const;
QString m_rootDir;
QString m_defaultDir;
@@ -111,7 +116,7 @@ private:
QStringList m_explicitFiles;
QSet<QString> m_files;
FileSystemWatcher m_dirWatcher;
Utils::FileSystemWatcher *m_dirWatcher;
QTimer m_updateFileListTimer;

View File

@@ -1,8 +1,6 @@
HEADERS += $$PWD/qmlprojectitem.h \
$$PWD/filefilteritems.h \
$$PWD/qmlprojectfileformat.h \
$$PWD/filesystemwatcher.h
$$PWD/qmlprojectfileformat.h
SOURCES += $$PWD/qmlprojectitem.cpp \
$$PWD/filefilteritems.cpp \
$$PWD/qmlprojectfileformat.cpp \
$$PWD/filesystemwatcher.cpp
$$PWD/qmlprojectfileformat.cpp

View File

@@ -1,250 +0,0 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#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));
}
#ifdef Q_OS_MAC
// Returns upper limit of file handles that can be opened by this process at once. Exceeding it will probably result in crashes!
static rlim_t getFileLimit()
{
struct rlimit rl;
getrlimit(RLIMIT_NOFILE, &rl);
return rl.rlim_cur;
}
#endif
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;
}
#ifdef Q_OS_MAC
static rlim_t maxFileOpen = getFileLimit();
// We're potentially watching a _lot_ of directories. This might crash qtcreator when we hit the upper limit.
// Heuristic is therefore: Don't use more than half of the file handles available in this watcher
if ((rlim_t)m_directories.size() + (rlim_t)m_files.size() > maxFileOpen / 2) {
qWarning() << "File" << file << "is not watched: Too many file handles are already open (max is" << maxFileOpen;
break;
}
#endif
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;
}
#ifdef Q_OS_MAC
static rlim_t maxFileOpen = getFileLimit();
// We're potentially watching a _lot_ of directories. This might crash qtcreator when we hit the upper limit.
// Heuristic is therefore: Don't use more than half of the file handles available in this watcher
if ((rlim_t)m_directories.size() + (rlim_t)m_files.size() > maxFileOpen / 2) {
qWarning() << "Directory" << directory << "is not watched: Too many file handles are already open (max is" << maxFileOpen;
break;
}
#endif
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

View File

@@ -1,93 +0,0 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#ifndef FSWATCHER_H
#define FSWATCHER_H
#include <QtCore/QDateTime>
#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QStringList>
#include <QtCore/QMap>
QT_BEGIN_NAMESPACE
class QTimer;
class QFileSystemWatcher;
QT_END_NAMESPACE
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

View File

@@ -41,11 +41,12 @@
#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/filewatcher.h>
#include <qt4projectmanager/qmldumptool.h>
#include <qt4projectmanager/qtversionmanager.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <utils/filesystemwatcher.h>
#include <QtCore/QTextStream>
#include <QtDeclarative/QDeclarativeComponent>
#include <QtCore/QtDebug>
@@ -56,8 +57,9 @@ QmlProject::QmlProject(Internal::Manager *manager, const QString &fileName)
: m_manager(manager),
m_fileName(fileName),
m_modelManager(ExtensionSystem::PluginManager::instance()->getObject<QmlJS::ModelManagerInterface>()),
m_fileWatcher(new ProjectExplorer::FileWatcher(this))
m_fileWatcher(new Utils::FileSystemWatcher(this))
{
m_fileWatcher->setObjectName(QLatin1String("QmlProjectWatcher"));
setProjectContext(Core::Context(QmlProjectManager::Constants::PROJECTCONTEXT));
setProjectLanguage(Core::Context(QmlProjectManager::Constants::LANG_QML));
@@ -67,7 +69,7 @@ QmlProject::QmlProject(Internal::Manager *manager, const QString &fileName)
m_file = new Internal::QmlProjectFile(this, fileName);
m_rootNode = new Internal::QmlProjectNode(this, m_file);
m_fileWatcher->addFile(fileName),
m_fileWatcher->addFile(fileName, Utils::FileSystemWatcher::WatchModifiedDate);
connect(m_fileWatcher, SIGNAL(fileChanged(QString)),
this, SLOT(refreshProjectFile()));

View File

@@ -45,8 +45,8 @@ namespace QmlJS {
class ModelManagerInterface;
}
namespace ProjectExplorer {
class FileWatcher;
namespace Utils {
class FileSystemWatcher;
}
namespace QmlProjectManager {
@@ -125,7 +125,7 @@ private:
// qml based, new format
QDeclarativeEngine m_engine;
QWeakPointer<QmlProjectItem> m_projectItem;
ProjectExplorer::FileWatcher *m_fileWatcher;
Utils::FileSystemWatcher *m_fileWatcher;
Internal::QmlProjectNode *m_rootNode;
};