* src/modest-text-utils.[ch]:
[modest] / src / maemo / modest-address-book.c
index 315b442..ac1996e 100644 (file)
@@ -37,6 +37,7 @@
 #include <libebook/e-book-view.h>
 #include <libosso-abook/osso-abook.h>
 #include "modest-hildon-includes.h"
+#include "modest-platform.h"
 #include <string.h>
 #include <gtk/gtksizegroup.h>
 #include <gtk/gtkbox.h>
@@ -50,7 +51,7 @@ static EBook *book = NULL;
 static EBookView * book_view = NULL;
 
 static GSList *get_recipients_for_given_contact(EContact * contact);
-static void commit_contact(EContact * contact);
+static void commit_contact(EContact * contact, gboolean is_new);
 static gchar *get_email_addr_from_user(const gchar * given_name);
 static gchar *ui_get_formatted_email_id(gchar * current_given_name,
                                        gchar * current_sur_name, gchar * current_email_id);
@@ -61,6 +62,14 @@ static gchar *unquote_string (const gchar *str);
 
 
 static void
+unref_gobject (GObject *obj)
+{
+       if (obj)
+               g_object_unref (obj);
+}
+       
+
+static void
 get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
 {
        if (status != E_BOOK_ERROR_OK) {
@@ -121,6 +130,7 @@ modest_address_book_add_address (const gchar *address)
 {
        OssoABookAccount *account = NULL;
        GtkWidget *dialog = NULL;
+       gchar *email_address = NULL;
 
        contact_model = osso_abook_contact_model_new ();
        if (!open_addressbook ()) {
@@ -130,8 +140,11 @@ modest_address_book_add_address (const gchar *address)
                }
                return;
        }
+
+       email_address = modest_text_utils_get_email_address (address);
        
-       account = osso_abook_account_get (EVC_EMAIL, NULL, address);
+       account = osso_abook_account_get (EVC_EMAIL, NULL, email_address);
+       g_free (email_address);
        if (account)
        {
                dialog = osso_abook_add_to_contacts_dialog_new (contact_model, account);
@@ -163,7 +176,6 @@ modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
 
        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (recpt_editor));
 
-       contact_model = osso_abook_contact_model_new ();
        if (!open_addressbook ()) {
                if (contact_model) {
                        g_object_unref (contact_model);
@@ -171,12 +183,12 @@ modest_address_book_select_addresses (ModestRecptEditor *recpt_editor)
                }
                return;
        }
+       contact_model = osso_abook_contact_model_new ();
 
        contact_view = osso_abook_contact_selector_new_basic (contact_model);
        osso_abook_contact_selector_set_minimum_selection (OSSO_ABOOK_CONTACT_SELECTOR (contact_view), 1);
 
        contact_dialog = osso_abook_select_dialog_new (OSSO_ABOOK_TREE_VIEW (contact_view));
-       osso_abook_select_dialog_set_new_contact (OSSO_ABOOK_SELECT_DIALOG (contact_dialog), TRUE);
        gtk_window_set_title (GTK_WINDOW (contact_dialog), _("mcen_ti_select_recipients"));
 
        gtk_widget_show (contact_dialog);
@@ -275,7 +287,7 @@ static GSList *get_recipients_for_given_contact(EContact * contact)
 
                if (emailid) {
                        e_contact_set(contact, E_CONTACT_EMAIL_1, emailid);
-                       commit_contact(contact);
+                       commit_contact(contact, FALSE);
                }
        }
 
@@ -321,15 +333,18 @@ static GSList *get_recipients_for_given_contact(EContact * contact)
  * @return void
  */
 static void 
-commit_contact(EContact * contact)
+commit_contact(EContact * contact, gboolean is_new)
 {
+       g_return_if_fail (contact);
+       g_return_if_fail (book);
+       
        if (!contact || !book)
                return;
-
-#ifdef MODEST_HILDON_VERSION_0 
-       osso_abook_contact_commit(contact, FALSE, book);
+       
+#ifdef MODEST_HAVE_OLD_ABOOK   
+       osso_abook_contact_commit(contact, is_new, book);
 #else
-       osso_abook_contact_commit(contact, FALSE, book, NULL);
+       osso_abook_contact_commit(contact, is_new, book, NULL);
 #endif /* MODEST_HILDON_VERSION_0 */
 }
 
@@ -450,13 +465,24 @@ run_add_email_addr_to_contact_dlg(const gchar * contact_name)
                result = gtk_dialog_run(GTK_DIALOG(add_email_addr_to_contact_dlg));
 
                if (result == GTK_RESPONSE_ACCEPT) {
+                       const gchar *invalid_char_offset = NULL;
                        new_email_addr = g_strdup(gtk_entry_get_text(GTK_ENTRY(email_entry)));
                        new_email_addr = g_strstrip(new_email_addr);
-                       if (!modest_text_utils_validate_email_address (new_email_addr)) {
+                       if (!modest_text_utils_validate_email_address (new_email_addr, &invalid_char_offset)) {
                                gtk_widget_grab_focus(email_entry);
+                               if ((invalid_char_offset != NULL)&&(*invalid_char_offset != '\0')) {
+                                       gchar *char_in_string = g_strdup_printf ("%c", *invalid_char_offset);
+                                       gchar *message = g_strdup_printf(
+                                               dgettext("hildon-common-strings", "ckdg_ib_illegal_characters_entered"), 
+                                               char_in_string);
+                                       hildon_banner_show_information (
+                                               add_email_addr_to_contact_dlg, NULL, message );
+                                       g_free (message);
+                               } else {
+                                       hildon_banner_show_information (add_email_addr_to_contact_dlg, NULL, _("mcen_ib_invalid_email"));
+                                       run_dialog = TRUE;
+                               }
                                gtk_editable_select_region((GtkEditable *) email_entry, 0, -1);
-                               hildon_banner_show_information (add_email_addr_to_contact_dlg, NULL, _("mcen_ib_invalid_email"));
-                               run_dialog = TRUE;
                                g_free(new_email_addr);
                                new_email_addr = NULL;
                        }
@@ -503,6 +529,10 @@ select_email_addrs_for_contact(GList * email_addr_list)
                                        _("mcen_bd_dialog_cancel"), GTK_RESPONSE_REJECT, NULL);
        gtk_dialog_set_has_separator(GTK_DIALOG(select_email_addr_dlg), FALSE);
 
+       /* Make the window approximately big enough, because it doesn't resize to be big enough 
+        * for the window title text: */
+       gtk_window_set_default_size (GTK_WINDOW (select_email_addr_dlg), 400, -1);
+
        scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
        gtk_box_pack_start(GTK_BOX(GTK_DIALOG(select_email_addr_dlg)->vbox), scrolledwindow, TRUE,
                           TRUE, 0);
@@ -552,8 +582,65 @@ select_email_addrs_for_contact(GList * email_addr_list)
        return selected_email_addr_list;
 }
 
+
+static gboolean /* make this public? */
+add_to_address_book (const gchar* address)
+{
+       EBookQuery *query;
+       GList *contacts = NULL;
+       GError *err = NULL;
+       
+       g_return_val_if_fail (address, FALSE);
+       
+       if (!book)
+               open_addressbook ();
+       
+       g_return_val_if_fail (book, FALSE);
+       
+       query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, address);
+       if (!e_book_get_contacts (book, query, &contacts, &err)) {
+               g_printerr ("modest: failed to get contacts: %s",
+                           err ? err->message : "<unknown>");
+               if (err)
+                       g_error_free (err);
+               return FALSE;
+       }
+       e_book_query_unref (query);
+       
+       /*  we need to 'commit' it, even if we already found the email
+        * address in the addressbook; thus, it will show up in the 'recent list' */
+       if (contacts)  {                
+               g_debug ("%s already in the address book", address);
+               commit_contact ((EContact*)contacts->data, FALSE);
+               
+               g_list_foreach (contacts, (GFunc)unref_gobject, NULL);
+               g_list_free (contacts);
+
+       } else {
+               /* it's not yet in the addressbook, add it now! */
+               EContact *new_contact = e_contact_new ();
+               e_contact_set (new_contact, E_CONTACT_EMAIL_1, (const gpointer)address);
+               commit_contact (new_contact, TRUE);
+               g_debug ("%s added to address book", address);
+               g_object_unref (new_contact);
+       }
+
+       return TRUE;
+}
+
+static gboolean
+show_check_names_banner (gpointer data)
+{
+       GtkWidget **banner = (GtkWidget **) data;
+
+       *banner = hildon_banner_show_animation (NULL, NULL, _("mail_ib_checking_names"));
+       g_object_ref (G_OBJECT (*banner));
+
+       return FALSE;
+}
+
 gboolean
