[epiphany/wip/exalm/container: 6/6] Prefer GFile-based GtkFileChooser API




commit aa14dcaf1c5859d285027efd03f225b8eeb7c631
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Thu Feb 17 16:14:34 2022 +0500

    Prefer GFile-based GtkFileChooser API
    
    Everything else will go away in GTK4, and while some of the old functions
    got renamed, it's easier to just rename them later rather than move them
    from path/uri to GFiles.
    
    Part-of: <https://gitlab.gnome.org/GNOME/epiphany/-/merge_requests/1072>

 embed/ephy-web-view.c                | 30 +++++++++++++++----
 src/popup-commands.c                 | 32 ++++++++++++++++----
 src/preferences/prefs-general-page.c | 36 +++++++++++++++++------
 src/window-commands.c                | 57 ++++++++++++++++++++++++++----------
 4 files changed, 119 insertions(+), 36 deletions(-)
---
diff --git a/embed/ephy-web-view.c b/embed/ephy-web-view.c
index 98e3e1533..6649bdfba 100644
--- a/embed/ephy-web-view.c
+++ b/embed/ephy-web-view.c
@@ -164,6 +164,8 @@ open_response_cb (GtkFileChooser           *dialog,
   if (response == GTK_RESPONSE_ACCEPT) {
     GSList *file_list = gtk_file_chooser_get_filenames (dialog);
     GPtrArray *file_array = g_ptr_array_new ();
+    g_autoptr (GFile) current_folder = NULL;
+    g_autofree char *current_folder_path = NULL;
 
     for (GSList *file = file_list; file; file = g_slist_next (file))
       g_ptr_array_add (file_array, file->data);
@@ -172,7 +174,12 @@ open_response_cb (GtkFileChooser           *dialog,
     webkit_file_chooser_request_select_files (request, (const char * const *)file_array->pdata);
     g_slist_free_full (file_list, g_free);
     g_ptr_array_free (file_array, TRUE);
-    g_settings_set_string (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_LAST_UPLOAD_DIRECTORY, 
gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog)));
+
+    current_folder = gtk_file_chooser_get_current_folder_file (dialog);
+    current_folder_path = g_file_get_path (current_folder);
+    g_settings_set_string (EPHY_SETTINGS_WEB,
+                           EPHY_PREFS_WEB_LAST_UPLOAD_DIRECTORY,
+                           current_folder_path);
   } else {
     webkit_file_chooser_request_cancel (request);
   }
