Merge branch 'wip/otte/filefilter-name' into 'main'

filefilter: If not given a name, deduce one

See merge request GNOME/gtk!5630
This commit is contained in:
Benjamin Otte 2023-03-10 17:11:25 +00:00
commit 6029c006b3
2 changed files with 134 additions and 6 deletions

View File

@ -68,15 +68,17 @@
*/ */
#include "config.h" #include "config.h"
#include <string.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "gtkfilefilterprivate.h" #include "gtkfilefilterprivate.h"
#include "gtkbuildable.h" #include "gtkbuildable.h"
#include "gtkbuilderprivate.h" #include "gtkbuilderprivate.h"
#include "gtkprivate.h"
#include "gtkfilter.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 _GtkFileFilterClass GtkFileFilterClass;
typedef struct _FilterRule FilterRule; typedef struct _FilterRule FilterRule;
@ -987,6 +989,13 @@ gtk_file_filter_to_gvariant (GtkFileFilter *filter)
{ {
GVariantBuilder builder; GVariantBuilder builder;
GSList *l; 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)")); g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(us)"));
for (l = filter->rules; l; l = l->next) for (l = filter->rules; l; l = l->next)
@ -998,6 +1007,8 @@ gtk_file_filter_to_gvariant (GtkFileFilter *filter)
{ {
case FILTER_RULE_PATTERN: case FILTER_RULE_PATTERN:
g_variant_builder_add (&builder, "(us)", 0, rule->u.pattern); g_variant_builder_add (&builder, "(us)", 0, rule->u.pattern);
if (name == NULL)
name = g_strdup (rule->u.pattern);
break; break;
case FILTER_RULE_SUFFIX: 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); char *pattern = _gtk_make_ci_glob_pattern (rule->u.pattern);
g_variant_builder_add (&builder, "(us)", 0, pattern); g_variant_builder_add (&builder, "(us)", 0, pattern);
g_free (pattern); g_free (pattern);
if (name == NULL)
name = g_strdup (rule->u.pattern);
} }
break; break;
case FILTER_RULE_MIME_TYPE: case FILTER_RULE_MIME_TYPE:
case FILTER_RULE_PIXBUF_FORMATS: case FILTER_RULE_PIXBUF_FORMATS:
for (i = 0; rule->u.content_types[i]; i++) 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; break;
default: 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;
} }
/** /**

View File

@ -192,6 +192,104 @@ test_builder (void)
g_object_unref (builder); 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 int
main (int argc, char *argv[]) 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/mimetype", test_mime_type);
g_test_add_func ("/filefilter/buildable", test_buildable); g_test_add_func ("/filefilter/buildable", test_buildable);
g_test_add_func ("/filefilter/builder", test_builder); g_test_add_func ("/filefilter/builder", test_builder);
g_test_add_func ("/filefilter/variant-no-name", test_variant_no_name);
return g_test_run (); return g_test_run ();
} }