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

« back to all changes in this revision

Viewing changes to app/pdb/gimppdb-query.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
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995-2003 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <glib/gstdio.h>
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
 
 
25
#ifdef HAVE_GLIBC_REGEX
 
26
#include <regex.h>
 
27
#else
 
28
#include "regexrepl/regex.h"
 
29
#endif
 
30
 
 
31
#include <glib-object.h>
 
32
 
 
33
#include "libgimpbase/gimpbase.h"
 
34
 
 
35
#include "pdb-types.h"
 
36
 
 
37
#include "core/gimpparamspecs-desc.h"
 
38
 
 
39
#include "gimppdb.h"
 
40
#include "gimppdb-query.h"
 
41
#include "gimp-pdb-compat.h"
 
42
#include "gimpprocedure.h"
 
43
 
 
44
#include "gimp-intl.h"
 
45
 
 
46
 
 
47
#define PDB_REGCOMP_FLAGS  REG_ICASE
 
48
 
 
49
#define COMPAT_BLURB       "This procedure is deprecated! Use '%s' instead."
 
50
 
 
51
 
 
52
typedef struct _PDBDump PDBDump;
 
53
 
 
54
struct _PDBDump
 
55
{
 
56
  GimpPDB  *pdb;
 
57
  FILE     *file;
 
58
 
 
59
  gboolean  dumping_compat;
 
60
};
 
61
 
 
62
typedef struct _PDBQuery PDBQuery;
 
63
 
 
64
struct _PDBQuery
 
65
{
 
66
  GimpPDB  *pdb;
 
67
 
 
68
  regex_t   name_regex;
 
69
  regex_t   blurb_regex;
 
70
  regex_t   help_regex;
 
71
  regex_t   author_regex;
 
72
  regex_t   copyright_regex;
 
73
  regex_t   date_regex;
 
74
  regex_t   proc_type_regex;
 
75
 
 
76
  gchar   **list_of_procs;
 
77
  gint      num_procs;
 
78
  gboolean  querying_compat;
 
79
};
 
80
 
 
81
typedef struct _PDBStrings PDBStrings;
 
82
 
 
83
struct _PDBStrings
 
84
{
 
85
  gboolean  compat;
 
86
 
 
87
  gchar    *blurb;
 
88
  gchar    *help;
 
89
  gchar    *author;
 
90
  gchar    *copyright;
 
91
  gchar    *date;
 
92
};
 
93
 
 
94
 
 
95
/*  local function prototypes  */
 
96
 
 
97
static void   gimp_pdb_query_entry  (gpointer       key,
 
98
                                     gpointer       value,
 
99
                                     gpointer       user_data);
 
100
static void   gimp_pdb_print_entry  (gpointer       key,
 
101
                                     gpointer       value,
 
102
                                     gpointer       user_data);
 
103
static void   gimp_pdb_get_strings  (PDBStrings    *strings,
 
104
                                     GimpProcedure *procedure,
 
105
                                     gboolean       compat);
 
106
static void   gimp_pdb_free_strings (PDBStrings    *strings);
 
107
 
 
108
 
 
109
/*  public functions  */
 
110
 
 
111
gboolean
 
112
gimp_pdb_dump (GimpPDB     *pdb,
 
113
               const gchar *filename)
 
114
{
 
115
  PDBDump pdb_dump;
 
116
 
 
117
  g_return_val_if_fail (GIMP_IS_PDB (pdb), FALSE);
 
118
  g_return_val_if_fail (filename != NULL, FALSE);
 
119
 
 
120
  pdb_dump.pdb  = pdb;
 
121
  pdb_dump.file = g_fopen (filename, "w");
 
122
 
 
123
  if (! pdb_dump.file)
 
124
    return FALSE;
 
125
 
 
126
  pdb_dump.dumping_compat = FALSE;
 
127
 
 
128
  g_hash_table_foreach (pdb->procedures,
 
129
                        gimp_pdb_print_entry,
 
130
                        &pdb_dump);
 
131
 
 
132
  pdb_dump.dumping_compat = TRUE;
 
133
 
 
134
  g_hash_table_foreach (pdb->compat_proc_names,
 
135
                        gimp_pdb_print_entry,
 
136
                        &pdb_dump);
 
137
 
 
138
  fclose (pdb_dump.file);
 
139
 
 
140
  return TRUE;
 
141
}
 
142
 
 
143
gboolean
 
