2007-04-20 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
index 744a719..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);
 }
@@ -103,9 +107,6 @@ set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
 
 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
 
-static gchar*
-util_increment_name (const gchar* text);
-
 static void
 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
 {
@@ -318,18 +319,15 @@ create_page_account_details (ModestEasysetupWizardDialog *self)
        
        /* 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_acount_name = g_strdup (_("mcen_ia_emailsetup_defaultname"));
-       while (modest_account_mgr_account_with_display_name_exists (self->account_manager, 
-               default_acount_name)) {
-                       
-               gchar * default_account_name2 = util_increment_name (default_acount_name);
-               g_free (default_acount_name);
-               default_acount_name = default_account_name2;
-       }
+       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_acount_name);
-       g_free (default_acount_name);
-       default_acount_name = 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);
@@ -578,30 +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);
        
-       /* 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 ());
+       /* 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. */);
+       }
 
-       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);
+       /* 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)
@@ -875,6 +866,7 @@ static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
        }
 }
 
+
 static gchar*
 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocol servertype)
 {
@@ -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,13 +940,13 @@ 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;
                        
                /* 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_name);
+                       account_wizard->account_manager, account_title);
 
                if (name_in_use) {
                        /* Warn the user via a dialog: */
@@ -1183,14 +1119,10 @@ create_account (ModestEasysetupWizardDialog *self)
 
        /* Increment the non-user visible name if necessary, 
         * based on the display name: */
-       gchar *account_name = g_strdup_printf ("%sID", display_name);
-       while (modest_account_mgr_account_exists (self->account_manager, 
-               account_name, FALSE /*  server_account */)) {
-                       
-               gchar * account_name2 = util_increment_name (account_name);
-               g_free (account_name);
-               account_name = account_name2;
-       }
+       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));
@@ -1253,7 +1185,10 @@ create_account (ModestEasysetupWizardDialog *self)
         * 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 = g_strconcat (account_name, "_store", NULL);
+       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,
                store_name,
                servername_incoming,
@@ -1273,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.");
        }
@@ -1330,7 +1265,10 @@ create_account (ModestEasysetupWizardDialog *self)
        }
            
        /* Add a (outgoing) server account to be used by the account: */
-       gchar *transport_name = g_strconcat (account_name, "_transport", NULL); /* What is this good for? */
+       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,
                transport_name,
                servername_outgoing,
@@ -1374,6 +1312,11 @@ create_account (ModestEasysetupWizardDialog *self)
        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;