From 9ea25fe0152ac14453953d417707e70ed922152c Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 26 Sep 2013 14:28:40 +0100 Subject: [PATCH 3/7] Make more use of tp_value_array_unpack, tp_value_array_build As well as being less code, they're not flagged as deprecated. This requires a bit of extra copying, because there's no tp_value_array_unpack_dup() or tp_value_array_build_take(), but it seems worth it. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=69849 --- examples/client/extended-client.c | 13 +++--- examples/cm/contactlist/conn.c | 12 ++---- examples/cm/extended/conn.c | 42 +++++++------------ telepathy-glib/account.c | 52 +++++++++++++---------- telepathy-glib/base-connection.c | 36 ++++++---------- telepathy-glib/capabilities.c | 15 +++++-- telepathy-glib/presence-mixin.c | 87 +++++++++++---------------------------- telepathy-glib/room-info.c | 22 ++++++---- tests/lib/contacts-conn.c | 14 ++----- 9 files changed, 123 insertions(+), 170 deletions(-) diff --git a/examples/client/extended-client.c b/examples/client/extended-client.c index 55e3086..1172231 100644 --- a/examples/client/extended-client.c +++ b/examples/client/extended-client.c @@ -71,14 +71,15 @@ got_hats_cb (TpConnection *conn, for (i = 0; i < hats->len; i++) { GValueArray *vals = g_ptr_array_index (hats, i); + guint handle; + const gchar *color; + guint style; + GHashTable *details; + tp_value_array_unpack (vals, 4, + &handle, &color, &style, &details); g_message ("Contact #%u has hat style %u, color \"%s\", with %u " - "properties", - g_value_get_uint (g_value_array_get_nth (vals, 0)), - g_value_get_uint (g_value_array_get_nth (vals, 2)), - g_value_get_string (g_value_array_get_nth (vals, 1)), - g_hash_table_size (g_value_get_boxed (g_value_array_get_nth (vals, - 3)))); + "properties", handle, style, color, g_hash_table_size (details)); } tp_cli_connection_call_disconnect (conn, -1, disconnect_cb, diff --git a/examples/cm/contactlist/conn.c b/examples/cm/contactlist/conn.c index a28ae85..bc7876c 100644 --- a/examples/cm/contactlist/conn.c +++ b/examples/cm/contactlist/conn.c @@ -163,14 +163,10 @@ alias_updated_cb (ExampleContactList *contact_list, GPtrArray *aliases; GValueArray *pair; - pair = g_value_array_new (2); - g_value_array_append (pair, NULL); - g_value_array_append (pair, NULL); - g_value_init (pair->values + 0, G_TYPE_UINT); - g_value_init (pair->values + 1, G_TYPE_STRING); - g_value_set_uint (pair->values + 0, contact); - g_value_set_string (pair->values + 1, - example_contact_list_get_alias (contact_list, contact)); + pair = tp_value_array_build (2, + G_TYPE_UINT, contact, + G_TYPE_STRING, example_contact_list_get_alias (contact_list, contact), + G_TYPE_INVALID); aliases = g_ptr_array_sized_new (1); g_ptr_array_add (aliases, pair); diff --git a/examples/cm/extended/conn.c b/examples/cm/extended/conn.c index 873fcf2..c92076f 100644 --- a/examples/cm/extended/conn.c +++ b/examples/cm/extended/conn.c @@ -274,40 +274,30 @@ my_get_hats (ExampleSvcConnectionInterfaceHats *iface, for (i = 0; i < contacts->len; i++) { TpHandle handle = g_array_index (contacts, guint, i); - GValueArray *vals = g_value_array_new (4); - - g_value_array_append (vals, NULL); - g_value_init (g_value_array_get_nth (vals, 0), G_TYPE_UINT); - g_value_set_uint (g_value_array_get_nth (vals, 0), handle); - - g_value_array_append (vals, NULL); - g_value_init (g_value_array_get_nth (vals, 1), G_TYPE_STRING); - - g_value_array_append (vals, NULL); - g_value_init (g_value_array_get_nth (vals, 2), G_TYPE_UINT); - - g_value_array_append (vals, NULL); - g_value_init (g_value_array_get_nth (vals, 3), - TP_HASH_TYPE_STRING_VARIANT_MAP); + GValueArray *vals; /* for the sake of a simple example, let's assume nobody except me * has any hats */ if (handle == tp_base_connection_get_self_handle (base)) { - g_value_set_string (g_value_array_get_nth (vals, 1), - self->priv->hat_color); - g_value_set_uint (g_value_array_get_nth (vals, 2), - self->priv->hat_style); - g_value_set_boxed (g_value_array_get_nth (vals, 3), - self->priv->hat_properties); + vals = tp_value_array_build (4, + G_TYPE_UINT, handle, + G_TYPE_STRING, self->priv->hat_color, + G_TYPE_UINT, self->priv->hat_style, + TP_HASH_TYPE_STRING_VARIANT_MAP, self->priv->hat_properties, + G_TYPE_INVALID); } else { - g_value_set_static_string (g_value_array_get_nth (vals, 1), ""); - g_value_set_uint (g_value_array_get_nth (vals, 2), - EXAMPLE_HAT_STYLE_NONE); - g_value_take_boxed (g_value_array_get_nth (vals, 3), - g_hash_table_new (NULL, NULL)); + GHashTable *empty = g_hash_table_new (g_str_hash, g_str_equal); + + vals = tp_value_array_build (4, + G_TYPE_UINT, handle, + G_TYPE_STRING, "", + G_TYPE_UINT, EXAMPLE_HAT_STYLE_NONE, + TP_HASH_TYPE_STRING_VARIANT_MAP, empty, + G_TYPE_INVALID); + g_hash_table_unref (empty); } g_ptr_array_add (ret, vals); diff --git a/telepathy-glib/account.c b/telepathy-glib/account.c index 24dbd00..0ad6d83 100644 --- a/telepathy-glib/account.c +++ b/telepathy-glib/account.c @@ -631,6 +631,8 @@ _tp_account_update (TpAccount *account, TpConnectionStatus old_s = priv->connection_status; gboolean status_changed = FALSE; gboolean presence_changed = FALSE; + const gchar *status; + const gchar *message; tp_proxy_add_interfaces (proxy, tp_asv_get_strv (properties, "Interfaces")); @@ -721,29 +723,30 @@ _tp_account_update (TpAccount *account, presence_changed = TRUE; arr = tp_asv_get_boxed (properties, "CurrentPresence", TP_STRUCT_TYPE_SIMPLE_PRESENCE); - priv->cur_presence = g_value_get_uint (g_value_array_get_nth (arr, 0)); + tp_value_array_unpack (arr, 3, + &priv->cur_presence, + &status, + &message); g_free (priv->cur_status); - priv->cur_status = g_value_dup_string (g_value_array_get_nth (arr, 1)); - + priv->cur_status = g_strdup (status); g_free (priv->cur_message); - priv->cur_message = g_value_dup_string (g_value_array_get_nth (arr, 2)); + priv->cur_message = g_strdup (message); } if (g_hash_table_lookup (properties, "RequestedPresence") != NULL) { arr = tp_asv_get_boxed (properties, "RequestedPresence", TP_STRUCT_TYPE_SIMPLE_PRESENCE); - priv->requested_presence = - g_value_get_uint (g_value_array_get_nth (arr, 0)); + tp_value_array_unpack (arr, 3, + &priv->requested_presence, + &status, + &message); g_free (priv->requested_status); - priv->requested_status = - g_value_dup_string (g_value_array_get_nth (arr, 1)); - + priv->requested_status = g_strdup (status); g_free (priv->requested_message); - priv->requested_message = - g_value_dup_string (g_value_array_get_nth (arr, 2)); + priv->requested_message = g_strdup (message); g_object_notify (G_OBJECT (account), "requested-presence-type"); g_object_notify (G_OBJECT (account), "requested-status"); @@ -754,16 +757,15 @@ _tp_account_update (TpAccount *account, { arr = tp_asv_get_boxed (properties, "AutomaticPresence", TP_STRUCT_TYPE_SIMPLE_PRESENCE); - priv->auto_presence = - g_value_get_uint (g_value_array_get_nth (arr, 0)); + tp_value_array_unpack (arr, 3, + &priv->auto_presence, + &status, + &message); g_free (priv->auto_status); - priv->auto_status = - g_value_dup_string (g_value_array_get_nth (arr, 1)); - + priv->auto_status = g_strdup (status); g_free (priv->auto_message); - priv->auto_message = - g_value_dup_string (g_value_array_get_nth (arr, 2)); + priv->auto_message = g_strdup (message); g_object_notify (G_OBJECT (account), "automatic-presence-type"); g_object_notify (G_OBJECT (account), "automatic-status"); @@ -3551,8 +3553,6 @@ _tp_account_got_avatar_cb (TpProxy *proxy, GObject *weak_object) { GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (user_data); - GValueArray *avatar; - GArray *res; if (error != NULL) { @@ -3567,8 +3567,18 @@ _tp_account_got_avatar_cb (TpProxy *proxy, } else { + GValueArray *avatar; + GArray *res; + const GArray *tmp; + const gchar *mime_type; + avatar = g_value_get_boxed (out_Value); - res = g_value_dup_boxed (g_value_array_get_nth (avatar, 0)); + tp_value_array_unpack (avatar, 2, + &tmp, + &mime_type); + + res = g_array_sized_new (FALSE, FALSE, 1, tmp->len); + g_array_append_vals (res, tmp->data, tmp->len); g_simple_async_result_set_op_res_gpointer (result, res, (GDestroyNotify) g_array_unref); } diff --git a/telepathy-glib/base-connection.c b/telepathy-glib/base-connection.c index 20a79b8..20c4440 100644 --- a/telepathy-glib/base-connection.c +++ b/telepathy-glib/base-connection.c @@ -728,7 +728,7 @@ exportable_channel_get_old_info (TpExportableChannel *channel, static GValueArray * get_channel_details (GObject *obj) { - GValueArray *structure = g_value_array_new (2); + GValueArray *structure; GHashTable *table; GValue *value; gchar *object_path; @@ -737,12 +737,6 @@ get_channel_details (GObject *obj) "object-path", &object_path, NULL); - g_value_array_append (structure, NULL); - value = g_value_array_get_nth (structure, 0); - g_value_init (value, DBUS_TYPE_G_OBJECT_PATH); - g_value_take_boxed (value, object_path); - object_path = NULL; - g_assert (TP_IS_EXPORTABLE_CHANNEL (obj) || TP_IS_CHANNEL_IFACE (obj)); if (TP_IS_EXPORTABLE_CHANNEL (obj)) @@ -769,10 +763,13 @@ get_channel_details (GObject *obj) g_hash_table_insert (table, TP_PROP_CHANNEL_CHANNEL_TYPE, value); } - g_value_array_append (structure, NULL); - value = g_value_array_get_nth (structure, 1); - g_value_init (value, TP_HASH_TYPE_QUALIFIED_PROPERTY_VALUE_MAP); - g_value_take_boxed (value, table); + structure = tp_value_array_build (2, + DBUS_TYPE_G_OBJECT_PATH, object_path, + TP_HASH_TYPE_QUALIFIED_PROPERTY_VALUE_MAP, table, + G_TYPE_INVALID); + + g_free (object_path); + g_hash_table_unref (table); return structure; } @@ -1458,20 +1455,11 @@ get_requestables_foreach (TpChannelManager *manager, gpointer user_data) { GPtrArray *details = user_data; - GValueArray *requestable = g_value_array_new (2); - GValue *value; - - g_value_array_append (requestable, NULL); - value = g_value_array_get_nth (requestable, 0); - g_value_init (value, TP_HASH_TYPE_CHANNEL_CLASS); - g_value_set_boxed (value, fixed_properties); - - g_value_array_append (requestable, NULL); - value = g_value_array_get_nth (requestable, 1); - g_value_init (value, G_TYPE_STRV); - g_value_set_boxed (value, allowed_properties); - g_ptr_array_add (details, requestable); + g_ptr_array_add (details, tp_value_array_build (2, + TP_HASH_TYPE_CHANNEL_CLASS, fixed_properties, + G_TYPE_STRV, allowed_properties, + G_TYPE_INVALID)); } diff --git a/telepathy-glib/capabilities.c b/telepathy-glib/capabilities.c index 4ddf368..d4557b4 100644 --- a/telepathy-glib/capabilities.c +++ b/telepathy-glib/capabilities.c @@ -322,11 +322,14 @@ supports_simple_channel (TpCapabilities *self, { GValueArray *arr = g_ptr_array_index (self->priv->classes, i); GHashTable *fixed; + const gchar * const *allowed; const gchar *chan_type; TpHandleType handle_type; gboolean valid; - fixed = g_value_get_boxed (g_value_array_get_nth (arr, 0)); + tp_value_array_unpack (arr, 2, + &fixed, + &allowed); if (g_hash_table_size (fixed) != 2) continue; @@ -439,8 +442,9 @@ tp_capabilities_supports_sms (TpCapabilities *self) gboolean valid; guint nb_fixed_props; - fixed = g_value_get_boxed (g_value_array_get_nth (arr, 0)); - allowed = g_value_get_boxed (g_value_array_get_nth (arr, 1)); + tp_value_array_unpack (arr, 2, + &fixed, + &allowed); handle_type = tp_asv_get_uint32 (fixed, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, &valid); @@ -809,12 +813,15 @@ tp_capabilities_supports_tubes_common (TpCapabilities *self, { GValueArray *arr = g_ptr_array_index (self->priv->classes, i); GHashTable *fixed; + const gchar * const *allowed; const gchar *chan_type; TpHandleType handle_type; gboolean valid; guint nb_fixed_props = 2; - fixed = g_value_get_boxed (g_value_array_get_nth (arr, 0)); + tp_value_array_unpack (arr, 2, + &fixed, + &allowed); chan_type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE); if (tp_strdiff (chan_type, expected_channel_type)) diff --git a/telepathy-glib/presence-mixin.c b/telepathy-glib/presence-mixin.c index f3b3b87..8bb5ac4 100644 --- a/telepathy-glib/presence-mixin.c +++ b/telepathy-glib/presence-mixin.c @@ -541,17 +541,11 @@ construct_presence_hash_foreach ( g_hash_table_insert (contact_status, (gpointer) supported_statuses[status->index].name, parameters); - vals = g_value_array_new (2); - - /* last-activity sucks and will probably be removed soon */ - g_value_array_append (vals, NULL); - g_value_init (g_value_array_get_nth (vals, 0), G_TYPE_UINT); - g_value_set_uint (g_value_array_get_nth (vals, 0), 0); - - g_value_array_append (vals, NULL); - g_value_init (g_value_array_get_nth (vals, 1), - TP_HASH_TYPE_MULTIPLE_STATUS_MAP); - g_value_take_boxed (g_value_array_get_nth (vals, 1), contact_status); + vals = tp_value_array_build (2, + G_TYPE_UINT, 0, + TP_HASH_TYPE_MULTIPLE_STATUS_MAP, contact_status, + G_TYPE_INVALID); + g_hash_table_unref (contact_status); g_hash_table_insert (presence_hash, GUINT_TO_POINTER (handle), vals); } @@ -850,34 +844,21 @@ tp_presence_mixin_get_statuses (TpSvcConnectionInterfacePresence *iface, for (i=0; mixin_cls->statuses[i].name != NULL; i++) { + GHashTable *args; + /* the spec says we include statuses here even if they're not available * to set on yourself */ if (!check_status_available (obj, mixin_cls, i, NULL, FALSE)) continue; - status = g_value_array_new (5); - - g_value_array_append (status, NULL); - g_value_init (g_value_array_get_nth (status, 0), G_TYPE_UINT); - g_value_set_uint (g_value_array_get_nth (status, 0), - mixin_cls->statuses[i].presence_type); - - g_value_array_append (status, NULL); - g_value_init (g_value_array_get_nth (status, 1), G_TYPE_BOOLEAN); - g_value_set_boolean (g_value_array_get_nth (status, 1), - mixin_cls->statuses[i].self); - - /* everything is exclusive */ - g_value_array_append (status, NULL); - g_value_init (g_value_array_get_nth (status, 2), G_TYPE_BOOLEAN); - g_value_set_boolean (g_value_array_get_nth (status, 2), - TRUE); - - g_value_array_append (status, NULL); - g_value_init (g_value_array_get_nth (status, 3), - DBUS_TYPE_G_STRING_STRING_HASHTABLE); - g_value_take_boxed (g_value_array_get_nth (status, 3), - get_statuses_arguments (mixin_cls->statuses[i].optional_arguments)); + args = get_statuses_arguments (mixin_cls->statuses[i].optional_arguments); + status = tp_value_array_build (4, + G_TYPE_UINT, (guint) mixin_cls->statuses[i].presence_type, + G_TYPE_BOOLEAN, mixin_cls->statuses[i].self, + G_TYPE_BOOLEAN, TRUE, /* exclusive */ + DBUS_TYPE_G_STRING_STRING_HASHTABLE, args, + G_TYPE_INVALID); + g_hash_table_unref (args); g_hash_table_insert (ret, (gchar *) mixin_cls->statuses[i].name, status); @@ -1284,21 +1265,11 @@ tp_presence_mixin_get_simple_presence_dbus_property (GObject *object, } } - status = g_value_array_new (3); - - g_value_array_append (status, NULL); - g_value_init (g_value_array_get_nth (status, 0), G_TYPE_UINT); - g_value_set_uint (g_value_array_get_nth (status, 0), - mixin_cls->statuses[i].presence_type); - - g_value_array_append (status, NULL); - g_value_init (g_value_array_get_nth (status, 1), G_TYPE_BOOLEAN); - g_value_set_boolean (g_value_array_get_nth (status, 1), - mixin_cls->statuses[i].self); - - g_value_array_append (status, NULL); - g_value_init (g_value_array_get_nth (status, 2), G_TYPE_BOOLEAN); - g_value_set_boolean (g_value_array_get_nth (status, 2), message); + status = tp_value_array_build (3, + G_TYPE_UINT, (guint) mixin_cls->statuses[i].presence_type, + G_TYPE_BOOLEAN, mixin_cls->statuses[i].self, + G_TYPE_BOOLEAN, message, + G_TYPE_INVALID); g_hash_table_insert (ret, (gchar *) mixin_cls->statuses[i].name, status); @@ -1425,19 +1396,11 @@ construct_simple_presence_value_array (TpPresenceStatus *status, if (message == NULL) message = ""; - presence = g_value_array_new (3); - - g_value_array_append (presence, NULL); - g_value_init (g_value_array_get_nth (presence, 0), G_TYPE_UINT); - g_value_set_uint (g_value_array_get_nth (presence, 0), status_type); - - g_value_array_append (presence, NULL); - g_value_init (g_value_array_get_nth (presence, 1), G_TYPE_STRING); - g_value_set_string (g_value_array_get_nth (presence, 1), status_name); - - g_value_array_append (presence, NULL); - g_value_init (g_value_array_get_nth (presence, 2), G_TYPE_STRING); - g_value_set_string (g_value_array_get_nth (presence, 2), message); + presence = tp_value_array_build (3, + G_TYPE_UINT, status_type, + G_TYPE_STRING, status_name, + G_TYPE_STRING, message, + G_TYPE_INVALID); return presence; } diff --git a/telepathy-glib/room-info.c b/telepathy-glib/room-info.c index 1843b1d..10942dc 100644 --- a/telepathy-glib/room-info.c +++ b/telepathy-glib/room-info.c @@ -25,6 +25,7 @@ #include #include +#include /** * SECTION: room-info @@ -97,7 +98,8 @@ TpRoomInfo * _tp_room_info_new (GValueArray *dbus_struct) { TpRoomInfo *room; - GValue *v; + const gchar *channel_type; + GHashTable *info; g_return_val_if_fail (dbus_struct != NULL, NULL); g_return_val_if_fail (dbus_struct->n_values == 3, NULL); @@ -107,14 +109,16 @@ _tp_room_info_new (GValueArray *dbus_struct) room = g_object_new (TP_TYPE_ROOM_INFO, NULL); - v = g_value_array_get_nth (dbus_struct, 0); - room->priv->handle = g_value_get_uint (v); - - v = g_value_array_get_nth (dbus_struct, 1); - room->priv->channel_type = g_value_dup_string (v); - - v = g_value_array_get_nth (dbus_struct, 2); - room->priv->info = g_value_dup_boxed (v); + tp_value_array_unpack (dbus_struct, 3, + &room->priv->handle, + &channel_type, + &info); + room->priv->channel_type = g_strdup (channel_type); + room->priv->info = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, (GDestroyNotify) tp_g_value_slice_free); + tp_g_hash_table_update (room->priv->info, info, + (GBoxedCopyFunc) g_strdup, + (GBoxedCopyFunc) tp_g_value_slice_dup); return room; } diff --git a/tests/lib/contacts-conn.c b/tests/lib/contacts-conn.c index 6cca69f..9d4387b 100644 --- a/tests/lib/contacts-conn.c +++ b/tests/lib/contacts-conn.c @@ -638,21 +638,15 @@ tp_tests_contacts_connection_change_aliases (TpTestsContactsConnection *self, for (i = 0; i < n; i++) { - GValueArray *pair = g_value_array_new (2); + GValueArray *pair = tp_value_array_build (2, + G_TYPE_UINT, handles[i], + G_TYPE_STRING, aliases[i], + G_TYPE_INVALID); DEBUG ("contact#%u -> %s", handles[i], aliases[i]); g_hash_table_insert (self->priv->aliases, GUINT_TO_POINTER (handles[i]), g_strdup (aliases[i])); - - g_value_array_append (pair, NULL); - g_value_init (pair->values + 0, G_TYPE_UINT); - g_value_set_uint (pair->values + 0, handles[i]); - - g_value_array_append (pair, NULL); - g_value_init (pair->values + 1, G_TYPE_STRING); - g_value_set_string (pair->values + 1, aliases[i]); - g_ptr_array_add (structs, pair); } -- 1.8.4.rc3