summaryrefslogtreecommitdiff
path: root/gtk/gtkboolfilter.c
blob: 12c2486d1c17d0b53085eb8c17039bdfbf810ff3 (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
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
/*
 * Copyright © 2020 Benjamin Otte
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include "config.h"

#include "gtkboolfilter.h"

#include "gtkintl.h"
#include "gtktypebuiltins.h"

/**
 * GtkBoolFilter:
 *
 * `GtkBoolFilter` evaluates a boolean `GtkExpression`
 * to determine whether to include items.
 */

struct _GtkBoolFilter
{
  GtkFilter parent_instance;

  gboolean invert;
  GtkExpression *expression;
};

enum {
  PROP_0,
  PROP_EXPRESSION,
  PROP_INVERT,
  NUM_PROPERTIES
};

G_DEFINE_TYPE (GtkBoolFilter, gtk_bool_filter, GTK_TYPE_FILTER)

static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };

static gboolean
gtk_bool_filter_match (GtkFilter *filter,
                       gpointer   item)
{
  GtkBoolFilter *self = GTK_BOOL_FILTER (filter);
  GValue value = G_VALUE_INIT;
  gboolean result;

  if (self->expression == NULL ||
      !gtk_expression_evaluate (self->expression, item, &value))
    return FALSE;
  result = g_value_get_boolean (&value);

  g_value_unset (&value);

  if (self->invert)
    result = !result;

  return result;
}

static GtkFilterMatch
gtk_bool_filter_get_strictness (GtkFilter *filter)
{
  GtkBoolFilter *self = GTK_BOOL_FILTER (filter);

  if (self->expression == NULL)
    return GTK_FILTER_MATCH_NONE;

  return GTK_FILTER_MATCH_SOME;
}

static void
gtk_bool_filter_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GtkBoolFilter *self = GTK_BOOL_FILTER (object);

  switch (prop_id)
    {
    case PROP_EXPRESSION:
      gtk_bool_filter_set_expression (self, gtk_value_get_expression (value));
      break;

    case PROP_INVERT:
      gtk_bool_filter_set_invert (self, g_value_get_boolean (value));
      break;

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

static void 
gtk_bool_filter_get_property (GObject     *object,
                              guint        prop_id,
                              GValue      *value,
                              GParamSpec  *pspec)
{
  GtkBoolFilter *self = GTK_BOOL_FILTER (object);

  switch (prop_id)
    {
    case PROP_EXPRESSION:
      gtk_value_set_expression (value, self->expression);
      break;

    case PROP_INVERT:
      g_value_set_boolean (value, self->invert);
      break;

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

static void
gtk_bool_filter_dispose (GObject *object)
{
  GtkBoolFilter *self = GTK_BOOL_FILTER (object);

  g_clear_pointer (&self->expression, gtk_expression_unref);

  G_OBJECT_CLASS (gtk_bool_filter_parent_class)->dispose (object);
}

static void
gtk_bool_filter_class_init (GtkBoolFilterClass *class)
{
  GtkFilterClass *filter_class = GTK_FILTER_CLASS (class);
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  filter_class->match = gtk_bool_filter_match;
  filter_class->get_strictness = gtk_bool_filter_get_strictness;

  object_class->get_property = gtk_bool_filter_get_property;
  object_class->set_property = gtk_bool_filter_set_property;
  object_class->dispose = gtk_bool_filter_dispose;

  /**
   * GtkBoolFilter:expression: (type GtkExpression) (attributes org.gtk.Property.get=gtk_bool_filter_get_expression org.gtk.Property.set=gtk_bool_filter_set_expression)
   *
   * The boolean expression to evaluate on item.
   */
  properties[PROP_EXPRESSION] =
    gtk_param_spec_expression ("expression",
                               P_("Expression"),
                               P_("Expression to evaluate"),
                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * GtkBoolFilter:invert: (attributes org.gtk.Property.get=gtk_bool_filter_get_invert org.gtk.Property.set=gtk_bool_filter_set_invert)
   *
   * If the expression result should be inverted.
   */
  properties[PROP_INVERT] =
      g_param_spec_boolean ("invert",
                            P_("Invert"),
                            P_("If the expression result should be inverted"),
                            FALSE,
                            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
}

static void
gtk_bool_filter_init (GtkBoolFilter *self)
{
}

/**
 * gtk_bool_filter_new:
 * @expression: (transfer full) (nullable): The expression to evaluate
 *     or %NULL for none
 *
 * Creates a new bool filter.
 *
 * Returns: a new `GtkBoolFilter`
 **/
GtkBoolFilter *
gtk_bool_filter_new (GtkExpression *expression)
{
  GtkBoolFilter *result;

  result = g_object_new (GTK_TYPE_BOOL_FILTER,
                         "expression", expression,
                         NULL);

  g_clear_pointer (&expression, gtk_expression_unref);

  return result;
}

/**
 * gtk_bool_filter_get_expression: (attributes org.gtk.Method.get_property=expression)
 * @self: a `GtkBoolFilter`
 *
 * Gets the expression that the filter uses to evaluate if
 * an item should be filtered.
 *
 * Returns: (transfer none): a `GtkExpression`
 */
GtkExpression *
gtk_bool_filter_get_expression (GtkBoolFilter *self)
{
  g_return_val_if_fail (GTK_IS_BOOL_FILTER (self), NULL);

  return self->expression;
}

/**
 * gtk_bool_filter_set_expression: (attributes org.gtk.Method.set_property=expression)
 * @self: a `GtkBoolFilter`
 * @expression: a `GtkExpression`
 *
 * Sets the expression that the filter uses to check if items
 * should be filtered.
 *
 * The expression must have a value type of %G_TYPE_BOOLEAN.
 */
void
gtk_bool_filter_set_expression (GtkBoolFilter *self,
                                GtkExpression *expression)
{
  g_return_if_fail (GTK_IS_BOOL_FILTER (self));
  g_return_if_fail (expression == NULL || gtk_expression_get_value_type (expression) == G_TYPE_BOOLEAN);

  if (self->expression == expression)
    return;

  g_clear_pointer (&self->expression, gtk_expression_unref);
  if (expression)
    self->expression = gtk_expression_ref (expression);

  gtk_filter_changed (GTK_FILTER (self), GTK_FILTER_CHANGE_DIFFERENT);

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EXPRESSION]);
}

/**
 * gtk_bool_filter_get_invert: (attributes org.gtk.Method.get_property=invert)
 * @self: a `GtkBoolFilter`
 *
 * Returns whether the filter inverts the expression.
 *
 * Returns: %TRUE if the filter inverts
 */
gboolean
gtk_bool_filter_get_invert (GtkBoolFilter *self)
{
  g_return_val_if_fail (GTK_IS_BOOL_FILTER (self), TRUE);

  return self->invert;
}

/**
 * gtk_bool_filter_set_invert: (attributes org.gtk.Method.set_property=invert)
 * @self: a `GtkBoolFilter`
 * @invert: %TRUE to invert
 *
 * Sets whether the filter should invert the expression.
 */
void
gtk_bool_filter_set_invert (GtkBoolFilter *self,
                            gboolean       invert)
{
  g_return_if_fail (GTK_IS_BOOL_FILTER (self));

  if (self->invert == invert)
    return;

  self->invert = invert;

  gtk_filter_changed (GTK_FILTER (self), GTK_FILTER_CHANGE_DIFFERENT);

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INVERT]);
}