2007-07-31 Philip Van Hoof <pvanhoof@gnome.org>
[modest] / src / modest-account-mgr.c
index 06b8558..624bff0 100644 (file)
 static void modest_account_mgr_class_init (ModestAccountMgrClass * klass);
 static void modest_account_mgr_init       (ModestAccountMgr * obj);
 static void modest_account_mgr_finalize   (GObject * obj);
+static void modest_account_mgr_base_init  (gpointer g_class);
 
 /* list my signals */
 enum {
+       ACCOUNT_INSERTED_SIGNAL,
        ACCOUNT_CHANGED_SIGNAL,
        ACCOUNT_REMOVED_SIGNAL,
        ACCOUNT_BUSY_SIGNAL,
        LAST_SIGNAL
 };
 
-
 /* globals */
 static GObjectClass *parent_class = NULL;
 static guint signals[LAST_SIGNAL] = {0};
 
-/* We signal key changes in batches, every X seconds: */
-static gboolean
-on_timeout_notify_changes (gpointer data)
-{
-       ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data);
-       ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+/* /\* We signal key changes in batches, every X seconds: *\/ */
+/* static gboolean */
+/* on_timeout_notify_changes (gpointer data) */
+/* { */
+/*     ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data); */
+/*     ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self); */
                
-       /* TODO: Also store the account names, and notify one list for each account,
-        * if anything uses the account names. */
+/*     /\* TODO: Also store the account names, and notify one list for each account, */
+/*      * if anything uses the account names. *\/ */
        
-       if (priv->changed_conf_keys) {
-               gchar *default_account = 
-                               modest_account_mgr_get_default_account (self);
+/*     if (priv->changed_conf_keys) { */
+/*             gchar *default_account =  */
+/*                             modest_account_mgr_get_default_account (self); */
                
-               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
-                                default_account, priv->changed_conf_keys, FALSE);
+/*             /\* 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); */
                        
-               g_free (default_account);
+/*             g_free (default_account); */
                
-               g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL);
-               g_slist_free (priv->changed_conf_keys);
-               priv->changed_conf_keys = NULL;
-       }
+/*             g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL); */
+/*             g_slist_free (priv->changed_conf_keys); */
+/*             priv->changed_conf_keys = NULL; */
+/*     } */
        
