* Some more changes
[modest] / src / modest-tny-account-store.c
index a801b28..c416442 100644 (file)
@@ -87,13 +87,21 @@ static void    get_server_accounts                    (TnyAccountStore *self,
                                                       TnyList *list, 
                                                       TnyAccountType type);
 
+static void    on_account_inserted                    (ModestAccountMgr *acc_mgr, 
+                                                      const gchar *account,
+                                                      gpointer user_data);
+
+static void    on_account_removed                     (ModestAccountMgr *acc_mgr, 
+                                                      const gchar *account,
+                                                      gpointer user_data);
+
+
 /* list my signals */
 enum {
        ACCOUNT_CHANGED_SIGNAL,
        ACCOUNT_INSERTED_SIGNAL,
        ACCOUNT_REMOVED_SIGNAL,
 
-       ACCOUNT_UPDATE_SIGNAL,
        PASSWORD_REQUESTED_SIGNAL,
        LAST_SIGNAL
 };
@@ -106,6 +114,10 @@ struct _ModestTnyAccountStorePrivate {
        ModestAccountMgr   *account_mgr;
        TnySessionCamel    *session;
        TnyDevice          *device;
+
+       gulong acc_inserted_handler;
+       gulong acc_changed_handler;
+       gulong acc_removed_handler;
        
        /* We cache the lists of accounts here.
         * They are created in our get_accounts_func() implementation. */
@@ -207,15 +219,6 @@ modest_tny_account_store_base_init (gpointer g_class)
 /*                                   g_cclosure_marshal_VOID__VOID,  */
 /*                                   G_TYPE_NONE, 0); */
 
-               signals[ACCOUNT_UPDATE_SIGNAL] =
-                       g_signal_new ("account_update",
-                                     MODEST_TYPE_TNY_ACCOUNT_STORE,
-                                     G_SIGNAL_RUN_FIRST,
-                                     G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
-                                     NULL, NULL,
-                                     g_cclosure_marshal_VOID__STRING,
-                                     G_TYPE_NONE, 1, G_TYPE_STRING);
-               
                signals[PASSWORD_REQUESTED_SIGNAL] =
                        g_signal_new ("password_requested",
                                      MODEST_TYPE_TNY_ACCOUNT_STORE,
@@ -343,80 +346,128 @@ recreate_all_accounts (ModestTnyAccountStore *self)
 
 static void
 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor, 
-       GnomeVFSVolume *volume, gpointer user_data)
+                     GnomeVFSVolume *volume, 
+                     gpointer user_data)
 {
-       ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
+/*     ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data); */
        
        /* Check whether this was the external MMC1 card: */
        gchar *uri = gnome_vfs_volume_get_activation_uri (volume);      
        if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
                printf ("DEBUG: %s: MMC1 card mounted.\n", __FUNCTION__);
                
-               /* TODO: Just add an account and emit (and respond to) 
-                * TnyAccountStore::accountinserted signal?
-                */
-               recreate_all_accounts (self);
-               
-               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
-                              NULL);
+/*             /\* TODO: Just add an account and emit (and respond to)  */
+/*              * TnyAccountStore::accountinserted signal? */
+/*              *\/ */
+/*             recreate_all_accounts (self); */
+
+               /* TODO */
+/*             g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0, */
+/*                            NULL); */
+/*             g_signal_emit (G_OBJECT (self),  */
+/*                            signals [ACCOUNT_INSERTED_SIGNAL],  */
+/*                            0, mmc_account); */
        }
-       
        g_free (uri);
 }
 
 static void
 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor, 
-       GnomeVFSVolume *volume, gpointer user_data)
+                       GnomeVFSVolume *volume, 
+                       gpointer user_data)
 {
-       ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
+/*     ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data); */
        
        /* Check whether this was the external MMC1 card: */
        gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
        if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
                printf ("DEBUG: %s: MMC1 card unmounted.\n", __FUNCTION__);
                
-               /* TODO: Just add an account and emit (and respond to) 
-                * TnyAccountStore::accountinserted signal?
-                */
-               recreate_all_accounts (self);
+/*             /\* TODO: Just add an account and emit (and respond to)  */
+/*              * TnyAccountStore::accountinserted signal? */
+/*              *\/ */
+/*             recreate_all_accounts (self); */
                
-               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
-                              NULL);
+/*             g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0, */
+/*                            NULL); */
+
+               /* TODO */
+/*             g_signal_emit (G_OBJECT (self),  */
+/*                            signals [ACCOUNT_REMOVED_SIGNAL],  */
+/*                            0, mmc_account); */
        }
