From 15c782059435bbea671938124d6ff43dc0186b7e Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 22 May 2013 21:15:05 +0100 Subject: [PATCH] Reuse roster_add in roster_update --- src/xmpp/roster.c | 523 +++++++++++++++++++++++----------------------- 1 file changed, 257 insertions(+), 266 deletions(-) diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index 4a4f1709..9d2b765f 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -78,12 +78,269 @@ roster_request(void) xmpp_stanza_release(iq); } +void +roster_init(void) +{ + name_ac = autocomplete_new(); + barejid_ac = autocomplete_new(); + fulljid_ac = autocomplete_new(); + contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, + (GDestroyNotify)p_contact_free); + name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); +} + +void +roster_clear(void) +{ + autocomplete_clear(name_ac); + autocomplete_clear(barejid_ac); + autocomplete_clear(fulljid_ac); + g_hash_table_destroy(contacts); + contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, + (GDestroyNotify)p_contact_free); + g_hash_table_destroy(name_to_barejid); + name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); +} + +void +roster_free() +{ + autocomplete_free(name_ac); + autocomplete_free(barejid_ac); + autocomplete_free(fulljid_ac); +} + +void +roster_reset_search_attempts(void) +{ + autocomplete_reset(name_ac); + autocomplete_reset(barejid_ac); + autocomplete_reset(fulljid_ac); +} + +gboolean +roster_add(const char * const barejid, const char * const name, + 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, subscription, NULL, pending_out); + g_hash_table_insert(contacts, strdup(barejid), contact); + if (name != NULL) { + autocomplete_add(name_ac, strdup(name)); + g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid)); + } else { + autocomplete_add(name_ac, strdup(barejid)); + g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); + } + autocomplete_add(barejid_ac, strdup(barejid)); + + added = TRUE; + } + + return added; +} + +void +roster_update(const char * const barejid, const char * const name, + const char * const subscription, gboolean pending_out) +{ + PContact contact = g_hash_table_lookup(contacts, barejid); + + if (contact == NULL) { + roster_add(barejid, name, subscription, pending_out); + } else { + p_contact_set_subscription(contact, subscription); + p_contact_set_pending_out(contact, pending_out); + + const char * const new_handle = name; + const char * current_handle = NULL; + if (p_contact_name(contact) != NULL) { + current_handle = strdup(p_contact_name(contact)); + } + + p_contact_set_name(contact, new_handle); + + // current handle exists already + if (current_handle != NULL) { + autocomplete_remove(name_ac, current_handle); + g_hash_table_remove(name_to_barejid, current_handle); + + if (new_handle != NULL) { + autocomplete_add(name_ac, strdup(new_handle)); + g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid)); + } else { + autocomplete_add(name_ac, strdup(barejid)); + g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); + } + // no current handle + } else { + if (new_handle != NULL) { + autocomplete_remove(name_ac, barejid); + g_hash_table_remove(name_to_barejid, barejid); + + autocomplete_add(name_ac, strdup(new_handle)); + g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid)); + } + } + } +} + +gboolean +roster_update_presence(const char * const barejid, Resource *resource, + GDateTime *last_activity) +{ + assert(barejid != NULL); + assert(resource != NULL); + + PContact contact = g_hash_table_lookup(contacts, barejid); + if (contact == NULL) { + return FALSE; + } + + if (!_datetimes_equal(p_contact_last_activity(contact), last_activity)) { + p_contact_set_last_activity(contact, last_activity); + } + p_contact_set_presence(contact, resource); + Jid *jid = jid_create_from_bare_and_resource(barejid, resource->name); + autocomplete_add(fulljid_ac, strdup(jid->fulljid)); + jid_destroy(jid); + + return TRUE; +} + +gboolean +roster_contact_offline(const char * const barejid, + const char * const resource, const char * const status) +{ + PContact contact = g_hash_table_lookup(contacts, barejid); + if (contact == NULL) { + return FALSE; + } + if (resource == NULL) { + return TRUE; + } else { + gboolean result = p_contact_remove_resource(contact, resource); + if (result == TRUE) { + Jid *jid = jid_create_from_bare_and_resource(barejid, resource); + autocomplete_remove(fulljid_ac, jid->fulljid); + jid_destroy(jid); + } + + return result; + } +} + +void +roster_change_name(const char * const barejid, const char * const new_name) +{ + PContact contact = g_hash_table_lookup(contacts, barejid); + const char * current_name = NULL; + if (p_contact_name(contact) != NULL) { + current_name = strdup(p_contact_name(contact)); + } + + if (contact != NULL) { + p_contact_set_name(contact, new_name); + + // current name exists already + if (current_name != NULL) { + autocomplete_remove(name_ac, current_name); + g_hash_table_remove(name_to_barejid, current_name); + + if (new_name != NULL) { + autocomplete_add(name_ac, strdup(new_name)); + g_hash_table_insert(name_to_barejid, strdup(new_name), strdup(barejid)); + } else { + autocomplete_add(name_ac, strdup(barejid)); + g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); + } + // no current name + } else { + if (new_name != NULL) { + autocomplete_remove(name_ac, barejid); + g_hash_table_remove(name_to_barejid, barejid); + + autocomplete_add(name_ac, strdup(new_name)); + g_hash_table_insert(name_to_barejid, strdup(new_name), strdup(barejid)); + } + } + + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_roster_set(ctx, barejid, new_name); + xmpp_send(conn, iq); + xmpp_stanza_release(iq); + } +} + +gboolean +roster_has_pending_subscriptions(void) +{ + GHashTableIter iter; + gpointer key; + gpointer value; + + g_hash_table_iter_init(&iter, contacts); + while (g_hash_table_iter_next(&iter, &key, &value)) { + PContact contact = (PContact) value; + if (p_contact_pending_out(contact)) { + return TRUE; + } + } + + return FALSE; +} + +GSList * +roster_get_contacts(void) +{ + 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 * +roster_find_contact(char *search_str) +{ + return autocomplete_complete(name_ac, search_str); +} + +char * +roster_find_jid(char *search_str) +{ + return autocomplete_complete(barejid_ac, search_str); +} + +char * +roster_find_resource(char *search_str) +{ + return autocomplete_complete(fulljid_ac, search_str); +} + char * roster_barejid_from_name(const char * const name) { return g_hash_table_lookup(name_to_barejid, name); } +PContact +roster_get_contact(const char const *barejid) +{ + return g_hash_table_lookup(contacts, barejid); +} + static int _roster_handle_push(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) @@ -187,272 +444,6 @@ _roster_handle_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, return 1; } -void -roster_init(void) -{ - name_ac = autocomplete_new(); - barejid_ac = autocomplete_new(); - fulljid_ac = autocomplete_new(); - contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, - (GDestroyNotify)p_contact_free); - name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); -} - -void -roster_clear(void) -{ - autocomplete_clear(name_ac); - autocomplete_clear(barejid_ac); - autocomplete_clear(fulljid_ac); - g_hash_table_destroy(contacts); - contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, - (GDestroyNotify)p_contact_free); - g_hash_table_destroy(name_to_barejid); - name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); -} - -void -roster_free() -{ - autocomplete_free(name_ac); - autocomplete_free(barejid_ac); - autocomplete_free(fulljid_ac); -} - -void -roster_reset_search_attempts(void) -{ - autocomplete_reset(name_ac); - autocomplete_reset(barejid_ac); - autocomplete_reset(fulljid_ac); -} - -gboolean -roster_add(const char * const barejid, const char * const name, - 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, subscription, NULL, pending_out); - g_hash_table_insert(contacts, strdup(barejid), contact); - if (name != NULL) { - autocomplete_add(name_ac, strdup(name)); - g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid)); - } else { - autocomplete_add(name_ac, strdup(barejid)); - g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); - } - autocomplete_add(barejid_ac, strdup(barejid)); - - added = TRUE; - } - - return added; -} - -gboolean -roster_update_presence(const char * const barejid, Resource *resource, - GDateTime *last_activity) -{ - assert(barejid != NULL); - assert(resource != NULL); - - PContact contact = g_hash_table_lookup(contacts, barejid); - if (contact == NULL) { - return FALSE; - } - - if (!_datetimes_equal(p_contact_last_activity(contact), last_activity)) { - p_contact_set_last_activity(contact, last_activity); - } - p_contact_set_presence(contact, resource); - Jid *jid = jid_create_from_bare_and_resource(barejid, resource->name); - autocomplete_add(fulljid_ac, strdup(jid->fulljid)); - jid_destroy(jid); - - return TRUE; -} - -gboolean -roster_contact_offline(const char * const barejid, - const char * const resource, const char * const status) -{ - PContact contact = g_hash_table_lookup(contacts, barejid); - if (contact == NULL) { - return FALSE; - } - if (resource == NULL) { - return TRUE; - } else { - gboolean result = p_contact_remove_resource(contact, resource); - if (result == TRUE) { - Jid *jid = jid_create_from_bare_and_resource(barejid, resource); - autocomplete_remove(fulljid_ac, jid->fulljid); - jid_destroy(jid); - } - - return result; - } -} - -void -roster_change_name(const char * const barejid, const char * const new_name) -{ - PContact contact = g_hash_table_lookup(contacts, barejid); - const char * current_name = NULL; - if (p_contact_name(contact) != NULL) { - current_name = strdup(p_contact_name(contact)); - } - - if (contact != NULL) { - p_contact_set_name(contact, new_name); - - // current name exists already - if (current_name != NULL) { - autocomplete_remove(name_ac, current_name); - g_hash_table_remove(name_to_barejid, current_name); - - if (new_name != NULL) { - autocomplete_add(name_ac, strdup(new_name)); - g_hash_table_insert(name_to_barejid, strdup(new_name), strdup(barejid)); - } else { - autocomplete_add(name_ac, strdup(barejid)); - g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); - } - // no current name - } else { - if (new_name != NULL) { - autocomplete_remove(name_ac, barejid); - g_hash_table_remove(name_to_barejid, barejid); - - autocomplete_add(name_ac, strdup(new_name)); - g_hash_table_insert(name_to_barejid, strdup(new_name), strdup(barejid)); - } - } - - xmpp_conn_t * const conn = connection_get_conn(); - xmpp_ctx_t * const ctx = connection_get_ctx(); - xmpp_stanza_t *iq = stanza_create_roster_set(ctx, barejid, new_name); - xmpp_send(conn, iq); - xmpp_stanza_release(iq); - } -} - -void -roster_update(const char * const barejid, const char * const name, - const char * const subscription, gboolean pending_out) -{ - PContact contact = g_hash_table_lookup(contacts, barejid); - - if (contact == NULL) { - contact = p_contact_new(barejid, name, subscription, NULL, pending_out); - g_hash_table_insert(contacts, strdup(barejid), contact); - if (name != NULL) { - autocomplete_add(name_ac, strdup(name)); - g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid)); - } else { - autocomplete_add(name_ac, strdup(barejid)); - g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); - } - autocomplete_add(barejid_ac, strdup(barejid)); - } else { - p_contact_set_subscription(contact, subscription); - p_contact_set_pending_out(contact, pending_out); - - const char * const new_handle = name; - const char * current_handle = NULL; - if (p_contact_name(contact) != NULL) { - current_handle = strdup(p_contact_name(contact)); - } - - p_contact_set_name(contact, new_handle); - - // current handle exists already - if (current_handle != NULL) { - autocomplete_remove(name_ac, current_handle); - g_hash_table_remove(name_to_barejid, current_handle); - - if (new_handle != NULL) { - autocomplete_add(name_ac, strdup(new_handle)); - g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid)); - } else { - autocomplete_add(name_ac, strdup(barejid)); - g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); - } - // no current handle - } else { - if (new_handle != NULL) { - autocomplete_remove(name_ac, barejid); - g_hash_table_remove(name_to_barejid, barejid); - - autocomplete_add(name_ac, strdup(new_handle)); - g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid)); - } - } - } -} - -gboolean -roster_has_pending_subscriptions(void) -{ - GHashTableIter iter; - gpointer key; - gpointer value; - - g_hash_table_iter_init(&iter, contacts); - while (g_hash_table_iter_next(&iter, &key, &value)) { - PContact contact = (PContact) value; - if (p_contact_pending_out(contact)) { - return TRUE; - } - } - - return FALSE; -} - -GSList * -roster_get_contacts(void) -{ - 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 * -roster_find_contact(char *search_str) -{ - return autocomplete_complete(name_ac, search_str); -} - -char * -roster_find_jid(char *search_str) -{ - return autocomplete_complete(barejid_ac, search_str); -} - -char * -roster_find_resource(char *search_str) -{ - return autocomplete_complete(fulljid_ac, search_str); -} - -PContact -roster_get_contact(const char const *barejid) -{ - return g_hash_table_lookup(contacts, barejid); -} - static gboolean _key_equals(void *key1, void *key2) {