2007-04-20 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
index 581fb45..246a29f 100644 (file)
 #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 "maemo/easysetup/modest-easysetup-serversecurity-combo-box.h"
-#include "maemo/easysetup/modest-easysetup-secureauth-combo-box.h"
-#include "maemo/easysetup/modest-validating-entry.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-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>
@@ -85,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);
 }
@@ -101,8 +105,7 @@ create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
 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)
@@ -315,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: */
-       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);
@@ -482,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);
@@ -575,29 +576,23 @@ enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
        /* Set the starting sensitivity: */
        on_toggle_button_changed (button, widget);
 }
-       
-static void
-on_smtp_servers_window_hide (GtkWindow *window, gpointer user_data)
-{
-       /* Destroy the window when it is closed: */
-       gtk_widget_destroy (GTK_WIDGET (window));
-}
 
 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: */
-       /* TODO: Retrieve the chosen settings,
-        * so we can supply them when creating the connection somehow.
-        */
-       GtkWidget *window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
-       gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
-       g_signal_connect (G_OBJECT (window), "hide",
-               G_CALLBACK (on_smtp_servers_window_hide), self);
-    gtk_widget_show (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)
@@ -720,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);
        
-       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);
@@ -875,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)
@@ -903,62 +895,6 @@ util_get_default_servername_from_email_address (const gchar* email_address, Mode
        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)
 {
        if (!account_wizard->entry_incomingserver)
@@ -1004,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: */
-               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: */
-                       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;
                }
@@ -1168,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));
@@ -1238,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,
@@ -1257,7 +1208,7 @@ create_account (ModestEasysetupWizardDialog *self)
        /* Sanity check: */
        /* There must be at least one account now: */
        GSList *account_names = modest_account_mgr_account_names (self->account_manager);
-       if(account_names != NULL)
+       if(!account_names)
        {
                g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
        }
@@ -1313,8 +1264,13 @@ create_account (ModestEasysetupWizardDialog *self)
                
        }
            
+       /* 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,
@@ -1327,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;
 }