~ubuntu-branches/ubuntu/breezy/gimp/breezy

« back to all changes in this revision

Viewing changes to app/config/gimpconfig-dump.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-10-04 19:04:46 UTC
  • Revision ID: james.westby@ubuntu.com-20051004190446-ukh32kwk56s4sjhu
Tags: upstream-2.2.8
ImportĀ upstreamĀ versionĀ 2.2.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * GimpConfig object property dumper.
 
5
 * Copyright (C) 2001-2003  Sven Neumann <sven@gimp.org>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include "stdlib.h"
 
25
#include "string.h"
 
26
#ifdef HAVE_UNISTD_H
 
27
#include <unistd.h>
 
28
#endif
 
29
 
 
30
#include <glib-object.h>
 
31
 
 
32
#include "libgimpbase/gimpbase.h"
 
33
#include "libgimpcolor/gimpcolor.h"
 
34
 
 
35
#ifdef G_OS_WIN32
 
36
#include "libgimpbase/gimpwin32-io.h"
 
37
#endif
 
38
 
 
39
#include "config-types.h"
 
40
 
 
41
#include "gimpconfig.h"
 
42
#include "gimpconfig-dump.h"
 
43
#include "gimpconfig-params.h"
 
44
#include "gimpconfig-serialize.h"
 
45
#include "gimpconfig-types.h"
 
46
#include "gimpconfigwriter.h"
 
47
#include "gimprc.h"
 
48
 
 
49
 
 
50
 
 
51
static void    dump_gimprc_system   (GimpConfig       *rc,
 
52
                                     GimpConfigWriter *writer,
 
53
                                     gint              fd);
 
54
static void    dump_gimprc_manpage  (GimpConfig       *rc,
 
55
                                     GimpConfigWriter *writer,
 
56
                                     gint              fd);
 
57
static gchar * dump_describe_param  (GParamSpec       *param_spec);
 
58
static void    dump_with_linebreaks (gint              fd,
 
59
                                     const gchar      *text);
 
60
 
 
61
 
 
62
gboolean
 
63
gimp_config_dump (GimpConfigDumpFormat  format)
 
64
{
 
65
  GimpConfigWriter *writer;
 
66
  GimpConfig       *rc;
 
67
 
 
68
  rc = g_object_new (GIMP_TYPE_RC, NULL);
 
69
  writer = gimp_config_writer_new_fd (1);
 
70
 
 
71
  switch (format)
 
72
    {
 
73
    case GIMP_CONFIG_DUMP_NONE:
 
74
      break;
 
75
 
 
76
    case GIMP_CONFIG_DUMP_GIMPRC:
 
77
      gimp_config_writer_comment (writer,
 
78
                                  "Dump of the GIMP default configuration");
 
79
      gimp_config_writer_linefeed (writer);
 
80
      gimp_config_serialize_properties (rc, writer);
 
81
      gimp_config_writer_linefeed (writer);
 
82
      break;
 
83
 
 
84
    case GIMP_CONFIG_DUMP_GIMPRC_SYSTEM:
 
85
      dump_gimprc_system (rc, writer, 1);
 
86
      break;
 
87
 
 
88
    case GIMP_CONFIG_DUMP_GIMPRC_MANPAGE:
 
89
      dump_gimprc_manpage (rc, writer, 1);
 
90
      break;
 
91
    }
 
92
 
 
93
  gimp_config_writer_finish (writer, NULL, NULL);
 
94
  g_object_unref (rc);
 
95
 
 
96
  return TRUE;
 
97
}
 
98
 
 
99
 
 
100
static const gchar *system_gimprc_header =
 
101
"This is the system-wide gimprc file.  Any change made in this file "
 
102
"will affect all users of this system, provided that they are not "
 
103
"overriding the default values in their personal gimprc file.\n"
 
104
"\n"
 
105
"Lines that start with a '#' are comments. Blank lines are ignored.\n"
 
106
"\n"
 
107
"By default everything in this file is commented out.  The file then "
 
108
"documents the default values and shows what changes are possible.\n"
 
109
"\n"
 
110
"The variable ${gimp_dir} is set to the value of the environment "
 
111
"variable GIMP2_DIRECTORY or, if that is not set, the compiled-in "
 
