File: gimpitemlist.c

package info (click to toggle)
gimp 3.0.4-6.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 210,548 kB
  • sloc: ansic: 842,405; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (750 lines) | stat: -rw-r--r-- 24,166 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"

#include "core-types.h"

#include "gimp.h"
#include "gimpchannel.h"
#include "gimpimage.h"
#include "gimpitem.h"
#include "gimpitemlist.h"
#include "gimplayer.h"
#include "gimpmarshal.h"

#include "vectors/gimppath.h"

#include "gimp-intl.h"


enum
{
  EMPTY,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_IMAGE,
  PROP_IS_PATTERN,
  PROP_SELECT_METHOD,
  PROP_ITEMS,
  PROP_ITEM_TYPE,
  N_PROPS
};


typedef struct _GimpItemListPrivate GimpItemListPrivate;

struct _GimpItemListPrivate
{
  GimpImage        *image;

  gboolean          is_pattern;    /* Whether a named fixed set or a pattern-search. */
  GimpSelectMethod  select_method; /* Pattern format if is_pattern is TRUE           */

  GList            *items;         /* Fixed item list if is_pattern is TRUE.         */
  GList            *deleted_items; /* Removed item list kept for undoes.             */
  GType             item_type;
};


/*  local function prototypes  */

static void       gimp_item_list_constructed         (GObject        *object);
static void       gimp_item_list_dispose             (GObject        *object);
static void       gimp_item_list_finalize            (GObject        *object);
static void       gimp_item_list_set_property        (GObject        *object,
                                                      guint           property_id,
                                                      const GValue   *value,
                                                      GParamSpec     *pspec);
static void       gimp_item_list_get_property        (GObject        *object,
                                                      guint           property_id,
                                                      GValue         *value,
                                                      GParamSpec     *pspec);

static void       gimp_item_list_item_add            (GimpContainer  *container,
                                                      GimpObject     *object,
                                                      GimpItemList   *set);
static void       gimp_item_list_item_remove         (GimpContainer  *container,
                                                      GimpObject     *object,
                                                      GimpItemList   *set);

static GList *    gimp_item_list_get_items_by_substr (GimpItemList   *set,
                                                      const gchar    *pattern,
                                                      GError        **error);
static GList *    gimp_item_list_get_items_by_glob   (GimpItemList   *set,
                                                      const gchar    *pattern,
                                                      GError        **error);
static GList *    gimp_item_list_get_items_by_regexp (GimpItemList   *set,
                                                      const gchar    *pattern,
                                                      GError        **error);
static void       gimp_item_list_clean_deleted_items (GimpItemList   *set,
                                                      GimpItem       *searched,
                                                      gboolean       *found);
static void       gimp_item_list_free_deleted_item   (GWeakRef       *item);


G_DEFINE_TYPE_WITH_PRIVATE (GimpItemList, gimp_item_list, GIMP_TYPE_OBJECT)

#define parent_class gimp_item_list_parent_class

static guint       gimp_item_list_signals[LAST_SIGNAL] = { 0 };
static GParamSpec *gimp_item_list_props[N_PROPS]       = { NULL, };

static void
gimp_item_list_class_init (GimpItemListClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  /**
   * GimpItemList::empty:
   *
   * Sent when the item set changed and would return an empty set of
   * items.
   */
  gimp_item_list_signals[EMPTY] =
    g_signal_new ("empty",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpItemListClass, empty),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  object_class->constructed       = gimp_item_list_constructed;
  object_class->dispose           = gimp_item_list_dispose;
  object_class->finalize          = gimp_item_list_finalize;
  object_class->set_property      = gimp_item_list_set_property;
  object_class->get_property      = gimp_item_list_get_property;

  gimp_item_list_props[PROP_IMAGE]         = g_param_spec_object ("image", NULL, NULL,
                                                                 GIMP_TYPE_IMAGE,
                                                                 GIMP_PARAM_READWRITE |
                                                                 G_PARAM_CONSTRUCT_ONLY);
  gimp_item_list_props[PROP_IS_PATTERN]    = g_param_spec_boolean ("is-pattern", NULL, NULL,
                                                                   FALSE,
                                                                   GIMP_PARAM_READWRITE |
                                                                   G_PARAM_CONSTRUCT_ONLY);
  gimp_item_list_props[PROP_SELECT_METHOD] = g_param_spec_enum ("select-method", NULL, NULL,
                                                                GIMP_TYPE_SELECT_METHOD,
                                                                GIMP_SELECT_PLAIN_TEXT,
                                                                GIMP_PARAM_READWRITE |
                                                                G_PARAM_CONSTRUCT_ONLY);
  gimp_item_list_props[PROP_ITEMS]         = g_param_spec_pointer ("items",
                                                                   NULL, NULL,
                                                                   GIMP_PARAM_READWRITE |
                                                                   G_PARAM_CONSTRUCT_ONLY);
  gimp_item_list_props[PROP_ITEM_TYPE]     = g_param_spec_gtype ("item-type",
                                                                 NULL, NULL,
                                                                 G_TYPE_NONE,
                                                                 GIMP_PARAM_READWRITE |
                                                                 G_PARAM_CONSTRUCT_ONLY);

  g_object_class_install_properties (object_class, N_PROPS, gimp_item_list_props);
}

