summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/FileSystemModel.cpp
diff options
context:
space:
mode:
authorMarcus Nilsson <marcus.nilsson@genarp.com>2023-01-11 22:50:54 +0100
committerAndrew Kaster <andrewdkaster@gmail.com>2023-02-02 04:05:42 -0700
commitfe5dfe4cd5ef4ea0a73be2f9bd53a16c3c789b86 (patch)
tree549017163ca7c33c607f10bee648604d59410ae3 /Userland/Libraries/LibGUI/FileSystemModel.cpp
parentc3bd841d50167baa1cb627d01f2fb238c7c6d611 (diff)
downloadserenity-fe5dfe4cd5ef4ea0a73be2f9bd53a16c3c789b86.zip
LibGUI: Add allowed file extensions to FileSystemModel
This allows FileSystemModel to take an optional list of allowed file extensions which it will use to filter out all files that don't end with that file extension. The file extensions are set via `set_allowed_file_extensions` which has a coresponding `get_allowed_file_extensions`.
Diffstat (limited to 'Userland/Libraries/LibGUI/FileSystemModel.cpp')
-rw-r--r--Userland/Libraries/LibGUI/FileSystemModel.cpp26
1 files changed, 23 insertions, 3 deletions
diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp
index 91d804d25e..545b4fda7c 100644
--- a/Userland/Libraries/LibGUI/FileSystemModel.cpp
+++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp
@@ -120,10 +120,21 @@ void FileSystemModel::Node::traverse_if_needed()
auto child = maybe_child.release_nonnull();
total_size += child->size;
- if (S_ISDIR(child->mode))
+ if (S_ISDIR(child->mode)) {
directory_children.append(move(child));
- else
- file_children.append(move(child));
+ } else {
+ if (!m_model.m_allowed_file_extensions.has_value()) {
+ file_children.append(move(child));
+ continue;
+ }
+
+ for (auto& extension : *m_model.m_allowed_file_extensions) {
+ if (child_name.ends_with(DeprecatedString::formatted(".{}", extension))) {
+ file_children.append(move(child));
+ break;
+ }
+ }
+ }
}
m_children.extend(move(directory_children));
@@ -750,6 +761,15 @@ void FileSystemModel::set_should_show_dotfiles(bool show)
invalidate();
}
+void FileSystemModel::set_allowed_file_extensions(Optional<Vector<DeprecatedString>> const& allowed_file_extensions)
+{
+ if (m_allowed_file_extensions == allowed_file_extensions)
+ return;
+ m_allowed_file_extensions = allowed_file_extensions;
+
+ invalidate();
+}
+
bool FileSystemModel::is_editable(ModelIndex const& index) const
{
if (!index.is_valid())