Better parsing support
[modest] / src / hildon2 / modest-address-book.c
index f3f21ec..4e19d89 100644 (file)
@@ -38,6 +38,7 @@
 #include <libebook/e-vcard.h>
 #include "modest-hildon-includes.h"
 #include <libosso-abook/osso-abook.h>
+#include <libedataserver/e-data-server-util.h>
 #include "modest-platform.h"
 #include "modest-runtime.h"
 #include "widgets/modest-window-mgr.h"
@@ -104,7 +105,8 @@ open_addressbook ()
 }
 
 void
-modest_address_book_add_address (const gchar *address)
+modest_address_book_add_address (const gchar *address,
+                                GtkWindow *parent)
 {
        GtkWidget *dialog = NULL;
        gchar *email_address;
@@ -115,10 +117,11 @@ modest_address_book_add_address (const gchar *address)
        }
 
        email_address = modest_text_utils_get_email_address (address);
-       
+
        attribute = e_vcard_attribute_new (NULL, EVC_EMAIL);
        e_vcard_attribute_add_value (attribute, email_address);
-       dialog = osso_abook_temporary_contact_dialog_new (NULL, book, attribute, NULL);
+       dialog = osso_abook_temporary_contact_dialog_new (parent, book, attribute, NULL);
+
        gtk_dialog_run (GTK_DIALOG (dialog));
 
        gtk_widget_destroy (dialog);
@@ -239,7 +242,8 @@ get_recipients_for_given_contact (EContact * contact,
 
                emailid = get_email_addr_from_user(display_name, canceled);
                if (emailid) {
-                       e_contact_set(E_CONTACT (abook_contact), E_CONTACT_EMAIL_1, emailid);
+                       list = g_list_append (list, g_strdup (emailid));
+                       e_contact_set(E_CONTACT (abook_contact), E_CONTACT_EMAIL, list);
                        osso_abook_contact_commit (abook_contact, FALSE, NULL, NULL);
                }
                g_object_unref (abook_contact);
@@ -336,10 +340,9 @@ ui_get_formatted_email_id(gchar * current_given_name,
        } else if ((current_sur_name != NULL) && (strlen(current_sur_name) != 0)) {
                g_string_append_printf(email_id_str, "%s", current_sur_name);
        }
-       if (g_utf8_strchr (email_id_str->str, -1, ' ')) {
-               g_string_prepend_c (email_id_str, '\"');
-               g_string_append_c (email_id_str, '\"');
-       }
+       g_string_prepend_c (email_id_str, '\"');
+       g_string_append_c (email_id_str, '\"');
+
        g_string_append_printf (email_id_str, " %c%s%c", '<', current_email_id, '>');
        return g_string_free (email_id_str, FALSE);
 }
@@ -496,11 +499,16 @@ compare_addresses (const gchar *address1,
                   const gchar *mail2)
 {
        gint retval;
-       gchar *mail1;
+       gchar *mail1, *mail1_down, *mail2_down;
 
+       /* Perform a case insensitive comparison */
        mail1 = modest_text_utils_get_email_address (address1);
-       retval = g_strcmp0 (mail1, mail2);
+       mail1_down = g_ascii_strdown (mail1, -1);
+       mail2_down = g_ascii_strdown (mail2, -1);
+       retval = g_strcmp0 (mail1_down, mail2_down);
        g_free (mail1);
+       g_free (mail1_down);
+       g_free (mail2_down);
 
        return retval;
 }
@@ -576,12 +584,15 @@ async_get_contacts_cb (EBook *book,
                        g_debug ("----Preparing to commit contact %s", address);
                } else {
                        gchar *email_address, *display_address;
+                       GList *email_list = NULL;
 
                        /* 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);
+                       email_list = g_list_append (email_list, email_address);
+                       e_contact_set (contact, E_CONTACT_EMAIL, email_list);
                        g_free (email_address);
+                       g_list_free (email_list);
 
                        display_address = g_strdup (address);
                        if (display_address) {
@@ -619,40 +630,6 @@ async_get_contacts_cb (EBook *book,
                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);
-
-       /* Frees. This will unref the subqueries as well */
-       e_book_query_unref (composite_query);
-}
-
 typedef struct _CheckNamesInfo {
        GtkWidget *banner;
        guint show_banner_timeout;
@@ -744,11 +721,12 @@ void free_resolved_addresses_list (gpointer data,
 }
 
 gboolean
-modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean update_addressbook)
+modest_address_book_check_names (ModestRecptEditor *recpt_editor,
+                                GSList **address_list)
 {
        const gchar *recipients = NULL;
        GSList *start_indexes = NULL, *end_indexes = NULL;
-       GSList *current_start, *current_end, *to_commit_addresses;
+       GSList *current_start, *current_end;
        gboolean result = TRUE;
        GtkTextBuffer *buffer;
        gint offset_delta = 0;
@@ -773,7 +751,6 @@ 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;
@@ -784,7 +761,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);
 
