X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fmodest-conf.c;h=835d2873abd8ba803274fa1acadfa14034e92dd8;hp=3f6e246db4379ed3220327f0a9f406c80645d670;hb=2130b41c3722f14f8bda39544aff1c93c50ae8b4;hpb=64fcc38982f13e79e4acf8a3c96deacbf6b32688 diff --git a/src/modest-conf.c b/src/modest-conf.c index 3f6e246..835d287 100644 --- a/src/modest-conf.c +++ b/src/modest-conf.c @@ -31,22 +31,24 @@ #include #include #include +#include "modest-defs.h" #include "modest-conf.h" +#include "modest-error.h" #include "modest-marshal.h" #include 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); - -static void -modest_conf_maemo_fake_on_change (ModestConf *conf, const gchar* key, ModestConfEvent event); - /* list my signals */ enum { KEY_CHANGED_SIGNAL, @@ -106,18 +108,17 @@ modest_conf_class_init (ModestConfClass *klass) G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (ModestConfClass,key_changed), NULL, NULL, - modest_marshal_VOID__STRING_INT, - G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT); + modest_marshal_VOID__STRING_INT_INT, + G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT); } static void modest_conf_init (ModestConf *obj) { GConfClient *conf = NULL; + GError *error = NULL; ModestConfPrivate *priv = MODEST_CONF_GET_PRIVATE(obj); - GError *err = NULL; - priv->gconf_client = NULL; conf = gconf_client_get_default (); @@ -125,28 +126,26 @@ modest_conf_init (ModestConf *obj) g_printerr ("modest: could not get gconf client\n"); return; } - gconf_client_add_dir (conf,MODEST_CONF_NAMESPACE, + + priv->gconf_client = conf; + + /* All the tree will be listened */ + gconf_client_add_dir (priv->gconf_client, + "/apps/modest", GCONF_CLIENT_PRELOAD_NONE, - &err); - if (err) { - g_printerr ("modest: error %d with gconf_client_add_dir: '%s'\n", - err->code, err->message); - g_object_unref (conf); - g_error_free (err); - return; - } - - gconf_client_notify_add (conf, MODEST_CONF_NAMESPACE, - modest_conf_on_change, - obj, NULL, &err); - if (err) { - g_printerr ("modest: gconf_client_notify_add error %d: '%s'\n", - err->code, err->message); - g_object_unref (conf); - g_error_free (err); - return; + &error); + + /* Notify every change under namespace */ + if (!error) { + gconf_client_notify_add (priv->gconf_client, + "/apps/modest", + modest_conf_on_change, + obj, + NULL, + &error); + } else { + g_error_free (error); } - priv->gconf_client = conf; /* all went well! */ } static void @@ -213,6 +212,18 @@ modest_conf_get_int (ModestConf* self, const gchar* key, GError **err) return gconf_client_get_int (priv->gconf_client, key, err); } +gdouble +modest_conf_get_float (ModestConf* self, const gchar* key, GError **err) +{ + ModestConfPrivate *priv; + + g_return_val_if_fail (self, -1); + g_return_val_if_fail (key, -1); + + priv = MODEST_CONF_GET_PRIVATE(self); + + return gconf_client_get_float (priv->gconf_client, key, err); +} gboolean modest_conf_get_bool (ModestConf* self, const gchar* key, GError **err) @@ -265,14 +276,9 @@ modest_conf_set_string (ModestConf* self, const gchar* key, const gchar* val, return FALSE; } - if (gconf_client_set_string (priv->gconf_client, key, val, err)) { - modest_conf_maemo_fake_on_change (self, key, MODEST_CONF_EVENT_KEY_CHANGED); - return TRUE; - } else - return FALSE; + return gconf_client_set_string (priv->gconf_client, key, val, err); } - gboolean modest_conf_set_int (ModestConf* self, const gchar* key, gint val, GError **err) @@ -289,11 +295,28 @@ modest_conf_set_int (ModestConf* self, const gchar* key, gint val, return FALSE; } - if (gconf_client_set_int (priv->gconf_client, key, val, err)) { - modest_conf_maemo_fake_on_change (self, key, MODEST_CONF_EVENT_KEY_CHANGED); - return TRUE; - } else + return gconf_client_set_int (priv->gconf_client, key, val, err); +} + +gboolean +modest_conf_set_float (ModestConf* self, + const gchar* key, + gdouble val, + GError **err) +{ + ModestConfPrivate *priv; + + g_return_val_if_fail (self,FALSE); + g_return_val_if_fail (key, FALSE); + + priv = MODEST_CONF_GET_PRIVATE(self); + + if (!gconf_client_key_is_writable(priv->gconf_client,key,err)) { + g_printerr ("modest: '%s' is not writable\n", key); return FALSE; + } + + return gconf_client_set_float (priv->gconf_client, key, val, err); } @@ -313,11 +336,7 @@ modest_conf_set_bool (ModestConf* self, const gchar* key, gboolean val, return FALSE; } - if (gconf_client_set_bool (priv->gconf_client, key, val, err)) { - modest_conf_maemo_fake_on_change (self, key, MODEST_CONF_EVENT_KEY_CHANGED); - return TRUE; - } else - return FALSE; + return gconf_client_set_bool (priv->gconf_client, key, val, err); } @@ -328,7 +347,6 @@ modest_conf_set_list (ModestConf* self, const gchar* key, { ModestConfPrivate *priv; GConfValueType gconf_type; - gboolean result; g_return_val_if_fail (self, FALSE); g_return_val_if_fail (key, FALSE); @@ -336,33 +354,8 @@ modest_conf_set_list (ModestConf* self, const gchar* key, priv = MODEST_CONF_GET_PRIVATE(self); gconf_type = modest_conf_type_to_gconf_type (list_type, err); - if (*err) - return FALSE; - - 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); - } - if (result) - modest_conf_maemo_fake_on_change (self, key, MODEST_CONF_EVENT_KEY_CHANGED); - - return result; + return gconf_client_set_list (priv->gconf_client, key, gconf_type, val, err); } @@ -394,11 +387,7 @@ modest_conf_remove_key (ModestConf* self, const gchar* key, GError **err) retval = gconf_client_recursive_unset (priv->gconf_client,key,0,err); gconf_client_suggest_sync (priv->gconf_client, NULL); - if (retval) { - modest_conf_maemo_fake_on_change (self, key, MODEST_CONF_EVENT_KEY_UNSET); - return TRUE; - } else - return FALSE; + return retval; } @@ -451,20 +440,12 @@ 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, +modest_conf_on_change (GConfClient *client, + guint conn_id, + GConfEntry *entry, gpointer data) { - /* - * on maemo, there's a nasty bug in gconf, which makes it really - * slow, for updates and notifications. as an ugly hack, we turn off all - * gconf-based notifications, and send them ourselves, a short time - * after we do a change. this does not work for non-modest-conf - * changes of course... - */ -#ifndef MODEST_PLATFORM_MAEMO - ModestConfEvent event; const gchar* key; @@ -473,11 +454,9 @@ modest_conf_on_change (GConfClient *client, guint conn_id, GConfEntry *entry, g_signal_emit (G_OBJECT(data), signals[KEY_CHANGED_SIGNAL], 0, - key, event); -#endif /*!MODEST_PLATFORM_MAEMO*/ + key, event, conn_id); } - static GConfValueType modest_conf_type_to_gconf_type (ModestConfValueType value_type, GError **err) { @@ -497,80 +476,47 @@ modest_conf_type_to_gconf_type (ModestConfValueType value_type, GError **err) gconf_type = GCONF_VALUE_STRING; break; default: - /* FIXME: use MODEST_ERROR, and error code */ gconf_type = GCONF_VALUE_INVALID; g_printerr ("modest: invalid list value type %d\n", value_type); - *err = g_error_new_literal (0, 0, "invalid list value type"); - } + *err = g_error_new_literal (MODEST_CONF_ERROR, + MODEST_CONF_ERROR_INVALID_VALUE, + "invalid list value type"); + } return gconf_type; } +void +modest_conf_listen_to_namespace (ModestConf *self, + const gchar *namespace) +{ + ModestConfPrivate *priv; + GError *error = NULL; + g_return_if_fail (MODEST_IS_CONF (self)); + g_return_if_fail (namespace); + + priv = MODEST_CONF_GET_PRIVATE(self); -//////////////////////////////////////////////////////////////////////////////// -/* workaround for the b0rked dbus-gconf on maemo */ -/* fires a fake change notification after 0.3 secs */ -#ifdef MODEST_PLATFORM_MAEMO -typedef struct { - GObject *obj; - gchar *key; -} ChangeHelper; - - -ChangeHelper* -change_helper_new (ModestConf *conf, const gchar *key) -{ - ChangeHelper *helper = g_slice_alloc (sizeof(ChangeHelper)); - helper->obj = g_object_ref(G_OBJECT(conf)); - helper->key = g_strdup (key); - return helper; + /* Add the namespace to the list of the namespaces that will + be observed */ + gconf_client_add_dir (priv->gconf_client, + namespace, + GCONF_CLIENT_PRELOAD_NONE, + &error); } -static void -change_helper_free (ChangeHelper *helper) +void +modest_conf_forget_namespace (ModestConf *self, + const gchar *namespace) { - g_object_unref (helper->obj); - g_free (helper->key); - helper->key = NULL; - helper->obj = NULL; - g_slice_free (ChangeHelper,helper); -} + ModestConfPrivate *priv; -static gboolean -emit_change_cb (ChangeHelper *helper) -{ - if (!helper) - return FALSE; - g_signal_emit (G_OBJECT(helper->obj),signals[KEY_CHANGED_SIGNAL], 0, - helper->key, MODEST_CONF_EVENT_KEY_CHANGED); - change_helper_free (helper); + g_return_if_fail (MODEST_IS_CONF (self)); + g_return_if_fail (namespace); - return FALSE; -} + priv = MODEST_CONF_GET_PRIVATE(self); -static gboolean -emit_remove_cb (ChangeHelper *helper) -{ - if (!helper) - return FALSE; - g_signal_emit (G_OBJECT(helper->obj),signals[KEY_CHANGED_SIGNAL], 0, - helper->key, MODEST_CONF_EVENT_KEY_UNSET); - change_helper_free (helper); - - return FALSE; -} -#endif /* MODEST_PLATFORM_MAEMO */ - -static void -modest_conf_maemo_fake_on_change (ModestConf *conf, const gchar* key, ModestConfEvent event) -{ -#ifdef MODEST_PLATFORM_MAEMO - - ChangeHelper *helper = change_helper_new (conf,key); - g_timeout_add (100, /* after 100 ms */ - (event == MODEST_CONF_EVENT_KEY_CHANGED) - ? (GSourceFunc)emit_change_cb : (GSourceFunc)emit_remove_cb, - (gpointer)helper); -#endif /*MODEST_PLATFORM_MAEMO*/ + /* Remove the namespace to the list of the namespaces that will + be observed */ + gconf_client_remove_dir (priv->gconf_client, namespace, NULL); } -//////////////////////////////////////////////////////////////////////////////////