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
This commit is contained in:
Michael Weghorn 2020-05-23 14:37:24 +02:00
parent 8cb50ac6e9
commit ecc6c255ef

View File

@ -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++)