@@ -189,6 +196,7 @@ ephy_web_view_run_file_chooser (WebKitWebView            *web_view,
   GtkFileChooser *dialog;
   gboolean allows_multiple_selection = webkit_file_chooser_request_get_select_multiple (request);
   GtkFileFilter *filter = webkit_file_chooser_request_get_mime_types_filter (request);
+  g_autofree char *last_directory_path = NULL;
 
   dialog = ephy_create_file_chooser (_("Open"),
                                      GTK_WIDGET (toplevel),
@@ -196,12 +204,24 @@ ephy_web_view_run_file_chooser (WebKitWebView            *web_view,
                                      EPHY_FILE_FILTER_ALL);
 
   if (filter) {
-    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-    gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);
+    gtk_file_chooser_add_filter (dialog, filter);
+    gtk_file_chooser_set_filter (dialog, filter);
+  }
+
+  last_directory_path = g_settings_get_string (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_LAST_UPLOAD_DIRECTORY);
+
+  if (last_directory_path && last_directory_path[0]) {
+    g_autoptr (GFile) last_directory = NULL;
+    g_autoptr (GError) error = NULL;
+
+    last_directory = g_file_new_for_path (last_directory_path);
+    gtk_file_chooser_set_current_folder_file (dialog, last_directory, &error);
+
+    if (error)
+      g_warning ("Failed to set current folder %s: %s", last_directory_path, error->message);
   }
 
-  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_settings_get_string (EPHY_SETTINGS_WEB, 
EPHY_PREFS_WEB_LAST_UPLOAD_DIRECTORY));
-  gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), allows_multiple_selection);
+  gtk_file_chooser_set_select_multiple (dialog, allows_multiple_selection);
 
   g_signal_connect (dialog, "response",
                     G_CALLBACK (open_response_cb),
diff --git a/src/popup-commands.c b/src/popup-commands.c
index 2f321a190..ccb91725c 100644
--- a/src/popup-commands.c
+++ b/src/popup-commands.c
@@ -157,20 +157,27 @@ filename_confirmed_cb (GtkFileChooser      *dialog,
   gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (dialog));
 
   if (response == GTK_RESPONSE_ACCEPT) {
-    char *uri;
+    g_autoptr (GFile) file = NULL;
+    g_autoptr (GFile) current_folder = NULL;
+    g_autofree char *uri = NULL;
+    g_autofree char *current_folder_path = NULL;
     WebKitDownload *webkit_download;
 
-    uri = gtk_file_chooser_get_uri (dialog);
+    file = gtk_file_chooser_get_file (dialog);
+    uri = g_file_get_uri (file);
     ephy_download_set_destination_uri (data->download, uri);
-    g_free (uri);
 
     webkit_download = ephy_download_get_webkit_download (data->download);
     webkit_download_set_allow_overwrite (webkit_download, TRUE);
 
     ephy_downloads_manager_add_download (ephy_embed_shell_get_downloads_manager 
(ephy_embed_shell_get_default ()),
                                          data->download);
-    g_settings_set_string (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY,
-                           gtk_file_chooser_get_current_folder (dialog));
+
+    current_folder = gtk_file_chooser_get_current_folder_file (dialog);
+    current_folder_path = g_file_get_path (current_folder);
+    g_settings_set_string (EPHY_SETTINGS_WEB,
+                           EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY,
+                           current_folder_path);
   } else {
     g_idle_add_full (G_PRIORITY_DEFAULT,
                      (GSourceFunc)cancel_download_idle_cb,
@@ -190,6 +197,7 @@ filename_suggested_cb (EphyDownload        *download,
                        SavePropertyURLData *data)
 {
   GtkFileChooser *dialog;
+  const char *last_directory_path;
   char *sanitized_filename;
 
   dialog = ephy_create_file_chooser (data->title,
@@ -197,7 +205,19 @@ filename_suggested_cb (EphyDownload        *download,
                                      GTK_FILE_CHOOSER_ACTION_SAVE,
                                      EPHY_FILE_FILTER_NONE);
   gtk_file_chooser_set_do_overwrite_confirmation (dialog, TRUE);
-  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_settings_get_string (EPHY_SETTINGS_WEB, 
EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY));
+
+  last_directory_path = g_settings_get_string (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY);
+
+  if (last_directory_path && last_directory_path[0]) {
+    g_autoptr (GFile) last_directory = NULL;
+    g_autoptr (GError) error = NULL;
+
+    last_directory = g_file_new_for_path (last_directory_path);
+    gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (dialog), last_directory, &error);
+
+    if (error)
+      g_warning ("Failed to set current folder %s: %s", last_directory_path, error->message);
+  }
 
   sanitized_filename = ephy_sanitize_filename (g_strdup (suggested_filename));
   gtk_file_chooser_set_current_name (dialog, sanitized_filename);
diff --git a/src/preferences/prefs-general-page.c b/src/preferences/prefs-general-page.c
index cfce24fff..6dc6be6db 100644
--- a/src/preferences/prefs-general-page.c
+++ b/src/preferences/prefs-general-page.c
@@ -695,11 +695,15 @@ download_folder_file_chooser_cb (GtkNativeDialog  *chooser,
                                  PrefsGeneralPage *general_page)
 {
   if (response == GTK_RESPONSE_ACCEPT) {
-    g_autofree char *dir = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
+    g_autoptr (GFile) file = NULL;
+    g_autofree char *path = NULL;
 
-    if (dir)
+    file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (chooser));
+    path = g_file_get_path (file);
+
+    if (path)
       g_settings_set_string (EPHY_SETTINGS_STATE,
-                             EPHY_PREFS_STATE_DOWNLOAD_DIR, dir);
+                             EPHY_PREFS_STATE_DOWNLOAD_DIR, path);
   }
 
   gtk_native_dialog_destroy (chooser);
@@ -709,7 +713,7 @@ static void
 download_folder_row_activated_cb (PrefsGeneralPage *general_page)
 {
   GtkWidget *parent;
-  g_autofree char *dir = NULL;
+  g_autofree char *downloads_path = NULL;
   GtkFileChooserNative *chooser;
 
   parent = gtk_widget_get_toplevel (GTK_WIDGET (general_page));
@@ -720,8 +724,21 @@ download_folder_row_activated_cb (PrefsGeneralPage *general_page)
                                          _("_Cancel"));
   gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (chooser), TRUE);
 
-  dir = ephy_file_get_downloads_dir ();
-  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser), dir);
+  downloads_path = ephy_file_get_downloads_dir ();
+
+  if (downloads_path && downloads_path[0]) {
+    g_autoptr (GFile) downloads_dir = NULL;
+    g_autoptr (GError) error = NULL;
+
+    downloads_dir = g_file_new_for_path (downloads_path);
+
+    gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (chooser),
+                                              downloads_dir,
+                                              &error);
+
+    if (error)
+      g_warning ("Failed to set current folder %s: %s", downloads_path, error->message);
+  }
 
   g_signal_connect (chooser, "response",
                     G_CALLBACK (download_folder_file_chooser_cb),
@@ -835,11 +852,12 @@ webapp_icon_chooser_response_cb (GtkNativeDialog  *file_chooser,
                                  PrefsGeneralPage *general_page)
 {
   if (response == GTK_RESPONSE_ACCEPT) {
-    char *icon_url;
+    g_autoptr (GFile) icon_file = NULL;
+    g_autofree char *icon_url = NULL;
 
-    icon_url = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_chooser));
+    icon_file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_chooser));
+    icon_url = g_file_get_uri (icon_file);
     prefs_general_page_update_webapp_icon (general_page, icon_url);
