diff options
author | Benjamin Otte <otte@redhat.com> | 2023-03-10 16:12:00 +0100 |
---|---|---|
committer | Benjamin Otte <otte@redhat.com> | 2023-03-10 17:10:30 +0100 |
commit | 5c9fb3a6365577f67ec2e8d3858424877044a9d2 (patch) | |
tree | 485ef40196d37cc582cdcc280a2caf9580b45c47 | |
parent | 12858114a291b2e908bb6fdae982b5558c4ceee0 (diff) | |
download | gtk+-5c9fb3a6365577f67ec2e8d3858424877044a9d2.tar.gz |
filefilter: If not given a name, deduce one
Otherwise we end up with a critical when cosntructing the GVariant and
the portal claims our filter is for "[Invalid UTF-8]".
Test included
-rw-r--r-- | gtk/gtkfilefilter.c | 41 | ||||
-rw-r--r-- | testsuite/gtk/filefilter.c | 99 |
2 files changed, 134 insertions, 6 deletions
diff --git a/gtk/gtkfilefilter.c b/gtk/gtkfilefilter.c index 767c89b629..425d8d4ca7 100644 --- a/gtk/gtkfilefilter.c +++ b/gtk/gtkfilefilter.c @@ -68,15 +68,17 @@ */ #include "config.h" -#include <string.h> - -#include <gdk-pixbuf/gdk-pixbuf.h> #include "gtkfilefilterprivate.h" + #include "gtkbuildable.h" #include "gtkbuilderprivate.h" -#include "gtkprivate.h" #include "gtkfilter.h" +#include "gtkprivate.h" + +#include <glib/gi18n-lib.h> +#include <gdk-pixbuf/gdk-pixbuf.h> +#include <string.h> typedef struct _GtkFileFilterClass GtkFileFilterClass; typedef struct _FilterRule FilterRule; @@ -987,6 +989,13 @@ gtk_file_filter_to_gvariant (GtkFileFilter *filter) { GVariantBuilder builder; GSList *l; + GVariant *result; + char *name; + + if (filter->name) + name = g_strdup (filter->name); + else + name = NULL; g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(us)")); for (l = filter->rules; l; l = l->next) @@ -998,6 +1007,8 @@ gtk_file_filter_to_gvariant (GtkFileFilter *filter) { case FILTER_RULE_PATTERN: g_variant_builder_add (&builder, "(us)", 0, rule->u.pattern); + if (name == NULL) + name = g_strdup (rule->u.pattern); break; case FILTER_RULE_SUFFIX: @@ -1008,13 +1019,24 @@ gtk_file_filter_to_gvariant (GtkFileFilter *filter) char *pattern = _gtk_make_ci_glob_pattern (rule->u.pattern); g_variant_builder_add (&builder, "(us)", 0, pattern); g_free (pattern); + if (name == NULL) + name = g_strdup (rule->u.pattern); } break; case FILTER_RULE_MIME_TYPE: case FILTER_RULE_PIXBUF_FORMATS: for (i = 0; rule->u.content_types[i]; i++) - g_variant_builder_add (&builder, "(us)", 1, rule->u.content_types[i]); + { + g_variant_builder_add (&builder, "(us)", 1, rule->u.content_types[i]); + if (name == NULL) + { + if (rule->type == FILTER_RULE_PIXBUF_FORMATS) + name = g_strdup (_("Image")); + else + name = g_content_type_get_description (rule->u.content_types[i]); + } + } break; default: @@ -1022,7 +1044,14 @@ gtk_file_filter_to_gvariant (GtkFileFilter *filter) } } - return g_variant_new ("(s@a(us))", filter->name, g_variant_builder_end (&builder)); + if (name == NULL) + name = g_strdup (_("Unspecified")); + + result = g_variant_new ("(s@a(us))", name, g_variant_builder_end (&builder)); + + g_free (name); + + return result; } /** diff --git a/testsuite/gtk/filefilter.c b/testsuite/gtk/filefilter.c index b9f6c6f862..524efa5fce 100644 --- a/testsuite/gtk/filefilter.c +++ b/testsuite/gtk/filefilter.c @@ -192,6 +192,104 @@ test_builder (void) g_object_unref (builder); } +/* Create a filter but don't set a name, then + * turn it into a GVariant */ +static void +test_variant_no_name (void) +{ + GtkFileFilter *filter; + GVariant *variant; + + filter = gtk_file_filter_new (); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_mime_type (filter, "image/png"); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_mime_type (filter, "image/png"); + gtk_file_filter_add_mime_type (filter, "image/jpeg"); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_suffix (filter, "txt"); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_suffix (filter, "txt"); + gtk_file_filter_add_suffix (filter, "html"); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_pixbuf_formats (filter); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_pixbuf_formats (filter); + gtk_file_filter_add_pixbuf_formats (filter); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_pattern (filter, "*.*"); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_pattern (filter, "*.*"); + gtk_file_filter_add_pattern (filter, ".*.*"); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_mime_type (filter, "image/png"); + gtk_file_filter_add_pixbuf_formats (filter); + gtk_file_filter_add_suffix (filter, "txt"); + gtk_file_filter_add_pattern (filter, "*.*"); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_pixbuf_formats (filter); + gtk_file_filter_add_suffix (filter, "txt"); + gtk_file_filter_add_pattern (filter, "*.*"); + gtk_file_filter_add_mime_type (filter, "image/png"); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); + + filter = gtk_file_filter_new (); + gtk_file_filter_add_suffix (filter, "txt"); + gtk_file_filter_add_pattern (filter, "*.*"); + gtk_file_filter_add_mime_type (filter, "image/png"); + gtk_file_filter_add_pixbuf_formats (filter); + variant = gtk_file_filter_to_gvariant (filter); + g_variant_unref (variant); + g_object_unref (filter); +} + int main (int argc, char *argv[]) { @@ -204,6 +302,7 @@ main (int argc, char *argv[]) g_test_add_func ("/filefilter/mimetype", test_mime_type); g_test_add_func ("/filefilter/buildable", test_buildable); g_test_add_func ("/filefilter/builder", test_builder); + g_test_add_func ("/filefilter/variant-no-name", test_variant_no_name); return g_test_run (); } |