gdk: Add GdkContentFormatsBuilder

Yes, the name is too long.
No, I couldn't think of a shorter one.
This commit is contained in:
Benjamin Otte 2017-11-20 02:47:45 +01:00
parent d6a209816b
commit 1a482ad276
3 changed files with 130 additions and 0 deletions

View File

@ -370,6 +370,7 @@ gdk_fullscreen_mode_get_type
<SECTION> <SECTION>
<FILE>gdkcontentformats</FILE> <FILE>gdkcontentformats</FILE>
<TITLE>Content Formats</TITLE> <TITLE>Content Formats</TITLE>
GdkContentFormats
gdk_content_formats_new gdk_content_formats_new
gdk_content_formats_ref gdk_content_formats_ref
gdk_content_formats_unref gdk_content_formats_unref
@ -380,6 +381,14 @@ gdk_content_formats_union
gdk_content_formats_intersects gdk_content_formats_intersects
gdk_content_formats_remove gdk_content_formats_remove
gdk_content_formats_contains 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> <SUBSECTION Private>
gdk_content_formats_get_type gdk_content_formats_get_type
</SECTION> </SECTION>

View File

@ -45,6 +45,10 @@
* For debugging purposes, the function gdk_content_formats_to_string() exists. * 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 * It will print a comma-seperated formats of formats from most important to least
* important. * 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; 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++;
}

View File

@ -59,6 +59,19 @@ GDK_AVAILABLE_IN_3_94
gboolean gdk_content_formats_contains (const GdkContentFormats *formats, gboolean gdk_content_formats_contains (const GdkContentFormats *formats,
const char *mime_type); 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 G_END_DECLS
#endif /* __GTK_CONTENT_FORMATS_H__ */ #endif /* __GTK_CONTENT_FORMATS_H__ */