summaryrefslogtreecommitdiffstats
path: root/app/core/gimpcontainer-filter.c
blob: 956a4a0ef4d96eaa0e6de73126f9412d1ae81776 (plain)
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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
 * gimpcontainer-filter.c
 * Copyright (C) 2003  Sven Neumann <sven@gimp.org>
 *
 * 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 <string.h>

#include <glib-object.h>

#include "core-types.h"

#include "gimpcontainer.h"
#include "gimpcontainer-filter.h"
#include "gimplist.h"


typedef struct
{
  GimpObjectFilterFunc   filter;
  GimpContainer         *container;
  gpointer               user_data;
} GimpContainerFilterContext;


static void
gimp_container_filter_foreach_func (GimpObject                 *object,
                                    GimpContainerFilterContext *context)
{
  if (context->filter (object, context->user_data))
    gimp_container_add (context->container, object);
}

/**
 * gimp_container_filter:
 * @container: a #GimpContainer to filter
 * @filter: a #GimpObjectFilterFunc
 * @user_data: a pointer passed to @filter
 *
 * Calls the supplied @filter function on each object in @container.
 * A return value of %TRUE is interpreted as a match.
 *
 * Returns: a weak #GimpContainer filled with matching objects.
 **/
GimpContainer *
gimp_container_filter (GimpContainer        *container,
                       GimpObjectFilterFunc  filter,
                       gpointer              user_data)
{
  GimpContainer              *result;
  GimpContainerFilterContext  context;

  g_return_val_if_fail (GIMP_IS_CONTAINER (container), NULL);
  g_return_val_if_fail (filter != NULL, NULL);

  result =
    g_object_new (G_TYPE_FROM_INSTANCE (container),
                  "children-type", gimp_container_get_children_type (container),
                  "policy",        GIMP_CONTAINER_POLICY_WEAK,
                  NULL);

  context.filter    = filter;
  context.container = result;
  context.user_data = user_data;

  gimp_container_foreach (container,
                          (GFunc) gimp_container_filter_foreach_func,
                          &context);

  /*  This is somewhat ugly, but it keeps lists in the same order.  */
  if (GIMP_IS_LIST (result))
    gimp_list_reverse (GIMP_LIST (result));


  return result;
}


static gboolean
gimp_object_filter_by_name (GimpObject   *object,
                            const GRegex *regex)
{
  return g_regex_match (regex, gimp_object_get_name (object), 0, NULL);
}

/**
 * gimp_container_filter_by_name:
 * @container: a #GimpContainer to filter
 * @regexp: a regular expression (as a %NULL-terminated string)
 * @error: error location to report errors or %NULL
 *
 * This function performs a case-insensitive regular expression search
 * on the names of the GimpObjects in @container.
 *
 * Returns: a weak #GimpContainer filled with matching objects.
 **/
GimpContainer *
gimp_container_filter_by_name (GimpContainer  *container,
                               const gchar    *regexp,
                               GError        **error)
{
  GimpContainer *result;
  GRegex        *regex;

  g_return_val_if_fail (GIMP_IS_CONTAINER (container), NULL);
  g_return_val_if_fail (regexp != NULL, NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  regex = g_regex_new (regexp, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
                       error);

  if (! regex)
    return NULL;

  result =
    gimp_container_filter (container,
                           (GimpObjectFilterFunc) gimp_object_filter_by_name,
                           regex);

  g_regex_unref (regex);

  return result;
}


gchar **
gimp_container_get_filtered_name_array (GimpContainer *container,
                                        const gchar   *regexp,
                                        gint          *length)
{
  GimpContainer *weak;
  GError        *error = NULL;

  g_return_val_if_fail (GIMP_IS_CONTAINER (container), NULL);
  g_return_val_if_fail (length != NULL, NULL);

  if (regexp == NULL || strlen (regexp) == 0)
    return (gimp_container_get_name_array (container, length));

  weak = gimp_container_filter_by_name (container, regexp, &error);

  if (weak)
    {
      gchar **retval = gimp_container_get_name_array (weak, length);

      g_object_unref (weak);

      return retval;
    }
  else
    {
      g_warning ("%s", error->message);
      g_error_free (error);

      *length = 0;
      return NULL;
    }
}