diff --git a/gtk/gtkfilesystemmodel.c b/gtk/gtkfilesystemmodel.c index 128904a16..2e958492e 100644 --- a/gtk/gtkfilesystemmodel.c +++ b/gtk/gtkfilesystemmodel.c @@ -122,6 +122,11 @@ struct _GtkFileSystemModel GFileEnumerator * dir_enumerator; GCancellable * cancellable; GList * pending_files; + GList * pending_files_tail; + guint n_pending_files; + guint next_files_in_flight : 1; + guint enumeration_done : 1; + GError * enumeration_error; guint drain_source; guint resort_source; @@ -1644,6 +1649,9 @@ gtk_file_system_model_dispose (GObject *object) g_list_free_full (model->pending_files, g_object_unref); model->pending_files = NULL; + model->pending_files_tail = NULL; + model->n_pending_files = 0; + g_clear_error (&model->enumeration_error); g_clear_object (&model->dir_enumerator); @@ -1748,9 +1756,12 @@ static void gtk_file_system_model_request_next_files (GtkFileSystemModel *model) { if (model->dir_enumerator == NULL || + model->next_files_in_flight || + model->enumeration_done || g_cancellable_is_cancelled (model->cancellable)) return; + model->next_files_in_flight = TRUE; g_file_enumerator_next_files_async (model->dir_enumerator, FILES_PER_QUERY, IO_PRIORITY, @@ -1782,6 +1793,16 @@ gtk_file_system_model_drain (gpointer data) model->pending_files = g_list_delete_link (model->pending_files, link); + if (model->pending_files == NULL) + model->pending_files_tail = NULL; + model->n_pending_files--; + + /* Keep the enumerator busy while this batch is integrated: on a + * high-latency share the next batch then arrives while the main + * thread is still working, instead of only being requested once + * the queue ran dry. */ + if (model->n_pending_files < FILES_PER_QUERY) + gtk_file_system_model_request_next_files (model); name = g_file_info_get_name (info); if (name != NULL) @@ -1803,7 +1824,20 @@ gtk_file_system_model_drain (gpointer data) return TRUE; model->drain_source = 0; - gtk_file_system_model_request_next_files (model); + + /* All batches are in and integrated: finish loading. */ + if (model->enumeration_done && model->finalize_phase == MODEL_FINALIZE_NONE && + model->directory_loading) + { + GError *error = model->enumeration_error; + + model->enumeration_error = NULL; + gtk_file_system_model_begin_finalize_load (model, error); + g_clear_error (&error); + } + else + gtk_file_system_model_request_next_files (model); + return FALSE; } @@ -1821,9 +1855,25 @@ gtk_file_system_model_got_files (GObject *object, if (files != NULL) { + GList *tail = g_list_last (files); + gdk_threads_enter (); - g_assert (model->pending_files == NULL); - model->pending_files = files; + model->next_files_in_flight = FALSE; + + if (model->pending_files_tail) + { + model->pending_files_tail->next = files; + files->prev = model->pending_files_tail; + } + else + model->pending_files = files; + model->pending_files_tail = tail; + model->n_pending_files += g_list_length (files); + + /* Read ahead by one batch at most, so memory stays bounded. */ + if (model->n_pending_files < FILES_PER_QUERY) + gtk_file_system_model_request_next_files (model); + gtk_file_system_model_schedule_work (model); gdk_threads_leave (); return; @@ -1833,6 +1883,9 @@ gtk_file_system_model_got_files (GObject *object, { gdk_threads_enter (); + model->next_files_in_flight = FALSE; + model->enumeration_done = TRUE; + g_file_enumerator_close_async (enumerator, IO_PRIORITY, model->cancellable, @@ -1841,7 +1894,12 @@ gtk_file_system_model_got_files (GObject *object, g_clear_object (&model->dir_enumerator); - gtk_file_system_model_begin_finalize_load (model, error); + /* Batches that are still queued must be integrated first. */ + if (model->pending_files == NULL) + gtk_file_system_model_begin_finalize_load (model, error); + else if (error) + model->enumeration_error = g_error_copy (error); + gdk_threads_leave (); }