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;
142 gboolean focus_recpt_editor = FALSE;
145 g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
147 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (recpt_editor));
149 contact_model = osso_abook_contact_model_new ();
150 if (!open_addressbook ()) {
152 g_object_unref (contact_model);
153 contact_model = NULL;
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);
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);
164 gtk_widget_show (contact_dialog);
166 if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK) {
168 osso_abook_contact_selector_get_extended_selection (OSSO_ABOOK_CONTACT_SELECTOR
175 for (node = contacts_list; node != NULL; node = g_list_next (node)) {
176 EContact *contact = (EContact *) node->data;
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;
189 g_list_free (contacts_list);
193 g_object_unref (contact_view);
198 g_object_unref (contact_model);
199 contact_model = NULL;
202 gtk_widget_destroy (contact_dialog);
204 if (focus_recpt_editor)
205 modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (recpt_editor));
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.
216 * @param Contact of type #EContact
217 * @return List of resolved recipient strings, to be freed by calling function.
219 static GSList *get_recipients_for_given_contact(EContact * contact)
221 gchar *givenname = NULL;
222 gchar *familyname = NULL;
223 gchar *nickname = NULL;
224 gchar *emailid = NULL;
225 const gchar *display_name = NULL;
227 gchar *formatted_string = NULL;
228 gboolean email_not_present = FALSE;
229 GSList *formattedlist = NULL, *selected_email_addr_list = NULL, *node = NULL;
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);
240 list = (GList *) e_contact_get(contact, E_CONTACT_EMAIL);
243 email_not_present = TRUE;
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;
250 emailid = g_strstrip(g_strdup(list->data));
251 if (g_utf8_strlen(emailid, -1) == 0) {
253 email_not_present = TRUE;
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);
264 e_contact_set(contact, E_CONTACT_EMAIL_1, emailid);
265 commit_contact(contact);
270 if (givenname || familyname)
272 ui_get_formatted_email_id(givenname, familyname, emailid);
274 formatted_string = g_strdup(emailid);
275 formattedlist = g_slist_append(formattedlist, formatted_string);
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)
285 ui_get_formatted_email_id(givenname, familyname, node->data);
287 formatted_string = g_strdup(node->data);
288 formattedlist = g_slist_append(formattedlist, formatted_string);
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);
297 g_list_foreach(list, (GFunc) g_free, NULL);
301 return formattedlist;
305 * This is a helper function to commit a EContact to Address_Book application.
307 * @param contact Contact of type #EContact
311 commit_contact(EContact * contact)
313 if (!contact || !book)
316 #ifdef MODEST_HILDON_VERSION_0
317 osso_abook_contact_commit(contact, FALSE, book);
319 osso_abook_contact_commit(contact, FALSE, book, NULL);
320 #endif /* MODEST_HILDON_VERSION_0 */
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.
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.
333 get_email_addr_from_user(const gchar * given_name)
335 gchar *notification = NULL;
336 gchar *email_addr = NULL;
338 gboolean note_response;
341 notification = g_strdup_printf(_("mcen_nc_email_address_not_defined"), given_name);
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);
348 if (note_response == GTK_RESPONSE_OK) {
349 email_addr = run_add_email_addr_to_contact_dlg(given_name);
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
364 ui_get_formatted_email_id(gchar * current_given_name,
365 gchar * current_sur_name, gchar * current_email_id)
367 GString *email_id_str = NULL;
368 gchar *email_id = NULL;
370 email_id_str = g_string_new(NULL);
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);
386 g_string_free(email_id_str, TRUE);
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.
396 * @param contact_name Full name of the contact
397 * @return E-mail address string entered by user, to be freed by calling function.
400 run_add_email_addr_to_contact_dlg(const gchar * contact_name)
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;
408 gchar *new_email_addr = NULL;
409 gboolean run_dialog = TRUE;
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 */
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);
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,
428 email_entry = gtk_entry_new();
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,
436 gtk_widget_show_all(add_email_addr_to_contact_dlg);
440 gtk_widget_grab_focus(email_entry);
441 result = gtk_dialog_run(GTK_DIALOG(add_email_addr_to_contact_dlg));
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"));
451 g_free(new_email_addr);
452 new_email_addr = NULL;
457 gtk_widget_destroy(add_email_addr_to_contact_dlg);
459 return new_email_addr;
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.
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.
471 select_email_addrs_for_contact(GList * email_addr_list)
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;
481 GList *pathslist = NULL, *node = NULL;
483 gchar *email_addr = NULL;
484 GSList *selected_email_addr_list = NULL;
486 if (!email_addr_list)
489 select_email_addr_dlg =
490 gtk_dialog_new_with_buttons(_("mcen_ti_select_email_title"),
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);
497 scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
498 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(select_email_addr_dlg)->vbox), scrolledwindow, TRUE,
500 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_AUTOMATIC,
501 GTK_POLICY_AUTOMATIC);
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);
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);
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);
523 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter);
524 gtk_tree_selection_select_iter (selection, &iter);
526 gtk_widget_show_all(select_email_addr_dlg);
527 result = gtk_dialog_run(GTK_DIALOG(select_email_addr_dlg));
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));
541 gtk_list_store_clear(list_store);
542 gtk_widget_destroy(select_email_addr_dlg);
543 return selected_email_addr_list;