diff options
author | Will Thompson <will.thompson@collabora.co.uk> | 2011-06-22 00:26:25 +0100 |
---|---|---|
committer | Will Thompson <will.thompson@collabora.co.uk> | 2011-06-28 11:01:04 +0100 |
commit | e4450664ff14d65b76f869aeb0aeb7cc9fe51438 (patch) | |
tree | a7e30238c8bc14c591ffe1341f51fb613ebf6549 /src/conn-aliasing.c | |
parent | 7bea4d961ec7361455258ed01750df28a7207c26 (diff) |
Aliasing: remove a hateful g_hash_table_foreach
Diffstat (limited to 'src/conn-aliasing.c')
-rw-r--r-- | src/conn-aliasing.c | 87 |
1 files changed, 36 insertions, 51 deletions
diff --git a/src/conn-aliasing.c b/src/conn-aliasing.c index 782fb82cf..802b1ca07 100644 --- a/src/conn-aliasing.c +++ b/src/conn-aliasing.c @@ -472,14 +472,6 @@ gabble_connection_request_aliases (TpSvcConnectionInterfaceAliasing *iface, aliases_request_free (request); } - -struct _i_hate_g_hash_table_foreach -{ - GabbleConnection *conn; - GError **error; - gboolean retval; -}; - static LmHandlerResult nick_publish_msg_reply_cb (GabbleConnection *conn, LmMessage *sent_msg, @@ -510,40 +502,39 @@ nick_publish_msg_reply_cb (GabbleConnection *conn, return LM_HANDLER_RESULT_REMOVE_MESSAGE; } -static void -setaliases_foreach (gpointer key, gpointer value, gpointer user_data) +static gboolean +set_one_alias ( + GabbleConnection *conn, + TpHandle handle, + gchar *alias, + GError **error) { - struct _i_hate_g_hash_table_foreach *data = - (struct _i_hate_g_hash_table_foreach *) user_data; - TpHandle handle = GPOINTER_TO_UINT (key); - gchar *alias = (gchar *) value; - GError *error = NULL; - TpBaseConnection *base = (TpBaseConnection *) data->conn; + TpBaseConnection *base = (TpBaseConnection *) conn; TpHandleRepoIface *contact_handles = tp_base_connection_get_handles (base, TP_HANDLE_TYPE_CONTACT); + gboolean ret = TRUE; g_assert (base->status == TP_CONNECTION_STATUS_CONNECTED); - if (!tp_handle_is_valid (contact_handles, handle, &error)) + if (!tp_handle_is_valid (contact_handles, handle, error)) { - data->retval = FALSE; + ret = FALSE; } else if (base->self_handle == handle) { /* only alter the roster if we're already there, e.g. because someone * added us with another client */ - if (gabble_roster_handle_has_entry (data->conn->roster, handle) - && !gabble_roster_handle_set_name (data->conn->roster, handle, - alias, data->error)) + if (gabble_roster_handle_has_entry (conn->roster, handle) + && !gabble_roster_handle_set_name (conn->roster, handle, + alias, error)) { - data->retval = FALSE; + ret = FALSE; } } - else if (!gabble_roster_handle_set_name (data->conn->roster, handle, alias, - data->error)) + else if (!gabble_roster_handle_set_name (conn->roster, handle, alias, error)) { - data->retval = FALSE; + ret = FALSE; } if (base->self_handle == handle) @@ -555,18 +546,17 @@ setaliases_foreach (gpointer key, gpointer value, gpointer user_data) * here, and just let the request happen in the background. */ - if (data->conn->features & GABBLE_CONNECTION_FEATURES_PEP) + if (conn->features & GABBLE_CONNECTION_FEATURES_PEP) { /* Publish nick using PEP */ LmMessage *msg; WockyNode *item; - msg = wocky_pep_service_make_publish_stanza (data->conn->pep_nick, - &item); + msg = wocky_pep_service_make_publish_stanza (conn->pep_nick, &item); wocky_node_add_child_with_content_ns (item, "nick", alias, NS_NICK); - _gabble_connection_send_with_reply (data->conn, msg, + _gabble_connection_send_with_reply (conn, msg, nick_publish_msg_reply_cb, NULL, NULL, NULL); lm_message_unref (msg); @@ -574,21 +564,11 @@ setaliases_foreach (gpointer key, gpointer value, gpointer user_data) edits = g_list_append (edits, gabble_vcard_manager_edit_info_new ( NULL, alias, GABBLE_VCARD_EDIT_SET_ALIAS, NULL)); - gabble_vcard_manager_edit (data->conn->vcard_manager, 0, NULL, - NULL, G_OBJECT (data->conn), edits); + gabble_vcard_manager_edit (conn->vcard_manager, 0, NULL, + NULL, G_OBJECT (conn), edits); } - if (NULL != error) - { - if (NULL == *(data->error)) - { - *(data->error) = error; - } - else - { - g_error_free (error); - } - } + return ret; } /** @@ -604,27 +584,32 @@ gabble_connection_set_aliases (TpSvcConnectionInterfaceAliasing *iface, { GabbleConnection *self = GABBLE_CONNECTION (iface); TpBaseConnection *base = (TpBaseConnection *) self; - GError *error = NULL; - struct _i_hate_g_hash_table_foreach data = { NULL, NULL, TRUE }; + GHashTableIter iter; + gpointer key, value; + gboolean retval = TRUE; + GError *first_error = NULL; g_assert (GABBLE_IS_CONNECTION (self)); TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (base, context); - data.conn = self; - data.error = &error; - - g_hash_table_foreach (aliases, setaliases_foreach, &data); + g_hash_table_iter_init (&iter, aliases); + while (g_hash_table_iter_next (&iter, &key, &value)) + { + if (!set_one_alias (self, GPOINTER_TO_UINT (key), value, + (first_error == NULL ? &first_error : NULL))) + retval = FALSE; + } - if (data.retval) + if (retval) { tp_svc_connection_interface_aliasing_return_from_set_aliases ( context); } else { - dbus_g_method_return_error (context, error); - g_error_free (error); + dbus_g_method_return_error (context, first_error); + g_error_free (first_error); } } |