about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-10-29 21:44:33 +0000
committerJames Booth <boothj5@gmail.com>2012-10-29 21:44:33 +0000
commita0eda4207cbc25329754944da2dedf9e19fa0150 (patch)
tree1210fef8d1b29f6c173cdd0eb9b527c1d8fd916f
parentdbb744532482baab8e012af500dbeda29600d2e1 (diff)
downloadprofani-tty-a0eda4207cbc25329754944da2dedf9e19fa0150.tar.gz
Split contact add and update presence
-rw-r--r--src/contact.c30
-rw-r--r--src/contact.h2
-rw-r--r--src/contact_list.c70
-rw-r--r--src/contact_list.h2
-rw-r--r--src/profanity.c30
5 files changed, 79 insertions, 55 deletions
diff --git a/src/contact.c b/src/contact.c
index 2d58308a..3b0d1944 100644
--- a/src/contact.c
+++ b/src/contact.c
@@ -156,6 +156,36 @@ p_contact_subscription(const PContact contact)
     return contact->subscription;
 }
 
+void
+p_contact_set_presence(const PContact contact, const char * const presence)
+{
+    if (contact->presence != NULL) {
+        free(contact->presence);
+        contact->presence = NULL;
+    }
+
+    if (presence == NULL) {
+        contact->presence = NULL;
+    } else {
+        contact->presence = strdup(presence);
+    }
+}
+
+void
+p_contact_set_status(const PContact contact, const char * const status)
+{
+    if (contact->status != NULL) {
+        free(contact->status);
+        contact->status = NULL;
+    }
+
+    if (status == NULL) {
+        contact->status = NULL;
+    } else {
+        contact->status = strdup(status);
+    }
+}
+
 int
 p_contacts_equal_deep(const PContact c1, const PContact c2)
 {
diff --git a/src/contact.h b/src/contact.h
index 194d867b..a4f487e8 100644
--- a/src/contact.h
+++ b/src/contact.h
@@ -35,6 +35,8 @@ const char * p_contact_name(PContact contact);
 const char * p_contact_presence(PContact contact);
 const char * p_contact_status(PContact contact);
 const char * p_contact_subscription(const PContact contact);
+void p_contact_set_presence(const PContact contact, const char * const presence);
+void p_contact_set_status(const PContact contact, const char * const status);
 int p_contacts_equal_deep(const PContact c1, const PContact c2);
 
 #endif
diff --git a/src/contact_list.c b/src/contact_list.c
index 427d0722..3f8ff2ee 100644
--- a/src/contact_list.c
+++ b/src/contact_list.c
@@ -25,6 +25,7 @@
 #include <glib.h>
 
 #include "contact.h"
+#include "log.h"
 #include "prof_autocomplete.h"
 
 static PAutocomplete ac;
@@ -60,58 +61,41 @@ contact_list_add(const char * const jid, const char * const name,
 {
     PContact contact = g_hash_table_lookup(contacts, jid);
 
-    // contact not yet in list
     if (contact == NULL) {
         contact = p_contact_new(jid, name, presence, status, subscription);
         g_hash_table_insert(contacts, strdup(jid), contact);
-
-    // contact already exists, update non NULL arguments
     } else {
-        char *new_name = NULL;
-        if (name != NULL) {
-            new_name = strdup(name);
-        } else {
-            if (p_contact_name(contact) != NULL) {
-                new_name = strdup(p_contact_name(contact));
-            }
-        }
-
-        char *new_presence = NULL;
-        if (presence != NULL) {
-            new_presence = strdup(presence);
-        } else {
-            if (p_contact_presence(contact) != NULL) {
-                new_presence = strdup(p_contact_presence(contact));
-            }
-        }
-
-        char *new_status = NULL;
-        if (status != NULL) {
-            new_status = strdup(status);
-        } else {
-            if (p_contact_status(contact) != NULL) {
-                new_status = strdup(p_contact_status(contact));
-            }
-        }
-
-        char *new_subscription = NULL;
-        if (subscription != NULL) {
-            new_subscription = strdup(subscription);
-        } else {
-            if (p_contact_subscription(contact) != NULL) {
-                new_subscription = strdup(p_contact_subscription(contact));
-            }
-        }
-
-        PContact new_contact = p_contact_new(jid, new_name, new_presence,
-                new_status, new_subscription);
-
-        g_hash_table_replace(contacts, strdup(jid), new_contact);
+        log_warning("Duplicate roster entry: %s", jid);
     }
 
     p_autocomplete_add(ac, strdup(jid));
 }
 
+gboolean
+contact_list_update_contact(const char * const jid, const char * const presence,
+    const char * const 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)
 {
diff --git a/src/contact_list.h b/src/contact_list.h
index ac9ab6cd..9f6bbe4c 100644
--- a/src/contact_list.h
+++ b/src/contact_list.h
@@ -33,6 +33,8 @@ void contact_list_reset_search_attempts(void);
 void contact_list_add(const char * const jid, const char * const name,
     const char * const presence, const char * const status,
     const char * const subscription);
+gboolean contact_list_update_contact(const char * const jid, const char * const presence,
+    const char * const status);
 GSList * get_contact_list(void);
 char * contact_list_find_contact(char *search_str);
 PContact contact_list_get_contact(const char const *jid);
diff --git a/src/profanity.c b/src/profanity.c
index dd6aae6e..e4888e97 100644
--- a/src/profanity.c
+++ b/src/profanity.c
@@ -165,12 +165,15 @@ prof_handle_failed_login(void)
 void
 prof_handle_contact_online(char *contact, char *show, char *status)
 {
-    contact_list_add(contact, NULL, show, status, NULL);
-    PContact result = contact_list_get_contact(contact);
-    if (p_contact_subscription(result) != NULL) {
-        if (strcmp(p_contact_subscription(result), "none") != 0) {
-            win_contact_online(contact, show, status);
-            win_page_off();
+    gboolean updated = contact_list_update_contact(contact, show, status);
+
+    if (updated) {
+        PContact result = contact_list_get_contact(contact);
+        if (p_contact_subscription(result) != NULL) {
+            if (strcmp(p_contact_subscription(result), "none") != 0) {
+                win_contact_online(contact, show, status);
+                win_page_off();
+            }
         }
     }
 }
@@ -178,12 +181,15 @@ prof_handle_contact_online(char *contact, char *show, char *status)
 void
 prof_handle_contact_offline(char *contact, char *show, char *status)
 {
-    contact_list_add(contact, NULL, "offline", status, NULL);
-    PContact result = contact_list_get_contact(contact);
-    if (p_contact_subscription(result) != NULL) {
-        if (strcmp(p_contact_subscription(result), "none") != 0) {
-            win_contact_offline(contact, show, status);
-            win_page_off();
+    gboolean updated = contact_list_update_contact(contact, "offline", status);
+
+    if (updated) {
+        PContact result = contact_list_get_contact(contact);
+        if (p_contact_subscription(result) != NULL) {
+            if (strcmp(p_contact_subscription(result), "none") != 0) {
+                win_contact_offline(contact, show, status);
+                win_page_off();
+            }
         }
     }
 }