Skip to content

Commit 5f7a680

Browse files
authored
application: improve FileWatcher (#282)
1 parent a25f5d1 commit 5f7a680

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/application/filewatcher.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,19 @@ void FileWatcher::setNameFilters(const QStringList &nameFilters)
103103

104104
m_nameFilters = nameFilters;
105105
emit nameFiltersChanged(m_nameFilters);
106+
107+
updateRegExps();
108+
}
109+
110+
void FileWatcher::updateRegExps()
111+
{
112+
m_regExps.clear();
113+
for (QString &filter: m_nameFilters) {
114+
m_regExps.append(QRegExp(filter, Qt::CaseInsensitive, QRegExp::WildcardUnix));
115+
}
106116
}
107117

108-
void FileWatcher::updateWatchedFile()
118+
bool FileWatcher::updateWatchedFile()
109119
{
110120
const auto &files = m_fileSystemWatcher.files();
111121
if (files.length() > 0) {
@@ -117,29 +127,43 @@ void FileWatcher::updateWatchedFile()
117127
}
118128

119129
if (!m_fileUrl.isValid() || !m_enabled) {
120-
return;
130+
return false;
121131
}
122132

123133
if (!m_fileUrl.isLocalFile()) {
124134
qCWarning(filewatcherCategory) << "Can only watch local files";
125-
return;
135+
return false;
126136
}
127137
const auto &localFile = m_fileUrl.toLocalFile();
128138
if (localFile.isEmpty()) {
129-
return;
139+
return false;
130140
}
131141

132142
if (m_recursive && QDir(localFile).exists()) {
143+
QSet<QString> newPaths;
133144
m_fileSystemWatcher.addPath(localFile);
145+
newPaths.insert(localFile);
146+
134147
QDirIterator it(localFile, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
135148
while (it.hasNext()) {
136149
const auto &file = it.next();
150+
const QString extension = it.fileInfo().completeSuffix();
151+
bool filtered = false;
152+
for (QRegExp &regExp: m_regExps) {
153+
if (regExp.exactMatch(extension)) {
154+
filtered = true;
155+
break;
156+
}
157+
}
137158
if ((it.fileName() == QLatin1String("..")) || (it.fileName() == QLatin1String("."))
138-
|| m_nameFilters.contains(it.fileInfo().completeSuffix())) {
159+
|| filtered) {
139160
continue;
140161
}
141162
m_fileSystemWatcher.addPath(file);
163+
newPaths.insert(it.filePath());
142164
}
165+
166+
return newPaths != QSet<QString>::fromList(files).unite(QSet<QString>::fromList(directories));
143167
}
144168
else if (QFile::exists(localFile)) {
145169
m_fileSystemWatcher.addPath(localFile);
@@ -149,6 +173,7 @@ void FileWatcher::updateWatchedFile()
149173
qCWarning(filewatcherCategory) << "File to watch does not exist" << localFile;
150174
#endif
151175
}
176+
return false;
152177
}
153178

154179
void FileWatcher::onWatchedFileChanged()
@@ -160,8 +185,9 @@ void FileWatcher::onWatchedFileChanged()
160185

161186
void FileWatcher::onWatchedDirectoryChanged(const QString &)
162187
{
163-
updateWatchedFile();
164-
onWatchedFileChanged(); // propagate event
188+
if (updateWatchedFile()) {
189+
onWatchedFileChanged(); // propagate event
190+
}
165191
}
166192

167193
} // namespace qtquickvcp

src/application/filewatcher.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ public slots:
4343
bool m_recursive;
4444
QFileSystemWatcher m_fileSystemWatcher;
4545
QStringList m_nameFilters;
46+
QList<QRegExp> m_regExps;
47+
48+
void updateRegExps();
4649

4750
private slots:
48-
void updateWatchedFile();
51+
bool updateWatchedFile();
4952
void onWatchedFileChanged();
5053
void onWatchedDirectoryChanged(const QString &);
5154

tests/applicationtests/test_filewatcher.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ TEST_CASE("FileWatcher Tests", "[application][!mayfail]")
117117

118118
AND_WHEN("a file in with extension from the nameFilters is created") {
119119
QStringList nameFilters;
120-
nameFilters.append("qmlc");
120+
nameFilters.append("*.qmlc");
121121
watcher.setNameFilters(nameFilters);
122122
const auto &filePath = tempDir.filePath("testfile.qmlc");
123123
writeTestFile(filePath);

0 commit comments

Comments
 (0)