Commit ccadf57e authored by Matthias Clasen's avatar Matthias Clasen

Add methods to serialize GtkFileFilter to a GVariant

This will be used to send filters of D-Bus in future commits.
parent 89aa77b7
......@@ -718,3 +718,82 @@ gtk_file_filter_filter (GtkFileFilter *filter,
return FALSE;
}
GVariant *
gtk_file_filter_to_gvariant (GtkFileFilter *filter)
{
GVariantBuilder builder;
GSList *l;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(us)"));
for (l = filter->rules; l; l = l->next)
{
FilterRule *rule = l->data;
switch (rule->type)
{
case FILTER_RULE_PATTERN:
g_variant_builder_add (&builder, "(us)", 0, rule->u.mime_type);
break;
case FILTER_RULE_MIME_TYPE:
g_variant_builder_add (&builder, "(us)", 1, rule->u.mime_type);
break;
case FILTER_RULE_PIXBUF_FORMATS:
{
GSList *f;
for (f = rule->u.pixbuf_formats; f; f = f->next)
{
GdkPixbufFormat *fmt = f->data;
gchar **mime_types;
int i;
mime_types = gdk_pixbuf_format_get_mime_types (fmt);
for (i = 0; mime_types[i]; i++)
g_variant_builder_add (&builder, "(us)", 1, mime_types[i]);
g_strfreev (mime_types);
}
}
break;
case FILTER_RULE_CUSTOM:
default:
break;
}
}
return g_variant_new ("(s@a(us))", filter->name, g_variant_builder_end (&builder));
}
GtkFileFilter *
gtk_file_filter_from_gvariant (GVariant *variant)
{
GtkFileFilter *filter;
GVariantIter *iter;
const char *name;
int type;
char *tmp;
filter = gtk_file_filter_new ();
g_variant_get (variant, "(&sa(us))", &name, &iter);
gtk_file_filter_set_name (filter, name);
while (g_variant_iter_next (iter, "(u&s)", &type, &tmp))
{
switch (type)
{
case 0:
gtk_file_filter_add_pattern (filter, tmp);
break;
case 1:
gtk_file_filter_add_mime_type (filter, tmp);
break;
default:
break;
}
}
g_variant_iter_free (iter);
return filter;
}
......@@ -25,6 +25,8 @@ G_BEGIN_DECLS
char ** _gtk_file_filter_get_as_patterns (GtkFileFilter *filter);
GVariant *gtk_file_filter_to_gvariant (GtkFileFilter *filter);
GtkFileFilter *gtk_file_filter_from_gvariant (GVariant *variant);
G_END_DECLS
#endif /* __GTK_FILE_FILTER_PRIVATE_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment