[gtk/gtk-3-24: 1/2] Make gtk_file_chooser_get_filter work for portal case



commit d59b28d5f4e2c4ff4cb51df0a39d94c7df7fee96
Author: Michael Weghorn <m weghorn posteo de>
Date:   Sat May 23 14:37:24 2020 +0200

    Make gtk_file_chooser_get_filter work for portal case
    
    This makes 'gtk_file_chooser_get_filter' work for the
    portal native file chooser by handling the corresponding
    'current_filter' argument in the response retrieved via
    D-Bus.
    
    In order to try to map the retrieved 'current_filter' to one
    of the existing list of filters, use the retrieved filter's name,
    similar to how xdg-desktop-portal-gtk does it when evaluating the
    'current_filter' input parameter in 'options'.)
    
    Note: This depends on the following merge/pull requests
    which fix the filter handling in gtk for native file choosers
    and introduce the 'current_filter' handling for FileChooser portal.
    
    * https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/1959
    * https://github.com/flatpak/xdg-desktop-portal/pull/493
    * https://github.com/flatpak/xdg-desktop-portal-gtk/pull/311
    
    This fixes #1820 for desktop portal case.
    
    Fixes: #1820

 gtk/gtkfilechoosernativeportal.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
---
diff --git a/gtk/gtkfilechoosernativeportal.c b/gtk/gtkfilechoosernativeportal.c
index d005571f1b..e784784a41 100644
--- a/gtk/gtkfilechoosernativeportal.c
+++ b/gtk/gtkfilechoosernativeportal.c
@@ -104,6 +104,7 @@ response_cb (GDBusConnection  *connection,
   int i;
   GVariant *response_data;
   GVariant *choices = NULL;
+  GVariant *current_filter = NULL;
 
   g_variant_get (parameters, "(u@a{sv})", &portal_response, &response_data);
   g_variant_lookup (response_data, "uris", "^a&s", &uris);
@@ -121,6 +122,35 @@ response_cb (GDBusConnection  *connection,
       g_variant_unref (choices);
     }
 
+  current_filter = g_variant_lookup_value (response_data, "current_filter", G_VARIANT_TYPE ("(sa(us))"));
+  if (current_filter)
+    {
+      GtkFileFilter *filter = gtk_file_filter_new_from_gvariant (current_filter);
+      const gchar *current_filter_name = gtk_file_filter_get_name (filter);
+
+      /* Try to find  the given filter in the list of filters.
+       * Since filters are compared by pointer value, using the passed
+       * filter would otherwise not match in a comparison, even if
+       * a filter in the list of filters has been selected.
+       * We'll use the heuristic that if two filters have the same name,
+       * they must be the same.
+       * If there is no match, just set the filter as it was retrieved.
+       */
+      GtkFileFilter *filter_to_select = filter;
+      GSList *filters = gtk_file_chooser_list_filters (GTK_FILE_CHOOSER (self));
+      for (GSList *l = filters; l; l = l->next)
+        {
+          GtkFileFilter *f = l->data;
+          if (g_strcmp0 (gtk_file_filter_get_name (f), current_filter_name) == 0)
+            {
+              filter_to_select = f;
+              break;
+            }
+        }
+      g_slist_free (filters);
+      gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (self), filter_to_select);
+    }
+
   g_slist_free_full (self->custom_files, g_object_unref);
   self->custom_files = NULL;
   for (i = 0; uris[i]; i++)


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