144
gimp_pdb_query (GimpPDB       *pdb,
 
145
                const gchar   *name,
 
146
                const gchar   *blurb,
 
147
                const gchar   *help,
 
148
                const gchar   *author,
 
149
                const gchar   *copyright,
 
150
                const gchar   *date,
 
151
                const gchar   *proc_type,
 
152
                gint          *num_procs,
 
153
                gchar       ***procs)
 
154
{
 
155
  PDBQuery pdb_query;
 
156
  gboolean success = FALSE;
 
157
 
 
158
  g_return_val_if_fail (GIMP_IS_PDB (pdb), FALSE);
 
159
  g_return_val_if_fail (name != NULL, FALSE);
 
160
  g_return_val_if_fail (blurb != NULL, FALSE);
 
161
  g_return_val_if_fail (help != NULL, FALSE);
 
162
  g_return_val_if_fail (author != NULL, FALSE);
 
163
  g_return_val_if_fail (copyright != NULL, FALSE);
 
164
  g_return_val_if_fail (date != NULL, FALSE);
 
165
  g_return_val_if_fail (proc_type != NULL, FALSE);
 
166
  g_return_val_if_fail (num_procs != NULL, FALSE);
 
167
  g_return_val_if_fail (procs != NULL, FALSE);
 
168
 
 
169
  *num_procs = 0;
 
170
  *procs     = NULL;
 
171
 
 
172
  if (regcomp (&pdb_query.name_regex, name, PDB_REGCOMP_FLAGS))
 
173
    goto free_name;
 
174
  if (regcomp (&pdb_query.blurb_regex, blurb, PDB_REGCOMP_FLAGS))
 
175
    goto free_blurb;
 
176
  if (regcomp (&pdb_query.help_regex, help, PDB_REGCOMP_FLAGS))
 
177
    goto free_help;
 
178
  if (regcomp (&pdb_query.author_regex, author, PDB_REGCOMP_FLAGS))
 
179
    goto free_author;
 
180
  if (regcomp (&pdb_query.copyright_regex, copyright, PDB_REGCOMP_FLAGS))
 
181
    goto free_copyright;
 
182
  if (regcomp (&pdb_query.date_regex, date, PDB_REGCOMP_FLAGS))
 
183
    goto free_date;
 
184
  if (regcomp (&pdb_query.proc_type_regex, proc_type, PDB_REGCOMP_FLAGS))
 
185
    goto free_proc_type;
 
186
 
 
187
  success = TRUE;
 
188
 
 
189
  pdb_query.pdb             = pdb;
 
190
  pdb_query.list_of_procs   = NULL;
 
191
  pdb_query.num_procs       = 0;
 
192
  pdb_query.querying_compat = FALSE;
 
193
 
 
194
  g_hash_table_foreach (pdb->procedures,
 
195
                        gimp_pdb_query_entry, &pdb_query);
 
196
 
 
197
  pdb_query.querying_compat = TRUE;
 
198
 
 
199
  g_hash_table_foreach (pdb->compat_proc_names,
 
200
                        gimp_pdb_query_entry, &pdb_query);
 
201
 
 
202
 free_proc_type:
 
203
  regfree (&pdb_query.proc_type_regex);
 
204
 free_date:
 
205
  regfree (&pdb_query.date_regex);
 
206
 free_copyright:
 
207
  regfree (&pdb_query.copyright_regex);
 
208
 free_author:
 
209
  regfree (&pdb_query.author_regex);
 
210
 free_help:
 
211
  regfree (&pdb_query.help_regex);
 
212
 free_blurb:
 
213
  regfree (&pdb_query.blurb_regex);
 
214
 free_name:
 
215
  regfree (&pdb_query.name_regex);
 
216
 
 
217
  if (success)
 
218
    {
 
219
      *num_procs = pdb_query.num_procs;
 
220
      *procs     = pdb_query.list_of_procs;
 
221
    }
 
222
 
 
223
  return success;
 
224
}
 
225
 
 
226
gboolean
 
227
gimp_pdb_proc_info (GimpPDB          *pdb,
 
228
                    const gchar      *proc_name,
 
229
                    gchar           **blurb,
 
230
                    gchar           **help,
 
231
                    gchar           **author,
 
232
                    gchar           **copyright,
 
233
                    gchar           **date,
 
234
                    GimpPDBProcType  *proc_type,
 
235
                    gint             *num_args,
 
236
                    gint             *num_values)
 
