From 1511cac715c9d7b6df37cf56d605efc924bf59bb Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 22 Jul 2019 20:08:25 +0200 Subject: [PATCH] GDirectoryModel: Automagically update on filesystem changes. Use the new watch_file() mechanism to monitor the currently open directory for changes and refresh the model when notified. This makes FileManager automagically show newly added files. :^) --- Libraries/LibGUI/GDirectoryModel.cpp | 11 +++++++++++ Libraries/LibGUI/GDirectoryModel.h | 3 +++ 2 files changed, 14 insertions(+) diff --git a/Libraries/LibGUI/GDirectoryModel.cpp b/Libraries/LibGUI/GDirectoryModel.cpp index b682f9bf7e..be68c4dd7e 100644 --- a/Libraries/LibGUI/GDirectoryModel.cpp +++ b/Libraries/LibGUI/GDirectoryModel.cpp @@ -308,7 +308,18 @@ void GDirectoryModel::open(const StringView& a_path) if (!dirp) return; closedir(dirp); + if (m_notifier) + close(m_notifier->fd()); m_path = path; + int watch_fd = watch_file(path.characters(), path.length()); + if (watch_fd < 0) { + perror("watch_file"); + ASSERT_NOT_REACHED(); + } + m_notifier = make(watch_fd, CNotifier::Event::Read); + m_notifier->on_ready_to_read = [this] { + update(); + }; update(); set_selected_index(index(0, 0)); } diff --git a/Libraries/LibGUI/GDirectoryModel.h b/Libraries/LibGUI/GDirectoryModel.h index 05ad88cd28..8787a56d17 100644 --- a/Libraries/LibGUI/GDirectoryModel.h +++ b/Libraries/LibGUI/GDirectoryModel.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -77,4 +78,6 @@ private: HashMap m_user_names; HashMap m_group_names; + + OwnPtr m_notifier; };