* check for a valid foldername
[modest] / src / modest-account-mgr.c
index 46ed9d4..56e29ab 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: */
+/* is the account already in the queue? */
+static gboolean
+in_change_queue (GSList *change_queue, const gchar *account)
+{
+       GSList *cursor = change_queue;
+       while (cursor) {
+               const gchar *acc = cursor->data;
+               if (acc && strcmp (acc, account) == 0)
+                       return TRUE;
+               cursor = g_slist_next (cursor);
+       }
+       return FALSE;
+}
+
+static GSList*
+add_to_change_queue (GSList *change_queue, const gchar *account_name)
+{
+       g_return_val_if_fail (account_name, change_queue);      
+       return g_slist_prepend (change_queue, g_strdup (account_name));
+}
+
+
+/* we don't need to track specific accounts, as in our UI case
+ * it's impossible to change two accounts within .5 seconds.
+ * still, we might want to allow that later, and then this func
+ * will come in handy */
+#if 0
+static GSList*
+remove_from_queue (GSList *change_queue, const gchar *account)
+{
+       GSList *cursor = change_queue;
+       while (cursor) {
+               const gchar *acc = cursor->data;
+               if (acc && strcmp (acc, account) == 0) {
+                       g_free (acc);
+                       return g_slist_delete_link (change_queue, cursor);
+               }
+               cursor = g_slist_next (cursor);
+       }
+       return change_queue;
+}
+#endif
+
 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. */
-       
-       if (priv->changed_conf_keys) {
-               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);
-                       
-               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;
+       GSList *cursor = priv->change_queue;
+       while (cursor) {
+               const gchar *account = cursor->data;
+               if (account)
+                       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0, 
+                                      account);
+               cursor = g_slist_next (cursor);
        }
        
-       return TRUE; /* Call this again later. */
+       /* free our queue */
+       g_slist_foreach (priv->change_queue, (GFunc)g_free, NULL);
+       g_slist_free (priv->change_queue);
+       priv->change_queue = NULL;
+       priv->timeout = 0; /* hmmm */
+       
+       return FALSE; /* don't call me again */
+}
+
+
+/* little hack to retrieve the account name from a server account name,
+ * by relying on the convention for that. Note: this changes the
+ * string in-place
+ *
+ * server accounts look like fooID_transport or fooID_store
+ * FIXME: make the suffixes more explicit in the account setup
+ */
+static void
+get_account_name_from_server_account (gchar *server_account_name)
+{
+       static const gchar *t = "ID_transport";
+       static const gchar *s = "ID_store";
+       const guint len_t = strlen (t);
+       const guint len_s = strlen (s);
+       
+       guint len_a = strlen (server_account_name);
+               
+       if (g_str_has_suffix (server_account_name, t)) 
+               server_account_name [len_a - len_t] = '\0';
+       else if (g_str_has_suffix (server_account_name, s)) 
+               server_account_name [len_a - len_s] = '\0';
 }
 
+
+
 static void
-on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event, gpointer user_data)
+on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event,
+              ModestConfNotificationId id, 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;
+       gchar* account_name = 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));
+               gchar *default_account =  modest_account_mgr_get_default_account (self);
+               if (!default_account) {
+                       g_warning ("BUG: cannot find default account");
+                       return;
+               } else {
+                       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0, 
+                                      default_account);
+                       g_free(default_account);
+                       return;
+               }       
        }
        
        is_account_key = FALSE;
        is_server_account = FALSE;
-       account = _modest_account_mgr_account_from_key (key, &is_account_key, 
-                                                       &is_server_account);
+       account_name = _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)
+       if (!account_name)
                return;
 
+       /* ignore server account changes */
+       if (is_server_account)
+               /* change in place: retrieve the parent account name */
+               get_account_name_from_server_account (account_name);
+
        /* 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) && 
+       if ((is_account_key || is_server_account) &&
            event == MODEST_CONF_EVENT_KEY_UNSET) {
-               g_free (account);
+               g_free (account_name);
                return;
        }
 
@@ -124,20 +201,24 @@ on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event, gpoint
        if (is_server_account)
                enabled = TRUE;
        else
-               enabled = modest_account_mgr_get_enabled (self, account);
+               enabled = modest_account_mgr_get_enabled (self, account_name);
 
        /* 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) ||
+       if (!is_server_account)
+       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));
+               if (!in_change_queue (priv->change_queue, account_name)) {
+                       priv->change_queue = add_to_change_queue (priv->change_queue,
+                                                                 account_name);
+                       /* hmm, small race when this object is destroyed within
+                        * 500ms of the last change, and there are multiple timeouts... */
+                       priv->timeout = g_timeout_add (500, (GSourceFunc)on_timeout_notify_changes,
+                                                      self);
+               }   
        }
