From ec78914044978fee1119c5a905c6a8fdce03a476 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 10 Feb 2013 11:19:36 +0000 Subject: Only create contacts resource when online --- src/contact.c | 49 +++++++++++++++++++++++++++++++++++++------------ src/contact_list.c | 3 +-- src/contact_list.h | 1 - src/resource.c | 1 + src/xmpp/iq.c | 3 +-- 5 files changed, 40 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/contact.c b/src/contact.c index dd0205ee..801e4539 100644 --- a/src/contact.c +++ b/src/contact.c @@ -64,8 +64,10 @@ p_contact_new(const char * const barejid, const char * const name, contact->resources = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)resource_destroy); // TODO, priority, last activity - Resource *resource = resource_new("default", presence, status, 0, caps_str); - g_hash_table_insert(contact->resources, strdup(resource->name), resource); + if (g_strcmp0(presence, "offline") != 0) { + Resource *resource = resource_new("default", presence, status, 0, caps_str); + g_hash_table_insert(contact->resources, strdup(resource->name), resource); + } return contact; } @@ -127,15 +129,23 @@ p_contact_name(const PContact contact) const char * p_contact_presence(const PContact contact) { - Resource *resource = g_hash_table_lookup(contact->resources, "default"); - return resource->show; + if (g_hash_table_size(contact->resources) == 0) { + return "offline"; + } else { + Resource *resource = g_hash_table_lookup(contact->resources, "default"); + return resource->show; + } } const char * p_contact_status(const PContact contact) { - Resource *resource = g_hash_table_lookup(contact->resources, "default"); - return resource->status; + if (g_hash_table_size(contact->resources) == 0) { + return NULL; + } else { + Resource *resource = g_hash_table_lookup(contact->resources, "default"); + return resource->status; + } } const char * @@ -159,17 +169,32 @@ p_contact_last_activity(const PContact contact) const char * p_contact_caps_str(const PContact contact) { - Resource *resource = g_hash_table_lookup(contact->resources, "default"); - return resource->caps_str; + if (g_hash_table_size(contact->resources) == 0) { + return NULL; + } else { + Resource *resource = g_hash_table_lookup(contact->resources, "default"); + return resource->caps_str; + } } void p_contact_set_presence(const PContact contact, const char * const presence) { - Resource *resource = g_hash_table_lookup(contact->resources, "default"); - FREE_SET_NULL(resource->show); - if (presence != NULL) { - resource->show = strdup(presence); + if (g_strcmp0(presence, "offline") == 0) { + g_hash_table_remove(contact->resources, "default"); + } else { + if (g_hash_table_size(contact->resources) == 0) { + Resource *resource = resource_new("default", presence, NULL, 0, NULL); + g_hash_table_insert(contact->resources, strdup(resource->name), resource); + } else { + Resource *resource = g_hash_table_lookup(contact->resources, "default"); + if (presence != NULL) { + FREE_SET_NULL(resource->show); + resource->show = strdup(presence); + } else { + resource->show = NULL; + } + } } } diff --git a/src/contact_list.c b/src/contact_list.c index cac593ee..73333156 100644 --- a/src/contact_list.c +++ b/src/contact_list.c @@ -64,14 +64,13 @@ contact_list_reset_search_attempts(void) gboolean contact_list_add(const char * const barejid, const char * const name, - const char * const presence, const char * const status, const char * const subscription, gboolean pending_out) { gboolean added = FALSE; PContact contact = g_hash_table_lookup(contacts, barejid); if (contact == NULL) { - contact = p_contact_new(barejid, name, presence, status, subscription, + contact = p_contact_new(barejid, name, "offline", NULL, subscription, pending_out, NULL); g_hash_table_insert(contacts, strdup(barejid), contact); autocomplete_add(ac, strdup(barejid)); diff --git a/src/contact_list.h b/src/contact_list.h index c1ccf152..37511e67 100644 --- a/src/contact_list.h +++ b/src/contact_list.h @@ -33,7 +33,6 @@ void contact_list_free(void); void contact_list_reset_search_attempts(void); void contact_list_remove(const char * const barejid); gboolean contact_list_add(const char * const barejid, const char * const name, - const char * const presence, const char * const status, const char * const subscription, gboolean pending_out); gboolean contact_list_update_contact(const char * const barejid, const char * const presence, const char * const status, GDateTime *last_activity, const char * const caps_str); diff --git a/src/resource.c b/src/resource.c index e8443d58..74cb5755 100644 --- a/src/resource.c +++ b/src/resource.c @@ -30,6 +30,7 @@ Resource * resource_new(const char * const name, const char * const show, const char * const status, const int priority, const char * const caps_str) { + assert(g_strcmp0(show, "offline") != 0); assert(name != NULL); Resource *new_resource = malloc(sizeof(struct resource_t)); new_resource->name = strdup(name); diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 9954da1e..6d9e9e6f 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -144,8 +144,7 @@ _iq_handle_roster_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, pending_out = TRUE; } - gboolean added = contact_list_add(barejid, name, "offline", NULL, sub, - pending_out); + gboolean added = contact_list_add(barejid, name, sub, pending_out); if (!added) { log_warning("Attempt to add contact twice: %s", barejid); -- cgit 1.4.1-2-gfad0