-       
        g_free (uri);
 }
 
 static void
-on_account_removed (ModestAccountMgr *acc_mgr, 
-                   const gchar *account,
-                   gpointer user_data)
+on_account_inserted (ModestAccountMgr *acc_mgr, 
+                    const gchar *account,
+                    gpointer user_data)
 {
+       ModestTnyAccountStorePrivate *priv = NULL;
        TnyAccount *store_account = NULL, *transport_account = NULL;
-       ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
+       ModestTnyAccountStore *self = NULL;
        
+       self = MODEST_TNY_ACCOUNT_STORE(user_data);
+       priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
        /* Get the server and the transport account */
        store_account = 
                modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_STORE);
        transport_account = 
                modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT);
 
-       /* Clear the cache */
-       tny_store_account_delete_cache (TNY_STORE_ACCOUNT (store_account));
+       /* Add to the cached accounts */
+       tny_list_prepend (priv->store_accounts, G_OBJECT (store_account));
+       tny_list_prepend (priv->transport_accounts, G_OBJECT (transport_account));
 
        /* Notify the observers */
-       g_signal_emit (G_OBJECT (self),
-                      signals [ACCOUNT_REMOVED_SIGNAL],
-                      0, store_account);
-       g_signal_emit (G_OBJECT (self),
-                      signals [ACCOUNT_REMOVED_SIGNAL],
-                      0, transport_account);
+       g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, store_account);
+       g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, transport_account);
 
        /* Frees */
        g_object_unref (store_account);
        g_object_unref (transport_account);
 }
 
+static void
+on_account_removed (ModestAccountMgr *acc_mgr, 
+                   const gchar *account,
+                   gpointer user_data)
+{
+       TnyAccount *store_account = NULL, *transport_account = NULL;
+       ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
+       
+       /* Get the server and the transport account */
+       store_account = 
+               modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_STORE);
+       transport_account = 
+               modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT);
+
+       /* If there was any problem creating the account, for example,
+          with the configuration system this could not exist */
+       if (store_account) {
+               /* Clear the cache */
+               tny_store_account_delete_cache (TNY_STORE_ACCOUNT (store_account));
+
+               /* Notify the observers */
+               g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, store_account);
+               g_object_unref (store_account);
+       } else {
+               g_warning ("There is no store account for account %s\n", account);
+       }
+
+       /* If there was any problem creating the account, for example,
+          with the configuration system this could not exist */
+       if (transport_account) {
+               /* Notify the observers */
+               g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, transport_account);
+               g_object_unref (transport_account);
+       } else {
+               g_warning ("There is no transport account for account %s\n", account);
+       }
+}
+
 /**
  * modest_tny_account_store_forget_password_in_memory
  * @self: a TnyAccountStore instance
@@ -437,9 +488,11 @@ modest_tny_account_store_forget_password_in_memory (ModestTnyAccountStore *self,
 }
 
 static void
-on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
-                   const GSList *keys, gboolean server_account, gpointer user_data)
-
+on_account_changed (ModestAccountMgr *acc_mgr, 
+                   const gchar *account,
+                   const GSList *keys, 
+                   gboolean server_account, 
+                   gpointer user_data)
 {
        ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
        
@@ -476,8 +529,8 @@ on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
        }
        #endif
 
-       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
-                      account);
+/*     g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0, */
+/*                    account); */
 }
 
 
@@ -857,6 +910,20 @@ modest_tny_account_store_finalize (GObject *obj)
        destroy_password_hashtable (self);
 
        if (priv->account_mgr) {
+               /* Disconnect signals */
+               if (g_signal_handler_is_connected (priv->account_mgr, 
+                                                  priv->acc_inserted_handler))
+                       g_signal_handler_disconnect (priv->account_mgr, 
+                                                    priv->acc_inserted_handler);
+               if (g_signal_handler_is_connected (priv->account_mgr, 
+                                                  priv->acc_changed_handler))
+                       g_signal_handler_disconnect (priv->account_mgr, 
+                                                    priv->acc_changed_handler);
+               if (g_signal_handler_is_connected (priv->account_mgr, 
+                                                  priv->acc_removed_handler))
+                       g_signal_handler_disconnect (priv->account_mgr, 
+                                                    priv->acc_removed_handler);
+
                g_object_unref (G_OBJECT(priv->account_mgr));
                priv->account_mgr = NULL;
        }
