diff options
author | Wim Taymans <wtaymans@redhat.com> | 2015-09-01 12:48:04 +0200 |
---|---|---|
committer | Wim Taymans <wtaymans@redhat.com> | 2015-09-01 12:48:04 +0200 |
commit | a6dfb8831f8d9bf99c5b5969086f5ebd9d44ba2d (patch) | |
tree | e87f0cbc7aff5b320a2764ce91b9d8d176dfa87b | |
parent | fd276b482071a84b89bcf4def0f1cd45586b7c90 (diff) |
properties: add init_builder method
Add method to init a GVariant builder from properties.
-rw-r--r-- | src/client/properties.c | 59 | ||||
-rw-r--r-- | src/client/properties.h | 2 | ||||
-rw-r--r-- | src/server/source-output.c | 4 |
3 files changed, 61 insertions, 4 deletions
diff --git a/src/client/properties.c b/src/client/properties.c index f02c4413..40c3a843 100644 --- a/src/client/properties.c +++ b/src/client/properties.c @@ -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) { diff --git a/src/client/properties.h b/src/client/properties.h index 42080fd7..6297c671 100644 --- a/src/client/properties.h +++ b/src/client/properties.h @@ -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); diff --git a/src/server/source-output.c b/src/server/source-output.c index f9657d5f..a4d5c1c0 100644 --- a/src/server/source-output.c +++ b/src/server/source-output.c @@ -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; |