X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fhildon2%2Fmodest-address-book.c;h=160bd890dbad50c8371142a7a6ffaaea0a793bbf;hp=50e95ddf3d86852e8ad47a11dc5b4fdc72713365;hb=28fbc4e0d124d6c3d41cfd970ad699fe48555182;hpb=e3ec8ba7cf5a362e1f5f4d659b14779bdcef2dec diff --git a/src/hildon2/modest-address-book.c b/src/hildon2/modest-address-book.c index 50e95dd..160bd89 100644 --- a/src/hildon2/modest-address-book.c +++ b/src/hildon2/modest-address-book.c @@ -49,88 +49,58 @@ #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); -static void commit_contact(EContact * contact, gboolean is_new); -static gchar *get_email_addr_from_user(const gchar * given_name); +static GSList *get_recipients_for_given_contact (EContact * contact, gboolean *canceled); +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); -static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name); +static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name, gboolean *canceled); static GSList *select_email_addrs_for_contact(GList * email_addr_list); -static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, gchar **contact_id); +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) +static gboolean +open_addressbook () { - if (obj) - g_object_unref (obj); -} - + OssoABookRoster *roster; + GError *error = NULL; + time_t init,end; -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 (book && book_view) + return TRUE; - 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 */ + roster = osso_abook_aggregator_get_default (&error); + if (error) + goto error; - e_book_view_start (book_view); -} + /* 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); -static void -book_open_cb (EBook *view, EBookStatus status, gpointer data) -{ - EBookQuery *query = NULL; + if (error) + goto error; - 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 (!osso_abook_waitable_is_ready ((OssoABookWaitable *) roster, + &error)) + goto error; - if (e_book_async_open (book, FALSE, book_open_cb, NULL) != E_BOOK_ERROR_OK) - return FALSE; - - return TRUE; /* FIXME */ -} - -static gboolean -open_addressbook_sync () -{ - book = e_book_new_system_addressbook (NULL); - if (!book) - return FALSE; + book = osso_abook_roster_get_book (roster); + book_view = osso_abook_roster_get_book_view (roster); - return e_book_open (book, FALSE, NULL); + return TRUE; + error: + g_warning ("error opening addressbook %s", error->message); + g_error_free (error); + return FALSE; } void @@ -159,16 +129,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; @@ -176,33 +140,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); @@ -213,15 +152,15 @@ 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; for (node = contacts_list; node != NULL; node = g_list_next (node)) { EContact *contact = (EContact *) node->data; + gboolean canceled; - email_addrs_per_contact = get_recipients_for_given_contact (contact); + email_addrs_per_contact = get_recipients_for_given_contact (contact, &canceled); if (email_addrs_per_contact) { econtact_id = (gchar *) e_contact_get_const (contact, E_CONTACT_UID); modest_recpt_editor_add_resolved_recipient (MODEST_RECPT_EDITOR (recpt_editor), @@ -235,15 +174,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)); @@ -259,7 +189,9 @@ modest_address_book_select_addresses (ModestRecptEditor *recpt_editor) * @param Contact of type #EContact * @return List of resolved recipient strings, to be freed by calling function. */ -static GSList *get_recipients_for_given_contact(EContact * contact) +static GSList * +get_recipients_for_given_contact (EContact * contact, + gboolean *canceled) { gchar *givenname = NULL; gchar *familyname = NULL; @@ -300,23 +232,17 @@ static GSList *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); + emailid = get_email_addr_from_user(display_name, canceled); if (emailid) { - e_contact_set(contact, E_CONTACT_EMAIL_1, emailid); - commit_contact(contact, FALSE); + e_contact_set(E_CONTACT (abook_contact), E_CONTACT_EMAIL_1, emailid); + osso_abook_contact_commit (abook_contact, FALSE, NULL, NULL); } -#if MODEST_ABOOK_API >= 4 g_object_unref (abook_contact); -#endif } if (emailid) { @@ -355,35 +281,6 @@ static GSList *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 { - if (is_new) - e_book_add_contact (book, contact, NULL); - else - e_book_commit_contact (book, contact, NULL); - } -#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. @@ -393,7 +290,7 @@ commit_contact(EContact * contact, gboolean is_new) * @return E-mail address string entered by user, to be freed by calling function. */ static gchar * -get_email_addr_from_user(const gchar * given_name) +get_email_addr_from_user(const gchar * given_name, gboolean *canceled) { gchar *notification = NULL; gchar *email_addr = NULL; @@ -409,7 +306,7 @@ get_email_addr_from_user(const gchar * given_name) g_free(notification); if (note_response == GTK_RESPONSE_OK) { - email_addr = run_add_email_addr_to_contact_dlg(given_name); + email_addr = run_add_email_addr_to_contact_dlg (given_name, canceled); } return email_addr; @@ -452,11 +349,14 @@ ui_get_formatted_email_id(gchar * current_given_name, * It allows user to enter an e-mail address, and shows appropriate infonote if the * entered string is not a valid e-mail address. * + * It must return TRUE in canceled if the dialog was canceled by the user + * * @param contact_name Full name of the contact * @return E-mail address string entered by user, to be freed by calling function. */ static gchar * -run_add_email_addr_to_contact_dlg(const gchar * contact_name) +run_add_email_addr_to_contact_dlg(const gchar * contact_name, + gboolean *canceled) { GtkWidget *add_email_addr_to_contact_dlg = NULL; GtkSizeGroup *size_group = NULL; @@ -467,26 +367,35 @@ run_add_email_addr_to_contact_dlg(const gchar * contact_name) gchar *new_email_addr = NULL; gboolean run_dialog = TRUE; + g_return_val_if_fail (canceled, NULL); + + *canceled = FALSE; + add_email_addr_to_contact_dlg = - gtk_dialog_new_with_buttons(_AB("addr_ti_sel_contact"), NULL, + gtk_dialog_new_with_buttons(_("mcen_ti_add_email_title"), NULL, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, - _HL("wdgt_bd_done"), GTK_RESPONSE_ACCEPT, NULL); + _HL("wdgt_bd_save"), GTK_RESPONSE_ACCEPT, NULL); gtk_dialog_set_has_separator(GTK_DIALOG(add_email_addr_to_contact_dlg), FALSE); +#ifdef MODEST_TOOLKIT_HILDON2 + gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (add_email_addr_to_contact_dlg)->vbox), + HILDON_MARGIN_DOUBLE); +#endif /*Set app_name & state_save related tags to the window */ size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL); name_label = gtk_label_new(contact_name); - gtk_misc_set_alignment(GTK_MISC(name_label), 0, 0); + gtk_misc_set_alignment(GTK_MISC(name_label), 0.0, 0.5); cptn_cntrl = - hildon_caption_new(size_group, _("mcen_ia_add_email_name"), name_label, NULL, - HILDON_CAPTION_OPTIONAL); + modest_maemo_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 = gtk_entry_new(); - cptn_cntrl = - hildon_caption_new(size_group, _("mcen_fi_add_email_name"), email_entry, NULL, - HILDON_CAPTION_OPTIONAL); + 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); 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); @@ -500,7 +409,7 @@ 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(gtk_entry_get_text(GTK_ENTRY(email_entry))); + new_email_addr = g_strdup(hildon_entry_get_text(HILDON_ENTRY(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); @@ -509,6 +418,7 @@ run_add_email_addr_to_contact_dlg(const gchar * contact_name) gchar *message = g_strdup_printf( _CS("ckdg_ib_illegal_characters_entered"), char_in_string); + g_free (char_in_string); hildon_banner_show_information ( add_email_addr_to_contact_dlg, NULL, message ); g_free (message); @@ -520,6 +430,8 @@ run_add_email_addr_to_contact_dlg(const gchar * contact_name) g_free(new_email_addr); new_email_addr = NULL; } + } else { + *canceled = TRUE; } } @@ -562,7 +474,7 @@ select_email_addrs_for_contact(GList * email_addr_list) hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (select_email_addr_dlg), HILDON_TOUCH_SELECTOR (selector)); gtk_window_set_default_size (GTK_WINDOW (select_email_addr_dlg), MODEST_DIALOG_WINDOW_MAX_HEIGHT, -1); - + gtk_widget_show_all(select_email_addr_dlg); result = gtk_dialog_run(GTK_DIALOG(select_email_addr_dlg)); @@ -571,97 +483,264 @@ select_email_addrs_for_contact(GList * email_addr_list) current_text = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)); selected_email_addr_list = g_slist_append (selected_email_addr_list, current_text); - } gtk_widget_destroy(select_email_addr_dlg); return selected_email_addr_list; } +/* Assumes that the second argument (the user provided one) is a pure + email address without name */ +static gint +compare_addresses (const gchar *address1, + const gchar *mail2) +{ + gint retval; + gchar *mail1; -static gboolean /* make this public? */ -add_to_address_book (const gchar* address) + mail1 = modest_text_utils_get_email_address (address1); + retval = g_strcmp0 (mail1, mail2); + g_free (mail1); + + return retval; +} + +static EContact * +get_contact_for_address (GList *contacts, + const gchar *address) { - EBookQuery *query; - GList *contacts = NULL; - GError *err = NULL; + EContact *retval = NULL, *contact; + GList *iter; gchar *email; - - g_return_val_if_fail (address, FALSE); - - if (!book) { - if (!open_addressbook ()) { - g_return_val_if_reached (FALSE); + + email = modest_text_utils_get_email_address (address); + iter = contacts; + while (iter && !retval) { + GList *emails = NULL; + + contact = E_CONTACT (iter->data); + emails = e_contact_get (contact, E_CONTACT_EMAIL); + if (emails) { + /* Look for the email address */ + if (g_list_find_custom (emails, email, (GCompareFunc) compare_addresses)) + retval = contact; + + /* Free the list */ + g_list_foreach (emails, (GFunc) g_free, NULL); + g_list_free (emails); } + iter = g_list_next (iter); } - - g_return_val_if_fail (book, FALSE); + g_free (email); - email = modest_text_utils_get_email_address (address); - - query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, email); - if (!e_book_get_contacts (book, query, &contacts, &err)) { - g_printerr ("modest: failed to get contacts: %s", - err ? err->message : ""); - if (err) - g_error_free (err); - return FALSE; + return retval; +} + +static void +async_get_contacts_cb (EBook *book, + EBookStatus status, + GList *contacts, + gpointer closure) +{ + GSList *addresses, *iter; + GList *to_commit_contacts, *to_add_contacts; + EContact *self_contact; + + addresses = (GSList *) closure; + + /* Check errors */ + if (status != E_BOOK_ERROR_OK) + goto frees; + + self_contact = (EContact *) osso_abook_self_contact_get_default (); + if (self_contact) { + contacts = g_list_prepend (contacts, self_contact); } - e_book_query_unref (query); - - /* we need to 'commit' it, even if we already found the email - * address in the addressbook; thus, it will show up in the 'recent list' */ - if (contacts) { - g_debug ("%s already in the address book", address); - commit_contact ((EContact*)contacts->data, FALSE); - - g_list_foreach (contacts, (GFunc)unref_gobject, NULL); - g_list_free (contacts); - } else { - /* it's not yet in the addressbook, add it now! */ - EContact *new_contact = e_contact_new (); - gchar *display_address; - display_address = g_strdup (address); - if (display_address) { - modest_text_utils_get_display_address (display_address); - if ((display_address[0] != '\0') && (strlen (display_address) != strlen (address))) - e_contact_set (new_contact, E_CONTACT_FULL_NAME, (const gpointer)display_address); + iter = addresses; + to_commit_contacts = NULL; + to_add_contacts = NULL; + while (iter) { + EContact *contact; + const gchar *address; + + /* Look for a contact with such address. We perform + this kind of search because we assume that users + don't usually send emails to tons of addresses */ + address = (const gchar *) iter->data; + contact = get_contact_for_address (contacts, address); + + /* Add new or commit existing contact */ + if (contact) { + to_commit_contacts = g_list_prepend (to_commit_contacts, contact); + g_debug ("----Preparing to commit contact %s", address); + } else { + gchar *email_address, *display_address; + + /* Create new contact and add it to the list */ + contact = e_contact_new (); + email_address = modest_text_utils_get_email_address (address); + e_contact_set (contact, E_CONTACT_EMAIL_1, email_address); + g_free (email_address); + + display_address = g_strdup (address); + if (display_address) { + modest_text_utils_get_display_address (display_address); + if ((display_address[0] != '\0') && (strlen (display_address) != strlen (address))) + e_contact_set (contact, E_CONTACT_FULL_NAME, (const gpointer)display_address); + g_free (display_address); + } + + to_add_contacts = g_list_prepend (to_add_contacts, contact); + g_debug ("----Preparing to add contact %s", address); } - e_contact_set (new_contact, E_CONTACT_EMAIL_1, (const gpointer)email); - g_free (display_address); - commit_contact (new_contact, TRUE); - g_debug ("%s added to address book", address); - g_object_unref (new_contact); + + iter = g_slist_next (iter); } - g_free (email); + /* Asynchronously add contacts */ + if (to_add_contacts) + e_book_async_add_contacts (book, to_add_contacts, NULL, NULL); - return TRUE; + /* Asynchronously commit contacts */ + if (to_commit_contacts) + e_book_async_commit_contacts (book, to_commit_contacts, NULL, NULL); + + /* Free lists */ + g_list_free (to_add_contacts); + g_list_free (to_commit_contacts); + + frees: + if (addresses) { + g_slist_foreach (addresses, (GFunc) g_free, NULL); + g_slist_free (addresses); + } + if (contacts) + g_list_free (contacts); } -static gboolean -show_check_names_banner (gpointer userdata) + +static void +add_to_address_book (GSList *addresses) { - GtkWidget **banner = (GtkWidget **) userdata; + EBookQuery **queries, *composite_query; + gint num_add, i; - gdk_threads_enter (); - *banner = hildon_banner_show_animation (NULL, NULL, _("mail_ib_checking_names")); - gdk_threads_leave (); + g_return_if_fail (addresses); - return FALSE; + 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; + guint hide_banner_timeout; + gboolean hide; + gboolean free_info; +} CheckNamesInfo; + static void -hide_check_names_banner (GtkWidget **banner, guint banner_timeout) +hide_check_names_banner (CheckNamesInfo *info) { - if (*banner != NULL) { - gtk_widget_destroy (*banner); - *banner = NULL; + if (info->show_banner_timeout > 0) { + g_source_remove (info->show_banner_timeout); + info->show_banner_timeout = 0; + } + if (info->hide_banner_timeout > 0) { + info->hide = TRUE; + return; + } + + if (info->banner) { + gtk_widget_destroy (info->banner); + info->banner = NULL; + info->hide = FALSE; + } + + if (info->free_info) { + g_slice_free (CheckNamesInfo, info); + } +} + +static gboolean hide_banner_timeout_handler (CheckNamesInfo *info) +{ + info->hide_banner_timeout = 0; + if (info->hide) { + gtk_widget_destroy (info->banner); + info->banner = NULL; + } + if (info->free_info) { + g_slice_free (CheckNamesInfo, info); + } + return FALSE; +} + +static gboolean show_banner_timeout_handler (CheckNamesInfo *info) +{ + info->show_banner_timeout = 0; + info->banner = hildon_banner_show_animation (NULL, NULL, _("mail_ib_checking_names")); + info->hide_banner_timeout = g_timeout_add (1000, (GSourceFunc) hide_banner_timeout_handler, (gpointer) info); + return FALSE; +} + +static void show_check_names_banner (CheckNamesInfo *info) +{ + if (info->hide_banner_timeout > 0) { + g_source_remove (info->hide_banner_timeout); + info->hide_banner_timeout = 0; + } + + info->hide = FALSE; + if (info->show_banner_timeout > 0) + return; + + if (info->banner == NULL) { + info->show_banner_timeout = g_timeout_add (500, (GSourceFunc) show_banner_timeout_handler, (gpointer) info); + } +} + +static void clean_check_names_banner (CheckNamesInfo *info) +{ + if (info->hide_banner_timeout) { + info->free_info = TRUE; } else { - g_source_remove (banner_timeout); + if (info->show_banner_timeout) { + g_source_remove (info->show_banner_timeout); + } + if (info->banner) + gtk_widget_destroy (info->banner); + g_slice_free (CheckNamesInfo, info); } +} +void free_resolved_addresses_list (gpointer data, + gpointer ignored) +{ + GSList *list = (GSList *)data; + g_slist_foreach (list, (GFunc) g_free, NULL); + g_slist_free (list); } gboolean @@ -669,7 +748,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat { const gchar *recipients = NULL; GSList *start_indexes = NULL, *end_indexes = NULL; - GSList *current_start, *current_end; + GSList *current_start, *current_end, *to_commit_addresses; gboolean result = TRUE; GtkTextBuffer *buffer; gint offset_delta = 0; @@ -694,6 +773,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat current_start = start_indexes; current_end = end_indexes; buffer = modest_recpt_editor_get_buffer (recpt_editor); + to_commit_addresses = NULL; while (current_start != NULL) { gchar *address; @@ -704,7 +784,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); @@ -721,31 +801,47 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat char_in_string); g_free (char_in_string); hildon_banner_show_information (NULL, NULL, message ); - g_free (message); + g_free (message); result = FALSE; } else if (strstr (address, "@") == NULL) { /* here goes searching in addressbook */ - gchar *contact_id = NULL; + gboolean canceled; + GSList *contact_ids = NULL; GSList *resolved_addresses = NULL; - result = resolve_address (address, &resolved_addresses, &contact_id); + result = resolve_address (address, &resolved_addresses, &contact_ids, &canceled); if (result) { gint new_length; - /* replace string */ - modest_recpt_editor_replace_with_resolved_recipient (recpt_editor, - &start_iter, &end_iter, - resolved_addresses, - contact_id); - g_free (contact_id); - g_slist_foreach (resolved_addresses, (GFunc) g_free, NULL); + + modest_recpt_editor_replace_with_resolved_recipients (recpt_editor, + &start_iter, &end_iter, + resolved_addresses, + contact_ids); + g_slist_foreach (contact_ids, (GFunc) g_free, NULL); + g_slist_foreach (resolved_addresses, free_resolved_addresses_list, NULL); + g_slist_free (contact_ids); g_slist_free (resolved_addresses); /* update offset delta */ recipients = modest_recpt_editor_get_recipients (recpt_editor); new_length = g_utf8_strlen (recipients, -1); offset_delta = offset_delta + new_length - last_length; - last_length = new_length; + last_length = new_length; + } else { + if (canceled) { + /* We have to remove the recipient if not resolved */ + modest_recpt_editor_replace_with_resolved_recipient (recpt_editor, + &start_iter, + &end_iter, + NULL, + NULL); + } else { + /* There is no contact with that name so it's not + valid. Don't show any error because it'll be done + later */ + result = FALSE; + } } } else { /* this address is not valid, select it and return control to user showing banner */ @@ -782,9 +878,10 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat /* note: adding it the to the addressbook if it did not exist yet, * and adding it to the recent_list */ if (result && update_addressbook && store_address) - add_to_address_book (address); + to_commit_addresses = g_slist_prepend (to_commit_addresses, address); + else + g_free (address); - g_free (address); if (result == FALSE) break; @@ -792,6 +889,13 @@ 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) { + to_commit_addresses = modest_text_utils_remove_duplicate_addresses_list (to_commit_addresses); + if (to_commit_addresses) + add_to_address_book (to_commit_addresses); + } + if (current_start == NULL) { gtk_text_buffer_get_end_iter (buffer, &end_iter); gtk_text_buffer_place_cursor (buffer, &end_iter); @@ -806,12 +910,32 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat } +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) { EBookQuery *full_name_book_query = NULL; GList *result; gchar *unquoted; + GetContactsInfo *info; if (name == NULL) return NULL; @@ -820,90 +944,105 @@ get_contacts_for_name (const gchar *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_contacts (book, full_name_book_query, &result, NULL); + /* 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) { + GDK_THREADS_LEAVE (); + g_main_loop_run (info->mainloop); + GDK_THREADS_ENTER (); + } + result = info->result; e_book_query_unref (full_name_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) { - EBookQuery *full_name_book_query = NULL; - EBookView *book_view = NULL; + GtkWidget *contact_view; + OssoABookContactChooser *contact_dialog; GList *result = NULL; gchar *unquoted; - 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); + 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); - 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); + /* Enable multiselection */ + osso_abook_contact_chooser_set_maximum_selection (contact_dialog, G_MAXUINT); - contact_view = osso_abook_contact_selector_new_basic (contact_model); - contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view)); + /* 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_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); - - /* 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); - 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); -#endif /* MODEST_ABOOK_API < 4 */ - } + if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) + result = osso_abook_contact_chooser_get_selection (contact_dialog); + + g_free (unquoted); return result; } static gboolean -resolve_address (const gchar *address, GSList **resolved_addresses, gchar **contact_id) +resolve_address (const gchar *address, + GSList **resolved_addresses, + GSList **contact_ids, + gboolean *canceled) { GList *resolved_contacts; - guint banner_timeout; - GtkWidget *banner = NULL; + CheckNamesInfo *info;; - banner_timeout = g_timeout_add (500, show_check_names_banner, &banner); + g_return_val_if_fail (canceled, FALSE); - contact_model = osso_abook_contact_model_new (); - if (!open_addressbook_sync ()) { + *resolved_addresses = NULL; + *contact_ids = NULL; + *canceled = FALSE; + info = g_slice_new0 (CheckNamesInfo); + show_check_names_banner (info); + + contact_model = osso_abook_contact_model_get_default (); + if (!open_addressbook ()) { + hide_check_names_banner (info); if (contact_model) { g_object_unref (contact_model); contact_model = NULL; } + clean_check_names_banner (info); return FALSE; } resolved_contacts = get_contacts_for_name (address); + hide_check_names_banner (info); if (resolved_contacts == NULL) { /* no matching contacts for the search string */ modest_platform_run_information_dialog (NULL, _("mcen_nc_no_matching_contacts"), FALSE); - hide_check_names_banner (&banner, banner_timeout); - + clean_check_names_banner (info); return FALSE; } @@ -911,34 +1050,36 @@ resolve_address (const gchar *address, GSList **resolved_addresses, gchar **cont /* show a dialog to select the contact from the resolved ones */ g_list_free (resolved_contacts); - hide_check_names_banner (&banner, banner_timeout); resolved_contacts = select_contacts_for_name_dialog (address); - banner_timeout = g_timeout_add (500, show_check_names_banner, &banner); - } - + /* get the resolved contacts (can be no contact) */ if (resolved_contacts) { - gboolean found; - EContact *contact = (EContact *) resolved_contacts->data; - - *resolved_addresses = get_recipients_for_given_contact (contact); - hide_check_names_banner (&banner, banner_timeout); - if (*resolved_addresses) { - *contact_id = g_strdup (e_contact_get_const (contact, E_CONTACT_UID)); - found = TRUE; - } else { - found = FALSE; + GList *node; + gboolean found = FALSE; + + for (node = resolved_contacts; node != NULL; node = g_list_next (node)) { + EContact *contact = (EContact *) node->data; + GSList *resolved; + gchar *contact_id; + + resolved = get_recipients_for_given_contact (contact, canceled); + if (resolved) { + contact_id = g_strdup (e_contact_get_const (contact, E_CONTACT_UID)); + *contact_ids = g_slist_append (*contact_ids, contact_id); + found = TRUE; + *resolved_addresses = g_slist_append (*resolved_addresses, resolved); + } } - g_list_foreach (resolved_contacts, (GFunc)unref_gobject, NULL); g_list_free (resolved_contacts); + clean_check_names_banner (info); return found; } else { /* cancelled dialog to select more than one contact or * selected no contact */ - hide_check_names_banner (&banner, banner_timeout); + clean_check_names_banner (info); return FALSE; } @@ -961,7 +1102,7 @@ unquote_string (const gchar *str) if (*p == '\\') { g_string_append_unichar (buffer, g_utf8_get_char (p)); p = g_utf8_next_char (p); - + } g_string_append_unichar (buffer, g_utf8_get_char (p)); p = g_utf8_next_char (p); @@ -978,41 +1119,58 @@ 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); 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 (); +}