static void
gimp_item_list_init (GimpItemList *set)
{
  set->p = gimp_item_list_get_instance_private (set);

  set->p->items         = NULL;
  set->p->select_method = GIMP_SELECT_PLAIN_TEXT;
  set->p->is_pattern    = FALSE;
}

static void
gimp_item_list_constructed (GObject *object)
{
  GimpItemList *set = GIMP_ITEM_LIST (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  gimp_assert (GIMP_IS_IMAGE (set->p->image));
  gimp_assert (set->p->item_type == GIMP_TYPE_LAYER   ||
               set->p->item_type == GIMP_TYPE_PATH ||
               set->p->item_type == GIMP_TYPE_CHANNEL);

  if (! set->p->is_pattern)
    {
      GimpContainer *container;

      if (set->p->item_type == GIMP_TYPE_LAYER)
        container = gimp_image_get_layers (set->p->image);
      else if (set->p->item_type == GIMP_TYPE_PATH)
        container = gimp_image_get_paths (set->p->image);
      else
        container = gimp_image_get_channels (set->p->image);
      g_signal_connect (container, "remove",
                        G_CALLBACK (gimp_item_list_item_remove),
                        set);
      g_signal_connect (container, "add",
                        G_CALLBACK (gimp_item_list_item_add),
                        set);
    }
}

static void
gimp_item_list_dispose (GObject *object)
{
  GimpItemList *set = GIMP_ITEM_LIST (object);

  if (! set->p->is_pattern)
    {
      GimpContainer *container;

      if (set->p->item_type == GIMP_TYPE_LAYER)
        container = gimp_image_get_layers (set->p->image);
      else if (set->p->item_type == GIMP_TYPE_PATH)
        container = gimp_image_get_paths (set->p->image);
      else
        container = gimp_image_get_channels (set->p->image);
      g_signal_handlers_disconnect_by_func (container,
                                            G_CALLBACK (gimp_item_list_item_remove),
                                            set);
      g_signal_handlers_disconnect_by_func (container,
                                            G_CALLBACK (gimp_item_list_item_add),
                                            set);
    }
}

static void
gimp_item_list_finalize (GObject *object)
{
  GimpItemList *set = GIMP_ITEM_LIST (object);

  g_list_free (set->p->items);
  g_list_free_full (set->p->deleted_items,
                    (GDestroyNotify) gimp_item_list_free_deleted_item);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gimp_item_list_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GimpItemList *set = GIMP_ITEM_LIST (object);

  switch (property_id)
    {
    case PROP_IMAGE:
      set->p->image = g_value_get_object (value);
      break;
    case PROP_IS_PATTERN:
      set->p->is_pattern = g_value_get_boolean (value);
      break;
    case PROP_SELECT_METHOD:
      set->p->select_method = g_value_get_enum (value);
      break;
    case PROP_ITEMS:
      set->p->items = g_list_copy (g_value_get_pointer (value));
      break;
    case PROP_ITEM_TYPE:
      set->p->item_type = g_value_get_gtype (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_item_list_get_property (GObject    *object,
                            guint       property_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  GimpItemList *set = GIMP_ITEM_LIST (object);

  switch (property_id)
    {
    case PROP_IMAGE:
      g_value_set_object (value, set->p->image);
      break;
    case PROP_IS_PATTERN:
      g_value_set_boolean (value, set->p->is_pattern);
      break;
    case PROP_SELECT_METHOD:
      g_value_set_enum (value, set->p->select_method);
      break;
    case PROP_ITEMS:
      g_value_set_pointer (value, set->p->items);
      break;
    case PROP_ITEM_TYPE:
      g_value_set_gtype (value, set->p->item_type);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}


/*  Public functions  */


/**
 * gimp_item_list_named_new:
 * @image:     The new item_list's #GimpImage.
 * @item_type: The type of #GimpItem in the list.
 * @name:      The name to assign the item list.
 * @items:     The items in the list.
 *
 * Create a fixed list of items made of items. It cannot be edited and
 * will only auto-update when items get deleted from @image, until the
 * list reaches 0 (in which case, the list will self-destroy).
 *
 * If @items is %NULL, the current item selection of type @item_type in
 * @image is used. If this selection is empty, then %NULL is returned.
 *
 * Returns: The newly created #GimpItemList of %NULL if it corresponds
 *          to no items.
 */
GimpItemList *
gimp_item_list_named_new (GimpImage   *image,
                          GType        item_type,
                          const gchar *name,
                          GList       *items)
{
  GimpItemList *set;
  GList        *iter;

  g_return_val_if_fail (g_type_is_a (item_type, GIMP_TYPE_ITEM), NULL);
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);

  for (iter = items; iter; iter = iter->next)
    g_return_val_if_fail (g_type_is_a (G_OBJECT_TYPE (iter->data), item_type), NULL);

  if (! items)
    {
      if (item_type == GIMP_TYPE_LAYER)
        items = gimp_image_get_selected_layers (image);
      else if (item_type == GIMP_TYPE_PATH)
        items = gimp_image_get_selected_paths (image);
      else if (item_type == GIMP_TYPE_CHANNEL)
        items = gimp_image_get_selected_channels (image);

      if (! items)
        return NULL;
    }

  set = g_object_new (GIMP_TYPE_ITEM_LIST,
                      "image",      image,
                      "name",       name,
                      "is-pattern", FALSE,
                      "item-type",  item_type,
                      "items",      items,
                      NULL);

  return set;
}

/**
 * gimp_item_list_pattern_new:
 * @image:     The new item_list's #GimpImage.
 * @item_type: The type of #GimpItem in the list.
 * @pattern_syntax: type of patterns we are handling.
 * @pattern:   The pattern generating the contents of the list.
 *
 * Create a list of items generated from a pattern. It cannot be edited.
 *
 * Returns: The newly created #GimpItemList.
 */
GimpItemList *
gimp_item_list_pattern_new (GimpImage        *image,
                            GType             item_type,
                            GimpSelectMethod  pattern_syntax,
                            const gchar      *pattern)
{
  GimpItemList *set;

  g_return_val_if_fail (g_type_is_a (item_type, GIMP_TYPE_ITEM), NULL);
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);

  /* TODO: check pattern first and fail if invalid. */
  set = g_object_new (GIMP_TYPE_ITEM_LIST,
                      "image",          image,
                      "name",           pattern,
                      "is-pattern",     TRUE,
                      "select-method",  pattern_syntax,
                      "item-type",      item_type,
                      NULL);

  return set;
}

GType
gimp_item_list_get_item_type (GimpItemList *set)
{
  g_return_val_if_fail (GIMP_IS_ITEM_LIST (set), FALSE);

  return set->p->item_type;
}

/**
 * gimp_item_list_get_items:
 * @set:
 *
 * Returns: (transfer container): The unordered list of items
 *          represented by @set to be freed with g_list_free().
 */
GList *
gimp_item_list_get_items (GimpItemList  *set,
                          GError       **error)
{
  GList *items = NULL;

  g_return_val_if_fail (GIMP_IS_ITEM_LIST (set), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (set->p->is_pattern)
    {
      switch (set->p->select_method)
        {
        case GIMP_SELECT_PLAIN_TEXT:
          items = gimp_item_list_get_items_by_substr (set,
                                                      gimp_object_get_name (set),
                                                      error);
          break;
        case GIMP_SELECT_GLOB_PATTERN:
          items = gimp_item_list_get_items_by_glob (set,
                                                    gimp_object_get_name (set),
                                                    error);
          break;
        case GIMP_SELECT_REGEX_PATTERN:
          items = gimp_item_list_get_items_by_regexp (set,
                                                      gimp_object_get_name (set),
                                                      error);
          break;
        }
    }
  else
    {
      items = g_list_copy (set->p->items);
    }

  return items;
}

/**
 * gimp_item_list_is_pattern:
 * @set:            The #GimpItemList.
 * @pattern_syntax: The type of patterns @set handles.
 *
 * Indicate if @set is a pattern list. If the returned value is %TRUE,
 * then @pattern_syntax will be set to the syntax we are dealing with.
 *
 * Returns: %TRUE if @set is a pattern list, %FALSE if it is a named
 *          list.
 */
gboolean
gimp_item_list_is_pattern (GimpItemList      *set,
                           GimpSelectMethod  *pattern_syntax)
{
  g_return_val_if_fail (GIMP_IS_ITEM_LIST (set), FALSE);

  if (set->p->is_pattern && pattern_syntax)
    *pattern_syntax = set->p->select_method;

  return (set->p->is_pattern);
}

/**
 * gimp_item_list_is_pattern:
 * @set:  The #GimpItemList.
 * @item: #GimpItem to add to @set.
 *
 * Add @item to the named list @set whose item type must also agree.
 */
void
gimp_item_list_add (GimpItemList *set,
                    GimpItem     *item)
{
  g_return_if_fail (GIMP_IS_ITEM_LIST (set));
  g_return_if_fail (! gimp_item_list_is_pattern (set, NULL));
  g_return_if_fail (g_type_is_a (G_TYPE_FROM_INSTANCE (item), set->p->item_type));

  set->p->items = g_list_prepend (set->p->items, item);
}


/*  Private functions  */


static void
gimp_item_list_item_add (GimpContainer  *container,
                         GimpObject     *object,
                         GimpItemList   *set)
{
  gboolean found = FALSE;

  gimp_item_list_clean_deleted_items (set, GIMP_ITEM (object), &found);

  if (found)
    {
      /* Such an item can only have been added back as part of an redo
       * step.
       */
      set->p->items = g_list_prepend (set->p->items, object);
    }
}

static void
gimp_item_list_item_remove (GimpContainer  *container,
                            GimpObject     *object,
                            GimpItemList   *set)
{
  GWeakRef *deleted_item = g_slice_new (GWeakRef);

  /* Keep a weak link on object so that it disappears by itself when no
   * other piece of code has a reference to it. In particular, we expect
   * undo to keep references to deleted items. So if a redo happens we
   * will get a "add" signal with the same object.
   */
  set->p->items = g_list_remove (set->p->items, object);

  g_weak_ref_init (deleted_item, object);
  set->p->deleted_items = g_list_prepend (set->p->deleted_items, deleted_item);
}

/*
 * @gimp_item_list_get_items_by_substr:
 * @image:
 * @pattern:
 * @error: unused #GError.
 *
 * Replace currently selected items in @image with the items whose
 * names match with the @pattern after tokenisation, case-folding and
 * normalization.
 *
 * Returns: %TRUE if some items matched @pattern (even if it turned out
 *          selected items stay the same), %FALSE otherwise.
 */
static GList *
gimp_item_list_get_items_by_substr (GimpItemList  *set,
                                    const gchar   *pattern,
                                    GError       **error)
{
  GList  *items;
  GList  *match = NULL;
  GList  *iter;

  g_return_val_if_fail (GIMP_IS_ITEM_LIST (set), FALSE);
  g_return_val_if_fail (error && *error == NULL, FALSE);

  if (pattern == NULL)
    return NULL;

  if (set->p->item_type == GIMP_TYPE_LAYER)
    {
      items = gimp_image_get_layer_list (set->p->image);
    }
  else
    {
      g_critical ("%s: only list of GimpLayer supported for now.",
                  G_STRFUNC);
      return NULL;
    }

  for (iter = items; iter; iter = iter->next)
    {
      if (g_str_match_string (pattern,
                              gimp_object_get_name (iter->data),
                              TRUE))
        match = g_list_prepend (match, iter->data);
    }

  return match;
}

/*
 * @gimp_item_list_get_items_by_glob:
 * @image:
 * @pattern:
 * @error: unused #GError.
 *
 * Replace currently selected items in @image with the items whose
 * names match with the @pattern glob expression.
 *
 * Returns: %TRUE if some items matched @pattern (even if it turned out
 *          selected items stay the same), %FALSE otherwise.
 */
static GList *
gimp_item_list_get_items_by_glob (GimpItemList  *set,
                                  const gchar   *pattern,
                                  GError       **error)
{
  GList        *items;
  GList        *match = NULL;
  GList        *iter;
  GPatternSpec *spec;

  g_return_val_if_fail (GIMP_IS_ITEM_LIST (set), FALSE);
  g_return_val_if_fail (error && *error == NULL, FALSE);

  if (pattern == NULL)
    return NULL;

  if (set->p->item_type == GIMP_TYPE_LAYER)
    {
      items = gimp_image_get_layer_list (set->p->image);
    }
  else
    {
      g_critical ("%s: only list of GimpLayer supported for now.",
                  G_STRFUNC);
      return NULL;
    }

  spec = g_pattern_spec_new (pattern);
  for (iter = items; iter; iter = iter->next)
    {
      if (g_pattern_spec_match_string (spec, gimp_object_get_name (iter->data)))
        match = g_list_prepend (match, iter->data);
    }
  g_pattern_spec_free (spec);

  return match;
}

/*
 * @gimp_item_list_get_items_by_regexp:
 * @image:
 * @pattern:
 * @error:
 *
 * Replace currently selected items in @image with the items whose
 * names match with the @pattern regular expression.
 *
 * Returns: %TRUE if some items matched @pattern (even if it turned out
 *          selected items stay the same), %FALSE otherwise or if
 *          @pattern is an invalid regular expression (in which case,
 *          @error will be filled with the appropriate error).
 */
static GList *
gimp_item_list_get_items_by_regexp (GimpItemList  *set,
                                    const gchar   *pattern,
                                    GError       **error)
{
  GList  *items;
  GList  *match = NULL;
  GList  *iter;
  GRegex *regex;

  g_return_val_if_fail (GIMP_IS_ITEM_LIST (set), FALSE);
  g_return_val_if_fail (pattern != NULL, FALSE);
  g_return_val_if_fail (error && *error == NULL, FALSE);

  regex = g_regex_new (pattern, 0, 0, error);

  if (regex == NULL)
    return NULL;

  if (set->p->item_type == GIMP_TYPE_LAYER)
    {
      items = gimp_image_get_layer_list (set->p->image);
    }
  else
    {
      g_critical ("%s: only list of GimpLayer supported for now.",
                  G_STRFUNC);
      return NULL;
    }

  for (iter = items; iter; iter = iter->next)
    {
      if (g_regex_match (regex,
                         gimp_object_get_name (iter->data),
                         0, NULL))
        match = g_list_prepend (match, iter->data);
    }
  g_regex_unref (regex);

  return match;
}

/*
 * Remove all deleted items which don't have any reference left anywhere
 * (only leaving the shell of the weak reference), hence whose deletion
 * cannot be undone anyway.
 * If @searched is not %NULL, check if it belonged to the deleted item
 * list and return TRUE if so. In this case, you must call the function
 * with @found set to %FALSE initially.
 */
static void
gimp_item_list_clean_deleted_items (GimpItemList *set,
                                    GimpItem     *searched,
                                    gboolean     *found)
{
  GList *iter;

  g_return_if_fail (GIMP_IS_ITEM_LIST (set));
  g_return_if_fail (! searched || (found && *found == FALSE));

  for (iter = set->p->deleted_items; iter; iter = iter->next)
    {
      GimpItem *item = g_weak_ref_get (iter->data);

      if (item == NULL)
        {
          set->p->deleted_items = g_list_delete_link (set->p->deleted_items,
                                                      iter);
          break;
        }
      else
        {
          if (searched && item == searched)
            {
              set->p->deleted_items = g_list_delete_link (set->p->deleted_items,
                                                          iter);
              *found = TRUE;
              g_object_unref (item);
              break;
            }
          g_object_unref (item);
        }
    }

  if (iter)
    gimp_item_list_clean_deleted_items (set,
                                        (found && *found) ? NULL : searched,
                                        found);
}

static void
gimp_item_list_free_deleted_item (GWeakRef *item)
{
  g_weak_ref_clear (item);

  g_slice_free (GWeakRef, item);
}