filechooser: use fast-content-type, read files only for MIME filters
This commit is contained in:
parent
7453c733c7
commit
e98b38f46e
2 changed files with 125 additions and 2 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue