From 1ca2147844c4a72404d21389fa7716a9168a8fd9 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 30 May 2013 21:47:30 +0100 Subject: [PATCH] Sort contacts on /who output --- src/xmpp/roster.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index 3aeef34d..ce92226c 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -68,6 +68,7 @@ static void _replace_name(const char * const current_name, GSList * _get_groups_from_item(xmpp_stanza_t *item); static gboolean _key_equals(void *key1, void *key2); static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2); +static gint _compare_contacts(PContact a, PContact b); void roster_add_handlers(void) @@ -288,7 +289,7 @@ roster_get_contacts(void) g_hash_table_iter_init(&iter, contacts); while (g_hash_table_iter_next(&iter, &key, &value)) { - result = g_slist_append(result, value); + result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); } // resturn all contact structs @@ -494,6 +495,34 @@ gboolean _key_equals(void *key1, void *key2) return (g_strcmp0(str1, str2) == 0); } +static +gint _compare_contacts(PContact a, PContact b) +{ + const char * utf8_str_a = NULL; + const char * utf8_str_b = NULL; + + if (p_contact_name(a) != NULL) { + utf8_str_a = p_contact_name(a); + } else { + utf8_str_a = p_contact_barejid(a); + } + if (p_contact_name(b) != NULL) { + utf8_str_b = p_contact_name(b); + } else { + utf8_str_b = p_contact_barejid(b); + } + + gchar *key_a = g_utf8_collate_key(utf8_str_a, -1); + gchar *key_b = g_utf8_collate_key(utf8_str_b, -1); + + gint result = g_strcmp0(key_a, key_b); + + g_free(key_a); + g_free(key_b); + + return result; +} + static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2) {