| 1 | // Copyright (C) 2018 The Qt Company Ltd. | 
|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 | 
|---|
| 3 |  | 
|---|
| 4 | #ifndef QMLPREVIEWFILESYSTEMWATCHER_H | 
|---|
| 5 | #define QMLPREVIEWFILESYSTEMWATCHER_H | 
|---|
| 6 |  | 
|---|
| 7 | #include <QtCore/qfilesystemwatcher.h> | 
|---|
| 8 | #include <QtCore/qobject.h> | 
|---|
| 9 | #include <QtCore/qset.h> | 
|---|
| 10 |  | 
|---|
| 11 | class QmlPreviewFileSystemWatcher : public QObject | 
|---|
| 12 | { | 
|---|
| 13 | Q_OBJECT | 
|---|
| 14 |  | 
|---|
| 15 | public: | 
|---|
| 16 | explicit QmlPreviewFileSystemWatcher(QObject *parent = nullptr); | 
|---|
| 17 |  | 
|---|
| 18 | void addFile(const QString &file); | 
|---|
| 19 | void removeFile(const QString &file); | 
|---|
| 20 | bool watchesFile(const QString &file) const; | 
|---|
| 21 |  | 
|---|
| 22 | void addDirectory(const QString &file); | 
|---|
| 23 | void removeDirectory(const QString &file); | 
|---|
| 24 | bool watchesDirectory(const QString &file) const; | 
|---|
| 25 |  | 
|---|
| 26 | Q_SIGNALS: | 
|---|
| 27 | void fileChanged(const QString &path); | 
|---|
| 28 | void directoryChanged(const QString &path); | 
|---|
| 29 |  | 
|---|
| 30 | private: | 
|---|
| 31 | using WatchEntrySet = QSet<QString>; | 
|---|
| 32 | using WatchEntrySetIterator = WatchEntrySet::iterator; | 
|---|
| 33 |  | 
|---|
| 34 | void onDirectoryChanged(const QString &path); | 
|---|
| 35 |  | 
|---|
| 36 | WatchEntrySet m_files; | 
|---|
| 37 | WatchEntrySet m_directories; | 
|---|
| 38 |  | 
|---|
| 39 | // Directories watched either explicitly or implicitly through files contained in them. | 
|---|
| 40 | QHash<QString, int> m_directoryCount; | 
|---|
| 41 |  | 
|---|
| 42 | QFileSystemWatcher *m_watcher = nullptr; | 
|---|
| 43 | }; | 
|---|
| 44 |  | 
|---|
| 45 | #endif // QMLPREVIEWFILESYSTEMWATCHER_H | 
|---|
| 46 |  | 
|---|