@@ -914,10 +981,12 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device)
        tny_session_camel_set_ui_locker (priv->session,  tny_gtk_lockable_new ());
                
        /* Connect signals */
-       g_signal_connect (G_OBJECT(account_mgr), "account_changed",
-                         G_CALLBACK (on_account_changed), obj);
-       g_signal_connect (G_OBJECT(account_mgr), "account_removed",
-                         G_CALLBACK (on_account_removed), obj);
+       priv->acc_inserted_handler = g_signal_connect (G_OBJECT(account_mgr), "account_inserted",
+                                                     G_CALLBACK (on_account_inserted), obj);
+       priv->acc_changed_handler = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
+                                                     G_CALLBACK (on_account_changed), obj);
+       priv->acc_removed_handler = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
+                                                     G_CALLBACK (on_account_removed), obj);
 
        return MODEST_TNY_ACCOUNT_STORE(obj);
 }
@@ -983,7 +1052,6 @@ get_server_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
 
        g_return_if_fail (type == TNY_ACCOUNT_TYPE_STORE || 
                          type == TNY_ACCOUNT_TYPE_TRANSPORT);
-       g_return_if_fail (list);
        g_return_if_fail (self);
 
        priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
@@ -1030,7 +1098,8 @@ get_server_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
                                                   (gpointer)self);
 
                                /* Append to the list and cache it */
-                               tny_list_append (list, G_OBJECT (tny_account));
+                               if (list)
+                                       tny_list_append (list, G_OBJECT (tny_account));
                                tny_list_append (accounts, G_OBJECT (tny_account));
                        } else
                                g_printerr ("modest: failed to create account for %s\n",
@@ -1069,7 +1138,8 @@ get_server_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
                                        priv->session, MODEST_MCC1_VOLUMEPATH);
 
                        /* Append to the list and cache it */
-                       tny_list_prepend (list, G_OBJECT(tny_account));
+                       if (list)
+                               tny_list_prepend (list, G_OBJECT(tny_account));
                        tny_list_append (accounts, G_OBJECT (tny_account));
                }
        }
@@ -1107,7 +1177,8 @@ get_server_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
                                                                           (gpointer)self);
                                                        
                                                        /* Append to the list and cache it */
-                                                       tny_list_prepend (list, G_OBJECT(tny_account));
+                                                       if (list)
+                                                               tny_list_prepend (list, G_OBJECT(tny_account));
                                                        tny_list_append (accounts, G_OBJECT (tny_account));
                                                } else
                                                        g_printerr ("modest: failed to create smtp-specific account for %s\n",
@@ -1143,8 +1214,9 @@ get_server_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
                /* Add them to the TnyList: */
                if (priv->store_accounts_outboxes) {
                        /* Add accounts to the lists */
-                       tny_list_foreach (priv->store_accounts_outboxes, 
-                                         foreach_account_append_to_list, list);
+                       if (list)
+                               tny_list_foreach (priv->store_accounts_outboxes, 
+                                                 foreach_account_append_to_list, list);
                        tny_list_foreach (priv->store_accounts_outboxes, 
                                          foreach_account_append_to_list, accounts);
                }
@@ -1160,7 +1232,8 @@ get_server_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
                }
                
                /* Append to the list and cache it */
-               tny_list_append (list, G_OBJECT(tny_account));
+               if (list)
+                       tny_list_append (list, G_OBJECT(tny_account));
                tny_list_append (accounts, G_OBJECT(tny_account));
        }
                
@@ -1568,10 +1641,8 @@ modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
 
        /* ******************** WARNING ******************** */
 /*     /\* Make sure that the tny accounts have been created: */
-/*      * TODO: We might want to do this in several places. */
-/*      *\/ */
-/*     if (!priv->store_accounts || !priv->transport_accounts) */
-/*             recreate_all_accounts (self); */
+       if (!priv->store_accounts || !priv->transport_accounts)
+               recreate_all_accounts (self);
 
        account_list = (type == TNY_ACCOUNT_TYPE_STORE) ? 
                priv->store_accounts :