Fixes several potential infinite loops with corrupted account data
[modest] / src / modest-account-mgr.c
index 65536cb..c3e12dc 100644 (file)
@@ -30,6 +30,7 @@
 #include <string.h>
 #include <modest-marshal.h>
 #include <modest-runtime.h>
+#include <modest-defs.h>
 #include <modest-account-mgr.h>
 #include <modest-account-mgr-priv.h>
 #include <modest-account-mgr-helpers.h>
@@ -218,9 +219,8 @@ modest_account_mgr_finalize (GObject * obj)
                MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
 
        if (priv->notification_id_accounts) {
-               /* TODO: forget dirs */
-
                g_hash_table_destroy (priv->notification_id_accounts);
+               priv->notification_id_accounts = NULL;
        }
 
        if (priv->modest_conf) {
@@ -242,6 +242,12 @@ modest_account_mgr_finalize (GObject * obj)
                priv->account_key_hash = NULL;
        }
 
+       if (priv->busy_accounts) {
+               g_slist_foreach (priv->busy_accounts, (GFunc) g_free, NULL);
+               g_slist_free (priv->busy_accounts);
+               priv->busy_accounts = NULL;
+       }
+
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
@@ -292,7 +298,7 @@ modest_account_mgr_add_account_from_settings (ModestAccountMgr *self,
         * but let's check again just in case */
        if (!display_name || 
            modest_account_mgr_account_with_display_name_exists (self, display_name)) {
-               return FALSE;
+               display_name = _("mcen_ia_emailsetup_defaultname");
        }
 
        /* Increment the non-user visible name if necessary, 
@@ -472,19 +478,21 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
                                       guint portnumber,
                                       const gchar *username, 
                                       const gchar *password,
-                                      ModestTransportStoreProtocol proto,
-                                      ModestConnectionProtocol security,
-                                      ModestAuthProtocol auth)
+                                      ModestProtocolType proto,
+                                      ModestProtocolType security,
+                                      ModestProtocolType auth)
 {
        ModestAccountMgrPrivate *priv;
        const gchar *key;
        gboolean ok = TRUE;
        GError *err = NULL;
+       ModestProtocolRegistry *protocol_registry;
 
        g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        g_return_val_if_fail (name, FALSE);
        g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
-                             
+
+       protocol_registry = modest_runtime_get_protocol_registry ();
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
 
        /* hostname */
@@ -531,7 +539,7 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
        /* proto */
        key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
        ok = modest_conf_set_string (priv->modest_conf, key,
-                                    modest_protocol_info_get_transport_store_protocol_name(proto),
+                                    modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, proto)),
                                     &err);
        if (err) {
                g_printerr ("modest: failed to set %s: %s\n", key, err->message);
@@ -557,7 +565,7 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
        /* auth mechanism */
        key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
        ok = modest_conf_set_string (priv->modest_conf, key,
-                                    modest_protocol_info_get_auth_protocol_name (auth),
+                                    modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, auth)),
                                     &err);
        if (err) {
                g_printerr ("modest: failed to set %s: %s\n", key, err->message);
@@ -585,12 +593,13 @@ cleanup:
 gboolean
 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
                                           const gchar *name, 
-                                          ModestTransportStoreProtocol proto,
+                                          ModestProtocolType proto,
                                           const gchar *uri)
 {
        ModestAccountMgrPrivate *priv;
        const gchar *key;
        gboolean ok;
+       ModestProtocolRegistry *protocol_registry;
        
        g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        g_return_val_if_fail (name, FALSE);
@@ -598,11 +607,12 @@ modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
        g_return_val_if_fail (uri, FALSE);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       protocol_registry = modest_runtime_get_protocol_registry ();
        
        /* proto */
        key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
        ok = modest_conf_set_string (priv->modest_conf, key,
-                                    modest_protocol_info_get_transport_store_protocol_name(proto),
+                                    modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, proto)),
                                     NULL);
 
        if (!ok) {
@@ -758,9 +768,11 @@ modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled
        /* Unescape the keys to get the account names: */
        GSList *iter = accounts;
        while (iter) {
-               if (!(iter->data))
+               if (!(iter->data)) {
+                       iter = iter->next;
                        continue;
-                       
+               }
+
                const gchar* account_name_key = (const gchar*)iter->data;
                gchar* unescaped_name = account_name_key ? 
                        modest_conf_key_unescape (account_name_key) 
@@ -946,7 +958,7 @@ modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
                g_printerr ("modest: error getting list '%s': %s\n", keyname,
                            err->message);
                g_error_free (err);
-               retval = FALSE;
+               retval = NULL;
        }
        return retval;
 }
