From 045e7b5f1b9e45aae79ed6d45a10444fd2d7406f Mon Sep 17 00:00:00 2001 From: Stefan Becker Date: Sun, 11 Aug 2013 00:49:28 +0300 Subject: [PATCH] buddy: refactor add buddy to group code --- src/core/sipe-buddy.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/core/sipe-buddy.h | 16 +++++++++++++++- src/core/sipe-notify.c | 47 +++++++++-------------------------------------- src/core/sipe-ucs.c | 29 ++++++----------------------- 4 files changed, 74 insertions(+), 62 deletions(-) diff --git a/src/core/sipe-buddy.c b/src/core/sipe-buddy.c index c074cea4..a5b3b9c8 100644 --- a/src/core/sipe-buddy.c +++ b/src/core/sipe-buddy.c @@ -108,6 +108,50 @@ struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private, return buddy; } +void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private, + struct sipe_buddy *buddy, + struct sipe_group *group, + const gchar *alias) +{ + const gchar *uri = buddy->name; + const gchar *group_name = group->name; + sipe_backend_buddy bb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, + uri, + group_name); + + if (!bb) { + bb = sipe_backend_buddy_add(SIPE_CORE_PUBLIC, + uri, + alias, + group_name); + SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'", + uri, alias ? alias : ""); + } + + + if (!is_empty(alias)) { + gchar *old_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, + bb); + + if (sipe_strcase_equal(sipe_get_no_sip_uri(uri), + old_alias)) { + sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, + bb, + alias); + SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'", + uri, old_alias, alias); + } + g_free(old_alias); + } + + buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups, + group, + (GCompareFunc) sipe_group_compare, + NULL); + SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s", + uri, group_name); +} + void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private) { GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, diff --git a/src/core/sipe-buddy.h b/src/core/sipe-buddy.h index 87d2727f..d81e77d8 100644 --- a/src/core/sipe-buddy.h +++ b/src/core/sipe-buddy.h @@ -21,8 +21,9 @@ */ /* Forward declarations */ -struct sipe_core_private; struct sipe_cal_working_hours; +struct sipe_core_private; +struct sipe_group; struct sipe_buddy { gchar *name; @@ -75,6 +76,19 @@ struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private, const gchar *exchange_key); /** + * Add buddy to a group. + * + * @param sipe_private SIPE core data + * @param buddy sipe_buddy data structure + * @param group sipe_group data structure + * @param alias alias for the buddy in that group (may be @c NULL) + */ +void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private, + struct sipe_buddy *buddy, + struct sipe_group *group, + const gchar *alias); + +/** * Remove entries from local buddy list that do not have corresponding entries * in the ones in the contact list sent by the server * diff --git a/src/core/sipe-notify.c b/src/core/sipe-notify.c index 9c288de5..bdcc227f 100644 --- a/src/core/sipe-notify.c +++ b/src/core/sipe-notify.c @@ -1095,8 +1095,7 @@ static void add_new_group(struct sipe_core_private *sipe_private, static void add_new_buddy(struct sipe_core_private *sipe_private, const sipe_xml *node, - const gchar *uri, - const gchar *alias) + const gchar *uri) { const gchar *name = sipe_xml_attribute(node, "name"); /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */ @@ -1129,43 +1128,15 @@ static void add_new_buddy(struct sipe_core_private *sipe_private, group = sipe_private->groups->data; if (group) { - sipe_backend_buddy b = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, - normalized_uri, - group->name); - gchar *b_alias; - - if (!b) { - b = sipe_backend_buddy_add(SIPE_CORE_PUBLIC, - normalized_uri, - alias, - group->name); - SIPE_DEBUG_INFO("Created new buddy %s with alias %s", - normalized_uri, alias); - } - - b_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, b); - if (sipe_strcase_equal(alias, b_alias) && - !is_empty(name)) { - sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, - b, - name); - SIPE_DEBUG_INFO("Replaced for buddy %s in group '%s' old alias '%s' with '%s'", - normalized_uri, group->name, b_alias, name); - } - g_free(b_alias); - if (!buddy) buddy = sipe_buddy_add(sipe_private, normalized_uri, NULL); - buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups, - group, - (GCompareFunc)sipe_group_compare, - NULL); - - SIPE_DEBUG_INFO("Added buddy %s to group %s", - buddy->name, group->name); + sipe_buddy_add_to_group(sipe_private, + buddy, + group, + name); } else { SIPE_DEBUG_INFO("No group found for contact %s! Unable to add to buddy list", name); @@ -1248,7 +1219,7 @@ static gboolean sipe_process_roaming_contacts(struct sipe_core_private *sipe_pri for (item = sipe_xml_child(isc, "contact"); item; item = sipe_xml_twin(item)) { const gchar *name = sipe_xml_attribute(item, "uri"); gchar *uri = sip_uri_from_name(name); - add_new_buddy(sipe_private, item, uri, name); + add_new_buddy(sipe_private, item, uri); g_free(uri); } @@ -1292,9 +1263,9 @@ static gboolean sipe_process_roaming_contacts(struct sipe_core_private *sipe_pri /* Process new buddies */ for (item = sipe_xml_child(isc, "addedContact"); item; item = sipe_xml_twin(item)) { - const gchar *uri = sipe_xml_attribute(item, "uri"); - const gchar *name = sipe_get_no_sip_uri(uri); - add_new_buddy(sipe_private, item, uri, name); + add_new_buddy(sipe_private, + item, + sipe_xml_attribute(item, "uri")); } /* Process modified buddies */ diff --git a/src/core/sipe-ucs.c b/src/core/sipe-ucs.c index 926928b5..918ff7a3 100644 --- a/src/core/sipe-ucs.c +++ b/src/core/sipe-ucs.c @@ -325,29 +325,12 @@ static void sipe_ucs_get_im_item_list_response(struct sipe_core_private *sipe_pr struct sipe_buddy *buddy = sipe_buddy_find_by_exchange_key(sipe_private, sipe_xml_attribute(member_node, "Id")); - if (buddy) { - sipe_backend_buddy b = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, - buddy->name, - group->name); - - if (!b) { - b = sipe_backend_buddy_add(SIPE_CORE_PUBLIC, - buddy->name, - /* alias will be set via buddy presence update */ - NULL, - group->name); - SIPE_DEBUG_INFO("Created new buddy %s", buddy->name); - } - - } - - buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups, - group, - (GCompareFunc) sipe_group_compare, - NULL); - - SIPE_DEBUG_INFO("Added buddy %s to group %s", - buddy->name, group->name); + if (buddy) + sipe_buddy_add_to_group(sipe_private, + buddy, + group, + /* alias will be set via buddy presence update */ + NULL); } } -- 2.11.4.GIT