237
{
 
238
  GimpProcedure *procedure;
 
239
  PDBStrings     strings;
 
240
 
 
241
  g_return_val_if_fail (GIMP_IS_PDB (pdb), FALSE);
 
242
  g_return_val_if_fail (proc_name != NULL, FALSE);
 
243
 
 
244
  procedure = gimp_pdb_lookup_procedure (pdb, proc_name);
 
245
 
 
246
  if (procedure)
 
247
    {
 
248
      gimp_pdb_get_strings (&strings, procedure, FALSE);
 
249
    }
 
250
  else
 
251
    {
 
252
      const gchar *compat_name;
 
253
 
 
254
      compat_name = gimp_pdb_lookup_compat_proc_name (pdb, proc_name);
 
255
 
 
256
      if (compat_name)
 
257
        {
 
258
          procedure = gimp_pdb_lookup_procedure (pdb, compat_name);
 
259
 
 
260
          if (procedure)
 
261
            gimp_pdb_get_strings (&strings, procedure, TRUE);
 
262
        }
 
263
    }
 
264
 
 
265
  if (procedure)
 
266
    {
 
267
      *blurb      = strings.compat ? strings.blurb : g_strdup (strings.blurb);
 
268
      *help       = strings.compat ? strings.help : g_strdup (strings.help);
 
269
      *author     = strings.compat ? strings.author : g_strdup (strings.author);
 
270
      *copyright  = strings.compat ? strings.copyright : g_strdup (strings.copyright);
 
271
      *date       = strings.compat ? strings.date : g_strdup (strings.date);
 
272
      *proc_type  = procedure->proc_type;
 
273
      *num_args   = procedure->num_args;
 
274
      *num_values = procedure->num_values;
 
275
 
 
276
      return TRUE;
 
277
    }
 
278
 
 
279
  return FALSE;
 
280
}
 
281
 
 
282
 
 
283
/*  private functions  */
 
284
 
 
285
static int
 
286
match_strings (regex_t     *preg,
 
287
               const gchar *a)
 
288
{
 
289
  if (!a)
 
290
    a = "";
 
291
 
 
292
  return regexec (preg, a, 0, NULL, 0);
 
293
}
 
294
 
 
295
static void
 
296
gimp_pdb_query_entry (gpointer key,
 
297
                      gpointer value,
 
298
                      gpointer user_data)
 
299
{
 
300
  PDBQuery      *pdb_query = user_data;
 
301
  GList         *list;
 
302
  GimpProcedure *procedure;
 
303
  const gchar   *proc_name;
 
304
  PDBStrings     strings;
 
305
  GEnumClass    *enum_class;
 
306
  GimpEnumDesc  *type_desc;
 
307
 
 
308
  proc_name = key;
 
309
 
 
310
  if (pdb_query->querying_compat)
 
311
    list = g_hash_table_lookup (pdb_query->pdb->procedures, value);
 
312
  else
 
313
    list = value;
 
314
 
 
315
  if (! list)
 
316
    return;
 
317
 
 
318
  procedure = list->data;
 
319
 
 
320
  gimp_pdb_get_strings (&strings, procedure, pdb_query->querying_compat);
 
321
 
 
322
  enum_class = g_type_class_ref (GIMP_TYPE_PDB_PROC_TYPE);
 
323
  type_desc = gimp_enum_get_desc (enum_class, procedure->proc_type);
 
324
  g_type_class_unref  (enum_class);
 
325
 
 
326
  if (! match_strings (&pdb_query->name_regex,      proc_name)         &&
 
327
      ! match_strings (&pdb_query->blurb_regex,     strings.blurb)     &&
 
328
      ! match_strings (&pdb_query->help_regex,      strings.help)      &&
 
329
      ! match_strings (&pdb_query->author_regex,    strings.author)    &&
 
330
      ! match_strings (&pdb_query->copyright_regex, strings.copyright) &&
 
331
      ! match_strings (&pdb_query->date_regex,      strings.date)      &&
 
332
      ! match_strings (&pdb_query->proc_type_regex, type_desc->value_desc))
 
333
    {
 
334
      pdb_query->num_procs++;
 
335
      pdb_query->list_of_procs = g_renew (gchar *, pdb_query->list_of_procs,
 
336
                                          pdb_query->num_procs);
 
337
      pdb_query->list_of_procs[pdb_query->num_procs - 1] = g_strdup (proc_name);
 
338
    }
 
339
 
 
340
  gimp_pdb_free_strings (&strings);
 
341
}
 
342
 
 
343
/* #define DEBUG_OUTPUT 1 */
 
344
 
 
345
static gboolean
 
346
output_string (FILE        *file,
 
347
               const gchar *string)
 