-       return TRUE; /* Call this again later. */
-}
-
-static void
-on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event, gpointer user_data)
-{
-       ModestAccountMgr *self = MODEST_ACCOUNT_MGR (user_data);
-       ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       gboolean is_account_key;
-       gboolean is_server_account;
-       gchar* account = NULL;
-
-       /* there is only one not-really-account key which will still emit
-        * a signal: a change in MODEST_CONF_DEFAULT_ACCOUNT */
-       if (key && strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
-               /* Get the default account instead. */
+/*     return TRUE; /\* Call this again later. *\/ */
+/* } */
+
+/* static void */
+/* on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event, gpointer user_data) */
+/* { */
+/*     ModestAccountMgr *self = MODEST_ACCOUNT_MGR (user_data); */
+/*     ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self); */
+/*     gboolean is_account_key; */
+/*     gboolean is_server_account; */
+/*     gchar* account = NULL; */
+
+/*     /\* there is only one not-really-account key which will still emit */
+/*      * a signal: a change in MODEST_CONF_DEFAULT_ACCOUNT *\/ */
+/*     if (key && strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) { */
+/*             /\* Get the default account instead. *\/ */
                
-               /* Store the key for later notification in our timeout callback.
-                * Notifying for every key change would cause unnecessary work: */
-               priv->changed_conf_keys = g_slist_append (priv->changed_conf_keys,
-                       (gpointer) g_strdup (key));
-       }
+/*             /\* Store the key for later notification in our timeout callback. */
+/*              * Notifying for every key change would cause unnecessary work: *\/ */
+/*             priv->changed_conf_keys = g_slist_append (priv->changed_conf_keys, */
+/*                     (gpointer) g_strdup (key)); */
+/*     } */
        
-       is_account_key = FALSE;
-       is_server_account = FALSE;
-       account = _modest_account_mgr_account_from_key (key, &is_account_key, 
-                                                       &is_server_account);
-
-       /* if this is not an account-related key change, ignore */
-       if (!account)
-               return;
-
-       /* account was removed. Do not emit an account removed signal
-          because it was already being done in the remove_account
-          method. Do not notify also the removal of the server
-          account keys for the same reason */
-       if ((is_account_key || is_server_account) && 
-           event == MODEST_CONF_EVENT_KEY_UNSET) {
-               g_free (account);
-               return;
-       }
-
-       /* is this account enabled? */
-       gboolean enabled = FALSE;
-       if (is_server_account)
-               enabled = TRUE;
-       else
-               enabled = modest_account_mgr_get_enabled (self, account);
-
-       /* Notify is server account was changed, default account was changed
-        * or when enabled/disabled changes:
-        */
-       if (enabled ||
-           g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED) ||
-           strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
-               /* Store the key for later notification in our timeout callback.
-                * Notifying for every key change would cause unnecessary work: */
-               priv->changed_conf_keys = g_slist_append (NULL,
-                       (gpointer) g_strdup (key));
-       }
-       g_free (account);
-}
+/*     is_account_key = FALSE; */
+/*     is_server_account = FALSE; */
+/*     account = _modest_account_mgr_account_from_key (key, &is_account_key,  */
+/*                                                     &is_server_account); */
+
+/*     /\* if this is not an account-related key change, ignore *\/ */
+/*     if (!account) */
+/*             return; */
+
+/*     /\* account was removed. Do not emit an account removed signal */
+/*        because it was already being done in the remove_account */
+/*        method. Do not notify also the removal of the server */
+/*        account keys for the same reason *\/ */
+/*     if ((is_account_key || is_server_account) &&  */
+/*         event == MODEST_CONF_EVENT_KEY_UNSET) { */
+/*             g_free (account); */
+/*             return; */
+/*     } */
+
+/*     /\* is this account enabled? *\/ */
+/*     gboolean enabled = FALSE; */
+/*     if (is_server_account) */
+/*             enabled = TRUE; */
+/*     else */
+/*             enabled = modest_account_mgr_get_enabled (self, account); */
+
+/*     /\* Notify is server account was changed, default account was changed */
+/*      * or when enabled/disabled changes: */
+/*      *\/ */
+/*     if (enabled || */
+/*         g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED) || */
+/*         strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) { */
+/*             /\* Store the key for later notification in our timeout callback. */
+/*              * Notifying for every key change would cause unnecessary work: *\/ */
+/*             priv->changed_conf_keys = g_slist_append (NULL, */
+/*                     (gpointer) g_strdup (key)); */
+/*     } */
+/*     g_free (account); */
+/* } */
 
 
 GType
