2007-04-19 Murray Cumming <murrayc@murrayc.com>
authorMurray Cumming <murrayc@murrayc.com>
Thu, 19 Apr 2007 09:25:27 +0000 (09:25 +0000)
committerMurray Cumming <murrayc@murrayc.com>
Thu, 19 Apr 2007 09:25:27 +0000 (09:25 +0000)
* src/modest-account-mgr-helpers.h:
        * src/modest-account-mgr-helpers.c:
        (modest_account_mgr_get_unused_account_name),
        (modest_account_mgr_get_unused_account_display_name): Added these
        utility functions, to avoid code duplication.

        * src/maemo/easysetup/modest-easysetup-wizard.c:
        (create_page_account_details), (create_account):
        * src/maemo/modest-connection-specific-smtp-edit-window.c:
        (on_combo_security_changed),
        (modest_connection_specific_smtp_edit_window_init),
        (modest_connection_specific_smtp_edit_window_save_settings):
        * src/maemo/modest-connection-specific-smtp-edit-window.h:
        * src/maemo/modest-connection-specific-smtp-window.c:
        (modest_connection_specific_smtp_window_finalize),
        (modest_connection_specific_smtp_window_fill_with_connections),
        (on_button_edit), (modest_connection_specific_smtp_window_init):
        * src/maemo/modest-connection-specific-smtp-window.h:
        Implementing saving of connection-specific server accounts.

        But I need to refactor this so we can save the data for each
        possible connection-specific account and only actually create the server accounts
        when the easysetup finished, or the OK button is pressed on the account settings
        dialog.

pmo-trunk-r1593

ChangeLog2
src/maemo/easysetup/modest-easysetup-wizard.c
src/maemo/modest-connection-specific-smtp-edit-window.c
src/maemo/modest-connection-specific-smtp-edit-window.h
src/maemo/modest-connection-specific-smtp-window.c
src/maemo/modest-connection-specific-smtp-window.h
src/modest-account-mgr-helpers.c
src/modest-account-mgr-helpers.h

index 1c9556a..8f8de14 100644 (file)
@@ -1,3 +1,30 @@
+2007-04-19  Murray Cumming  <murrayc@murrayc.com>
+
+       * src/modest-account-mgr-helpers.h:
+       * src/modest-account-mgr-helpers.c:
+       (modest_account_mgr_get_unused_account_name),
+       (modest_account_mgr_get_unused_account_display_name): Added these 
+       utility functions, to avoid code duplication.
+
+       * src/maemo/easysetup/modest-easysetup-wizard.c:
+       (create_page_account_details), (create_account):
+       * src/maemo/modest-connection-specific-smtp-edit-window.c:
+       (on_combo_security_changed),
+       (modest_connection_specific_smtp_edit_window_init),
+       (modest_connection_specific_smtp_edit_window_save_settings):
+       * src/maemo/modest-connection-specific-smtp-edit-window.h:
+       * src/maemo/modest-connection-specific-smtp-window.c:
+       (modest_connection_specific_smtp_window_finalize),
+       (modest_connection_specific_smtp_window_fill_with_connections),
+       (on_button_edit), (modest_connection_specific_smtp_window_init):
+       * src/maemo/modest-connection-specific-smtp-window.h:
+       Implementing saving of connection-specific server accounts.
+       
+       But I need to refactor this so we can save the data for each 
+       possible connection-specific account and only actually create the server accounts 
+       when the easysetup finished, or the OK button is pressed on the account settings 
+       dialog.
+
 2007-04-18  Murray Cumming  <murrayc@murrayc.com>
 
        * src/maemo/modest-connection-specific-smtp-edit-window.c:
index ea40c2f..06010ca 100644 (file)
@@ -23,6 +23,7 @@
 #include "maemo/easysetup/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>
@@ -103,9 +104,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 +316,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);
@@ -874,6 +869,7 @@ static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
        }
 }
 
+
 static gchar*
 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocol servertype)
 {
@@ -902,62 +898,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)
@@ -1182,14 +1122,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));
@@ -1252,7 +1188,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,
@@ -1329,7 +1268,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,
index 93f430b..ea6e9db 100644 (file)
@@ -8,6 +8,7 @@
 #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 <modest-account-mgr-helpers.h>
 #include <gtk/gtkbutton.h>
 #include <gtk/gtkhbox.h>
 #include <gtk/gtkvbox.h>
