2007-04-20 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
index a9eaed6..246a29f 100644 (file)
 #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/modest-account-mgr.h"
+#include "modest-account-mgr.h"
+#include "modest-account-mgr-helpers.h"
+#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 config.h so that _() works: */
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
 
 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
@@ -78,6 +86,9 @@ modest_easysetup_wizard_dialog_finalize (GObject *object)
                
        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);
 }
@@ -94,6 +105,8 @@ create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
 static void
 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
 
+static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
+
 static void
 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
 {
@@ -303,10 +316,19 @@ create_page_account_details (ModestEasysetupWizardDialog *self)
        
        /* The description widgets: */  
        self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
-       gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), 
-               _("mcen_ia_emailsetup_defaultname")); /* default description. */
-       /* TODO: Check if an account with this default name exists, and increment if necessary. */
-               
+       
+       /* Set a default account title, choosing one that does not already exist: */
+       /* 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);
        gtk_widget_show (self->entry_account_title);
@@ -461,7 +483,7 @@ static void update_incoming_server_security_choices (ModestEasysetupWizardDialog
                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);
@@ -554,7 +576,25 @@ enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
        /* 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);
@@ -617,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));
+               
+       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));
@@ -671,14 +714,22 @@ modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
 
        /* Read in the information about known service providers: */
        ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
-       priv->presets = modest_presets_new ("provider-data-test.keyfile"); /* TODO: the actual filepath. */
-
-       /* Create the account manager object, 
+       
+       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);
+       
+       
+       /* Get the account manager object, 
         * so we can check for existing accounts,
         * and create new accounts: */
-       ModestConf *conf = modest_conf_new (); /* Just a thin wrapper around GConfClient. */
-       self->account_manager = modest_account_mgr_new (conf);
-       g_object_unref (conf);
+       self->account_manager = modest_runtime_get_account_mgr ();
+       g_assert (self->account_manager);
+       g_object_ref (self->account_manager);
        
     /* Create the common pages, 
      */
@@ -815,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)
@@ -845,6 +897,9 @@ util_get_default_servername_from_email_address (const gchar* email_address, Mode
 
 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.
         */
@@ -854,7 +909,10 @@ static void set_default_custom_servernames (ModestEasysetupWizardDialog *account
                const ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
                        EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
                const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
-               gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), util_get_default_servername_from_email_address (email_address, protocol));
+               
+               gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
+               gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), servername);
+               g_free (servername);
        }
        
        /* Set a default domain for the server, based on the email address,
@@ -864,7 +922,10 @@ static void set_default_custom_servernames (ModestEasysetupWizardDialog *account
        if ((!outgoing_existing || (strlen(outgoing_existing) == 0)) 
                && account_wizard->entry_user_email) {
                const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
-               gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP));
+               
+               gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
+               gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), servername);
+               g_free (servername);
        }
 }
 
@@ -879,20 +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: */
-               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;
                        
-               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: */
-                       /* TODO: The UI spec says we should increment the title,
-                        * as well as warning. This seems contradictory.
-                        */
-                       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;
                }
@@ -1046,18 +1104,25 @@ create_account (ModestEasysetupWizardDialog *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: */
-       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:
         */
-       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;
+
+       /* 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));
@@ -1116,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,
-               account_name,
+               store_name,
                servername_incoming,
                username, password,
                protocol_incoming,
@@ -1132,6 +1205,16 @@ create_account (ModestEasysetupWizardDialog *self)
                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;
@@ -1174,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));
                
+               /* TODO: 
                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,
-               account_name,
+               transport_name,
                servername_outgoing,
                username, password,
                protocol_outgoing,
@@ -1193,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;   
-       }            
+       }
+       
        
+       /* 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;
 }