1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef QMLPREVIEWFILESYSTEMWATCHER_H |
30 | #define QMLPREVIEWFILESYSTEMWATCHER_H |
31 | |
32 | #include <QtCore/qfilesystemwatcher.h> |
33 | #include <QtCore/qobject.h> |
34 | #include <QtCore/qset.h> |
35 | |
36 | class QmlPreviewFileSystemWatcher : public QObject |
37 | { |
38 | Q_OBJECT |
39 | |
40 | public: |
41 | explicit QmlPreviewFileSystemWatcher(QObject *parent = nullptr); |
42 | |
43 | void addFile(const QString &file); |
44 | void removeFile(const QString &file); |
45 | bool watchesFile(const QString &file) const; |
46 | |
47 | void addDirectory(const QString &file); |
48 | void removeDirectory(const QString &file); |
49 | bool watchesDirectory(const QString &file) const; |
50 | |
51 | signals: |
52 | void fileChanged(const QString &path); |
53 | void directoryChanged(const QString &path); |
54 | |
55 | private: |
56 | using WatchEntrySet = QSet<QString>; |
57 | using WatchEntrySetIterator = WatchEntrySet::iterator; |
58 | |
59 | void onDirectoryChanged(const QString &path); |
60 | |
61 | WatchEntrySet m_files; |
62 | WatchEntrySet m_directories; |
63 | |
64 | // Directories watched either explicitly or implicitly through files contained in them. |
65 | QHash<QString, int> m_directoryCount; |
66 | |
67 | QFileSystemWatcher *m_watcher = nullptr; |
68 | }; |
69 | |
70 | #endif // QMLPREVIEWFILESYSTEMWATCHER_H |
71 | |