Refresh the book and book view in addressbook if it changes in the
[modest] / src / hildon2 / modest-address-book.c
index 2aaaf56..6a2b55d 100644 (file)
 #include <gtk/gtktreeselection.h>
 #include <gtk/gtkentry.h>
 #include <modest-maemo-utils.h>
+#include <modest-toolkit-utils.h>
+#ifdef MODEST_PLATFORM_MAEMO
+#include <asdbus.h>
+#endif
 
 static OssoABookContactModel *contact_model =  NULL;
 static EBook *book = NULL;
@@ -64,6 +68,20 @@ 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 void
+on_roster_notify (GObject    *gobject,
+                 GParamSpec *pspec,
+                 gpointer    user_data)
+{
+       if ((g_strcmp0 (g_param_spec_get_name (pspec), "book") == 0) ||
+           (g_strcmp0 (g_param_spec_get_name (pspec), "book-view") == 0)) {
+               OssoABookRoster *roster = OSSO_ABOOK_ROSTER (gobject);
+               book = osso_abook_roster_get_book (roster);
+               book_view = osso_abook_roster_get_book_view (roster);
+       }
+}
 
 static gboolean
 open_addressbook ()
@@ -97,6 +115,8 @@ open_addressbook ()
        book = osso_abook_roster_get_book (roster);
        book_view = osso_abook_roster_get_book_view (roster);
 
+       g_signal_connect (G_OBJECT (roster), "notify", G_CALLBACK (on_roster_notify), NULL);
+
        return TRUE;
  error:
        g_warning ("error opening addressbook %s", error->message);
@@ -148,6 +168,7 @@ modest_address_book_select_addresses (ModestRecptEditor *recpt_editor,
                                                                            _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_chooser),
                                                          G_MAXUINT);
@@ -389,16 +410,16 @@ run_add_email_addr_to_contact_dlg(const gchar * contact_name,
        name_label = gtk_label_new(contact_name);
        gtk_misc_set_alignment(GTK_MISC(name_label), 0.0, 0.5);
        cptn_cntrl =
-               modest_maemo_utils_create_captioned (size_group, NULL,
-                                                    _("mcen_ia_add_email_name"), FALSE,
-                                                    name_label);
+               modest_toolkit_utils_create_captioned (size_group, NULL,
+                                                      _("mcen_ia_add_email_name"), FALSE,
+                                                      name_label);
        gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
                           FALSE, FALSE, 0);
 
        email_entry = modest_toolkit_factory_create_entry (modest_runtime_get_toolkit_factory ());
-       cptn_cntrl = modest_maemo_utils_create_captioned (size_group, NULL, 
-                                                         _("mcen_fi_add_email_name"), FALSE,
-                                                         email_entry);
+       cptn_cntrl = modest_toolkit_utils_create_captioned (size_group, NULL, 
+                                                           _("mcen_fi_add_email_name"), FALSE,
+                                                           email_entry);
        hildon_gtk_entry_set_input_mode(GTK_ENTRY(email_entry), HILDON_GTK_INPUT_MODE_FULL);
        gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
                           TRUE, TRUE, 0);
@@ -419,7 +440,7 @@ run_add_email_addr_to_contact_dlg(const gchar * contact_name,
                                if ((invalid_char_offset != NULL)&&(*invalid_char_offset != '\0')) {
                                        gchar *char_in_string = g_strdup_printf ("%c", *invalid_char_offset);
                                        gchar *message = g_strdup_printf(
-                                               _CS("ckdg_ib_illegal_characters_entered"), 
+                                               _CS_ILLEGAL_CHARACTERS_ENTERED, 
                                                char_in_string);
                                        g_free (char_in_string);
                                        hildon_banner_show_information (
@@ -598,7 +619,7 @@ async_get_contacts_cb (EBook *book,
                        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);
                        }
 
@@ -732,6 +753,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
        gint offset_delta = 0;
        gint last_length;
        GtkTextIter start_iter, end_iter;
+       gboolean empty_recipients = 0;
 
        g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
 
@@ -757,7 +779,6 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
                gchar *start_ptr, *end_ptr;
                gint start_pos, end_pos;
                const gchar *invalid_char_position = NULL;
-               gboolean store_address = FALSE;
 
                start_pos = (*((gint*) current_start->data)) + offset_delta;
                end_pos = (*((gint*) current_end->data)) + offset_delta;
@@ -766,6 +787,14 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
                end_ptr = g_utf8_offset_to_pointer (recipients, end_pos);
 
                address = g_strstrip (g_strndup (start_ptr, end_ptr - start_ptr));
+
+               /* Ignore empty addresses */
+               if (!g_strcmp0 (address, "")) {
+                       g_free (address);
+                       empty_recipients++;
+                       goto next_address;
+               }
+
                gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_pos);
                gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_pos);
                gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
