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"
41 #include "modest-runtime.h"
42 #include "widgets/modest-window-mgr.h"
44 #include <gtk/gtksizegroup.h>
45 #include <gtk/gtkbox.h>
46 #include <gtk/gtklabel.h>
47 #include <gtk/gtkcellrenderertext.h>
48 #include <gtk/gtktreeselection.h>
49 #include <gtk/gtkentry.h>
51 static OssoABookContactModel *contact_model = NULL;
52 static EBook *book = NULL;
53 static EBookView * book_view = NULL;
55 static GSList *get_recipients_for_given_contact(EContact * contact);
56 static void commit_contact(EContact * contact, gboolean is_new);
57 static gchar *get_email_addr_from_user(const gchar * given_name);
58 static gchar *ui_get_formatted_email_id(gchar * current_given_name,
59 gchar * current_sur_name, gchar * current_email_id);
60 static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name);
61 static GSList *select_email_addrs_for_contact(GList * email_addr_list);
62 static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, gchar **contact_id);
63 static gchar *unquote_string (const gchar *str);
67 unref_gobject (GObject *obj)
75 get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
77 if (status != E_BOOK_ERROR_OK) {
78 g_object_unref (book);
85 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model),
88 e_book_view_start (book_view);
92 book_open_cb (EBook *view, EBookStatus status, gpointer data)
94 EBookQuery *query = NULL;
96 if (status != E_BOOK_ERROR_OK) {
97 g_object_unref (book);
101 query = e_book_query_any_field_contains ("");
102 e_book_async_get_book_view (book, query, NULL, -1, get_book_view_cb, NULL);
103 e_book_query_unref (query);
109 book = e_book_new_system_addressbook (NULL);
113 e_book_async_open (book, FALSE, book_open_cb, NULL);
115 return TRUE; /* FIXME */
119 open_addressbook_sync ()
121 book = e_book_new_system_addressbook (NULL);
125 e_book_open (book, FALSE, NULL);
131 modest_address_book_add_address (const gchar *address)
133 OssoABookAccount *account = NULL;
134 GtkWidget *dialog = NULL;
135 gchar *email_address = NULL;
137 contact_model = osso_abook_contact_model_new ();
138 if (!open_addressbook ()) {
140 g_object_unref (contact_model);
141 contact_model = NULL;
146 email_address = modest_text_utils_get_email_address (address);
148 account = osso_abook_account_get (EVC_EMAIL, NULL, email_address);
149 g_free (email_address);
152 dialog = osso_abook_add_to_contacts_dialog_new (contact_model, account);
153 g_object_unref (account);
154 gtk_dialog_run (GTK_DIALOG (dialog));
157 g_object_unref (contact_model);
158 contact_model = NULL;
161 gtk_widget_destroy (dialog);
167 modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
169 GtkWidget *contact_view = NULL;
170 GList *contacts_list = NULL;
171 GtkWidget *contact_dialog;
172 GSList *email_addrs_per_contact = NULL;
174 gboolean focus_recpt_editor = FALSE;
176 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
178 if (!open_addressbook ()) {
180 g_object_unref (contact_model);
181 contact_model = NULL;
185 contact_model = osso_abook_contact_model_new ();
187 contact_view = osso_abook_contact_selector_new_basic (contact_model);
188 osso_abook_contact_selector_set_minimum_selection (OSSO_ABOOK_CONTACT_SELECTOR (contact_view), 1);
190 contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
191 gtk_window_set_title (GTK_WINDOW (contact_dialog), _("mcen_ti_select_recipients"));
192 modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (contact_dialog));
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;
594 g_return_val_if_fail (address, FALSE);
599 g_return_val_if_fail (book, FALSE);
601 email = modest_text_utils_get_email_address (address);
603 query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, email);
604 if (!e_book_get_contacts (book, query, &contacts, &err)) {
605 g_printerr ("modest: failed to get contacts: %s",
606 err ? err->message : "<unknown>");
611 e_book_query_unref (query);
613 /* we need to 'commit' it, even if we already found the email
614 * address in the addressbook; thus, it will show up in the 'recent list' */
616 g_debug ("%s already in the address book", address);
617 commit_contact ((EContact*)contacts->data, FALSE);
619 g_list_foreach (contacts, (GFunc)unref_gobject, NULL);
620 g_list_free (contacts);
623 /* it's not yet in the addressbook, add it now! */
624 EContact *new_contact = e_contact_new ();
625 gchar *display_address;
626 display_address = g_strdup (address);
627 if (display_address) {
628 modest_text_utils_get_display_address (display_address);
629 if ((display_address[0] != '\0') && (strlen (display_address) != strlen (address)))
630 e_contact_set (new_contact, E_CONTACT_FULL_NAME, (const gpointer)display_address);
632 e_contact_set (new_contact, E_CONTACT_EMAIL_1, (const gpointer)email);
633 g_free (display_address);
634 commit_contact (new_contact, TRUE);
635 g_debug ("%s added to address book", address);
636 g_object_unref (new_contact);
645 show_check_names_banner (gpointer userdata)
647 GtkWidget **banner = (GtkWidget **) userdata;
649 gdk_threads_enter ();
650 *banner = modest_platform_animation_banner (NULL, NULL, _("mail_ib_checking_names"));
651 gdk_threads_leave ();
657 modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean update_addressbook)
659 const gchar *recipients = NULL;
660 GSList *start_indexes = NULL, *end_indexes = NULL;
661 GSList *current_start, *current_end;
662 gboolean result = TRUE;
663 GtkTextBuffer *buffer;
664 gint offset_delta = 0;
666 GtkTextIter start_iter, end_iter;
667 guint banner_timeout;
668 GtkWidget *banner = NULL;
670 g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
672 recipients = modest_recpt_editor_get_recipients (recpt_editor);
673 last_length = g_utf8_strlen (recipients, -1);
674 modest_text_utils_get_addresses_indexes (recipients, &start_indexes, &end_indexes);
676 if (start_indexes == NULL) {
677 if (last_length != 0) {
678 hildon_banner_show_information (NULL, NULL, _("mcen_nc_no_matching_contacts"));
685 banner_timeout = g_timeout_add (500, show_check_names_banner, &banner);
687 current_start = start_indexes;
688 current_end = end_indexes;
689 buffer = modest_recpt_editor_get_buffer (recpt_editor);
691 while (current_start != NULL) {
693 gchar *start_ptr, *end_ptr;
694 gint start_pos, end_pos;
695 const gchar *invalid_char_position = NULL;
696 gboolean store_address = FALSE;
698 start_pos = (*((gint*) current_start->data)) + offset_delta;
699 end_pos = (*((gint*) current_end->data)) + offset_delta;
701 start_ptr = g_utf8_offset_to_pointer (recipients, start_pos);
702 end_ptr = g_utf8_offset_to_pointer (recipients, end_pos);
704 address = g_strstrip (g_strndup (start_ptr, end_ptr - start_ptr));
705 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_pos);
706 gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_pos);
707 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
709 if (!modest_text_utils_validate_recipient (address, &invalid_char_position)) {
710 if ((invalid_char_position != NULL) && (*invalid_char_position != '\0')) {
711 gchar *char_in_string = g_strdup_printf("%c", *invalid_char_position);
712 gchar *message = g_strdup_printf(
713 dgettext("hildon-common-strings", "ckdg_ib_illegal_characters_entered"),
715 g_free (char_in_string);
716 hildon_banner_show_information (NULL, NULL, message );
719 } else if (strstr (address, "@") == NULL) {
720 /* here goes searching in addressbook */
721 gchar *contact_id = NULL;
722 GSList *resolved_addresses = NULL;
723 result = resolve_address (address, &resolved_addresses, &contact_id);
728 modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
729 &start_iter, &end_iter,
733 g_slist_foreach (resolved_addresses, (GFunc) g_free, NULL);
734 g_slist_free (resolved_addresses);
736 /* update offset delta */
737 recipients = modest_recpt_editor_get_recipients (recpt_editor);
738 new_length = g_utf8_strlen (recipients, -1);
739 offset_delta = offset_delta + new_length - last_length;
740 last_length = new_length;
743 /* this address is not valid, select it and return control to user showing banner */
744 hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
749 gboolean has_recipient = FALSE;
751 tags = gtk_text_iter_get_tags (&start_iter);
752 for (node = tags; node != NULL; node = g_slist_next (node)) {
753 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
754 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id") != NULL) {
755 has_recipient = TRUE;
760 if (!has_recipient) {
761 GSList * address_list = NULL;
763 address_list = g_slist_prepend (address_list, address);
764 modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
765 &start_iter, &end_iter,
768 g_slist_free (address_list);
769 store_address = TRUE;
773 /* so, it seems a valid address */
774 /* note: adding it the to the addressbook if it did not exist yet,
775 * and adding it to the recent_list */
776 if (result && update_addressbook && store_address)
777 add_to_address_book (address);
783 current_start = g_slist_next (current_start);
784 current_end = g_slist_next (current_end);
787 if (current_start == NULL) {
788 gtk_text_buffer_get_end_iter (buffer, &end_iter);
789 gtk_text_buffer_place_cursor (buffer, &end_iter);
792 g_slist_foreach (start_indexes, (GFunc) g_free, NULL);
793 g_slist_foreach (end_indexes, (GFunc) g_free, NULL);
794 g_slist_free (start_indexes);
795 g_slist_free (end_indexes);
797 g_source_remove (banner_timeout);
798 if (banner != NULL) {
799 gtk_widget_destroy (banner);
802 g_source_remove (banner_timeout);
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_view = NULL;
845 GtkWidget *contact_dialog = NULL;
846 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model), book_view);
847 e_book_view_start (book_view);
849 contact_view = osso_abook_contact_view_new_basic (contact_model);
850 contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
852 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
853 result = osso_abook_contact_view_get_selection (OSSO_ABOOK_CONTACT_VIEW (contact_view));
855 e_book_view_stop (book_view);
856 g_object_unref (book_view);
857 gtk_widget_destroy (contact_dialog);
864 resolve_address (const gchar *address, GSList **resolved_addresses, gchar **contact_id)
866 GList *resolved_contacts;
868 contact_model = osso_abook_contact_model_new ();
869 if (!open_addressbook_sync ()) {
871 g_object_unref (contact_model);
872 contact_model = NULL;
877 resolved_contacts = get_contacts_for_name (address);
879 if (resolved_contacts == NULL) {
880 /* no matching contacts for the search string */
881 modest_platform_run_information_dialog (NULL, _("mcen_nc_no_matching_contacts"), FALSE);
885 if (g_list_length (resolved_contacts) > 1) {
886 /* show a dialog to select the contact from the resolved ones */
887 g_list_free (resolved_contacts);
889 resolved_contacts = select_contacts_for_name_dialog (address);
892 /* get the resolved contacts (can be no contact) */
893 if (resolved_contacts) {
895 EContact *contact = (EContact *) resolved_contacts->data;
897 *resolved_addresses = get_recipients_for_given_contact (contact);
898 if (*resolved_addresses) {
899 *contact_id = g_strdup (e_contact_get_const (contact, E_CONTACT_UID));
905 g_list_foreach (resolved_contacts, (GFunc)unref_gobject, NULL);
906 g_list_free (resolved_contacts);
910 /* cancelled dialog to select more than one contact or
911 * selected no contact */
918 unquote_string (const gchar *str)
926 buffer = g_string_new_len (NULL, strlen (str));
927 for (p = (gchar *) str; *p != '\0'; p = g_utf8_next_char (p)) {
929 p = g_utf8_next_char (p);
930 while ((*p != '\0')&&(*p != '"')) {
932 g_string_append_unichar (buffer, g_utf8_get_char (p));
933 p = g_utf8_next_char (p);
936 g_string_append_unichar (buffer, g_utf8_get_char (p));
937 p = g_utf8_next_char (p);
940 g_string_append_unichar (buffer, g_utf8_get_char (p));
944 return g_string_free (buffer, FALSE);