* Fix some detected memory leaks
[modest] / src / modest-conf.c
index d878028..019b168 100644 (file)
  */
 
 #include <gconf/gconf-client.h>
+#include <string.h>
+#include <glib/gi18n.h>
 #include "modest-conf.h"
 #include "modest-marshal.h"
+#include <stdio.h>
 
 static void   modest_conf_class_init     (ModestConfClass *klass);
 static void   modest_conf_init           (ModestConf *obj);
 static void   modest_conf_finalize       (GObject *obj);
 static void   modest_conf_on_change     (GConfClient *client, guint conn_id,
                                          GConfEntry *entry, gpointer data);
+static GConfValueType modest_conf_type_to_gconf_type (ModestConfValueType value_type, 
+                                                     GError **err);
 /* list my signals */
 enum {
        KEY_CHANGED_SIGNAL,
@@ -69,6 +74,7 @@ modest_conf_get_type (void)
                        sizeof(ModestConf),
                        1,              /* n_preallocs */
                        (GInstanceInitFunc) modest_conf_init,
+                       NULL
                };
                my_type = g_type_register_static (G_TYPE_OBJECT,
                                                  "ModestConf",
@@ -149,25 +155,30 @@ modest_conf_finalize (GObject *obj)
                g_object_unref (priv->gconf_client);
                priv->gconf_client = NULL;
        }       
+
+       G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
-GObject*
+ModestConf*
 modest_conf_new (void)
 {
-       ModestConf *conf = MODEST_CONF(g_object_new(MODEST_TYPE_CONF, NULL));
+       ModestConf *conf;
+       ModestConfPrivate *priv;
+       
+       conf = MODEST_CONF(g_object_new(MODEST_TYPE_CONF, NULL));
        if (!conf) {
                g_printerr ("modest: failed to init ModestConf (GConf)\n");
                return NULL;
        }
 
-       ModestConfPrivate *priv = MODEST_CONF_GET_PRIVATE(conf);
+       priv = MODEST_CONF_GET_PRIVATE(conf);
        if (!priv->gconf_client) {
                g_printerr ("modest: failed to init gconf\n");
                g_object_unref (conf);
                return NULL;
        }
        
-       return G_OBJECT(conf);
+       return conf;
 }
 
 
@@ -212,6 +223,26 @@ modest_conf_get_bool (ModestConf* self, const gchar* key, GError **err)
 }
 
 
+GSList * 
+modest_conf_get_list (ModestConf* self, const gchar* key, ModestConfValueType list_type,
+                     GError **err)
+{
+       ModestConfPrivate *priv;
+       GConfValueType gconf_type;
+       
+       g_return_val_if_fail (self, NULL);
+       g_return_val_if_fail (key,  NULL);
+
+       priv = MODEST_CONF_GET_PRIVATE(self);
+
+       gconf_type = modest_conf_type_to_gconf_type (list_type, err);
+
+       return gconf_client_get_list (priv->gconf_client, key, gconf_type, err);
+}
+
+
+
+
 gboolean
 modest_conf_set_string (ModestConf* self, const gchar* key, const gchar* val,
                        GError **err)
@@ -272,6 +303,45 @@ modest_conf_set_bool (ModestConf* self, const gchar* key, gboolean val,
 }
 
 
+gboolean
+modest_conf_set_list (ModestConf* self, const gchar* key, 
+                     GSList *val, ModestConfValueType list_type, 
+                     GError **err)
+{
+       ModestConfPrivate *priv;
+       GConfValueType gconf_type;
+       
+       g_return_val_if_fail (self, FALSE);
+       g_return_val_if_fail (key, FALSE);
+
+       priv = MODEST_CONF_GET_PRIVATE(self);
+
+       gconf_type = modest_conf_type_to_gconf_type (list_type, err);
+       if (*err)
+              return FALSE;
+
+
+       gboolean result = gconf_client_set_list (priv->gconf_client, key, gconf_type, val, err);
+       if(*err) {
+                  g_warning("gconf_client_set_list() failed with key=%s. error=%s", key, (*err)->message);
+           result = FALSE;
+       }
+       
+       /* TODO: Remove this, when we fix the problem: */
+       /* This shows that sometimes set_list fails, while saying that it succeeded: */
+       if (result) {
+         const gint debug_list_length_start = g_slist_length(val);
+         GSList* debug_list = gconf_client_get_list(priv->gconf_client, key, gconf_type, err);
+         const gint debug_list_length_after = g_slist_length(debug_list);
+         
+         if(debug_list_length_start != debug_list_length_after)
+               g_warning("modest_conf_set_list(): The list length after setting is not the same as the specified list. key=%s", key);
+               
+         g_slist_free(debug_list);
+       }
+
+       return result;
+}
 
 
 GSList*
