Me card contact is normally a OssoABookContact and not a EContact
[modest] / src / hildon2 / modest-address-book.c
index 6a057ce..b26a597 100644 (file)
@@ -226,7 +226,8 @@ modest_address_book_add_address (const gchar *address)
 }
 
 void
-modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
+modest_address_book_select_addresses (ModestRecptEditor *recpt_editor,
+                                     GtkWindow *parent_window)
 {
 #if MODEST_ABOOK_API < 4
        GtkWidget *contact_view = NULL;
@@ -269,7 +270,7 @@ modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
        }
 #else /* MODEST_ABOOK_API < 4 */
        /* TODO: figure out how to make the contact chooser modal */
-       contact_chooser = osso_abook_contact_chooser_new_with_capabilities (NULL,
+       contact_chooser = osso_abook_contact_chooser_new_with_capabilities (parent_window,
                                                                            _AB("addr_ti_dia_select_contacts"),
                                                                            OSSO_ABOOK_CAPS_EMAIL, 
                                                                            OSSO_ABOOK_CONTACT_ORDER_NAME);
@@ -445,10 +446,18 @@ commit_contact(EContact * contact, gboolean is_new)
        if (OSSO_ABOOK_IS_CONTACT (contact)) {
                osso_abook_contact_commit(OSSO_ABOOK_CONTACT(contact), is_new, book, NULL);
        } else {
-               if (is_new)
-                       e_book_add_contact (book, contact, NULL);
-               else
-                       e_book_commit_contact (book, contact, NULL);
+               GError *err = NULL;
+               if (is_new) {
+                       if (!e_book_add_contact (book, contact, &err)) {
+                               g_warning ("Failed to add contact: %s", err->message);
+                               g_error_free (err);
+                       }
+               } else {
+                       if (!e_book_commit_contact (book, contact, &err)) {
+                               g_warning ("Failed to commit contact: %s", err->message);
+                               g_error_free (err);
+                       }
+               }
        }
 #endif /* MODEST_ABOOK_API < 2 */
 }
@@ -591,6 +600,7 @@ run_add_email_addr_to_contact_dlg(const gchar * contact_name,
                                        gchar *message = g_strdup_printf(
                                                _CS("ckdg_ib_illegal_characters_entered"), 
                                                char_in_string);
+                                       g_free (char_in_string);
                                        hildon_banner_show_information (
                                                add_email_addr_to_contact_dlg, NULL, message );
                                        g_free (message);
@@ -646,7 +656,7 @@ select_email_addrs_for_contact(GList * email_addr_list)
        hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (select_email_addr_dlg),
                                           HILDON_TOUCH_SELECTOR (selector));
        gtk_window_set_default_size (GTK_WINDOW (select_email_addr_dlg), MODEST_DIALOG_WINDOW_MAX_HEIGHT, -1);
-       
+
        gtk_widget_show_all(select_email_addr_dlg);
        result = gtk_dialog_run(GTK_DIALOG(select_email_addr_dlg));
 
@@ -655,73 +665,176 @@ select_email_addrs_for_contact(GList * email_addr_list)
 
                current_text = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector));
                selected_email_addr_list = g_slist_append (selected_email_addr_list, current_text);
-               
        }
 
        gtk_widget_destroy(select_email_addr_dlg);
        return selected_email_addr_list;
 }
 
+/* Assumes that the second argument (the user provided one) is a pure
+   email address without name */
+static gint
+compare_addresses (const gchar *address1,
+                  const gchar *mail2)
+{
+       gint retval;
+       gchar *mail1;
+
+       mail1 = modest_text_utils_get_email_address (address1);
+       retval = g_strcmp0 (mail1, mail2);
+       g_free (mail1);
+
+       return retval;
+}
 