@@ -92,8 +93,8 @@ on_combo_security_changed (GtkComboBox *widget, gpointer user_data)
 {
        ModestConnectionSpecificSmtpEditWindow *self = 
                MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
-       ModestConnectionSpecificSmtpEditWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
-       
+       ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
+               CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
        
        const gint port_number = 
                easysetup_serversecurity_combo_box_get_active_serversecurity_port (
@@ -109,7 +110,8 @@ on_combo_security_changed (GtkComboBox *widget, gpointer user_data)
 static void
 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
 {
-       ModestConnectionSpecificSmtpEditWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
+       ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
+               CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
        
        GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, 2); */
        
@@ -214,3 +216,49 @@ modest_connection_specific_smtp_edit_window_set_connection (
        g_free (title);
 }
 
+gboolean
+modest_connection_specific_smtp_edit_window_save_settings (
+       ModestConnectionSpecificSmtpEditWindow *window, 
+       ModestAccountMgr *account_manager, const gchar* server_account_name)
+{
+       ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
+               CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
+       
+       g_assert (server_account_name);
+       
+       const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
+       gboolean test = modest_account_mgr_set_string (account_manager, server_account_name,
+               MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
+       if (!test)
+               return FALSE;
+               
+       const gchar* username = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username));
+       test = modest_account_mgr_set_string (account_manager, server_account_name,
+               MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
+       if (!test)
+               return FALSE;
+               
+       const gchar* password = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password));
+       test = modest_account_mgr_set_string (account_manager, server_account_name,
+               MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
+       if (!test)
+               return FALSE;
+       
+       const ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
+               EASYSETUP_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
+       modest_server_account_set_security (account_manager, server_account_name, protocol_security_outgoing);
+       
+       const ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
+               EASYSETUP_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
+       modest_server_account_set_secure_auth (account_manager, server_account_name, protocol_authentication_outgoing); 
+               
+       /* port: */
+       const gchar * port_str = gtk_entry_get_text (GTK_ENTRY (priv->entry_port));
+       gint port_num = 0;
+       if (port_str)
+               port_num = atoi (port_str);
+       modest_account_mgr_set_int (account_manager, server_account_name,
+                       MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
+                       
+       return TRUE;
+}
index 4a115e0..3626fc4 100644 (file)
@@ -30,6 +30,7 @@
 #ifndef __MODEST_MAEMO_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW
 #define __MODEST_MAEMO_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW
 
+#include <modest-account-mgr.h>
 #include <gtk/gtkdialog.h>
 #include <gtk/gtktreeview.h>
 
@@ -73,6 +74,11 @@ ModestConnectionSpecificSmtpEditWindow* modest_connection_specific_smtp_edit_win
 void modest_connection_specific_smtp_edit_window_set_connection (
        ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name);
        
+gboolean modest_connection_specific_smtp_edit_window_save_settings (
+       ModestConnectionSpecificSmtpEditWindow *window, 
+       ModestAccountMgr *account_manager, const gchar* server_account_name);
+       
+       
 G_END_DECLS
 
 #endif /* __MODEST_MAEMO_CONNECTION_SPECIFIC_SMTP_WINDOW */
index e1fd609..769a2e0 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "modest-connection-specific-smtp-window.h"
 #include "modest-connection-specific-smtp-edit-window.h"
+#include <modest-account-mgr-helpers.h>
 
 #include <modest-runtime.h>
 #include <tny-maemo-conic-device.h>
@@ -28,6 +29,9 @@ struct _ModestConnectionSpecificSmtpWindowPrivate
 {
        GtkTreeView *treeview;
        GtkTreeModel *model;
+       
+       ModestAccountMgr *account_manager;
+       gchar* account_name;
 };
 
 static void
@@ -63,6 +67,7 @@ modest_connection_specific_smtp_window_finalize (GObject *object)
        ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
 
        g_object_unref (G_OBJECT (priv->model));
+       g_free (priv->account_name);
        
        G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
 }
@@ -82,16 +87,20 @@ modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpW
 
 enum MODEL_COLS {
        MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
-       MODEL_COL_SERVER_NAME = 1, /* a string */
-       MODEL_COL_ID = 2 /* libconic IAP ID: a string */
+       MODEL_COL_ID = 1, /* libconic IAP ID: a string */
+       MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
+       MODEL_COL_SERVER_NAME = 3 /* a string */
 };
 
-static void
-fill_with_connections (ModestConnectionSpecificSmtpWindow *self)
+void
+modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, ModestAccountMgr *account_manager,
+       const gchar* account_name)
 {
-       printf("debug: fill_with_connections()\n");
        ModestConnectionSpecificSmtpWindowPrivate *priv = 
                CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
+       priv->account_manager = account_manager;
+       priv->account_name = g_strdup (account_name);
+       
        GtkListStore *liststore = GTK_LIST_STORE (priv->model);
        
        TnyDevice *device = modest_runtime_get_device ();
@@ -132,14 +141,16 @@ on_button_edit (GtkButton *button, gpointer user_data)
        ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
        
        gchar *id = NULL;
-       gchar *name = NULL;
+       gchar *connection_name = NULL;
+       gchar *server_account_name = NULL;
        GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
        GtkTreeIter iter;
        GtkTreeModel *model = 0;
        if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
                gtk_tree_model_get (priv->model, &iter, 
                                    MODEL_COL_ID, &id, 
-                                   MODEL_COL_NAME, &name, 
+                                   MODEL_COL_NAME, &connection_name, 
+                                   MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
                                    -1);
        
                /* TODO: Is 0 an allowed libconic IAP ID? 
@@ -147,15 +158,41 @@ on_button_edit (GtkButton *button, gpointer user_data)
                
                GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
                modest_connection_specific_smtp_edit_window_set_connection (
-                       MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, name);
+                       MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name);
                        
                gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
                gint response = gtk_dialog_run (GTK_DIALOG (window));
                gtk_widget_hide (window);
+               
                if (response == GTK_RESPONSE_OK) {
-
+                       if (server_account_name) {
+                               /* Change the existing server account. */
+                               modest_connection_specific_smtp_edit_window_save_settings (
+                                       MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), 
+                                       priv->account_manager, server_account_name);    
+                       } else {
+                               /* Add a new server account, building a (non-human-visible) name: */
+                               gchar *name_start = g_strdup_printf("%s_specific_%s", 
+                                       priv->account_name, connection_name);
+                               server_account_name = modest_account_mgr_get_unused_account_name (
+                                       priv->account_manager, name_start, TRUE /* server account. */);
+                               g_free (name_start);
+                               
+                               modest_connection_specific_smtp_edit_window_save_settings (
+                                       MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), 
+                                       priv->account_manager, server_account_name);
+                                       
+                               /* Store it in the model so it can be edited again: */
+                               gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
+                                   MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
+                                   -1);
+                       }
                }               
        }