-modest_address_book_check_names (ModestRecptEditor *recpt_editor)
+modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean update_addressbook)
 {
        const gchar *recipients = NULL;
        GSList *start_indexes = NULL, *end_indexes = NULL;
@@ -563,20 +650,22 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor)
        gint offset_delta = 0;
        gint last_length;
        GtkTextIter start_iter, end_iter;
-       GtkWidget *banner;
+       GtkWidget *banner = NULL;
+       guint show_banner_timeout;
 
        g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
 
-       banner = hildon_banner_show_animation (NULL, NULL, _("mail_ib_checking_names"));
-       g_object_ref (G_OBJECT (banner));
-
+       show_banner_timeout = g_timeout_add (2000, show_check_names_banner, &banner);
        recipients = modest_recpt_editor_get_recipients (recpt_editor);
        last_length = g_utf8_strlen (recipients, -1);
        modest_text_utils_get_addresses_indexes (recipients, &start_indexes, &end_indexes);
 
        if (start_indexes == NULL) {
-               gtk_widget_destroy (banner);
-               g_object_unref (G_OBJECT(banner));
+               g_source_remove (show_banner_timeout);
+               if (banner != NULL) {
+                       gtk_widget_destroy (banner);
+                       g_object_unref (G_OBJECT(banner));
+               }
                return TRUE;
        }
 
