properties: add init_builder method

Add method to init a GVariant builder from properties.
This commit is contained in:
Wim Taymans 2015-09-01 12:48:04 +02:00
parent fd276b4820
commit a6dfb8831f
3 changed files with 61 additions and 4 deletions

View file

@ -183,6 +183,20 @@ pinos_properties_remove (PinosProperties *properties,
g_hash_table_remove (properties->hashtable, key);
}
/**
* pinos_properties_iterate:
* @properties: a #PinosProperties
* @state: state
*
* Iterate over @properties, returning each key in turn. @state should point
* to a pointer holding %NULL to get the first element and will be updated
* after each iteration. When %NULL is returned, all elements have been
* iterated.
*
* Aborting the iteration before %NULL is returned might cause memory leaks.
*
* Returns: The next key or %NULL when there are no more keys to iterate.
*/
const gchar *
pinos_properties_iterate (PinosProperties *properties,
gpointer *state)
@ -220,18 +234,57 @@ add_to_variant (const gchar *key, const gchar *value, GVariantBuilder *b)
g_variant_builder_add (b, "{sv}", key, g_variant_new_string (value));
}
/**
* pinos_properties_init_builder:
* @properties: a #PinosProperties
* @builder: a #GVariantBuilder
*
* Initialize the @builder of type a{sv} and add @properties to it.
*
* Returns: %TRUE if @builder could be initialized.
*/
gboolean
pinos_properties_init_builder (PinosProperties *properties,
GVariantBuilder *builder)
{
g_return_val_if_fail (properties != NULL, FALSE);
g_return_val_if_fail (builder != NULL, FALSE);
g_variant_builder_init (builder, G_VARIANT_TYPE ("a{sv}"));
g_hash_table_foreach (properties->hashtable, (GHFunc) add_to_variant, builder);
return TRUE;
}
/**
* pinos_properties_to_variant:
* @properties: a #PinosProperties
*
* Convert @properties to a #GVariant of type a{sv}
*
* Returns: a new #GVariant of @properties. use g_variant_unref() after
* use.
*/
GVariant *
pinos_properties_to_variant (PinosProperties *properties)
{
GVariantBuilder builder;
g_return_val_if_fail (properties != NULL, NULL);
if (!pinos_properties_init_builder (properties, &builder))
return NULL;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
g_hash_table_foreach (properties->hashtable, (GHFunc) add_to_variant, &builder);
return g_variant_builder_end (&builder);
}
/**
* pinos_properties_from_variant:
* @variant: a #GVariant
*
* Convert @variant to a #PinosProperties
*
* Returns: a new #PinosProperties of @variant. use pinos_properties_free()
* after use.
*/
PinosProperties *
pinos_properties_from_variant (GVariant *variant)
{

View file

@ -48,6 +48,8 @@ void pinos_properties_remove (PinosProperties *properties,
const gchar * pinos_properties_iterate (PinosProperties *properties,
gpointer *state);
gboolean pinos_properties_init_builder (PinosProperties *properties,
GVariantBuilder *builder);
GVariant * pinos_properties_to_variant (PinosProperties *properties);
PinosProperties * pinos_properties_from_variant (GVariant *variant);

View file

@ -200,12 +200,14 @@ handle_start (PinosSourceOutput1 *interface,
priv->state = PINOS_SOURCE_OUTPUT_STATE_STARTING;
priv->requested_format = g_bytes_new (arg_requested_format, strlen (arg_requested_format) + 1);
priv->requested_format = g_bytes_new (arg_requested_format,
strlen (arg_requested_format) + 1);
socketpair (AF_UNIX, SOCK_STREAM, 0, fd);
priv->socket = g_socket_new_from_fd (fd[0], NULL);
g_object_notify (G_OBJECT (output), "socket");
/* the notify of the socket above should configure the format */
if (priv->format == NULL)
goto no_format;