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