~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to libgimpconfig/gimpconfig-iface.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LIBGIMP - The GIMP Library
 
2
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * Config file serialization and deserialization interface
 
5
 * Copyright (C) 2001-2002  Sven Neumann <sven@gimp.org>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <string.h>
 
26
 
 
27
#include <glib-object.h>
 
28
 
 
29
#include "libgimpbase/gimpbase.h"
 
30
 
 
31
#include "gimpconfigtypes.h"
 
32
 
 
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"
 
40
 
 
41
#include "libgimp/libgimp-intl.h"
 
42
 
 
43
 
 
44
/*
 
45
 * The GimpConfig serialization and deserialization interface.
 
46
 */
 
47
 
 
48
static void         gimp_config_iface_base_init (GimpConfigInterface  *config_iface);
 
49
 
 
50
static gboolean     gimp_config_iface_serialize   (GimpConfig       *config,
 
51
                                                   GimpConfigWriter *writer,
 
52
                                                   gpointer          data);
 
53
static gboolean     gimp_config_iface_deserialize (GimpConfig       *config,
 
54
                                                   GScanner         *scanner,
 
55
                                                   gint              nest_level,
 
56
                                                   gpointer          data);
 
57
static GimpConfig * gimp_config_iface_duplicate   (GimpConfig       *config);
 
58
static gboolean     gimp_config_iface_equal       (GimpConfig       *a,
 
59
                                                   GimpConfig       *b);
 
60
static void         gimp_config_iface_reset       (GimpConfig       *config);
 
61
 
 
62
 
 
63
GType
 
64
gimp_config_interface_get_type (void)
 
65
{
 
66
  static GType config_iface_type = 0;
 
67
 
 
68
  if (! config_iface_type)
 
69
    {
 
70
      const GTypeInfo config_iface_info =
 
71
      {
 
72
        sizeof (GimpConfigInterface),
 
73
        (GBaseInitFunc)     gimp_config_iface_base_init,
 
74
        (GBaseFinalizeFunc) NULL,
 
75
      };
 
76
 
 
77
      config_iface_type = g_type_register_static (G_TYPE_INTERFACE,
 
78
                                                  "GimpConfigInterface",
 
79
                                                  &config_iface_info,
 
80
                                                  0);
 
81
 
 
82
      g_type_interface_add_prerequisite (config_iface_type, G_TYPE_OBJECT);
 
83
    }
 
84
 
 
85
  return config_iface_type;
 
86
}
 
87
 
 
88
static void
 
89
gimp_config_iface_base_init (GimpConfigInterface *config_iface)
 
90
{
 
91
  if (! config_iface->serialize)
 
92
    {
 
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;
 
98
    }
 
99
 
 
100
  /*  always set these to NULL since we don't want to inherit them
 
101
   *  from parent classes
 
102
   */
 
103
  config_iface->serialize_property   = NULL;
 
104
  config_iface->deserialize_property = NULL;
 
105
}
 
106
 
 
107
static gboolean
 
108
gimp_config_iface_serialize (GimpConfig       *config,
 
109
                             GimpConfigWriter *writer,
 
110
                             gpointer          data)
 
111
{
 
112
  return gimp_config_serialize_properties (config, writer);
 
113
}
 
114
 
 
115
static gboolean
 
116
gimp_config_iface_deserialize (GimpConfig *config,
 
117
                               GScanner   *scanner,
 
118
                               gint        nest_level,
 
119
                               gpointer    data)
 
120
{
 
121
  return gimp_config_deserialize_properties (config, scanner, nest_level);
 
122
}
 
123
 
 
124
static GimpConfig *
 
125
gimp_config_iface_duplicate (GimpConfig *config)
 