+       
+       g_free (connection_name);
+       g_free (id);
+       g_free (server_account_name);
 }
 
 static void
@@ -183,7 +220,7 @@ modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow
         * with a string for the name, a string for the server name, and an int for the ID.
         * This must match our MODEL_COLS enum constants.
         */
-       priv->model = GTK_TREE_MODEL (gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING));
+       priv->model = GTK_TREE_MODEL (gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING));
 
        /* Setup the tree view: */
        priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
@@ -208,8 +245,7 @@ modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow
        gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
        gtk_tree_view_append_column (priv->treeview, view_column);
        
-       /* Fill the model with rows: */
-       fill_with_connections (self);
+       /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
        
        GtkWidget *vbox = gtk_vbox_new (FALSE, 2);
        
index 8ad34ca..b584703 100644 (file)
@@ -30,6 +30,7 @@
 #ifndef __MODEST_MAEMO_CONNECTION_SPECIFIC_SMTP_WINDOW
 #define __MODEST_MAEMO_CONNECTION_SPECIFIC_SMTP_WINDOW
 
+#include <modest-account-mgr.h>
 #include <gtk/gtkwindow.h>
 #include <gtk/gtktreeview.h>
 
@@ -70,6 +71,10 @@ GType modest_connection_specific_smtp_window_get_type (void);
 
 ModestConnectionSpecificSmtpWindow* modest_connection_specific_smtp_window_new (void);
 
+void
+modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, 
+       ModestAccountMgr *account_manager, const gchar* account_name);
+
 G_END_DECLS
 
 #endif /* __MODEST_MAEMO_CONNECTION_SPECIFIC_SMTP_WINDOW */
index 2d38cb0..fbbbc51 100644 (file)
@@ -558,3 +558,91 @@ modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
 
        return from;
 }
+
+/* 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;  
+}
+
+gchar*
+modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
+       gboolean server_account)
+{
+       gchar *account_name = g_strdup (starting_name);
+
+       while (modest_account_mgr_account_exists (self, 
+               account_name, server_account /*  server_account */)) {
+                       
+               gchar * account_name2 = util_increment_name (account_name);
+               g_free (account_name);
+               account_name = account_name2;
+       }
+       
+       return account_name;
+}
+
+gchar*
+modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
+{
+       gchar *account_name = g_strdup (starting_name);
+
+       while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
+                       
+               gchar * account_name2 = util_increment_name (account_name);
+               g_free (account_name);
+               account_name = account_name2;
+       }
+       
+       return account_name;
+}
index cbf1062..00034b5 100644 (file)
@@ -269,6 +269,31 @@ gboolean modest_server_account_data_get_option_bool (GSList* options_list, const
 gchar * modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name);
 
 
+/**
+ * modest_account_mgr_get_unused_account_name
+ * @self: a #ModestAccountMgr instance
+ * @name: The initial account name
+ *
+ * get an unused account name, based on a starting string.
+ *
+ * Returns: the newly allocated name.
+ */
+gchar*
+modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
+       gboolean server_account);
+       
+/**
+ * modest_account_mgr_get_unused_account_display name
+ * @self: a #ModestAccountMgr instance
+ * @name: The initial account display name
+ *
+ * get an unused account display name, based on a starting string.
+ *
+ * Returns: the newly allocated name.
+ */
+gchar*
+modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name);
+
 G_END_DECLS
 
 #endif /* __MODEST_ACCOUNT_MGR_H__ */