@@ -292,35 +362,71 @@ gboolean
 modest_conf_remove_key (ModestConf* self, const gchar* key, GError **err)
 {
        ModestConfPrivate *priv;
-               
+       gboolean retval;
+       
        g_return_val_if_fail (self,FALSE);
        g_return_val_if_fail (key, FALSE);
        
        priv = MODEST_CONF_GET_PRIVATE(self);
                        
-       return gconf_client_recursive_unset (priv->gconf_client,key,0,err);
-}
-
+       retval = gconf_client_recursive_unset (priv->gconf_client,key,0,err);
+       gconf_client_suggest_sync (priv->gconf_client, NULL);
 
+       return retval;
+}
 
 
 gboolean
 modest_conf_key_exists (ModestConf* self, const gchar* key, GError **err)
 {
        ModestConfPrivate *priv;
-       
+       GConfValue *val;
+
        g_return_val_if_fail (self,FALSE);
        g_return_val_if_fail (key, FALSE);
        
        priv = MODEST_CONF_GET_PRIVATE(self);
-                       
-       return gconf_client_dir_exists (priv->gconf_client,key,err);
+
+       /* the fast way... */
+       if (gconf_client_dir_exists (priv->gconf_client,key,err))
+               return TRUE;
+       
+       val = gconf_client_get (priv->gconf_client, key, NULL);
+       if (!val)
+               return FALSE;
+       else {
+               gconf_value_free (val);
+               return TRUE;
+       }       
 }
 
 
+gchar*
+modest_conf_key_escape (const gchar* key)
+{
+       g_return_val_if_fail (key, NULL);
+       g_return_val_if_fail (strlen (key) > 0, g_strdup (key));
+       
+       return gconf_escape_key (key, strlen(key));
+}
 
 
+gchar*
+modest_conf_key_unescape (const gchar* key)
+{
+       g_return_val_if_fail (key, NULL);
 
+       return gconf_unescape_key (key, strlen(key));
+}
+
+gboolean
+modest_conf_key_is_valid (const gchar* key)
+{
+       return gconf_valid_key (key, NULL);
+}
+
+
+/* hmmm... might need to make specific callback for specific keys */
 static void
 modest_conf_on_change (GConfClient *client, guint conn_id, GConfEntry *entry,
                       gpointer data)
@@ -335,3 +441,28 @@ modest_conf_on_change (GConfClient *client, guint conn_id, GConfEntry *entry,
                       signals[KEY_CHANGED_SIGNAL], 0,
                       key, event);
 }
+
+static GConfValueType
+modest_conf_type_to_gconf_type (ModestConfValueType value_type, GError **err)
+{
+       GConfValueType gconf_type = 0;
+
+       switch (value_type) {
+       case MODEST_CONF_VALUE_INT:
+               gconf_type = GCONF_VALUE_INT;
+               break;
+       case MODEST_CONF_VALUE_BOOL:
+               gconf_type = GCONF_VALUE_BOOL;
+               break;
+       case MODEST_CONF_VALUE_FLOAT:
+               gconf_type = GCONF_VALUE_FLOAT;
+               break;
+       case MODEST_CONF_VALUE_STRING:
+               gconf_type = GCONF_VALUE_STRING;
+               break;
+       default:
+               /* FIXME: use MODEST_ERROR, and error code */
+               *err = g_error_new_literal (0, 0, _("Invalid list value type"));
+       }
+       return gconf_type;
+}