2007-04-20 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
index 112c024..246a29f 100644 (file)
 #include <gtk/gtkcheckbutton.h>
 #include <gtk/gtkmessagedialog.h>
 #include <hildon-widgets/hildon-caption.h>
 #include <gtk/gtkcheckbutton.h>
 #include <gtk/gtkmessagedialog.h>
 #include <hildon-widgets/hildon-caption.h>
-#include "modest-easysetup-country-combo-box.h"
-#include "modest-easysetup-provider-combo-box.h"
-#include "modest-easysetup-servertype-combo-box.h"
-#include "modest-easysetup-serversecurity-combo-box.h"
-#include "modest-easysetup-secureauth-combo-box.h"
-#include "modest-validating-entry.h"
+#include "maemo/easysetup/modest-easysetup-country-combo-box.h"
+#include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
+#include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
+#include "widgets/modest-easysetup-serversecurity-combo-box.h"
+#include "widgets/modest-easysetup-secureauth-combo-box.h"
+#include "widgets/modest-validating-entry.h"
 #include "modest-text-utils.h"
 #include "modest-account-mgr.h"
 #include "modest-text-utils.h"
 #include "modest-account-mgr.h"
+#include "modest-account-mgr-helpers.h"
 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
+#include "maemo/modest-connection-specific-smtp-window.h"
 #include <gconf/gconf-client.h>
 #include <string.h> /* For strlen(). */
 
 #include <gconf/gconf-client.h>
 #include <string.h> /* For strlen(). */
 
@@ -84,6 +86,9 @@ modest_easysetup_wizard_dialog_finalize (GObject *object)
                
        if (priv->presets)
                modest_presets_destroy (priv->presets);
                
        if (priv->presets)
                modest_presets_destroy (priv->presets);
+               
+       if (self->specific_window)
+               gtk_widget_destroy (self->specific_window);
        
        G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
 }
        
        G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
 }
@@ -100,8 +105,7 @@ create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
 static void
 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
 
 static void
 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
 
-static gchar*
-util_increment_name (const gchar* text);
+static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
 
 static void
 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
 
 static void
 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
@@ -314,18 +318,16 @@ create_page_account_details (ModestEasysetupWizardDialog *self)
        self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
        
        /* Set a default account title, choosing one that does not already exist: */
        self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
        
        /* Set a default account title, choosing one that does not already exist: */
-       gchar* default_acount_name = g_strdup (_("mcen_ia_emailsetup_defaultname"));
-       while (modest_account_mgr_account_exists (self->account_manager, 
-               default_acount_name, TRUE /*  server_account */)) {
-                       
-               gchar * default_account_name2 = util_increment_name (default_acount_name);
-               g_free (default_acount_name);
-               default_acount_name = default_account_name2;
-       }
-       
-       gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_acount_name);
-       g_free (default_acount_name);
-       default_acount_name = NULL;
+       /* Note that this is irrelevant to the non-user visible name, which we will create later. */
+       gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
+       gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
+               self->account_manager, default_account_name_start);
+       g_free (default_account_name_start);
+       default_account_name_start = NULL;
+       
+       gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_account_name);
+       g_free (default_account_name);
+       default_account_name = NULL;
 
        caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
                self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
 
        caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
                self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
@@ -481,7 +483,7 @@ static void update_incoming_server_security_choices (ModestEasysetupWizardDialog
                EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
 }
 
                EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
 }
 
-void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
+static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
 {
        ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
        update_incoming_server_title (self);
 {
        ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
        update_incoming_server_title (self);
@@ -574,7 +576,25 @@ enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
        /* Set the starting sensitivity: */
        on_toggle_button_changed (button, widget);
 }
        /* Set the starting sensitivity: */
        on_toggle_button_changed (button, widget);
 }
-       
+
+static void
+on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
+{
+       ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
+       
+       /* Create the window, if necessary: */
+       if (!(self->specific_window)) {
+               self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
+               modest_connection_specific_smtp_window_fill_with_connections (
+                       MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
+                       NULL /* account_name, not known yet. */);
+       }
+
+       /* Show the window: */
+       gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
+       gtk_widget_show (self->specific_window);
+}
+
 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
 {
        GtkWidget *box = gtk_vbox_new (FALSE, 2);
 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
 {
        GtkWidget *box = gtk_vbox_new (FALSE, 2);
@@ -637,6 +657,9 @@ static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self
        /* Only enable the button when the checkbox is checked: */
        enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
                GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
        /* Only enable the button when the checkbox is checked: */
        enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
                GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
+               
+       g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
+               G_CALLBACK (on_button_outgoing_smtp_servers), self);
        
        
        gtk_widget_show (GTK_WIDGET (box));
        
        
        gtk_widget_show (GTK_WIDGET (box));
@@ -692,14 +715,10 @@ modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
        /* Read in the information about known service providers: */
        ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
        
        /* Read in the information about known service providers: */
        ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
        
-       const gchar* filepath = "/usr/share/operator-wizard/provider-data.keyfile";
-       priv->presets = modest_presets_new (filepath); /* TODO: the actual filepath. */
-       if (!(priv->presets))
-       {
-               const gchar* filepath_hack = "./src/maemo/easysetup/provider-data-test.keyfile";
-               g_warning ("Could not locate the official provider data keyfile from %s, "
-                       "so attempting to load it instead from %s", filepath, filepath_hack);
-               priv->presets = modest_presets_new (filepath_hack); /* TODO: the actual filepath. */
+       const gchar* filepath = MODEST_PROVIDERS_DATA_PATH; /* Defined in config.h */
+       priv->presets = modest_presets_new (filepath);
+       if (!(priv->presets)) {
+               g_warning ("Could not locate the official provider data keyfile from %s", filepath);
        }
        
        g_assert(priv->presets);
        }
        
        g_assert(priv->presets);
@@ -847,7 +866,8 @@ static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
        }
 }
 
        }
 }
 
