* Fixes NB@63545
[modest] / src / maemo / modest-account-settings-dialog.c
index 766c4f6..12656cb 100644 (file)
@@ -71,6 +71,9 @@
 
 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
 
+#define PORT_MIN 1
+#define PORT_MAX 65535
+
 G_DEFINE_TYPE (ModestAccountSettingsDialog, modest_account_settings_dialog, GTK_TYPE_DIALOG);
 
 #define ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
@@ -257,19 +260,30 @@ static GtkWidget* create_caption_new_with_asterisk(ModestAccountSettingsDialog *
 static void
 on_entry_invalid_account_title_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
 {
-       gchar *message = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"),
-                                         "\\ / : * ? \" < > | ^");
-       show_error (GTK_WIDGET (self), message);
+       gchar *tmp, *msg;
+                       
+       tmp = g_strndup (account_title_forbidden_chars, ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH);
+       msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
+
+       show_error (GTK_WIDGET (self), msg);
+
+       g_free (msg);
+       g_free (tmp);
 }
 
 static void
 on_entry_invalid_fullname_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
 {
-       gchar *message = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"),
-                                         "< >");
-       show_error (GTK_WIDGET (self), message);
-}
+       gchar *tmp, *msg;
+                       
+       tmp = g_strndup (user_name_forbidden_chars, USER_NAME_FORBIDDEN_CHARS_LENGTH);
+       msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
 
+       show_error (GTK_WIDGET (self), msg);
+
+       g_free (msg);
+       g_free (tmp);
+}
 
 
 static void
@@ -365,6 +379,31 @@ create_page_account_details (ModestAccountSettingsDialog *self)
        return GTK_WIDGET (box);
 }
 
+static gchar*
+get_entered_account_title (ModestAccountSettingsDialog *dialog)
+{
+       const gchar* account_title = 
+               gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
+       if (!account_title || (strlen (account_title) == 0))
+               return NULL;
+       else {
+               /* Strip it of whitespace at the start and end: */
+               gchar *result = g_strdup (account_title);
+               result = g_strstrip (result);
+               
+               if (!result)
+                       return NULL;
+                       
+               if (strlen (result) == 0) {
+                       g_free (result);
+                       return NULL;    
+               }
+               
+               return result;
+       }
+}
+
+
 static void
 on_button_signature (GtkButton *button, gpointer user_data)
 {
@@ -377,10 +416,12 @@ on_button_signature (GtkButton *button, gpointer user_data)
                gboolean use_signature = FALSE;
                gchar *signature = modest_account_mgr_get_signature(self->account_manager, self->account_name, 
                        &use_signature);
-               const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
+               gchar* account_title = get_entered_account_title (self);
                modest_signature_editor_dialog_set_settings (
                        MODEST_SIGNATURE_EDITOR_DIALOG (self->signature_dialog), 
                        use_signature, signature, account_title);
+               g_free (account_title);
+               account_title = NULL;
                g_free (signature);
                signature = NULL;
        }
@@ -601,9 +642,8 @@ static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
        
        
        /* The port widgets: */
-       /* TODO: There are various rules about this in the UI spec. */
        if (!self->entry_incoming_port)
-               self->entry_incoming_port = GTK_WIDGET (hildon_number_editor_new (1, 65535));
+               self->entry_incoming_port = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
        caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
                self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
        gtk_widget_show (self->entry_incoming_port);
@@ -808,7 +848,7 @@ static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
        
        /* The port widgets: */
        if (!self->entry_outgoing_port)
-               self->entry_outgoing_port = GTK_WIDGET (hildon_number_editor_new (1, 65535));
+               self->entry_outgoing_port = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
        caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
                self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
        gtk_widget_show (self->entry_outgoing_port);
@@ -861,14 +901,45 @@ static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
 }
 
 
+/** TODO: This doesn't work because hildon_number_editor_get_value() does not work until 
+ * focus has been lost:
+ * See https://bugs.maemo.org/show_bug.cgi?id=1806.
+ */
+static gboolean
+check_hildon_number_editor_and_warn_value_not_in_range (HildonNumberEditor *widget, gint min, gint max)
+{
+       g_return_val_if_fail (widget, FALSE);
+       
+       const gint port = hildon_number_editor_get_value (widget);
+       /* printf ("DEBUG: %s, port=%d\n", __FUNCTION__, port); */
+       if (port < PORT_MIN || 
+               port > PORT_MAX) {
+                       
+               /* Warn the user via a dialog: */
+               /*show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));*/
+               gchar *message = g_strdup_printf (_CS("ckct_ib_set_a_value_within_range"), 
+                                      min, 
+                                      max);
+               hildon_banner_show_information (GTK_WIDGET (widget), NULL, message);
+               g_free (message);
+               message = NULL;
+
+               /* Return focus to the email address entry: */
+               gtk_widget_grab_focus (GTK_WIDGET (widget));
+               
+               return FALSE;
+       }
+       
+       return TRUE;
+}
 
        
 static gboolean
 check_data (ModestAccountSettingsDialog *self)
 {
        /* Check that the title is not already in use: */
-       const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
-       if ((!account_title) || (strlen(account_title) == 0))
+       gchar* account_title = get_entered_account_title (self);
+       if (!account_title)
                return FALSE; /* Should be prevented already anyway. */
                
        if (strcmp(account_title, self->original_account_title) != 0) {
@@ -880,14 +951,19 @@ check_data (ModestAccountSettingsDialog *self)
                        /* Warn the user via a dialog: */
                        hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
                
+               g_free (account_title);
                        return FALSE;
                }
        }