@@ -588,6 +677,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor)
                gchar *address;
                gchar *start_ptr, *end_ptr;
                gint start_pos, end_pos;
+               const gchar *invalid_char_position = NULL;
 
                start_pos = (*((gint*) current_start->data)) + offset_delta;
                end_pos = (*((gint*) current_end->data)) + offset_delta;
@@ -600,10 +690,19 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor)
                gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_pos);
                gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
 
-               if (!modest_text_utils_validate_recipient (address)) {
-                       if (strstr (address, "@") == NULL) {
+               if (!modest_text_utils_validate_recipient (address, &invalid_char_position)) {
+                       if ((invalid_char_position != NULL) && (*invalid_char_position != '\0')) {
+                               gchar *char_in_string = g_strdup_printf("%c", *invalid_char_position);
+                               gchar *message = g_strdup_printf(
+                                       dgettext("hildon-common-strings", "ckdg_ib_illegal_characters_entered"), 
+                                       char_in_string);
+                               g_free (char_in_string);
+                               hildon_banner_show_information (NULL, NULL, message );
+                               g_free (message);                               
+                               result = FALSE;
+                       } else if (strstr (address, "@") == NULL) {
                                /* here goes searching in addressbook */
-                               gchar *contact_id = NULL;;
+                               gchar *contact_id = NULL;
                                GSList *resolved_addresses = NULL;
                                result = resolve_address (address, &resolved_addresses, &contact_id);
 
@@ -622,15 +721,21 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor)
                                        recipients = modest_recpt_editor_get_recipients (recpt_editor);
                                        new_length = g_utf8_strlen (recipients, -1);
                                        offset_delta = offset_delta + new_length - last_length;
-                                       last_length = new_length;
+                                       last_length = new_length;                                       
                                }
                        } else {
                                /* this address is not valid, select it and return control to user showing banner */
-
-                               hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_mail"));
+                               hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
                                result = FALSE;
                        }
-               }
+               } 
+
+               /* so, it seems a valid address */
+               /* note: adding it the to the addressbook if it did not exist yet,
+                * and adding it to the recent_list */
+               if (result && update_addressbook)
+                       add_to_address_book (address);
+
                g_free (address);
                if (result == FALSE)
                        break;
@@ -644,8 +749,11 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor)
                gtk_text_buffer_place_cursor (buffer, &end_iter);
        }
 
-       gtk_widget_destroy (banner);
-       g_object_unref (G_OBJECT (banner));
+       g_source_remove (show_banner_timeout);
+       if (banner != NULL) {
+               gtk_widget_destroy (banner);
+               g_object_unref (G_OBJECT (banner));
+       }
        modest_recpt_editor_grab_focus (recpt_editor);
 
        g_slist_foreach (start_indexes, (GFunc) g_free, NULL);
@@ -729,7 +837,7 @@ resolve_address (const gchar *address, GSList **resolved_addresses, gchar **cont
 
        if (resolved_contacts == NULL) {
                /* no matching contacts for the search string */
-               hildon_banner_show_information (NULL, NULL, _("mcen_nc_no_matching_contacts"));
+               modest_platform_run_information_dialog (NULL, _("mcen_nc_no_matching_contacts"));
                return FALSE;
        }
 
@@ -748,7 +856,10 @@ resolve_address (const gchar *address, GSList **resolved_addresses, gchar **cont
                if (*resolved_addresses) {
                        *contact_id = g_strdup (e_contact_get_const (contact, E_CONTACT_UID));
                }
-               /* TODO: free the resolved_contacts list */
+
+               g_list_foreach (resolved_contacts, (GFunc)unref_gobject, NULL);
+               g_list_free (resolved_contacts);
+
                return TRUE;
        } else {
                /* cancelled dialog to select more than one contact or