[gtk+/wip/alexl/file-chooser-portal] Add methods to serialize GtkFileFilter to a GVariant
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/wip/alexl/file-chooser-portal] Add methods to serialize GtkFileFilter to a GVariant
- Date: Sat, 11 Jun 2016 04:52:52 +0000 (UTC)
commit ccadf57ee323a6146058082dc3386b4b79aba2ae
Author: Matthias Clasen <mclasen redhat com>
Date: Sat Jun 11 00:45:27 2016 -0400
Add methods to serialize GtkFileFilter to a GVariant
This will be used to send filters of D-Bus in future commits.
gtk/gtkfilefilter.c | 79 ++++++++++++++++++++++++++++++++++++++++++++
gtk/gtkfilefilterprivate.h | 2 +
2 files changed, 81 insertions(+), 0 deletions(-)
---
diff --git a/gtk/gtkfilefilter.c b/gtk/gtkfilefilter.c
index c4751e2..43f766f 100644
--- a/gtk/gtkfilefilter.c
+++ b/gtk/gtkfilefilter.c
@@ -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;
+}
diff --git a/gtk/gtkfilefilterprivate.h b/gtk/gtkfilefilterprivate.h
index 0a3d4a4..80c2e77 100644
--- a/gtk/gtkfilefilterprivate.h
+++ b/gtk/gtkfilefilterprivate.h
@@ -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__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]