2007-07-25 Murray Cumming <murrayc@murrayc.com>
[modest] / src / modest-account-mgr.c
index 06b8558..46ed9d4 100644 (file)
@@ -65,6 +65,8 @@ on_timeout_notify_changes (gpointer data)
                gchar *default_account = 
                                modest_account_mgr_get_default_account (self);
                
+               /* printf ("DEBUG: %s: priv->changed_conf_key length=%d\n", 
+                       __FUNCTION__, g_slist_length (priv->changed_conf_keys)); */
                g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
                                 default_account, priv->changed_conf_keys, FALSE);
                        
@@ -184,8 +186,8 @@ modest_account_mgr_class_init (ModestAccountMgrClass * klass)
                              G_SIGNAL_RUN_FIRST,
                              G_STRUCT_OFFSET(ModestAccountMgrClass,account_removed),
                              NULL, NULL,
-                             modest_marshal_VOID__STRING_BOOLEAN,
-                             G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
+                             g_cclosure_marshal_VOID__STRING,
+                             G_TYPE_NONE, 1, G_TYPE_STRING);
        signals[ACCOUNT_CHANGED_SIGNAL] =
                g_signal_new ("account_changed",
                               G_TYPE_FROM_CLASS (klass),
@@ -529,76 +531,82 @@ modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
        return TRUE;
 }
 
+/* 
+ * Utility function used by modest_account_mgr_remove_account
+ */
+static void
+real_remove_account (ModestConf *conf,
+                    const gchar *acc_name,
+                    gboolean server_account)
+{
+       GError *err = NULL;
+       gchar *key = NULL;
+
+       key = _modest_account_mgr_get_account_keyname (acc_name, NULL, server_account);
+       modest_conf_remove_key (conf, key, &err);
+       g_free (key);       
+
+       if (err) {
+               g_printerr ("modest: error removing key: %s\n", err->message);
+               g_error_free (err);
+       }
+}
+
 gboolean
 modest_account_mgr_remove_account (ModestAccountMgr * self,
-                                  const gchar* name,  gboolean server_account)
+                                  const gchar* name)
 {
        ModestAccountMgrPrivate *priv;
-       gchar *key;
-       gboolean retval;
-       GError *err = NULL;
+       gchar *default_account_name, *store_acc_name, *transport_acc_name;
+       gboolean default_account_deleted;
 
        g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        g_return_val_if_fail (name, FALSE);
 
-       if (!modest_account_mgr_account_exists (self, name, server_account)) {
+       if (!modest_account_mgr_account_exists (self, name, FALSE)) {
                g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
                return FALSE;
        }
 
-       if (!server_account) {
-               gchar *server_account_name;
-
-               /* in case we're deleting an account, also delete the dependent store and transport account */
-               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);
-       
-       retval = modest_conf_remove_key (priv->modest_conf, key, &err);
-       g_free (key);
-
-       if (err) {
-               g_printerr ("modest: error removing key: %s\n", err->message);
-               g_error_free (err);
-       }
+       default_account_deleted = FALSE;
 
        /* 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);
-               
-               /* pick another one as the new default account */
+       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);
+               default_account_deleted = TRUE;
+       }
+       g_free (default_account_name);
+
+       /* Delete transport and store accounts */
+       store_acc_name = modest_account_mgr_get_string (self, name, 
+                                                       MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
+       if (store_acc_name)
+               real_remove_account (priv->modest_conf, store_acc_name, TRUE);
+
+       transport_acc_name = modest_account_mgr_get_string (self, name, 
+                                                           MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE);
+       if (transport_acc_name)
+               real_remove_account (priv->modest_conf, transport_acc_name, TRUE);
+                       
+       /* Remove the modest account */
+       real_remove_account (priv->modest_conf, name, FALSE);
+
+       if (default_account_deleted) {  
+               /* pick another one as the new default account. We do
+                  this *after* deleting the keys, because otherwise a
+                  call to account_names will retrieve also the
+                  deleted account */
                modest_account_mgr_set_first_account_as_default (self);
-
-               /* Notify the observers. We do this *after* deleting
-                  the keys, because otherwise a call to account_names
-                  will retrieve also the deleted account */
-               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0,
-                              name, server_account);
        }
-       return retval;
+       
+       /* Notify the observers. We do this *after* deleting
+          the keys, because otherwise a call to account_names
+          will retrieve also the deleted account */
+       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, name);
+
+       return TRUE;
 }
 
 
@@ -664,6 +672,31 @@ modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled
                        }
                }
                
+               /* Ignore modest accounts whose server accounts don't exist: 
+                * (We could be getting this list while the account is being deleted, 
+                * while the child server accounts have already been deleted, but the 
+                * parent modest account already exists.
+                */
+               if (add) {
+                       gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
+                                                                           FALSE);
+                       if (server_account_name) {
+                               if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
+                                       add = FALSE;
+                               g_free (server_account_name);
+                       }
+               }
+               
+               if (add) {
+                       gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
+                                                                           FALSE);
+                       if (server_account_name) {
+                               if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
+                                       add = FALSE;
+                               g_free (server_account_name);
+                       }
+               }
+               
                if (add)        
                        result = g_slist_append (result, unescaped_name);
                else