X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fhildon2%2Fmodest-address-book.c;h=369d5d1a0af860d6c7eb5026c9ba9cdececb5709;hb=dfc4b048b130546b172abef3aacd9dabaffe9cd7;hp=0cd0ca82311bd137fbc7c33b19d9c4fbfb47f04f;hpb=428bc3f5208923c7ed77f4361d385d3858149efc;p=modest diff --git a/src/hildon2/modest-address-book.c b/src/hildon2/modest-address-book.c index 0cd0ca8..369d5d1 100644 --- a/src/hildon2/modest-address-book.c +++ b/src/hildon2/modest-address-book.c @@ -38,6 +38,7 @@ #include #include "modest-hildon-includes.h" #include +#include #include "modest-platform.h" #include "modest-runtime.h" #include "widgets/modest-window-mgr.h" @@ -50,13 +51,13 @@ #include #include #include +#include static OssoABookContactModel *contact_model = NULL; static EBook *book = NULL; static EBookView * book_view = NULL; static GSList *get_recipients_for_given_contact (EContact * contact, gboolean *canceled); -static void commit_contact(EContact * contact, gboolean is_new); static gchar *get_email_addr_from_user(const gchar * given_name, gboolean *canceled); static gchar *ui_get_formatted_email_id(gchar * current_given_name, gchar * current_sur_name, gchar * current_email_id); @@ -64,144 +65,50 @@ 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 -unref_gobject (GObject *obj) -{ - if (obj) - g_object_unref (obj); -} - - -static void -get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data) -{ - if (status != E_BOOK_ERROR_OK) { - g_object_unref (book); - book = NULL; - return; - } - book_view = bookview; - - if (contact_model) -#if MODEST_ABOOK_API < 4 - osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model), - book_view); -#else /* MODEST_ABOOK_API < 4 */ - osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model), - book_view); -#endif /* MODEST_ABOOK_API < 4 */ - - e_book_view_start (book_view); -} - -static void -book_open_cb (EBook *view, EBookStatus status, gpointer data) -{ - EBookQuery *query = NULL; - - if (status != E_BOOK_ERROR_OK) { - g_object_unref (book); - book = NULL; - return; - } - query = e_book_query_any_field_contains (""); - e_book_async_get_book_view (book, query, NULL, -1, get_book_view_cb, NULL); - e_book_query_unref (query); -} - -static gboolean -open_addressbook () -{ - book = e_book_new_system_addressbook (NULL); - if (!book) - return FALSE; - - if (e_book_async_open (book, FALSE, book_open_cb, NULL) != E_BOOK_ERROR_OK) - return FALSE; - - return TRUE; /* FIXME */ -} - -typedef struct _OpenAddressbookSyncInfo { - gboolean retval; - GMainLoop *mainloop; -} OpenAddressbookSyncInfo; - -static void -get_book_view_sync_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data) -{ - OpenAddressbookSyncInfo *info = (OpenAddressbookSyncInfo *) data; - - if (status != E_BOOK_ERROR_OK) { - g_object_unref (book); - book = NULL; - info->retval = FALSE; - g_main_loop_quit (info->mainloop); - return; - } - book_view = bookview; - - if (contact_model) -#if MODEST_ABOOK_API < 4 - osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model), - book_view); -#else /* MODEST_ABOOK_API < 4 */ - osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model), - book_view); -#endif /* MODEST_ABOOK_API < 4 */ - - e_book_view_start (book_view); - info->retval = TRUE; - g_main_loop_quit (info->mainloop); -} - -static void -book_open_sync_cb (EBook *view, EBookStatus status, gpointer data) -{ - EBookQuery *query = NULL; - OpenAddressbookSyncInfo *info = (OpenAddressbookSyncInfo *) data; - - if (status != E_BOOK_ERROR_OK) { - g_object_unref (book); - book = NULL; - info->retval = FALSE; - g_main_loop_quit (info->mainloop); - return; - } - query = e_book_query_any_field_contains (""); - e_book_async_get_book_view (book, query, NULL, -1, get_book_view_sync_cb, info); - e_book_query_unref (query); -} +static void set_contact_from_display_name (EContact *contact, const gchar *display_name); static gboolean -open_addressbook_sync () +open_addressbook () { - OpenAddressbookSyncInfo *info; - gboolean retval; - - book = e_book_new_system_addressbook (NULL); - if (!book) - return FALSE; - - info = g_slice_new (OpenAddressbookSyncInfo); - info->mainloop = g_main_loop_new (NULL, FALSE); - info->retval = FALSE; - if (e_book_async_open (book, FALSE, book_open_sync_cb, info) == E_BOOK_ERROR_OK) { - GDK_THREADS_LEAVE (); - g_main_loop_run (info->mainloop); - GDK_THREADS_ENTER (); - } - retval = info->retval; - g_main_loop_unref (info->mainloop); - g_slice_free (OpenAddressbookSyncInfo, info); - /* Make it launch a main loop */ - return e_book_open (book, FALSE, NULL); + OssoABookRoster *roster; + GError *error = NULL; + time_t init,end; + + if (book && book_view) + return TRUE; + + roster = osso_abook_aggregator_get_default (&error); + if (error) + goto error; + + /* Wait until it's ready */ + init = time (NULL); + osso_abook_waitable_run ((OssoABookWaitable *) roster, + g_main_context_default (), + &error); + end = time (NULL); + g_debug ("Opening addressbook lasted %ld seconds", (gint) end-init); + + if (error) + goto error; + + if (!osso_abook_waitable_is_ready ((OssoABookWaitable *) roster, + &error)) + goto error; + + book = osso_abook_roster_get_book (roster); + book_view = osso_abook_roster_get_book_view (roster); + + return TRUE; + error: + g_warning ("error opening addressbook %s", error->message); + g_error_free (error); + return FALSE; } 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; @@ -212,10 +119,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); @@ -226,16 +134,10 @@ 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; - GtkWidget *contact_dialog; - GtkWidget *toplevel; -#else /* MODEST_ABOOK_API < 4 */ GtkWidget *contact_chooser = NULL; -#endif /* MODEST_ABOOK_API < 4 */ - GList *contacts_list = NULL; GSList *email_addrs_per_contact = NULL; gchar *econtact_id; @@ -243,33 +145,8 @@ modest_address_book_select_addresses (ModestRecptEditor *recpt_editor) g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor)); -#if MODEST_ABOOK_API < 4 - if (!open_addressbook ()) { - if (contact_model) { - g_object_unref (contact_model); - contact_model = NULL; - } - return; - } - contact_model = osso_abook_contact_model_new (); - - contact_view = osso_abook_contact_selector_new_basic (contact_model); - osso_abook_contact_selector_set_minimum_selection (OSSO_ABOOK_CONTACT_SELECTOR (contact_view), 1); - - contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view)); - toplevel = gtk_widget_get_toplevel (GTK_WIDGET (recpt_editor)); - modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (contact_dialog), GTK_WINDOW (toplevel)); - - gtk_widget_show (contact_dialog); - - if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) { - contacts_list = - osso_abook_contact_selector_get_extended_selection (OSSO_ABOOK_CONTACT_SELECTOR - (contact_view)); - } -#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); @@ -280,7 +157,6 @@ modest_address_book_select_addresses (ModestRecptEditor *recpt_editor) if (gtk_dialog_run (GTK_DIALOG (contact_chooser)) == GTK_RESPONSE_OK) contacts_list = osso_abook_contact_chooser_get_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_chooser)); gtk_widget_destroy (contact_chooser); -#endif if (contacts_list) { GList *node; @@ -303,15 +179,6 @@ modest_address_book_select_addresses (ModestRecptEditor *recpt_editor) g_list_free (contacts_list); } -#if MODEST_ABOOK_API < 4 - if (contact_model) { - g_object_unref (contact_model); - contact_model = NULL; - } - - gtk_widget_destroy (contact_dialog); -#endif - if (focus_recpt_editor) modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (recpt_editor)); @@ -370,23 +237,18 @@ get_recipients_for_given_contact (EContact * contact, /*Launch the 'Add e-mail addr to contact' dialog if required */ if (email_not_present) { -#if MODEST_ABOOK_API < 4 - display_name = osso_abook_contact_get_display_name(contact); -#else OssoABookContact *abook_contact; abook_contact = osso_abook_contact_new_from_template (contact); display_name = osso_abook_contact_get_display_name(abook_contact); -#endif emailid = get_email_addr_from_user(display_name, canceled); if (emailid) { - e_contact_set(contact, E_CONTACT_EMAIL_1, emailid); - commit_contact(contact, FALSE); + 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); } -#if MODEST_ABOOK_API >= 4 g_object_unref (abook_contact); -#endif } if (emailid) { @@ -425,43 +287,6 @@ get_recipients_for_given_contact (EContact * contact, } /** - * This is a helper function to commit a EContact to Address_Book application. - * - * @param contact Contact of type #EContact - * @return void - */ -static void -commit_contact(EContact * contact, gboolean is_new) -{ - g_return_if_fail (contact); - g_return_if_fail (book); - - if (!contact || !book) - return; - -#if MODEST_ABOOK_API < 4 - osso_abook_contact_commit(contact, is_new, book); -#else - if (OSSO_ABOOK_IS_CONTACT (contact)) { - osso_abook_contact_commit(OSSO_ABOOK_CONTACT(contact), is_new, book, NULL); - } else { - 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 */ -} - -/** * This is a helper function used to launch 'Add e-mail address to contact' dialog * after showing appropriate notification, when there is no e-mail address defined * for a selected contact. @@ -517,10 +342,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); } @@ -567,16 +391,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 = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH); - cptn_cntrl = modest_maemo_utils_create_captioned (size_group, NULL, - _("mcen_fi_add_email_name"), FALSE, - email_entry); + email_entry = modest_toolkit_factory_create_entry (modest_runtime_get_toolkit_factory ()); + 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); @@ -590,14 +414,14 @@ run_add_email_addr_to_contact_dlg(const gchar * contact_name, if (result == GTK_RESPONSE_ACCEPT) { const gchar *invalid_char_offset = NULL; - new_email_addr = g_strdup(hildon_entry_get_text(HILDON_ENTRY(email_entry))); + new_email_addr = g_strdup(modest_entry_get_text(email_entry)); new_email_addr = g_strstrip(new_email_addr); if (!modest_text_utils_validate_email_address (new_email_addr, &invalid_char_offset)) { gtk_widget_grab_focus(email_entry); 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 ( @@ -670,13 +494,36 @@ select_email_addrs_for_contact(GList * email_addr_list) 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_down, *mail2_down; + + /* Perform a case insensitive comparison */ + mail1 = modest_text_utils_get_email_address (address1); + 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; +} + static EContact * get_contact_for_address (GList *contacts, const gchar *address) { EContact *retval = NULL, *contact; GList *iter; + gchar *email; + email = modest_text_utils_get_email_address (address); iter = contacts; while (iter && !retval) { GList *emails = NULL; @@ -685,7 +532,7 @@ get_contact_for_address (GList *contacts, emails = e_contact_get (contact, E_CONTACT_EMAIL); if (emails) { /* Look for the email address */ - if (g_list_find_custom (emails, address, (GCompareFunc) g_strcmp0)) + if (g_list_find_custom (emails, email, (GCompareFunc) compare_addresses)) retval = contact; /* Free the list */ @@ -694,6 +541,8 @@ get_contact_for_address (GList *contacts, } iter = g_list_next (iter); } + g_free (email); + return retval; } @@ -705,6 +554,7 @@ async_get_contacts_cb (EBook *book, { GSList *addresses, *iter; GList *to_commit_contacts, *to_add_contacts; + EContact *self_contact; addresses = (GSList *) closure; @@ -712,6 +562,11 @@ async_get_contacts_cb (EBook *book, 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); + } + iter = addresses; to_commit_contacts = NULL; to_add_contacts = NULL; @@ -731,18 +586,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); } @@ -770,44 +628,8 @@ async_get_contacts_cb (EBook *book, g_slist_foreach (addresses, (GFunc) g_free, NULL); g_slist_free (addresses); } - if (contacts) { - g_list_foreach (contacts, (GFunc) g_object_unref, NULL); + if (contacts) 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 { @@ -901,11 +723,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; @@ -930,7 +753,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; @@ -941,7 +763,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); @@ -954,7 +776,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat 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 ); @@ -1019,14 +841,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; } } @@ -1034,8 +856,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); @@ -1046,9 +868,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); @@ -1086,66 +908,122 @@ 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_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; } +static void +set_contact_from_display_name (EContact *contact, const gchar *disp_name) +{ + const gchar *parent_open; + const gchar *comma_separator; + GString *buffer; + gchar *display_name; + + 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); +} + static GList * select_contacts_for_name_dialog (const gchar *name) { - EBookQuery *full_name_book_query = NULL; + EBookQuery *book_query = NULL; EBookView *book_view = NULL; GList *result = NULL; gchar *unquoted; + EBookQuery *queries[10]; unquoted = unquote_string (name); - full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted); - g_free (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; -#if MODEST_ABOOK_API < 4 - GtkWidget *contact_view = NULL; - osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model), book_view); - e_book_view_start (book_view); - - contact_view = osso_abook_contact_selector_new_basic (contact_model); - contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view)); - - if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) { - result = osso_abook_contact_view_get_selection (OSSO_ABOOK_CONTACT_VIEW (contact_view)); - } - e_book_view_stop (book_view); - g_object_unref (book_view); - gtk_widget_destroy (contact_dialog); -#else /* MODEST_ABOOK_API < 4 */ osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model), book_view); e_book_view_start (book_view); @@ -1165,8 +1043,8 @@ select_contacts_for_name_dialog (const gchar *name) e_book_view_stop (book_view); g_object_unref (book_view); gtk_widget_destroy (contact_dialog); -#endif /* MODEST_ABOOK_API < 4 */ } + g_free (unquoted); return result; } @@ -1187,9 +1065,9 @@ resolve_address (const gchar *address, *canceled = FALSE; info = g_slice_new0 (CheckNamesInfo); show_check_names_banner (info); - - contact_model = osso_abook_contact_model_new (); - if (!open_addressbook_sync ()) { + + contact_model = osso_abook_contact_model_get_default (); + if (!open_addressbook ()) { hide_check_names_banner (info); if (contact_model) { g_object_unref (contact_model); @@ -1235,7 +1113,6 @@ resolve_address (const gchar *address, } } - g_list_foreach (resolved_contacts, (GFunc)unref_gobject, NULL); g_list_free (resolved_contacts); clean_check_names_banner (info); @@ -1283,43 +1160,179 @@ unquote_string (const gchar *str) gboolean modest_address_book_has_address (const gchar *address) { - EBookQuery *query; GList *contacts = NULL; GError *err = NULL; gchar *email; gboolean result; + OssoABookAggregator *roster; g_return_val_if_fail (address, FALSE); - + if (!book) { if (!open_addressbook ()) { g_return_val_if_reached (FALSE); } } - g_return_val_if_fail (book, FALSE); 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 : ""); + + roster = (OssoABookAggregator *) osso_abook_aggregator_get_default (NULL); + contacts = osso_abook_aggregator_find_contacts_for_email_address (roster, email); + if (!contacts) { if (err) g_error_free (err); g_free (email); - e_book_query_unref (query); return FALSE; } - e_book_query_unref (query); - result = (contacts != NULL); if (contacts) { - g_list_foreach (contacts, (GFunc)unref_gobject, NULL); g_list_free (contacts); + result = TRUE; } - + g_free (email); 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) + return e_contact_get ((EContact *) self_contact, E_CONTACT_FULL_NAME); + else + return NULL; +} + +void +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); +} + +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, ¤t_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); + } +}