From e98b38f46eaaeebf2f8eb7436eb4eee1eb17309c Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 13 Oct 2026 13:57:55 +0200 Subject: [PATCH] filechooser: use fast-content-type, read files only for MIME filters --- gtk/gtkfilechooserdefault.c | 2 +- gtk/gtkfilesystemmodel.c | 125 +++++++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index 2ba1f9dff..e295980a1 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -196,7 +196,7 @@ typedef enum { #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \ "standard::is-hidden,standard::is-backup,standard::size," \ - "standard::content-type,time::modified" + "standard::fast-content-type,time::modified" enum { /* the first 3 must be these due to settings caching sort column */ MODEL_COL_NAME, diff --git a/gtk/gtkfilesystemmodel.c b/gtk/gtkfilesystemmodel.c index 16bc2b81e..f7bf1bb13 100644 --- a/gtk/gtkfilesystemmodel.c +++ b/gtk/gtkfilesystemmodel.c @@ -116,6 +116,11 @@ #define MODEL_COMMIT_INTERVAL_US (250 * 1000) #define MODEL_MAX_COMMIT_INTERVAL_US (1000 * 1000) +/* Reading file contents to find out its type is slow on a slow disk or + * share; only a few such reads run at a time. */ +#define MODEL_MAX_SNIFFS 2 +#define MODEL_SNIFFED_ATTRIBUTE "gtk-fsmodel::sniffed" + #define VALUE_PAGE_SHIFT 6 #define VALUE_PAGE_SIZE (1u << VALUE_PAGE_SHIFT) #define VALUE_PAGE_MASK (VALUE_PAGE_SIZE - 1u) @@ -174,6 +179,12 @@ struct _GtkFileSystemModel guint monitor_serial; GHashTable * deleted_files; /* deleted while the enumerator may still return them */ + /* Content type detection for files whose name does not tell the type + * (see node_get_content_type()) */ + GQueue sniff_queue; /* GFiles waiting */ + GHashTable * sniff_pending; /* GFiles waiting or running */ + guint sniff_running; + /* Loading */ GQueue pending_infos; /* GFileInfos from the enumerator, not added yet */ GError * load_error; @@ -655,6 +666,115 @@ node_retire_slot (GtkFileSystemModel *model, model->free_id_head = id; } +static void gtk_file_system_model_sniff_next (GtkFileSystemModel *model); + +static void +gtk_file_system_model_sniff_done (GObject *object, + GAsyncResult *res, + gpointer data) +{ + GtkFileSystemModel *model = data; /* only a valid pointer if not cancelled */ + GFile *file = G_FILE (object); + GError *error = NULL; + GFileInfo *sniffed; + guint id; + + sniffed = g_file_query_info_finish (file, res, &error); + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + { + g_error_free (error); + return; + } + g_clear_error (&error); + + gdk_threads_enter (); + + model->sniff_running--; + + id = node_get_for_file (model, file); + if (id != 0) + { + FileModelNode *node = get_node (model, id); + GFileInfo *info = g_file_info_dup (node->info); + + /* Failures are remembered too, so the file is not read again. */ + g_file_info_set_attribute_boolean (info, MODEL_SNIFFED_ATTRIBUTE, TRUE); + if (sniffed && + g_file_info_has_attribute (sniffed, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE)) + g_file_info_set_content_type (info, g_file_info_get_content_type (sniffed)); + + _gtk_file_system_model_update_file (model, file, info); + g_object_unref (info); + + /* The type decides whether the filter lets the file pass. */ + node->want_refilter = TRUE; + node_queue (model, id); + } + + g_hash_table_remove (model->sniff_pending, file); /* drops the last ref of @file */ + gtk_file_system_model_sniff_next (model); + + gdk_threads_leave (); + + if (sniffed) + g_object_unref (sniffed); +} + +static void +gtk_file_system_model_sniff_next (GtkFileSystemModel *model) +{ + while (model->sniff_running < MODEL_MAX_SNIFFS && + !g_queue_is_empty (&model->sniff_queue)) + { + GFile *file = g_queue_pop_head (&model->sniff_queue); + + model->sniff_running++; + g_file_query_info_async (file, + G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, + G_FILE_QUERY_INFO_NONE, + G_PRIORITY_LOW, + model->cancellable, + gtk_file_system_model_sniff_done, + model); + } +} + +/* Returns the content type of a node for filtering. The file chooser + * lists folders with standard::fast-content-type, which only looks at the + * file name: reading every file is far too slow on a slow disk or share. + * If the name does not tell the type, the content is read in the + * background and the node filtered again when the type is known. */ +static const char * +node_get_content_type (GtkFileSystemModel *model, + FileModelNode *node) +{ + const char *type; + + /* Ask for attributes only if they were queried; GLib warns otherwise. */ + if (g_file_info_has_attribute (node->info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE)) + return g_file_info_get_content_type (node->info); + + type = g_file_info_get_attribute_string (node->info, + G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE); + if (type == NULL || !g_content_type_is_unknown (type) || + _gtk_file_info_consider_as_directory (node->info) || + g_file_info_has_attribute (node->info, MODEL_SNIFFED_ATTRIBUTE)) + return type; + + if (model->sniff_pending == NULL) + model->sniff_pending = g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal, + g_object_unref, NULL); + + if (!g_hash_table_contains (model->sniff_pending, node->file)) + { + g_hash_table_add (model->sniff_pending, g_object_ref (node->file)); + g_queue_push_tail (&model->sniff_queue, node->file); + gtk_file_system_model_sniff_next (model); + } + + return type; +} + static gboolean node_should_be_filtered_out (GtkFileSystemModel *model, guint id) { @@ -680,7 +800,7 @@ node_should_be_filtered_out (GtkFileSystemModel *model, guint id) if (required & GTK_FILE_FILTER_MIME_TYPE) { - const char *s = g_file_info_get_content_type (node->info); + const char *s = node_get_content_type (model, node); if (s) { mime_type = g_content_type_get_mime_type (s); @@ -2064,6 +2184,9 @@ gtk_file_system_model_finalize (GObject *object) g_hash_table_destroy (model->monitor_queries); if (model->deleted_files) g_hash_table_destroy (model->deleted_files); + g_queue_clear (&model->sniff_queue); + if (model->sniff_pending) + g_hash_table_destroy (model->sniff_pending); g_hash_table_destroy (model->file_lookup); if (model->filter) g_object_unref (model->filter);