+
+/* 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;
+}
+
+void
+modest_account_mgr_set_leave_on_server (ModestAccountMgr *self,
+ const gchar *account_name,
+ gboolean leave_on_server)
+{
+ modest_account_mgr_set_bool (self,
+ account_name,
+ MODEST_ACCOUNT_LEAVE_ON_SERVER,
+ leave_on_server,
+ FALSE);
+}
+
+gboolean
+modest_account_mgr_get_leave_on_server (ModestAccountMgr *self,
+ const gchar* account_name)
+{
+ return modest_account_mgr_get_bool (self,
+ account_name,
+ MODEST_ACCOUNT_LEAVE_ON_SERVER,
+ FALSE);
+}
+
+gint
+modest_account_mgr_get_last_updated (ModestAccountMgr *self,
+ const gchar* account_name)
+{
+ return modest_account_mgr_get_int (modest_runtime_get_account_mgr (),
+ account_name,
+ MODEST_ACCOUNT_LAST_UPDATED,
+ TRUE);
+}
+
+void
+modest_account_mgr_set_last_updated (ModestAccountMgr *self,
+ const gchar* account_name,
+ gint time)
+{
+ modest_account_mgr_set_int (self,
+ account_name,
+ MODEST_ACCOUNT_LAST_UPDATED,
+ time,
+ TRUE);
+
+ /* TODO: notify about changes */
+}
+
+gint
+modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self,
+ const gchar* account_name)
+{
+ return modest_account_mgr_get_int (self,
+ account_name,
+ MODEST_ACCOUNT_LIMIT_RETRIEVE,
+ FALSE);
+}
+
+void
+modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self,
+ const gchar* account_name,
+ gint limit_retrieve)
+{
+ modest_account_mgr_set_int (self,
+ account_name,
+ MODEST_ACCOUNT_LIMIT_RETRIEVE,
+ limit_retrieve,
+ FALSE /* not server account */);
+}
+
+gint
+modest_account_mgr_get_server_account_port (ModestAccountMgr *self,
+ const gchar* account_name)
+{
+ return modest_account_mgr_get_int (self,
+ account_name,
+ MODEST_ACCOUNT_PORT,
+ TRUE);
+}
+
+void
+modest_account_mgr_set_server_account_port (ModestAccountMgr *self,
+ const gchar *account_name,
+ gint port_num)
+{
+ modest_account_mgr_set_int (self,
+ account_name,
+ MODEST_ACCOUNT_PORT,
+ port_num, TRUE /* server account */);
+}
+
+gchar*
+modest_account_mgr_get_server_account_name (ModestAccountMgr *self,
+ const gchar *account_name,
+ TnyAccountType account_type)
+{
+ return modest_account_mgr_get_string (self,
+ account_name,
+ (account_type == TNY_ACCOUNT_TYPE_STORE) ?
+ MODEST_ACCOUNT_STORE_ACCOUNT :
+ MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
+ FALSE);
+}
+
+gchar*
+modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
+ const gchar *account_name)
+{
+ return modest_account_mgr_get_string (self,
+ account_name,
+ MODEST_ACCOUNT_RETRIEVE,
+ FALSE /* not server account */);
+}
+
+void
+modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
+ const gchar *account_name,
+ const gchar *retrieve_type)
+{
+ modest_account_mgr_set_string (self,
+ account_name,
+ MODEST_ACCOUNT_RETRIEVE,
+ retrieve_type,
+ FALSE /* not server account */);
+}
+
+
+void
+modest_account_mgr_set_user_fullname (ModestAccountMgr *self,
+ const gchar *account_name,
+ const gchar *fullname)
+{
+ modest_account_mgr_set_string (self,
+ account_name,
+ MODEST_ACCOUNT_FULLNAME,
+ fullname,
+ FALSE /* not server account */);
+}
+
+void
+modest_account_mgr_set_user_email (ModestAccountMgr *self,
+ const gchar *account_name,
+ const gchar *email)
+{
+ modest_account_mgr_set_string (self,
+ account_name,
+ MODEST_ACCOUNT_EMAIL,
+ email,
+ FALSE /* not server account */);
+}