@@ -774,7 +803,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
                        if ((invalid_char_position != NULL) && (*invalid_char_position != '\0')) {
                                gchar *char_in_string = g_strdup_printf("%c", *invalid_char_position);
                                gchar *message = 
-                                       g_strdup_printf(_CS("ckdg_ib_illegal_characters_entered"), 
+                                       g_strdup_printf(_CS_ILLEGAL_CHARACTERS_ENTERED, 
                                                        char_in_string);
                                g_free (char_in_string);
                                hildon_banner_show_information (NULL, NULL, message );
@@ -847,14 +876,13 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
                                                                                     addr_list,
                                                                                     "");
                                g_slist_free (addr_list);
-                               store_address = TRUE;
                        }
                }
 
                /* 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 && address_list && store_address)
+               if (result && address_list)
                        *address_list = g_slist_prepend (*address_list, address);
                else
                        g_free (address);
@@ -862,6 +890,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
                if (result == FALSE)
                        break;
 
+       next_address:
                current_start = g_slist_next (current_start);
                current_end = g_slist_next (current_end);
        }
@@ -875,6 +904,10 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
                gtk_text_buffer_place_cursor (buffer, &end_iter);
        }
 
+       /* Check that at least there is one non-empty recipient */
+       if ((g_slist_length (start_indexes) - empty_recipients) == 0)
+               result = FALSE;
+
        g_slist_foreach (start_indexes, (GFunc) g_free, NULL);
        g_slist_foreach (end_indexes, (GFunc) g_free, NULL);
        g_slist_free (start_indexes);
@@ -884,93 +917,77 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
 
 }
 
-typedef struct _GetContactsInfo {
-       GMainLoop *mainloop;
-       GList *result;
-} GetContactsInfo;
-
-static void 
-get_contacts_for_name_cb (EBook *book, 
-                         EBookStatus status, 
-                         GList *list, 
-                         gpointer userdata)
-{
-       GetContactsInfo *info = (GetContactsInfo *) userdata;
-
-       if (status == E_BOOK_ERROR_OK)
-               info->result = list;
 
-       g_main_loop_quit (info->mainloop);
-}
 
-static GList *
-get_contacts_for_name (const gchar *name)
+static void
+set_contact_from_display_name (EContact *contact, const gchar *disp_name)
 {
-       EBookQuery *book_query = NULL;
-       GList *result;
-       gchar *unquoted;
-       GetContactsInfo *info;
-       EBookQuery *queries[10];
+       const gchar *parent_open;
+       const gchar *comma_separator;
+       GString *buffer;
+       gchar *display_name;
 
-       if (name == NULL)
-               return NULL;
+       display_name = unquote_string (disp_name);
+       buffer = g_string_new ("");
 
-       unquoted = unquote_string (name);
+       /* First we remove part in () */
+       parent_open = g_strstr_len (display_name, -1, "(");
+       if (parent_open) {
+               const gchar *parent_close;
 
-       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);
+               parent_close = g_strstr_len (parent_open, -1, ")");
 
-       g_free (unquoted);
+               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);
+       }
 
-       /* 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, 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 (book_query);
-       g_main_loop_unref (info->mainloop);
-       g_slice_free (GetContactsInfo, info);
+       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);
 
-       return result;
-}
+               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);
+}
 
 static GList *
-select_contacts_for_name_dialog (const gchar *name)
+select_contacts_for_name_dialog (const gchar *name, GList *external_contacts)
 {
        EBookQuery *book_query = NULL;
        EBookView *book_view = NULL;
        GList *result = NULL;
        gchar *unquoted;
        EBookQuery *queries[10];
+       gint i=0;
 
        unquoted = unquote_string (name);
 
-       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);
+       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);
        e_book_get_book_view (book, book_query, NULL, -1, &book_view, NULL);
        e_book_query_unref (book_query);
 
@@ -982,8 +999,40 @@ select_contacts_for_name_dialog (const gchar *name)
                /* 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_CAPS_ALL,
                                                                                   OSSO_ABOOK_CONTACT_ORDER_NAME);
+
+#ifdef MODEST_PLATFORM_MAEMO
+               if (external_contacts) {
+
+                       GList *row_list = NULL;
+                       while (external_contacts) {
+
+                               AsDbusRecipient *recipient = (AsDbusRecipient*)external_contacts->data;
+                               external_contacts = g_list_next (external_contacts);
+                               if (!recipient)
+                                       continue;
+
+                               char *uid = osso_abook_create_temporary_uid ();
+                               OssoABookContact *contact = osso_abook_contact_new ();
+                               osso_abook_contact_set_uid (contact, uid);
+                               e_contact_set (E_CONTACT (contact), E_CONTACT_FULL_NAME, recipient->display_name);
+                               osso_abook_contact_set_value (E_CONTACT (contact), EVC_EMAIL, recipient->email_address);
+
+                               OssoABookListStoreRow *row = osso_abook_list_store_row_new (contact);
+                               row_list = g_list_prepend (row_list, row);
+                               /* FIXME: unref row? */
+
+                               g_free (uid);
+                       }
+
+                       if (row_list) {
+                               osso_abook_list_store_merge_rows (OSSO_ABOOK_LIST_STORE (contact_model), row_list);
+                               g_list_free (row_list);
+                       }
+               }
+#endif
+
                /* Enable multiselection */
                osso_abook_contact_chooser_set_maximum_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
                                                                  G_MAXUINT);