@@ -147,7 +150,7 @@ modest_account_mgr_get_type (void)
        if (!my_type) {
                static const GTypeInfo my_info = {
                        sizeof (ModestAccountMgrClass),
-                       NULL,   /* base init */
+                       modest_account_mgr_base_init,   /* base init */
                        NULL,   /* base finalize */
                        (GClassInitFunc) modest_account_mgr_class_init,
                        NULL,   /* class finalize */
@@ -165,6 +168,53 @@ modest_account_mgr_get_type (void)
        return my_type;
 }
 
+static void 
+modest_account_mgr_base_init (gpointer g_class)
+{
+       static gboolean modest_account_mgr_initialized = FALSE;
+
+       if (!modest_account_mgr_initialized) {
+               /* signal definitions */
+               signals[ACCOUNT_INSERTED_SIGNAL] =
+                       g_signal_new ("account_inserted",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass,account_inserted),
+                                     NULL, NULL,
+                                     g_cclosure_marshal_VOID__STRING,
+                                     G_TYPE_NONE, 1, G_TYPE_STRING);
+
+               signals[ACCOUNT_REMOVED_SIGNAL] =
+                       g_signal_new ("account_removed",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass,account_removed),
+                                     NULL, NULL,
+                                     g_cclosure_marshal_VOID__STRING,
+                                     G_TYPE_NONE, 1, G_TYPE_STRING);
+
+               signals[ACCOUNT_CHANGED_SIGNAL] =
+                       g_signal_new ("account_changed",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass,account_changed),
+                                     NULL, NULL,
+                                     modest_marshal_VOID__STRING_POINTER_BOOLEAN,
+                                     G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN);
+
+               signals[ACCOUNT_BUSY_SIGNAL] =
+                       g_signal_new ("account_busy_changed",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass,account_busy_changed),
+                                     NULL, NULL,
+                                     modest_marshal_VOID__STRING_BOOLEAN,
+                                     G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
+
+               modest_account_mgr_initialized = TRUE;
+       }
+}
+
 static void
 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
 {
@@ -176,32 +226,6 @@ modest_account_mgr_class_init (ModestAccountMgrClass * klass)
 
        g_type_class_add_private (gobject_class,
                                  sizeof (ModestAccountMgrPrivate));
-
-       /* signal definitions */
-       signals[ACCOUNT_REMOVED_SIGNAL] =
-               g_signal_new ("account_removed",
-                             G_TYPE_FROM_CLASS (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);
-       signals[ACCOUNT_CHANGED_SIGNAL] =
-               g_signal_new ("account_changed",
-                              G_TYPE_FROM_CLASS (klass),
-                             G_SIGNAL_RUN_FIRST,
-                             G_STRUCT_OFFSET(ModestAccountMgrClass,account_changed),
-                             NULL, NULL,
-                             modest_marshal_VOID__STRING_POINTER_BOOLEAN,
-                             G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN);
-       signals[ACCOUNT_BUSY_SIGNAL] =
-               g_signal_new ("account_busy_changed",
-                             G_TYPE_FROM_CLASS (klass),
-                             G_SIGNAL_RUN_FIRST,
-                             G_STRUCT_OFFSET(ModestAccountMgrClass,account_busy_changed),
-                             NULL, NULL,
-                             modest_marshal_VOID__STRING_BOOLEAN,
-                             G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
 }
 
 
@@ -213,7 +237,8 @@ modest_account_mgr_init (ModestAccountMgr * obj)
 
        priv->modest_conf = NULL;
        priv->busy_accounts = NULL;
-       priv->timeout = g_timeout_add (1000 /* milliseconds */, on_timeout_notify_changes, obj);
+
+       priv->notification_id_accounts = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
 }
 
 static void
@@ -222,6 +247,12 @@ modest_account_mgr_finalize (GObject * obj)
        ModestAccountMgrPrivate *priv = 
                MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
 
+       if (priv->notification_id_accounts) {
+               /* TODO: forget dirs */
+
+               g_hash_table_destroy (priv->notification_id_accounts);
+       }
+
        if (priv->key_changed_handler_uid) {
                g_signal_handler_disconnect (priv->modest_conf, 
                                             priv->key_changed_handler_uid);
@@ -233,13 +264,13 @@ modest_account_mgr_finalize (GObject * obj)
                priv->modest_conf = NULL;
        }
        
-       if (priv->timeout)
-               g_source_remove (priv->timeout);
+/*     if (priv->timeout) */
+/*             g_source_remove (priv->timeout); */
                
-       if (priv->changed_conf_keys) {
-               g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL);
-               g_slist_free (priv->changed_conf_keys);
-       }
+/*     if (priv->changed_conf_keys) { */
+/*             g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL); */
+/*             g_slist_free (priv->changed_conf_keys); */
+/*     } */
 
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
@@ -259,10 +290,10 @@ modest_account_mgr_new (ModestConf *conf)
        g_object_ref (G_OBJECT(conf));
        priv->modest_conf = conf;
 
-       priv->key_changed_handler_uid =
-               g_signal_connect (G_OBJECT (conf), "key_changed",
-                                 G_CALLBACK (on_key_change),
-                                 obj);
+/*     priv->key_changed_handler_uid = */
+/*             g_signal_connect (G_OBJECT (conf), "key_changed", */
+/*                               G_CALLBACK (on_key_change), */
+/*                               obj); */
        
        return MODEST_ACCOUNT_MGR (obj);
 }
@@ -356,6 +387,9 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
 
        modest_account_mgr_set_enabled (self, name, enabled);
 
+       /* Notify the observers */
+       g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, name);
+
        /* if no default account has been defined yet, do so now */
        default_account = modest_account_mgr_get_default_account (self);
        if (!default_account)
@@ -529,76 +563,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 +704,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