-gchar*
+
+static gchar*
 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocol servertype)
 {
        if (!email_address)
 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocol servertype)
 {
        if (!email_address)
@@ -875,64 +895,11 @@ util_get_default_servername_from_email_address (const gchar* email_address, Mode
        return g_strdup_printf ("%s.%s", hostname, domain);
 }
 
        return g_strdup_printf ("%s.%s", hostname, domain);
 }
 
-/* Add a number to the end of the text, or increment a number that is already there.
- */
-static gchar*
-util_increment_name (const gchar* text)
-{
-       /* Get the end character,
-        * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
-        */
-       const gchar* end = NULL;
-       if (!g_utf8_validate (text, -1, &end))
-               return NULL;
-  
-       if (!end)
-               return NULL;
-               
-       --end; /* Go to before the null-termination. */
-               
-       /* Look at each UTF-8 characer, starting at the end: */
-       const gchar* p = end;
-       const gchar* alpha_end = NULL;
-       while (p)
-       {       
-               /* Stop when we reach the first character that is not a numeric digit: */
-               const gunichar ch = g_utf8_get_char (p);
-               if (!g_unichar_isdigit (ch)) {
-                       alpha_end = p;
-                       break;
-               }
-               
-               p = g_utf8_prev_char (p);       
-       }
-       
-       if(!alpha_end) {
-               /* The text must consist completely of numeric digits. */
-               alpha_end = text;
-       }
-       else
-               ++alpha_end;
-       
-       /* Intepret and increment the number, if any: */
-       gint num = atol (alpha_end);
-       ++num;
-       
-       /* Get the name part: */
-       gint name_len = alpha_end - text;
-       gchar *name_without_number = g_malloc(name_len + 1);
-       memcpy (name_without_number, text, name_len);
-       name_without_number[name_len] = 0;\
-       
-    /* Concatenate the text part and the new number: */        
-       gchar *result = g_strdup_printf("%s%d", name_without_number, num);
-       g_free (name_without_number);
-       
-       return result;  
-}
-       
 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
 {
 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
 {
+       if (!account_wizard->entry_incomingserver)
+               return;
+               
        /* Set a default domain for the server, based on the email address,
         * if no server name was already specified.
         */
        /* Set a default domain for the server, based on the email address,
         * if no server name was already specified.
         */
@@ -973,17 +940,17 @@ on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *
         */
        if (current_page == account_wizard->page_account_details) {     
                /* Check that the title is not already in use: */
         */
        if (current_page == account_wizard->page_account_details) {     
                /* Check that the title is not already in use: */
-               const gchar* account_name = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
-               if ((!account_name) || (strlen(account_name) == 0))
+               const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
+               if ((!account_title) || (strlen(account_title) == 0))
                        return FALSE;
                        
                        return FALSE;
                        
-               gboolean name_in_use = FALSE;
-               name_in_use = modest_account_mgr_account_exists (account_wizard->account_manager,
-                       account_name, TRUE /*  server_account */);
-               
+               /* Aavoid a clash with an existing display name: */
+               const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
+                       account_wizard->account_manager, account_title);
+
                if (name_in_use) {
                        /* Warn the user via a dialog: */
                if (name_in_use) {
                        /* Warn the user via a dialog: */
-                       show_error (GTK_WINDOW (account_wizard), _("mail_ib_account_name_already_existing."));
+                       show_error (GTK_WINDOW (account_wizard), _("mail_ib_account_name_already_existing"));
             
                        return FALSE;
                }
             
                        return FALSE;
                }
@@ -1137,18 +1104,25 @@ create_account (ModestEasysetupWizardDialog *self)
 {
        ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
        
 {
        ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
        
-       const gchar* account_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
+       const gchar* display_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
 
        /* Some checks: */
 
        /* Some checks: */
-       if (!account_name)
+       if (!display_name)
                return FALSE;
                
        /* We should have checked for this already, 
         * and changed that name accordingly, 
         * but let's check again just in case:
         */
                return FALSE;
                
        /* We should have checked for this already, 
         * and changed that name accordingly, 
         * but let's check again just in case:
         */
-       if (modest_account_mgr_account_exists (self->account_manager, account_name, FALSE)) 
+       if (modest_account_mgr_account_with_display_name_exists (self->account_manager, display_name)) 
                return FALSE;
                return FALSE;
+
+       /* Increment the non-user visible name if necessary, 
+        * based on the display name: */
+       gchar *account_name_start = g_strdup_printf ("%sID", display_name);
+       gchar* account_name = modest_account_mgr_get_unused_account_name (self->account_manager,
+               account_name_start, FALSE /* not a server account */);
+       g_free (account_name_start);
                
        /* username and password (for both incoming and outgoing): */
        const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
                
        /* username and password (for both incoming and outgoing): */
        const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
@@ -1207,8 +1181,16 @@ create_account (ModestEasysetupWizardDialog *self)
                
        }
        
                
        }
        
+       /* First we add the 2 server accounts, and then we add the account that uses them.
+        * If we don't do it in this order then we will experience a crash. */
+        
+       /* Add a (incoming) server account, to be used by the account: */
+       gchar *store_name_start = g_strconcat (account_name, "_store", NULL);
+       gchar *store_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
+               store_name_start, TRUE /* server account */);
+       g_free (store_name_start);
        gboolean created = modest_account_mgr_add_server_account (self->account_manager,
        gboolean created = modest_account_mgr_add_server_account (self->account_manager,
-               account_name,
+               store_name,
                servername_incoming,
                username, password,
                protocol_incoming,
                servername_incoming,
                username, password,
                protocol_incoming,
@@ -1223,6 +1205,16 @@ create_account (ModestEasysetupWizardDialog *self)
                return FALSE;   
        }
        
                return FALSE;   
        }
        
+       /* Sanity check: */
+       /* There must be at least one account now: */
+       GSList *account_names = modest_account_mgr_account_names (self->account_manager);
+       if(!account_names)
+       {
+               g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
+       }
+       g_slist_free (account_names);
+       
+       
        /* Outgoing server: */
        gchar* servername_outgoing = NULL;
        ModestProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
        /* Outgoing server: */
        gchar* servername_outgoing = NULL;
        ModestProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
@@ -1265,13 +1257,20 @@ create_account (ModestEasysetupWizardDialog *self)
                protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
                        EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
                
                protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
                        EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
                
+               /* TODO: 
                gboolean specific = gtk_toggle_button_get_active (
                        GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
                gboolean specific = gtk_toggle_button_get_active (
                        GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
+               */
                
        }
            
                
        }
            