112
"default value is used.  If GIMP2_DIRECTORY is not an absolute path, "
 
113
"it is interpreted relative to your home directory.";
 
114
 
 
115
static void
 
116
dump_gimprc_system (GimpConfig       *rc,
 
117
                    GimpConfigWriter *writer,
 
118
                    gint              fd)
 
119
{
 
120
  GObjectClass  *klass;
 
121
  GParamSpec   **property_specs;
 
122
  guint          n_property_specs;
 
123
  guint          i;
 
124
 
 
125
  gimp_config_writer_comment (writer, system_gimprc_header);
 
126
  gimp_config_writer_linefeed (writer);
 
127
 
 
128
  klass = G_OBJECT_GET_CLASS (rc);
 
129
  property_specs = g_object_class_list_properties (klass, &n_property_specs);
 
130
 
 
131
  for (i = 0; i < n_property_specs; i++)
 
132
    {
 
133
      GParamSpec *prop_spec = property_specs[i];
 
134
      gchar      *comment;
 
135
 
 
136
      if (! (prop_spec->flags & GIMP_PARAM_SERIALIZE))
 
137
        continue;
 
138
 
 
139
      if (prop_spec->flags & GIMP_PARAM_IGNORE)
 
140
        continue;
 
141
 
 
142
      comment = dump_describe_param (prop_spec);
 
143
      if (comment)
 
144
        {
 
145
          gimp_config_writer_comment (writer, comment);
 
146
          g_free (comment);
 
147
        }
 
148
 
 
149
      gimp_config_writer_comment_mode (writer, TRUE);
 
150
      gimp_config_writer_linefeed (writer);
 
151
 
 
152
      gimp_config_serialize_property (rc, prop_spec, writer);
 
153
 
 
154
      gimp_config_writer_comment_mode (writer, FALSE);
 
155
      gimp_config_writer_linefeed (writer);
 
156
    }
 
157
 
 
158
  g_free (property_specs);
 
159
}
 
160
 
 
161
 
 
162
static const gchar *man_page_header =
 
163
".\\\" This man-page is auto-generated by gimp --dump-gimprc-manpage.\n"
 
164
"\n"
 
165
".TH GIMPRC 5 \"Version @GIMP_VERSION@\" \"GIMP Manual Pages\"\n"
 
166
".SH NAME\n"
 
167
"gimprc \\- gimp configuration file\n"
 
168
".SH DESCRIPTION\n"
 
169
"The\n"
 
170
".B gimprc\n"
 
171
"file is a configuation file read by the GIMP when it starts up.  There\n"
 
172
"are two of these: one system-wide one stored in\n"
 
173
"@gimpsysconfdir@/gimprc and a per-user \\fB$HOME\\fP/@gimpdir@/gimprc\n"
 
174
"which may override system settings.\n"
 
175
"\n"
 
176
"Comments are introduced by a hash sign (#), and continue until the end\n"
 
177
"of the line.  Blank lines are ignored.\n"
 
178
"\n"
 
179
"The\n"
 
180
".B gimprc\n"
 
181
"file associates values with properties.  These properties may be set\n"
 
182
"by lisp-like assignments of the form:\n"
 
183
".IP\n"
 
184
"\\f3(\\f2property\\-name\\ value\\f3)\\f1\n"
 
185
".TP\n"
 
186
"where:\n"
 
187
".TP 10\n"
 
188
".I property\\-name\n"
 
189
"is one of the property names described below.\n"
 
190
".TP\n"
 
191
".I value\n"
 
192
"is the value the property is to be set to.\n"
 
193
".PP\n"
 
194
"\n"
 
195
"Either spaces or tabs may be used to separate the name from the value.\n"
 
196
".PP\n"
 
197
".SH PROPERTIES\n"
 
198
"Valid properties and their default values are:\n"
 
199
"\n";
 
200
 
 
201
static const gchar *man_page_path =
 
202
".PP\n"
 
203
".SH PATH EXPANSION\n"
 
204
"Strings of type PATH are expanded in a manner similar to\n"
 
205
".BR bash (1).\n"
 
206
"Specifically: tilde (~) is expanded to the user's home directory. Note that\n"
 
207
"the bash feature of being able to refer to other user's home directories\n"
 
