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 <libosso-abook/osso-abook.h>
39 #include "modest-hildon-includes.h"
40 #include "modest-platform.h"
42 #include <gtk/gtksizegroup.h>
43 #include <gtk/gtkbox.h>
44 #include <gtk/gtklabel.h>
45 #include <gtk/gtkcellrenderertext.h>
46 #include <gtk/gtktreeselection.h>
47 #include <gtk/gtkentry.h>
49 static OssoABookContactModel *contact_model = NULL;
50 static EBook *book = NULL;
51 static EBookView * book_view = NULL;
53 static GSList *get_recipients_for_given_contact(EContact * contact);
54 static void commit_contact(EContact * contact, gboolean is_new);
55 static gchar *get_email_addr_from_user(const gchar * given_name);
56 static gchar *ui_get_formatted_email_id(gchar * current_given_name,
57 gchar * current_sur_name, gchar * current_email_id);
58 static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name);
59 static GSList *select_email_addrs_for_contact(GList * email_addr_list);
60 static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, gchar **contact_id);
61 static gchar *unquote_string (const gchar *str);
65 unref_gobject (GObject *obj)
73 get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
75 if (status != E_BOOK_ERROR_OK) {
76 g_object_unref (book);
83 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model),
86 e_book_view_start (book_view);
90 book_open_cb (EBook *view, EBookStatus status, gpointer data)
92 EBookQuery *query = NULL;
94 if (status != E_BOOK_ERROR_OK) {
95 g_object_unref (book);
99 query = e_book_query_any_field_contains ("");
100 e_book_async_get_book_view (book, query, NULL, -1, get_book_view_cb, NULL);
101 e_book_query_unref (query);
107 book = e_book_new_system_addressbook (NULL);
111 e_book_async_open (book, FALSE, book_open_cb, NULL);
113 return TRUE; /* FIXME */
117 open_addressbook_sync ()
119 book = e_book_new_system_addressbook (NULL);
123 e_book_open (book, FALSE, NULL);
129 modest_address_book_add_address (const gchar *address)
131 OssoABookAccount *account = NULL;
132 GtkWidget *dialog = NULL;
133 gchar *email_address = NULL;
135 contact_model = osso_abook_contact_model_new ();
136 if (!open_addressbook ()) {
138 g_object_unref (contact_model);
139 contact_model = NULL;
144 email_address = modest_text_utils_get_email_address (address);
146 account = osso_abook_account_get (EVC_EMAIL, NULL, email_address);
147 g_free (email_address);
150 dialog = osso_abook_add_to_contacts_dialog_new (contact_model, account);
151 g_object_unref (account);
152 gtk_dialog_run (GTK_DIALOG (dialog));
155 g_object_unref (contact_model);
156 contact_model = NULL;
159 gtk_widget_destroy (dialog);
165 modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
167 GtkWidget *contact_view = NULL;
168 GList *contacts_list = NULL;
169 GtkWidget *contact_dialog;
170 GSList *email_addrs_per_contact = NULL;
172 gboolean focus_recpt_editor = FALSE;
175 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
177 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (recpt_editor));
179 if (!open_addressbook ()) {
181 g_object_unref (contact_model);
182 contact_model = NULL;
186 contact_model = osso_abook_contact_model_new ();
188 contact_view = osso_abook_contact_selector_new_basic (contact_model);
189 osso_abook_contact_selector_set_minimum_selection (OSSO_ABOOK_CONTACT_SELECTOR (contact_view), 1);
191 contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
192 gtk_window_set_title (GTK_WINDOW (contact_dialog), _("mcen_ti_select_recipients"));
194 gtk_widget_show (contact_dialog);
196 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
198 osso_abook_contact_selector_get_extended_selection (OSSO_ABOOK_CONTACT_SELECTOR
205 for (node = contacts_list; node != NULL; node = g_list_next (node)) {
206 EContact *contact = (EContact *) node->data;
208 email_addrs_per_contact = get_recipients_for_given_contact (contact);
209 if (email_addrs_per_contact) {
210 econtact_id = (gchar *) e_contact_get_const (contact, E_CONTACT_UID);
211 modest_recpt_editor_add_resolved_recipient (MODEST_RECPT_EDITOR (recpt_editor),
212 email_addrs_per_contact, econtact_id);
213 g_slist_foreach (email_addrs_per_contact, (GFunc) g_free, NULL);
214 g_slist_free (email_addrs_per_contact);
215 email_addrs_per_contact = NULL;
216 focus_recpt_editor = TRUE;
219 g_list_free (contacts_list);
223 g_object_unref (contact_model);
224 contact_model = NULL;
227 gtk_widget_destroy (contact_dialog);
229 if (focus_recpt_editor)
230 modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (recpt_editor));
235 * This function returns the resolved recipients for a given EContact.
236 * If no e-mail address is defined, it launches 'Add e-mail address to contact'
237 * dialog to obtain one. If multiple e-mail addresses are found, it launches
238 * 'Select e-mail address' dialog to allow user to select one or more e-mail
239 * addresses for that contact.
241 * @param Contact of type #EContact
242 * @return List of resolved recipient strings, to be freed by calling function.
244 static GSList *get_recipients_for_given_contact(EContact * contact)
246 gchar *givenname = NULL;
247 gchar *familyname = NULL;
248 gchar *nickname = NULL;
249 gchar *emailid = NULL;
250 const gchar *display_name = NULL;
252 gchar *formatted_string = NULL;
253 gboolean email_not_present = FALSE;
254 GSList *formattedlist = NULL, *selected_email_addr_list = NULL, *node = NULL;
260 givenname = (gchar *) e_contact_get_const(contact, E_CONTACT_GIVEN_NAME);
261 familyname = (gchar *) e_contact_get_const(contact, E_CONTACT_FAMILY_NAME);
262 nickname = (gchar *) e_contact_get_const(contact, E_CONTACT_NICKNAME);
265 list = (GList *) e_contact_get(contact, E_CONTACT_EMAIL);
268 email_not_present = TRUE;
271 if (list && g_list_length(list) == 1) {
272 if (list->data == NULL || g_utf8_strlen(list->data, -1) == 0) {
273 email_not_present = TRUE;
275 emailid = g_strstrip(g_strdup(list->data));
276 if (g_utf8_strlen(emailid, -1) == 0) {
278 email_not_present = TRUE;
283 /*Launch the 'Add e-mail addr to contact' dialog if required */
284 if (email_not_present) {
285 display_name = osso_abook_contact_get_display_name(contact);
286 emailid = get_email_addr_from_user(display_name);
289 e_contact_set(contact, E_CONTACT_EMAIL_1, emailid);
290 commit_contact(contact, FALSE);
295 if (givenname || familyname)
297 ui_get_formatted_email_id(givenname, familyname, emailid);
299 formatted_string = g_strdup(emailid);
300 formattedlist = g_slist_append(formattedlist, formatted_string);
304 /*Launch the 'Select e-mail address' dialog if required */
305 if (g_list_length(list) > 1) {
306 selected_email_addr_list = select_email_addrs_for_contact(list);
307 for (node = selected_email_addr_list; node != NULL; node = node->next) {
308 if (givenname || familyname)
310 ui_get_formatted_email_id(givenname, familyname, node->data);
312 formatted_string = g_strdup(node->data);
313 formattedlist = g_slist_append(formattedlist, formatted_string);
315 if (selected_email_addr_list) {
316 g_slist_foreach(selected_email_addr_list, (GFunc) g_free, NULL);
317 g_slist_free(selected_email_addr_list);
322 g_list_foreach(list, (GFunc) g_free, NULL);
326 return formattedlist;
330 * This is a helper function to commit a EContact to Address_Book application.
332 * @param contact Contact of type #EContact
336 commit_contact(EContact * contact, gboolean is_new)
338 g_return_if_fail (contact);
339 g_return_if_fail (book);
341 if (!contact || !book)
344 #ifdef MODEST_HAVE_OLD_ABOOK
345 osso_abook_contact_commit(contact, is_new, book);
347 osso_abook_contact_commit(contact, is_new, book, NULL);
348 #endif /* MODEST_HILDON_VERSION_0 */
352 * This is a helper function used to launch 'Add e-mail address to contact' dialog
353 * after showing appropriate notification, when there is no e-mail address defined
354 * for a selected contact.
356 * @param given_name Given name of the contact
357 * @param family_name Family name of the contact
358 * @return E-mail address string entered by user, to be freed by calling function.
361 get_email_addr_from_user(const gchar * given_name)
363 gchar *notification = NULL;
364 gchar *email_addr = NULL;
366 gboolean note_response;
369 notification = g_strdup_printf(_("mcen_nc_email_address_not_defined"), given_name);
371 note = hildon_note_new_confirmation (NULL, notification);
372 note_response = gtk_dialog_run (GTK_DIALOG(note));
373 gtk_widget_destroy (note);
374 g_free(notification);
376 if (note_response == GTK_RESPONSE_OK) {
377 email_addr = run_add_email_addr_to_contact_dlg(given_name);
384 This function is used to get the formated email id with given name and sur name
385 in the format "GIVENNAME SURNAME <EMAIL ADDRESS>".
386 @param current_given_name to hold the given name
387 @param current_sur_name to hold the sur name
388 @param current_email_id to hold the email id.
389 @return gchar* string to be freed by calling function
392 ui_get_formatted_email_id(gchar * current_given_name,
393 gchar * current_sur_name, gchar * current_email_id)
395 GString *email_id_str = NULL;
397 email_id_str = g_string_new(NULL);
399 if ((current_given_name != NULL) && ((strlen(current_given_name) != 0))
400 && (current_sur_name != NULL) && ((strlen(current_sur_name) != 0))) {
401 g_string_append_printf(email_id_str, "%s %s", current_given_name, current_sur_name);
402 } else if ((current_given_name != NULL) && (strlen(current_given_name) != 0)) {
403 g_string_append_printf(email_id_str, "%s", current_given_name);
404 } else if ((current_sur_name != NULL) && (strlen(current_sur_name) != 0)) {
405 g_string_append_printf(email_id_str, "%s", current_sur_name);
407 if (g_utf8_strchr (email_id_str->str, -1, ' ')) {
408 g_string_prepend_c (email_id_str, '\"');
409 g_string_append_c (email_id_str, '\"');
411 g_string_append_printf (email_id_str, " %c%s%c", '<', current_email_id, '>');
412 return g_string_free (email_id_str, FALSE);
416 * This is a helper function used to create & run 'Add e-mail address to contact' dialog.
417 * It allows user to enter an e-mail address, and shows appropriate infonote if the
418 * entered string is not a valid e-mail address.
420 * @param contact_name Full name of the contact
421 * @return E-mail address string entered by user, to be freed by calling function.
424 run_add_email_addr_to_contact_dlg(const gchar * contact_name)
426 GtkWidget *add_email_addr_to_contact_dlg = NULL;
427 GtkSizeGroup *size_group = NULL;
428 GtkWidget *cptn_cntrl = NULL;
429 GtkWidget *name_label = NULL;
430 GtkWidget *email_entry = NULL;
432 gchar *new_email_addr = NULL;
433 gboolean run_dialog = TRUE;
435 add_email_addr_to_contact_dlg =
436 gtk_dialog_new_with_buttons(_("mcen_ti_add_email_title"), NULL,
437 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
438 _("mcen_bd_dialog_ok"), GTK_RESPONSE_ACCEPT,
439 _("mcen_bd_dialog_cancel"), GTK_RESPONSE_REJECT, NULL);
440 gtk_dialog_set_has_separator(GTK_DIALOG(add_email_addr_to_contact_dlg), FALSE);
441 /*Set app_name & state_save related tags to the window */
443 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
444 name_label = gtk_label_new(contact_name);
445 gtk_misc_set_alignment(GTK_MISC(name_label), 0, 0);
447 hildon_caption_new(size_group, _("mcen_ia_add_email_name"), name_label, NULL,
448 HILDON_CAPTION_OPTIONAL);
449 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
452 email_entry = gtk_entry_new();
454 hildon_caption_new(size_group, _("mcen_fi_add_email_name"), email_entry, NULL,
455 HILDON_CAPTION_OPTIONAL);
456 hildon_gtk_entry_set_input_mode(GTK_ENTRY(email_entry), HILDON_GTK_INPUT_MODE_FULL);
457 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
460 gtk_widget_show_all(add_email_addr_to_contact_dlg);
464 gtk_widget_grab_focus(email_entry);
465 result = gtk_dialog_run(GTK_DIALOG(add_email_addr_to_contact_dlg));
467 if (result == GTK_RESPONSE_ACCEPT) {
468 const gchar *invalid_char_offset = NULL;
469 new_email_addr = g_strdup(gtk_entry_get_text(GTK_ENTRY(email_entry)));
470 new_email_addr = g_strstrip(new_email_addr);
471 if (!modest_text_utils_validate_email_address (new_email_addr, &invalid_char_offset)) {
472 gtk_widget_grab_focus(email_entry);
473 if ((invalid_char_offset != NULL)&&(*invalid_char_offset != '\0')) {
474 gchar *char_in_string = g_strdup_printf ("%c", *invalid_char_offset);
475 gchar *message = g_strdup_printf(
476 dgettext("hildon-common-strings", "ckdg_ib_illegal_characters_entered"),
478 hildon_banner_show_information (
479 add_email_addr_to_contact_dlg, NULL, message );
482 hildon_banner_show_information (add_email_addr_to_contact_dlg, NULL, _("mcen_ib_invalid_email"));
485 gtk_editable_select_region((GtkEditable *) email_entry, 0, -1);
486 g_free(new_email_addr);
487 new_email_addr = NULL;
492 gtk_widget_destroy(add_email_addr_to_contact_dlg);
494 return new_email_addr;
498 * This is helper function to create & run 'Select e-mail address' dialog, used when
499 * multiple e-mail addresses are found for a selected contact. It allows user to select
500 * one or more e-mail addresses for that contact.
502 * @param email_addr_list List of e-mail addresses for that contact
503 * @return List of user selected e-mail addresses, to be freed by calling function.
506 select_email_addrs_for_contact(GList * email_addr_list)
508 GtkWidget *select_email_addr_dlg = NULL;
509 GtkWidget *view = NULL, *scrolledwindow = NULL;
510 GtkTreeSelection *selection = NULL;
511 GtkCellRenderer *renderer = NULL;
512 GtkTreeViewColumn *col = NULL;
513 GtkListStore *list_store = NULL;
514 GtkTreeModel *model = NULL;
516 GList *pathslist = NULL, *node = NULL;
518 gchar *email_addr = NULL;
519 GSList *selected_email_addr_list = NULL;
521 if (!email_addr_list)
524 select_email_addr_dlg =
525 gtk_dialog_new_with_buttons(_("mcen_ti_select_email_title"),
527 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
528 _("mcen_bd_dialog_ok"), GTK_RESPONSE_ACCEPT,
529 _("mcen_bd_dialog_cancel"), GTK_RESPONSE_REJECT, NULL);
530 gtk_dialog_set_has_separator(GTK_DIALOG(select_email_addr_dlg), FALSE);
532 /* Make the window approximately big enough, because it doesn't resize to be big enough
533 * for the window title text: */
534 gtk_window_set_default_size (GTK_WINDOW (select_email_addr_dlg), 400, -1);
536 scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
537 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(select_email_addr_dlg)->vbox), scrolledwindow, TRUE,
539 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_AUTOMATIC,
540 GTK_POLICY_AUTOMATIC);
542 view = gtk_tree_view_new();
543 col = gtk_tree_view_column_new();
544 renderer = gtk_cell_renderer_text_new();
545 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
546 gtk_tree_view_column_pack_start(col, renderer, TRUE);
547 gtk_tree_view_column_add_attribute(col, renderer, "text", 0);
548 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
549 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
550 gtk_container_add(GTK_CONTAINER(scrolledwindow), view);
552 list_store = gtk_list_store_new(1, G_TYPE_STRING);
553 model = GTK_TREE_MODEL(list_store);
554 gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
556 for (node = email_addr_list; node != NULL && node->data != NULL; node = node->next) {
557 email_addr = g_strstrip(g_strdup(node->data));
558 gtk_list_store_append(list_store, &iter);
559 gtk_list_store_set(list_store, &iter, 0, email_addr, -1);
562 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter);
563 gtk_tree_selection_select_iter (selection, &iter);
565 gtk_widget_show_all(select_email_addr_dlg);
566 result = gtk_dialog_run(GTK_DIALOG(select_email_addr_dlg));
568 if (result == GTK_RESPONSE_ACCEPT) {
569 pathslist = gtk_tree_selection_get_selected_rows(selection, NULL);
570 for (node = pathslist; node != NULL; node = node->next) {
571 if (gtk_tree_model_get_iter(model, &iter, (GtkTreePath *) node->data)) {
572 gtk_tree_model_get(model, &iter, 0, &email_addr, -1);
573 selected_email_addr_list =
574 g_slist_append(selected_email_addr_list, g_strdup(email_addr));
580 gtk_list_store_clear(list_store);
581 gtk_widget_destroy(select_email_addr_dlg);
582 return selected_email_addr_list;
586 static gboolean /* make this public? */
587 add_to_address_book (const gchar* address)
590 GList *contacts = NULL;
593 g_return_val_if_fail (address, FALSE);
598 g_return_val_if_fail (book, FALSE);
600 query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, address);
601 if (!e_book_get_contacts (book, query, &contacts, &err)) {
602 g_printerr ("modest: failed to get contacts: %s",
603 err ? err->message : "<unknown>");
608 e_book_query_unref (query);
610 /* we need to 'commit' it, even if we already found the email
611 * address in the addressbook; thus, it will show up in the 'recent list' */
613 g_debug ("%s already in the address book", address);
614 commit_contact ((EContact*)contacts->data, FALSE);
616 g_list_foreach (contacts, (GFunc)unref_gobject, NULL);
617 g_list_free (contacts);
620 /* it's not yet in the addressbook, add it now! */
621 EContact *new_contact = e_contact_new ();
622 e_contact_set (new_contact, E_CONTACT_EMAIL_1, (const gpointer)address);
623 commit_contact (new_contact, TRUE);
624 g_debug ("%s added to address book", address);
625 g_object_unref (new_contact);
632 show_check_names_banner (gpointer data)
634 GtkWidget **banner = (GtkWidget **) data;
636 *banner = hildon_banner_show_animation (NULL, NULL, _("mail_ib_checking_names"));
637 g_object_ref (G_OBJECT (*banner));
643 modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean update_addressbook)
645 const gchar *recipients = NULL;
646 GSList *start_indexes = NULL, *end_indexes = NULL;
647 GSList *current_start, *current_end;
648 gboolean result = TRUE;
649 GtkTextBuffer *buffer;
650 gint offset_delta = 0;
652 GtkTextIter start_iter, end_iter;
653 GtkWidget *banner = NULL;
654 guint show_banner_timeout;
656 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
658 show_banner_timeout = g_timeout_add (2000, show_check_names_banner, &banner);
659 recipients = modest_recpt_editor_get_recipients (recpt_editor);
660 last_length = g_utf8_strlen (recipients, -1);
661 modest_text_utils_get_addresses_indexes (recipients, &start_indexes, &end_indexes);
663 if (start_indexes == NULL) {
664 g_source_remove (show_banner_timeout);
665 if (banner != NULL) {
666 gtk_widget_destroy (banner);
667 g_object_unref (G_OBJECT(banner));
672 current_start = start_indexes;
673 current_end = end_indexes;
674 buffer = modest_recpt_editor_get_buffer (recpt_editor);
676 while (current_start != NULL) {
678 gchar *start_ptr, *end_ptr;
679 gint start_pos, end_pos;
680 const gchar *invalid_char_position = NULL;
682 start_pos = (*((gint*) current_start->data)) + offset_delta;
683 end_pos = (*((gint*) current_end->data)) + offset_delta;
685 start_ptr = g_utf8_offset_to_pointer (recipients, start_pos);
686 end_ptr = g_utf8_offset_to_pointer (recipients, end_pos);
688 address = g_strndup (start_ptr, end_ptr - start_ptr);
689 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_pos);
690 gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_pos);
691 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
693 if (!modest_text_utils_validate_recipient (address, &invalid_char_position)) {
694 if ((invalid_char_position != NULL) && (*invalid_char_position != '\0')) {
695 gchar *char_in_string = g_strdup_printf("%c", *invalid_char_position);
696 gchar *message = g_strdup_printf(
697 dgettext("hildon-common-strings", "ckdg_ib_illegal_characters_entered"),
699 g_free (char_in_string);
700 hildon_banner_show_information (NULL, NULL, message );
703 } else if (strstr (address, "@") == NULL) {
704 /* here goes searching in addressbook */
705 gchar *contact_id = NULL;
706 GSList *resolved_addresses = NULL;
707 result = resolve_address (address, &resolved_addresses, &contact_id);
712 modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
713 &start_iter, &end_iter,
717 g_slist_foreach (resolved_addresses, (GFunc) g_free, NULL);
718 g_slist_free (resolved_addresses);
720 /* update offset delta */
721 recipients = modest_recpt_editor_get_recipients (recpt_editor);
722 new_length = g_utf8_strlen (recipients, -1);
723 offset_delta = offset_delta + new_length - last_length;
724 last_length = new_length;
727 /* this address is not valid, select it and return control to user showing banner */
728 hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
733 /* so, it seems a valid address */
734 /* note: adding it the to the addressbook if it did not exist yet,
735 * and adding it to the recent_list */
736 if (result && update_addressbook)
737 add_to_address_book (address);
743 current_start = g_slist_next (current_start);
744 current_end = g_slist_next (current_end);
747 if (current_start == NULL) {
748 gtk_text_buffer_get_end_iter (buffer, &end_iter);
749 gtk_text_buffer_place_cursor (buffer, &end_iter);
752 g_source_remove (show_banner_timeout);
753 if (banner != NULL) {
754 gtk_widget_destroy (banner);
755 g_object_unref (G_OBJECT (banner));
758 g_slist_foreach (start_indexes, (GFunc) g_free, NULL);
759 g_slist_foreach (end_indexes, (GFunc) g_free, NULL);
760 g_slist_free (start_indexes);
761 g_slist_free (end_indexes);
768 get_contacts_for_name (const gchar *name)
770 EBookQuery *full_name_book_query = NULL;
777 unquoted = unquote_string (name);
778 full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted);
781 e_book_get_contacts (book, full_name_book_query, &result, NULL);
782 e_book_query_unref (full_name_book_query);
788 select_contacts_for_name_dialog (const gchar *name)
790 EBookQuery *full_name_book_query = NULL;
791 EBookView *book_view = NULL;
792 GList *result = NULL;
795 unquoted = unquote_string (name);
796 full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted);
798 e_book_get_book_view (book, full_name_book_query, NULL, -1, &book_view, NULL);
799 e_book_query_unref (full_name_book_query);
802 GtkWidget *contact_view = NULL;
803 GtkWidget *contact_dialog = NULL;
804 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model), book_view);
805 e_book_view_start (book_view);
807 contact_view = osso_abook_contact_view_new_basic (contact_model);
808 contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
810 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
811 result = osso_abook_contact_view_get_selection (OSSO_ABOOK_CONTACT_VIEW (contact_view));
813 e_book_view_stop (book_view);
814 g_object_unref (book_view);
815 gtk_widget_destroy (contact_dialog);
822 resolve_address (const gchar *address, GSList **resolved_addresses, gchar **contact_id)
824 GList *resolved_contacts;
826 contact_model = osso_abook_contact_model_new ();
827 if (!open_addressbook_sync ()) {
829 g_object_unref (contact_model);
830 contact_model = NULL;
835 resolved_contacts = get_contacts_for_name (address);
837 if (resolved_contacts == NULL) {
838 /* no matching contacts for the search string */
839 modest_platform_run_information_dialog (NULL, _("mcen_nc_no_matching_contacts"));
843 if (g_list_length (resolved_contacts) > 1) {
844 /* show a dialog to select the contact from the resolved ones */
845 g_list_free (resolved_contacts);
847 resolved_contacts = select_contacts_for_name_dialog (address);
850 /* get the resolved contacts (can be no contact) */
851 if (resolved_contacts) {
852 EContact *contact = (EContact *) resolved_contacts->data;
854 *resolved_addresses = get_recipients_for_given_contact (contact);
855 if (*resolved_addresses) {
856 *contact_id = g_strdup (e_contact_get_const (contact, E_CONTACT_UID));
859 g_list_foreach (resolved_contacts, (GFunc)unref_gobject, NULL);
860 g_list_free (resolved_contacts);
864 /* cancelled dialog to select more than one contact or
865 * selected no contact */
872 unquote_string (const gchar *str)
880 buffer = g_string_new_len (NULL, strlen (str));
881 for (p = (gchar *) str; *p != '\0'; p = g_utf8_next_char (p)) {
883 p = g_utf8_next_char (p);
884 while ((*p != '\0')&&(*p != '"')) {
886 g_string_append_unichar (buffer, g_utf8_get_char (p));
887 p = g_utf8_next_char (p);
890 g_string_append_unichar (buffer, g_utf8_get_char (p));
891 p = g_utf8_next_char (p);
894 g_string_append_unichar (buffer, g_utf8_get_char (p));
898 return g_string_free (buffer, FALSE);