filefilter: Add properties for the criteria

Add construct-only properties for patterns,
suffixes and mime-types. This provides an
alternative way to specify these in ui files.

Related: #5350
This commit is contained in:
Matthias Clasen 2022-11-21 22:44:03 -05:00
parent 055f3e5d97
commit d34ac99916
2 changed files with 102 additions and 0 deletions

View File

@ -120,6 +120,9 @@ struct _FilterRule
enum {
PROP_0,
PROP_NAME,
PROP_PATTERNS,
PROP_MIME_TYPES,
PROP_SUFFIXES,
NUM_PROPERTIES
};
@ -149,12 +152,35 @@ gtk_file_filter_set_property (GObject *object,
GParamSpec *pspec)
{
GtkFileFilter *filter = GTK_FILE_FILTER (object);
const char * const *strv;
switch (prop_id)
{
case PROP_NAME:
gtk_file_filter_set_name (filter, g_value_get_string (value));
break;
case PROP_PATTERNS:
strv = (const char * const *) g_value_get_boxed (value);
if (strv)
for (int i = 0; strv[i]; i++)
gtk_file_filter_add_pattern (filter, strv[i]);
break;
case PROP_MIME_TYPES:
strv = (const char * const *) g_value_get_boxed (value);
if (strv)
for (int i = 0; strv[i]; i++)
gtk_file_filter_add_mime_type (filter, strv[i]);
break;
case PROP_SUFFIXES:
strv = (const char * const *) g_value_get_boxed (value);
if (strv)
for (int i = 0; strv[i]; i++)
gtk_file_filter_add_suffix (filter, strv[i]);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -174,6 +200,7 @@ gtk_file_filter_get_property (GObject *object,
case PROP_NAME:
g_value_set_string (value, filter->name);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -238,6 +265,42 @@ gtk_file_filter_class_init (GtkFileFilterClass *class)
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFileFilter:patterns:
*
* The patterns that this filter matches.
*
* Since: 4.10
*/
props[PROP_PATTERNS] =
g_param_spec_boxed ("patterns", NULL, NULL,
G_TYPE_STRV,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
/**
* GtkFileFilter:mime-types:
*
* The MIME types that this filter matches.
*
* Since: 4.10
*/
props[PROP_MIME_TYPES] =
g_param_spec_boxed ("mime-types", NULL, NULL,
G_TYPE_STRV,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
/**
* GtkFileFilter:suffixes:
*
* The suffixes that this filter matches.
*
* Since: 4.10
*/
props[PROP_SUFFIXES] =
g_param_spec_boxed ("suffixes", NULL, NULL,
G_TYPE_STRV,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, props);
}

View File

@ -154,6 +154,44 @@ test_buildable (void)
g_object_unref (builder);
}
static void
test_builder (void)
{
GtkBuilder *builder;
const char *buffer =
"<interface>\n"
" <object class=\"GtkFileFilter\" id=\"filter\">\n"
" <property name=\"name\">Audio Files</property>\n"
" <property name=\"mime-types\">audio/*</property>\n"
" <property name=\"suffixes\">blah</property>\n"
" <property name=\"patterns\">*.x\n*.y</property>\n"
" </object>\n"
"</interface>";
GtkFileFilter *filter;
GVariant *v1, *v2;
char *s1, *s2;
builder = gtk_builder_new ();
gtk_builder_add_from_string (builder, buffer, strlen (buffer), NULL);
filter = GTK_FILE_FILTER (gtk_builder_get_object (builder, "filter"));
v1 = gtk_file_filter_to_gvariant (filter);
v2 = g_variant_parse (NULL, "('Audio Files', [(0, '*.x'), (0, '*.y'), (1, 'audio/*'), (0, '*.[bB][lL][aA][hH]')])", NULL, NULL, NULL);
s1 = g_variant_print (v1, FALSE);
s2 = g_variant_print (v2, FALSE);
g_assert_cmpstr (s1, ==, s2);
g_free (s1);
g_free (s2);
g_variant_unref (v1);
g_variant_unref (v2);
g_object_unref (builder);
}
int
main (int argc, char *argv[])
{
@ -165,6 +203,7 @@ main (int argc, char *argv[])
g_test_add_func ("/filefilter/suffix", test_suffix);
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);
return g_test_run ();
}