348
{
 
349
#ifndef DEBUG_OUTPUT
 
350
  if (fprintf (file, "\"") < 0)
 
351
    return FALSE;
 
352
#endif
 
353
 
 
354
  if (string)
 
355
    while (*string)
 
356
      {
 
357
        switch (*string)
 
358
          {
 
359
          case '\\' : if (fprintf (file, "\\\\") < 0) return FALSE; break;
 
360
          case '\"' : if (fprintf (file, "\\\"") < 0) return FALSE; break;
 
361
          case '{'  : if (fprintf (file, "@{")   < 0) return FALSE; break;
 
362
          case '@'  : if (fprintf (file, "@@")   < 0) return FALSE; break;
 
363
          case '}'  : if (fprintf (file, "@}")   < 0) return FALSE; break;
 
364
 
 
365
          default:
 
366
            if (fprintf (file, "%c", *string) < 0)
 
367
              return FALSE;
 
368
          }
 
369
        string++;
 
370
      }
 
371
 
 
372
#ifndef DEBUG_OUTPUT
 
373
  if (fprintf (file, "\"\n") < 0)
 
374
    return FALSE;
 
375
#endif
 
376
 
 
377
  return TRUE;
 
378
}
 
379
 
 
380
static void
 
381
gimp_pdb_print_entry (gpointer key,
 
382
                      gpointer value,
 
383
                      gpointer user_data)
 
