Revert "Query activesync for addressbooks too (if available)."
[modest] / src / hildon2 / modest-address-book.c
index 1256dd6..5cbc826 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"
@@ -63,6 +64,7 @@ static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name, gboo
 static GSList *select_email_addrs_for_contact(GList * email_addr_list);
 static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, GSList **contact_id, gboolean *canceled);
 static gchar *unquote_string (const gchar *str);
+static void set_contact_from_display_name (EContact *contact, const gchar *display_name);
 
 static gboolean
 open_addressbook ()
@@ -241,7 +243,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);
@@ -338,10 +341,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);
 }
@@ -498,11 +500,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;
 }
@@ -578,18 +585,21 @@ 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) {
                                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);
+                                       set_contact_from_display_name (contact, (const gchar *) display_address);
                                g_free (display_address);
                        }
 
@@ -897,92 +907,120 @@ 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];
+       gint i;
 
        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);
+
+       i = 0;
+       queries[i++] = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[i++] = e_book_query_field_test (E_CONTACT_GIVEN_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[i++] = e_book_query_field_test (E_CONTACT_FAMILY_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       queries[i++] = e_book_query_field_test (E_CONTACT_NICKNAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       if (strchr (name, '@')) {
+               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL_1, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL_2, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL_3, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL_4, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       }
+       queries[i] = e_book_query_field_test (E_CONTACT_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+       book_query = e_book_query_or (i, 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;
 }
 
-#ifdef HAVE_OSSO_ABOOK_CONTACT_CHOOSER_SET_VISIBLE_FUNC
-static gboolean
-filter_by_name (OssoABookContactChooser *chooser,
-               OssoABookContact        *contact,
-               gpointer                 user_data)
+static void
+set_contact_from_display_name (EContact *contact, const gchar *disp_name)
 {
-       const gchar *contact_name;
-       const gchar *name = (const gchar *) user_data;
+       const gchar *parent_open;
+       const gchar *comma_separator;
+       GString *buffer;
+       gchar *display_name;
 
-       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;
+       display_name = unquote_string (disp_name);
+       buffer = g_string_new ("");
+
+       /* First we remove part in () */
+       parent_open = g_strstr_len (display_name, -1, "(");
+       if (parent_open) {
+               const gchar *parent_close;
+
+               parent_close = g_strstr_len (parent_open, -1, ")");
+
+               buffer = g_string_append_len (buffer, display_name, parent_open - display_name);
+               if (parent_close) {
+                       buffer = g_string_append (buffer, parent_close + 1);
+               }
+       } else {
+               buffer = g_string_append (buffer, display_name);
+       }
+
+       comma_separator = g_strstr_len (buffer->str, -1, ", ");
+       if (comma_separator) {
+               gchar *surname, *name;
+               surname = g_strndup (buffer->str, comma_separator - buffer->str);
+               name = g_strdup (comma_separator + 2);
+
+               e_contact_set (contact, E_CONTACT_FAMILY_NAME, (const gpointer) surname);
+               e_contact_set (contact, E_CONTACT_GIVEN_NAME, (const gpointer) name);
+               g_free (name);
+               g_free (surname);
+       } else {
+               e_contact_set (contact, E_CONTACT_GIVEN_NAME, (const gpointer) buffer->str);
+       }
+
+       g_string_free (buffer, TRUE);
+       g_free (display_name);
 }
-#endif
 
 static GList *
 select_contacts_for_name_dialog (const gchar *name)
 {
-#ifdef HAVE_OSSO_ABOOK_CONTACT_CHOOSER_SET_VISIBLE_FUNC
-       GtkWidget *contact_view;
-       OssoABookContactChooser *contact_dialog;
-#else
-       EBookQuery *full_name_book_query = NULL;
+       EBookQuery *book_query = NULL;
        EBookView *book_view = NULL;
-#endif
-
        GList *result = NULL;
        gchar *unquoted;
+       EBookQuery *queries[10];
 
        unquoted = unquote_string (name);
 
-#ifdef HAVE_OSSO_ABOOK_CONTACT_CHOOSER_SET_VISIBLE_FUNC
-       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 */
-       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);
-
-       gtk_widget_destroy ((GtkWidget *) contact_dialog);
-#else
-       full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted);
-       e_book_get_book_view (book, full_name_book_query, NULL, -1, &book_view, NULL);
-       e_book_query_unref (full_name_book_query);
+       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;
@@ -1006,8 +1044,6 @@ select_contacts_for_name_dialog (const gchar *name)
                g_object_unref (book_view);
                gtk_widget_destroy (contact_dialog);
        }
-#endif
-
        g_free (unquoted);
 
        return result;
@@ -1212,3 +1248,91 @@ modest_address_book_add_address_list (GSList *address_list)
        /* Frees. This will unref the subqueries as well */
        e_book_query_unref (composite_query);
 }
+
+static void
+selector_on_response (GtkDialog *dialog,
+                     gint       response_id,
+                     gpointer   user_data)
+{
+       if (response_id == GTK_RESPONSE_OK) {
+               gchar *current_selection = NULL;
+               GtkTreePath *selected_row = NULL;
+               HildonTouchSelector *selector;
+
+               selector = hildon_picker_dialog_get_selector (HILDON_PICKER_DIALOG (dialog));
+               selected_row = hildon_touch_selector_get_last_activated_row (selector, 0);
+               if (selected_row) {
+                       GtkTreeIter iter;
+                       GtkTreeModel *model = hildon_touch_selector_get_model (selector, 0);
+                       if (gtk_tree_model_get_iter (model, &iter, selected_row)) {
+                               gtk_tree_model_get (model, &iter, 0, &current_selection, -1);
+                               modest_address_book_add_address (current_selection, user_data);
+                               g_debug ("Current selection : %s", current_selection);
+                               g_free (current_selection);
+                       }
+               }
+       }
+
+       if (response_id != GTK_RESPONSE_DELETE_EVENT)
+               gtk_widget_destroy ((GtkWidget *) dialog);
+}
+
+static void
+selector_selection_changed (HildonTouchSelector * selector,
+                           gint column,
+                           gpointer *user_data)
+{
+       /* Close the dialog */
+       gtk_dialog_response (GTK_DIALOG (user_data), GTK_RESPONSE_OK);
+}
+
+void
+modest_address_book_add_address_list_with_selector (GSList *address_list, GtkWindow *parent)
+{
+       GtkWidget *picker_dialog;
+       HildonTouchSelector *selector;
+       GSList *node;
+       GtkTreeModel *model;
+       gboolean contacts_to_add = FALSE;
+
+       /* We cannot use hildon_touch_selector_new_text() because
+          there is a bug in hildon that does not retrieve the current
+          selected text when using MODES_NORMAL. So we need a
+          temporary workaround here */
+       selector = (HildonTouchSelector*) hildon_touch_selector_new ();
+
+       model = (GtkTreeModel *) gtk_list_store_new (1,  G_TYPE_STRING);
+       hildon_touch_selector_append_text_column (selector, model, TRUE);
+       hildon_touch_selector_set_hildon_ui_mode (selector, HILDON_UI_MODE_NORMAL);
+       g_object_unref (model);
+
+       for (node = address_list; node != NULL; node = g_slist_next (node)) {
+               const gchar *recipient = (const gchar *) node->data;
+               if (modest_text_utils_validate_recipient (recipient, NULL)) {
+                       if (!modest_address_book_has_address (recipient)) {
+                               GtkTreeIter iter;
+                               gtk_list_store_append ((GtkListStore *) model, &iter);
+                               gtk_list_store_set ((GtkListStore *) model, &iter, 0, recipient, -1);
+                               contacts_to_add = TRUE;
+                       }
+               }
+       }
+
+       if (contacts_to_add) {
+               picker_dialog = hildon_picker_dialog_new (parent);
+               gtk_window_set_title (GTK_WINDOW (picker_dialog), _("mcen_me_viewer_addtocontacts"));
+
+               hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (picker_dialog),
+                                                  selector);
+
+               g_signal_connect ((GObject*) selector, "changed",
+                                 G_CALLBACK (selector_selection_changed), picker_dialog);
+
+               g_signal_connect ((GObject*) picker_dialog, "response",
+                                 G_CALLBACK (selector_on_response), parent);
+
+               gtk_widget_show (picker_dialog);
+       } else {
+               gtk_widget_destroy ((GtkWidget *) selector);
+       }
+}