[gtranslator/file-chooser-native] Use GtkFileChooserNative




commit de0b0dadbd227cca1d86d7f8e15d509d0417b64e
Author: Daniel GarcĂ­a Moreno <dani danigm net>
Date:   Fri Sep 11 11:01:01 2020 +0200

    Use GtkFileChooserNative
    
    This patch uses the GtkFileChooserNative instead of the GtkFileChooser
    to open and save po files. This will improve the experience for flatpak
    because it will show bookmarks and the integration with the host system
    is better.
    
    This change also introduces a small issue with file paths. The
    GtkFileChooserNative returns a path like `/run/user/...` for each file
    so it's not possible to know the real path of that file.
    
    We were showing the full file path on the headerbar, but with this temp
    path that's not useful, so this patch removes that subtitle.
    
    You can notice this problem also when clicking on "save as" because the
    default folder will be that temp path, instead the user folder.
    
    Fix https://gitlab.gnome.org/GNOME/gtranslator/-/issues/116

 src/gtr-actions-file.c | 51 ++++++++++----------------------------------------
 src/gtr-file-dialogs.c | 11 +++++------
 src/gtr-window.c       |  3 +--
 3 files changed, 16 insertions(+), 49 deletions(-)
---
diff --git a/src/gtr-actions-file.c b/src/gtr-actions-file.c
index 27137b3b..672d8c66 100644
--- a/src/gtr-actions-file.c
+++ b/src/gtr-actions-file.c
@@ -151,24 +151,13 @@ gtr_file_chooser_analyse (gpointer dialog,
 {
   gint reply;
 
-  reply = gtk_dialog_run (GTK_DIALOG (dialog));
-  switch (reply)
+  reply = gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog));
+  if (reply == GTK_RESPONSE_ACCEPT && mode == FILESEL_OPEN)
     {
-    case GTK_RESPONSE_ACCEPT:
-      if (mode == FILESEL_OPEN)
-        {
-          gtr_po_parse_files_from_dialog (GTK_WIDGET (dialog), window);
-        }
-      break;
-    case GTK_RESPONSE_CANCEL:
-      gtk_widget_hide (GTK_WIDGET (dialog));
-      break;
-    case GTK_RESPONSE_DELETE_EVENT:
-      gtk_widget_hide (GTK_WIDGET (dialog));
-      break;
-    default:
-      break;
+      gtr_po_parse_files_from_dialog (GTK_WIDGET (dialog), window);
     }
+
+  g_object_unref (dialog);
 }
 
 gboolean
@@ -261,7 +250,7 @@ save_dialog_response_cb (GtkDialog * dialog,
 
   if (response_id != GTK_RESPONSE_ACCEPT)
     {
-      gtk_widget_destroy (GTK_WIDGET (dialog));
+      g_object_unref (dialog);
       return;
     }
 
@@ -271,7 +260,7 @@ save_dialog_response_cb (GtkDialog * dialog,
   location = g_file_new_for_path (filename);
   g_free (filename);
 
-  gtk_widget_destroy (GTK_WIDGET (dialog));
+  g_object_unref (dialog);
 
   if (po != NULL)
     {
@@ -330,8 +319,7 @@ gtr_save_file_as_dialog (GtkAction * action, GtrWindow * window)
   GtkWidget *dialog = NULL;
   GtrTab *current_page;
   GtrPo *po;
-  GFile *location;
-  gchar *uri = NULL;
+  gint reply = 0;
 
   if (dialog != NULL)
     {
@@ -354,29 +342,10 @@ gtr_save_file_as_dialog (GtkAction * action, GtrWindow * window)
                     G_CALLBACK (confirm_overwrite_callback), NULL);
 
   gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
-
-  /*Set the suggested file */
-  location = gtr_po_get_location (po);
-
-  uri = g_file_get_uri (location);
-
-  g_object_unref (location);
-
-  if (uri)
-    gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (dialog), uri);
-
-  g_free (uri);
-
-  /*
-   * FIXME: If we can't set the uri we should add a default path and name
-   */
-
   g_object_set_data (G_OBJECT (dialog), GTR_TAB_SAVE_AS, current_page);
 
-  g_signal_connect (dialog,
-                    "response", G_CALLBACK (save_dialog_response_cb), window);
-
-  gtk_widget_show (GTK_WIDGET (dialog));
+  reply = gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog));
+  save_dialog_response_cb (GTK_DIALOG (dialog), reply, window);
 }
 
 /*
diff --git a/src/gtr-file-dialogs.c b/src/gtr-file-dialogs.c
index 9a0d7c58..1972ccb6 100644
--- a/src/gtr-file-dialogs.c
+++ b/src/gtr-file-dialogs.c
@@ -33,20 +33,19 @@ gtr_file_chooser_new (GtkWindow * parent,
                       FileselMode mode,
                       const gchar * title, const gchar * dir)
 {
-  GtkWidget *dialog;
+  GtkFileChooserNative *dialog;
   GtkFileFilter *filter;
 
-  dialog = gtk_file_chooser_dialog_new (title,
+  dialog = gtk_file_chooser_native_new (title,
                                         parent,
                                         (mode ==
                                          FILESEL_SAVE) ?
                                         GTK_FILE_CHOOSER_ACTION_SAVE :
                                         GTK_FILE_CHOOSER_ACTION_OPEN,
-                                        _("_Cancel"), GTK_RESPONSE_CANCEL,
                                         (mode ==
                                          FILESEL_SAVE) ? _("_Save") :
-                                        _("_Open"), GTK_RESPONSE_ACCEPT,
-                                        NULL);
+                                        _("_Open"), _("_Cancel"));
+
   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
 
   if (dir)
@@ -80,5 +79,5 @@ gtr_file_chooser_new (GtkWindow * parent,
 
   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
 
-  return dialog;
+  return GTK_WIDGET (dialog);
 }
diff --git a/src/gtr-window.c b/src/gtr-window.c
index 2e5c1cc2..8ad54585 100644
--- a/src/gtr-window.c
+++ b/src/gtr-window.c
@@ -208,9 +208,8 @@ set_window_title (GtrWindow * window, gboolean with_path)
       state = gtr_po_get_state (gtr_tab_get_po (active_tab));
       po = gtr_tab_get_po (active_tab);
       file = gtr_po_get_location (po);
-      path = g_file_get_path (file);
       basename = g_file_get_basename (file);
-      subtitle = path;
+      subtitle = g_strdup ("");
 
       if (state == GTR_PO_STATE_MODIFIED)
         {


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