+       /* Add a (outgoing) server account to be used by the account: */
+       gchar *transport_name_start = g_strconcat (account_name, "_transport", NULL);
+       gchar *transport_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
+               transport_name_start, TRUE /* server account */);
+       g_free (transport_name_start);
        created = modest_account_mgr_add_server_account (self->account_manager,
        created = modest_account_mgr_add_server_account (self->account_manager,
-               account_name,
+               transport_name,
                servername_outgoing,
                username, password,
                protocol_outgoing,
                servername_outgoing,
                username, password,
                protocol_outgoing,
@@ -1284,8 +1283,42 @@ create_account (ModestEasysetupWizardDialog *self)
                /* TODO: Provide a Logical ID for the text: */
                show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
                return FALSE;   
                /* TODO: Provide a Logical ID for the text: */
                show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
                return FALSE;   
-       }            
+       }
+       
        
        
+       /* Create the account, which will contain the two "server accounts": */
+       created = modest_account_mgr_add_account (self->account_manager, account_name, 
+               store_name, /* The name of our POP/IMAP server account. */
+               transport_name /* The name of our SMTP server account. */);
+       g_free (store_name);
+       g_free (transport_name);
+       
+       if (!created) {
+               /* TODO: Provide a Logical ID for the text: */
+               show_error (GTK_WINDOW (self), _("An error occurred while creating the account."));
+               return FALSE;   
+       }
+       
+       /* The user name and email address must be set additionally: */
+       const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (self->entry_user_name));
+       modest_account_mgr_set_string (self->account_manager, account_name,
+               MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
+
+       const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
+       modest_account_mgr_set_string (self->account_manager, account_name,
+               MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
+
+       /* Set the display name: */
+       modest_account_mgr_set_string (self->account_manager, account_name,
+               MODEST_ACCOUNT_DISPLAY_NAME, display_name, FALSE /* not server account */);
+
+       /* Save the connection-specific SMTP server accounts. */
+       if (self->specific_window)
+               return modest_connection_specific_smtp_window_save_server_accounts (
+                       MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), account_name);
+                       
+       g_free (account_name);
+
        return FALSE;
 }
 
        return FALSE;
 }