From 8f7092d5bb826759e5f32c8b3e9c4a6e7a6a6b43 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Thu, 20 Jul 2006 14:11:32 +0000 Subject: [PATCH] * added convenience functions modest_conf_get_string_or_default and modest_conf_get_int_or_default to get a (user-provided) default if there is no value in gconf pmo-trunk-r391 --- src/modest-conf.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++-- src/modest-conf.h | 39 +++++++++++++++++++++++++++++++--- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/src/modest-conf.c b/src/modest-conf.c index d9c2c68..1d92571 100644 --- a/src/modest-conf.c +++ b/src/modest-conf.c @@ -155,13 +155,16 @@ modest_conf_finalize (GObject *obj) 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); @@ -213,6 +216,59 @@ modest_conf_get_bool (ModestConf* self, const gchar* key, GError **err) } + +gchar* +modest_conf_get_string_or_default (ModestConf* self, const gchar* key, + const gchar *defaultval) +{ + ModestConfPrivate *priv; + GConfValue *val; + const gchar *str; + + g_return_val_if_fail (self, g_strdup(defaultval)); + g_return_val_if_fail (key, g_strdup(defaultval)); + + priv = MODEST_CONF_GET_PRIVATE(self); + val = gconf_client_get (priv->gconf_client, key, NULL); + + if (!val) + str = defaultval; + else { + str = gconf_value_get_string (val); + gconf_value_free (val); + } + + return g_strdup (str); +} + + +gint +modest_conf_get_int_or_default (ModestConf* self, const gchar* key, + gint defaultval) +{ + ModestConfPrivate *priv; + GConfValue *val; + gint retval; + + g_return_val_if_fail (self, defaultval); + g_return_val_if_fail (key, defaultval); + + priv = MODEST_CONF_GET_PRIVATE(self); + val = gconf_client_get (priv->gconf_client, key, NULL); + + if (!val) + retval = defaultval; + else { + retval = gconf_value_get_int (val); + gconf_value_free (val); + } + + return retval; +} + + + + gboolean modest_conf_set_string (ModestConf* self, const gchar* key, const gchar* val, GError **err) diff --git a/src/modest-conf.h b/src/modest-conf.h index 7dd2514..e451a4b 100644 --- a/src/modest-conf.h +++ b/src/modest-conf.h @@ -108,7 +108,7 @@ gchar* modest_conf_get_string (ModestConf* self, const gchar* key, GError * (of course, -1 can also be returned in non-error cases). * @err gives details in case of error */ -int modest_conf_get_int (ModestConf* self, const gchar* key, GError **err); +gint modest_conf_get_int (ModestConf* self, const gchar* key, GError **err); /** @@ -119,13 +119,46 @@ int modest_conf_get_int (ModestConf* self, const gchar* key, GError * * get a boolean value from the configuration system * - * Returns: a boolean value with the value for the key, or -1 in case of error - * (of course, -1 can also be returned in non-error cases). + * Returns: a boolean value with the value for the key, or FALSE in case of error + * (of course, FALSE can also be returned in non-error cases). * @err gives details in case of error */ gboolean modest_conf_get_bool (ModestConf* self, const gchar* key, GError **err); + +/** + * modest_conf_get_string_or_default: + * @self: a ModestConf instance + * @key: the key of the value to retrieve + * @err: a GError ptr, or NULL to ignore. + * + * get a string from the configuration system; if the value is not set, + * or some error occurs, return @defaultval (copied) + * + * Returns: a newly allocated string with the value for the key, + * or the @defaultval in case of any error + */ +gchar* modest_conf_get_string_or_default (ModestConf* self, const gchar* key, + const gchar *defaultval); + + +/** + * modest_conf_get_int_or_default: + * @self: a ModestConf instance + * @key: the key of the value to retrieve + * @err: a GError ptr, or NULL to ignore. + * + * get an integer from the configuration system; if the value is not set, + * or some error occurs, return @defaultval. + * + * Returns: an integer with the value for the key, or the @defaultval in case + * of any error + */ +gint modest_conf_get_int_or_default (ModestConf* self, const gchar* key, + int defaultval); + + /** * modest_conf_set_string: * @self: a ModestConf instance -- 1.7.9.5