From 28fbc4e0d124d6c3d41cfd970ad699fe48555182 Mon Sep 17 00:00:00 2001 From: Sergio Villar Senin Date: Mon, 22 Jun 2009 16:19:36 +0200 Subject: [PATCH] Fixes NB#123812, duplicate contacts created when inserting the same email address many times --- src/hildon2/modest-address-book.c | 9 ++++++--- src/modest-text-utils.c | 36 ++++++++++++++++++++++++++++++++++++ src/modest-text-utils.h | 11 +++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/hildon2/modest-address-book.c b/src/hildon2/modest-address-book.c index 9f11f09..160bd89 100644 --- a/src/hildon2/modest-address-book.c +++ b/src/hildon2/modest-address-book.c @@ -784,7 +784,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat start_pos = (*((gint*) current_start->data)) + offset_delta; end_pos = (*((gint*) current_end->data)) + offset_delta; - + start_ptr = g_utf8_offset_to_pointer (recipients, start_pos); end_ptr = g_utf8_offset_to_pointer (recipients, end_pos); @@ -890,8 +890,11 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat } /* Add addresses to address-book */ - if (to_commit_addresses) - add_to_address_book (to_commit_addresses); + if (to_commit_addresses) { + to_commit_addresses = modest_text_utils_remove_duplicate_addresses_list (to_commit_addresses); + if (to_commit_addresses) + add_to_address_book (to_commit_addresses); + } if (current_start == NULL) { gtk_text_buffer_get_end_iter (buffer, &end_iter); diff --git a/src/modest-text-utils.c b/src/modest-text-utils.c index 2223fd1..2b7cbb5 100644 --- a/src/modest-text-utils.c +++ b/src/modest-text-utils.c @@ -1955,3 +1955,39 @@ modest_text_utils_simplify_recipients (const gchar *recipients) return g_string_free (result, FALSE); } + +GSList * +modest_text_utils_remove_duplicate_addresses_list (GSList *address_list) +{ + GSList *new_list, *iter; + GHashTable *table; + + g_return_val_if_fail (address_list, NULL); + + table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); + + new_list = address_list; + iter = address_list; + while (iter) { + const gchar* address = (const gchar*)iter->data; + + /* We need only the email to just compare it and not + the full address which would make "a " + different from "a@a.com" */ + const gchar *email = get_email_from_address (address); + + /* ignore the address if already seen */ + if (g_hash_table_lookup (table, email) == 0) { + g_hash_table_insert (table, (gchar*)email, GINT_TO_POINTER(1)); + iter = g_slist_next (iter); + } else { + GSList *tmp = g_slist_next (iter); + new_list = g_slist_delete_link (new_list, iter); + iter = tmp; + } + } + + g_hash_table_unref (table); + + return new_list; +} diff --git a/src/modest-text-utils.h b/src/modest-text-utils.h index d881f00..3608935 100644 --- a/src/modest-text-utils.h +++ b/src/modest-text-utils.h @@ -504,4 +504,15 @@ gchar *modest_text_utils_escape_mnemonics (const gchar *text); */ gchar *modest_text_utils_simplify_recipients (const gchar *recipient); +/** + * modest_text_utils_remove_duplicate_addresses_list + * @address_list: non-NULL #GSList of email addresses + * + * remove duplicate addresses from a list of email addresses + * + * Returns: a list without the duplicate addresses or NULL in case of + * error or the original @address_list was NULL + */ +GSList *modest_text_utils_remove_duplicate_addresses_list (GSList *address_list); + #endif /* __MODEST_TEXT_UTILS_H__ */ -- 1.7.9.5