diff options
author | Michael Weghorn <m.weghorn@posteo.de> | 2020-05-23 14:37:24 +0200 |
---|---|---|
committer | Michael Weghorn <m.weghorn@posteo.de> | 2020-06-09 22:59:54 +0200 |
commit | ecc6c255efba2f779d06e434b5c10779d8972591 (patch) | |
tree | c28c77195b8b562065c5cea4ad464abc1adf9fec /gtk/gtkfilechoosernativeportal.c | |
parent | 8cb50ac6e9c6a78f337e4ad2bb61aa0558844904 (diff) | |
download | gtk+-ecc6c255efba2f779d06e434b5c10779d8972591.tar.gz |
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
Diffstat (limited to 'gtk/gtkfilechoosernativeportal.c')
-rw-r--r-- | gtk/gtkfilechoosernativeportal.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/gtk/gtkfilechoosernativeportal.c b/gtk/gtkfilechoosernativeportal.c index f87a974c89..f0854d30e4 100644 --- a/gtk/gtkfilechoosernativeportal.c +++ b/gtk/gtkfilechoosernativeportal.c @@ -105,6 +105,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); @@ -122,6 +123,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++) |