126
{
 
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;
 
133
  guint          i;
 
134
  GObject       *dup;
 
135
 
 
136
  property_specs = g_object_class_list_properties (klass, &n_property_specs);
 
137
 
 
138
  construct_params = g_new0 (GParameter, n_property_specs);
 
139
 
 
140
  for (i = 0; i < n_property_specs; i++)
 
141
    {
 
142
      GParamSpec *prop_spec = property_specs[i];
 
143
 
 
144
      if ((prop_spec->flags & G_PARAM_READABLE) &&
 
145
          (prop_spec->flags & G_PARAM_WRITABLE) &&
 
146
          (prop_spec->flags & G_PARAM_CONSTRUCT_ONLY))
 
147
        {
 
148
          GParameter *construct_param;
 
149
 
 
150
          construct_param = &construct_params[n_construct_params++];
 
151
 
 
152
          construct_param->name = prop_spec->name;
 
153
 
 
154
          g_value_init (&construct_param->value, prop_spec->value_type);
 
155
          g_object_get_property (object,
 
156
                                 prop_spec->name, &construct_param->value);
 
157
        }
 
158
    }
 
159
 
 
160
  g_free (property_specs);
 
161
 
 
162
  dup = g_object_newv (G_TYPE_FROM_INSTANCE (object),
 
163
                       n_construct_params, construct_params);
 
164
 
 
165
  for (i = 0; i < n_construct_params; i++)
 
166
    g_value_unset (&construct_params[i].value);
 
167
 
 
168
  g_free (construct_params);
 
169
 
 
170
  gimp_config_sync (object, dup, 0);
 
171
 
 
172
  return GIMP_CONFIG (dup);
 
173
}
 
174
 
 
175
static gboolean
 
176
gimp_config_iface_equal (GimpConfig *a,
 
177
                         GimpConfig *b)
 
178
{
 
179
  GObjectClass  *klass;
 
180
  GParamSpec   **property_specs;
 
181
  guint          n_property_specs;
 
182
  guint          i;
 
183
  gboolean       equal = TRUE;
 
184
 
 
185
  klass = G_OBJECT_GET_CLASS (a);
 
186
 
 
187
  property_specs = g_object_class_list_properties (klass, &n_property_specs);
 
188
 
 
189
  for (i = 0; equal && i < n_property_specs; i++)
 
190
    {
 
191
      GParamSpec  *prop_spec;
 
192
      GValue       a_value = { 0, };
 
193
      GValue       b_value = { 0, };
 
194
 
 
195
      prop_spec = property_specs[i];
 
196
 
 
197
      if (! (prop_spec->flags & G_PARAM_READABLE))
 
198
        continue;
 
199
 
 
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);
 
204
 
 
205
      if (g_param_values_cmp (prop_spec, &a_value, &b_value))
 
206
        {
 
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),
 
210
                                     GIMP_TYPE_CONFIG))
 
211
            {
 
212
              if (! gimp_config_is_equal_to (g_value_get_object (&a_value),
 
213
                                             g_value_get_object (&b_value)))
 
214
                {
 
215
                  equal = FALSE;
 
216
                }
 
217
            }
 
218
          else
 
219
            {
 
220
              equal = FALSE;
 
221
            }
 
222
        }
 
223
 
 
224
      g_value_unset (&a_value);
 
225
      g_value_unset (&b_value);
 
226
    }
 
227
 
 
228
  g_free (property_specs);
 
229
 
 
230
  return equal;
 
231
}
 
232
 
 
233
static void
 
234
gimp_config_iface_reset (GimpConfig *config)
 
235
{
 
236
  gimp_config_reset_properties (G_OBJECT (config));
 
237
}
 
238
 
 
239
/**
 
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.
 
246
 * @error:
 
247
 *
 
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.
 
252
 *
 
253
 * Return value: %TRUE if serialization succeeded, %FALSE otherwise.
 
254
 *
 
255
 * Since: GIMP 2.4
 
256
 **/
 
257
gboolean
 
258
gimp_config_serialize_to_file (GimpConfig   *config,
 
259
                               const gchar  *filename,
 
260
                               const gchar  *header,
 
261
                               const gchar  *footer,
 
262
                               gpointer      data,
 
263
                               GError      **error)
 
264
{
 
265
  GimpConfigWriter *writer;
 
266
 
 
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);
 
270
 
 
271
  writer = gimp_config_writer_new_file (filename, TRUE, header, error);
 
272
  if (!writer)
 
273
    return FALSE;
 
274
 
 
275
  GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
 
