[gtk+] gdk: Add GdkContentFormatsBuilder



commit 1a482ad2760b2f078605f807a13aed2877e95ef0
Author: Benjamin Otte <otte redhat com>
Date:   Mon Nov 20 02:47:45 2017 +0100

    gdk: Add GdkContentFormatsBuilder
    
    Yes, the name is too long.
    No, I couldn't think of a shorter one.

 docs/reference/gdk/gdk4-sections.txt |    9 +++
 gdk/gdkcontentformats.c              |  108 ++++++++++++++++++++++++++++++++++
 gdk/gdkcontentformats.h              |   13 ++++
 3 files changed, 130 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gdk/gdk4-sections.txt b/docs/reference/gdk/gdk4-sections.txt
index 2752f03..1746f19 100644
--- a/docs/reference/gdk/gdk4-sections.txt
+++ b/docs/reference/gdk/gdk4-sections.txt
@@ -370,6 +370,7 @@ gdk_fullscreen_mode_get_type
 <SECTION>
 <FILE>gdkcontentformats</FILE>
 <TITLE>Content Formats</TITLE>
+GdkContentFormats
 gdk_content_formats_new
 gdk_content_formats_ref
 gdk_content_formats_unref
@@ -380,6 +381,14 @@ gdk_content_formats_union
 gdk_content_formats_intersects
 gdk_content_formats_remove
 gdk_content_formats_contains
+
+<SUBSECTION>
+GdkContentFormatsBuilder
+gdk_content_formats_builder_new
+gdk_content_formats_builder_free
+gdk_content_formats_builder_add_formats
+gdk_content_formats_builder_add_mime_type
+
 <SUBSECTION Private>
 gdk_content_formats_get_type
 </SECTION>
diff --git a/gdk/gdkcontentformats.c b/gdk/gdkcontentformats.c
index c3cd9cd..bb14964 100644
--- a/gdk/gdkcontentformats.c
+++ b/gdk/gdkcontentformats.c
@@ -45,6 +45,10 @@
  * For debugging purposes, the function gdk_content_formats_to_string() exists.
  * It will print a comma-seperated formats of formats from most important to least
  * important.
+ *
+ * #GdkContentFormats is an immutable struct. After creation, you cannot change
+ * the types it represents. Instead, new #GdkContentFormats have to be created.
+ * The #GdkContentFormatsBuilder structure is meant to help in this endeavor.
  */
 
 /**
@@ -310,3 +314,107 @@ gdk_content_formats_get_atoms (GdkContentFormats *formats,
   return atoms;
 }
 
+/**
+ * GdkContentFormatsBuilder:
+ *
+ * A #GdkContentFormatsBuilder struct is an opaque struct. It is meant to
+ * not be kept around and only be used to create new #GdkContentFormats
+ * objects.
+ */
+
+struct _GdkContentFormatsBuilder
+{
+  GSList *mime_types;
+  gsize n_mime_types;
+};
+
+/**
+ * gdk_content_formats_builder_new:
+ *
+ * Create a new #GdkContentFormatsBuilder object. The resulting builder
+ * would create an empty #GdkContentFormats. Use addition functions to add
+ * types to it.
+ *
+ * Returns: a new #GdkContentFormatsBuilder
+ **/
+GdkContentFormatsBuilder *
+gdk_content_formats_builder_new (void)
+{
+  return g_slice_new0 (GdkContentFormatsBuilder);
+}
+
+/**
+ * gdk_content_formats_builder_free:
+ * @builder: a #GdkContentFormatsBuilder
+ *
+ * Frees @builder and creates a new #GdkContentFormats from it.
+ *
+ * Returns: a new #GdkContentFormats with all the formats added to @builder
+ **/
+GdkContentFormats *
+gdk_content_formats_builder_free (GdkContentFormatsBuilder *builder)
+{
+  GdkContentFormats *result;
+  const char **mime_types;
+  GSList *l;
+  gsize i;
+
+  g_return_val_if_fail (builder != NULL, NULL);
+
+  mime_types = g_new (const char *, builder->n_mime_types + 1);
+  i = builder->n_mime_types;
+  mime_types[i--] = NULL;
+  /* add backwards because most important type is last in the list */
+  for (l = builder->mime_types; l; l = l->next)
+    mime_types[i--] = l->data;
+
+  result = gdk_content_formats_new (mime_types, builder->n_mime_types);
+  g_free (mime_types);
+
+  return result;
+}
+
+/**
+ * gdk_content_formats_builder_add_formats:
+ * @builder: a #GdkContentFormatsBuilder
+ * @formats: the formats to add
+ *
+ * Appends all formats from @formats to @builder, skipping those that
+ * already exist.
+ **/
+void
+gdk_content_formats_builder_add_formats (GdkContentFormatsBuilder *builder,
+                                         GdkContentFormats        *formats)
+{
+  GList *l;
+
+  g_return_if_fail (builder != NULL);
+  g_return_if_fail (formats != NULL);
+
+  for (l = formats->formats; l; l = l->next)
+    gdk_content_formats_builder_add_mime_type (builder, l->data);
+}
+
+/**
+ * gdk_content_formats_builder_add_formats:
+ * @builder: a #GdkContentFormatsBuilder
+ * @mime_type: a mime type
+ *
+ * Appends @mime_type to @builder if it has not already been added.
+ **/
+void
+gdk_content_formats_builder_add_mime_type (GdkContentFormatsBuilder *builder,
+                                           const char               *mime_type)
+{
+  g_return_if_fail (builder != NULL);
+  g_return_if_fail (mime_type != NULL);
+
+  mime_type = g_intern_string (mime_type);
+
+  if (g_slist_find (builder->mime_types, mime_type))
+    return;
+
+  builder->mime_types = g_slist_prepend (builder->mime_types, (gpointer) mime_type);
+  builder->n_mime_types++;
+}
+
diff --git a/gdk/gdkcontentformats.h b/gdk/gdkcontentformats.h
index f73c5b4..5b27a47 100644
--- a/gdk/gdkcontentformats.h
+++ b/gdk/gdkcontentformats.h
@@ -59,6 +59,19 @@ GDK_AVAILABLE_IN_3_94
 gboolean                gdk_content_formats_contains            (const GdkContentFormats        *formats,
                                                                  const char                     *mime_type);
 
+typedef struct _GdkContentFormatsBuilder GdkContentFormatsBuilder;
+
+GDK_AVAILABLE_IN_3_94
+GdkContentFormatsBuilder*gdk_content_formats_builder_new        (void);
+GDK_AVAILABLE_IN_3_94
+GdkContentFormats *     gdk_content_formats_builder_free        (GdkContentFormatsBuilder       *builder) 
G_GNUC_WARN_UNUSED_RESULT;
+GDK_AVAILABLE_IN_3_94
+void                    gdk_content_formats_builder_add_formats (GdkContentFormatsBuilder       *builder,
+                                                                 GdkContentFormats              *formats);
+GDK_AVAILABLE_IN_3_94
+void                    gdk_content_formats_builder_add_mime_type(GdkContentFormatsBuilder      *builder,
+                                                                 const char                     *mime_type);
+
 G_END_DECLS
 
 #endif /* __GTK_CONTENT_FORMATS_H__ */


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