forked from qt-creator/qt-creator
DocumentManager: Use FilePath::watch() instead of QFileSystemWatcher
Change-Id: I9fcbbc62366f3191bfca3ecce937ad11109e885c Reviewed-by: Eike Ziller <eike.ziller@qt.io> Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -135,13 +135,55 @@ struct FileState
|
|||||||
FileStateItem expected;
|
FileStateItem expected;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class FileWatchers : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
FilePaths files() const { return watchers.keys(); }
|
||||||
|
|
||||||
|
bool addPath(const FilePath &path)
|
||||||
|
{
|
||||||
|
if (watchers.contains(path))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
auto res = path.watch();
|
||||||
|
QTC_ASSERT_EXPECTED(res, return false;);
|
||||||
|
|
||||||
|
connect(res->get(), &FilePathWatcher::pathChanged, this, [this, path] {
|
||||||
|
emit fileChanged(path);
|
||||||
|
});
|
||||||
|
watchers.insert(path, std::move(*res));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void addPaths(const FilePaths &paths)
|
||||||
|
{
|
||||||
|
for (const FilePath &path : paths)
|
||||||
|
addPath(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool removePath(const FilePath &path)
|
||||||
|
{
|
||||||
|
if (!watchers.contains(path))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
watchers.remove(path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void fileChanged(const FilePath &filePath);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
QMap<FilePath, std::shared_ptr<FilePathWatcher>> watchers;
|
||||||
|
};
|
||||||
|
|
||||||
class DocumentManagerPrivate final : public QObject
|
class DocumentManagerPrivate final : public QObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DocumentManagerPrivate();
|
DocumentManagerPrivate();
|
||||||
|
|
||||||
QFileSystemWatcher *fileWatcher();
|
FileWatchers *fileWatcher();
|
||||||
QFileSystemWatcher *linkWatcher();
|
|
||||||
|
|
||||||
void checkOnNextFocusChange();
|
void checkOnNextFocusChange();
|
||||||
void onApplicationFocusChange();
|
void onApplicationFocusChange();
|
||||||
@@ -159,8 +201,7 @@ public:
|
|||||||
bool m_checkOnFocusChange = false;
|
bool m_checkOnFocusChange = false;
|
||||||
bool m_useProjectsDirectory = kUseProjectsDirectoryDefault;
|
bool m_useProjectsDirectory = kUseProjectsDirectoryDefault;
|
||||||
|
|
||||||
QFileSystemWatcher *m_fileWatcher = nullptr; // Delayed creation.
|
FileWatchers m_fileWatcher; // Delayed creation.
|
||||||
QFileSystemWatcher *m_linkWatcher = nullptr; // Delayed creation (only UNIX/if a link is seen).
|
|
||||||
FilePath m_lastVisitedDirectory = FilePath::fromString(QDir::currentPath());
|
FilePath m_lastVisitedDirectory = FilePath::fromString(QDir::currentPath());
|
||||||
FilePath m_defaultLocationForNewFiles;
|
FilePath m_defaultLocationForNewFiles;
|
||||||
FilePath m_projectsDirectory;
|
FilePath m_projectsDirectory;
|
||||||
@@ -177,29 +218,9 @@ public:
|
|||||||
static DocumentManager *m_instance;
|
static DocumentManager *m_instance;
|
||||||
static DocumentManagerPrivate *d;
|
static DocumentManagerPrivate *d;
|
||||||
|
|
||||||
QFileSystemWatcher *DocumentManagerPrivate::fileWatcher()
|
FileWatchers *DocumentManagerPrivate::fileWatcher()
|
||||||
{
|
{
|
||||||
if (!m_fileWatcher) {
|
return &m_fileWatcher;
|
||||||
m_fileWatcher= new QFileSystemWatcher(m_instance);
|
|
||||||
QObject::connect(m_fileWatcher, &QFileSystemWatcher::fileChanged,
|
|
||||||
m_instance, &DocumentManager::changedFile);
|
|
||||||
}
|
|
||||||
return m_fileWatcher;
|
|
||||||
}
|
|
||||||
|
|
||||||
QFileSystemWatcher *DocumentManagerPrivate::linkWatcher()
|
|
||||||
{
|
|
||||||
if (HostOsInfo::isAnyUnixHost()) {
|
|
||||||
if (!m_linkWatcher) {
|
|
||||||
m_linkWatcher = new QFileSystemWatcher(m_instance);
|
|
||||||
m_linkWatcher->setObjectName(QLatin1String("_qt_autotest_force_engine_poller"));
|
|
||||||
QObject::connect(m_linkWatcher, &QFileSystemWatcher::fileChanged,
|
|
||||||
m_instance, &DocumentManager::changedFile);
|
|
||||||
}
|
|
||||||
return m_linkWatcher;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fileWatcher();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DocumentManagerPrivate::checkOnNextFocusChange()
|
void DocumentManagerPrivate::checkOnNextFocusChange()
|
||||||
@@ -238,6 +259,9 @@ DocumentManager::DocumentManager(QObject *parent)
|
|||||||
d = new DocumentManagerPrivate;
|
d = new DocumentManagerPrivate;
|
||||||
m_instance = this;
|
m_instance = this;
|
||||||
|
|
||||||
|
QObject::connect(
|
||||||
|
&d->m_fileWatcher, &FileWatchers::fileChanged, this, &DocumentManager::changedFile);
|
||||||
|
|
||||||
connect(Utils::GlobalFileChangeBlocker::instance(), &Utils::GlobalFileChangeBlocker::stateChanged,
|
connect(Utils::GlobalFileChangeBlocker::instance(), &Utils::GlobalFileChangeBlocker::stateChanged,
|
||||||
this, [](bool blocked) {
|
this, [](bool blocked) {
|
||||||
d->m_postponeAutoReload = blocked;
|
d->m_postponeAutoReload = blocked;
|
||||||
@@ -302,7 +326,7 @@ static void addFileInfos(const QList<IDocument *> &documents)
|
|||||||
linkPathsToWatch.append(d->m_states.value(filePath).watchedFilePath);
|
linkPathsToWatch.append(d->m_states.value(filePath).watchedFilePath);
|
||||||
pathsToWatch.append(d->m_states.value(resolvedFilePath).watchedFilePath);
|
pathsToWatch.append(d->m_states.value(resolvedFilePath).watchedFilePath);
|
||||||
}
|
}
|
||||||
} else if (!filePath.needsDevice()) {
|
} else {
|
||||||
pathsToWatch.append(d->m_states.value(filePath).watchedFilePath);
|
pathsToWatch.append(d->m_states.value(filePath).watchedFilePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -311,11 +335,11 @@ static void addFileInfos(const QList<IDocument *> &documents)
|
|||||||
// update link targets, even if there are multiple documents registered for it
|
// update link targets, even if there are multiple documents registered for it
|
||||||
if (!pathsToWatch.isEmpty()) {
|
if (!pathsToWatch.isEmpty()) {
|
||||||
qCDebug(log) << "adding full watch for" << pathsToWatch;
|
qCDebug(log) << "adding full watch for" << pathsToWatch;
|
||||||
d->fileWatcher()->addPaths(Utils::transform(pathsToWatch, &FilePath::toString));
|
d->fileWatcher()->addPaths(pathsToWatch);
|
||||||
}
|
}
|
||||||
if (!linkPathsToWatch.isEmpty()) {
|
if (!linkPathsToWatch.isEmpty()) {
|
||||||
qCDebug(log) << "adding link watch for" << linkPathsToWatch;
|
qCDebug(log) << "adding link watch for" << linkPathsToWatch;
|
||||||
d->linkWatcher()->addPaths(Utils::transform(linkPathsToWatch, &FilePath::toString));
|
d->fileWatcher()->addPaths(linkPathsToWatch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,16 +403,9 @@ static void removeFileInfo(IDocument *document)
|
|||||||
if (d->m_states.value(filePath).lastUpdatedState.isEmpty()) {
|
if (d->m_states.value(filePath).lastUpdatedState.isEmpty()) {
|
||||||
const FilePath &watchedFilePath = d->m_states.value(filePath).watchedFilePath;
|
const FilePath &watchedFilePath = d->m_states.value(filePath).watchedFilePath;
|
||||||
if (!watchedFilePath.needsDevice()) {
|
if (!watchedFilePath.needsDevice()) {
|
||||||
const QString &localFilePath = watchedFilePath.path();
|
if (d->m_fileWatcher.files().contains(watchedFilePath)) {
|
||||||
if (d->m_fileWatcher
|
qCDebug(log) << "removing watch for" << watchedFilePath;
|
||||||
&& d->m_fileWatcher->files().contains(localFilePath)) {
|
d->m_fileWatcher.removePath(watchedFilePath);
|
||||||
qCDebug(log) << "removing watch for" << localFilePath;
|
|
||||||
d->m_fileWatcher->removePath(localFilePath);
|
|
||||||
}
|
|
||||||
if (d->m_linkWatcher
|
|
||||||
&& d->m_linkWatcher->files().contains(localFilePath)) {
|
|
||||||
qCDebug(log) << "removing watch for" << localFilePath;
|
|
||||||
d->m_linkWatcher->removePath(localFilePath);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
d->m_states.remove(filePath);
|
d->m_states.remove(filePath);
|
||||||
@@ -1031,9 +1048,8 @@ FilePaths DocumentManager::getOpenFileNames(const QString &filters,
|
|||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DocumentManager::changedFile(const QString &fileName)
|
void DocumentManager::changedFile(const FilePath &filePath)
|
||||||
{
|
{
|
||||||
const FilePath filePath = FilePath::fromString(fileName);
|
|
||||||
const bool wasempty = d->m_changedFiles.isEmpty();
|
const bool wasempty = d->m_changedFiles.isEmpty();
|
||||||
|
|
||||||
if (d->m_states.contains(filePathKey(filePath, KeepLinks)))
|
if (d->m_states.contains(filePathKey(filePath, KeepLinks)))
|
||||||
@@ -1567,3 +1583,5 @@ FileChangeBlocker::~FileChangeBlocker()
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
|
||||||
|
#include "documentmanager.moc"
|
||||||
|
@@ -148,7 +148,7 @@ private:
|
|||||||
void documentDestroyed(QObject *obj);
|
void documentDestroyed(QObject *obj);
|
||||||
void checkForNewFileName(IDocument *document);
|
void checkForNewFileName(IDocument *document);
|
||||||
void checkForReload();
|
void checkForReload();
|
||||||
void changedFile(const QString &file);
|
void changedFile(const Utils::FilePath &file);
|
||||||
void updateSaveAll();
|
void updateSaveAll();
|
||||||
static void registerSaveAllAction();
|
static void registerSaveAllAction();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user