-static gboolean /* make this public? */
-add_to_address_book (const gchar* address)
+static EContact *
+get_contact_for_address (GList *contacts,
+                        const gchar *address)
 {
-       EBookQuery *query;
-       GList *contacts = NULL;
-       GError *err = NULL;
+       EContact *retval = NULL, *contact;
+       GList *iter;
        gchar *email;
-       
-       g_return_val_if_fail (address, FALSE);
-       
-       if (!book) {
-               if (!open_addressbook ()) {
-                       g_return_val_if_reached (FALSE);
+
+       email = modest_text_utils_get_email_address (address);
+       iter = contacts;
+       while (iter && !retval) {
+               GList *emails = NULL;
+
+               contact = E_CONTACT (iter->data);
+               emails = e_contact_get (contact, E_CONTACT_EMAIL);
+               if (emails) {
+                       /* Look for the email address */
+                       if (g_list_find_custom (emails, email, (GCompareFunc) compare_addresses))
+                               retval = contact;
+
+                       /* Free the list */
+                       g_list_foreach (emails, (GFunc) g_free, NULL);
+                       g_list_free (emails);
                }
+               iter = g_list_next (iter);
        }
-       
-       g_return_val_if_fail (book, FALSE);
+       g_free (email);
 
-       email = modest_text_utils_get_email_address (address);
-       
-       query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, email);
-       if (!e_book_get_contacts (book, query, &contacts, &err)) {
-               g_printerr ("modest: failed to get contacts: %s",
-                           err ? err->message : "<unknown>");
-               if (err)
-                       g_error_free (err);
-               return FALSE;
+       return retval;
+}
+
+static void
+async_get_contacts_cb (EBook *book,
+                      EBookStatus status,
+                      GList *contacts,
+                      gpointer closure)
+{
+       GSList *addresses, *iter;
+       GList *to_commit_contacts, *to_add_contacts;
+       EContact *self_contact;
+
+       addresses = (GSList *) closure;
+
+       /* Check errors */
+       if (status != E_BOOK_ERROR_OK)
+               goto frees;
+
+       self_contact = (EContact *) osso_abook_self_contact_get_default ();
+       if (self_contact) {
+               contacts = g_list_prepend (contacts, self_contact);
        }
-       e_book_query_unref (query);
-       
-       /*  we need to 'commit' it, even if we already found the email
-        * address in the addressbook; thus, it will show up in the 'recent list' */
-       if (contacts)  {                
-               g_debug ("%s already in the address book", address);
-               commit_contact ((EContact*)contacts->data, FALSE);
-               
-               g_list_foreach (contacts, (GFunc)unref_gobject, NULL);
-               g_list_free (contacts);
 
-       } else {
-               /* it's not yet in the addressbook, add it now! */
-               EContact *new_contact = e_contact_new ();
-               gchar *display_address;
-               display_address = g_strdup (address);
-               if (display_address) {
-                       modest_text_utils_get_display_address (display_address);
-                       if ((display_address[0] != '\0') && (strlen (display_address) != strlen (address)))
-                               e_contact_set (new_contact, E_CONTACT_FULL_NAME, (const gpointer)display_address);
+       iter = addresses;
+       to_commit_contacts = NULL;
+       to_add_contacts = NULL;
+       while (iter) {
+               EContact *contact;
+               const gchar *address;
+
+               /* Look for a contact with such address. We perform
+                  this kind of search because we assume that users
+                  don't usually send emails to tons of addresses */
+               address = (const gchar *) iter->data;
+               contact = get_contact_for_address (contacts, address);
+
+               /* Add new or commit existing contact */
+               if (contact) {
+                       to_commit_contacts = g_list_prepend (to_commit_contacts, contact);
+                       g_debug ("----Preparing to commit contact %s", address);
+               } else {
+                       gchar *email_address, *display_address;
+
+                       /* Create new contact and add it to the list */
+                       contact = e_contact_new ();
+                       email_address = modest_text_utils_get_email_address (address);
+                       e_contact_set (contact, E_CONTACT_EMAIL_1, email_address);
+                       g_free (email_address);
+
+                       display_address = g_strdup (address);
+                       if (display_address) {
+                               modest_text_utils_get_display_address (display_address);
+                               if ((display_address[0] != '\0') && (strlen (display_address) != strlen (address)))
+                                       e_contact_set (contact, E_CONTACT_FULL_NAME, (const gpointer)display_address);
+                               g_free (display_address);
+                       }
+
+                       to_add_contacts = g_list_prepend (to_add_contacts, contact);
+                       g_debug ("----Preparing to add contact %s", address);
                }
-               e_contact_set (new_contact, E_CONTACT_EMAIL_1, (const gpointer)email);
-               g_free (display_address);
-               commit_contact (new_contact, TRUE);
-               g_debug ("%s added to address book", address);
-               g_object_unref (new_contact);
+
+               iter = g_slist_next (iter);
        }
 
-       g_free (email);
+       /* Asynchronously add contacts */
+       if (to_add_contacts)
+               e_book_async_add_contacts (book, to_add_contacts, NULL, NULL);
+
+       /* Asynchronously commit contacts */
+       if (to_commit_contacts)
+               e_book_async_commit_contacts (book, to_commit_contacts, NULL, NULL);
+
+       /* Free lists */
+       g_list_free (to_add_contacts);
+       g_list_free (to_commit_contacts);
+
+ frees:
+       if (addresses) {
+               g_slist_foreach (addresses, (GFunc) g_free, NULL);
+               g_slist_free (addresses);
+       }
+       if (contacts) {
+               g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
+               g_list_free (contacts);
+       }
+}
+
+
+static void
+add_to_address_book (GSList *addresses)
+{
+       EBookQuery **queries, *composite_query;
+       gint num_add, i;
+
+       g_return_if_fail (addresses);
+
+       if (!book)
+               if (!open_addressbook ())
+                       g_return_if_reached ();
+
+       /* Create the list of queries */
+       num_add = g_slist_length (addresses);
+       queries = g_malloc0 (sizeof (EBookQuery *) * num_add);
+       for (i = 0; i < num_add; i++) {
+               gchar *email;
+
+               email = modest_text_utils_get_email_address (g_slist_nth_data (addresses, i));
+               queries[i] = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, email);
+               g_free (email);
+       }
+
+       /* Create the query */
+       composite_query = e_book_query_or (num_add, queries, TRUE);
+
+       /* Asynchronously retrieve contacts */
+       e_book_async_get_contacts (book, composite_query, async_get_contacts_cb, addresses);
 
-       return TRUE;
+       /* Frees. This will unref the subqueries as well */
+       e_book_query_unref (composite_query);
 }
 
 typedef struct _CheckNamesInfo {
@@ -806,12 +919,20 @@ static void clean_check_names_banner (CheckNamesInfo *info)
        }
 }
 
+void free_resolved_addresses_list (gpointer data,
+                                  gpointer ignored)
+{
+       GSList *list = (GSList *)data;
+       g_slist_foreach (list, (GFunc) g_free, NULL);
+       g_slist_free (list);
+}
+
 gboolean
 modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean update_addressbook)
 {
        const gchar *recipients = NULL;
        GSList *start_indexes = NULL, *end_indexes = NULL;
-       GSList *current_start, *current_end;
+       GSList *current_start, *current_end, *to_commit_addresses;
        gboolean result = TRUE;
        GtkTextBuffer *buffer;
        gint offset_delta = 0;
@@ -836,6 +957,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat
        current_start = start_indexes;
        current_end = end_indexes;
        buffer = modest_recpt_editor_get_buffer (recpt_editor);
+       to_commit_addresses = NULL;
 
        while (current_start != NULL) {
                gchar *address;
@@ -875,30 +997,13 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat
 
                                if (result) {
                                        gint new_length;
-                                       GSList *contact_ids_node, *resolved_addresses_node;
 
-                                       contact_ids_node = contact_ids;
-                                       resolved_addresses_node = resolved_addresses;
-
-                                       while (contact_ids_node != NULL) {
-                                               gchar *contact_id = (gchar *) contact_ids_node->data;
-                                               GSList *resolved_addresses_for_contact = 
-                                                       (GSList *) resolved_addresses_node->data;
-
-                                               /* replace string */
-                                               modest_recpt_editor_replace_with_resolved_recipient 
-                                                       (recpt_editor,
-                                                        &start_iter, &end_iter,
-                                                        resolved_addresses_for_contact, 
-                                                        contact_id);
-
-                                               g_free (contact_id);
-                                               g_slist_foreach (resolved_addresses_for_contact, (GFunc) g_free, NULL);
-                                               g_slist_free (resolved_addresses_for_contact);
-
-                                               contact_ids_node = g_slist_next (contact_ids_node);
-                                               resolved_addresses_node = g_slist_next (resolved_addresses_node);
-                                       }
+                                       modest_recpt_editor_replace_with_resolved_recipients (recpt_editor,
+                                                                                             &start_iter, &end_iter,
+                                                                                             resolved_addresses,
+                                                                                             contact_ids);
+                                       g_slist_foreach (contact_ids, (GFunc) g_free, NULL);
+                                       g_slist_foreach (resolved_addresses, free_resolved_addresses_list, NULL);
                                        g_slist_free (contact_ids);
                                        g_slist_free (resolved_addresses);
 
@@ -907,12 +1012,20 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat
                                        new_length = g_utf8_strlen (recipients, -1);
                                        offset_delta = offset_delta + new_length - last_length;
                                        last_length = new_length;
-                               } else if (canceled) {
-                                       /* We have to remove the recipient if not resolved */
-                                       modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
-                                                                                            &start_iter, &end_iter,
-                                                                                            NULL,
-                                                                                            NULL);
+                               } else {
+                                       if (canceled) {
+                                               /* We have to remove the recipient if not resolved */
+                                               modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
+                                                                                                    &start_iter, 
+                                                                                                    &end_iter,
+                                                                                                    NULL,
+                                                                                                    NULL);
+                                       } else {
+                                               /* There is no contact with that name so it's not
+                                                  valid. Don't show any error because it'll be done
+                                                  later */
+                                               result = FALSE;
+                                       }
                                }
                        } else {
                                /* this address is not valid, select it and return control to user showing banner */
@@ -949,9 +1062,10 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat
                /* note: adding it the to the addressbook if it did not exist yet,
                 * and adding it to the recent_list */
                if (result && update_addressbook && store_address)
-                       add_to_address_book (address);
+                       to_commit_addresses = g_slist_prepend (to_commit_addresses, address);
+               else
+                       g_free (address);
 
-               g_free (address);
                if (result == FALSE)
                        break;
 
@@ -959,6 +1073,10 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat
                current_end = g_slist_next (current_end);
        }
 
