Select contacts dialog for checking names is multiselection
[modest] / src / hildon2 / modest-address-book.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 /* modest-address-book.c */
31
32 #include <config.h>
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"
45 #include <string.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>
52 #include <modest-maemo-utils.h>
53
54 static OssoABookContactModel *contact_model =  NULL;
55 static EBook *book = NULL;
56 static EBookView * book_view = NULL;
57
58 static GSList *get_recipients_for_given_contact (EContact * contact, gboolean *canceled);
59 static void commit_contact(EContact * contact, gboolean is_new);
60 static gchar *get_email_addr_from_user(const gchar * given_name, gboolean *canceled);
61 static gchar *ui_get_formatted_email_id(gchar * current_given_name,
62                                         gchar * current_sur_name, gchar * current_email_id);
63 static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name, gboolean *canceled);
64 static GSList *select_email_addrs_for_contact(GList * email_addr_list);
65 static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, GSList **contact_id, gboolean *canceled);
66 static gchar *unquote_string (const gchar *str);
67
68
69 static void
70 unref_gobject (GObject *obj)
71 {
72         if (obj)
73                 g_object_unref (obj);
74 }
75         
76
77 static void
78 get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
79 {
80         if (status != E_BOOK_ERROR_OK) {
81                 g_object_unref (book);
82                 book = NULL;
83                 return;
84         }
85         book_view = bookview;
86
87         if (contact_model)
88 #if MODEST_ABOOK_API < 4
89                 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model),
90                                                      book_view);
91 #else /* MODEST_ABOOK_API < 4 */
92                 osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model),
93                                                      book_view);
94 #endif /* MODEST_ABOOK_API < 4 */
95
96         e_book_view_start (book_view);
97 }
98
99 static void
100 book_open_cb (EBook *view, EBookStatus status, gpointer data)
101 {
102         EBookQuery *query = NULL;
103
104         if (status != E_BOOK_ERROR_OK) {
105                 g_object_unref (book);
106                 book = NULL;
107                 return;
108         }
109         query = e_book_query_any_field_contains ("");
110         e_book_async_get_book_view (book, query, NULL, -1, get_book_view_cb, NULL);
111         e_book_query_unref (query);
112 }
113
114 static gboolean 
115 open_addressbook ()
116 {
117         book = e_book_new_system_addressbook (NULL);
118         if (!book)
119                 return FALSE;
120
121         if (e_book_async_open (book, FALSE, book_open_cb, NULL) != E_BOOK_ERROR_OK)
122                 return FALSE;
123
124         return TRUE; /* FIXME */        
125 }
126
127 typedef struct _OpenAddressbookSyncInfo {
128         gboolean retval;
129         GMainLoop *mainloop;
130 } OpenAddressbookSyncInfo;
131
132 static void
133 get_book_view_sync_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
134 {
135         OpenAddressbookSyncInfo *info = (OpenAddressbookSyncInfo *) data;
136
137         if (status != E_BOOK_ERROR_OK) {
138                 g_object_unref (book);
139                 book = NULL;
140                 info->retval = FALSE;
141                 g_main_loop_quit (info->mainloop);
142                 return;
143         }
144         book_view = bookview;
145
146         if (contact_model)
147 #if MODEST_ABOOK_API < 4
148                 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model),
149                                                      book_view);
150 #else /* MODEST_ABOOK_API < 4 */
151                 osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model),
152                                                      book_view);
153 #endif /* MODEST_ABOOK_API < 4 */
154
155         e_book_view_start (book_view);
156         info->retval = TRUE;
157         g_main_loop_quit (info->mainloop);
158 }
159
160 static void
161 book_open_sync_cb (EBook *view, EBookStatus status, gpointer data)
162 {
163         EBookQuery *query = NULL;
164         OpenAddressbookSyncInfo *info = (OpenAddressbookSyncInfo *) data;
165
166         if (status != E_BOOK_ERROR_OK) {
167                 g_object_unref (book);
168                 book = NULL;
169                 info->retval = FALSE;
170                 g_main_loop_quit (info->mainloop);
171                 return;
172         }
173         query = e_book_query_any_field_contains ("");
174         e_book_async_get_book_view (book, query, NULL, -1, get_book_view_sync_cb, info);
175         e_book_query_unref (query);
176 }
177
178 static gboolean
179 open_addressbook_sync ()
180 {
181         OpenAddressbookSyncInfo *info;
182         gboolean retval;
183
184         book = e_book_new_system_addressbook (NULL);
185         if (!book)
186                 return FALSE;
187
188         info = g_slice_new (OpenAddressbookSyncInfo);
189         info->mainloop = g_main_loop_new (NULL, FALSE);
190         info->retval = FALSE;
191         if (e_book_async_open (book, FALSE, book_open_sync_cb, info) == E_BOOK_ERROR_OK) {
192                 GDK_THREADS_LEAVE ();
193                 g_main_loop_run (info->mainloop);
194                 GDK_THREADS_ENTER ();
195         }
196         retval = info->retval;
197         g_main_loop_unref (info->mainloop);
198         g_slice_free (OpenAddressbookSyncInfo, info);
199         /* Make it launch a main loop */
200         return e_book_open (book, FALSE, NULL);
201 }
202
203 void
204 modest_address_book_add_address (const gchar *address)
205 {
206         GtkWidget *dialog = NULL;
207         gchar *email_address;
208         EVCardAttribute *attribute;
209
210         if (!open_addressbook ()) {
211                 return;
212         }
213
214         email_address = modest_text_utils_get_email_address (address);
215         
216         attribute = e_vcard_attribute_new (NULL, EVC_EMAIL);
217         e_vcard_attribute_add_value (attribute, email_address);
218         dialog = osso_abook_temporary_contact_dialog_new (NULL, book, attribute, NULL);
219         gtk_dialog_run (GTK_DIALOG (dialog));
220
221         gtk_widget_destroy (dialog);
222
223         e_vcard_attribute_free (attribute);
224         g_free (email_address);
225
226 }
227
228 void
229 modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
230 {
231 #if MODEST_ABOOK_API < 4
232         GtkWidget *contact_view = NULL;
233         GtkWidget *contact_dialog;
234         GtkWidget *toplevel;
235 #else /* MODEST_ABOOK_API < 4 */
236         GtkWidget *contact_chooser = NULL;
237 #endif /* MODEST_ABOOK_API < 4 */
238
239         GList *contacts_list = NULL;
240         GSList *email_addrs_per_contact = NULL;
241         gchar *econtact_id;
242         gboolean focus_recpt_editor = FALSE;
243
244         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
245
246 #if MODEST_ABOOK_API < 4
247         if (!open_addressbook ()) {
248                 if (contact_model) {
249                         g_object_unref (contact_model);
250                         contact_model = NULL;
251                 }
252                 return;
253         }
254         contact_model = osso_abook_contact_model_new ();
255
256         contact_view = osso_abook_contact_selector_new_basic (contact_model);
257         osso_abook_contact_selector_set_minimum_selection (OSSO_ABOOK_CONTACT_SELECTOR (contact_view), 1);
258
259         contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
260         toplevel = gtk_widget_get_toplevel (GTK_WIDGET (recpt_editor));
261         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (contact_dialog), GTK_WINDOW (toplevel));
262
263         gtk_widget_show (contact_dialog);
264
265         if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
266                 contacts_list = 
267                         osso_abook_contact_selector_get_extended_selection (OSSO_ABOOK_CONTACT_SELECTOR
268                                                                              (contact_view));
269         }
270 #else /* MODEST_ABOOK_API < 4 */
271         /* TODO: figure out how to make the contact chooser modal */
272         contact_chooser = osso_abook_contact_chooser_new_with_capabilities (NULL,
273                                                                             _AB("addr_ti_dia_select_contacts"),
274                                                                             OSSO_ABOOK_CAPS_EMAIL, 
275                                                                             OSSO_ABOOK_CONTACT_ORDER_NAME);
276         /* Enable multiselection */
277         osso_abook_contact_chooser_set_maximum_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_chooser),
278                                                           G_MAXUINT);
279
280         if (gtk_dialog_run (GTK_DIALOG (contact_chooser)) == GTK_RESPONSE_OK)
281                 contacts_list = osso_abook_contact_chooser_get_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_chooser));
282         gtk_widget_destroy (contact_chooser);
283 #endif
284
285         if (contacts_list) {
286                 GList *node;
287
288                 for (node = contacts_list; node != NULL; node = g_list_next (node)) {
289                         EContact *contact = (EContact *) node->data;
290                         gboolean canceled;
291
292                         email_addrs_per_contact = get_recipients_for_given_contact (contact, &canceled);
293                         if (email_addrs_per_contact) {
294                                 econtact_id = (gchar *) e_contact_get_const (contact, E_CONTACT_UID);
295                                 modest_recpt_editor_add_resolved_recipient (MODEST_RECPT_EDITOR (recpt_editor), 
296                                                                             email_addrs_per_contact, econtact_id);
297                                 g_slist_foreach (email_addrs_per_contact, (GFunc) g_free, NULL);
298                                 g_slist_free (email_addrs_per_contact);
299                                 email_addrs_per_contact = NULL;
300                                 focus_recpt_editor = TRUE;
301                         }
302                 }
303                 g_list_free (contacts_list);
304         }
305
306 #if MODEST_ABOOK_API < 4
307         if (contact_model) {
308                 g_object_unref (contact_model);
309                 contact_model = NULL;
310         }
311
312         gtk_widget_destroy (contact_dialog);
313 #endif
314
315         if (focus_recpt_editor)
316                 modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (recpt_editor));
317
318 }
319
320 /**
321  * This function returns the resolved recipients for a given EContact.
322  * If no e-mail address is defined, it launches 'Add e-mail address to contact'
323  * dialog to obtain one. If multiple e-mail addresses are found, it launches
324  * 'Select e-mail address' dialog to allow user to select one or more e-mail
325  * addresses for that contact.
326  *
327  * @param  Contact of type #EContact
328  * @return List of resolved recipient strings, to be freed by calling function.
329  */
330 static GSList *
331 get_recipients_for_given_contact (EContact * contact,
332                                   gboolean *canceled)
333 {
334         gchar *givenname = NULL;
335         gchar *familyname = NULL;
336         gchar *nickname = NULL;
337         gchar *emailid = NULL;
338         const gchar *display_name = NULL;
339         GList *list = NULL;
340         gchar *formatted_string = NULL;
341         gboolean email_not_present = FALSE;
342         GSList *formattedlist = NULL, *selected_email_addr_list = NULL, *node = NULL;
343
344         if (!contact) {
345                 return NULL;
346         }
347
348         givenname = (gchar *) e_contact_get_const(contact, E_CONTACT_GIVEN_NAME);
349         familyname = (gchar *) e_contact_get_const(contact, E_CONTACT_FAMILY_NAME);
350         nickname = (gchar *) e_contact_get_const(contact, E_CONTACT_NICKNAME);
351         if (!nickname)
352                 nickname = "";
353         list = (GList *) e_contact_get(contact, E_CONTACT_EMAIL);
354
355         if (!list) {
356                 email_not_present = TRUE;
357         }
358
359         if (list && g_list_length(list) == 1) {
360                 if (list->data == NULL || g_utf8_strlen(list->data, -1) == 0) {
361                         email_not_present = TRUE;
362                 } else {
363                         emailid = g_strstrip(g_strdup(list->data));
364                         if (g_utf8_strlen(emailid, -1) == 0) {
365                                 g_free(emailid);
366                                 email_not_present = TRUE;
367                         }
368                 }
369         }
370
371         /*Launch the 'Add e-mail addr to contact' dialog if required */
372         if (email_not_present) {
373 #if MODEST_ABOOK_API < 4
374                 display_name = osso_abook_contact_get_display_name(contact);
375 #else
376                 OssoABookContact *abook_contact;
377
378                 abook_contact = osso_abook_contact_new_from_template (contact);
379                 display_name = osso_abook_contact_get_display_name(abook_contact);
380 #endif
381
382                 emailid = get_email_addr_from_user(display_name, canceled);
383                 if (emailid) {
384                         e_contact_set(contact, E_CONTACT_EMAIL_1, emailid);
385                         commit_contact(contact, FALSE);
386                 }
387 #if MODEST_ABOOK_API >= 4
388                 g_object_unref (abook_contact);
389 #endif
390         }
391
392         if (emailid) {
393                 if (givenname || familyname)
394                         formatted_string =
395                             ui_get_formatted_email_id(givenname, familyname, emailid);
396                 else
397                         formatted_string = g_strdup(emailid);
398                 formattedlist = g_slist_append(formattedlist, formatted_string);
399                 g_free(emailid);
400         }
401
402         /*Launch the 'Select e-mail address' dialog if required */
403         if (g_list_length(list) > 1) {
404                 selected_email_addr_list = select_email_addrs_for_contact(list);
405                 for (node = selected_email_addr_list; node != NULL; node = node->next) {
406                         if (givenname || familyname)
407                                 formatted_string =
408                                     ui_get_formatted_email_id(givenname, familyname, node->data);
409                         else
410                                 formatted_string = g_strdup(node->data);
411                         formattedlist = g_slist_append(formattedlist, formatted_string);
412                 }
413                 if (selected_email_addr_list) {
414                         g_slist_foreach(selected_email_addr_list, (GFunc) g_free, NULL);
415                         g_slist_free(selected_email_addr_list);
416                 }
417         }
418
419         if (list) {
420                 g_list_foreach(list, (GFunc) g_free, NULL);
421                 g_list_free(list);
422         }
423
424         return formattedlist;
425 }
426
427 /**
428  * This is a helper function to commit a EContact to Address_Book application.
429  *
430  * @param  contact  Contact of type #EContact
431  * @return void
432  */
433 static void 
434 commit_contact(EContact * contact, gboolean is_new)
435 {
436         g_return_if_fail (contact);
437         g_return_if_fail (book);
438
439         if (!contact || !book)
440                 return;
441
442 #if MODEST_ABOOK_API < 4
443         osso_abook_contact_commit(contact, is_new, book);
444 #else
445         if (OSSO_ABOOK_IS_CONTACT (contact)) {
446                 osso_abook_contact_commit(OSSO_ABOOK_CONTACT(contact), is_new, book, NULL);
447         } else {
448                 if (is_new)
449                         e_book_add_contact (book, contact, NULL);
450                 else
451                         e_book_commit_contact (book, contact, NULL);
452         }
453 #endif /* MODEST_ABOOK_API < 2 */
454 }
455
456 /**
457  * This is a helper function used to launch 'Add e-mail address to contact' dialog
458  * after showing appropriate notification, when there is no e-mail address defined
459  * for a selected contact.
460  *
461  * @param  given_name  Given name of the contact
462  * @param  family_name  Family name of the contact
463  * @return E-mail address string entered by user, to be freed by calling function.
464  */
465 static gchar *
466 get_email_addr_from_user(const gchar * given_name, gboolean *canceled)
467 {
468         gchar *notification = NULL;
469         gchar *email_addr = NULL;
470         GtkWidget *note;
471         gboolean note_response;
472
473
474         notification = g_strdup_printf(_("mcen_nc_email_address_not_defined"), given_name);
475
476         note = hildon_note_new_confirmation (NULL, notification);
477         note_response = gtk_dialog_run (GTK_DIALOG(note));
478         gtk_widget_destroy (note);
479         g_free(notification);
480
481         if (note_response == GTK_RESPONSE_OK) {
482                 email_addr = run_add_email_addr_to_contact_dlg (given_name, canceled);
483         }
484
485         return email_addr;
486 }
487
488 /**
489 This function is used to get the formated email id with given name and sur name
490 in the format "GIVENNAME SURNAME <EMAIL ADDRESS>".
491 @param current_given_name    to hold the given name
492 @param current_sur_name      to hold the sur name
493 @param current_email_id      to hold the email id. 
494 @return gchar* string to be freed by calling function
495 */
496 static gchar *
497 ui_get_formatted_email_id(gchar * current_given_name,
498                           gchar * current_sur_name, gchar * current_email_id)
499 {
500         GString *email_id_str = NULL;
501
502         email_id_str = g_string_new(NULL);
503
504         if ((current_given_name != NULL) && ((strlen(current_given_name) != 0))
505             && (current_sur_name != NULL) && ((strlen(current_sur_name) != 0))) {
506                 g_string_append_printf(email_id_str, "%s %s", current_given_name, current_sur_name);
507         } else if ((current_given_name != NULL) && (strlen(current_given_name) != 0)) {
508                 g_string_append_printf(email_id_str, "%s", current_given_name);
509         } else if ((current_sur_name != NULL) && (strlen(current_sur_name) != 0)) {
510                 g_string_append_printf(email_id_str, "%s", current_sur_name);
511         }
512         if (g_utf8_strchr (email_id_str->str, -1, ' ')) {
513                 g_string_prepend_c (email_id_str, '\"');
514                 g_string_append_c (email_id_str, '\"');
515         }
516         g_string_append_printf (email_id_str, " %c%s%c", '<', current_email_id, '>');
517         return g_string_free (email_id_str, FALSE);
518 }
519
520 /**
521  * This is a helper function used to create & run 'Add e-mail address to contact' dialog.
522  * It allows user to enter an e-mail address, and shows appropriate infonote if the
523  * entered string is not a valid e-mail address.
524  *
525  * It must return TRUE in canceled if the dialog was canceled by the user
526  *
527  * @param  contact_name  Full name of the contact
528  * @return E-mail address string entered by user, to be freed by calling function.
529  */
530 static gchar *
531 run_add_email_addr_to_contact_dlg(const gchar * contact_name,
532                                   gboolean *canceled)
533 {
534         GtkWidget *add_email_addr_to_contact_dlg = NULL;
535         GtkSizeGroup *size_group = NULL;
536         GtkWidget *cptn_cntrl = NULL;
537         GtkWidget *name_label = NULL;
538         GtkWidget *email_entry = NULL;
539         gint result = -1;
540         gchar *new_email_addr = NULL;
541         gboolean run_dialog = TRUE;
542
543         g_return_val_if_fail (canceled, NULL);
544
545         *canceled = FALSE;
546
547         add_email_addr_to_contact_dlg =
548             gtk_dialog_new_with_buttons(_("mcen_ti_add_email_title"), NULL,
549                                         GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
550                                         _HL("wdgt_bd_save"), GTK_RESPONSE_ACCEPT, NULL);
551         gtk_dialog_set_has_separator(GTK_DIALOG(add_email_addr_to_contact_dlg), FALSE);
552 #ifdef MODEST_TOOLKIT_HILDON2
553         gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (add_email_addr_to_contact_dlg)->vbox), 
554                                         HILDON_MARGIN_DOUBLE);
555 #endif
556         /*Set app_name & state_save related tags to the window */
557
558         size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
559         name_label = gtk_label_new(contact_name);
560         gtk_misc_set_alignment(GTK_MISC(name_label), 0.0, 0.5);
561         cptn_cntrl =
562                 modest_maemo_utils_create_captioned (size_group, NULL,
563                                                      _("mcen_ia_add_email_name"), FALSE,
564                                                      name_label);
565         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
566                            FALSE, FALSE, 0);
567
568         email_entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
569         cptn_cntrl = modest_maemo_utils_create_captioned (size_group, NULL, 
570                                                           _("mcen_fi_add_email_name"), FALSE,
571                                                           email_entry);
572         hildon_gtk_entry_set_input_mode(GTK_ENTRY(email_entry), HILDON_GTK_INPUT_MODE_FULL);
573         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
574                            TRUE, TRUE, 0);
575
576         gtk_widget_show_all(add_email_addr_to_contact_dlg);
577
578         while (run_dialog) {
579                 run_dialog = FALSE;
580                 gtk_widget_grab_focus(email_entry);
581                 result = gtk_dialog_run(GTK_DIALOG(add_email_addr_to_contact_dlg));
582
583                 if (result == GTK_RESPONSE_ACCEPT) {
584                         const gchar *invalid_char_offset = NULL;
585                         new_email_addr = g_strdup(hildon_entry_get_text(HILDON_ENTRY(email_entry)));
586                         new_email_addr = g_strstrip(new_email_addr);
587                         if (!modest_text_utils_validate_email_address (new_email_addr, &invalid_char_offset)) {
588                                 gtk_widget_grab_focus(email_entry);
589                                 if ((invalid_char_offset != NULL)&&(*invalid_char_offset != '\0')) {
590                                         gchar *char_in_string = g_strdup_printf ("%c", *invalid_char_offset);
591                                         gchar *message = g_strdup_printf(
592                                                 _CS("ckdg_ib_illegal_characters_entered"), 
593                                                 char_in_string);
594                                         hildon_banner_show_information (
595                                                 add_email_addr_to_contact_dlg, NULL, message );
596                                         g_free (message);
597                                 } else {
598                                         hildon_banner_show_information (add_email_addr_to_contact_dlg, NULL, _("mcen_ib_invalid_email"));
599                                         run_dialog = TRUE;
600                                 }
601                                 gtk_editable_select_region((GtkEditable *) email_entry, 0, -1);
602                                 g_free(new_email_addr);
603                                 new_email_addr = NULL;
604                         }
605                 } else {
606                         *canceled = TRUE;
607                 }
608         }
609
610         gtk_widget_destroy(add_email_addr_to_contact_dlg);
611
612         return new_email_addr;
613 }
614
615 /**
616  * This is helper function to create & run 'Select e-mail address' dialog, used when
617  * multiple e-mail addresses are found for a selected contact. It allows user to select
618  * one or more e-mail addresses for that contact.
619  *
620  * @param  email_addr_list  List of e-mail addresses for that contact
621  * @return List of user selected e-mail addresses, to be freed by calling function.
622  */
623 static GSList *
624 select_email_addrs_for_contact(GList * email_addr_list)
625 {
626         GtkWidget *select_email_addr_dlg = NULL;
627         GSList *selected_email_addr_list = NULL;
628         GList *node;
629         GtkWidget *selector;
630         gint result = -1;
631
632         if (!email_addr_list)
633                 return NULL;
634
635         select_email_addr_dlg = hildon_picker_dialog_new (NULL);
636         gtk_window_set_title (GTK_WINDOW (select_email_addr_dlg), _("mcen_ti_select_email_title"));
637
638         selector = hildon_touch_selector_new_text ();
639         for (node = email_addr_list; node != NULL && node->data != NULL; node = node->next) {
640                 gchar *email_addr;
641                 email_addr = g_strstrip(g_strdup(node->data));
642                 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), email_addr);
643                 g_free(email_addr);
644         }
645
646         hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (select_email_addr_dlg),
647                                            HILDON_TOUCH_SELECTOR (selector));
648         gtk_window_set_default_size (GTK_WINDOW (select_email_addr_dlg), MODEST_DIALOG_WINDOW_MAX_HEIGHT, -1);
649         
650         gtk_widget_show_all(select_email_addr_dlg);
651         result = gtk_dialog_run(GTK_DIALOG(select_email_addr_dlg));
652
653         if (result == GTK_RESPONSE_OK) {
654                 gchar *current_text;
655
656                 current_text = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector));
657                 selected_email_addr_list = g_slist_append (selected_email_addr_list, current_text);
658                 
659         }
660
661         gtk_widget_destroy(select_email_addr_dlg);
662         return selected_email_addr_list;
663 }
664
665
666 static gboolean /* make this public? */
667 add_to_address_book (const gchar* address)
668 {
669         EBookQuery *query;
670         GList *contacts = NULL;
671         GError *err = NULL;
672         gchar *email;
673         
674         g_return_val_if_fail (address, FALSE);
675         
676         if (!book) {
677                 if (!open_addressbook ()) {
678                         g_return_val_if_reached (FALSE);
679                 }
680         }
681         
682         g_return_val_if_fail (book, FALSE);
683
684         email = modest_text_utils_get_email_address (address);
685         
686         query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, email);
687         if (!e_book_get_contacts (book, query, &contacts, &err)) {
688                 g_printerr ("modest: failed to get contacts: %s",
689                             err ? err->message : "<unknown>");
690                 if (err)
691                         g_error_free (err);
692                 return FALSE;
693         }
694         e_book_query_unref (query);
695         
696         /*  we need to 'commit' it, even if we already found the email
697          * address in the addressbook; thus, it will show up in the 'recent list' */
698         if (contacts)  {                
699                 g_debug ("%s already in the address book", address);
700                 commit_contact ((EContact*)contacts->data, FALSE);
701                 
702                 g_list_foreach (contacts, (GFunc)unref_gobject, NULL);
703                 g_list_free (contacts);
704
705         } else {
706                 /* it's not yet in the addressbook, add it now! */
707                 EContact *new_contact = e_contact_new ();
708                 gchar *display_address;
709                 display_address = g_strdup (address);
710                 if (display_address) {
711                         modest_text_utils_get_display_address (display_address);
712                         if ((display_address[0] != '\0') && (strlen (display_address) != strlen (address)))
713                                 e_contact_set (new_contact, E_CONTACT_FULL_NAME, (const gpointer)display_address);
714                 }
715                 e_contact_set (new_contact, E_CONTACT_EMAIL_1, (const gpointer)email);
716                 g_free (display_address);
717                 commit_contact (new_contact, TRUE);
718                 g_debug ("%s added to address book", address);
719                 g_object_unref (new_contact);
720         }
721
722         g_free (email);
723
724         return TRUE;
725 }
726
727 typedef struct _CheckNamesInfo {
728         GtkWidget *banner;
729         guint show_banner_timeout;
730         guint hide_banner_timeout;
731         gboolean hide;
732         gboolean free_info;
733 } CheckNamesInfo;
734
735 static void
736 hide_check_names_banner (CheckNamesInfo *info)
737 {
738         if (info->show_banner_timeout > 0) {
739                 g_source_remove (info->show_banner_timeout);
740                 info->show_banner_timeout = 0;
741         }
742         if (info->hide_banner_timeout > 0) {
743                 info->hide = TRUE;
744                 return;
745         }
746
747         if (info->banner) {
748                 gtk_widget_destroy (info->banner);
749                 info->banner = NULL;
750                 info->hide = FALSE;
751         }
752
753         if (info->free_info) {
754                 g_slice_free (CheckNamesInfo, info);
755         }
756 }
757
758 static gboolean hide_banner_timeout_handler (CheckNamesInfo *info)
759 {
760         info->hide_banner_timeout = 0;
761         if (info->hide) {
762                 gtk_widget_destroy (info->banner);
763                 info->banner = NULL;
764         }
765         if (info->free_info) {
766                 g_slice_free (CheckNamesInfo, info);
767         }
768         return FALSE;
769 }
770
771 static gboolean show_banner_timeout_handler (CheckNamesInfo *info)
772 {
773         info->show_banner_timeout = 0;
774         info->banner = hildon_banner_show_animation (NULL, NULL, _("mail_ib_checking_names"));
775         info->hide_banner_timeout = g_timeout_add (1000, (GSourceFunc) hide_banner_timeout_handler, (gpointer) info);
776         return FALSE;
777 }
778
779 static void show_check_names_banner (CheckNamesInfo *info)
780 {
781         if (info->hide_banner_timeout > 0) {
782                 g_source_remove (info->hide_banner_timeout);
783                 info->hide_banner_timeout = 0;
784         }
785
786         info->hide = FALSE;
787         if (info->show_banner_timeout > 0)
788                 return;
789
790         if (info->banner == NULL) {
791                 info->show_banner_timeout = g_timeout_add (500, (GSourceFunc) show_banner_timeout_handler, (gpointer) info);
792         }
793 }
794
795 static void clean_check_names_banner (CheckNamesInfo *info)
796 {
797         if (info->hide_banner_timeout) {
798                 info->free_info = TRUE;
799         } else {
800                 if (info->show_banner_timeout) {
801                         g_source_remove (info->show_banner_timeout);
802                 }
803                 if (info->banner)
804                         gtk_widget_destroy (info->banner);
805                 g_slice_free (CheckNamesInfo, info);
806         }
807 }
808
809 gboolean
810 modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean update_addressbook)
811 {
812         const gchar *recipients = NULL;
813         GSList *start_indexes = NULL, *end_indexes = NULL;
814         GSList *current_start, *current_end;
815         gboolean result = TRUE;
816         GtkTextBuffer *buffer;
817         gint offset_delta = 0;
818         gint last_length;
819         GtkTextIter start_iter, end_iter;
820
821         g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
822
823         recipients = modest_recpt_editor_get_recipients (recpt_editor);
824         last_length = g_utf8_strlen (recipients, -1);
825         modest_text_utils_get_addresses_indexes (recipients, &start_indexes, &end_indexes);
826
827         if (start_indexes == NULL) {
828                 if (last_length != 0) {
829                         hildon_banner_show_information (NULL, NULL, _("mcen_nc_no_matching_contacts"));
830                         return FALSE;
831                 } else {
832                         return TRUE;
833                 }
834         }
835
836         current_start = start_indexes;
837         current_end = end_indexes;
838         buffer = modest_recpt_editor_get_buffer (recpt_editor);
839
840         while (current_start != NULL) {
841                 gchar *address;
842                 gchar *start_ptr, *end_ptr;
843                 gint start_pos, end_pos;
844                 const gchar *invalid_char_position = NULL;
845                 gboolean store_address = FALSE;
846
847                 start_pos = (*((gint*) current_start->data)) + offset_delta;
848                 end_pos = (*((gint*) current_end->data)) + offset_delta;
849                
850                 start_ptr = g_utf8_offset_to_pointer (recipients, start_pos);
851                 end_ptr = g_utf8_offset_to_pointer (recipients, end_pos);
852
853                 address = g_strstrip (g_strndup (start_ptr, end_ptr - start_ptr));
854                 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_pos);
855                 gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_pos);
856                 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
857
858                 if (!modest_text_utils_validate_recipient (address, &invalid_char_position)) {
859                         if ((invalid_char_position != NULL) && (*invalid_char_position != '\0')) {
860                                 gchar *char_in_string = g_strdup_printf("%c", *invalid_char_position);
861                                 gchar *message = 
862                                         g_strdup_printf(_CS("ckdg_ib_illegal_characters_entered"), 
863                                                         char_in_string);
864                                 g_free (char_in_string);
865                                 hildon_banner_show_information (NULL, NULL, message );
866                                 g_free (message);
867                                 result = FALSE;
868                         } else if (strstr (address, "@") == NULL) {
869                                 /* here goes searching in addressbook */
870                                 gboolean canceled;
871                                 GSList *contact_ids = NULL;
872                                 GSList *resolved_addresses = NULL;
873
874                                 result = resolve_address (address, &resolved_addresses, &contact_ids, &canceled);
875
876                                 if (result) {
877                                         gint new_length;
878                                         GSList *contact_ids_node, *resolved_addresses_node;
879
880                                         contact_ids_node = contact_ids;
881                                         resolved_addresses_node = resolved_addresses;
882
883                                         while (contact_ids_node != NULL) {
884                                                 gchar *contact_id = (gchar *) contact_ids_node->data;
885                                                 GSList *resolved_addresses_for_contact = 
886                                                         (GSList *) resolved_addresses_node->data;
887
888                                                 /* replace string */
889                                                 modest_recpt_editor_replace_with_resolved_recipient 
890                                                         (recpt_editor,
891                                                          &start_iter, &end_iter,
892                                                          resolved_addresses_for_contact, 
893                                                          contact_id);
894
895                                                 g_free (contact_id);
896                                                 g_slist_foreach (resolved_addresses_for_contact, (GFunc) g_free, NULL);
897                                                 g_slist_free (resolved_addresses_for_contact);
898
899                                                 contact_ids_node = g_slist_next (contact_ids_node);
900                                                 resolved_addresses_node = g_slist_next (resolved_addresses_node);
901                                         }
902                                         g_slist_free (contact_ids);
903                                         g_slist_free (resolved_addresses);
904
905                                         /* update offset delta */
906                                         recipients = modest_recpt_editor_get_recipients (recpt_editor);
907                                         new_length = g_utf8_strlen (recipients, -1);
908                                         offset_delta = offset_delta + new_length - last_length;
909                                         last_length = new_length;
910                                 } else if (canceled) {
911                                         /* We have to remove the recipient if not resolved */
912                                         modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
913                                                                                              &start_iter, &end_iter,
914                                                                                              NULL,
915                                                                                              NULL);
916                                 }
917                         } else {
918                                 /* this address is not valid, select it and return control to user showing banner */
919                                 hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
920                                 result = FALSE;
921                         }
922                 } else {
923                         GSList *tags, *node;
924                         gboolean has_recipient = FALSE;
925
926                         tags = gtk_text_iter_get_tags (&start_iter);
927                         for (node = tags; node != NULL; node = g_slist_next (node)) {
928                                 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
929                                 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id") != NULL) {
930                                         has_recipient = TRUE;
931                                         break;
932                                 }
933                         }
934                         g_slist_free (tags);
935                         if (!has_recipient) {
936                                 GSList * address_list = NULL;
937
938                                 address_list = g_slist_prepend (address_list, address);
939                                 modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
940                                                                                      &start_iter, &end_iter,
941                                                                                      address_list, 
942                                                                                      "");
943                                 g_slist_free (address_list);
944                                 store_address = TRUE;
945                         }
946                 }
947
948                 /* so, it seems a valid address */
949                 /* note: adding it the to the addressbook if it did not exist yet,
950                  * and adding it to the recent_list */
951                 if (result && update_addressbook && store_address)
952                         add_to_address_book (address);
953
954                 g_free (address);
955                 if (result == FALSE)
956                         break;
957
958                 current_start = g_slist_next (current_start);
959                 current_end = g_slist_next (current_end);
960         }
961
962         if (current_start == NULL) {
963                 gtk_text_buffer_get_end_iter (buffer, &end_iter);
964                 gtk_text_buffer_place_cursor (buffer, &end_iter);
965         }
966
967         g_slist_foreach (start_indexes, (GFunc) g_free, NULL);
968         g_slist_foreach (end_indexes, (GFunc) g_free, NULL);
969         g_slist_free (start_indexes);
970         g_slist_free (end_indexes);
971
972         return result;
973
974 }
975
976 typedef struct _GetContactsInfo {
977         GMainLoop *mainloop;
978         GList *result;
979 } GetContactsInfo;
980
981 static void 
982 get_contacts_for_name_cb (EBook *book, 
983                           EBookStatus status, 
984                           GList *list, 
985                           gpointer userdata)
986 {
987         GetContactsInfo *info = (GetContactsInfo *) userdata;
988
989         if (status == E_BOOK_ERROR_OK)
990                 info->result = list;
991
992         g_main_loop_quit (info->mainloop);
993 }
994
995 static GList *
996 get_contacts_for_name (const gchar *name)
997 {
998         EBookQuery *full_name_book_query = NULL;
999         GList *result;
1000         gchar *unquoted;
1001         GetContactsInfo *info;
1002
1003         if (name == NULL)
1004                 return NULL;
1005
1006         unquoted = unquote_string (name);
1007         full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted);
1008         g_free (unquoted);
1009
1010         /* TODO: Make it launch a mainloop */
1011         info = g_slice_new (GetContactsInfo);
1012         info->mainloop = g_main_loop_new (NULL, FALSE);
1013         info->result = NULL;
1014         if (e_book_async_get_contacts (book, full_name_book_query, get_contacts_for_name_cb, info) == 0) {
1015                 GDK_THREADS_LEAVE ();
1016                 g_main_loop_run (info->mainloop);
1017                 GDK_THREADS_ENTER ();
1018         } 
1019         result = info->result;
1020         e_book_query_unref (full_name_book_query);
1021         g_main_loop_unref (info->mainloop);
1022         g_slice_free (GetContactsInfo, info);
1023
1024         return result;
1025 }
1026
1027 static GList *
1028 select_contacts_for_name_dialog (const gchar *name)
1029 {
1030         EBookQuery *full_name_book_query = NULL;
1031         EBookView *book_view = NULL;
1032         GList *result = NULL;
1033         gchar *unquoted;
1034
1035         unquoted = unquote_string (name);
1036         full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted);
1037         g_free (unquoted);
1038         e_book_get_book_view (book, full_name_book_query, NULL, -1, &book_view, NULL);
1039         e_book_query_unref (full_name_book_query);
1040
1041         if (book_view) {
1042                 GtkWidget *contact_dialog = NULL;
1043 #if MODEST_ABOOK_API < 4
1044                 GtkWidget *contact_view = NULL;
1045                 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model), book_view);
1046                 e_book_view_start (book_view);
1047
1048                 contact_view = osso_abook_contact_selector_new_basic (contact_model);
1049                 contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
1050
1051                 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
1052                         result = osso_abook_contact_view_get_selection (OSSO_ABOOK_CONTACT_VIEW (contact_view));
1053                 }
1054                 e_book_view_stop (book_view);
1055                 g_object_unref (book_view);
1056                 gtk_widget_destroy (contact_dialog);
1057 #else /* MODEST_ABOOK_API < 4 */
1058                 osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model), book_view);
1059                 e_book_view_start (book_view);
1060
1061                 /* TODO: figure out how to make the contact chooser modal */
1062                 contact_dialog = osso_abook_contact_chooser_new_with_capabilities (NULL,
1063                                                                                    _AB("addr_ti_dia_select_contacts"),
1064                                                                                    OSSO_ABOOK_CAPS_EMAIL,
1065                                                                                    OSSO_ABOOK_CONTACT_ORDER_NAME);
1066                 /* Enable multiselection */
1067                 osso_abook_contact_chooser_set_maximum_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
1068                                                                   G_MAXUINT);
1069                 osso_abook_contact_chooser_set_model (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
1070                                                       contact_model);
1071
1072                 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK)
1073                         result = osso_abook_contact_chooser_get_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog));
1074                 e_book_view_stop (book_view);
1075                 g_object_unref (book_view);
1076                 gtk_widget_destroy (contact_dialog);
1077 #endif /* MODEST_ABOOK_API < 4 */
1078         }
1079
1080         return result;
1081 }
1082
1083 static gboolean
1084 resolve_address (const gchar *address, 
1085                  GSList **resolved_addresses, 
1086                  GSList **contact_ids,
1087                  gboolean *canceled)
1088 {
1089         GList *resolved_contacts;
1090         CheckNamesInfo *info;;
1091
1092         g_return_val_if_fail (canceled, FALSE);
1093
1094         *resolved_addresses = NULL;
1095         *contact_ids = NULL;
1096         *canceled = FALSE;
1097         info = g_slice_new0 (CheckNamesInfo);
1098         show_check_names_banner (info);
1099         
1100         contact_model = osso_abook_contact_model_new ();
1101         if (!open_addressbook_sync ()) {
1102                 hide_check_names_banner (info);
1103                 if (contact_model) {
1104                         g_object_unref (contact_model);
1105                         contact_model = NULL;
1106                 }
1107                 clean_check_names_banner (info);
1108                 return FALSE;
1109         }
1110         hide_check_names_banner (info);
1111
1112         resolved_contacts = get_contacts_for_name (address);
1113
1114         if (resolved_contacts == NULL) {
1115                 /* no matching contacts for the search string */
1116                 modest_platform_run_information_dialog (NULL, _("mcen_nc_no_matching_contacts"), FALSE);
1117                 clean_check_names_banner (info);
1118                 return FALSE;
1119         }
1120
1121         if (g_list_length (resolved_contacts) > 1) {
1122                 /* show a dialog to select the contact from the resolved ones */
1123                 g_list_free (resolved_contacts);
1124
1125                 resolved_contacts = select_contacts_for_name_dialog (address);
1126         }
1127
1128         /* get the resolved contacts (can be no contact) */
1129         if (resolved_contacts) {
1130                 GList *node;
1131                 gboolean found = FALSE;
1132
1133                 for (node = resolved_contacts; node != NULL; node = g_list_next (node)) {
1134                         EContact *contact = (EContact *) node->data;
1135                         GSList *resolved;
1136                         gchar *contact_id;
1137
1138                         resolved = get_recipients_for_given_contact (contact, canceled);
1139                         if (resolved) {
1140                                 contact_id = g_strdup (e_contact_get_const (contact, E_CONTACT_UID));
1141                                 *contact_ids = g_slist_append (*contact_ids, contact_id);
1142                                 found = TRUE;
1143                                 *resolved_addresses = g_slist_append (*resolved_addresses, resolved);
1144                         }
1145                 }
1146
1147                 g_list_foreach (resolved_contacts, (GFunc)unref_gobject, NULL);
1148                 g_list_free (resolved_contacts);
1149                 clean_check_names_banner (info);
1150
1151                 return found;
1152         } else {
1153                 /* cancelled dialog to select more than one contact or
1154                  * selected no contact */
1155                 clean_check_names_banner (info);
1156                 return FALSE;
1157         }
1158
1159 }
1160
1161 static gchar *
1162 unquote_string (const gchar *str)
1163 {
1164         GString *buffer;
1165         gchar *p;
1166
1167         if (str == NULL)
1168                 return NULL;
1169
1170         buffer = g_string_new_len (NULL, strlen (str));
1171         for (p = (gchar *) str; *p != '\0'; p = g_utf8_next_char (p)) {
1172                 if (*p == '"') {
1173                         p = g_utf8_next_char (p);
1174                         while ((*p != '\0')&&(*p != '"')) {
1175                                 if (*p == '\\') {
1176                                         g_string_append_unichar (buffer, g_utf8_get_char (p));
1177                                         p = g_utf8_next_char (p);
1178
1179                                 }
1180                                 g_string_append_unichar (buffer, g_utf8_get_char (p));
1181                                 p = g_utf8_next_char (p);
1182                         }
1183                 } else {
1184                         g_string_append_unichar (buffer, g_utf8_get_char (p));
1185                 }
1186         }
1187
1188         return g_string_free (buffer, FALSE);
1189
1190 }
1191
1192 gboolean
1193 modest_address_book_has_address (const gchar *address)
1194 {
1195         EBookQuery *query;
1196         GList *contacts = NULL;
1197         GError *err = NULL;
1198         gchar *email;
1199         gboolean result;
1200
1201         g_return_val_if_fail (address, FALSE);
1202         
1203         if (!book) {
1204                 if (!open_addressbook ()) {
1205                         g_return_val_if_reached (FALSE);
1206                 }
1207         }
1208         
1209         g_return_val_if_fail (book, FALSE);
1210
1211         email = modest_text_utils_get_email_address (address);
1212         
1213         query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, email);
1214         if (!e_book_get_contacts (book, query, &contacts, &err)) {
1215                 g_printerr ("modest: failed to get contacts: %s",
1216                             err ? err->message : "<unknown>");
1217                 if (err)
1218                         g_error_free (err);
1219                 return FALSE;
1220         }
1221         e_book_query_unref (query);
1222
1223         result = (contacts != NULL);
1224         if (contacts) {
1225                 g_list_foreach (contacts, (GFunc)unref_gobject, NULL);
1226                 g_list_free (contacts);
1227         }
1228         
1229         g_free (email);
1230
1231         return result;
1232 }