1
/* LIBGIMP - The GIMP Library
2
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
4
* Config file serialization and deserialization interface
5
* Copyright (C) 2001-2002 Sven Neumann <sven@gimp.org>
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Library General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the
19
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
* Boston, MA 02111-1307, USA.
27
#include <glib-object.h>
29
#include "libgimpbase/gimpbase.h"
31
#include "gimpconfigtypes.h"
33
#include "gimpconfigwriter.h"
34
#include "gimpconfig-iface.h"
35
#include "gimpconfig-deserialize.h"
36
#include "gimpconfig-serialize.h"
37
#include "gimpconfig-params.h"
38
#include "gimpconfig-utils.h"
39
#include "gimpscanner.h"
41
#include "libgimp/libgimp-intl.h"
45
* The GimpConfig serialization and deserialization interface.
48
static void gimp_config_iface_base_init (GimpConfigInterface *config_iface);
50
static gboolean gimp_config_iface_serialize (GimpConfig *config,
51
GimpConfigWriter *writer,
53
static gboolean gimp_config_iface_deserialize (GimpConfig *config,
57
static GimpConfig * gimp_config_iface_duplicate (GimpConfig *config);
58
static gboolean gimp_config_iface_equal (GimpConfig *a,
60
static void gimp_config_iface_reset (GimpConfig *config);
64
gimp_config_interface_get_type (void)
66
static GType config_iface_type = 0;
68
if (! config_iface_type)
70
const GTypeInfo config_iface_info =
72
sizeof (GimpConfigInterface),
73
(GBaseInitFunc) gimp_config_iface_base_init,
74
(GBaseFinalizeFunc) NULL,
77
config_iface_type = g_type_register_static (G_TYPE_INTERFACE,
78
"GimpConfigInterface",
82
g_type_interface_add_prerequisite (config_iface_type, G_TYPE_OBJECT);
85
return config_iface_type;
89
gimp_config_iface_base_init (GimpConfigInterface *config_iface)
91
if (! config_iface->serialize)
93
config_iface->serialize = gimp_config_iface_serialize;
94
config_iface->deserialize = gimp_config_iface_deserialize;
95
config_iface->duplicate = gimp_config_iface_duplicate;
96
config_iface->equal = gimp_config_iface_equal;
97
config_iface->reset = gimp_config_iface_reset;
100
/* always set these to NULL since we don't want to inherit them
101
* from parent classes
103
config_iface->serialize_property = NULL;
104
config_iface->deserialize_property = NULL;
108
gimp_config_iface_serialize (GimpConfig *config,
109
GimpConfigWriter *writer,
112
return gimp_config_serialize_properties (config, writer);
116
gimp_config_iface_deserialize (GimpConfig *config,
121
return gimp_config_deserialize_properties (config, scanner, nest_level);
125
gimp_config_iface_duplicate (GimpConfig *config)
127
GObject *object = G_OBJECT (config);
128
GObjectClass *klass = G_OBJECT_GET_CLASS (object);
129
GParamSpec **property_specs;
130
guint n_property_specs;
131
GParameter *construct_params = NULL;
132
gint n_construct_params = 0;
136
property_specs = g_object_class_list_properties (klass, &n_property_specs);
138
construct_params = g_new0 (GParameter, n_property_specs);
140
for (i = 0; i < n_property_specs; i++)
142
GParamSpec *prop_spec = property_specs[i];
144
if ((prop_spec->flags & G_PARAM_READABLE) &&
145
(prop_spec->flags & G_PARAM_WRITABLE) &&
146
(prop_spec->flags & G_PARAM_CONSTRUCT_ONLY))
148
GParameter *construct_param;
150
construct_param = &construct_params[n_construct_params++];
152
construct_param->name = prop_spec->name;
154
g_value_init (&construct_param->value, prop_spec->value_type);
155
g_object_get_property (object,
156
prop_spec->name, &construct_param->value);
160
g_free (property_specs);
162
dup = g_object_newv (G_TYPE_FROM_INSTANCE (object),
163
n_construct_params, construct_params);
165
for (i = 0; i < n_construct_params; i++)
166
g_value_unset (&construct_params[i].value);
168
g_free (construct_params);
170
gimp_config_sync (object, dup, 0);
172
return GIMP_CONFIG (dup);
176
gimp_config_iface_equal (GimpConfig *a,
180
GParamSpec **property_specs;
181
guint n_property_specs;
183
gboolean equal = TRUE;
185
klass = G_OBJECT_GET_CLASS (a);
187
property_specs = g_object_class_list_properties (klass, &n_property_specs);
189
for (i = 0; equal && i < n_property_specs; i++)
191
GParamSpec *prop_spec;
192
GValue a_value = { 0, };
193
GValue b_value = { 0, };
195
prop_spec = property_specs[i];
197
if (! (prop_spec->flags & G_PARAM_READABLE))
200
g_value_init (&a_value, prop_spec->value_type);
201
g_value_init (&b_value, prop_spec->value_type);
202
g_object_get_property (G_OBJECT (a), prop_spec->name, &a_value);
203
g_object_get_property (G_OBJECT (b), prop_spec->name, &b_value);
205
if (g_param_values_cmp (prop_spec, &a_value, &b_value))
207
if ((prop_spec->flags & GIMP_CONFIG_PARAM_AGGREGATE) &&
208
G_IS_PARAM_SPEC_OBJECT (prop_spec) &&
209
g_type_interface_peek (g_type_class_peek (prop_spec->value_type),
212
if (! gimp_config_is_equal_to (g_value_get_object (&a_value),
213
g_value_get_object (&b_value)))
224
g_value_unset (&a_value);
225
g_value_unset (&b_value);
228
g_free (property_specs);
234
gimp_config_iface_reset (GimpConfig *config)
236
gimp_config_reset_properties (G_OBJECT (config));
240
* gimp_config_serialize_to_file:
241
* @config: a #GObject that implements the #GimpConfigInterface.
242
* @filename: the name of the file to write the configuration to.
243
* @header: optional file header (must be ASCII only)
244
* @footer: optional file footer (must be ASCII only)
245
* @data: user data passed to the serialize implementation.
248
* Serializes the object properties of @config to the file specified
249
* by @filename. If a file with that name already exists, it is
250
* overwritten. Basically this function opens @filename for you and
251
* calls the serialize function of the @config's #GimpConfigInterface.
253
* Return value: %TRUE if serialization succeeded, %FALSE otherwise.
258
gimp_config_serialize_to_file (GimpConfig *config,
259
const gchar *filename,
265
GimpConfigWriter *writer;
267
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
268
g_return_val_if_fail (filename != NULL, FALSE);
269
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
271
writer = gimp_config_writer_new_file (filename, TRUE, header, error);
275
GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
277
return gimp_config_writer_finish (writer, footer, error);
281
* gimp_config_serialize_to_fd:
282
* @config: a #GObject that implements the #GimpConfigInterface.
283
* @fd: a file descriptor, opened for writing
284
* @data: user data passed to the serialize implementation.
286
* Serializes the object properties of @config to the given file
289
* Return value: %TRUE if serialization succeeded, %FALSE otherwise.
294
gimp_config_serialize_to_fd (GimpConfig *config,
298
GimpConfigWriter *writer;
300
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
301
g_return_val_if_fail (fd > 0, FALSE);
303
writer = gimp_config_writer_new_fd (fd);
307
GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
309
return gimp_config_writer_finish (writer, NULL, NULL);
313
* gimp_config_serialize_to_string:
314
* @config: a #GObject that implements the #GimpConfigInterface.
315
* @data: user data passed to the serialize implementation.
317
* Serializes the object properties of @config to a string.
319
* Return value: a newly allocated %NUL-terminated string.
324
gimp_config_serialize_to_string (GimpConfig *config,
327
GimpConfigWriter *writer;
330
g_return_val_if_fail (GIMP_IS_CONFIG (config), NULL);
332
str = g_string_new (NULL);
333
writer = gimp_config_writer_new_string (str);
335
GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
337
gimp_config_writer_finish (writer, NULL, NULL);
339
return g_string_free (str, FALSE);
343
* gimp_config_deserialize:
344
* @config: a #GObject that implements the #GimpConfigInterface.
345
* @filename: the name of the file to read configuration from.
346
* @data: user data passed to the deserialize implementation.
349
* Opens the file specified by @filename, reads configuration data
350
* from it and configures @config accordingly. Basically this function
351
* creates a properly configured #GScanner for you and calls the
352
* deserialize function of the @config's #GimpConfigInterface.
354
* Return value: %TRUE if deserialization succeeded, %FALSE otherwise.
359
gimp_config_deserialize_file (GimpConfig *config,
360
const gchar *filename,
367
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
368
g_return_val_if_fail (filename != NULL, FALSE);
369
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
371
scanner = gimp_scanner_new_file (filename, error);
375
success = GIMP_CONFIG_GET_INTERFACE (config)->deserialize (config,
378
gimp_scanner_destroy (scanner);
381
g_assert (error == NULL || *error != NULL);
387
* gimp_config_deserialize_string:
388
* @config: a #GObject that implements the #GimpConfigInterface.
389
* @text: string to deserialize (in UTF-8 encoding)
390
* @text_len: length of @text in bytes or -1
394
* Configures @config from @text. Basically this function creates a
395
* properly configured #GScanner for you and calls the deserialize
396
* function of the @config's #GimpConfigInterface.
398
* Returns: %TRUE if deserialization succeeded, %FALSE otherwise.
403
gimp_config_deserialize_string (GimpConfig *config,
412
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
413
g_return_val_if_fail (text != NULL || text_len == 0, FALSE);
414
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
416
scanner = gimp_scanner_new_string (text, text_len, error);
418
success = GIMP_CONFIG_GET_INTERFACE (config)->deserialize (config,
421
gimp_scanner_destroy (scanner);
424
g_assert (error == NULL || *error != NULL);
430
* gimp_config_deserialize_return:
440
gimp_config_deserialize_return (GScanner *scanner,
441
GTokenType expected_token,
444
GTokenType next_token;
446
g_return_val_if_fail (scanner != NULL, FALSE);
448
next_token = g_scanner_peek_next_token (scanner);
450
if (expected_token != G_TOKEN_LEFT_PAREN)
452
g_scanner_get_next_token (scanner);
453
g_scanner_unexp_token (scanner, expected_token, NULL, NULL, NULL,
454
_("fatal parse error"), TRUE);
459
if (nest_level > 0 && next_token == G_TOKEN_RIGHT_PAREN)
463
else if (next_token != G_TOKEN_EOF)
465
g_scanner_get_next_token (scanner);
466
g_scanner_unexp_token (scanner, expected_token, NULL, NULL, NULL,
467
_("fatal parse error"), TRUE);
477
* gimp_config_duplicate:
478
* @config: a #GObject that implements the #GimpConfigInterface.
480
* Creates a copy of the passed object by copying all object
481
* properties. The default implementation of the #GimpConfigInterface
482
* only works for objects that are completely defined by their
485
* Return value: the duplicated #GimpConfig object
490
gimp_config_duplicate (GimpConfig *config)
492
g_return_val_if_fail (GIMP_IS_CONFIG (config), NULL);
494
return GIMP_CONFIG_GET_INTERFACE (config)->duplicate (config);
498
* gimp_config_is_equal_to:
499
* @a: a #GObject that implements the #GimpConfigInterface.
500
* @b: another #GObject of the same type as @a.
502
* Compares the two objects. The default implementation of the
503
* #GimpConfigInterface compares the object properties and thus only
504
* works for objects that are completely defined by their
507
* Return value: %TRUE if the two objects are equal.
512
gimp_config_is_equal_to (GimpConfig *a,
515
g_return_val_if_fail (GIMP_IS_CONFIG (a), FALSE);
516
g_return_val_if_fail (GIMP_IS_CONFIG (b), FALSE);
517
g_return_val_if_fail (G_TYPE_FROM_INSTANCE (a) == G_TYPE_FROM_INSTANCE (b),
520
return GIMP_CONFIG_GET_INTERFACE (a)->equal (a, b);
525
* @config: a #GObject that implements the #GimpConfigInterface.
527
* Resets the object to its default state. The default implementation of the
528
* #GimpConfigInterface only works for objects that are completely defined by
534
gimp_config_reset (GimpConfig *config)
536
g_return_if_fail (GIMP_IS_CONFIG (config));
538
GIMP_CONFIG_GET_INTERFACE (config)->reset (config);