Skip to content

Commit a1ef679

Browse files
committed
fix DirectoryWatcher with multiple directories
1 parent 6d1ac3c commit a1ef679

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

Core/src/DirectoryWatcher.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ void DirectoryWatcher::OnDirectoryChanged(const QString& path)
8686
emit DirectoryChanged(path.toStdString());
8787

8888
const QDir dir(path);
89-
const QFileInfoList watchedList = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
89+
const QFileInfoList files = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
9090

91-
std::unordered_set<QString> missing(m_lastModified.size());
92-
for (const auto& file : m_lastModified | std::views::keys)
93-
missing.insert(file);
91+
auto beforeFiles = m_lastModified | std::views::keys | std::views::filter([&path](const QString& m) -> bool
92+
{
93+
return m.startsWith(path);
94+
}) | std::ranges::to<std::unordered_set<QString>>();
9495

95-
for (const QFileInfo& fileInfo : watchedList)
96+
for (const QFileInfo& fileInfo : files)
9697
{
97-
QString filePath = fileInfo.absoluteFilePath();
98-
QDateTime lastModified = fileInfo.lastModified();
98+
const QString filePath = fileInfo.absoluteFilePath();
99+
const QDateTime lastModified = fileInfo.lastModified();
99100

100-
const bool contains = m_lastModified.contains(filePath);
101-
if (!contains || m_lastModified[filePath] < lastModified)
101+
if (!m_lastModified.contains(filePath) || m_lastModified[filePath] < lastModified)
102102
{
103103
m_lastModified[filePath] = lastModified;
104104
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
@@ -108,13 +108,15 @@ void DirectoryWatcher::OnDirectoryChanged(const QString& path)
108108
#endif
109109
}
110110

111-
if (missing.contains(filePath))
111+
// remove it from the list if it still exists
112+
if (beforeFiles.contains(filePath))
112113
{
113-
missing.erase(filePath);
114+
beforeFiles.erase(filePath);
114115
}
115116
}
116117

117-
for (const QString& file : missing)
118+
// these files were removed
119+
for (const QString& file : beforeFiles)
118120
{
119121
m_lastModified.erase(file);
120122
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)

0 commit comments

Comments
 (0)