276
 
 
277
  return gimp_config_writer_finish (writer, footer, error);
 
278
}
 
279
 
 
280
/**
 
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.
 
285
 *
 
286
 * Serializes the object properties of @config to the given file
 
287
 * descriptor.
 
288
 *
 
289
 * Return value: %TRUE if serialization succeeded, %FALSE otherwise.
 
290
 *
 
291
 * Since: GIMP 2.4
 
292
 **/
 
293
gboolean
 
294
gimp_config_serialize_to_fd (GimpConfig *config,
 
295
                             gint        fd,
 
296
                             gpointer    data)
 
297
{
 
298
  GimpConfigWriter *writer;
 
299
 
 
300
  g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
 
301
  g_return_val_if_fail (fd > 0, FALSE);
 
302
 
 
303
  writer = gimp_config_writer_new_fd (fd);
 
304
  if (!writer)
 
305
    return FALSE;
 
306
 
 
307
  GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
 
308
 
 
309
  return gimp_config_writer_finish (writer, NULL, NULL);
 
310
}
 
311
 
 
312
/**
 
313
 * gimp_config_serialize_to_string:
 
314
 * @config: a #GObject that implements the #GimpConfigInterface.
 
315
 * @data: user data passed to the serialize implementation.
 
316
 *
 
317
 * Serializes the object properties of @config to a string.
 
318
 *
 
319
 * Return value: a newly allocated %NUL-terminated string.
 
320
 *
 
321
 * Since: GIMP 2.4
 
322
 **/
 
323
gchar *
 
324
gimp_config_serialize_to_string (GimpConfig *config,
 
325
                                 gpointer    data)
 
326
{
 
327
  GimpConfigWriter *writer;
 
328
  GString          *str;
 
329
 
 
330
  g_return_val_if_fail (GIMP_IS_CONFIG (config), NULL);
 
331
 
 
332
  str = g_string_new (NULL);
 
333
  writer = gimp_config_writer_new_string (str);
 
334
 
 
335
  GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
 
336
 
 
337
  gimp_config_writer_finish (writer, NULL, NULL);
 
338
 
 
339
  return g_string_free (str, FALSE);
 
340
}
 
341
 
 
342
/**
 
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.
 
347
 * @error:
 
348
 *
 
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.
 
353
 *
 
354
 * Return value: %TRUE if deserialization succeeded, %FALSE otherwise.
 
355
 *
 
356
 * Since: GIMP 2.4
 
357
 **/
 
358
gboolean
 
359
gimp_config_deserialize_file (GimpConfig   *config,
 
360
                              const gchar  *filename,
 
361
                              gpointer      data,
 
362
                              GError      **error)
 
363
{
 
364
  GScanner *scanner;
 
365
  gboolean  success;
 
366
 
 
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);
 
370
 
 
371
  scanner = gimp_scanner_new_file (filename, error);
 
372
  if (! scanner)
 
373
    return FALSE;
 
374
 
 
375
  success = GIMP_CONFIG_GET_INTERFACE (config)->deserialize (config,
 
376
                                                             scanner, 0, data);
 
377
 
 
378
  gimp_scanner_destroy (scanner);
 
379
 
 
380
  if (! success)
 
381
    g_assert (error == NULL || *error != NULL);
 
382
 
 
383
  return success;
 
384
}
 
385
 
 
386
/**
 
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
 
391
 * @data:
 
392
 * @error:
 
393
 *
 
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.
 
397
 *
 
398
 * Returns: %TRUE if deserialization succeeded, %FALSE otherwise.
 
399
 *
 
400
 * Since: GIMP 2.4
 
401
 **/
 
402
gboolean
 
403
gimp_config_deserialize_string (GimpConfig      *config,
 
404
                                const gchar  *text,
 
405
                                gint          text_len,
 
406
                                gpointer      data,
 
407
                                GError      **error)
 
408
{
 
409
  GScanner *scanner;
 
410
  gboolean  success;
 
411
 
 
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);
 
415
 
 
416
  scanner = gimp_scanner_new_string (text, text_len, error);
 
417
 
 
418
  success = GIMP_CONFIG_GET_INTERFACE (config)->deserialize (config,
 
419
                                                             scanner, 0, data);
 