208
"by writing ~userid/ is not valid in this file.\n"
 
209
"\n"
 
210
"${variable} is expanded to the current value of an environment variable.\n"
 
211
"There are a few variables that are pre-defined:\n"
 
212
".TP\n"
 
213
".I gimp_dir\n"
 
214
"The personal gimp directory which is set to the value of the environment\n"
 
215
"variable GIMP2_DIRECTORY or to ~/@gimpdir@.\n"
 
216
".TP\n"
 
217
".I gimp_data_dir\n"
 
218
"Nase for paths to shareable data, which is set to the value of the\n"
 
219
"environment variable GIMP2_DATADIR or to the compiled-in default value\n"
 
220
"@gimpdatadir@.\n"
 
221
".TP\n"
 
222
".I gimp_plug_in_dir\n"
 
223
"Base to paths for architecture-specific plugins and modules, which is set\n"
 
224
"to the value of the environment variable GIMP2_PLUGINDIR or to the\n"
 
225
"compiled-in default value @gimpplugindir@.\n"
 
226
".TP\n"
 
227
".I gimp_sysconf_dir\n"
 
228
"Path to configuration files, which is set to the value of the environment\n"
 
229
"variable GIMP2_SYSCONFDIR or to the compiled-in default value \n"
 
230
"@gimpsysconfdir@.\n"
 
231
"\n";
 
232
 
 
233
static const gchar *man_page_footer =
 
234
".SH FILES\n"
 
235
".TP\n"
 
236
".I @gimpsysconfdir@/gimprc\n"
 
237
"System-wide configuration file\n"
 
238
".TP\n"
 
239
".I \\fB$HOME\\fP/@gimpdir@/gimprc\n"
 
240
"Per-user configuration file\n"
 
241
"\n"
 
242
".SH \"SEE ALSO\"\n"
 
243
".BR gimp (1),\n"
 
244
".BR gimptool (1),\n"
 
245
".BR gimp\\-remote (1)\n";
 
246
 
 
247
 
 
248
static void
 
249
dump_gimprc_manpage (GimpConfig       *rc,
 
250
                     GimpConfigWriter *writer,
 
251
                     gint              fd)
 
252
{
 
253
  GObjectClass  *klass;
 
254
  GParamSpec   **property_specs;
 
255
  guint          n_property_specs;
 
256
  guint          i;
 
257
 
 
258
  write (fd, man_page_header, strlen (man_page_header));
 
259
 
 
260
  klass = G_OBJECT_GET_CLASS (rc);
 
261
  property_specs = g_object_class_list_properties (klass, &n_property_specs);
 
262
 
 
263
  for (i = 0; i < n_property_specs; i++)
 
264
    {
 
265
      GParamSpec *prop_spec = property_specs[i];
 
266
      gchar      *desc;
 
267
 
 
268
      if (! (prop_spec->flags & GIMP_PARAM_SERIALIZE))
 
269
        continue;
 
270
 
 
271
      if (prop_spec->flags & GIMP_PARAM_IGNORE)
 
272
        continue;
 
273
 
 
274
      write (fd, ".TP\n", strlen (".TP\n"));
 
275
 
 
276
      if (gimp_config_serialize_property (rc, prop_spec, writer))
 
277
        {
 
278
          write (fd, "\n", 1);
 
279
 
 
280
          desc = dump_describe_param (prop_spec);
 
281
 
 
282
          dump_with_linebreaks (fd, desc);
 
283
          write (fd, "\n", 1);
 
284
 
 
285
          g_free (desc);
 
286
        }
 
287
    }
 
288
 
 
289
  g_free (property_specs);
 
290
 
 
291
  write (fd, man_page_path,   strlen (man_page_path));
 
292
  write (fd, man_page_footer, strlen (man_page_footer));
 
293
}
 
294
 
 
295
 
 
296
static const gchar * display_format_description =
 
297
"This is a format string; certain % character sequences are recognised and "
 
298
"expanded as follows:\n"
 
299
"\n"
 
300
"%%  literal percent sign\n"
 
301
"%f  bare filename, or \"Untitled\"\n"
 
302
"%F  full path to file, or \"Untitled\"\n"
 
303
"%p  PDB image id\n"
 
304
"%i  view instance number\n"
 
