about summary refs log tree commit diff stats
path: root/src/contact_list.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contact_list.c')
-rw-r--r--src/contact_list.c84
1 files changed, 65 insertions, 19 deletions
diff --git a/src/contact_list.c b/src/contact_list.c
index e266e25e..d05d7412 100644
--- a/src/contact_list.c
+++ b/src/contact_list.c
@@ -22,24 +22,30 @@
 
 #include <string.h>
 
+#include <glib.h>
+
 #include "contact.h"
+#include "log.h"
 #include "prof_autocomplete.h"
 
 static PAutocomplete ac;
+static GHashTable *contacts;
+
+static gboolean _key_equals(void *key1, void *key2);
 
 void
 contact_list_init(void)
 {
-    ac = p_obj_autocomplete_new((PStrFunc)p_contact_name,
-                            (PCopyFunc)p_contact_copy,
-                            (PEqualDeepFunc)p_contacts_equal_deep,
-                            (GDestroyNotify)p_contact_free);
+    ac = p_autocomplete_new();
+    contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free,
+        (GDestroyNotify)p_contact_free);
 }
 
 void
 contact_list_clear(void)
 {
     p_autocomplete_clear(ac);
+    g_hash_table_remove_all(contacts);
 }
 
 void
@@ -49,22 +55,63 @@ contact_list_reset_search_attempts(void)
 }
 
 gboolean
-contact_list_remove(const char * const name)
+contact_list_add(const char * const jid, const char * const name,
+    const char * const presence, const char * const status,
+    const char * const subscription)
 {
-    return p_autocomplete_remove(ac, name);
+    gboolean added = FALSE;
+    PContact contact = g_hash_table_lookup(contacts, jid);
+
+    if (contact == NULL) {
+        contact = p_contact_new(jid, name, presence, status, subscription);
+        g_hash_table_insert(contacts, strdup(jid), contact);
+        p_autocomplete_add(ac, strdup(jid));
+        added = TRUE;
+    }
+
+    return added;
 }
 
 gboolean
-contact_list_add(const char * const name, const char * const show,
+contact_list_update_contact(const char * const jid, const char * const presence,
     const char * const status)
 {
-    return p_autocomplete_add(ac, p_contact_new(name, show, status));
+    gboolean changed = FALSE;
+    PContact contact = g_hash_table_lookup(contacts, jid);
+
+    if (contact == NULL) {
+        log_warning("Contact not in list: %s", jid);
+        return FALSE;
+    }
+
+    if (g_strcmp0(p_contact_presence(contact), presence) != 0) {
+        p_contact_set_presence(contact, presence);
+        changed = TRUE;
+    }
+
+    if (g_strcmp0(p_contact_status(contact), status) != 0) {
+        p_contact_set_status(contact, status);
+        changed = TRUE;
+    }
+
+    return changed;
 }
 
 GSList *
 get_contact_list(void)
 {
-    return p_autocomplete_get_list(ac);
+    GSList *result = NULL;
+    GHashTableIter iter;
+    gpointer key;
+    gpointer value;
+
+    g_hash_table_iter_init(&iter, contacts);
+    while (g_hash_table_iter_next(&iter, &key, &value)) {
+        result = g_slist_append(result, value);
+    }
+
+    // resturn all contact structs
+    return result;
 }
 
 char *
@@ -76,15 +123,14 @@ contact_list_find_contact(char *search_str)
 PContact
 contact_list_get_contact(const char const *jid)
 {
-    GSList *contacts = get_contact_list();
-
-    while (contacts != NULL) {
-        PContact contact = contacts->data;
-        if (strcmp(p_contact_name(contact), jid) == 0) {
-            return contact;
-        }
-        contacts = g_slist_next(contacts);
-    }
+    return g_hash_table_lookup(contacts, jid);
+}
+
+static
+gboolean _key_equals(void *key1, void *key2)
+{
+    gchar *str1 = (gchar *) key1;
+    gchar *str2 = (gchar *) key2;
 
-    return NULL;
+    return (g_strcmp0(str1, str2) == 0);
 }