* let the app have the official name
[modest] / src / maemo / 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 <libosso-abook/osso-abook.h>
39 #include <hildon-widgets/hildon-note.h>
40 #include <hildon-widgets/hildon-caption.h>
41 #include <hildon-widgets/hildon-banner.h>
42 #include <string.h>
43 #include <gtk/gtksizegroup.h>
44 #include <gtk/gtkbox.h>
45 #include <gtk/gtklabel.h>
46 #include <gtk/gtkcellrenderertext.h>
47 #include <gtk/gtktreeselection.h>
48 #include <gtk/gtkentry.h>
49
50 static OssoABookContactModel *contact_model =  NULL;
51 static EBook *book = NULL;
52 static EBookView * book_view = NULL;
53
54 static GSList *get_recipients_for_given_contact(EContact * contact);
55 static void commit_contact(EContact * contact);
56 static gchar *get_email_addr_from_user(const gchar * given_name);
57 static gchar *ui_get_formatted_email_id(gchar * current_given_name,
58                                         gchar * current_sur_name, gchar * current_email_id);
59 static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name);
60 static GSList *select_email_addrs_for_contact(GList * email_addr_list);
61
62
63 static void
64 get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
65 {
66         if (status != E_BOOK_ERROR_OK) {
67                 g_object_unref (book);
68                 book = NULL;
69                 return;
70         }
71         book_view = bookview;
72
73         if (contact_model)
74                 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model),
75                                                      book_view);
76
77         e_book_view_start (book_view);
78 }
79
80 static void
81 book_open_cb (EBook *view, EBookStatus status, gpointer data)
82 {
83         EBookQuery *query = NULL;
84
85         if (status != E_BOOK_ERROR_OK) {
86                 g_object_unref (book);
87                 book = NULL;
88                 return;
89         }
90         query = e_book_query_any_field_contains ("");
91         e_book_async_get_book_view (book, query, NULL, -1, get_book_view_cb, NULL);
92         e_book_query_unref (query);
93 }
94
95 static gboolean 
96 open_addressbook ()
97 {
98         book = e_book_new_system_addressbook (NULL);
99         if (!book)
100                 return FALSE;
101
102         e_book_async_open (book, FALSE, book_open_cb, NULL);
103
104         return TRUE; /* FIXME */        
105 }
106
107 void
108 modest_address_book_add_address (const gchar *address)
109 {
110         OssoABookAccount *account = NULL;
111         GtkWidget *dialog = NULL;
112
113         contact_model = osso_abook_contact_model_new ();
114         if (!open_addressbook ()) {
115                 if (contact_model) {
116                         g_object_unref (contact_model);
117                         contact_model = NULL;
118                 }
119                 return;
120         }
121         
122         account = osso_abook_account_get (EVC_EMAIL, NULL, address);
123         dialog = osso_abook_add_to_contacts_dialog_new (contact_model, account);
124         g_object_unref (account);
125         gtk_dialog_run (GTK_DIALOG (dialog));
126
127         if (contact_model) {
128                 g_object_unref (contact_model);
129                 contact_model = NULL;
130         }
131
132         gtk_widget_destroy (dialog);
133
134 }
135
136 void
137 modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
138 {
139         GtkWidget *contact_view = NULL;
140         GList *contacts_list = NULL;
141         GtkWidget *contact_dialog;
142         GSList *email_addrs_per_contact = NULL;
143         gchar *econtact_id;
144
145         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
146
147         contact_model = osso_abook_contact_model_new ();
148         if (!open_addressbook ()) {
149                 if (contact_model) {
150                         g_object_unref (contact_model);
151                         contact_model = NULL;
152                 }
153                 return;
154         }
155
156         contact_view = osso_abook_contact_selector_new_basic (contact_model);
157         osso_abook_contact_selector_set_minimum_selection (OSSO_ABOOK_CONTACT_SELECTOR (contact_view), 1);
158
159         contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
160         osso_abook_select_dialog_set_new_contact (OSSO_ABOOK_SELECT_DIALOG (contact_dialog), TRUE);
161
162         gtk_widget_show (contact_dialog);
163
164         if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
165                 contacts_list = 
166                         osso_abook_contact_selector_get_extended_selection (OSSO_ABOOK_CONTACT_SELECTOR
167                                                                              (contact_view));
168         }
169         
170         if (contacts_list) {
171                 GList *node;
172
173                 for (node = contacts_list; node != NULL; node = g_list_next (node)) {
174                         EContact *contact = (EContact *) node->data;
175
176                         email_addrs_per_contact = get_recipients_for_given_contact (contact);
177                         if (email_addrs_per_contact) {
178                                 econtact_id = (gchar *) e_contact_get_const (contact, E_CONTACT_UID);
179                                 modest_recpt_editor_add_resolved_recipient (MODEST_RECPT_EDITOR (recpt_editor), 
180                                                                             email_addrs_per_contact, econtact_id);
181                                 g_slist_foreach (email_addrs_per_contact, (GFunc) g_free, NULL);
182                                 g_slist_free (email_addrs_per_contact);
183                                 email_addrs_per_contact = NULL;
184                         }
185                 }
186                 g_list_free (contacts_list);
187         }
188
189         if (contact_view) {
190                 g_object_unref (contact_view);
191                 contact_view = NULL;
192         }
193
194         if (contact_model) {
195                 g_object_unref (contact_model);
196                 contact_model = NULL;
197         }
198
199         gtk_widget_destroy (contact_dialog);
200
201 }
202
203 /**
204  * This function returns the resolved recipients for a given EContact.
205  * If no e-mail address is defined, it launches 'Add e-mail address to contact'
206  * dialog to obtain one. If multiple e-mail addresses are found, it launches
207  * 'Select e-mail address' dialog to allow user to select one or more e-mail
208  * addresses for that contact.
209  *
210  * @param  Contact of type #EContact
211  * @return List of resolved recipient strings, to be freed by calling function.
212  */
213 static GSList *get_recipients_for_given_contact(EContact * contact)
214 {
215         gchar *givenname = NULL;
216         gchar *familyname = NULL;
217         gchar *nickname = NULL;
218         gchar *emailid = NULL;
219         const gchar *display_name = NULL;
220         GList *list = NULL;
221         gchar *formatted_string = NULL;
222         gboolean email_not_present = FALSE;
223         GSList *formattedlist = NULL, *selected_email_addr_list = NULL, *node = NULL;
224
225         if (!contact) {
226                 return NULL;
227         }
228
229         givenname = (gchar *) e_contact_get_const(contact, E_CONTACT_GIVEN_NAME);
230         familyname = (gchar *) e_contact_get_const(contact, E_CONTACT_FAMILY_NAME);
231         nickname = (gchar *) e_contact_get_const(contact, E_CONTACT_NICKNAME);
232         if (!nickname)
233                 nickname = "";
234         list = (GList *) e_contact_get(contact, E_CONTACT_EMAIL);
235
236         if (!list) {
237                 email_not_present = TRUE;
238         }
239
240         if (list && g_list_length(list) == 1) {
241                 if (list->data == NULL || g_utf8_strlen(list->data, -1) == 0) {
242                         email_not_present = TRUE;
243                 } else {
244                         emailid = g_strstrip(g_strdup(list->data));
245                         if (g_utf8_strlen(emailid, -1) == 0) {
246                                 g_free(emailid);
247                                 email_not_present = TRUE;
248                         }
249                 }
250         }
251
252         /*Launch the 'Add e-mail addr to contact' dialog if required */
253         if (email_not_present) {
254                 display_name = osso_abook_contact_get_display_name(contact);
255                 emailid = get_email_addr_from_user(display_name);
256
257                 if (emailid) {
258                         e_contact_set(contact, E_CONTACT_EMAIL_1, emailid);
259                         commit_contact(contact);
260                 }
261         }
262
263         if (emailid) {
264                 if (givenname || familyname)
265                         formatted_string =
266                             ui_get_formatted_email_id(givenname, familyname, emailid);
267                 else
268                         formatted_string = g_strdup(emailid);
269                 formattedlist = g_slist_append(formattedlist, formatted_string);
270                 g_free(emailid);
271         }
272
273         /*Launch the 'Select e-mail address' dialog if required */
274         if (g_list_length(list) > 1) {
275                 selected_email_addr_list = select_email_addrs_for_contact(list);
276                 for (node = selected_email_addr_list; node != NULL; node = node->next) {
277                         if (givenname || familyname)
278                                 formatted_string =
279                                     ui_get_formatted_email_id(givenname, familyname, node->data);
280                         else
281                                 formatted_string = g_strdup(node->data);
282                         formattedlist = g_slist_append(formattedlist, formatted_string);
283                 }
284                 if (selected_email_addr_list) {
285                         g_slist_foreach(selected_email_addr_list, (GFunc) g_free, NULL);
286                         g_slist_free(selected_email_addr_list);
287                 }
288         }
289
290         if (list) {
291                 g_list_foreach(list, (GFunc) g_free, NULL);
292                 g_list_free(list);
293         }
294
295         return formattedlist;
296 }
297
298 /**
299  * This is a helper function to commit a EContact to Address_Book application.
300  *
301  * @param  contact  Contact of type #EContact
302  * @return void
303  */
304 static void 
305 commit_contact(EContact * contact)
306 {
307         if (!contact || !book)
308                 return;
309
310 #ifdef MODEST_HILDON_VERSION_0  
311         osso_abook_contact_commit(contact, FALSE, book);
312 #else
313         osso_abook_contact_commit(contact, FALSE, book, NULL);
314 #endif /* MODEST_HILDON_VERSION_0 */
315 }
316
317 /**
318  * This is a helper function used to launch 'Add e-mail address to contact' dialog
319  * after showing appropriate notification, when there is no e-mail address defined
320  * for a selected contact.
321  *
322  * @param  given_name  Given name of the contact
323  * @param  family_name  Family name of the contact
324  * @return E-mail address string entered by user, to be freed by calling function.
325  */
326 static gchar *
327 get_email_addr_from_user(const gchar * given_name)
328 {
329         gchar *notification = NULL;
330         gchar *email_addr = NULL;
331         GtkWidget *note;
332         gboolean note_response;
333
334
335         notification = g_strdup_printf(_("mcen_nc_email_address_not_defined"), given_name);
336
337         note = hildon_note_new_confirmation (NULL, notification);
338         note_response = gtk_dialog_run (GTK_DIALOG(note));
339         gtk_widget_destroy (note);
340         g_free(notification);
341
342         if (note_response == GTK_RESPONSE_OK) {
343                 email_addr = run_add_email_addr_to_contact_dlg(given_name);
344         }
345
346         return email_addr;
347 }
348
349 /**
350 This function is used to get the formated email id with given name and sur name
351 in the format "GIVENNAME SURNAME <EMAIL ADDRESS>".
352 @param current_given_name    to hold the given name
353 @param current_sur_name      to hold the sur name
354 @param current_email_id      to hold the email id. 
355 @return gchar* string to be freed by calling function
356 */
357 static gchar *
358 ui_get_formatted_email_id(gchar * current_given_name,
359                           gchar * current_sur_name, gchar * current_email_id)
360 {
361         GString *email_id_str = NULL;
362         gchar *email_id = NULL;
363
364         email_id_str = g_string_new(NULL);
365
366         if ((current_given_name != NULL) && ((strlen(current_given_name) != 0))
367             && (current_sur_name != NULL) && ((strlen(current_sur_name) != 0))) {
368                 g_string_printf(email_id_str, "%s %s %c%s%c", current_given_name,
369                                 current_sur_name, '<', current_email_id, '>');
370                 email_id = g_strdup(email_id_str->str);
371         } else if ((current_given_name != NULL) && (strlen(current_given_name) != 0)) {
372                 g_string_printf(email_id_str, "%s %c%s%c", current_given_name, '<',
373                                 current_email_id, '>');
374                 email_id = g_strdup(email_id_str->str);
375         } else if ((current_sur_name != NULL) && (strlen(current_sur_name) != 0)) {
376                 g_string_printf(email_id_str, "%s %c%s%c", current_sur_name, '<',
377                                 current_email_id, '>');
378                 email_id = g_strdup(email_id_str->str);
379         }
380         g_string_free(email_id_str, TRUE);
381         email_id_str = NULL;
382         return (email_id);
383 }
384
385 /**
386  * This is a helper function used to create & run 'Add e-mail address to contact' dialog.
387  * It allows user to enter an e-mail address, and shows appropriate infonote if the
388  * entered string is not a valid e-mail address.
389  *
390  * @param  contact_name  Full name of the contact
391  * @return E-mail address string entered by user, to be freed by calling function.
392  */
393 static gchar *
394 run_add_email_addr_to_contact_dlg(const gchar * contact_name)
395 {
396         GtkWidget *add_email_addr_to_contact_dlg = NULL;
397         GtkSizeGroup *size_group = NULL;
398         GtkWidget *cptn_cntrl = NULL;
399         GtkWidget *name_label = NULL;
400         GtkWidget *email_entry = NULL;
401         gint result = -1;
402         gchar *new_email_addr = NULL;
403         gboolean run_dialog = TRUE;
404
405         add_email_addr_to_contact_dlg =
406             gtk_dialog_new_with_buttons(_("mcen_ti_add_email_title"), NULL,
407                                         GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
408                                         _("mcen_bd_dialog_ok"), GTK_RESPONSE_ACCEPT,
409                                         _("mcen_bd_dialog_cancel"), GTK_RESPONSE_REJECT, NULL);
410         gtk_dialog_set_has_separator(GTK_DIALOG(add_email_addr_to_contact_dlg), FALSE);
411         /*Set app_name & state_save related tags to the window */
412
413         size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
414         name_label = gtk_label_new(contact_name);
415         gtk_misc_set_alignment(GTK_MISC(name_label), 0, 0);
416         cptn_cntrl =
417             hildon_caption_new(size_group, _("mcen_ia_add_email_name"), name_label, NULL,
418                                HILDON_CAPTION_OPTIONAL);
419         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
420                            FALSE, FALSE, 0);
421
422         email_entry = gtk_entry_new();
423         cptn_cntrl =
424             hildon_caption_new(size_group, _("mcen_fi_add_email_name"), email_entry, NULL,
425                                HILDON_CAPTION_OPTIONAL);
426         hildon_gtk_entry_set_input_mode(GTK_ENTRY(email_entry), HILDON_GTK_INPUT_MODE_FULL);
427         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
428                            TRUE, TRUE, 0);
429
430         gtk_widget_show_all(add_email_addr_to_contact_dlg);
431
432         while (run_dialog) {
433                 run_dialog = FALSE;
434                 gtk_widget_grab_focus(email_entry);
435                 result = gtk_dialog_run(GTK_DIALOG(add_email_addr_to_contact_dlg));
436
437                 if (result == GTK_RESPONSE_ACCEPT) {
438                         new_email_addr = g_strdup(gtk_entry_get_text(GTK_ENTRY(email_entry)));
439                         new_email_addr = g_strstrip(new_email_addr);
440                         if (!modest_text_utils_validate_email_address (new_email_addr)) {
441                                 gtk_widget_grab_focus(email_entry);
442                                 gtk_editable_select_region((GtkEditable *) email_entry, 0, -1);
443                                 hildon_banner_show_information (add_email_addr_to_contact_dlg, NULL, _("mcen_ib_invalid_email"));
444                                 run_dialog = TRUE;
445                                 g_free(new_email_addr);
446                                 new_email_addr = NULL;
447                         }
448                 }
449         }
450
451         gtk_widget_destroy(add_email_addr_to_contact_dlg);
452
453         return new_email_addr;
454 }
455
456 /**
457  * This is helper function to create & run 'Select e-mail address' dialog, used when
458  * multiple e-mail addresses are found for a selected contact. It allows user to select
459  * one or more e-mail addresses for that contact.
460  *
461  * @param  email_addr_list  List of e-mail addresses for that contact
462  * @return List of user selected e-mail addresses, to be freed by calling function.
463  */
464 static GSList *
465 select_email_addrs_for_contact(GList * email_addr_list)
466 {
467         GtkWidget *select_email_addr_dlg = NULL;
468         GtkWidget *view = NULL, *scrolledwindow = NULL;
469         GtkTreeSelection *selection = NULL;
470         GtkCellRenderer *renderer = NULL;
471         GtkTreeViewColumn *col = NULL;
472         GtkListStore *list_store = NULL;
473         GtkTreeModel *model = NULL;
474         GtkTreeIter iter;
475         GList *pathslist = NULL, *node = NULL;
476         gint result = -1;
477         gchar *email_addr = NULL;
478         GSList *selected_email_addr_list = NULL;
479
480         if (!email_addr_list)
481                 return NULL;
482
483         select_email_addr_dlg =
484             gtk_dialog_new_with_buttons(_("mcen_ti_select_email_title"),
485                                         NULL,
486                                         GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
487                                         _("mcen_bd_dialog_ok"), GTK_RESPONSE_ACCEPT,
488                                         _("mcen_bd_dialog_cancel"), GTK_RESPONSE_REJECT, NULL);
489         gtk_dialog_set_has_separator(GTK_DIALOG(select_email_addr_dlg), FALSE);
490
491         scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
492         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(select_email_addr_dlg)->vbox), scrolledwindow, TRUE,
493                            TRUE, 0);
494         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_AUTOMATIC,
495                                        GTK_POLICY_AUTOMATIC);
496
497         view = gtk_tree_view_new();
498         col = gtk_tree_view_column_new();
499         renderer = gtk_cell_renderer_text_new();
500         gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
501         gtk_tree_view_column_pack_start(col, renderer, TRUE);
502         gtk_tree_view_column_add_attribute(col, renderer, "text", 0);
503         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
504         gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
505         gtk_container_add(GTK_CONTAINER(scrolledwindow), view);
506
507         list_store = gtk_list_store_new(1, G_TYPE_STRING);
508         model = GTK_TREE_MODEL(list_store);
509         gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
510
511         for (node = email_addr_list; node != NULL && node->data != NULL; node = node->next) {
512                 email_addr = g_strstrip(g_strdup(node->data));
513                 gtk_list_store_append(list_store, &iter);
514                 gtk_list_store_set(list_store, &iter, 0, email_addr, -1);
515                 g_free(email_addr);
516         }
517
518         gtk_widget_show_all(select_email_addr_dlg);
519         result = gtk_dialog_run(GTK_DIALOG(select_email_addr_dlg));
520
521         if (result == GTK_RESPONSE_ACCEPT) {
522                 pathslist = gtk_tree_selection_get_selected_rows(selection, NULL);
523                 for (node = pathslist; node != NULL; node = node->next) {
524                         if (gtk_tree_model_get_iter(model, &iter, (GtkTreePath *) node->data)) {
525                                 gtk_tree_model_get(model, &iter, 0, &email_addr, -1);
526                                 selected_email_addr_list =
527                                     g_slist_append(selected_email_addr_list, g_strdup(email_addr));
528                                 g_free(email_addr);
529                         }
530                 }
531         }
532
533         gtk_list_store_clear(list_store);
534         gtk_widget_destroy(select_email_addr_dlg);
535         return selected_email_addr_list;
536 }