* Needed initialization
[modest] / src / modest-account-mgr.c
index d37c498..9b622de 100644 (file)
@@ -56,6 +56,7 @@ enum {
        ACCOUNT_BUSY_SIGNAL,
        DEFAULT_ACCOUNT_CHANGED_SIGNAL,
        DISPLAY_NAME_CHANGED_SIGNAL,
+       ACCOUNT_UPDATED_SIGNAL,
        LAST_SIGNAL
 };
 
@@ -149,6 +150,16 @@ modest_account_mgr_base_init (gpointer g_class)
                                      NULL, NULL,
                                      g_cclosure_marshal_VOID__STRING,
                                      G_TYPE_NONE, 1, G_TYPE_STRING);
+               
+               signals[ACCOUNT_UPDATED_SIGNAL] =
+                       g_signal_new ("account_updated",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass, account_updated),
+                                     NULL, NULL,
+                                     g_cclosure_marshal_VOID__STRING,
+                                     G_TYPE_NONE, 1, G_TYPE_STRING);
+
 
                modest_account_mgr_initialized = TRUE;
        }
@@ -194,6 +205,10 @@ modest_account_mgr_init (ModestAccountMgr * obj)
                                                               g_str_equal,
                                                               g_free,
                                                               (GDestroyNotify)g_hash_table_destroy);
+
+       /* FALSE means: status is unknown */
+       priv->has_accounts = FALSE;
+       priv->has_enabled_accounts = FALSE;
 }
 
 static void
@@ -472,7 +487,7 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
        
        /* Add the security settings: */
        modest_account_mgr_set_server_account_security (self, name, security);
-       
+
 cleanup:
        if (!ok) {
                g_printerr ("modest: failed to add server account\n");
@@ -602,9 +617,13 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
        /* if this was the last account, stop any auto-updating */
        /* (re)set the automatic account update */
        GSList *acc_names = modest_account_mgr_account_names (self, TRUE);
-       if (!acc_names) 
+       if (!acc_names) {
                modest_platform_set_update_interval (0);
-       else
+               /* it was the last account, the has_account / has_enabled_account
+                * changes
+                */
+               priv->has_accounts = priv->has_enabled_accounts = FALSE; 
+       } else
                modest_account_mgr_free_account_names (acc_names);
        
        return TRUE;
@@ -886,7 +905,6 @@ modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
                            const gchar * key, int val, gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
-
        const gchar *keyname;
        gboolean retval;
        GError *err = NULL;
@@ -904,6 +922,13 @@ modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
                g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
                g_error_free (err);
                retval = FALSE;
+       } else {
+               /* check whether this field is one of those interesting for the 
+                * "account-updated" signal */
+               if (strcmp(key, MODEST_ACCOUNT_LAST_UPDATED) == 0) {
+                       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATED_SIGNAL], 
+                                       0, name);
+               }
        }
        return retval;
 }
@@ -1167,8 +1192,9 @@ _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar*
                retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
 
        /* Sanity check: */
-       if (!modest_conf_key_is_valid (retval)) {
-               g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
+       if (!retval || !modest_conf_key_is_valid (retval)) {
+               g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__,
+                          retval ? retval: "<empty>");
                g_free (retval);
                retval = NULL;
        }
@@ -1221,13 +1247,30 @@ _modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv,
 gboolean
 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
 {
-       /* Check that at least one account exists: */
-       GSList *account_names = modest_account_mgr_account_names (self,
-                                                                 enabled);
-       gboolean accounts_exist = account_names != NULL;
+       ModestAccountMgrPrivate* priv;
+       GSList *account_names;
+       gboolean accounts_exist;
+
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       
+       if (enabled && priv->has_enabled_accounts)
+               return TRUE;
+       else if (priv->has_accounts)
+               return TRUE;
+               
+       /* Check that at least one account exists: */
+       account_names = modest_account_mgr_account_names (self,enabled);
+       accounts_exist = account_names != NULL;
        modest_account_mgr_free_account_names (account_names);
        account_names = NULL;
+
+       /* cache it. */
+       if (enabled)
+               priv->has_enabled_accounts = accounts_exist;
+       else
+               priv->has_accounts = accounts_exist;
        
        return accounts_exist;
 }
@@ -1295,8 +1338,14 @@ modest_account_mgr_notify_account_update (ModestAccountMgr* self,
                                          const gchar *server_account_name)
 {
        ModestTransportStoreProtocol proto;
+       ModestAccountMgrPrivate* priv;
        gchar *proto_name = NULL;
 
+       g_return_if_fail (self);
+       g_return_if_fail (server_account_name);
+       
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       
        /* Get protocol */
        proto_name = modest_account_mgr_get_string (self, server_account_name, 
                                                    MODEST_ACCOUNT_PROTO, TRUE);
@@ -1307,6 +1356,12 @@ modest_account_mgr_notify_account_update (ModestAccountMgr* self,
        proto = modest_protocol_info_get_transport_store_protocol (proto_name);
        g_free (proto_name);
 
+       /* there is some update in the account, so we can't
+        * be sure about whether there are still enabled accounts...
+        */
+       priv->has_enabled_accounts = FALSE;
+       priv->has_accounts         = FALSE;
+       
        /* Emit "update-account" */
        g_signal_emit (G_OBJECT(self), 
                       signals[ACCOUNT_CHANGED_SIGNAL], 0,