420
 
 
421
  gimp_scanner_destroy (scanner);
 
422
 
 
423
  if (! success)
 
424
    g_assert (error == NULL || *error != NULL);
 
425
 
 
426
  return success;
 
427
}
 
428
 
 
429
/**
 
430
 * gimp_config_deserialize_return:
 
431
 * @scanner:
 
432
 * @expected_token:
 
433
 * @nest_level:
 
434
 *
 
435
 * Returns:
 
436
 *
 
437
 * Since: GIMP 2.4
 
438
 **/
 
439
gboolean
 
440
gimp_config_deserialize_return (GScanner     *scanner,
 
441
                                GTokenType    expected_token,
 
442
                                gint          nest_level)
 
443
{
 
444
  GTokenType next_token;
 
445
 
 
446
  g_return_val_if_fail (scanner != NULL, FALSE);
 
447
 
 
448
  next_token = g_scanner_peek_next_token (scanner);
 
449
 
 
450
  if (expected_token != G_TOKEN_LEFT_PAREN)
 
451
    {
 
452
      g_scanner_get_next_token (scanner);
 
453
      g_scanner_unexp_token (scanner, expected_token, NULL, NULL, NULL,
 
454
                             _("fatal parse error"), TRUE);
 
455
      return FALSE;
 
456
    }
 
457
  else
 
458
    {
 
459
      if (nest_level > 0 && next_token == G_TOKEN_RIGHT_PAREN)
 
460
        {
 
461
          return TRUE;
 
462
        }
 
463
      else if (next_token != G_TOKEN_EOF)
 
464
        {
 
465
          g_scanner_get_next_token (scanner);
 
466
          g_scanner_unexp_token (scanner, expected_token, NULL, NULL, NULL,
 
467
                                 _("fatal parse error"), TRUE);
 
468
          return FALSE;
 
469
        }
 
470
    }
 
471
 
 
472
  return TRUE;
 
473
}
 
474
 
 
475
 
 
476
/**
 
477
 * gimp_config_duplicate:
 
478
 * @config: a #GObject that implements the #GimpConfigInterface.
 
479
 *
 
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
 
483
 * properties.
 
484
 *
 
485
 * Return value: the duplicated #GimpConfig object
 
486
 *
 
487
 * Since: GIMP 2.4
 
488
 **/
 
489
gpointer
 
490
gimp_config_duplicate (GimpConfig *config)
 
491
{
 
492
  g_return_val_if_fail (GIMP_IS_CONFIG (config), NULL);
 
493
 
 
494
  return GIMP_CONFIG_GET_INTERFACE (config)->duplicate (config);
 
495
}
 
496
 
 
497
/**
 
498
 * gimp_config_is_equal_to:
 
499
 * @a: a #GObject that implements the #GimpConfigInterface.
 
500
 * @b: another #GObject of the same type as @a.
 
501
 *
 
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
 
505
 * properties.
 
506
 *
 
507
 * Return value: %TRUE if the two objects are equal.
 
508
 *
 
509
 * Since: GIMP 2.4
 
510
 **/
 
511
gboolean
 
512
gimp_config_is_equal_to (GimpConfig *a,
 
513
                         GimpConfig *b)
 
514
{
 
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),
 
518
                        FALSE);
 
519
 
 
520
  return GIMP_CONFIG_GET_INTERFACE (a)->equal (a, b);
 
521
}
 
522
 
 
523
/**
 
524
 * gimp_config_reset:
 
525
 * @config: a #GObject that implements the #GimpConfigInterface.
 
526
 *
 
527
 * Resets the object to its default state. The default implementation of the
 
528
 * #GimpConfigInterface only works for objects that are completely defined by
 
529
 * their properties.
 
530
 *
 
531
 * Since: GIMP 2.4
 
532
 **/
 
533
void
 
534
gimp_config_reset (GimpConfig *config)
 
535
{
 
536
  g_return_if_fail (GIMP_IS_CONFIG (config));
 
537
 
 
538
  GIMP_CONFIG_GET_INTERFACE (config)->reset (config);
 
539
}