From a0eda4207cbc25329754944da2dedf9e19fa0150 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 29 Oct 2012 21:44:33 +0000 Subject: Split contact add and update presence --- src/contact.c | 30 +++++++++++++++++++++++ src/contact.h | 2 ++ src/contact_list.c | 70 +++++++++++++++++++++--------------------------------- src/contact_list.h | 2 ++ src/profanity.c | 30 +++++++++++++---------- 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 #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(); + } } } } -- cgit 1.4.1-2-gfad0