305
"%t  image type (RGB, grayscale, indexed)\n"
 
306
"%z  zoom factor as a percentage\n"
 
307
"%s  source scale factor\n"
 
308
"%d  destination scale factor\n"
 
309
"%Dx expands to x if the image is dirty, the empty string otherwise\n"
 
310
"%Cx expands to x if the image is clean, the empty string otherwise\n"
 
311
"%B  expands to (modified) if the image is dirty, the empty string otherwise\n"
 
312
"%A  expands to (clean) if the image is clean, the empty string otherwise\n"
 
313
"%l  the number of layers\n"
 
314
"%L  the number of layers (long form)\n"
 
315
"%m  memory used by the image\n"
 
316
"%n  the name of the active layer/channel\n"
 
317
"%P  the PDB id of the active layer/channel\n"
 
318
"%w  image width in pixels\n"
 
319
"%W  image width in real-world units\n"
 
320
"%h  image height in pixels\n"
 
321
"%H  image height in real-world units\n"
 
322
"%u  unit symbol\n"
 
323
"%U  unit abbreviation\n\n";
 
324
 
 
325
 
 
326
static gchar *
 
327
dump_describe_param (GParamSpec *param_spec)
 
328
{
 
329
  GType        type;
 
330
  const gchar *blurb;
 
331
  const gchar *values = NULL;
 
332
 
 
333
  blurb = g_param_spec_get_blurb (param_spec);
 
334
 
 
335
  if (!blurb)
 
336
    {
 
337
      g_warning ("FIXME: Property '%s' has no blurb.", param_spec->name);
 
338
 
 
339
      blurb = g_strdup_printf ("The %s property has no description.",
 
340
                               param_spec->name);
 
341
    }
 
342
 
 
343
  type = param_spec->value_type;
 
344
 
 
345
  if (g_type_is_a (type, GIMP_TYPE_RGB))
 
346
    {
 
347
      values =
 
348
        "The color is specified in the form (color-rgba red green blue alpha) "
 
349
        "with channel values as floats between 0.0 and 1.0.";
 
350
    }
 
351
  else if (g_type_is_a (type, GIMP_TYPE_MEMSIZE))
 
352
    {
 
353
      values =
 
354
        "The integer size can contain a suffix of 'B', 'K', 'M' or 'G' which "
 
355
        "makes GIMP interpret the size as being specified in bytes, kilobytes, "
 
356
        "megabytes or gigabytes. If no suffix is specified the size defaults "
 
357
        "to being specified in kilobytes.";
 
358
    }
 
359
  else if (g_type_is_a (type, GIMP_TYPE_PATH))
 
360
    {
 
361
      switch (gimp_param_spec_path_type (param_spec))
 
362
        {
 
363
        case GIMP_PARAM_PATH_FILE:
 
364
          values = "This is a single filename.";
 
365
          break;
 
366
 
 
367
        case GIMP_PARAM_PATH_FILE_LIST:
 
368
          switch (G_SEARCHPATH_SEPARATOR)
 
369
            {
 
370
            case ':':
 
371
              values = "This is a colon-separated list of files.";
 
372
              break;
 
373
            case ';':
 
374
              values = "This is a semicolon-separated list of files.";
 
375
              break;
 
376
            default:
 
377
              g_warning ("unhandled G_SEARCHPATH_SEPARATOR value");
 
378
              break;
 
379
            }
 
380
          break;
 
381
 
 
382
        case GIMP_PARAM_PATH_DIR:
 
383
          values = "This is a single folder.";
 
384
          break;
 
385
 
 
386
        case GIMP_PARAM_PATH_DIR_LIST:
 
387
          switch (G_SEARCHPATH_SEPARATOR)
 
388
            {
 
389
            case ':':
 
390
              values = "This is a colon-separated list of folders to search.";
 
391
              break;
 
392
            case ';':
 
393
              values = "This is a semicolon-separated list of folders to search.";
 
394
              break;
 
395
            default:
 
396
              g_warning ("unhandled G_SEARCHPATH_SEPARATOR value");
 
397
              break;
 
398
            }
 
399
          break;
 
400
        }
 
401
    }
 
402
  else if (g_type_is_a (type, GIMP_TYPE_UNIT))
 
403
    {
 
404
      values =
 
405
        "The unit can be one inches, millimeters, points or picas plus "
 
406
        "those in your user units database.";
 
407
    }
 
408
  else if (g_type_is_a (type, GIMP_TYPE_CONFIG))
 
409
    {
 
410
      values = "This is a parameter list.";
 
411
    }
 
412
  else
 
413
    {
 
414
      switch (G_TYPE_FUNDAMENTAL (type))
 
415
        {
 
416
        case G_TYPE_BOOLEAN:
 
417
          values = "Possible values are yes and no.";
 
418
          break;
 
419
        case G_TYPE_INT:
 
420
        case G_TYPE_UINT:
 
421
        case G_TYPE_LONG:
 
422
        case G_TYPE_ULONG:
 
423
          values = "This is an integer value.";
 
424
          break;
 
425
        case G_TYPE_FLOAT:
 
426
        case G_TYPE_DOUBLE:
 
427
          values = "This is a float value.";
 
428
          break;
 
429
        case G_TYPE_STRING:
 
430
          /* eek */
 
431
          if (strcmp (g_param_spec_get_name (param_spec), "image-title-format")
 
432
              &&
 
433
              strcmp (g_param_spec_get_name (param_spec), "image-status-format"))
 
434
            {
 
435
              values = "This is a string value.";
 
436
            }
 
437
          else
 
438
            {
 
439
              values = display_format_description;
 
440
            }
 
441
          break;
 
442
        case G_TYPE_ENUM:
 
443
          {
 
444
            GEnumClass *enum_class;
 
445
            GEnumValue *enum_value;
 
446
            GString    *str;
 
447
            gint        i;
 
448
 
 
449
            enum_class = g_type_class_peek (type);
 
450
 
 
451
            str = g_string_new (blurb);
 
452
 
 
453
            g_string_append (str, "  Possible values are ");
 
454
 
 
455
            for (i = 0, enum_value = enum_class->values;
 
456
                 i < enum_class->n_values;
 
457
                 i++, enum_value++)
 
458
              {
 
459
                g_string_append (str, enum_value->value_nick);
 
460
 
 
461
                switch (enum_class->n_values - i)
 
462
                  {
 
463
                  case 1:
 
464
                    g_string_append_c (str, '.');
 
465
                    break;
 
466
                  case 2:
 
467
                    g_string_append (str, " and ");
 
468
                    break;
 
469
                  default:
 
470
                    g_string_append (str, ", ");
 
471
                    break;
 
472
                  }
 
473
              }
 
474
 
 
475
            return g_string_free (str, FALSE);
 
476
          }
 
477
          break;
 
478
        default:
 
479
          break;
 
480
        }
 
481
    }
 
482
 
 
483
  if (!values)
 
484
    g_warning ("FIXME: Can't tell anything about a %s.", g_type_name (type));
 
485
 
 
486
  return g_strdup_printf ("%s  %s", blurb, values);
 
487
}
 