@@ -1002,13 +1051,66 @@ select_contacts_for_name_dialog (const gchar *name)
 }
 
 static gboolean
+contact_name_or_email_starts_with (OssoABookContact *contact,
+                                   gpointer          user_data)
+{
+       const char *prefix = user_data;
+       GList *contacts, *l;
+       gboolean contact_match;
+
+       contacts = osso_abook_contact_get_roster_contacts (contact);
+       contacts = g_list_prepend (contacts, contact);
+
+       for (l = contacts; l; l = l->next) {
+               GList *attrs;
+
+               contact_match = FALSE;
+
+               attrs = e_vcard_get_attributes (E_VCARD (l->data));
+               for (;attrs;attrs = attrs->next) {
+                       EVCardAttribute *attr = attrs->data;
+                       const char *name;
+
+                       name = e_vcard_attribute_get_name (attr);
+
+                       if (!g_strcmp0 (name, "N") ||
+                           (strchr (prefix, '@') && !g_strcmp0 (name, "EMAIL"))) {
+                               GList *values = e_vcard_attribute_get_values (attr);
+                               gchar *prefix_down = g_utf8_strdown (prefix, -1);
+
+                               for (;values; values = values->next) {
+                                       gchar *value_down = NULL;
+
+                                       if (g_strcmp0 (values->data, ""))
+                                               value_down = g_utf8_strdown (values->data, -1);
+
+                                       if (value_down && g_str_has_prefix (value_down, prefix_down)) {
+                                               contact_match = TRUE;
+                                               g_free (value_down);
+                                               g_free (prefix_down);
+                                               goto out;
+                                       }
+                                       g_free (value_down);
+                               }
+                               g_free (prefix_down);
+                       }
+               }
+       }
+ out:
+       g_list_free (contacts);
+
+       return contact_match;
+}
+
+static gboolean
 resolve_address (const gchar *address, 
                 GSList **resolved_addresses, 
                 GSList **contact_ids,
                 gboolean *canceled)
 {
        GList *resolved_contacts;
-       CheckNamesInfo *info;;
+       CheckNamesInfo *info;
+       OssoABookRoster *roster;
 
        g_return_val_if_fail (canceled, FALSE);
 
@@ -1029,23 +1131,123 @@ resolve_address (const gchar *address,
                return FALSE;
        }
 
-       resolved_contacts = get_contacts_for_name (address);
+       roster = osso_abook_aggregator_get_default (NULL);
+       resolved_contacts =
+               osso_abook_aggregator_find_contacts_full ((OssoABookAggregator *) roster,
+                                                         contact_name_or_email_starts_with,
+                                                         (gpointer) address);
+#ifdef MODEST_PLATFORM_MAEMO
+       GList *external_contacts = asdbus_resolve_recipients (address);
+#else
+       GList *external_contacts = NULL;
+#endif
        hide_check_names_banner (info);
 
-       if (resolved_contacts == NULL) {
+       if (resolved_contacts == NULL && NULL == external_contacts) {
                /* no matching contacts for the search string */
                modest_platform_run_information_dialog (NULL, _("mcen_nc_no_matching_contacts"), FALSE);
                clean_check_names_banner (info);
                return FALSE;
        }
 
-       if (g_list_length (resolved_contacts) > 1) {
+#ifdef MODEST_PLATFORM_MAEMO
+       /* check for duplicate emails and remove from external_contacts if any */
+       if (resolved_contacts && external_contacts) {
+
+               GList *node, *ex_node;
+
+               for (ex_node = external_contacts; ex_node != NULL; ex_node = g_list_next (ex_node)) {
+
+                       AsDbusRecipient *recipient = (AsDbusRecipient*)ex_node->data;
+                       if (!recipient)
+                               continue;
+
+                       for (node = resolved_contacts; node != NULL; node = g_list_next (node)) {
+
+                               EContact *contact = (EContact*)node->data;
+                               GList *emails = e_contact_get (contact, E_CONTACT_EMAIL);
+                               if (!emails)
+                                       continue;
+
+                               if (g_list_find_custom (emails, recipient->email_address, (GCompareFunc) compare_addresses)) {
+
+                                       g_free (recipient->display_name);
+                                       g_free (recipient->email_address);
+                                       g_free (recipient);
+                                       recipient = NULL;
+                                       ex_node->data = NULL;
+                               }
+
+                               g_list_foreach (emails, (GFunc) g_free, NULL);
+                               g_list_free (emails);
+
+                               if (!recipient)
+                                       break;
+                       }
+               }
+       }
+#endif
+
+       if (g_list_length (resolved_contacts) + g_list_length (external_contacts) > 1) {
                /* show a dialog to select the contact from the resolved ones */
                g_list_free (resolved_contacts);
 
-               resolved_contacts = select_contacts_for_name_dialog (address);
+               resolved_contacts = select_contacts_for_name_dialog (address, external_contacts);
+
+#ifdef MODEST_PLATFORM_MAEMO
+               if (external_contacts) {
+
+                       GList *node;
+                       for (node = external_contacts; node != NULL; node = g_list_next (node)) {
+
+                               AsDbusRecipient *recipient = (AsDbusRecipient*)node->data;
+                               if (!recipient)
+                                       continue;
+
+                               g_free (recipient->display_name);
+                               g_free (recipient->email_address);
+                               g_free (recipient);
+                       }
+
+                       g_list_free (external_contacts);
+                       external_contacts = NULL;
+               }
+#endif
        }
 
+#ifdef MODEST_PLATFORM_MAEMO
+       if (external_contacts) {
+
+               gboolean found = FALSE;
+               GList *node;
+               for (node = external_contacts; node != NULL; node = g_list_next (node)) {
+
+                       AsDbusRecipient *recipient = (AsDbusRecipient*)node->data;
+                       if (!recipient)
+                               continue;
+
+                       GString *formatted_recipient = g_string_new (NULL);
+                       g_string_printf (formatted_recipient, "\"%s\" <%s>", recipient->display_name, recipient->email_address);
+
+                       /* FIXME: why we have to have list of lists? */
+                       GSList *formattedlist = g_slist_append(NULL, formatted_recipient->str);
+                       *resolved_addresses = g_slist_append (*resolved_addresses, formattedlist);
+                       /* FIXME: how important is an UID? */
+                       *contact_ids = g_slist_append (*contact_ids, g_strdup ("temp-uid"));
+                       found = TRUE;
+
+                       g_string_free (formatted_recipient, FALSE); /* character data segment is NOT freed */
+                       g_free (recipient->display_name);
+                       g_free (recipient->email_address);
+                       g_free (recipient);
+               }
+
+               g_list_free (external_contacts);
+               external_contacts = NULL;
+               return found;
+        }
+#endif
+
        /* get the resolved contacts (can be no contact) */
        if (resolved_contacts) {
                GList *node;
@@ -1201,50 +1403,90 @@ modest_address_book_add_address_list (GSList *address_list)
        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;
-       GtkWidget *selector;
+       HildonTouchSelector *selector;
        GSList *node;
-       gchar *selected = NULL;
+       GtkTreeModel *model;
        gboolean contacts_to_add = FALSE;
 
-       selector = hildon_touch_selector_new_text ();
-       g_object_ref (selector);
+       /* 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)) {
-                               hildon_touch_selector_append_text ((HildonTouchSelector *) selector,
-                                                                  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) {
-               gint picker_result;
-
                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),
-                                                  HILDON_TOUCH_SELECTOR (selector));
-
-               picker_result = gtk_dialog_run (GTK_DIALOG (picker_dialog));
+                                                  selector);
 
-               if (picker_result == GTK_RESPONSE_OK) {
-                       selected = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector));
-               }
-               gtk_widget_destroy (picker_dialog);
+               g_signal_connect ((GObject*) selector, "changed",
+                                 G_CALLBACK (selector_selection_changed), picker_dialog);
 
-               if (selected)
-                       modest_address_book_add_address (selected, parent);
-               g_free (selected);
+               g_signal_connect ((GObject*) picker_dialog, "response",
+                                 G_CALLBACK (selector_on_response), parent);
 
+               gtk_widget_show (picker_dialog);
        } else {
-               g_object_unref (selector);
+               gtk_widget_destroy ((GtkWidget *) selector);
        }
 }