Commit 1e5888d4 authored by Sven Neumann's avatar Sven Neumann Committed by Sven Neumann
Browse files

app/config/gimpconfig-deserialize.c app/config/gimpconfig-params.h

2001-12-13  Sven Neumann  <sven@gimp.org>

	* app/config/gimpconfig-deserialize.c
	* app/config/gimpconfig-params.h
	* app/config/gimpconfig-serialize.[ch]
	* app/config/gimpconfig-substitute.c
	* app/config/gimpconfig-types.c
	* app/config/gimpconfig.[ch]
	* app/config/test-config.c: code cleanup, I18N

	* app/config/gimpcoreconfig.[ch]: some more properties.
parent 7494ad1b
2001-12-13 Sven Neumann <sven@gimp.org>
* app/config/gimpconfig-deserialize.c
* app/config/gimpconfig-params.h
* app/config/gimpconfig-serialize.[ch]
* app/config/gimpconfig-substitute.c
* app/config/gimpconfig-types.c
* app/config/gimpconfig.[ch]
* app/config/test-config.c: code cleanup, I18N
* app/config/gimpcoreconfig.[ch]: some more properties.
2001-12-13 Michael Natterer <mitch@gimp.org>
* app/core/gimp.[ch]: made gimp->global_buffer a GimpBuffer,
......
......@@ -54,8 +54,6 @@ enum
PROP_INTERPOLATION_TYPE,
};
static GObjectClass *parent_class = NULL;
GType
gimp_base_config_get_type (void)
......@@ -95,8 +93,6 @@ gimp_base_config_class_init (GimpBaseConfigClass *klass)
{
GObjectClass *object_class;
parent_class = g_type_class_peek_parent (klass);
object_class = G_OBJECT_CLASS (klass);
object_class->set_property = gimp_base_config_set_property;
......
......@@ -34,6 +34,8 @@
#include "gimpconfig-substitute.h"
#include "gimpconfig-types.h"
#include "libgimp/gimpintl.h"
/*
* All functions return G_TOKEN_RIGHT_PAREN on success,
......@@ -63,8 +65,8 @@ static GTokenType gimp_config_deserialize_any (GValue *value,
GParamSpec *prop_spec,
GScanner *scanner);
static inline gboolean scanner_string_utf8_valid (GScanner *scanner,
const gchar *token_name);
static inline gboolean scanner_string_utf8_valid (GScanner *scanner,
const gchar *token_name);
gboolean
......@@ -103,6 +105,8 @@ gimp_config_deserialize_properties (GObject *object,
}
}
g_free (property_specs);
token = G_TOKEN_LEFT_PAREN;
do
......@@ -146,12 +150,9 @@ gimp_config_deserialize_properties (GObject *object,
{
g_scanner_get_next_token (scanner);
g_scanner_unexp_token (scanner, token, NULL, NULL, NULL,
"fatal parse error", TRUE);
_("fatal parse error"), TRUE);
}
if (property_specs)
g_free (property_specs);
g_scanner_set_scope (scanner, old_scope_id);
return (token == G_TOKEN_EOF);
......@@ -292,9 +293,10 @@ gimp_config_deserialize_fundamental (GValue *value,
g_value_set_boolean (value, FALSE);
else
{
/* don't translate 'yes' and 'no' */
g_scanner_warn
(scanner,
"expected 'yes' or 'no' for boolean token %s, got '%s'",
_("expected 'yes' or 'no' for boolean token %s, got '%s'"),
prop_spec->name, scanner->value.v_identifier);
return G_TOKEN_NONE;
}
......@@ -350,7 +352,7 @@ gimp_config_deserialize_enum (GValue *value,
if (!enum_value)
{
g_scanner_warn (scanner,
"invalid value '%s' for enum property %s",
_("invalid value '%s' for token %s"),
scanner->value.v_identifier, prop_spec->name);
return G_TOKEN_NONE;
}
......@@ -443,14 +445,11 @@ scanner_string_utf8_valid (GScanner *scanner,
const gchar *token_name)
{
if (g_utf8_validate (scanner->value.v_string, -1, NULL))
{
return TRUE;
}
else
{
g_scanner_warn (scanner,
"value for token %s is not a valid UTF-8 string",
token_name);
return FALSE;
}
return TRUE;
g_scanner_warn (scanner,
_("value for token %s is not a valid UTF-8 string"),
token_name);
return FALSE;
}
......@@ -26,27 +26,27 @@
#define GIMP_TYPE_PARAM_MEMSIZE (gimp_param_memsize_get_type ())
#define GIMP_IS_PARAM_SPEC_MEMSIZE(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), GIMP_TYPE_PARAM_SPEC_MEMSIZE))
GType gimp_param_memsize_get_type (void) G_GNUC_CONST;
GType gimp_param_memsize_get_type (void) G_GNUC_CONST;
GParamSpec * gimp_param_spec_memsize (const gchar *name,
const gchar *nick,
const gchar *blurb,
guint minimum,
guint maximum,
guint default_value,
GParamFlags flags);
GParamSpec * gimp_param_spec_memsize (const gchar *name,
const gchar *nick,
const gchar *blurb,
guint minimum,
guint maximum,
guint default_value,
GParamFlags flags);
#define GIMP_TYPE_PARAM_PATH (gimp_param_path_get_type ())
#define GIMP_IS_PARAM_SPEC_PATH(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), GIMP_TYPE_PARAM_SPEC_PATH))
GType gimp_param_path_get_type (void) G_GNUC_CONST;
GType gimp_param_path_get_type (void) G_GNUC_CONST;
GParamSpec * gimp_param_spec_path (const gchar *name,
const gchar *nick,
const gchar *blurb,
gchar *default_value,
GParamFlags flags);
GParamSpec * gimp_param_spec_path (const gchar *name,
const gchar *nick,
const gchar *blurb,
gchar *default_value,
GParamFlags flags);
/* some convenience macros to install object properties */
......
......@@ -23,90 +23,66 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <glib-object.h>
#include "gimpconfig.h"
#include "gimpconfig-serialize.h"
#include "gimpconfig-types.h"
static gboolean gimpconfig_serialize_value (const GValue *value,
GString *str);
static void serialize_unknown_token (const gchar *key,
const gchar *value,
gpointer data);
void
gimp_config_serialize_properties (GObject *object,
FILE *file)
gint fd)
{
GObjectClass *klass;
GParamSpec **property_specs;
guint n_property_specs;
guint i;
GValue value = { 0, };
GString *str;
g_return_if_fail (G_IS_OBJECT (object));
klass = G_OBJECT_GET_CLASS (object);
property_specs = g_object_class_list_properties (klass, &n_property_specs);
if (!property_specs)
return;
str = g_string_new (NULL);
for (i = 0; i < n_property_specs; i++)
{
GParamSpec *prop_spec;
const gchar *cstr;
gchar *str;
GValue value = { 0, };
prop_spec = property_specs[i];
if (! (prop_spec->flags & G_PARAM_READWRITE))
continue;
str = NULL;
cstr = NULL;
g_value_init (&value, prop_spec->value_type);
g_object_get_property (object, prop_spec->name, &value);
if (G_VALUE_HOLDS_BOOLEAN (&value))
g_string_assign (str, "(");
g_string_append (str, prop_spec->name);
if (gimpconfig_serialize_value (&value, str))
{
gboolean bool;
bool = g_value_get_boolean (&value);
cstr = bool ? "yes" : "no";
}
else if (G_VALUE_HOLDS_ENUM (&value))
{
GEnumClass *enum_class;
GEnumValue *enum_value;
enum_class = g_type_class_peek (prop_spec->value_type);
enum_value = g_enum_get_value (G_ENUM_CLASS (enum_class),
g_value_get_enum (&value));
if (enum_value && enum_value->value_nick)
cstr = enum_value->value_nick;
else
g_warning ("Couldn't get nick for enum_value of %s",
G_ENUM_CLASS_TYPE_NAME (enum_class));
g_string_append (str, ")\n");
write (fd, str->str, str->len);
}
else if (G_VALUE_HOLDS_STRING (&value))
{
cstr = g_value_get_string (&value);
if (cstr)
{
gchar *s = g_strescape (cstr, NULL);
str = g_strdup_printf ("\"%s\"", s);
g_free (s);
cstr = NULL;
}
}
else if (g_value_type_transformable (prop_spec->value_type,
G_TYPE_STRING))
{
GValue tmp_value = { 0, };
g_value_init (&tmp_value, G_TYPE_STRING);
g_value_transform (&value, &tmp_value);
str = g_value_dup_string (&tmp_value);
g_value_unset (&tmp_value);
}
else
else if (prop_spec->value_type != G_TYPE_STRING)
{
g_warning ("couldn't serialize property %s::%s of type %s",
g_type_name (G_TYPE_FROM_INSTANCE (object)),
......@@ -114,12 +90,104 @@ gimp_config_serialize_properties (GObject *object,
g_type_name (prop_spec->value_type));
}
if (cstr || str)
g_value_unset (&value);
}
g_free (property_specs);
g_string_free (str, TRUE);
}
static gboolean
gimpconfig_serialize_value (const GValue *value,
GString *str)
{
if (G_VALUE_HOLDS_BOOLEAN (value))
{
gboolean bool;
bool = g_value_get_boolean (value);
g_string_append (str, bool ? " yes" : " no");
return TRUE;
}
if (G_VALUE_HOLDS_ENUM (value))
{
GEnumClass *enum_class;
GEnumValue *enum_value;
enum_class = g_type_class_peek (G_VALUE_TYPE (value));
enum_value = g_enum_get_value (G_ENUM_CLASS (enum_class),
g_value_get_enum (value));
if (enum_value && enum_value->value_nick)
{
fprintf (file, "(%s %s)\n", prop_spec->name, cstr ? cstr : str);
g_free (str);
g_string_append_c (str, ' ');
g_string_append (str, enum_value->value_nick);
return TRUE;
}
else
{
g_warning ("Couldn't get nick for enum_value of %s",
G_ENUM_CLASS_TYPE_NAME (enum_class));
return FALSE;
}
}
if (G_VALUE_HOLDS_STRING (value))
{
gchar *escaped;
const gchar *cstr = g_value_get_string (value);
g_value_unset (&value);
if (!cstr)
return FALSE;
escaped = g_strescape (cstr, NULL);
g_string_append_printf (str, " \"%s\"", escaped);
g_free (escaped);
return TRUE;
}
if (g_value_type_transformable (G_VALUE_TYPE (value), G_TYPE_STRING))
{
GValue tmp_value = { 0, };
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);
return TRUE;
}
return FALSE;
}
void
gimp_config_serialize_unknown_tokens (GObject *object,
gint fd)
{
GString *str;
g_return_if_fail (G_IS_OBJECT (object));
str = g_string_new (NULL);
gimp_config_foreach_unknown_token (object, serialize_unknown_token, str);
write (fd, str->str, str->len);
}
static void
serialize_unknown_token (const gchar *key,
const gchar *value,
gpointer data)
{
gchar *escaped = g_strescape (value, NULL);
g_string_append_printf ((GString *) data, "(%s \"%s\")\n", key, escaped);
g_free (escaped);
}
......@@ -23,8 +23,10 @@
#define __GIMP_CONFIG_SERIALIZE_H__
void gimp_config_serialize_properties (GObject *object,
FILE *file);
void gimp_config_serialize_properties (GObject *object,
gint fd);
void gimp_config_serialize_unknown_tokens (GObject *object,
gint fd);
#endif /* __GIMP_CONFIG_SERIALIZE_H__ */
......@@ -32,6 +32,8 @@
#include "gimpconfig-substitute.h"
#define SUBSTS_ALLOC 4
static inline gchar * extract_token (const gchar **str);
......@@ -40,14 +42,14 @@ gimp_config_substitute_path (GObject *object,
const gchar *path,
gboolean use_env)
{
const gchar *s;
const gchar *p;
const gchar *s;
gchar *n;
gchar *token;
gchar *new_path = NULL;
gchar **substs = NULL;
gint n_substs = 0;
glong length = 0;
gchar *new_path = NULL;
gchar **substs = NULL;
guint n_substs = 0;
gint length = 0;
gint i;
g_return_val_if_fail (G_IS_OBJECT (object), NULL);
......@@ -117,7 +119,9 @@ gimp_config_substitute_path (GObject *object,
goto cleanup;
}
substs = g_renew (gchar *, substs, 2 * (n_substs + 1));
if (n_substs % SUBSTS_ALLOC == 0)
substs = g_renew (gchar *, substs, 2*(n_substs+SUBSTS_ALLOC));
substs[2*n_substs] = token;
substs[2*n_substs + 1] = (gchar *) s;
n_substs++;
......@@ -132,7 +136,7 @@ gimp_config_substitute_path (GObject *object,
}
}
if (!substs)
if (!n_substs)
return g_strdup (path);
new_path = g_new (gchar, length + 1);
......
......@@ -90,6 +90,7 @@ memsize_to_string (const GValue *src_value,
dest_value->data[0].v_pointer = g_strdup_printf ("%u", size);
};
static void
string_to_memsize (const GValue *src_value,
GValue *dest_value)
......
......@@ -21,8 +21,11 @@
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <glib-object.h>
......@@ -30,6 +33,8 @@
#include "gimpconfig-serialize.h"
#include "gimpconfig-deserialize.h"
#include "libgimp/gimpintl.h"
/*
* The GimpConfig serialization and deserialization interface.
......@@ -38,7 +43,8 @@
static void gimp_config_iface_init (GimpConfigInterface *gimp_config_iface);
static void gimp_config_iface_serialize (GObject *object,
FILE *file);
gint fd,
gboolean put_unknown);
static gboolean gimp_config_iface_deserialize (GObject *object,
GScanner *scanner,
gboolean store_unknown);
......@@ -78,10 +84,14 @@ gimp_config_iface_init (GimpConfigInterface *gimp_config_iface)
}
static void
gimp_config_iface_serialize (GObject *object,
FILE *file)
gimp_config_iface_serialize (GObject *object,
gint fd,
gboolean put_unknown)
{
gimp_config_serialize_properties (object, file);
if (put_unknown)
gimp_config_serialize_unknown_tokens (object, fd);
gimp_config_serialize_properties (object, fd);
}
static gboolean
......@@ -95,11 +105,12 @@ gimp_config_iface_deserialize (GObject *object,
}
gboolean
gimp_config_serialize (GObject *object,
const gchar *filename)
gimp_config_serialize (GObject *object,
const gchar *filename,
gboolean put_unknown)
{
GimpConfigInterface *gimp_config_iface;
FILE *file;
gint fd;
g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
......@@ -108,22 +119,26 @@ gimp_config_serialize (GObject *object,
g_return_val_if_fail (gimp_config_iface != NULL, FALSE);
file = fopen (filename, "w");
fd = open (filename, O_WRONLY | O_CREAT);
if (!file)
return FALSE;
if (fd == -1)
{
g_message (_("Failed to open file '%s': %s"),
filename, g_strerror (errno));
return FALSE;
}
gimp_config_iface->serialize (object, file);
gimp_config_iface->serialize (object, fd, put_unknown);
fclose (file);
close (fd);
return TRUE;
}
gboolean
gimp_config_deserialize (GObject *object,
const gchar *filename,
gboolean store_unknown)
gimp_config_deserialize (GObject *object,
const gchar *filename,
gboolean store_unknown)
{
GimpConfigInterface *gimp_config_iface;
gint fd;
......@@ -140,7 +155,11 @@ gimp_config_deserialize (GObject *object,
fd = open (filename, O_RDONLY);
if (fd == -1)
return FALSE;
{
g_message (_("Failed to open file '%s': %s"),
filename, g_strerror (errno));
return FALSE;
}
scanner = g_scanner_new (NULL);
......
......@@ -35,7 +35,8 @@ struct _GimpConfigInterface
GTypeInterface base_iface;
void (* serialize) (GObject *object,
FILE *fd);
gint fd,
gboolean put_unknown);
gboolean (* deserialize) (GObject *object,
GScanner *scanner,
gboolean store_unknown);
......@@ -49,7 +50,8 @@ typedef void (*GimpConfigForeachFunc) (const gchar *key,
GType gimp_config_interface_get_type (void) G_GNUC_CONST;
gboolean gimp_config_serialize (GObject *object,
const gchar *filename);
const gchar *filename,
gboolean put_unknown);
gboolean gimp_config_deserialize (GObject *object,
const gchar *filename,
gboolean store_unknown);
......
......@@ -64,11 +64,11 @@ enum
PROP_DEFAULT_PALETTE,
PROP_DEFAULT_GRADIENT,
PROP_DEFAULT_COMMENT,
PROP_DEFAULT_IMAGE_TYPE
PROP_DEFAULT_IMAGE_TYPE,
PROP_DEFAULT_IMAGE_WIDTH,
PROP_DEFAULT_IMAGE_HEIGHT,
};
static GObjectClass *parent_class = NULL;
GType
gimp_core_config_get_type (void)
......@@ -103,8 +103,6 @@ gimp_core_config_class_init (GimpCoreConfigClass *klass)
{
GObjectClass *object_class;
parent_class = g_type_class_peek_parent (klass);
object_class = G_OBJECT_CLASS (klass);
object_class->set_property = gimp_core_config_set_property;
......@@ -146,6 +144,12 @@ gimp_core_config_class_init (GimpCoreConfigClass *klass)
GIMP_CONFIG_INSTALL_PROP_ENUM (object_class, PROP_DEFAULT_IMAGE_TYPE,
"default-image-type",
GIMP_TYPE_IMAGE_BASE_TYPE, GIMP_RGB);
GIMP_CONFIG_INSTALL_PROP_INT (object_class, PROP_DEFAULT_IMAGE_WIDTH,
"default-image-width",
1, 0x8000, 256);
GIMP_CONFIG_INSTALL_PROP_INT (object_class, PROP_DEFAULT_IMAGE_HEIGHT,
"default-image-height",
1, 0x8000, 256);
}
static void
......@@ -207,6 +211,12 @@ gimp_core_config_set_property (GObject *object,