1 /* Copyright (c) 2007, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /* modest-address-book.c */
33 #include <glib/gi18n.h>
34 #include <modest-address-book.h>
35 #include <modest-text-utils.h>
36 #include <libebook/e-book.h>
37 #include <libebook/e-book-view.h>
38 #include <libebook/e-vcard.h>
39 #include "modest-hildon-includes.h"
40 #include <libosso-abook/osso-abook.h>
41 #include "modest-platform.h"
42 #include "modest-runtime.h"
43 #include "widgets/modest-window-mgr.h"
44 #include "widgets/modest-ui-constants.h"
46 #include <gtk/gtksizegroup.h>
47 #include <gtk/gtkbox.h>
48 #include <gtk/gtklabel.h>
49 #include <gtk/gtkcellrenderertext.h>
50 #include <gtk/gtktreeselection.h>
51 #include <gtk/gtkentry.h>
53 static OssoABookContactModel *contact_model = NULL;
54 static EBook *book = NULL;
55 static EBookView * book_view = NULL;
57 static GSList *get_recipients_for_given_contact(EContact * contact);
58 static void commit_contact(EContact * contact, gboolean is_new);
59 static gchar *get_email_addr_from_user(const gchar * given_name);
60 static gchar *ui_get_formatted_email_id(gchar * current_given_name,
61 gchar * current_sur_name, gchar * current_email_id);
62 static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name);
63 static GSList *select_email_addrs_for_contact(GList * email_addr_list);
64 static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, gchar **contact_id);
65 static gchar *unquote_string (const gchar *str);
69 unref_gobject (GObject *obj)
77 get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
79 if (status != E_BOOK_ERROR_OK) {
80 g_object_unref (book);
87 #if MODEST_ABOOK_API < 4
88 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model),
90 #else /* MODEST_ABOOK_API < 4 */
91 osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model),
93 #endif /* MODEST_ABOOK_API < 4 */
95 e_book_view_start (book_view);
99 book_open_cb (EBook *view, EBookStatus status, gpointer data)
101 EBookQuery *query = NULL;
103 if (status != E_BOOK_ERROR_OK) {
104 g_object_unref (book);
108 query = e_book_query_any_field_contains ("");
109 e_book_async_get_book_view (book, query, NULL, -1, get_book_view_cb, NULL);
110 e_book_query_unref (query);
116 book = e_book_new_system_addressbook (NULL);
120 if (e_book_async_open (book, FALSE, book_open_cb, NULL) != E_BOOK_ERROR_OK)
123 return TRUE; /* FIXME */
127 open_addressbook_sync ()
129 book = e_book_new_system_addressbook (NULL);
133 return e_book_open (book, FALSE, NULL);
137 modest_address_book_add_address (const gchar *address)
139 GtkWidget *dialog = NULL;
140 gchar *email_address;
141 EVCardAttribute *attribute;
143 if (!open_addressbook ()) {
147 email_address = modest_text_utils_get_email_address (address);
149 attribute = e_vcard_attribute_new (NULL, EVC_EMAIL);
150 e_vcard_attribute_add_value (attribute, email_address);
151 dialog = osso_abook_temporary_contact_dialog_new (NULL, book, attribute, NULL);
152 gtk_dialog_run (GTK_DIALOG (dialog));
154 gtk_widget_destroy (dialog);
156 e_vcard_attribute_free (attribute);
157 g_free (email_address);
162 modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
164 #if MODEST_ABOOK_API < 4
165 GtkWidget *contact_view = NULL;
166 GtkWidget *contact_dialog;
168 #else /* MODEST_ABOOK_API < 4 */
169 GtkWidget *contact_chooser = NULL;
170 #endif /* MODEST_ABOOK_API < 4 */
172 GList *contacts_list = NULL;
173 GSList *email_addrs_per_contact = NULL;
175 gboolean focus_recpt_editor = FALSE;
177 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
179 #if MODEST_ABOOK_API < 4
180 if (!open_addressbook ()) {
182 g_object_unref (contact_model);
183 contact_model = NULL;
187 contact_model = osso_abook_contact_model_new ();
189 contact_view = osso_abook_contact_selector_new_basic (contact_model);
190 osso_abook_contact_selector_set_minimum_selection (OSSO_ABOOK_CONTACT_SELECTOR (contact_view), 1);
192 contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
193 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (recpt_editor));
194 modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (contact_dialog), GTK_WINDOW (toplevel));
196 gtk_widget_show (contact_dialog);
198 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
200 osso_abook_contact_selector_get_extended_selection (OSSO_ABOOK_CONTACT_SELECTOR
203 #else /* MODEST_ABOOK_API < 4 */
204 /* TODO: figure out how to make the contact chooser modal */
205 contact_chooser = osso_abook_contact_chooser_new_with_capabilities (NULL,
206 _AB("addr_ti_dia_select_contacts"),
207 OSSO_ABOOK_CAPS_EMAIL,
208 OSSO_ABOOK_CONTACT_ORDER_NAME);
209 /* Enable multiselection */
210 osso_abook_contact_chooser_set_maximum_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_chooser),
213 if (gtk_dialog_run (GTK_DIALOG (contact_chooser)) == GTK_RESPONSE_OK)
214 contacts_list = osso_abook_contact_chooser_get_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_chooser));
215 gtk_widget_destroy (contact_chooser);
221 for (node = contacts_list; node != NULL; node = g_list_next (node)) {
222 EContact *contact = (EContact *) node->data;
224 email_addrs_per_contact = get_recipients_for_given_contact (contact);
225 if (email_addrs_per_contact) {
226 econtact_id = (gchar *) e_contact_get_const (contact, E_CONTACT_UID);
227 modest_recpt_editor_add_resolved_recipient (MODEST_RECPT_EDITOR (recpt_editor),
228 email_addrs_per_contact, econtact_id);
229 g_slist_foreach (email_addrs_per_contact, (GFunc) g_free, NULL);
230 g_slist_free (email_addrs_per_contact);
231 email_addrs_per_contact = NULL;
232 focus_recpt_editor = TRUE;
235 g_list_free (contacts_list);
238 #if MODEST_ABOOK_API < 4
240 g_object_unref (contact_model);
241 contact_model = NULL;
244 gtk_widget_destroy (contact_dialog);
247 if (focus_recpt_editor)
248 modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (recpt_editor));
253 * This function returns the resolved recipients for a given EContact.
254 * If no e-mail address is defined, it launches 'Add e-mail address to contact'
255 * dialog to obtain one. If multiple e-mail addresses are found, it launches
256 * 'Select e-mail address' dialog to allow user to select one or more e-mail
257 * addresses for that contact.
259 * @param Contact of type #EContact
260 * @return List of resolved recipient strings, to be freed by calling function.
262 static GSList *get_recipients_for_given_contact(EContact * contact)
264 gchar *givenname = NULL;
265 gchar *familyname = NULL;
266 gchar *nickname = NULL;
267 gchar *emailid = NULL;
268 const gchar *display_name = NULL;
270 gchar *formatted_string = NULL;
271 gboolean email_not_present = FALSE;
272 GSList *formattedlist = NULL, *selected_email_addr_list = NULL, *node = NULL;
278 givenname = (gchar *) e_contact_get_const(contact, E_CONTACT_GIVEN_NAME);
279 familyname = (gchar *) e_contact_get_const(contact, E_CONTACT_FAMILY_NAME);
280 nickname = (gchar *) e_contact_get_const(contact, E_CONTACT_NICKNAME);
283 list = (GList *) e_contact_get(contact, E_CONTACT_EMAIL);
286 email_not_present = TRUE;
289 if (list && g_list_length(list) == 1) {
290 if (list->data == NULL || g_utf8_strlen(list->data, -1) == 0) {
291 email_not_present = TRUE;
293 emailid = g_strstrip(g_strdup(list->data));
294 if (g_utf8_strlen(emailid, -1) == 0) {
296 email_not_present = TRUE;
301 /*Launch the 'Add e-mail addr to contact' dialog if required */
302 if (email_not_present) {
303 #if MODEST_ABOOK_API < 4
304 display_name = osso_abook_contact_get_display_name(contact);
306 OssoABookContact *abook_contact;
308 abook_contact = osso_abook_contact_new_from_template (contact);
309 display_name = osso_abook_contact_get_display_name(abook_contact);
312 emailid = get_email_addr_from_user(display_name);
314 e_contact_set(contact, E_CONTACT_EMAIL_1, emailid);
315 commit_contact(contact, FALSE);
317 #if MODEST_ABOOK_API >= 4
318 g_object_unref (abook_contact);
323 if (givenname || familyname)
325 ui_get_formatted_email_id(givenname, familyname, emailid);
327 formatted_string = g_strdup(emailid);
328 formattedlist = g_slist_append(formattedlist, formatted_string);
332 /*Launch the 'Select e-mail address' dialog if required */
333 if (g_list_length(list) > 1) {
334 selected_email_addr_list = select_email_addrs_for_contact(list);
335 for (node = selected_email_addr_list; node != NULL; node = node->next) {
336 if (givenname || familyname)
338 ui_get_formatted_email_id(givenname, familyname, node->data);
340 formatted_string = g_strdup(node->data);
341 formattedlist = g_slist_append(formattedlist, formatted_string);
343 if (selected_email_addr_list) {
344 g_slist_foreach(selected_email_addr_list, (GFunc) g_free, NULL);
345 g_slist_free(selected_email_addr_list);
350 g_list_foreach(list, (GFunc) g_free, NULL);
354 return formattedlist;
358 * This is a helper function to commit a EContact to Address_Book application.
360 * @param contact Contact of type #EContact
364 commit_contact(EContact * contact, gboolean is_new)
366 g_return_if_fail (contact);
367 g_return_if_fail (book);
369 if (!contact || !book)
372 #if MODEST_ABOOK_API < 4
373 osso_abook_contact_commit(contact, is_new, book);
375 if (OSSO_ABOOK_IS_CONTACT (contact)) {
376 osso_abook_contact_commit(OSSO_ABOOK_CONTACT(contact), is_new, book, NULL);
379 e_book_add_contact (book, contact, NULL);
381 e_book_commit_contact (book, contact, NULL);
383 #endif /* MODEST_ABOOK_API < 2 */
387 * This is a helper function used to launch 'Add e-mail address to contact' dialog
388 * after showing appropriate notification, when there is no e-mail address defined
389 * for a selected contact.
391 * @param given_name Given name of the contact
392 * @param family_name Family name of the contact
393 * @return E-mail address string entered by user, to be freed by calling function.
396 get_email_addr_from_user(const gchar * given_name)
398 gchar *notification = NULL;
399 gchar *email_addr = NULL;
401 gboolean note_response;
404 notification = g_strdup_printf(_("mcen_nc_email_address_not_defined"), given_name);
406 note = hildon_note_new_confirmation (NULL, notification);
407 note_response = gtk_dialog_run (GTK_DIALOG(note));
408 gtk_widget_destroy (note);
409 g_free(notification);
411 if (note_response == GTK_RESPONSE_OK) {
412 email_addr = run_add_email_addr_to_contact_dlg(given_name);
419 This function is used to get the formated email id with given name and sur name
420 in the format "GIVENNAME SURNAME <EMAIL ADDRESS>".
421 @param current_given_name to hold the given name
422 @param current_sur_name to hold the sur name
423 @param current_email_id to hold the email id.
424 @return gchar* string to be freed by calling function
427 ui_get_formatted_email_id(gchar * current_given_name,
428 gchar * current_sur_name, gchar * current_email_id)
430 GString *email_id_str = NULL;
432 email_id_str = g_string_new(NULL);
434 if ((current_given_name != NULL) && ((strlen(current_given_name) != 0))
435 && (current_sur_name != NULL) && ((strlen(current_sur_name) != 0))) {
436 g_string_append_printf(email_id_str, "%s %s", current_given_name, current_sur_name);
437 } else if ((current_given_name != NULL) && (strlen(current_given_name) != 0)) {
438 g_string_append_printf(email_id_str, "%s", current_given_name);
439 } else if ((current_sur_name != NULL) && (strlen(current_sur_name) != 0)) {
440 g_string_append_printf(email_id_str, "%s", current_sur_name);
442 if (g_utf8_strchr (email_id_str->str, -1, ' ')) {
443 g_string_prepend_c (email_id_str, '\"');
444 g_string_append_c (email_id_str, '\"');
446 g_string_append_printf (email_id_str, " %c%s%c", '<', current_email_id, '>');
447 return g_string_free (email_id_str, FALSE);
451 * This is a helper function used to create & run 'Add e-mail address to contact' dialog.
452 * It allows user to enter an e-mail address, and shows appropriate infonote if the
453 * entered string is not a valid e-mail address.
455 * @param contact_name Full name of the contact
456 * @return E-mail address string entered by user, to be freed by calling function.
459 run_add_email_addr_to_contact_dlg(const gchar * contact_name)
461 GtkWidget *add_email_addr_to_contact_dlg = NULL;
462 GtkSizeGroup *size_group = NULL;
463 GtkWidget *cptn_cntrl = NULL;
464 GtkWidget *name_label = NULL;
465 GtkWidget *email_entry = NULL;
467 gchar *new_email_addr = NULL;
468 gboolean run_dialog = TRUE;
470 add_email_addr_to_contact_dlg =
471 gtk_dialog_new_with_buttons(_AB("addr_ti_sel_contact"), NULL,
472 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
473 _HL("wdgt_bd_done"), GTK_RESPONSE_ACCEPT, NULL);
474 gtk_dialog_set_has_separator(GTK_DIALOG(add_email_addr_to_contact_dlg), FALSE);
475 /*Set app_name & state_save related tags to the window */
477 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
478 name_label = gtk_label_new(contact_name);
479 gtk_misc_set_alignment(GTK_MISC(name_label), 0, 0);
481 hildon_caption_new(size_group, _("mcen_ia_add_email_name"), name_label, NULL,
482 HILDON_CAPTION_OPTIONAL);
483 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
486 email_entry = gtk_entry_new();
488 hildon_caption_new(size_group, _("mcen_fi_add_email_name"), email_entry, NULL,
489 HILDON_CAPTION_OPTIONAL);
490 hildon_gtk_entry_set_input_mode(GTK_ENTRY(email_entry), HILDON_GTK_INPUT_MODE_FULL);
491 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
494 gtk_widget_show_all(add_email_addr_to_contact_dlg);
498 gtk_widget_grab_focus(email_entry);
499 result = gtk_dialog_run(GTK_DIALOG(add_email_addr_to_contact_dlg));
501 if (result == GTK_RESPONSE_ACCEPT) {
502 const gchar *invalid_char_offset = NULL;
503 new_email_addr = g_strdup(gtk_entry_get_text(GTK_ENTRY(email_entry)));
504 new_email_addr = g_strstrip(new_email_addr);
505 if (!modest_text_utils_validate_email_address (new_email_addr, &invalid_char_offset)) {
506 gtk_widget_grab_focus(email_entry);
507 if ((invalid_char_offset != NULL)&&(*invalid_char_offset != '\0')) {
508 gchar *char_in_string = g_strdup_printf ("%c", *invalid_char_offset);
509 gchar *message = g_strdup_printf(
510 _CS("ckdg_ib_illegal_characters_entered"),
512 hildon_banner_show_information (
513 add_email_addr_to_contact_dlg, NULL, message );
516 hildon_banner_show_information (add_email_addr_to_contact_dlg, NULL, _("mcen_ib_invalid_email"));
519 gtk_editable_select_region((GtkEditable *) email_entry, 0, -1);
520 g_free(new_email_addr);
521 new_email_addr = NULL;
526 gtk_widget_destroy(add_email_addr_to_contact_dlg);
528 return new_email_addr;
532 * This is helper function to create & run 'Select e-mail address' dialog, used when
533 * multiple e-mail addresses are found for a selected contact. It allows user to select
534 * one or more e-mail addresses for that contact.
536 * @param email_addr_list List of e-mail addresses for that contact
537 * @return List of user selected e-mail addresses, to be freed by calling function.
540 select_email_addrs_for_contact(GList * email_addr_list)
542 GtkWidget *select_email_addr_dlg = NULL;
543 GSList *selected_email_addr_list = NULL;
548 if (!email_addr_list)
551 select_email_addr_dlg = hildon_picker_dialog_new (NULL);
552 gtk_window_set_title (GTK_WINDOW (select_email_addr_dlg), _("mcen_ti_select_email_title"));
554 selector = hildon_touch_selector_new_text ();
555 for (node = email_addr_list; node != NULL && node->data != NULL; node = node->next) {
557 email_addr = g_strstrip(g_strdup(node->data));
558 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), email_addr);
562 hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (select_email_addr_dlg),
563 HILDON_TOUCH_SELECTOR (selector));
564 gtk_window_set_default_size (GTK_WINDOW (select_email_addr_dlg), MODEST_DIALOG_WINDOW_MAX_HEIGHT, -1);
566 gtk_widget_show_all(select_email_addr_dlg);
567 result = gtk_dialog_run(GTK_DIALOG(select_email_addr_dlg));
569 if (result == GTK_RESPONSE_OK) {
572 current_text = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector));
573 selected_email_addr_list = g_slist_append (selected_email_addr_list, current_text);
577 gtk_widget_destroy(select_email_addr_dlg);
578 return selected_email_addr_list;
582 static gboolean /* make this public? */
583 add_to_address_book (const gchar* address)
586 GList *contacts = NULL;
590 g_return_val_if_fail (address, FALSE);
593 if (!open_addressbook ()) {
594 g_return_val_if_reached (FALSE);
598 g_return_val_if_fail (book, FALSE);
600 email = modest_text_utils_get_email_address (address);
602 query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, email);
603 if (!e_book_get_contacts (book, query, &contacts, &err)) {
604 g_printerr ("modest: failed to get contacts: %s",
605 err ? err->message : "<unknown>");
610 e_book_query_unref (query);
612 /* we need to 'commit' it, even if we already found the email
613 * address in the addressbook; thus, it will show up in the 'recent list' */
615 g_debug ("%s already in the address book", address);
616 commit_contact ((EContact*)contacts->data, FALSE);
618 g_list_foreach (contacts, (GFunc)unref_gobject, NULL);
619 g_list_free (contacts);
622 /* it's not yet in the addressbook, add it now! */
623 EContact *new_contact = e_contact_new ();
624 gchar *display_address;
625 display_address = g_strdup (address);
626 if (display_address) {
627 modest_text_utils_get_display_address (display_address);
628 if ((display_address[0] != '\0') && (strlen (display_address) != strlen (address)))
629 e_contact_set (new_contact, E_CONTACT_FULL_NAME, (const gpointer)display_address);
631 e_contact_set (new_contact, E_CONTACT_EMAIL_1, (const gpointer)email);
632 g_free (display_address);
633 commit_contact (new_contact, TRUE);
634 g_debug ("%s added to address book", address);
635 g_object_unref (new_contact);
644 show_check_names_banner (gpointer userdata)
646 GtkWidget **banner = (GtkWidget **) userdata;
648 gdk_threads_enter ();
649 *banner = hildon_banner_show_animation (NULL, NULL, _("mail_ib_checking_names"));
650 gdk_threads_leave ();
656 hide_check_names_banner (GtkWidget **banner, guint banner_timeout)
658 if (*banner != NULL) {
659 gtk_widget_destroy (*banner);
662 g_source_remove (banner_timeout);
668 modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean update_addressbook)
670 const gchar *recipients = NULL;
671 GSList *start_indexes = NULL, *end_indexes = NULL;
672 GSList *current_start, *current_end;
673 gboolean result = TRUE;
674 GtkTextBuffer *buffer;
675 gint offset_delta = 0;
677 GtkTextIter start_iter, end_iter;
679 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
681 recipients = modest_recpt_editor_get_recipients (recpt_editor);
682 last_length = g_utf8_strlen (recipients, -1);
683 modest_text_utils_get_addresses_indexes (recipients, &start_indexes, &end_indexes);
685 if (start_indexes == NULL) {
686 if (last_length != 0) {
687 hildon_banner_show_information (NULL, NULL, _("mcen_nc_no_matching_contacts"));
694 current_start = start_indexes;
695 current_end = end_indexes;
696 buffer = modest_recpt_editor_get_buffer (recpt_editor);
698 while (current_start != NULL) {
700 gchar *start_ptr, *end_ptr;
701 gint start_pos, end_pos;
702 const gchar *invalid_char_position = NULL;
703 gboolean store_address = FALSE;
705 start_pos = (*((gint*) current_start->data)) + offset_delta;
706 end_pos = (*((gint*) current_end->data)) + offset_delta;
708 start_ptr = g_utf8_offset_to_pointer (recipients, start_pos);
709 end_ptr = g_utf8_offset_to_pointer (recipients, end_pos);
711 address = g_strstrip (g_strndup (start_ptr, end_ptr - start_ptr));
712 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_pos);
713 gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_pos);
714 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
716 if (!modest_text_utils_validate_recipient (address, &invalid_char_position)) {
717 if ((invalid_char_position != NULL) && (*invalid_char_position != '\0')) {
718 gchar *char_in_string = g_strdup_printf("%c", *invalid_char_position);
720 g_strdup_printf(_CS("ckdg_ib_illegal_characters_entered"),
722 g_free (char_in_string);
723 hildon_banner_show_information (NULL, NULL, message );
726 } else if (strstr (address, "@") == NULL) {
727 /* here goes searching in addressbook */
728 gchar *contact_id = NULL;
729 GSList *resolved_addresses = NULL;
731 result = resolve_address (address, &resolved_addresses, &contact_id);
736 modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
737 &start_iter, &end_iter,
741 g_slist_foreach (resolved_addresses, (GFunc) g_free, NULL);
742 g_slist_free (resolved_addresses);
744 /* update offset delta */
745 recipients = modest_recpt_editor_get_recipients (recpt_editor);
746 new_length = g_utf8_strlen (recipients, -1);
747 offset_delta = offset_delta + new_length - last_length;
748 last_length = new_length;
751 /* this address is not valid, select it and return control to user showing banner */
752 hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
757 gboolean has_recipient = FALSE;
759 tags = gtk_text_iter_get_tags (&start_iter);
760 for (node = tags; node != NULL; node = g_slist_next (node)) {
761 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
762 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id") != NULL) {
763 has_recipient = TRUE;
768 if (!has_recipient) {
769 GSList * address_list = NULL;
771 address_list = g_slist_prepend (address_list, address);
772 modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
773 &start_iter, &end_iter,
776 g_slist_free (address_list);
777 store_address = TRUE;
781 /* so, it seems a valid address */
782 /* note: adding it the to the addressbook if it did not exist yet,
783 * and adding it to the recent_list */
784 if (result && update_addressbook && store_address)
785 add_to_address_book (address);
791 current_start = g_slist_next (current_start);
792 current_end = g_slist_next (current_end);
795 if (current_start == NULL) {
796 gtk_text_buffer_get_end_iter (buffer, &end_iter);
797 gtk_text_buffer_place_cursor (buffer, &end_iter);
800 g_slist_foreach (start_indexes, (GFunc) g_free, NULL);
801 g_slist_foreach (end_indexes, (GFunc) g_free, NULL);
802 g_slist_free (start_indexes);
803 g_slist_free (end_indexes);
810 get_contacts_for_name (const gchar *name)
812 EBookQuery *full_name_book_query = NULL;
819 unquoted = unquote_string (name);
820 full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted);
823 e_book_get_contacts (book, full_name_book_query, &result, NULL);
824 e_book_query_unref (full_name_book_query);
830 select_contacts_for_name_dialog (const gchar *name)
832 EBookQuery *full_name_book_query = NULL;
833 EBookView *book_view = NULL;
834 GList *result = NULL;
837 unquoted = unquote_string (name);
838 full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted);
840 e_book_get_book_view (book, full_name_book_query, NULL, -1, &book_view, NULL);
841 e_book_query_unref (full_name_book_query);
844 GtkWidget *contact_dialog = NULL;
845 #if MODEST_ABOOK_API < 4
846 GtkWidget *contact_view = NULL;
847 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model), book_view);
848 e_book_view_start (book_view);
850 contact_view = osso_abook_contact_selector_new_basic (contact_model);
851 contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
853 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
854 result = osso_abook_contact_view_get_selection (OSSO_ABOOK_CONTACT_VIEW (contact_view));
856 e_book_view_stop (book_view);
857 g_object_unref (book_view);
858 gtk_widget_destroy (contact_dialog);
859 #else /* MODEST_ABOOK_API < 4 */
860 osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model), book_view);
861 e_book_view_start (book_view);
863 /* TODO: figure out how to make the contact chooser modal */
864 contact_dialog = osso_abook_contact_chooser_new_with_capabilities (NULL,
865 _AB("addr_ti_dia_select_contacts"),
866 OSSO_ABOOK_CAPS_EMAIL,
867 OSSO_ABOOK_CONTACT_ORDER_NAME);
868 osso_abook_contact_chooser_set_model (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
871 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK)
872 result = osso_abook_contact_chooser_get_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog));
873 e_book_view_stop (book_view);
874 g_object_unref (book_view);
875 gtk_widget_destroy (contact_dialog);
876 #endif /* MODEST_ABOOK_API < 4 */
883 resolve_address (const gchar *address, GSList **resolved_addresses, gchar **contact_id)
885 GList *resolved_contacts;
886 guint banner_timeout;
887 GtkWidget *banner = NULL;
889 banner_timeout = g_timeout_add (500, show_check_names_banner, &banner);
891 contact_model = osso_abook_contact_model_new ();
892 if (!open_addressbook_sync ()) {
894 g_object_unref (contact_model);
895 contact_model = NULL;
900 resolved_contacts = get_contacts_for_name (address);
902 if (resolved_contacts == NULL) {
903 /* no matching contacts for the search string */
904 modest_platform_run_information_dialog (NULL, _("mcen_nc_no_matching_contacts"), FALSE);
905 hide_check_names_banner (&banner, banner_timeout);
910 if (g_list_length (resolved_contacts) > 1) {
911 /* show a dialog to select the contact from the resolved ones */
912 g_list_free (resolved_contacts);
914 hide_check_names_banner (&banner, banner_timeout);
915 resolved_contacts = select_contacts_for_name_dialog (address);
916 banner_timeout = g_timeout_add (500, show_check_names_banner, &banner);
920 /* get the resolved contacts (can be no contact) */
921 if (resolved_contacts) {
923 EContact *contact = (EContact *) resolved_contacts->data;
925 *resolved_addresses = get_recipients_for_given_contact (contact);
926 hide_check_names_banner (&banner, banner_timeout);
927 if (*resolved_addresses) {
928 *contact_id = g_strdup (e_contact_get_const (contact, E_CONTACT_UID));
934 g_list_foreach (resolved_contacts, (GFunc)unref_gobject, NULL);
935 g_list_free (resolved_contacts);
939 /* cancelled dialog to select more than one contact or
940 * selected no contact */
941 hide_check_names_banner (&banner, banner_timeout);
948 unquote_string (const gchar *str)
956 buffer = g_string_new_len (NULL, strlen (str));
957 for (p = (gchar *) str; *p != '\0'; p = g_utf8_next_char (p)) {
959 p = g_utf8_next_char (p);
960 while ((*p != '\0')&&(*p != '"')) {
962 g_string_append_unichar (buffer, g_utf8_get_char (p));
963 p = g_utf8_next_char (p);
966 g_string_append_unichar (buffer, g_utf8_get_char (p));
967 p = g_utf8_next_char (p);
970 g_string_append_unichar (buffer, g_utf8_get_char (p));
974 return g_string_free (buffer, FALSE);
979 modest_address_book_has_address (const gchar *address)
982 GList *contacts = NULL;
987 g_return_val_if_fail (address, FALSE);
990 if (!open_addressbook ()) {
991 g_return_val_if_reached (FALSE);
995 g_return_val_if_fail (book, FALSE);
997 email = modest_text_utils_get_email_address (address);
999 query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, email);
1000 if (!e_book_get_contacts (book, query, &contacts, &err)) {
1001 g_printerr ("modest: failed to get contacts: %s",
1002 err ? err->message : "<unknown>");
1007 e_book_query_unref (query);
1009 result = (contacts != NULL);
1011 g_list_foreach (contacts, (GFunc)unref_gobject, NULL);
1012 g_list_free (contacts);