488
 
 
489
 
 
490
#define LINE_LENGTH 78
 
491
 
 
492
static void
 
493
dump_with_linebreaks (gint         fd,
 
494
                      const gchar *text)
 
495
{
 
496
  gint len = strlen (text);
 
497
 
 
498
  while (len > 0)
 
499
    {
 
500
      const gchar *t;
 
501
      gint         i, space;
 
502
 
 
503
      /*  groff doesn't like lines to start with a single quote  */
 
504
      if (*text == '\'')
 
505
        write (fd, "\\&", 2);  /*  this represents a zero width space  */
 
506
 
 
507
      for (t = text, i = 0, space = 0;
 
508
           *t != '\n' && (i <= LINE_LENGTH || space == 0) && i < len;
 
509
           t++, i++)
 
510
        {
 
511
          if (g_ascii_isspace (*t))
 
512
            space = i;
 
513
        }
 
514
 
 
515
      if (i > LINE_LENGTH && space && *t != '\n')
 
516
        i = space;
 
517
 
 
518
      write (fd, text, i);
 
519
      write (fd, "\n", 1);
 
520
 
 
521
      if (*t == '\n')
 
522
        write (fd, ".br\n", 4);
 
523
 
 
524
      i++;
 
525
 
 
526
      text += i;
 
527
      len  -= i;
 
528
    }
 
529
}