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