* Fix some detected memory leaks
[modest] / src / modest-account-mgr.c
index e6859df..ba553d8 100644 (file)
@@ -222,7 +222,8 @@ gboolean
 modest_account_mgr_add_account (ModestAccountMgr *self,
                                const gchar *name,
                                const gchar *store_account,
-                               const gchar *transport_account)
+                               const gchar *transport_account,
+                               gboolean enabled)
 {
        ModestAccountMgrPrivate *priv;
        gchar *key;
@@ -289,7 +290,7 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
                        return FALSE;
                }
        }
-       modest_account_mgr_set_enabled (self, name, TRUE);
+       modest_account_mgr_set_enabled (self, name, enabled);
 
        /* if no default account has been defined yet, do so now */
        default_account = modest_account_mgr_get_default_account (self);
@@ -407,19 +408,24 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
                   supported */
                key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_OPTIONS, TRUE);
                /* Enable subscriptions and check the mails in all folders */
-               option_list = g_slist_append (option_list, "use_lsub");
-               option_list = g_slist_append (option_list, "check_all");
+               option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_USE_LSUB);
+               option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_CHECK_ALL);
+
+               /* TODO: Remove this hack. These are hard-coded camel options to make the connection work.
+                * The regular connection options (set later here) should be interpreted instead 
+                * because in future these camel options will not be in gconf. murrayc.
+                */
                /* Security options */
                switch (security) {
                case MODEST_PROTOCOL_SECURITY_NONE:
-                       option_list = g_slist_append (option_list, "use_ssl=never");
+                       option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_NEVER);
                        break;
                case MODEST_PROTOCOL_SECURITY_SSL:
                case MODEST_PROTOCOL_SECURITY_TLS:
-                       option_list = g_slist_append (option_list, "use_ssl=always");
+                       option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_ALWAYS);
                        break;
                case MODEST_PROTOCOL_SECURITY_TLS_OP:
-                       option_list = g_slist_append (option_list, "use_ssl=when-possible");
+                       option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE);
                        break;
                default:
                        g_warning ("Invalid security option");
@@ -433,6 +439,10 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
                }
                g_slist_free (option_list);
                g_free (key);
+               
+               
+               /* Add the security settings: */
+               modest_server_account_set_security (self, name, security);
        }
 
 cleanup:
@@ -504,6 +514,31 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
                return FALSE;
        }
 
+       /* in case we're not deleting an account, also delete the dependent store and transport account */
+       if (!server_account) {
+               gchar *server_account_name;
+               
+               server_account_name = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_STORE_ACCOUNT,
+                                                                   FALSE);
+               if (server_account_name) {
+                       if (!modest_account_mgr_remove_account (self, server_account_name, TRUE))
+                               g_printerr ("modest: failed to remove store account '%s' (%s)\n",
+                                           server_account_name, name);
+                       g_free (server_account_name);
+               } else
+                       g_printerr ("modest: could not find the store account for %s\n", name);
+               
+               server_account_name = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
+                                                                   FALSE);
+               if (server_account_name) {
+                       if (!modest_account_mgr_remove_account (self, server_account_name, TRUE))
+                               g_printerr ("modest: failed to remove transport account '%s' (%s)\n",
+                                           server_account_name, name);
+                       g_free (server_account_name);
+               } else
+                       g_printerr ("modest: could not find the transport account for %s\n", name);
+       }                       
+                       
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
        key = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
        
@@ -514,6 +549,14 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
                g_printerr ("modest: error removing key: %s\n", err->message);
                g_error_free (err);
        }
+
+       /* If this was the default, then remove that setting: */
+       if (!server_account) {
+               gchar *default_account_name = modest_account_mgr_get_default_account (self);
+               if (default_account_name && (strcmp (default_account_name, name) == 0))
+                       modest_account_mgr_unset_default_account (self);
+               g_free (default_account_name);
+       }
        
        return retval;
 }
@@ -598,7 +641,7 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
 
 
 GSList*
-modest_account_mgr_account_names (ModestAccountMgr * self)
+modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
 {
        GSList *accounts;
        ModestAccountMgrPrivate *priv;
@@ -620,7 +663,32 @@ modest_account_mgr_account_names (ModestAccountMgr * self)
        }
        
        strip_prefix_from_elements (accounts, prefix_len);
-       return accounts;
+               
+       GSList *result = NULL;
+       
+       /* Filter-out the disabled accounts if requested: */
+       if (only_enabled) {
+               GSList *iter = accounts;
+               while (iter) {
+                       if (!(iter->data))
+                               continue;
+                               
+                       const gchar* account_name = (const gchar*)iter->data;
+                       if (account_name && modest_account_mgr_get_enabled (self, account_name))
+                               result = g_slist_append (result, g_strdup (account_name));
+                               
+                       iter = g_slist_next (iter);     
+               }
+               
+               /* TODO: Free the strings too? */
+               g_slist_free (accounts);
+               accounts = NULL;
+       }
+       else
+               result = accounts;
+       
+
+       return result;
 }
 
 
@@ -910,6 +978,41 @@ modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
        return retval;
 }
 
+gboolean       modest_account_mgr_account_with_display_name_exists       (ModestAccountMgr *self,
+                                                          const gchar *display_name)
+{
+       GSList *account_names = NULL;
+       GSList *cursor = NULL;
+       
+       cursor = account_names = modest_account_mgr_account_names (self, 
+               TRUE /* enabled accounts, because disabled accounts are not user visible. */);
+
+       gboolean found = FALSE;
+       
+       /* Look at each non-server account to check their display names; */
+       while (cursor) {
+               const gchar * account_name = (gchar*)cursor->data;
+               
+               ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
+               if (!account_data) {
+                       g_printerr ("modest: failed to get account data for %s\n", account_name);
+                       continue;
+               }
+
+               if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
+                       found = TRUE;
+                       break;
+               }
+
+               modest_account_mgr_free_account_data (self, account_data);
+               cursor = cursor->next;
+       }
+       g_slist_free (account_names);
+       
+       return found;
+}
+
+
 
 
 gboolean