384
{
 
385
  PDBDump     *pdb_dump = user_data;
 
386
  FILE        *file     = pdb_dump->file;
 
387
  const gchar *proc_name;
 
388
  GList       *list;
 
389
  GEnumClass  *arg_class;
 
390
  GEnumClass  *proc_class;
 
391
  GString     *buf;
 
392
  gint         num = 0;
 
393
 
 
394
  proc_name = key;
 
395
 
 
396
  if (pdb_dump->dumping_compat)
 
397
    list = g_hash_table_lookup (pdb_dump->pdb->procedures, value);
 
398
  else
 
399
    list = value;
 
400
 
 
401
  arg_class  = g_type_class_ref (GIMP_TYPE_PDB_ARG_TYPE);
 
402
  proc_class = g_type_class_ref (GIMP_TYPE_PDB_PROC_TYPE);
 
403
 
 
404
  buf = g_string_new ("");
 
405
 
 
406
  for (; list; list = list->next)
 
407
    {
 
408
      GimpProcedure *procedure = list->data;
 
409
      PDBStrings     strings;
 
410
      GEnumValue    *arg_value;
 
411
      GimpEnumDesc  *type_desc;
 
412
      gint           i;
 
413
 
 
414
      num++;
 
415
 
 
416
      gimp_pdb_get_strings (&strings, procedure, pdb_dump->dumping_compat);
 
417
 
 
418
#ifdef DEBUG_OUTPUT
 
419
      fprintf (file, "(");
 
420
#else
 
421
      fprintf (file, "(register-procedure ");
 
422
#endif
 
423
 
 
424
      if (num != 1)
 
425
        {
 
426
          g_string_printf (buf, "%s <%d>", proc_name, num);
 
427
          output_string (file, buf->str);
 
428
        }
 
429
      else
 
430
        {
 
431
          output_string (file, proc_name);
 
432
        }
 
433
 
 
434
      type_desc = gimp_enum_get_desc (proc_class, procedure->proc_type);
 
435
 
 
436
#ifdef DEBUG_OUTPUT
 
437
 
 
438
      fprintf (file, " (");
 
439
 
 
440
      for (i = 0; i < procedure->num_args; i++)
 
441
        {
 
442
          GParamSpec     *pspec = procedure->args[i];
 
443
          GimpPDBArgType  arg_type;
 
444
 
 
445
          arg_type = gimp_pdb_compat_arg_type_from_gtype (pspec->value_type);
 
446
 
 
447
          arg_value = g_enum_get_value (arg_class, arg_type);
 
448
 
 
449
          if (i > 0)
 
450
            fprintf (file, " ");
 
451
 
 
452
          output_string (file, arg_value->value_name);
 
453
        }
 
454
 
 
455
      fprintf (file, ") (");
 
456
 
 
457
      for (i = 0; i < procedure->num_values; i++)
 
458
        {
 
459
          GParamSpec     *pspec = procedure->values[i];
 
460
          GimpPDBArgType  arg_type;
 
461
 
 
462
          arg_type = gimp_pdb_compat_arg_type_from_gtype (pspec->value_type);
 
463
 
 
464
          arg_value = g_enum_get_value (arg_class, arg_type);
 
465
 
 
466
          if (i > 0)
 
467
            fprintf (file, " ");
 
468
 
 
469
          output_string (file, arg_value->value_name);
 
470
        }
 
471
      fprintf (file, "))\n");
 
472
 
 
473
#else /* ! DEBUG_OUTPUT */
 
474
 
 
475
      fprintf (file, "  ");
 
476
      output_string (file, strings.blurb);
 
477
      fprintf (file, "  ");
 
478
      output_string (file, strings.help);
 
479
      fprintf (file, "  ");
 
480
      output_string (file, strings.author);
 
481
      fprintf (file, "  ");
 
482
      output_string (file, strings.copyright);
 
483
      fprintf (file, "  ");
 
484
      output_string (file, strings.date);
 
485
      fprintf (file, "  ");
 
486
      output_string (file, type_desc->value_desc);
 
487
 
 
488
      fprintf (file, "  (");
 
489
      for (i = 0; i < procedure->num_args; i++)
 
490
        {
 
491
          GParamSpec     *pspec = procedure->args[i];
 
492
          GimpPDBArgType  arg_type;
 
493
          gchar          *desc  = gimp_param_spec_get_desc (pspec);
 
494
 
 
495
          fprintf (file, "\n    (\n");
 
496
 
 
497
          arg_type = gimp_pdb_compat_arg_type_from_gtype (pspec->value_type);
 
498
 
 
499
          arg_value = g_enum_get_value (arg_class, arg_type);
 
500
 
 
501
          fprintf (file, "      ");
 
502
          output_string (file, g_param_spec_get_name (pspec));
 
503
          fprintf (file, "      ");
 
504
          output_string (file, arg_value->value_name);
 
505
          fprintf (file, "      ");
 
506
          output_string (file, desc);
 
507
          g_free (desc);
 
508
 
 
509
          fprintf (file, "    )");
 
510
        }
 
511
      fprintf (file, "\n  )\n");
 
512
 
 
513
      fprintf (file, "  (");
 
514
      for (i = 0; i < procedure->num_values; i++)
 
515
        {
 
516
          GParamSpec     *pspec = procedure->values[i];
 
517
          GimpPDBArgType  arg_type;
 
518
          gchar          *desc  = gimp_param_spec_get_desc (pspec);
 
519
 
 
520
          fprintf (file, "\n    (\n");
 
521
 
 
522
          arg_type = gimp_pdb_compat_arg_type_from_gtype (pspec->value_type);
 
523
 
 
524
          arg_value = g_enum_get_value (arg_class, arg_type);
 
525
 
 
526
          fprintf (file, "      ");
 
527
          output_string (file, g_param_spec_get_name (pspec));
 
528
          fprintf (file, "      ");
 
529
          output_string (file, arg_value->value_name);
 
530
          fprintf (file, "      ");
 
531
          output_string (file, desc);
 
532
          g_free (desc);
 
533
 
 
534
          fprintf (file, "    )");
 
535
        }
 
536
      fprintf (file, "\n  )");
 
537
      fprintf (file, "\n)\n");
 
538
 
 
539
#endif /* DEBUG_OUTPUT */
 
540
 
 
541
      gimp_pdb_free_strings (&strings);
 
542
    }
 
543
 
 
544
  g_string_free (buf, TRUE);
 
545
 
 
546
  g_type_class_unref (arg_class);
 
547
  g_type_class_unref (proc_class);
 
548
}
 
549
 
 
550
static void
 
551
gimp_pdb_get_strings (PDBStrings    *strings,
 
552
                      GimpProcedure *procedure,
 
553
                      gboolean       compat)
 
554
{
 
555
  strings->compat = compat;
 
556
 
 
557
  if (compat)
 
558
    {
 
559
      strings->blurb     = g_strdup_printf (COMPAT_BLURB,
 
560
                                            GIMP_OBJECT (procedure)->name);
 
561
      strings->help      = g_strdup (strings->blurb);
 
562
      strings->author    = NULL;
 
563
      strings->copyright = NULL;
 
564
      strings->date      = NULL;
 
565
    }
 
566
  else
 
567
    {
 
568
      strings->blurb     = procedure->blurb;
 
569
      strings->help      = procedure->help;
 
570
      strings->author    = procedure->author;
 
571
      strings->copyright = procedure->copyright;
 
572
      strings->date      = procedure->date;
 
573
    }
 
574
}
 
575
 
 
576
static void
 
577
gimp_pdb_free_strings (PDBStrings *strings)
 
578
{
 
579
  if (strings->compat)
 
580
    {
 
581
      g_free (strings->blurb);
 
582
      g_free (strings->help);
 
583
    }
 
584
}