1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* Config file serialization and deserialization interface
5
* Copyright (C) 2001-2002 Sven Neumann <sven@gimp.org>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program 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
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
#include <glib-object.h>
28
#include "libgimpbase/gimpbase.h"
30
#include "config-types.h"
32
#include "gimpconfig.h"
33
#include "gimpconfig-deserialize.h"
34
#include "gimpconfig-serialize.h"
35
#include "gimpconfig-params.h"
36
#include "gimpconfig-utils.h"
37
#include "gimpconfigwriter.h"
38
#include "gimpscanner.h"
40
#include "gimp-intl.h"
44
* The GimpConfig serialization and deserialization interface.
47
static void gimp_config_iface_base_init (GimpConfigInterface *config_iface);
49
static gboolean gimp_config_iface_serialize (GimpConfig *config,
50
GimpConfigWriter *writer,
52
static gboolean gimp_config_iface_deserialize (GimpConfig *config,
56
static GimpConfig * gimp_config_iface_duplicate (GimpConfig *config);
57
static gboolean gimp_config_iface_equal (GimpConfig *a,
59
static void gimp_config_iface_reset (GimpConfig *config);
63
gimp_config_interface_get_type (void)
65
static GType config_iface_type = 0;
67
if (!config_iface_type)
69
static const GTypeInfo config_iface_info =
71
sizeof (GimpConfigInterface),
72
(GBaseInitFunc) gimp_config_iface_base_init,
73
(GBaseFinalizeFunc) NULL,
76
config_iface_type = g_type_register_static (G_TYPE_INTERFACE,
77
"GimpConfigInterface",
81
g_type_interface_add_prerequisite (config_iface_type, G_TYPE_OBJECT);
84
return config_iface_type;
88
gimp_config_iface_base_init (GimpConfigInterface *config_iface)
90
if (! config_iface->serialize)
92
config_iface->serialize = gimp_config_iface_serialize;
93
config_iface->deserialize = gimp_config_iface_deserialize;
94
config_iface->duplicate = gimp_config_iface_duplicate;
95
config_iface->equal = gimp_config_iface_equal;
96
config_iface->reset = gimp_config_iface_reset;
99
/* always set these to NULL since we don't want to inherit them
100
* from parent classes
102
config_iface->serialize_property = NULL;
103
config_iface->deserialize_property = NULL;
107
gimp_config_iface_serialize (GimpConfig *config,
108
GimpConfigWriter *writer,
111
return gimp_config_serialize_properties (config, writer);
115
gimp_config_iface_deserialize (GimpConfig *config,
120
return gimp_config_deserialize_properties (config,
121
scanner, nest_level, FALSE);
125
gimp_config_iface_duplicate (GimpConfig *config)
128
GParamSpec **property_specs;
129
guint n_property_specs;
130
GParameter *construct_params = NULL;
131
gint n_construct_params = 0;
135
klass = G_OBJECT_GET_CLASS (config);
137
property_specs = g_object_class_list_properties (klass, &n_property_specs);
139
construct_params = g_new0 (GParameter, n_property_specs);
141
for (i = 0; i < n_property_specs; i++)
143
GParamSpec *prop_spec = property_specs[i];
145
if ((prop_spec->flags & G_PARAM_READABLE) &&
146
(prop_spec->flags & G_PARAM_WRITABLE) &&
147
(prop_spec->flags & G_PARAM_CONSTRUCT_ONLY))
149
GParameter *construct_param;
151
construct_param = &construct_params[n_construct_params++];
153
construct_param->name = prop_spec->name;
155
g_value_init (&construct_param->value, prop_spec->value_type);
156
g_object_get_property (G_OBJECT (config), prop_spec->name,
157
&construct_param->value);
161
g_free (property_specs);
163
dup = g_object_newv (G_TYPE_FROM_INSTANCE (config),
164
n_construct_params, construct_params);
166
for (i = 0; i < n_construct_params; i++)
167
g_value_unset (&construct_params[i].value);
169
g_free (construct_params);
171
gimp_config_sync (config, dup, 0);
177
gimp_config_iface_equal (GimpConfig *a,
181
GParamSpec **property_specs;
182
guint n_property_specs;
184
gboolean equal = TRUE;
186
klass = G_OBJECT_GET_CLASS (a);
188
property_specs = g_object_class_list_properties (klass, &n_property_specs);
190
for (i = 0; equal && i < n_property_specs; i++)
192
GParamSpec *prop_spec;
193
GValue a_value = { 0, };
194
GValue b_value = { 0, };
196
prop_spec = property_specs[i];
198
if (! (prop_spec->flags & G_PARAM_READABLE))
201
g_value_init (&a_value, prop_spec->value_type);
202
g_value_init (&b_value, prop_spec->value_type);
203
g_object_get_property (G_OBJECT (a), prop_spec->name, &a_value);
204
g_object_get_property (G_OBJECT (b), prop_spec->name, &b_value);
206
if (g_param_values_cmp (prop_spec, &a_value, &b_value))
208
if ((prop_spec->flags & GIMP_PARAM_AGGREGATE) &&
209
G_IS_PARAM_SPEC_OBJECT (prop_spec) &&
210
g_type_interface_peek (g_type_class_peek (prop_spec->value_type),
213
if (! gimp_config_is_equal_to (g_value_get_object (&a_value),
214
g_value_get_object (&b_value)))
225
g_value_unset (&a_value);
226
g_value_unset (&b_value);
229
g_free (property_specs);
235
gimp_config_iface_reset (GimpConfig *config)
237
gimp_config_reset_properties (config);
241
* gimp_config_serialize_to_file:
242
* @config: a #GObject that implements the #GimpConfigInterface.
243
* @filename: the name of the file to write the configuration to.
244
* @header: optional file header (must be ASCII only)
245
* @footer: optional file footer (must be ASCII only)
246
* @data: user data passed to the serialize implementation.
249
* Serializes the object properties of @config to the file specified
250
* by @filename. If a file with that name already exists, it is
251
* overwritten. Basically this function opens @filename for you and
252
* calls the serialize function of the @config's #GimpConfigInterface.
254
* Return value: %TRUE if serialization succeeded, %FALSE otherwise.
257
gimp_config_serialize_to_file (GimpConfig *config,
258
const gchar *filename,
264
GimpConfigWriter *writer;
266
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
267
g_return_val_if_fail (filename != NULL, FALSE);
268
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
270
writer = gimp_config_writer_new_file (filename, TRUE, header, error);
274
GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
276
return gimp_config_writer_finish (writer, footer, error);
280
gimp_config_serialize_to_fd (GimpConfig *config,
284
GimpConfigWriter *writer;
286
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
287
g_return_val_if_fail (fd > 0, FALSE);
289
writer = gimp_config_writer_new_fd (fd);
293
GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
295
return gimp_config_writer_finish (writer, NULL, NULL);
299
* gimp_config_serialize_to_string:
300
* @config: a #GObject that implements the #GimpConfigInterface.
301
* @data: user data passed to the serialize implementation.
303
* Serializes the object properties of @config to a string.
305
* Return value: a newly allocated %NUL-terminated string.
308
gimp_config_serialize_to_string (GimpConfig *config,
311
GimpConfigWriter *writer;
314
g_return_val_if_fail (GIMP_IS_CONFIG (config), NULL);
316
str = g_string_new (NULL);
317
writer = gimp_config_writer_new_string (str);
319
GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
321
gimp_config_writer_finish (writer, NULL, NULL);
323
return g_string_free (str, FALSE);
327
* gimp_config_deserialize:
328
* @config: a #GObject that implements the #GimpConfigInterface.
329
* @filename: the name of the file to read configuration from.
330
* @data: user data passed to the deserialize implementation.
333
* Opens the file specified by @filename, reads configuration data
334
* from it and configures @config accordingly. Basically this function
335
* creates a properly configured #GScanner for you and calls the
336
* deserialize function of the @config's #GimpConfigInterface.
338
* Return value: %TRUE if deserialization succeeded, %FALSE otherwise.
341
gimp_config_deserialize_file (GimpConfig *config,
342
const gchar *filename,
349
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
350
g_return_val_if_fail (filename != NULL, FALSE);
351
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
353
scanner = gimp_scanner_new_file (filename, error);
357
success = GIMP_CONFIG_GET_INTERFACE (config)->deserialize (config,
360
gimp_scanner_destroy (scanner);
363
g_assert (error == NULL || *error != NULL);
369
* gimp_config_deserialize_string:
370
* @config: a #GObject that implements the #GimpConfigInterface.
371
* @text: string to deserialize (in UTF-8 encoding)
372
* @text_len: length of @text in bytes or -1
376
* Configures @config from @text. Basically this function creates a
377
* properly configured #GScanner for you and calls the deserialize
378
* function of the @config's #GimpConfigInterface.
380
* Returns: %TRUE if deserialization succeeded, %FALSE otherwise.
383
gimp_config_deserialize_string (GimpConfig *config,
392
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
393
g_return_val_if_fail (text != NULL || text_len == 0, FALSE);
394
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
396
scanner = gimp_scanner_new_string (text, text_len, error);
398
success = GIMP_CONFIG_GET_INTERFACE (config)->deserialize (config,
401
gimp_scanner_destroy (scanner);
404
g_assert (error == NULL || *error != NULL);
410
gimp_config_deserialize_return (GScanner *scanner,
411
GTokenType expected_token,
414
GTokenType next_token;
416
g_return_val_if_fail (scanner != NULL, FALSE);
418
next_token = g_scanner_peek_next_token (scanner);
420
if (expected_token != G_TOKEN_LEFT_PAREN)
422
g_scanner_get_next_token (scanner);
423
g_scanner_unexp_token (scanner, expected_token, NULL, NULL, NULL,
424
_("fatal parse error"), TRUE);
429
if (nest_level > 0 && next_token == G_TOKEN_RIGHT_PAREN)
433
else if (next_token != G_TOKEN_EOF)
435
g_scanner_get_next_token (scanner);
436
g_scanner_unexp_token (scanner, expected_token, NULL, NULL, NULL,
437
_("fatal parse error"), TRUE);
447
* gimp_config_duplicate:
448
* @config: a #GObject that implements the #GimpConfigInterface.
450
* Creates a copy of the passed object by copying all object
451
* properties. The default implementation of the #GimpConfigInterface
452
* only works for objects that are completely defined by their
455
* Return value: the duplicated #GimpConfig object
458
gimp_config_duplicate (GimpConfig *config)
460
g_return_val_if_fail (GIMP_IS_CONFIG (config), NULL);
462
return GIMP_CONFIG_GET_INTERFACE (config)->duplicate (config);
466
* gimp_config_is_equal_to:
467
* @a: a #GObject that implements the #GimpConfigInterface.
468
* @b: another #GObject of the same type as @a.
470
* Compares the two objects. The default implementation of the
471
* #GimpConfigInterface compares the object properties and thus only
472
* works for objects that are completely defined by their
475
* Return value: %TRUE if the two objects are equal.
478
gimp_config_is_equal_to (GimpConfig *a,
481
g_return_val_if_fail (GIMP_IS_CONFIG (a), FALSE);
482
g_return_val_if_fail (GIMP_IS_CONFIG (b), FALSE);
483
g_return_val_if_fail (G_TYPE_FROM_INSTANCE (a) == G_TYPE_FROM_INSTANCE (b),
486
return GIMP_CONFIG_GET_INTERFACE (a)->equal (a, b);
491
* @config: a #GObject that implements the #GimpConfigInterface.
493
* Resets the object to its default state. The default implementation of the
494
* #GimpConfigInterface only works for objects that are completely defined by
498
gimp_config_reset (GimpConfig *config)
500
g_return_if_fail (GIMP_IS_CONFIG (config));
502
GIMP_CONFIG_GET_INTERFACE (config)->reset (config);
507
* Code to store and lookup unknown tokens (string key/value pairs).
510
#define GIMP_CONFIG_UNKNOWN_TOKENS "gimp-config-unknown-tokens"
518
static void gimp_config_destroy_unknown_tokens (GSList *unknown_tokens);
522
* gimp_config_add_unknown_token:
523
* @config: a #GObject.
524
* @key: a nul-terminated string to identify the value.
525
* @value: a nul-terminated string representing the value.
527
* This function allows to add arbitrary key/value pairs to a GObject.
528
* It's purpose is to attach additional data to a #GimpConfig object
529
* that can be stored along with the object properties when
530
* serializing the object to a configuration file. Please note however
531
* that the default gimp_config_serialize() implementation does not
532
* serialize unknown tokens.
534
* If you want to remove a key/value pair from the object, call this
535
* function with a %NULL @value.
538
gimp_config_add_unknown_token (GimpConfig *config,
542
GimpConfigToken *token;
543
GSList *unknown_tokens;
547
g_return_if_fail (GIMP_IS_CONFIG (config));
548
g_return_if_fail (key != NULL);
550
unknown_tokens = (GSList *) g_object_get_data (G_OBJECT (config),
551
GIMP_CONFIG_UNKNOWN_TOKENS);
553
for (last = NULL, list = unknown_tokens;
555
last = list, list = g_slist_next (list))
557
token = (GimpConfigToken *) list->data;
559
if (strcmp (token->key, key) == 0)
561
g_free (token->value);
565
token->value = g_strdup (value);
571
unknown_tokens = g_slist_remove (unknown_tokens, token);
572
g_object_set_data_full (G_OBJECT (config),
573
GIMP_CONFIG_UNKNOWN_TOKENS,
575
(GDestroyNotify) gimp_config_destroy_unknown_tokens);
585
token = g_new (GimpConfigToken, 1);
586
token->key = g_strdup (key);
587
token->value = g_strdup (value);
591
g_slist_append (last, token);
595
unknown_tokens = g_slist_append (NULL, token);
597
g_object_set_data_full (G_OBJECT (config),
598
GIMP_CONFIG_UNKNOWN_TOKENS,
600
(GDestroyNotify) gimp_config_destroy_unknown_tokens);
605
* gimp_config_lookup_unknown_token:
606
* @config: a #GObject.
607
* @key: a nul-terminated string to identify the value.
609
* This function retrieves data that was previously attached using
610
* gimp_config_add_unknown_token(). You should not free or modify
611
* the returned string.
613
* Returns: a pointer to a constant string.
616
gimp_config_lookup_unknown_token (GimpConfig *config,
619
GimpConfigToken *token;
620
GSList *unknown_tokens;
623
g_return_val_if_fail (GIMP_IS_CONFIG (config), NULL);
624
g_return_val_if_fail (key != NULL, NULL);
626
unknown_tokens = (GSList *) g_object_get_data (G_OBJECT (config),
627
GIMP_CONFIG_UNKNOWN_TOKENS);
629
for (list = unknown_tokens; list; list = g_slist_next (list))
631
token = (GimpConfigToken *) list->data;
633
if (strcmp (token->key, key) == 0)
641
* gimp_config_foreach_unknown_token:
642
* @config: a #GObject.
643
* @func: a function to call for each key/value pair.
644
* @user_data: data to pass to @func.
646
* Calls @func for each key/value stored with the @config using
647
* gimp_config_add_unknown_token().
650
gimp_config_foreach_unknown_token (GimpConfig *config,
651
GimpConfigForeachFunc func,
654
GimpConfigToken *token;
655
GSList *unknown_tokens;
658
g_return_if_fail (GIMP_IS_CONFIG (config));
659
g_return_if_fail (func != NULL);
661
unknown_tokens = (GSList *) g_object_get_data (G_OBJECT (config),
662
GIMP_CONFIG_UNKNOWN_TOKENS);
664
for (list = unknown_tokens; list; list = g_slist_next (list))
666
token = (GimpConfigToken *) list->data;
668
func (token->key, token->value, user_data);
673
gimp_config_destroy_unknown_tokens (GSList *unknown_tokens)
675
GimpConfigToken *token;
678
for (list = unknown_tokens; list; list = g_slist_next (list))
680
token = (GimpConfigToken *) list->data;
683
g_free (token->value);
687
g_slist_free (unknown_tokens);