app/config/gimpconfig-serialize.c app/config/gimpconfig-utils.[ch]

2002-05-06  Sven Neumann  <sven@gimp.org>

	* app/config/gimpconfig-serialize.c
	* app/config/gimpconfig-utils.[ch]
	* app/config/gimpconfig.[ch]
	* app/config/gimprc.[ch]: added a GimpRc implementation of
	gimp_config_duplicate() that also copies unknown tokens. Added new
	function gimp_rc_query().

	* app/config/test-config.c: added tests for gimp_rc_query().

	* app/config/Makefile.am: need to link test app against libappwidgets.
This commit is contained in:
Sven Neumann 2002-05-05 22:11:34 +00:00 committed by Sven Neumann
parent c86ca2da6a
commit 7f6b1d5593
15 changed files with 255 additions and 99 deletions

View file

@ -1,3 +1,16 @@
2002-05-06 Sven Neumann <sven@gimp.org>
* app/config/gimpconfig-serialize.c
* app/config/gimpconfig-utils.[ch]
* app/config/gimpconfig.[ch]
* app/config/gimprc.[ch]: added a GimpRc implementation of
gimp_config_duplicate() that also copies unknown tokens. Added new
function gimp_rc_query().
* app/config/test-config.c: added tests for gimp_rc_query().
* app/config/Makefile.am: need to link test app against libappwidgets.
2002-05-05 Michael Natterer <mitch@gimp.org>
* app/Makefile.am

View file

@ -59,6 +59,7 @@ test_config_DEPENDENCIES = @STRIP_BEGIN@ \
../base/libappbase.a \
../core/libappcore.a \
../display/libappdisplay.a \
../widgets/libappwidgets.a \
$(top_builddir)/libgimpbase/libgimpbase-$(LT_RELEASE).la \
@STRIP_END@

View file

