+
+/* 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)
+{
+ g_return_val_if_fail (text, NULL);
+
+ /* 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_find_prev_char (text, 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);
+}
+
+static const gchar *
+get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
+{
+ switch(retrieve_type) {
+ case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
+ return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
+ break;
+ case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
+ return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
+ break;
+ case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
+ return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
+ break;
+ default:
+ return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
+ };
+}
+
+static ModestAccountRetrieveType
+get_retrieve_type (const gchar *name)
+{
+ if (!name || name[0] == 0)
+ return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
+ if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
+ return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
+ } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
+ return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
+ } else {
+ /* we fall back to headers only */
+ return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
+ }
+}
+
+ModestAccountRetrieveType
+modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
+ const gchar *account_name)
+{
+ gchar *string;
+ ModestAccountRetrieveType result;
+
+ string = modest_account_mgr_get_string (self,
+ account_name,
+ MODEST_ACCOUNT_RETRIEVE,
+ FALSE /* not server account */);
+ result = get_retrieve_type (string);
+ g_free (string);
+
+ return result;
+}
+
+void
+modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
+ const gchar *account_name,
+ ModestAccountRetrieveType retrieve_type)
+{
+ modest_account_mgr_set_string (self,
+ account_name,
+ MODEST_ACCOUNT_RETRIEVE,
+ get_retrieve_type_name (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 */);
+}
+
+gboolean
+modest_account_mgr_account_is_multimailbox (ModestAccountMgr *mgr,
+ const gchar *account_name,
+ ModestProtocol **mmb_protocol)
+{
+ gchar *transport_account;
+ gboolean result = FALSE;
+
+ if (mmb_protocol)
+ *mmb_protocol = NULL;
+
+ transport_account = modest_account_mgr_get_server_account_name (mgr,
+ account_name,
+ TNY_ACCOUNT_TYPE_TRANSPORT);
+ if (transport_account) {
+ gchar *proto;
+ ModestProtocolRegistry *registry;
+
+ registry = modest_runtime_get_protocol_registry ();
+
+ proto = modest_account_mgr_get_string (mgr, transport_account,
+ MODEST_ACCOUNT_PROTO, TRUE);
+ if (proto != NULL) {
+ ModestProtocol *protocol =
+ modest_protocol_registry_get_protocol_by_name (registry,
+ MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
+ proto);
+ if (protocol &&
+ modest_protocol_registry_protocol_type_has_tag
+ (registry,
+ modest_protocol_get_type_id (protocol),
+ MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS)) {
+ if (mmb_protocol)
+ *mmb_protocol = protocol;
+ result = TRUE;
+ }
+
+ }
+ }
+
+ return result;
+}
+
+gchar *
+modest_account_mgr_get_signature_from_recipient (ModestAccountMgr *mgr,
+ const gchar *current_recipient,
+ gboolean *has_signature)
+{
+ gchar *result = NULL;
+ gchar *mailbox = NULL;
+ gchar *account_name;
+ ModestProtocol *protocol = NULL;
+
+ *has_signature = FALSE;
+
+ account_name = modest_utils_get_account_name_from_recipient (current_recipient, &mailbox);
+ if (modest_account_mgr_account_is_multimailbox (mgr, account_name, &protocol)) {
+ if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
+ result = modest_account_protocol_get_signature (MODEST_ACCOUNT_PROTOCOL (protocol),
+ account_name, mailbox,
+ has_signature);
+ }
+ }
+
+ if (result == NULL) {
+ result = modest_account_mgr_get_signature (mgr,
+ account_name, has_signature);
+ }
+ return result;
+}
+