@@ -862,14 +839,14 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat
                        }
                        g_slist_free (tags);
                        if (!has_recipient) {
-                               GSList * address_list = NULL;
+                               GSList * addr_list = NULL;
 
-                               address_list = g_slist_prepend (address_list, address);
+                               addr_list = g_slist_prepend (addr_list, address);
                                modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
                                                                                     &start_iter, &end_iter,
-                                                                                    address_list, 
+                                                                                    addr_list,
                                                                                     "");
-                               g_slist_free (address_list);
+                               g_slist_free (addr_list);
                                store_address = TRUE;
                        }
                }
@@ -877,8 +854,8 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat
                /* so, it seems a valid address */
                /* 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)
-                       to_commit_addresses = g_slist_prepend (to_commit_addresses, address);
+               if (result && address_list && store_address)
+                       *address_list = g_slist_prepend (*address_list, address);
                else
                        g_free (address);
 
@@ -889,9 +866,9 @@ 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);
+       /* Remove dup's */
+       if (address_list && *address_list)
+               *address_list = modest_text_utils_remove_duplicate_addresses_list (*address_list);
 
        if (current_start == NULL) {
                gtk_text_buffer_get_end_iter (buffer, &end_iter);
@@ -929,77 +906,96 @@ get_contacts_for_name_cb (EBook *book,
 static GList *
 get_contacts_for_name (const gchar *name)
 {
-       EBookQuery *full_name_book_query = NULL;
+       EBookQuery *book_query = NULL;
        GList *result;
        gchar *unquoted;
        GetContactsInfo *info;
+       EBookQuery *queries[10];
 
        if (name == NULL)
                return NULL;
 
        unquoted = unquote_string (name);
-       full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted);
+
+       queries[0] = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[1] = e_book_query_field_test (E_CONTACT_GIVEN_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[2] = e_book_query_field_test (E_CONTACT_FAMILY_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[3] = e_book_query_field_test (E_CONTACT_NICKNAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[4] = e_book_query_field_test (E_CONTACT_EMAIL_1, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[5] = e_book_query_field_test (E_CONTACT_EMAIL_2, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[6] = e_book_query_field_test (E_CONTACT_EMAIL_3, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[7] = e_book_query_field_test (E_CONTACT_EMAIL_4, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[8] = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[9] = e_book_query_field_test (E_CONTACT_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       book_query = e_book_query_or (10, queries, TRUE);
+
        g_free (unquoted);
 
        /* TODO: Make it launch a mainloop */
        info = g_slice_new (GetContactsInfo);
        info->mainloop = g_main_loop_new (NULL, FALSE);
        info->result = NULL;
-       if (e_book_async_get_contacts (book, full_name_book_query, get_contacts_for_name_cb, info) == 0) {
+       if (e_book_async_get_contacts (book, book_query, get_contacts_for_name_cb, info) == 0) {
                GDK_THREADS_LEAVE ();
                g_main_loop_run (info->mainloop);
                GDK_THREADS_ENTER ();
        } 
        result = info->result;
-       e_book_query_unref (full_name_book_query);
+       e_book_query_unref (book_query);
        g_main_loop_unref (info->mainloop);
        g_slice_free (GetContactsInfo, info);
 
        return result;
 }
 
-static gboolean
-filter_by_name (OssoABookContactChooser *chooser,
-               OssoABookContact        *contact,
-               gpointer                 user_data)
-{
-       const gchar *contact_name;
-       const gchar *name = (const gchar *) user_data;
-
-       contact_name = osso_abook_contact_get_name (contact);
-       /* contact_name includes both name and surname */
-       if (contact_name && name && strstr (contact_name, name))
-               return TRUE;
-       else
-               return FALSE;
-}
 
 static GList *
 select_contacts_for_name_dialog (const gchar *name)
 {
-       GtkWidget *contact_view;
-       OssoABookContactChooser *contact_dialog;
+       EBookQuery *book_query = NULL;
+       EBookView *book_view = NULL;
        GList *result = NULL;
        gchar *unquoted;
+       EBookQuery *queries[10];
 
-       contact_dialog = (OssoABookContactChooser *)
-               osso_abook_contact_chooser_new_with_capabilities (NULL,
-                                                                 _AB("addr_ti_dia_select_contacts"),
-                                                                 OSSO_ABOOK_CAPS_EMAIL,
-                                                                 OSSO_ABOOK_CONTACT_ORDER_NAME);
-
-       /* Enable multiselection */
-       osso_abook_contact_chooser_set_maximum_selection (contact_dialog, G_MAXUINT);
-
-       /* Set up the filtering */
        unquoted = unquote_string (name);
-       contact_view = osso_abook_contact_chooser_get_contact_view (contact_dialog);
-       osso_abook_contact_chooser_set_model (contact_dialog, contact_model);
-       osso_abook_contact_chooser_set_visible_func (contact_dialog, filter_by_name, unquoted, NULL);
-
-       if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK)
-               result = osso_abook_contact_chooser_get_selection (contact_dialog);
 
+       queries[0] = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[1] = e_book_query_field_test (E_CONTACT_GIVEN_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[2] = e_book_query_field_test (E_CONTACT_FAMILY_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[3] = e_book_query_field_test (E_CONTACT_NICKNAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[4] = e_book_query_field_test (E_CONTACT_EMAIL_1, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[5] = e_book_query_field_test (E_CONTACT_EMAIL_2, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[6] = e_book_query_field_test (E_CONTACT_EMAIL_3, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[7] = e_book_query_field_test (E_CONTACT_EMAIL_4, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[8] = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[9] = e_book_query_field_test (E_CONTACT_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       book_query = e_book_query_or (10, queries, TRUE);
+       e_book_get_book_view (book, book_query, NULL, -1, &book_view, NULL);
+       e_book_query_unref (book_query);
+
+       if (book_view) {
+               GtkWidget *contact_dialog = NULL;
+               osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model), book_view);
+               e_book_view_start (book_view);
+
+               /* TODO: figure out how to make the contact chooser modal */
+               contact_dialog = osso_abook_contact_chooser_new_with_capabilities (NULL,
+                                                                                  _AB("addr_ti_dia_select_contacts"),
+                                                                                  OSSO_ABOOK_CAPS_EMAIL,
+                                                                                  OSSO_ABOOK_CONTACT_ORDER_NAME);
+               /* Enable multiselection */
+               osso_abook_contact_chooser_set_maximum_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
+                                                                 G_MAXUINT);
+               osso_abook_contact_chooser_set_model (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
+                                                     contact_model);
+
+               if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK)
+                       result = osso_abook_contact_chooser_get_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog));
+               e_book_view_stop (book_view);
+               g_object_unref (book_view);
+               gtk_widget_destroy (contact_dialog);
+       }
        g_free (unquoted);
 
        return result;
@@ -1022,7 +1018,7 @@ resolve_address (const gchar *address,
        info = g_slice_new0 (CheckNamesInfo);
        show_check_names_banner (info);
 
-       contact_model = osso_abook_contact_model_new ();
+       contact_model = osso_abook_contact_model_get_default ();
        if (!open_addressbook ()) {
                hide_check_names_banner (info);
                if (contact_model) {
@@ -1171,3 +1167,36 @@ modest_address_book_init (void)
 {
        open_addressbook ();
 }
+
+void
+modest_address_book_add_address_list (GSList *address_list)
+{
+       EBookQuery **queries, *composite_query;
+       gint num_add, i;
+
+       g_return_if_fail (address_list);
+
+       if (!book)
+               if (!open_addressbook ())
+                       g_return_if_reached ();
+
+       /* Create the list of queries */
+       num_add = g_slist_length (address_list);
+       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 (address_list, 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, address_list);
+
+       /* Frees. This will unref the subqueries as well */
+       e_book_query_unref (composite_query);
+}