* src/modest-text-utils.[ch]:
authorJose Dapena Paz <jdapena@igalia.com>
Tue, 3 Jul 2007 22:44:33 +0000 (22:44 +0000)
committerJose Dapena Paz <jdapena@igalia.com>
Tue, 3 Jul 2007 22:44:33 +0000 (22:44 +0000)
        * Now validate_email_address and validate_recipient also
          return the position of the invalid char in case validation
          failed because of this.
        * Added & as invalid character.
* src/maemo/modest-address-book.c:
        * Now we show a proper information banner when we enter invalid
          characters (fixes NB#59494).

pmo-trunk-r2557

src/maemo/easysetup/modest-easysetup-wizard.c
src/maemo/modest-account-settings-dialog.c
src/maemo/modest-address-book.c
src/maemo/modest-msg-view-window.c
src/modest-text-utils.c
src/modest-text-utils.h
src/modest-ui-dimming-rules.c

index 95b36ae..8c0bdff 100644 (file)
@@ -1265,7 +1265,7 @@ on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *
                if ((!email_address) || (strlen(email_address) == 0))
                        return FALSE;
                        
-               if (!modest_text_utils_validate_email_address (email_address)) {
+               if (!modest_text_utils_validate_email_address (email_address, NULL)) {
                        /* Warn the user via a dialog: */
                        hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
                                              
index 0aa7f85..c0d4d75 100644 (file)
@@ -835,7 +835,7 @@ check_data (ModestAccountSettingsDialog *self)
        if ((!email_address) || (strlen(email_address) == 0))
                return FALSE;
                        
-       if (!modest_text_utils_validate_email_address (email_address)) {
+       if (!modest_text_utils_validate_email_address (email_address, NULL)) {
                /* Warn the user via a dialog: */
                /*show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));*/
                hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
index ddb1a7e..bad6c93 100644 (file)
@@ -449,13 +449,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;
                        }
@@ -587,6 +598,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;
@@ -599,8 +611,17 @@ 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;
                                GSList *resolved_addresses = NULL;
index 22c809b..bb09bb7 100644 (file)
@@ -1428,7 +1428,7 @@ modest_msg_view_window_clipboard_owner_change (GtkClipboard *clipboard,
        parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
        selection = gtk_clipboard_wait_for_text (clipboard);
 
-       is_address = ((selection != NULL) && (modest_text_utils_validate_recipient (selection)));
+       is_address = ((selection != NULL) && (modest_text_utils_validate_recipient (selection, NULL)));
        
 /*     action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ToolsMenu/ToolsAddToContactsMenu"); */
 /*     gtk_action_set_sensitive (action, is_address); */
index 213e836..52d9ecf 100644 (file)
@@ -1018,12 +1018,15 @@ modest_text_utils_get_display_date (time_t date)
        return g_strdup(date_buf);
 }
 
-gboolean 
-modest_text_utils_validate_email_address (const gchar *email_address)
+gboolean
+modest_text_utils_validate_email_address (const gchar *email_address, const gchar **invalid_char_position)
 {
        int count = 0;
        const gchar *c = NULL, *domain = NULL;
-       static gchar *rfc822_specials = "()<>@,;:\\\"[]";
+       static gchar *rfc822_specials = "()<>@,;:\\\"[]&";
+
+       if (invalid_char_position != NULL)
+               *invalid_char_position = NULL;
 
        /* first we validate the name portion (name@domain) */
        for (c = email_address;  *c;  c++) {
@@ -1051,8 +1054,11 @@ modest_text_utils_validate_email_address (const gchar *email_address)
                        break;
                if (*c <= ' ' || *c >= 127) 
                        return FALSE;
-               if (strchr(rfc822_specials, *c)) 
+               if (strchr(rfc822_specials, *c)) {
+                       if (invalid_char_position)
+                               *invalid_char_position = c;
                        return FALSE;
+               }
        }
        if (c == email_address || *(c - 1) == '.') 
                return FALSE;
@@ -1068,21 +1074,24 @@ modest_text_utils_validate_email_address (const gchar *email_address)
                }
                if (*c <= ' ' || *c >= 127) 
                        return FALSE;
-               if (strchr(rfc822_specials, *c)) 
+               if (strchr(rfc822_specials, *c)) {
+                       if (invalid_char_position)
+                               *invalid_char_position = c;
                        return FALSE;
+               }
        } while (*++c);
 
        return (count >= 1) ? TRUE : FALSE;
 }
 
 gboolean 
-modest_text_utils_validate_recipient (const gchar *recipient)
+modest_text_utils_validate_recipient (const gchar *recipient, const gchar **invalid_char_position)
 {
        gchar *stripped, *current;
        gchar *right_part;
        gboolean has_error = FALSE;
 
-       if (modest_text_utils_validate_email_address (recipient))
+       if (modest_text_utils_validate_email_address (recipient, invalid_char_position))
                return TRUE;
        stripped = g_strdup (recipient);
        stripped = g_strstrip (stripped);
@@ -1139,7 +1148,7 @@ modest_text_utils_validate_recipient (const gchar *recipient)
 
                address = g_strndup (right_part+1, strlen (right_part) - 2);
                g_free (right_part);
-               valid = modest_text_utils_validate_email_address (address);
+               valid = modest_text_utils_validate_email_address (address, invalid_char_position);
                g_free (address);
                return valid;
        } else {
index 07b1c66..549bfbd 100644 (file)
@@ -255,16 +255,21 @@ gchar * modest_text_utils_get_display_size (guint64 size);
 /**
  * modest_text_utils_validate_email_address:
  * @email_address: a string
+ * @invalid_char_position: pointer to the position of the invalid
+ * character in case validation failed because of this, or %NULL.
  * 
  * validates the email address passed as argument
  * 
  * Returns: TRUE if the address is valid, FALSE otherwise
  **/
-gboolean     modest_text_utils_validate_email_address (const gchar *email_address);
+gboolean     modest_text_utils_validate_email_address (const gchar *email_address, 
+                                                      const gchar **invalid_char_position);
 
 /**
  * modest_text_utils_validate_recipient:
  * @recipient: a string
+ * @invalid_char_position: pointer to the position of the invalid char,
+ * if validation failed because there's an invalid char there, or %NULL.
  *
  * validates @recipient as a valid recipient field for header.
  * It's different from modest_text_utils_validate_email_address()
@@ -273,7 +278,8 @@ gboolean     modest_text_utils_validate_email_address (const gchar *email_addres
  *
  * Returns: %TRUE if the recipient is valid, FALSE otherwise
  **/
-gboolean     modest_text_utils_validate_recipient (const gchar *recipient);
+gboolean     modest_text_utils_validate_recipient (const gchar *recipient,
+                                                  const gchar **invalid_char_position);
 
 /**
  * modest_text_utils_split_addresses_list:
index 58789e1..c212f71 100644 (file)
@@ -1070,7 +1070,7 @@ modest_ui_dimming_rules_on_add_to_contacts (ModestWindow *win, gpointer user_dat
                gchar *selection = NULL;
                selection = gtk_clipboard_wait_for_text (clipboard);
 
-               dimmed = !((selection != NULL) && (modest_text_utils_validate_recipient (selection)));
+               dimmed = !((selection != NULL) && (modest_text_utils_validate_recipient (selection, NULL)));
        }
 
        return dimmed;