diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index 7ecf5f18e..a9394f9d0 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -2180,41 +2180,108 @@ shortcuts_add_bookmarks (GtkFileChooserDefault *impl) } /* 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_volume_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + struct CurrentFolderVolumeData *data = user_data; + GtkFileChooserDefault *impl = data->impl; + GtkFileSystemVolume *volume; + GMount *mount; + GFile *base_file; + int pos; + + mount = g_file_find_enclosing_mount_finish (G_FILE (source), result, NULL); + + GDK_THREADS_ENTER (); + + /* Superseded by another folder change, or the chooser went away. */ + if (impl->shortcuts_current_folder_cancellable == NULL || + g_cancellable_is_cancelled (impl->shortcuts_current_folder_cancellable) || + impl->current_folder == NULL || + !g_file_equal (impl->current_folder, data->file) || + impl->shortcuts_current_folder_active || + shortcut_find_position (impl, impl->current_folder) != -1) + { + if (mount) + g_object_unref (mount); + goto out; + } + + 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 (impl->current_folder, mount); + 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 (volume) + _gtk_file_system_volume_unref (volume); + } + + if (base_file) + g_object_unref (base_file); + +out: + GDK_THREADS_LEAVE (); + + g_object_unref (data->impl); + g_object_unref (data->file); + g_free (data); +} + +/* Adds the current folder to the shortcuts list if it is not already there. + * Finding the enclosing mount stats the folder and its parents, which can + * block for a long time on a slow or hung network share, so it is done + * asynchronously and the item appears once the answer is known. */ 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_volume_cb, + data); } /* Updates the current folder row in the shortcuts model */ @@ -2394,6 +2461,27 @@ add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback) return source; } +static void +new_folder_created_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + GtkFileChooserDefault *impl = user_data; + GFile *file = G_FILE (source); + GError *error = NULL; + + GDK_THREADS_ENTER (); + + if (g_file_make_directory_finish (file, result, &error)) + change_folder_and_display_error (impl, file, FALSE); + else + error_creating_folder_dialog (impl, file, error); + + GDK_THREADS_LEAVE (); + + g_object_unref (impl); +} + /* Idle handler for creating a new folder after editing its name cell, or for * canceling the editing. */ @@ -2422,13 +2510,10 @@ 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); - + /* Creating the folder can take long on a slow share. */ + g_file_make_directory_async (file, G_PRIORITY_DEFAULT, NULL, + new_folder_created_cb, + g_object_ref (impl)); g_object_unref (file); } else @@ -7009,6 +7094,12 @@ 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); + } + search_stop_searching (impl, TRUE); recent_stop_loading (impl); } @@ -7815,24 +7906,20 @@ 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); - } + /* Do not query the folder again here: reading it just failed, and on a + * hung network share a synchronous query would freeze the dialog. */ + name = g_file_get_parse_name (impl->current_folder); + if (name) + msg = g_strdup_printf (_("Could not read the contents of %s"), name); else msg = g_strdup (_("Could not read the contents of the folder")); error_message (impl, msg, error->message); g_free (msg); + g_free (name); } /* Callback used when the file system model finishes loading */ @@ -11251,8 +11338,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, @@ -11270,6 +11363,27 @@ shortcuts_activate_get_info_cb (GCancellable *cancellable, if (cancelled) goto out; + /* The location lives on a volume that is not mounted yet: mount it and + * query again (see shortcuts_activate_mount_enclosing_volume). */ + if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED) && + !data->tried_mount) + { + GMountOperation *mount_operation; + GtkWidget *toplevel; + + data->tried_mount = TRUE; + toplevel = gtk_widget_get_toplevel (GTK_WIDGET (data->impl)); + mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel)); + + data->impl->shortcuts_activate_iter_cancellable = + _gtk_file_system_mount_enclosing_volume (data->impl->file_system, data->file, + mount_operation, + 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 @@ -11354,38 +11468,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); - } + /* Query the location directly instead of looking up its volume + * first (which blocks on a slow share); the callback mounts the + * volume if the query reports that it is not mounted. */ + 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) { diff --git a/gtk/gtkfilechooserprivate.h b/gtk/gtkfilechooserprivate.h index 124ab90d9..b5963b4d4 100644 --- a/gtk/gtkfilechooserprivate.h +++ b/gtk/gtkfilechooserprivate.h @@ -265,6 +265,7 @@ struct _GtkFileChooserDefault GCancellable *file_exists_get_info_cancellable; GCancellable *update_from_entry_cancellable; GCancellable *shortcuts_activate_iter_cancellable; + GCancellable *shortcuts_current_folder_cancellable; LoadState load_state; ReloadState reload_state; diff --git a/gtk/gtkfilesystem.c b/gtk/gtkfilesystem.c index 4d78102b7..6234f2879 100644 --- a/gtk/gtkfilesystem.c +++ b/gtk/gtkfilesystem.c @@ -1077,6 +1077,19 @@ _gtk_file_system_set_bookmark_label (GtkFileSystem *file_system, g_signal_emit_by_name (file_system, "bookmarks-changed", 0); } +GtkFileSystemVolume * +_gtk_file_system_volume_for_mount (GFile *file, + GMount *mount) +{ + if (!mount && g_file_is_native (file)) + return (GtkFileSystemVolume *) root_volume_token; + + return (GtkFileSystemVolume *) mount; +} + +/* Note: this blocks (it stats @file and its parents); prefer + * g_file_find_enclosing_mount_async() plus _gtk_file_system_volume_for_mount() + * in code that runs on the main thread. */ GtkFileSystemVolume * _gtk_file_system_get_volume_for_file (GtkFileSystem *file_system, GFile *file) diff --git a/gtk/gtkfilesystem.h b/gtk/gtkfilesystem.h index 7fc539ec1..1c3399138 100644 --- a/gtk/gtkfilesystem.h +++ b/gtk/gtkfilesystem.h @@ -130,6 +130,11 @@ void _gtk_file_system_set_bookmark_label (GtkFileSystem *file_system, GFile *file, const gchar *label); +/* Turns the result of g_file_find_enclosing_mount{,_async}() for @file into + * the volume _gtk_file_system_get_volume_for_file() would return. Takes + * ownership of @mount. */ +GtkFileSystemVolume * _gtk_file_system_volume_for_mount (GFile *file, + GMount *mount); GtkFileSystemVolume * _gtk_file_system_get_volume_for_file (GtkFileSystem *file_system, GFile *file);