-    g_free (icon_url);
     prefs_general_page_save_web_application (general_page);
   }
 
diff --git a/src/window-commands.c b/src/window-commands.c
index 8b138257d..f69f3c21c 100644
--- a/src/window-commands.c
+++ b/src/window-commands.c
@@ -351,6 +351,7 @@ dialog_bookmarks_import_file_chooser_cb (GtkNativeDialog *file_chooser_dialog,
 {
   EphyBookmarksManager *manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
   g_autoptr (GError) error = NULL;
+  g_autoptr (GFile) file = NULL;
   g_autofree char *filename = NULL;
   gboolean imported;
 
@@ -359,7 +360,8 @@ dialog_bookmarks_import_file_chooser_cb (GtkNativeDialog *file_chooser_dialog,
   if (response != GTK_RESPONSE_ACCEPT)
     return;
 
-  filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_chooser_dialog));
+  file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_chooser_dialog));
+  filename = g_file_get_path (file);
   imported = ephy_bookmarks_import (manager, filename, &error);
 
   show_import_export_result (parent, imported, imported, error,
@@ -396,6 +398,7 @@ dialog_bookmarks_import_from_html_file_chooser_cb (GtkNativeDialog *file_chooser
 {
   EphyBookmarksManager *manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
   g_autoptr (GError) error = NULL;
+  g_autoptr (GFile) file = NULL;
   g_autofree char *filename = NULL;
   gboolean imported;
 
@@ -404,7 +407,8 @@ dialog_bookmarks_import_from_html_file_chooser_cb (GtkNativeDialog *file_chooser
   if (response != GTK_RESPONSE_ACCEPT)
     return;
 
-  filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_chooser_dialog));
+  file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_chooser_dialog));
+  filename = g_file_get_path (file);
   imported = ephy_bookmarks_import_from_html (manager, filename, &error);
 
   show_import_export_result (parent, imported, imported, error,
@@ -617,6 +621,7 @@ export_bookmarks_file_chooser_cb (GtkNativeDialog *dialog,
                                   GtkWindow       *parent)
 {
   EphyBookmarksManager *manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
+  g_autoptr (GFile) file = NULL;
   g_autofree char *filename = NULL;
 
   gtk_native_dialog_destroy (dialog);
@@ -624,7 +629,8 @@ export_bookmarks_file_chooser_cb (GtkNativeDialog *dialog,
   if (response != GTK_RESPONSE_ACCEPT)
     return;
 
-  filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+  file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
+  filename = g_file_get_path (file);
   ephy_bookmarks_export (g_object_ref (manager),
                          filename,
                          NULL,
@@ -1330,18 +1336,18 @@ open_response_cb (GtkNativeDialog *dialog,
                   EphyWindow      *window)
 {
   if (response == GTK_RESPONSE_ACCEPT) {
-    char *uri, *converted;
+    g_autoptr (GFile) file = NULL;
+    g_autofree char *uri = NULL;
+    g_autofree char *converted = NULL;
 
-    uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
+    file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
+    uri = g_file_get_uri (file);
     if (uri != NULL) {
       converted = g_filename_to_utf8 (uri, -1, NULL, NULL, NULL);
 
       if (converted != NULL) {
         ephy_window_load_url (window, converted);
       }
-
-      g_free (converted);
-      g_free (uri);
     }
   }
 
@@ -2099,9 +2105,14 @@ save_response_cb (GtkNativeDialog *dialog,
                   EphyEmbed       *embed)
 {
   if (response == GTK_RESPONSE_ACCEPT) {
-    char *uri, *converted;
-
-    uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
+    g_autoptr (GFile) file = NULL;
+    g_autoptr (GFile) current_file = NULL;
+    g_autofree char *uri = NULL;
+    g_autofree char *converted = NULL;
+    g_autofree char *current_path = NULL;
+
+    file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
+    uri = g_file_get_uri (file);
     if (uri != NULL) {
       converted = g_filename_to_utf8 (uri, -1, NULL, NULL, NULL);
 
@@ -2113,12 +2124,13 @@ save_response_cb (GtkNativeDialog *dialog,
           ephy_web_view_save (web_view, converted);
         }
       }
-
-      g_free (converted);
-      g_free (uri);
     }
 
-    g_settings_set_string (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY, 
gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog)));
+    current_file = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (dialog));
+    current_path = g_file_get_path (current_file);
+    g_settings_set_string (EPHY_SETTINGS_WEB,
+                           EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY,
+                           current_path);
   }
 
   g_object_unref (dialog);
@@ -2134,6 +2146,7 @@ window_cmd_save_as (GSimpleAction *action,
   GtkFileChooser *dialog;
   GtkFileFilter *filter;
   char *suggested_filename;
+  const char *last_directory_path;
 
   embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
   g_assert (embed != NULL);
@@ -2144,7 +2157,19 @@ window_cmd_save_as (GSimpleAction *action,
                                      EPHY_FILE_FILTER_NONE);
 
   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
-  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_settings_get_string (EPHY_SETTINGS_WEB, 
EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY));
+
+  last_directory_path = g_settings_get_string (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY);
+
+  if (last_directory_path && last_directory_path[0]) {
+    g_autoptr (GFile) last_directory = NULL;
+    g_autoptr (GError) error = NULL;
+
+    last_directory = g_file_new_for_path (last_directory_path);
+    gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (dialog), last_directory, &error);
+
+    if (error)
+      g_warning ("Failed to set current folder %s: %s", last_directory_path, error->message);
+  }
 
   filter = gtk_file_filter_new ();
   gtk_file_filter_set_name (GTK_FILE_FILTER (filter), _("HTML"));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]