@@ -1122,6 +1134,7 @@ modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self,
                ModestAccountSettings *settings = modest_account_mgr_load_account_settings (self, account_name);
                if (!settings) {
                        g_printerr ("modest: failed to get account data for %s\n", account_name);
+                       cursor = cursor->next;
                        continue;
                }
 
@@ -1200,6 +1213,7 @@ modest_account_mgr_check_already_configured_account  (ModestAccountMgr *self,
                from_mgr_settings = modest_account_mgr_load_account_settings (self, account_name);
                if (!settings) {
                        g_printerr ("modest: failed to get account data for %s\n", account_name);
+                       cursor = cursor->next;
                        continue;
                }
 
@@ -1504,23 +1518,27 @@ void
 modest_account_mgr_notify_account_update (ModestAccountMgr* self, 
                                          const gchar *server_account_name)
 {
-       ModestTransportStoreProtocol proto;
+       ModestProtocolType proto;
        ModestAccountMgrPrivate* priv;
+       ModestProtocolRegistry *protocol_registry;
        gchar *proto_name = NULL;
 
        g_return_if_fail (self);
        g_return_if_fail (server_account_name);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       protocol_registry = modest_runtime_get_protocol_registry ();
        
        /* Get protocol */
        proto_name = modest_account_mgr_get_string (self, server_account_name, 
                                                    MODEST_ACCOUNT_PROTO, TRUE);
        if (!proto_name) {
-               g_free (proto_name);
                g_return_if_reached ();
+               return;
        }
-       proto = modest_protocol_info_get_transport_store_protocol (proto_name);
+       proto = modest_protocol_get_type_id (modest_protocol_registry_get_protocol_by_name (protocol_registry,
+                                                                                           MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
+                                                                                           proto_name));
        g_free (proto_name);
 
        /* there is some update in the account, so we can't
@@ -1533,7 +1551,7 @@ modest_account_mgr_notify_account_update (ModestAccountMgr* self,
        g_signal_emit (G_OBJECT(self), 
                       signals[ACCOUNT_CHANGED_SIGNAL], 0, 
                       server_account_name, 
-                      (modest_protocol_info_protocol_is_store (proto)) ? 
+                      (modest_protocol_registry_protocol_type_has_tag (protocol_registry, proto, MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS)) ? 
                       TNY_ACCOUNT_TYPE_STORE : 
                       TNY_ACCOUNT_TYPE_TRANSPORT);
 }
@@ -1577,7 +1595,6 @@ modest_account_mgr_get_default_account  (ModestAccountMgr *self)
                g_printerr ("modest: failed to get '%s': %s\n",
                            MODEST_CONF_DEFAULT_ACCOUNT, err->message);
                g_error_free (err);
-               g_free (account);
                return  NULL;
        }
        
@@ -1637,3 +1654,28 @@ modest_account_mgr_set_display_name (ModestAccountMgr *self,
        if (notify)
                g_signal_emit (self, signals[DISPLAY_NAME_CHANGED_SIGNAL], 0, account_name);
 }
+
+gboolean 
+modest_account_mgr_singleton_protocol_exists (ModestAccountMgr *mgr,
+                                             ModestProtocolType protocol_type)
+{
+       GSList *account_names, *node;
+       gboolean found = FALSE;
+
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR (mgr), FALSE);
+       account_names = modest_account_mgr_account_names (mgr, TRUE);
+
+       for (node = account_names; node != NULL; node = g_slist_next (node)) {
+               ModestProtocolType current_protocol;
+
+               current_protocol = modest_account_mgr_get_store_protocol (mgr, (gchar *) node->data);
+               if (current_protocol == protocol_type) {
+                       found = TRUE;
+                       break;
+               }
+       }
+
+       modest_account_mgr_free_account_names (account_names);
+
+       return found;
+}