+       /* Add addresses to address-book */
+       if (to_commit_addresses)
+               add_to_address_book (to_commit_addresses);
+
        if (current_start == NULL) {
                gtk_text_buffer_get_end_iter (buffer, &end_iter);
                gtk_text_buffer_place_cursor (buffer, &end_iter);
@@ -1107,9 +1225,9 @@ resolve_address (const gchar *address,
                clean_check_names_banner (info);
                return FALSE;
        }
-       hide_check_names_banner (info);
 
        resolved_contacts = get_contacts_for_name (address);
+       hide_check_names_banner (info);
 
        if (resolved_contacts == NULL) {
                /* no matching contacts for the search string */
@@ -1216,6 +1334,8 @@ modest_address_book_has_address (const gchar *address)
                            err ? err->message : "<unknown>");
                if (err)
                        g_error_free (err);
+               g_free (email);
+               e_book_query_unref (query);
                return FALSE;
        }
        e_book_query_unref (query);
@@ -1230,3 +1350,20 @@ modest_address_book_has_address (const gchar *address)
 
        return result;
 }
+
+const gchar *
+modest_address_book_get_my_name ()
+{
+       OssoABookSelfContact *self_contact = osso_abook_self_contact_get_default ();
+
+       /* We are not using osso_abook_contact_get_display_name
+          because that method fallbacks to another fields if the name
+          is not defined */
+       if (self_contact)
+               if (OSSO_ABOOK_IS_CONTACT (self_contact))
+                       return osso_abook_contact_get_name ((OssoABookContact*)self_contact);
+               else
+                       return e_contact_get ((EContact *) self_contact, E_CONTACT_NAME);
+       else
+               return NULL;
+}