@ -87,6 +87,7 @@ gimp_config_serialize_properties (GObject *object,
g_string_assign (str, "(");
g_string_append (str, prop_spec->name);
g_string_append_c (str, ' ');
if (gimp_config_serialize_value (&value, str))
{
@ -162,6 +163,7 @@ gimp_config_serialize_changed_properties (GObject *new,
{
g_string_assign (str, "(");
g_string_append (str, prop_spec->name);
g_string_append_c (str, ' ');
if (gimp_config_serialize_value (&new_value, str))
{
@ -189,7 +191,7 @@ gimp_config_serialize_changed_properties (GObject *new,
* gimp_config_serialize_value:
* @value: a #GValue.
* @str: a #Gstring.
*
*
* This utility function appends a string representation of #GValue to @str.
*
* Return value: %TRUE if serialization succeeded, %FALSE otherwise.
@ -203,7 +205,7 @@ gimp_config_serialize_value (const GValue *value,
gboolean bool;
bool = g_value_get_boolean (value);
g_string_append (str, bool ? " yes" : " no");
g_string_append (str, bool ? "yes" : "no");
return TRUE;
}
@ -218,7 +220,6 @@ gimp_config_serialize_value (const GValue *value,
if (enum_value && enum_value->value_nick)
{
g_string_append_c (str, ' ');
g_string_append (str, enum_value->value_nick);
return TRUE;
}
@ -239,7 +240,7 @@ gimp_config_serialize_value (const GValue *value,
return FALSE;
escaped = g_strescape (cstr, NULL);
g_string_append_printf (str, " \"%s\"", escaped);
g_string_append_printf (str, "\"%s\"", escaped);
g_free (escaped);
return TRUE;
}
@ -255,7 +256,6 @@ gimp_config_serialize_value (const GValue *value,
v_double = (gdouble) g_value_get_float (value);
g_ascii_formatd (buf, sizeof (buf), "%f", v_double);
g_string_append_c (str, ' ');
g_string_append (str, buf);
return TRUE;
}
@ -267,7 +267,6 @@ gimp_config_serialize_value (const GValue *value,
g_value_init (&tmp_value, G_TYPE_STRING);
g_value_transform (value, &tmp_value);
g_string_append_c (str, ' ');
g_string_append (str, g_value_get_string (&tmp_value));
g_value_unset (&tmp_value);

View file

@ -62,6 +62,43 @@ gimp_config_values_equal (const GValue *a,
}
}
void
gimp_config_copy_properties (GObject *src,
GObject *dest)
{
GObjectClass *klass;
GParamSpec **property_specs;
guint n_property_specs;
guint i;
g_return_if_fail (G_IS_OBJECT (src));
g_return_if_fail (G_IS_OBJECT (dest));
g_return_if_fail (G_TYPE_FROM_INSTANCE (src) == G_TYPE_FROM_INSTANCE (dest));
klass = G_OBJECT_GET_CLASS (src);
property_specs = g_object_class_list_properties (klass, &n_property_specs);
if (!property_specs)
return;
for (i = 0; i < n_property_specs; i++)
{
GParamSpec *prop_spec;
GValue value = { 0, };
prop_spec = property_specs[i];
if (! (prop_spec->flags & G_PARAM_READWRITE))
continue;
g_value_init (&value, prop_spec->value_type);
g_object_get_property (src, prop_spec->name, &value);
g_object_set_property (dest, prop_spec->name, &value);
}
}
gchar *
gimp_config_build_data_path (const gchar *name)
{

View file

@ -25,6 +25,8 @@
gboolean gimp_config_values_equal (const GValue *a,
const GValue *b);
void gimp_config_copy_properties (GObject *src,
GObject *dest);
gchar * gimp_config_build_data_path (const gchar *name);
gchar * gimp_config_build_plug_in_path (const gchar *name);

View file

@ -114,36 +114,9 @@ gimp_config_iface_deserialize (GObject *object,
static GObject *
gimp_config_iface_duplicate (GObject *object)
{
GObject *dup;
GObjectClass *klass;
GParamSpec **property_specs;
guint n_property_specs;
guint i;
GObject *dup = g_object_new (G_TYPE_FROM_INSTANCE (object), NULL);
dup = g_object_new (G_TYPE_FROM_INSTANCE (object), NULL);
klass = G_OBJECT_GET_CLASS (object);
property_specs = g_object_class_list_properties (klass, &n_property_specs);
if (!property_specs)
return dup;
for (i = 0; i < n_property_specs; i++)
{
GParamSpec *prop_spec;
GValue value = { 0, };
prop_spec = property_specs[i];
if (! (prop_spec->flags & G_PARAM_READWRITE))
continue;
g_value_init (&value, prop_spec->value_type);
g_object_get_property (object, prop_spec->name, &value);
g_object_set_property (dup, prop_spec->name, &value);
}
gimp_config_copy_properties (object, dup);
return dup;
}
@ -377,7 +350,7 @@ gimp_config_is_equal_to (GObject *a,
g_return_val_if_fail (G_IS_OBJECT (a), FALSE);
g_return_val_if_fail (G_IS_OBJECT (b), FALSE);
g_return_val_if_fail (G_TYPE_FROM_INSTANCE (a) != G_TYPE_FROM_INSTANCE (b),
g_return_val_if_fail (G_TYPE_FROM_INSTANCE (a) == G_TYPE_FROM_INSTANCE (b),
FALSE);
gimp_config_iface = GIMP_GET_CONFIG_INTERFACE (a);
@ -411,8 +384,10 @@ static void gimp_config_destroy_unknown_tokens (GSList *unknown_tokens);
*
* This function allows to add arbitrary key/value pairs to a GObject.
* It's purpose is to attach additional data to a #GimpConfig object
* that is stored along with the object properties when serializing
* the object to a configuration file.
* that can be stored along with the object properties when
* serializing the object to a configuration file. Please note however
* that the default gimp_config_serialize() implementation does not
* serialize unknown tokens.
*
* If you want to remove a key/value pair from the object, call this
* function with a %NULL @value.

View file

@ -32,13 +32,13 @@ struct _GimpConfigInterface
{
GTypeInterface base_iface;
void (* serialize) (GObject *object,
gint fd);
gboolean (* deserialize) (GObject *object,
GScanner *scanner);
GObject * (* duplicate) (GObject *object);
gboolean (* equal) (GObject *a,
GObject *b);
void (* serialize) (GObject *object,
gint fd);
gboolean (* deserialize) (GObject *object,
GScanner *scanner);
GObject * (* duplicate) (GObject *object);
gboolean (* equal) (GObject *a,
GObject *b);
};
typedef void (* GimpConfigForeachFunc) (const gchar *key,

View file

@ -39,20 +39,22 @@
#include "libgimpbase/gimpbase.h"
#include "gimpconfig.h"
#include "gimpconfig-serialize.h"
#include "gimpconfig-deserialize.h"
#include "gimpconfig-serialize.h"
#include "gimpconfig-utils.h"
#include "gimprc.h"
#include "libgimp/gimpintl.h"
static void gimp_rc_config_iface_init (gpointer iface,
gpointer iface_data);
static void gimp_rc_serialize (GObject *object,
gint fd);
static gboolean gimp_rc_deserialize (GObject *object,
GScanner *scanner);
static void gimp_rc_write_header (gint fd);
static void gimp_rc_config_iface_init (gpointer iface,
gpointer iface_data);
static void gimp_rc_serialize (GObject *object,
gint fd);
static gboolean gimp_rc_deserialize (GObject *object,
GScanner *scanner);
static GObject * gimp_rc_duplicate (GObject *object);
static void gimp_rc_write_header (gint fd);
GType
@ -101,6 +103,7 @@ gimp_rc_config_iface_init (gpointer iface,
config_iface->serialize = gimp_rc_serialize;
config_iface->deserialize = gimp_rc_deserialize;
config_iface->duplicate = gimp_rc_duplicate;
}
static void
@ -118,6 +121,28 @@ gimp_rc_deserialize (GObject *object,
return gimp_config_deserialize_properties (object, scanner, TRUE);
}
static void
gimp_rc_duplicate_unknown_token (const gchar *key,
const gchar *value,
gpointer user_data)
{
gimp_config_add_unknown_token (G_OBJECT (user_data), key, value);
}
static GObject *
gimp_rc_duplicate (GObject *object)
{
GObject *dup = g_object_new (GIMP_TYPE_RC, NULL);
gimp_config_copy_properties (object, dup);
gimp_config_foreach_unknown_token (object,
gimp_rc_duplicate_unknown_token,
dup);
return dup;
}
/**
* gimp_rc_new:
*
@ -131,6 +156,74 @@ gimp_rc_new (void)
return GIMP_RC (g_object_new (GIMP_TYPE_RC, NULL));
}
/**
* gimp_rc_query:
* @rc: a #GimpRc object.
* @key: a string used as a key for the lookup.
*
* This function looks up @key in the object properties of @rc. If
* there's a matching property, a string representation of its value
* is returned. If no property is found, the list of unknown tokens
* attached to the @rc object is searched.
*
* Return value: a newly allocated string representing the value or %NULL
* if the key couldn't be found.
**/
gchar *
gimp_rc_query (GimpRc *rc,
const gchar *key)
{
GObjectClass *klass;
GParamSpec **property_specs;
GParamSpec *prop_spec;
guint i, n_property_specs;
gchar *retval = NULL;
g_return_val_if_fail (GIMP_IS_RC (rc), NULL);
g_return_val_if_fail (key != NULL, NULL);
klass = G_OBJECT_GET_CLASS (rc);
property_specs = g_object_class_list_properties (klass, &n_property_specs);
if (!property_specs)
return NULL;
for (i = 0, prop_spec = NULL; i < n_property_specs && !prop_spec; i++)
{
prop_spec = property_specs[i];
if (! (prop_spec->flags & G_PARAM_READABLE) ||
strcmp (prop_spec->name, key))
{
prop_spec = NULL;
}
}
if (prop_spec)
{
GString *str = g_string_new (NULL);
GValue value = { 0, };
g_value_init (&value, prop_spec->value_type);
g_object_get_property (G_OBJECT (rc), prop_spec->name, &value);
if (gimp_config_serialize_value (&value, str))
retval = g_string_free (str, FALSE);
else
g_string_free (str, TRUE);
g_value_unset (&value);
}
else
{
retval = g_strdup (gimp_config_lookup_unknown_token (G_OBJECT (rc),
key));
}
return retval;
}
/**
* gimp_rc_write_changes:
* @new_rc: a #GimpRc object.

View file

@ -48,6 +48,8 @@ struct _GimpRcClass
GType gimp_rc_get_type (void) G_GNUC_CONST;
GimpRc * gimp_rc_new (void);
gchar * gimp_rc_query (GimpRc *rc,
const gchar *key);
gboolean gimp_rc_write_changes (GimpRc *new_rc,
GimpRc *old_rc,
const gchar *filename);

View file

@ -48,6 +48,7 @@ main (int argc,
GimpRc *gimprc2;
const gchar *filename = "foorc";
gchar *header;
gchar *result;
gint i;
GError *error = NULL;
@ -131,10 +132,30 @@ main (int argc,
}
else
{
g_print ("This test should have failed :-(");
g_print ("This test should have failed :-(\n");
return -1;
}
g_print ("\n Querying for default-comment ... ");
result = gimp_rc_query (gimprc, "default-comment");
if (result)
g_print ("OK, found %s.\n", result);
else
g_print ("failed!\n");
g_free (result);
g_print (" Querying for foobar ... ");
result = gimp_rc_query (gimprc, "foobar");
if (result && strcmp (result, "hadjaha") == 0)
g_print ("OK, found %s.\n", result);
else
g_print ("failed!\n");
g_free (result);
g_object_unref (G_OBJECT (gimprc));
g_print ("\nFinished test of GimpConfig.\n\n");

View file

@ -114,36 +114,9 @@ gimp_config_iface_deserialize (GObject *object,
static GObject *
gimp_config_iface_duplicate (GObject *object)
{
GObject *dup;
GObjectClass *klass;
GParamSpec **property_specs;
guint n_property_specs;
guint i;
GObject *dup = g_object_new (G_TYPE_FROM_INSTANCE (object), NULL);
dup = g_object_new (G_TYPE_FROM_INSTANCE (object), NULL);
klass = G_OBJECT_GET_CLASS (object);
property_specs = g_object_class_list_properties (klass, &n_property_specs);
if (!property_specs)
return dup;
for (i = 0; i < n_property_specs; i++)
{
GParamSpec *prop_spec;
GValue value = { 0, };
prop_spec = property_specs[i];
if (! (prop_spec->flags & G_PARAM_READWRITE))
continue;
g_value_init (&value, prop_spec->value_type);
g_object_get_property (object, prop_spec->name, &value);
g_object_set_property (dup, prop_spec->name, &value);
}
gimp_config_copy_properties (object, dup);
return dup;
}
@ -377,7 +350,7 @@ gimp_config_is_equal_to (GObject *a,
g_return_val_if_fail (G_IS_OBJECT (a), FALSE);
g_return_val_if_fail (G_IS_OBJECT (b), FALSE);
g_return_val_if_fail (G_TYPE_FROM_INSTANCE (a) != G_TYPE_FROM_INSTANCE (b),
g_return_val_if_fail (G_TYPE_FROM_INSTANCE (a) == G_TYPE_FROM_INSTANCE (b),
FALSE);
gimp_config_iface = GIMP_GET_CONFIG_INTERFACE (a);
@ -411,8 +384,10 @@ static void gimp_config_destroy_unknown_tokens (GSList *unknown_tokens);
*
* This function allows to add arbitrary key/value pairs to a GObject.
* It's purpose is to attach additional data to a #GimpConfig object
* that is stored along with the object properties when serializing
* the object to a configuration file.
* that can be stored along with the object properties when
* serializing the object to a configuration file. Please note however
* that the default gimp_config_serialize() implementation does not
* serialize unknown tokens.
*
* If you want to remove a key/value pair from the object, call this
* function with a %NULL @value.

View file

@ -32,13 +32,13 @@ struct _GimpConfigInterface
{
GTypeInterface base_iface;
void (* serialize) (GObject *object,
gint fd);
gboolean (* deserialize) (GObject *object,
GScanner *scanner);
GObject * (* duplicate) (GObject *object);
gboolean (* equal) (GObject *a,
GObject *b);
void (* serialize) (GObject *object,
gint fd);
gboolean (* deserialize) (GObject *object,
GScanner *scanner);
GObject * (* duplicate) (GObject *object);
gboolean (* equal) (GObject *a,
GObject *b);
};
typedef void (* GimpConfigForeachFunc) (const gchar *key,

View file

@ -87,6 +87,7 @@ gimp_config_serialize_properties (GObject *object,
g_string_assign (str, "(");
g_string_append (str, prop_spec->name);
g_string_append_c (str, ' ');
if (gimp_config_serialize_value (&value, str))
{
@ -162,6 +163,7 @@ gimp_config_serialize_changed_properties (GObject *new,
{
g_string_assign (str, "(");
g_string_append (str, prop_spec->name);
g_string_append_c (str, ' ');
if (gimp_config_serialize_value (&new_value, str))
{
@ -189,7 +191,7 @@ gimp_config_serialize_changed_properties (GObject *new,
* gimp_config_serialize_value:
* @value: a #GValue.
* @str: a #Gstring.
*
*
* This utility function appends a string representation of #GValue to @str.
*
* Return value: %TRUE if serialization succeeded, %FALSE otherwise.
@ -203,7 +205,7 @@ gimp_config_serialize_value (const GValue *value,
gboolean bool;
bool = g_value_get_boolean (value);
g_string_append (str, bool ? " yes" : " no");
g_string_append (str, bool ? "yes" : "no");
return TRUE;
}
@ -218,7 +220,6 @@ gimp_config_serialize_value (const GValue *value,
if (enum_value && enum_value->value_nick)
{
g_string_append_c (str, ' ');
g_string_append (str, enum_value->value_nick);
return TRUE;
}
@ -239,7 +240,7 @@ gimp_config_serialize_value (const GValue *value,
return FALSE;
escaped = g_strescape (cstr, NULL);
g_string_append_printf (str, " \"%s\"", escaped);
g_string_append_printf (str, "\"%s\"", escaped);
g_free (escaped);
return TRUE;
}
@ -255,7 +256,6 @@ gimp_config_serialize_value (const GValue *value,
v_double = (gdouble) g_value_get_float (value);
g_ascii_formatd (buf, sizeof (buf), "%f", v_double);
g_string_append_c (str, ' ');
g_string_append (str, buf);
return TRUE;
}
@ -267,7 +267,6 @@ gimp_config_serialize_value (const GValue *value,
g_value_init (&tmp_value, G_TYPE_STRING);
g_value_transform (value, &tmp_value);
g_string_append_c (str, ' ');
g_string_append (str, g_value_get_string (&tmp_value));
g_value_unset (&tmp_value);

View file

@ -62,6 +62,43 @@ gimp_config_values_equal (const GValue *a,
}
}
void
gimp_config_copy_properties (GObject *src,
GObject *dest)
{
GObjectClass *klass;
GParamSpec **property_specs;
guint n_property_specs;
guint i;
g_return_if_fail (G_IS_OBJECT (src));
g_return_if_fail (G_IS_OBJECT (dest));
g_return_if_fail (G_TYPE_FROM_INSTANCE (src) == G_TYPE_FROM_INSTANCE (dest));
klass = G_OBJECT_GET_CLASS (src);
property_specs = g_object_class_list_properties (klass, &n_property_specs);
if (!property_specs)
return;
for (i = 0; i < n_property_specs; i++)
{
GParamSpec *prop_spec;
GValue value = { 0, };
prop_spec = property_specs[i];
if (! (prop_spec->flags & G_PARAM_READWRITE))
continue;
g_value_init (&value, prop_spec->value_type);
g_object_get_property (src, prop_spec->name, &value);
g_object_set_property (dest, prop_spec->name, &value);
}
}
gchar *
gimp_config_build_data_path (const gchar *name)
{

View file

@ -25,6 +25,8 @@
gboolean gimp_config_values_equal (const GValue *a,
const GValue *b);
void gimp_config_copy_properties (GObject *src,
GObject *dest);
gchar * gimp_config_build_data_path (const gchar *name);
gchar * gimp_config_build_plug_in_path (const gchar *name);