filechooser: no blocking file system calls when changing folders

This commit is contained in:
Daemonratte 2026-10-13 14:18:34 +02:00
commit 21087a4009
4 changed files with 292 additions and 107 deletions

View file

@ -410,6 +410,12 @@ static gboolean icons_queue_update (GtkWidget *view,
GdkEventExpose *event,
GtkFileChooserDefault *impl);
static void icon_queries_cancel_all (GtkFileChooserDefault *impl);
static void create_folder (GtkFileChooserDefault *impl,
GFile *file,
gboolean respond);
static void set_busy_cursor (GtkFileChooserDefault *impl,
gboolean busy);
static void request_response_and_add_to_recent_list (GtkFileChooserDefault *impl);
static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
static void view_mode_set (GtkFileChooserDefault *impl, ViewMode view_mode);
@ -2176,42 +2182,99 @@ shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
profile_end ("end", NULL);
}
/* Appends a separator and a row to the shortcuts list for the current folder */
struct CurrentFolderVolumeData
{
GtkFileChooserDefault *impl;
GFile *file;
};
static void
shortcuts_add_current_folder_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
struct CurrentFolderVolumeData *data = user_data;
GtkFileChooserDefault *impl = data->impl;
GtkFileSystemVolume *volume;
GError *error = NULL;
GFile *base_file;
GMount *mount;
int pos;
mount = g_file_find_enclosing_mount_finish (G_FILE (source), result, &error);
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
goto out;
GDK_THREADS_ENTER ();
g_clear_object (&impl->shortcuts_current_folder_cancellable);
/* Separator */
shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
/* Item */
pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
volume = _gtk_file_system_volume_for_mount (data->file, mount);
if (volume)
base_file = _gtk_file_system_volume_get_root (volume);
else
base_file = NULL;
if (base_file && g_file_equal (base_file, data->file))
shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
else
{
shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
if (volume)
_gtk_file_system_volume_unref (volume);
}
if (base_file)
g_object_unref (base_file);
GDK_THREADS_LEAVE ();
out:
g_clear_error (&error);
g_object_unref (data->impl);
g_object_unref (data->file);
g_free (data);
}
/* Appends a separator and a row to the shortcuts list for the current
* folder. Finding the volume of the folder stats the folder and its
* parents, which can block for a long time on a slow or hung share, so the
* row is added once the answer comes in. */
static void
shortcuts_add_current_folder (GtkFileChooserDefault *impl)
{
int pos;
struct CurrentFolderVolumeData *data;
g_assert (!impl->shortcuts_current_folder_active);
g_assert (impl->current_folder != NULL);
pos = shortcut_find_position (impl, impl->current_folder);
if (pos == -1)
if (impl->shortcuts_current_folder_cancellable)
{
GtkFileSystemVolume *volume;
GFile *base_file;
/* Separator */
shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
/* Item */
pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
if (volume)
base_file = _gtk_file_system_volume_get_root (volume);
else
base_file = NULL;
if (base_file && g_file_equal (base_file, impl->current_folder))
shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
else
shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
if (base_file)
g_object_unref (base_file);
g_cancellable_cancel (impl->shortcuts_current_folder_cancellable);
g_clear_object (&impl->shortcuts_current_folder_cancellable);
}
if (shortcut_find_position (impl, impl->current_folder) != -1)
return;
impl->shortcuts_current_folder_cancellable = g_cancellable_new ();
data = g_new (struct CurrentFolderVolumeData, 1);
data->impl = g_object_ref (impl);
data->file = g_object_ref (impl->current_folder);
g_file_find_enclosing_mount_async (impl->current_folder,
G_PRIORITY_DEFAULT,
impl->shortcuts_current_folder_cancellable,
shortcuts_add_current_folder_cb,
data);
}
/* Updates the current folder row in the shortcuts model */
@ -2396,6 +2459,85 @@ add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
return source;
}
struct CreateFolderData
{
GtkFileChooserDefault *impl;
GFile *file;
gboolean respond;
};
static void
create_folder_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
struct CreateFolderData *data = user_data;
GtkFileChooserDefault *impl = data->impl;
GError *error = NULL;
gboolean created;
created = g_file_make_directory_finish (G_FILE (source), result, &error);
GDK_THREADS_ENTER ();
if (data->respond)
set_busy_cursor (impl, FALSE);
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_error_free (error);
else
{
GFile *parent = g_file_get_parent (data->file);
g_clear_object (&impl->create_folder_cancellable);
if (!created)
error_creating_folder_dialog (impl, data->file, error);
else if (data->respond)
request_response_and_add_to_recent_list (impl);
/* Show the new folder, unless the user went elsewhere meanwhile. */
else if (impl->current_folder && parent && g_file_equal (impl->current_folder, parent))
change_folder_and_display_error (impl, data->file, FALSE);
if (parent)
g_object_unref (parent);
}
GDK_THREADS_LEAVE ();
g_object_unref (data->impl);
g_object_unref (data->file);
g_free (data);
}
/* Creates the folder @file; on a slow share this can take a while. Then
* either responds (the folder was typed in a "select folder" dialog) or
* shows the new folder. */
static void
create_folder (GtkFileChooserDefault *impl,
GFile *file,
gboolean respond)
{
struct CreateFolderData *data;
if (impl->create_folder_cancellable)
g_cancellable_cancel (impl->create_folder_cancellable);
g_clear_object (&impl->create_folder_cancellable);
impl->create_folder_cancellable = g_cancellable_new ();
data = g_new (struct CreateFolderData, 1);
data->impl = g_object_ref (impl);
data->file = g_object_ref (file);
data->respond = respond;
if (respond)
set_busy_cursor (impl, TRUE);
g_file_make_directory_async (file, G_PRIORITY_DEFAULT,
impl->create_folder_cancellable,
create_folder_cb, data);
}
/* Idle handler for creating a new folder after editing its name cell, or for
* canceling the editing.
*/
@ -2424,13 +2566,7 @@ edited_idle_cb (GtkFileChooserDefault *impl)
&error);
if (file)
{
GError *error = NULL;
if (g_file_make_directory (file, NULL, &error))
change_folder_and_display_error (impl, file, FALSE);
else
error_creating_folder_dialog (impl, file, error);
create_folder (impl, file, FALSE);
g_object_unref (file);
}
else
@ -7097,6 +7233,18 @@ cancel_all_operations (GtkFileChooserDefault *impl)
impl->shortcuts_activate_iter_cancellable = NULL;
}
if (impl->shortcuts_current_folder_cancellable)
{
g_cancellable_cancel (impl->shortcuts_current_folder_cancellable);
g_clear_object (&impl->shortcuts_current_folder_cancellable);
}
if (impl->create_folder_cancellable)
{
g_cancellable_cancel (impl->create_folder_cancellable);
g_clear_object (&impl->create_folder_cancellable);
}
icon_queries_cancel_all (impl);
search_stop_searching (impl, TRUE);
@ -7912,24 +8060,17 @@ pending_select_files_process (GtkFileChooserDefault *impl)
static void
show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
{
GFileInfo *info;
char *name;
char *msg;
info = g_file_query_info (impl->current_folder,
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
G_FILE_QUERY_INFO_NONE,
NULL,
NULL);
if (info)
{
msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
g_object_unref (info);
}
else
msg = g_strdup (_("Could not read the contents of the folder"));
/* Reading the folder just failed; asking it for its display name would
* block a hung network share again. The parse name needs no I/O. */
name = g_file_get_parse_name (impl->current_folder);
msg = g_strdup_printf (_("Could not read the contents of %s"), name);
error_message (impl, msg, error->message);
g_free (msg);
g_free (name);
}
/* Callback used when the file system model finishes loading */
@ -10162,20 +10303,10 @@ name_entry_get_parent_info_cb (GCancellable *cancellable,
else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
|| data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
{
GError *mkdir_error = NULL;
/* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
* "/blah/nonexistent" you *will* want a folder created.
*/
set_busy_cursor (data->impl, TRUE);
g_file_make_directory (data->file, NULL, &mkdir_error);
set_busy_cursor (data->impl, FALSE);
if (!mkdir_error)
request_response_and_add_to_recent_list (data->impl);
else
error_creating_folder_dialog (data->impl, data->file, mkdir_error);
create_folder (data->impl, data->file, TRUE);
}
else
g_assert_not_reached ();
@ -11466,8 +11597,14 @@ struct ShortcutsActivateData
{
GtkFileChooserDefault *impl;
GFile *file;
gboolean tried_mount;
};
static void shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
GtkFileSystemVolume *volume,
const GError *error,
gpointer user_data);
static void
shortcuts_activate_get_info_cb (GCancellable *cancellable,
GFileInfo *info,
@ -11485,6 +11622,22 @@ shortcuts_activate_get_info_cb (GCancellable *cancellable,
if (cancelled)
goto out;
/* The location is on a volume that is not mounted: mount it and ask
* again (see shortcuts_activate_mount_enclosing_volume()). */
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED) && !data->tried_mount)
{
GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (data->impl));
data->tried_mount = TRUE;
data->impl->shortcuts_activate_iter_cancellable =
_gtk_file_system_mount_enclosing_volume (data->impl->file_system, data->file,
gtk_mount_operation_new (GTK_WINDOW (toplevel)),
shortcuts_activate_mount_enclosing_volume,
data);
g_object_unref (cancellable);
return;
}
if (!error && _gtk_file_info_consider_as_directory (info))
change_folder_and_display_error (data->impl, data->file, FALSE);
else
@ -11507,25 +11660,34 @@ shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
gpointer user_data)
{
struct ShortcutsActivateData *data = user_data;
GtkFileChooserDefault *impl = data->impl;
/* Another location was activated meanwhile, or the dialog went away. */
if (cancellable != impl->shortcuts_activate_iter_cancellable ||
g_cancellable_is_cancelled (cancellable))
goto out;
impl->shortcuts_activate_iter_cancellable = NULL;
if (error)
{
error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
g_object_unref (data->impl);
g_object_unref (data->file);
g_free (data);
return;
error_changing_folder_dialog (impl, data->file, g_error_copy (error));
goto out;
}
data->impl->shortcuts_activate_iter_cancellable =
_gtk_file_system_get_info (data->impl->file_system, data->file,
impl->shortcuts_activate_iter_cancellable =
_gtk_file_system_get_info (impl->file_system, data->file,
"standard::type",
shortcuts_activate_get_info_cb, data);
g_object_unref (cancellable);
return;
if (volume)
_gtk_file_system_volume_unref (volume);
out:
g_object_unref (data->impl);
g_object_unref (data->file);
g_free (data);
g_object_unref (cancellable);
}
static void
@ -11569,38 +11731,20 @@ shortcuts_activate_iter (GtkFileChooserDefault *impl,
else if (shortcut_type == SHORTCUT_TYPE_FILE)
{
struct ShortcutsActivateData *data;
GtkFileSystemVolume *volume;
operation_mode_set (impl, OPERATION_MODE_BROWSE);
volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
data = g_new0 (struct ShortcutsActivateData, 1);
data->impl = g_object_ref (impl);
data->file = g_object_ref (col_data);
if (!volume || !_gtk_file_system_volume_is_mounted (volume))
{
GMountOperation *mount_operation;
GtkWidget *toplevel;
toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
impl->shortcuts_activate_iter_cancellable =
_gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
mount_operation,
shortcuts_activate_mount_enclosing_volume,
data);
}
else
{
impl->shortcuts_activate_iter_cancellable =
_gtk_file_system_get_info (impl->file_system, data->file,
"standard::type",
shortcuts_activate_get_info_cb, data);
}
/* Ask the location directly instead of looking up its volume first,
* which blocks on a slow share; if it is on a volume that is not
* mounted, the answer says so and the volume is mounted then. */
impl->shortcuts_activate_iter_cancellable =
_gtk_file_system_get_info (impl->file_system, data->file,
"standard::type",
shortcuts_activate_get_info_cb, data);
}
else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
{

View file

@ -264,6 +264,8 @@ struct _GtkFileChooserDefault
GCancellable *file_exists_get_info_cancellable;
GCancellable *update_from_entry_cancellable;
GCancellable *shortcuts_activate_iter_cancellable;
GCancellable *shortcuts_current_folder_cancellable;
GCancellable *create_folder_cancellable;
LoadState load_state;
ReloadState reload_state;

View file

@ -840,33 +840,62 @@ _gtk_file_system_mount_volume (GtkFileSystem *file_system,
return cancellable;
}
static void
enclosing_volume_found_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
AsyncFuncData *async_data = user_data;
GtkFileSystemVolume *volume;
GMount *mount;
mount = g_file_find_enclosing_mount_finish (G_FILE (source_object), result, NULL);
volume = _gtk_file_system_volume_for_mount (G_FILE (source_object), mount);
gdk_threads_enter ();
((GtkFileSystemVolumeMountCallback) async_data->callback) (async_data->cancellable, volume,
NULL, async_data->data);
gdk_threads_leave ();
_gtk_file_system_volume_unref (volume);
free_async_data (async_data);
}
static void
enclosing_volume_mount_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GtkFileSystemVolume *volume;
AsyncFuncData *async_data;
GError *error = NULL;
async_data = (AsyncFuncData *) user_data;
g_file_mount_enclosing_volume_finish (G_FILE (source_object), result, &error);
volume = _gtk_file_system_get_volume_for_file (async_data->file_system, G_FILE (source_object));
/* Silently drop G_IO_ERROR_ALREADY_MOUNTED error for gvfs backends without visible mounts. */
/* Better than doing query_info with additional I/O every time. */
if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED))
g_clear_error (&error);
gdk_threads_enter ();
((GtkFileSystemVolumeMountCallback) async_data->callback) (async_data->cancellable, volume,
error, async_data->data);
gdk_threads_leave ();
if (error)
g_error_free (error);
{
gdk_threads_enter ();
((GtkFileSystemVolumeMountCallback) async_data->callback) (async_data->cancellable, NULL,
error, async_data->data);
gdk_threads_leave ();
_gtk_file_system_volume_unref (volume);
g_error_free (error);
free_async_data (async_data);
return;
}
/* Finding the volume stats @file and its parents on the share that was
* just mounted, which can take a while. */
g_file_find_enclosing_mount_async (G_FILE (source_object),
G_PRIORITY_DEFAULT,
async_data->cancellable,
enclosing_volume_found_cb,
async_data);
}
GCancellable *
@ -1077,22 +1106,30 @@ _gtk_file_system_set_bookmark_label (GtkFileSystem *file_system,
g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
}
/* The volume for the result of g_file_find_enclosing_mount() (or its
* async variant) for @file; takes ownership of @mount. */
GtkFileSystemVolume *
_gtk_file_system_get_volume_for_file (GtkFileSystem *file_system,
GFile *file)
_gtk_file_system_volume_for_mount (GFile *file,
GMount *mount)
{
GMount *mount;
DEBUG ("get_volume_for_file");
mount = g_file_find_enclosing_mount (file, NULL, NULL);
if (!mount && g_file_is_native (file))
return (GtkFileSystemVolume *) root_volume_token;
return (GtkFileSystemVolume *) mount;
}
/* Blocks while @file and its parents are examined; code on the main thread
* should use g_file_find_enclosing_mount_async() and
* _gtk_file_system_volume_for_mount() instead. */
GtkFileSystemVolume *
_gtk_file_system_get_volume_for_file (GtkFileSystem *file_system,
GFile *file)
{
DEBUG ("get_volume_for_file");
return _gtk_file_system_volume_for_mount (file, g_file_find_enclosing_mount (file, NULL, NULL));
}
/* GtkFolder methods */
static void
gtk_folder_set_property (GObject *object,

View file

@ -130,6 +130,8 @@ void _gtk_file_system_set_bookmark_label (GtkFileSystem *file_system,
GFile *file,
const gchar *label);
GtkFileSystemVolume * _gtk_file_system_volume_for_mount (GFile *file,
GMount *mount);
GtkFileSystemVolume * _gtk_file_system_get_volume_for_file (GtkFileSystem *file_system,
GFile *file);