[gtk+/portal: 5/18] Add api to serialize GtkFileFilter to a variant



commit e9216bbba369b19b9e659acecc7554a2c950d0e3
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jun 11 00:45:27 2016 -0400

    Add api to serialize GtkFileFilter to a variant
    
    This will be used to send filters over D-Bus in future commits.

 docs/reference/gtk/gtk3-sections.txt |    5 ++
 gtk/gtkfilefilter.c                  |  100 ++++++++++++++++++++++++++++++++++
 gtk/gtkfilefilter.h                  |    5 ++
 3 files changed, 110 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt
index 0c072c6..8e21a13 100644
--- a/docs/reference/gtk/gtk3-sections.txt
+++ b/docs/reference/gtk/gtk3-sections.txt
@@ -1528,6 +1528,11 @@ gtk_file_filter_add_pixbuf_formats
 gtk_file_filter_add_custom
 gtk_file_filter_get_needed
 gtk_file_filter_filter
+
+<SUBSECTION Serialization>
+gtk_file_filter_new_from_gvariant
+gtk_file_filter_to_gvariant
+
 <SUBSECTION Standard>
 GTK_FILE_FILTER
 GTK_IS_FILE_FILTER
diff --git a/gtk/gtkfilefilter.c b/gtk/gtkfilefilter.c
index c4751e2..e6d05fa 100644
--- a/gtk/gtkfilefilter.c
+++ b/gtk/gtkfilefilter.c
@@ -718,3 +718,103 @@ gtk_file_filter_filter (GtkFileFilter           *filter,
 
   return FALSE;
 }
+
+/**
+ * gtk_file_filter_to_gvariant:
+ * @settings: a #GtkFileFilter
+ *
+ * Serialize a file filter to an a{sv} variant.
+ *
+ * Returns: (transfer none): a new, floating, #GVariant
+ *
+ * Since: 3.22
+ */
+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));
+}
+
+/**
+ * gtk_file_filter_new_from_gvariant:
+ * @variant: an a{sv} #GVariant
+ *
+ * Deserialize a file filter from an a{sv} variant in
+ * the format produced by gtk_file_filter_to_gvariant().
+ *
+ * Returns: (transfer full): a new #GtkFileFilter object
+ *
+ * Since: 3.22
+ */
+GtkFileFilter *
+gtk_file_filter_new_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/gtkfilefilter.h b/gtk/gtkfilefilter.h
index 02e8c08..8051bef 100644
--- a/gtk/gtkfilefilter.h
+++ b/gtk/gtkfilefilter.h
@@ -122,6 +122,11 @@ GDK_AVAILABLE_IN_ALL
 gboolean           gtk_file_filter_filter     (GtkFileFilter           *filter,
                                               const GtkFileFilterInfo *filter_info);
 
+GDK_AVAILABLE_IN_3_22
+GVariant      *gtk_file_filter_to_gvariant       (GtkFileFilter *filter);
+GDK_AVAILABLE_IN_3_22
+GtkFileFilter *gtk_file_filter_new_from_gvariant (GVariant      *variant);
+
 G_END_DECLS
 
 #endif /* __GTK_FILE_FILTER_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]