-       g_free (account);
+       g_free (account_name);
 }
 
 
@@ -149,7 +230,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 */
@@ -167,6 +248,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,
+                                     g_cclosure_marshal_VOID__STRING,
+                                     G_TYPE_NONE, 1, G_TYPE_STRING, G_TYPE_STRING, 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)
 {
@@ -178,32 +306,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,
-                             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),
-                             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,9 +315,12 @@ modest_account_mgr_init (ModestAccountMgr * obj)
        ModestAccountMgrPrivate *priv =
                MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
 
-       priv->modest_conf = NULL;
+       priv->modest_conf   = NULL;
        priv->busy_accounts = NULL;
-       priv->timeout = g_timeout_add (1000 /* milliseconds */, on_timeout_notify_changes, obj);
+       priv->change_queue  = NULL;
+       priv->timeout       = 0;
+       
+       priv->notification_id_accounts = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
 }
 
 static void
@@ -224,6 +329,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);
@@ -234,14 +345,11 @@ modest_account_mgr_finalize (GObject * obj)
                g_object_unref (G_OBJECT(priv->modest_conf));
                priv->modest_conf = NULL;
        }
-       
+
        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);
-       }
+       priv->timeout = 0;
+
 
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
@@ -261,10 +369,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);
 }
@@ -358,6 +466,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)
@@ -986,7 +1097,7 @@ modest_account_mgr_set_list (ModestAccountMgr *self,
 }
 
 gboolean
-modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
+modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
                                   gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
@@ -995,7 +1106,7 @@ modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
        gboolean retval;
        GError *err = NULL;
 
-       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
+       g_return_val_if_fail (self, FALSE);
         g_return_val_if_fail (name, FALSE);
 
        keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
@@ -1133,20 +1244,45 @@ _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key
 
 
 
+
+/* optimization: only with non-alphanum chars, escaping is needed */
+inline static gboolean
+is_alphanum (const gchar* str)
+{
+       const gchar *cursor;
+       for (cursor = str; cursor && *cursor; ++cursor) {
+               const char c = *cursor;
+               /* we cannot trust isalnum(3), because it might consider locales */
+               /*       numbers            ALPHA            alpha       */
+               if (!((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122)))
+                       return FALSE;
+       }
+       return TRUE;
+}
+               
+
 /* must be freed by caller */
 gchar *
 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
 {
-       gchar *retval = NULL;
-       
+       gchar *retval = NULL;   
        gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
+       gchar *escaped_account_name, *escaped_name;
        
        if (!account_name)
                return g_strdup (namespace);
        
-       /* Always escape the conf keys, so that it is acceptable to gconf: */
-       gchar *escaped_account_name = account_name ? modest_conf_key_escape (account_name) : NULL;
-       gchar *escaped_name =  name ? modest_conf_key_escape (name) : NULL;
+       /* optimization: only escape names when need to be escaped */
+       if (is_alphanum (account_name))
+               escaped_account_name = (gchar*)account_name;
+       else
+               escaped_account_name = modest_conf_key_escape (account_name);
+       
+       if (is_alphanum (name))
+               escaped_name = (gchar*)name;
+       else
+               escaped_name = modest_conf_key_escape (name);
+       //////////////////////////////////////////////////////////////
 
        if (escaped_account_name && escaped_name)
                retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
@@ -1160,9 +1296,12 @@ _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar
                retval = NULL;
        }
 
-       g_free (escaped_name);
-       g_free (escaped_account_name);
-
+       /* g_free is only needed if we actually allocated anything */
+       if (name != escaped_name)
+               g_free (escaped_name);
+       if (account_name != escaped_account_name)
+               g_free (escaped_account_name);
+       
        return retval;
 }
 
@@ -1190,7 +1329,7 @@ compare_account_name(gconstpointer a, gconstpointer b)
 
 void 
 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name, 
-                                                                                                                                               gboolean busy)
+                                   gboolean busy)
 {
        ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
        if (busy)