From 1ab318b391e32510911a7482294828c1703e2c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Harald=20J=C3=B8rgensen?= <58829763+adamjoer@users.noreply.github.com> Date: Tue, 17 Oct 2023 16:29:18 +0200 Subject: [PATCH] LibGUI: Add ability to watch file Nodes when traversing them This makes it possible to also capture changes to files, not just directories. We only need to watch for metadata changes on files because child created/deleted is not possible with nodes other than directories, and deletion will be captured by the file's parent --- Userland/Libraries/LibGUI/FileSystemModel.cpp | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp index 883883d296..195c587a39 100644 --- a/Userland/Libraries/LibGUI/FileSystemModel.cpp +++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp @@ -82,7 +82,7 @@ bool FileSystemModel::Node::fetch_data(DeprecatedString const& full_path, bool i void FileSystemModel::Node::traverse_if_needed() { - if (!is_directory() || m_has_traversed) + if (m_has_traversed) return; m_has_traversed = true; @@ -96,9 +96,24 @@ void FileSystemModel::Node::traverse_if_needed() return; } + auto full_path = this->full_path(); + + if (!is_directory()) { + if (m_model.m_mode != DirectoriesOnly && !m_model.m_file_watcher->is_watching(full_path)) { + // We are not already watching this file, watch it + auto result = m_model.m_file_watcher->add_watch(full_path, Core::FileWatcherEvent::Type::MetadataModified); + + if (result.is_error()) { + dbgln("Couldn't watch '{}': {}", full_path, result.error()); + } else if (!result.value()) { + dbgln("Couldn't watch '{}', probably already watching", full_path); + } + } + return; + } + total_size = 0; - auto full_path = this->full_path(); Core::DirIterator di(full_path, m_model.should_show_dotfiles() ? Core::DirIterator::SkipParentAndBaseDir : Core::DirIterator::SkipDots); if (di.has_error()) { auto error = di.error(); @@ -144,7 +159,7 @@ void FileSystemModel::Node::traverse_if_needed() m_children.extend(move(file_children)); if (!m_model.m_file_watcher->is_watching(full_path)) { - // We are not already watching this file, watch it + // We are not already watching this directory, watch it auto result = m_model.m_file_watcher->add_watch(full_path, Core::FileWatcherEvent::Type::MetadataModified | Core::FileWatcherEvent::Type::ChildCreated @@ -153,7 +168,7 @@ void FileSystemModel::Node::traverse_if_needed() if (result.is_error()) { dbgln("Couldn't watch '{}': {}", full_path, result.error()); - } else if (result.value() == false) { + } else if (!result.value()) { dbgln("Couldn't watch '{}', probably already watching", full_path); } }