+       
+       g_free (account_title);
+       account_title  = NULL;
 
        /* Check that the email address is valid: */
        const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
-       if ((!email_address) || (strlen(email_address) == 0))
+       if ((!email_address) || (strlen(email_address) == 0)) {
                return FALSE;
+       }
                        
        if (!modest_text_utils_validate_email_address (email_address, NULL)) {
                /* Warn the user via a dialog: */
@@ -902,38 +978,53 @@ check_data (ModestAccountSettingsDialog *self)
 
        /* make sure the domain name for the incoming server is valid */
        const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver));
-       if ((!hostname) || (strlen(hostname) == 0))
+       if ((!hostname) || (strlen(hostname) == 0)) {
                return FALSE;
+       }
+       
        if (!modest_text_utils_validate_domain_name (hostname)) {
                /* Warn the user via a dialog: */
                /*show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));*/
                hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_servername"));
                                          
-               /* Return focus to the email address entry: */
-               gtk_widget_grab_focus (self->entry_incomingserver);
+               /* Return focus to the email address entry: */
+        gtk_widget_grab_focus (self->entry_incomingserver);
                gtk_editable_select_region (GTK_EDITABLE (self->entry_incomingserver), 0, -1);
                return FALSE;
        }
 
        /* make sure the domain name for the outgoing server is valid */
        const gchar* hostname2 = gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver));
-       if ((!hostname2) || (strlen(hostname2) == 0))
+       if ((!hostname2) || (strlen(hostname2) == 0)) {
                return FALSE;
+       }
+       
        if (!modest_text_utils_validate_domain_name (hostname2)) {
                /* Warn the user via a dialog: */
                /*show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));*/
-               hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_servername"));
-                                         
-               /* Return focus to the email address entry: */
-               gtk_widget_grab_focus (self->entry_outgoingserver);
+               hildon_banner_show_information (self->entry_outgoingserver, NULL, _("mcen_ib_invalid_servername"));
+
+               /* Return focus to the email address entry: */
+               gtk_widget_grab_focus (self->entry_outgoingserver);
                gtk_editable_select_region (GTK_EDITABLE (self->entry_outgoingserver), 0, -1);
                return FALSE;
        }
+       
+       /* Check that the port numbers are acceptable: */
+       if (!check_hildon_number_editor_and_warn_value_not_in_range ( 
+               HILDON_NUMBER_EDITOR (self->entry_incoming_port), PORT_MIN, PORT_MAX)) {
+               return FALSE;
+       }
+               
+       if (!check_hildon_number_editor_and_warn_value_not_in_range ( 
+               HILDON_NUMBER_EDITOR (self->entry_outgoing_port), PORT_MIN, PORT_MAX)) {
+               return FALSE;
+       }
 
                
        /* Find a suitable authentication method when secure authentication is desired */
 
-       gint port_num = hildon_number_editor_get_value (
+       const gint port_num = hildon_number_editor_get_value (
                        HILDON_NUMBER_EDITOR (self->entry_incoming_port));
        const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
 
@@ -986,9 +1077,6 @@ check_data (ModestAccountSettingsDialog *self)
                }
        }
        
-       
-
-       
        return TRUE;
 }
 /*
@@ -1291,7 +1379,7 @@ void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialo
                }
                
                const gboolean has_specific = 
-                       modest_account_mgr_get_has_connection_specific_smtp (
+                       modest_account_mgr_get_use_connection_specific_smtp (
                                dialog->account_manager, 
                                account_name);
                gtk_toggle_button_set_active (
@@ -1465,19 +1553,26 @@ save_configuration (ModestAccountSettingsDialog *dialog)
        
        
        /* Set the changed account title last, to simplify the previous code: */
-       const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
-       if ((!account_title) || (strlen(account_title) == 0))
+       gchar* account_title = get_entered_account_title (dialog);
+       if (!account_title)
                return FALSE; /* Should be prevented already anyway. */
                
        if (strcmp(account_title, account_name) != 0) {
                /* Change the title: */
-               gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
+               const gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
                MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
-               if (!test)
+               if (!test) {
+                       g_free (account_title);
                        return FALSE;
+               }
        }
        
+       g_free (account_title);
+       account_title = NULL;
+       
        /* Save connection-specific SMTP server accounts: */
+        modest_account_mgr_set_use_connection_specific_smtp(dialog->account_manager, account_name,
+               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbox_outgoing_smtp_specific)));
        if (dialog->specific_window) {
                return modest_connection_specific_smtp_window_save_server_accounts (
                        MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window), account_name);
@@ -1494,8 +1589,19 @@ static gboolean entry_is_empty (GtkWidget *entry)
        const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
        if ((!text) || (strlen(text) == 0))
                return TRUE;
-       else
-               return FALSE;
+       else {
+               /* Strip it of whitespace at the start and end: */
+               gchar *stripped = g_strdup (text);
+               stripped = g_strstrip (stripped);
+               
+               if (!stripped)
+                       return TRUE;
+                       
+               const gboolean result = (strlen (stripped) == 0);
+               
+               g_free (stripped);
+               return result;
+       }
 }
 
 static void