Fixes NB#123812, duplicate contacts created when inserting the same email address...
authorSergio Villar Senin <svillar@igalia.com>
Mon, 22 Jun 2009 14:19:36 +0000 (16:19 +0200)
committerSergio Villar Senin <svillar@igalia.com>
Mon, 22 Jun 2009 14:19:36 +0000 (16:19 +0200)
src/hildon2/modest-address-book.c
src/modest-text-utils.c
src/modest-text-utils.h

index 9f11f09..160bd89 100644 (file)
@@ -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);
index 2223fd1..2b7cbb5 100644 (file)
@@ -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 <a@a.com>"
+                  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;
+}
index d881f00..3608935 100644 (file)
@@ -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__ */