1 /* Copyright (c) 2007, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /* modest-address-book.c */
33 #include <glib/gi18n.h>
34 #include <modest-address-book.h>
35 #include <modest-text-utils.h>
36 #include <libebook/e-book.h>
37 #include <libebook/e-book-view.h>
38 #include <libosso-abook/osso-abook.h>
39 #include "modest-hildon-includes.h"
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>
48 static OssoABookContactModel *contact_model = NULL;
49 static EBook *book = NULL;
50 static EBookView * book_view = NULL;
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);
62 get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
64 if (status != E_BOOK_ERROR_OK) {
65 g_object_unref (book);
72 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model),
75 e_book_view_start (book_view);
79 book_open_cb (EBook *view, EBookStatus status, gpointer data)
81 EBookQuery *query = NULL;
83 if (status != E_BOOK_ERROR_OK) {
84 g_object_unref (book);
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);
96 book = e_book_new_system_addressbook (NULL);
100 e_book_async_open (book, FALSE, book_open_cb, NULL);
102 return TRUE; /* FIXME */
106 modest_address_book_add_address (const gchar *address)
108 OssoABookAccount *account = NULL;
109 GtkWidget *dialog = NULL;
111 contact_model = osso_abook_contact_model_new ();
112 if (!open_addressbook ()) {
114 g_object_unref (contact_model);
115 contact_model = NULL;
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));
126 g_object_unref (contact_model);
127 contact_model = NULL;
130 gtk_widget_destroy (dialog);
135 modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
137 GtkWidget *contact_view = NULL;
138 GList *contacts_list = NULL;
139 GtkWidget *contact_dialog;
140 GSList *email_addrs_per_contact = NULL;
143 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
145 contact_model = osso_abook_contact_model_new ();
146 if (!open_addressbook ()) {
148 g_object_unref (contact_model);
149 contact_model = NULL;
154 contact_view = osso_abook_contact_selector_new_basic (contact_model);
155 osso_abook_contact_selector_set_minimum_selection (OSSO_ABOOK_CONTACT_SELECTOR (contact_view), 1);
157 contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
158 osso_abook_select_dialog_set_new_contact (OSSO_ABOOK_SELECT_DIALOG (contact_dialog), TRUE);
160 gtk_widget_show (contact_dialog);
162 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
164 osso_abook_contact_selector_get_extended_selection (OSSO_ABOOK_CONTACT_SELECTOR
171 for (node = contacts_list; node != NULL; node = g_list_next (node)) {
172 EContact *contact = (EContact *) node->data;
174 email_addrs_per_contact = get_recipients_for_given_contact (contact);
175 if (email_addrs_per_contact) {
176 econtact_id = (gchar *) e_contact_get_const (contact, E_CONTACT_UID);
177 modest_recpt_editor_add_resolved_recipient (MODEST_RECPT_EDITOR (recpt_editor),
178 email_addrs_per_contact, econtact_id);
179 g_slist_foreach (email_addrs_per_contact, (GFunc) g_free, NULL);
180 g_slist_free (email_addrs_per_contact);
181 email_addrs_per_contact = NULL;
184 g_list_free (contacts_list);
188 g_object_unref (contact_view);
193 g_object_unref (contact_model);
194 contact_model = NULL;
197 gtk_widget_destroy (contact_dialog);
202 * This function returns the resolved recipients for a given EContact.
203 * If no e-mail address is defined, it launches 'Add e-mail address to contact'
204 * dialog to obtain one. If multiple e-mail addresses are found, it launches
205 * 'Select e-mail address' dialog to allow user to select one or more e-mail
206 * addresses for that contact.
208 * @param Contact of type #EContact
209 * @return List of resolved recipient strings, to be freed by calling function.
211 static GSList *get_recipients_for_given_contact(EContact * contact)
213 gchar *givenname = NULL;
214 gchar *familyname = NULL;
215 gchar *nickname = NULL;
216 gchar *emailid = NULL;
217 const gchar *display_name = NULL;
219 gchar *formatted_string = NULL;
220 gboolean email_not_present = FALSE;
221 GSList *formattedlist = NULL, *selected_email_addr_list = NULL, *node = NULL;
227 givenname = (gchar *) e_contact_get_const(contact, E_CONTACT_GIVEN_NAME);
228 familyname = (gchar *) e_contact_get_const(contact, E_CONTACT_FAMILY_NAME);
229 nickname = (gchar *) e_contact_get_const(contact, E_CONTACT_NICKNAME);
232 list = (GList *) e_contact_get(contact, E_CONTACT_EMAIL);
235 email_not_present = TRUE;
238 if (list && g_list_length(list) == 1) {
239 if (list->data == NULL || g_utf8_strlen(list->data, -1) == 0) {
240 email_not_present = TRUE;
242 emailid = g_strstrip(g_strdup(list->data));
243 if (g_utf8_strlen(emailid, -1) == 0) {
245 email_not_present = TRUE;
250 /*Launch the 'Add e-mail addr to contact' dialog if required */
251 if (email_not_present) {
252 display_name = osso_abook_contact_get_display_name(contact);
253 emailid = get_email_addr_from_user(display_name);
256 e_contact_set(contact, E_CONTACT_EMAIL_1, emailid);
257 commit_contact(contact);
262 if (givenname || familyname)
264 ui_get_formatted_email_id(givenname, familyname, emailid);
266 formatted_string = g_strdup(emailid);
267 formattedlist = g_slist_append(formattedlist, formatted_string);
271 /*Launch the 'Select e-mail address' dialog if required */
272 if (g_list_length(list) > 1) {
273 selected_email_addr_list = select_email_addrs_for_contact(list);
274 for (node = selected_email_addr_list; node != NULL; node = node->next) {
275 if (givenname || familyname)
277 ui_get_formatted_email_id(givenname, familyname, node->data);
279 formatted_string = g_strdup(node->data);
280 formattedlist = g_slist_append(formattedlist, formatted_string);
282 if (selected_email_addr_list) {
283 g_slist_foreach(selected_email_addr_list, (GFunc) g_free, NULL);
284 g_slist_free(selected_email_addr_list);
289 g_list_foreach(list, (GFunc) g_free, NULL);
293 return formattedlist;
297 * This is a helper function to commit a EContact to Address_Book application.
299 * @param contact Contact of type #EContact
303 commit_contact(EContact * contact)
305 if (!contact || !book)
308 #ifdef MODEST_HILDON_VERSION_0
309 osso_abook_contact_commit(contact, FALSE, book);
311 osso_abook_contact_commit(contact, FALSE, book, NULL);
312 #endif /* MODEST_HILDON_VERSION_0 */
316 * This is a helper function used to launch 'Add e-mail address to contact' dialog
317 * after showing appropriate notification, when there is no e-mail address defined
318 * for a selected contact.
320 * @param given_name Given name of the contact
321 * @param family_name Family name of the contact
322 * @return E-mail address string entered by user, to be freed by calling function.
325 get_email_addr_from_user(const gchar * given_name)
327 gchar *notification = NULL;
328 gchar *email_addr = NULL;
330 gboolean note_response;
333 notification = g_strdup_printf(_("mcen_nc_email_address_not_defined"), given_name);
335 note = hildon_note_new_confirmation (NULL, notification);
336 note_response = gtk_dialog_run (GTK_DIALOG(note));
337 gtk_widget_destroy (note);
338 g_free(notification);
340 if (note_response == GTK_RESPONSE_OK) {
341 email_addr = run_add_email_addr_to_contact_dlg(given_name);
348 This function is used to get the formated email id with given name and sur name
349 in the format "GIVENNAME SURNAME <EMAIL ADDRESS>".
350 @param current_given_name to hold the given name
351 @param current_sur_name to hold the sur name
352 @param current_email_id to hold the email id.
353 @return gchar* string to be freed by calling function
356 ui_get_formatted_email_id(gchar * current_given_name,
357 gchar * current_sur_name, gchar * current_email_id)
359 GString *email_id_str = NULL;
360 gchar *email_id = NULL;
362 email_id_str = g_string_new(NULL);
364 if ((current_given_name != NULL) && ((strlen(current_given_name) != 0))
365 && (current_sur_name != NULL) && ((strlen(current_sur_name) != 0))) {
366 g_string_printf(email_id_str, "%s %s %c%s%c", current_given_name,
367 current_sur_name, '<', current_email_id, '>');
368 email_id = g_strdup(email_id_str->str);
369 } else if ((current_given_name != NULL) && (strlen(current_given_name) != 0)) {
370 g_string_printf(email_id_str, "%s %c%s%c", current_given_name, '<',
371 current_email_id, '>');
372 email_id = g_strdup(email_id_str->str);
373 } else if ((current_sur_name != NULL) && (strlen(current_sur_name) != 0)) {
374 g_string_printf(email_id_str, "%s %c%s%c", current_sur_name, '<',
375 current_email_id, '>');
376 email_id = g_strdup(email_id_str->str);
378 g_string_free(email_id_str, TRUE);
384 * This is a helper function used to create & run 'Add e-mail address to contact' dialog.
385 * It allows user to enter an e-mail address, and shows appropriate infonote if the
386 * entered string is not a valid e-mail address.
388 * @param contact_name Full name of the contact
389 * @return E-mail address string entered by user, to be freed by calling function.
392 run_add_email_addr_to_contact_dlg(const gchar * contact_name)
394 GtkWidget *add_email_addr_to_contact_dlg = NULL;
395 GtkSizeGroup *size_group = NULL;
396 GtkWidget *cptn_cntrl = NULL;
397 GtkWidget *name_label = NULL;
398 GtkWidget *email_entry = NULL;
400 gchar *new_email_addr = NULL;
401 gboolean run_dialog = TRUE;
403 add_email_addr_to_contact_dlg =
404 gtk_dialog_new_with_buttons(_("mcen_ti_add_email_title"), NULL,
405 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
406 _("mcen_bd_dialog_ok"), GTK_RESPONSE_ACCEPT,
407 _("mcen_bd_dialog_cancel"), GTK_RESPONSE_REJECT, NULL);
408 gtk_dialog_set_has_separator(GTK_DIALOG(add_email_addr_to_contact_dlg), FALSE);
409 /*Set app_name & state_save related tags to the window */
411 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
412 name_label = gtk_label_new(contact_name);
413 gtk_misc_set_alignment(GTK_MISC(name_label), 0, 0);
415 hildon_caption_new(size_group, _("mcen_ia_add_email_name"), name_label, NULL,
416 HILDON_CAPTION_OPTIONAL);
417 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
420 email_entry = gtk_entry_new();
422 hildon_caption_new(size_group, _("mcen_fi_add_email_name"), email_entry, NULL,
423 HILDON_CAPTION_OPTIONAL);
424 hildon_gtk_entry_set_input_mode(GTK_ENTRY(email_entry), HILDON_GTK_INPUT_MODE_FULL);
425 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(add_email_addr_to_contact_dlg)->vbox), cptn_cntrl,
428 gtk_widget_show_all(add_email_addr_to_contact_dlg);
432 gtk_widget_grab_focus(email_entry);
433 result = gtk_dialog_run(GTK_DIALOG(add_email_addr_to_contact_dlg));
435 if (result == GTK_RESPONSE_ACCEPT) {
436 new_email_addr = g_strdup(gtk_entry_get_text(GTK_ENTRY(email_entry)));
437 new_email_addr = g_strstrip(new_email_addr);
438 if (!modest_text_utils_validate_email_address (new_email_addr)) {
439 gtk_widget_grab_focus(email_entry);
440 gtk_editable_select_region((GtkEditable *) email_entry, 0, -1);
441 hildon_banner_show_information (add_email_addr_to_contact_dlg, NULL, _("mcen_ib_invalid_email"));
443 g_free(new_email_addr);
444 new_email_addr = NULL;
449 gtk_widget_destroy(add_email_addr_to_contact_dlg);
451 return new_email_addr;
455 * This is helper function to create & run 'Select e-mail address' dialog, used when
456 * multiple e-mail addresses are found for a selected contact. It allows user to select
457 * one or more e-mail addresses for that contact.
459 * @param email_addr_list List of e-mail addresses for that contact
460 * @return List of user selected e-mail addresses, to be freed by calling function.
463 select_email_addrs_for_contact(GList * email_addr_list)
465 GtkWidget *select_email_addr_dlg = NULL;
466 GtkWidget *view = NULL, *scrolledwindow = NULL;
467 GtkTreeSelection *selection = NULL;
468 GtkCellRenderer *renderer = NULL;
469 GtkTreeViewColumn *col = NULL;
470 GtkListStore *list_store = NULL;
471 GtkTreeModel *model = NULL;
473 GList *pathslist = NULL, *node = NULL;
475 gchar *email_addr = NULL;
476 GSList *selected_email_addr_list = NULL;
478 if (!email_addr_list)
481 select_email_addr_dlg =
482 gtk_dialog_new_with_buttons(_("mcen_ti_select_email_title"),
484 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
485 _("mcen_bd_dialog_ok"), GTK_RESPONSE_ACCEPT,
486 _("mcen_bd_dialog_cancel"), GTK_RESPONSE_REJECT, NULL);
487 gtk_dialog_set_has_separator(GTK_DIALOG(select_email_addr_dlg), FALSE);
489 scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
490 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(select_email_addr_dlg)->vbox), scrolledwindow, TRUE,
492 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_AUTOMATIC,
493 GTK_POLICY_AUTOMATIC);
495 view = gtk_tree_view_new();
496 col = gtk_tree_view_column_new();
497 renderer = gtk_cell_renderer_text_new();
498 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
499 gtk_tree_view_column_pack_start(col, renderer, TRUE);
500 gtk_tree_view_column_add_attribute(col, renderer, "text", 0);
501 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
502 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
503 gtk_container_add(GTK_CONTAINER(scrolledwindow), view);
505 list_store = gtk_list_store_new(1, G_TYPE_STRING);
506 model = GTK_TREE_MODEL(list_store);
507 gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
509 for (node = email_addr_list; node != NULL && node->data != NULL; node = node->next) {
510 email_addr = g_strstrip(g_strdup(node->data));
511 gtk_list_store_append(list_store, &iter);
512 gtk_list_store_set(list_store, &iter, 0, email_addr, -1);
516 gtk_widget_show_all(select_email_addr_dlg);
517 result = gtk_dialog_run(GTK_DIALOG(select_email_addr_dlg));
519 if (result == GTK_RESPONSE_ACCEPT) {
520 pathslist = gtk_tree_selection_get_selected_rows(selection, NULL);
521 for (node = pathslist; node != NULL; node = node->next) {
522 if (gtk_tree_model_get_iter(model, &iter, (GtkTreePath *) node->data)) {
523 gtk_tree_model_get(model, &iter, 0, &email_addr, -1);
524 selected_email_addr_list =
525 g_slist_append(selected_email_addr_list, g_strdup(email_addr));
531 gtk_list_store_clear(list_store);
532 gtk_widget_destroy(select_email_addr_dlg);
533 return selected_email_addr_list;