about summary refs log tree commit diff stats
path: root/src/roster_list.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/roster_list.c')
-rw-r--r--src/roster_list.c96
1 files changed, 61 insertions, 35 deletions
diff --git a/src/roster_list.c b/src/roster_list.c
index 44d05ff0..2d9df860 100644
--- a/src/roster_list.c
+++ b/src/roster_list.c
@@ -1,7 +1,7 @@
 /*
  * roster_list.c
  *
- * Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
+ * Copyright (C) 2012 - 2015 James Booth <boothj5@gmail.com>
  *
  * This file is part of Profanity.
  *
@@ -42,6 +42,7 @@
 #include "contact.h"
 #include "jid.h"
 #include "tools/autocomplete.h"
+#include "config/preferences.h"
 
 // nicknames
 static Autocomplete name_ac;
@@ -91,7 +92,7 @@ roster_update_presence(const char * const barejid, Resource *resource,
     assert(barejid != NULL);
     assert(resource != NULL);
 
-    PContact contact = g_hash_table_lookup(contacts, barejid);
+    PContact contact = roster_get_contact(barejid);
     if (contact == NULL) {
         return FALSE;
     }
@@ -109,14 +110,45 @@ roster_update_presence(const char * const barejid, Resource *resource,
 PContact
 roster_get_contact(const char * const barejid)
 {
-    return g_hash_table_lookup(contacts, barejid);
+    gchar *barejidlower = g_utf8_strdown(barejid, -1);
+    PContact contact = g_hash_table_lookup(contacts, barejidlower);
+    g_free(barejidlower);
+
+    return contact;
+}
+
+char *
+roster_get_msg_display_name(const char * const barejid, const char * const resource)
+{
+    GString *result = g_string_new("");
+
+    PContact contact = roster_get_contact(barejid);
+    if (contact) {
+        if (p_contact_name(contact)) {
+            g_string_append(result, p_contact_name(contact));
+        } else {
+            g_string_append(result, barejid);
+        }
+    } else {
+        g_string_append(result, barejid);
+    }
+
+    if (resource && prefs_get_boolean(PREF_RESOURCE_MESSAGE)) {
+        g_string_append(result, "/");
+        g_string_append(result, resource);
+    }
+
+    char *result_str = result->str;
+    g_string_free(result, FALSE);
+
+    return result_str;
 }
 
 gboolean
 roster_contact_offline(const char * const barejid,
     const char * const resource, const char * const status)
 {
-    PContact contact = g_hash_table_lookup(contacts, barejid);
+    PContact contact = roster_get_contact(barejid);
 
     if (contact == NULL) {
         return FALSE;
@@ -174,7 +206,7 @@ roster_change_name(PContact contact, const char * const new_name)
     const char *current_name = NULL;
     const char *barejid = p_contact_barejid(contact);
 
-    if (p_contact_name(contact) != NULL) {
+    if (p_contact_name(contact)) {
         current_name = strdup(p_contact_name(contact));
     }
 
@@ -191,9 +223,9 @@ roster_remove(const char * const name, const char * const barejid)
 
     // remove each fulljid
     PContact contact = roster_get_contact(barejid);
-    if (contact != NULL) {
+    if (contact) {
         GList *resources = p_contact_get_available_resources(contact);
-        while (resources != NULL) {
+        while (resources) {
             GString *fulljid = g_string_new(strdup(barejid));
             g_string_append(fulljid, "/");
             g_string_append(fulljid, resources->data);
@@ -212,7 +244,7 @@ void
 roster_update(const char * const barejid, const char * const name,
     GSList *groups, const char * const subscription, gboolean pending_out)
 {
-    PContact contact = g_hash_table_lookup(contacts, barejid);
+    PContact contact = roster_get_contact(barejid);
     assert(contact != NULL);
 
     p_contact_set_subscription(contact, subscription);
@@ -220,7 +252,7 @@ roster_update(const char * const barejid, const char * const name,
 
     const char * const new_name = name;
     const char * current_name = NULL;
-    if (p_contact_name(contact) != NULL) {
+    if (p_contact_name(contact)) {
         current_name = strdup(p_contact_name(contact));
     }
 
@@ -229,7 +261,7 @@ roster_update(const char * const barejid, const char * const name,
     _replace_name(current_name, new_name, barejid);
 
     // add groups
-    while (groups != NULL) {
+    while (groups) {
         autocomplete_add(groups_ac, groups->data);
         groups = g_slist_next(groups);
     }
@@ -239,8 +271,8 @@ gboolean
 roster_add(const char * const barejid, const char * const name, GSList *groups,
     const char * const subscription, gboolean pending_out)
 {
-    PContact contact = g_hash_table_lookup(contacts, barejid);
-    if (contact != NULL) {
+    PContact contact = roster_get_contact(barejid);
+    if (contact) {
         return FALSE;
     }
 
@@ -248,7 +280,7 @@ roster_add(const char * const barejid, const char * const name, GSList *groups,
         pending_out);
 
     // add groups
-    while (groups != NULL) {
+    while (groups) {
         autocomplete_add(groups_ac, groups->data);
         groups = g_slist_next(groups);
     }
@@ -286,7 +318,7 @@ roster_get_contacts_by_presence(const char * const presence)
         }
     }
 
-    // resturn all contact structs
+    // return all contact structs
     return result;
 }
 
@@ -303,7 +335,7 @@ roster_get_contacts(void)
         result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts);
     }
 
-    // resturn all contact structs
+    // return all contact structs
     return result;
 }
 
@@ -321,7 +353,7 @@ roster_get_contacts_online(void)
             result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts);
     }
 
-    // resturn all contact structs
+    // return all contact structs
     return result;
 }
 
@@ -371,7 +403,7 @@ roster_get_nogroup(void)
         }
     }
 
-    // resturn all contact structs
+    // return all contact structs
     return result;
 }
 
@@ -386,7 +418,7 @@ roster_get_group(const char * const group)
     g_hash_table_iter_init(&iter, contacts);
     while (g_hash_table_iter_next(&iter, &key, &value)) {
         GSList *groups = p_contact_groups(value);
-        while (groups != NULL) {
+        while (groups) {
             if (strcmp(groups->data, group) == 0) {
                 result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts);
                 break;
@@ -395,7 +427,7 @@ roster_get_group(const char * const group)
         }
     }
 
-    // resturn all contact structs
+    // return all contact structs
     return result;
 }
 
@@ -445,12 +477,12 @@ _replace_name(const char * const current_name, const char * const new_name,
     const char * const barejid)
 {
     // current handle exists already
-    if (current_name != NULL) {
+    if (current_name) {
         autocomplete_remove(name_ac, current_name);
         g_hash_table_remove(name_to_barejid, current_name);
         _add_name_and_barejid(new_name, barejid);
     // no current handle
-    } else if (new_name != NULL) {
+    } else if (new_name) {
         autocomplete_remove(name_ac, barejid);
         g_hash_table_remove(name_to_barejid, barejid);
         _add_name_and_barejid(new_name, barejid);
@@ -460,7 +492,7 @@ _replace_name(const char * const current_name, const char * const new_name,
 static void
 _add_name_and_barejid(const char * const name, const char * const barejid)
 {
-    if (name != NULL) {
+    if (name) {
         autocomplete_add(name_ac, name);
         g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid));
     } else {
@@ -475,24 +507,18 @@ 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);
+    if (p_contact_name_collate_key(a)) {
+        utf8_str_a = p_contact_name_collate_key(a);
     } else {
-        utf8_str_a = p_contact_barejid(a);
+        utf8_str_a = p_contact_barejid_collate_key(a);
     }
-    if (p_contact_name(b) != NULL) {
-        utf8_str_b = p_contact_name(b);
+    if (p_contact_name_collate_key(b)) {
+        utf8_str_b = p_contact_name_collate_key(b);
     } else {
-        utf8_str_b = p_contact_barejid(b);
+        utf8_str_b = p_contact_barejid_collate_key(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);
+    gint result = g_strcmp0(utf8_str_a, utf8_str_b);
 
     return result;
 }