[Telepathy-commits] [telepathy-haze/master] DO NOT MERGE Connection: refactor RequestAliases implementation.
Will Thompson
will.thompson at collabora.co.uk
Mon Oct 27 17:44:38 PDT 2008
Also, don't leak a gchar ** if not all handles are valid.
---
src/connection-aliasing.c | 76 ++++++++++++++++++++++++++------------------
1 files changed, 45 insertions(+), 31 deletions(-)
diff --git a/src/connection-aliasing.c b/src/connection-aliasing.c
index 079730e..8da5f07 100644
--- a/src/connection-aliasing.c
+++ b/src/connection-aliasing.c
@@ -60,6 +60,40 @@ haze_connection_get_alias_flags (TpSvcConnectionInterfaceAliasing *self,
context, flags);
}
+
+static gchar *
+conn_get_alias (HazeConnection *conn,
+ TpHandle handle,
+ TpHandleRepoIface *contact_handles,
+ gboolean fall_back_to_id)
+{
+ const gchar *alias;
+
+ if (handle == base_conn->self_handle)
+ {
+ alias = purple_connection_get_display_name (conn->account->gc);
+
+ if (alias == NULL && fall_back_to_id)
+ alias = purple_account_get_username (conn->account);
+ }
+ else
+ {
+ const gchar *id = tp_handle_inspect (contact_handles, handle);
+ PurpleBuddy *buddy = purple_find_buddy (conn->account, id);
+
+ if (buddy != NULL)
+ alias = purple_buddy_get_alias (buddy);
+ else if (fall_back_to_id)
+ alias = id;
+ }
+
+ if (fall_back_to_id)
+ g_assert (alias != NULL);
+
+ return alias;
+}
+
+
void
haze_connection_request_aliases (TpSvcConnectionInterfaceAliasing *self,
const GArray *contacts,
@@ -71,7 +105,7 @@ haze_connection_request_aliases (TpSvcConnectionInterfaceAliasing *self,
tp_base_connection_get_handles (base, TP_HANDLE_TYPE_CONTACT);
guint i;
GError *error = NULL;
- gchar **aliases = g_new0 (gchar *, contacts->len + 1);
+ gchar **aliases;
if (!tp_handles_are_valid (contact_handles, contacts, FALSE, &error))
{
@@ -80,55 +114,35 @@ haze_connection_request_aliases (TpSvcConnectionInterfaceAliasing *self,
return;
}
+ aliases = g_new0 (gchar *, contacts->len + 1);
+
for (i = 0; i < contacts->len; i++)
{
TpHandle handle = g_array_index (contacts, TpHandle, i);
- const gchar *bname = tp_handle_inspect (contact_handles, handle);;
- PurpleBuddy *buddy;
- const gchar *alias;
+ const gchar *alias = conn_get_alias (conn, handle, contact_handles,
+ FALSE);
- if (handle == base->self_handle)
- {
- alias = purple_connection_get_display_name (conn->account->gc);
- if (!alias)
- {
- DEBUG ("%s has no display_name, throwing NotAvailable", bname);
- g_set_error (&error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
- "%u has no alias", handle);
- break;
- }
- }
- else
+ if (alias == NULL)
{
- buddy = purple_find_buddy (conn->account, bname);
-
- if (!buddy)
- {
- DEBUG ("%s not on blist; throwing NotAvailable", bname);
- g_set_error (&error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
- "Alias for %u unknown; subscribe to them first", handle);
- break;
- }
-
- alias = purple_buddy_get_alias (buddy);
+ g_set_error (&error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
+ "%u has no alias", handle);
+ break;
}
- DEBUG ("%s has alias \"%s\"", bname, alias);
- /* They'll be made const again shortly */
aliases[i] = (gchar *) alias;
}
- if (error)
+ if (error != NULL)
{
dbus_g_method_return_error (context, error);
g_error_free (error);
}
else
{
- /* Hrm, why do I need to cast up to const? */
tp_svc_connection_interface_aliasing_return_from_request_aliases (
context, (const gchar **)aliases);
}
+
g_free (aliases);
}
--
1.5.6.5
More information about the Telepathy-commits
mailing list