-
Notifications
You must be signed in to change notification settings - Fork 762
/
search-string.c
379 lines (311 loc) · 10.7 KB
/
search-string.c
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
/*
* Copyright (C) 2002 Derek Atkins
*
* Authors: Derek Atkins <warlord@MIT.EDU>
*
* Copyright (c) 2006 David Hampton <hampton@employees.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 2 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, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#include "search-string.h"
#include "search-core-utils.h"
#include "qof.h"
#define d(x)
static void editable_enters (GNCSearchCoreType *fe);
static void pass_parent (GNCSearchCoreType *fe, gpointer parent);
static void grab_focus (GNCSearchCoreType *fe);
static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe);
static gboolean gncs_validate (GNCSearchCoreType *fe);
static GtkWidget *gncs_get_widget(GNCSearchCoreType *fe);
static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe);
static void gnc_search_string_finalize (GObject *obj);
typedef struct _GNCSearchStringPrivate GNCSearchStringPrivate;
struct _GNCSearchStringPrivate
{
GtkWidget *entry;
GtkWindow *parent;
};
G_DEFINE_TYPE_WITH_PRIVATE(GNCSearchString, gnc_search_string, GNC_TYPE_SEARCH_CORE_TYPE)
#define _PRIVATE(o) \
((GNCSearchStringPrivate*)gnc_search_string_get_instance_private((GNCSearchString*)o))
static void
gnc_search_string_class_init (GNCSearchStringClass *klass)
{
GObjectClass *object_class;
GNCSearchCoreTypeClass *gnc_search_core_type = (GNCSearchCoreTypeClass *)klass;
object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gnc_search_string_finalize;
/* override methods */
gnc_search_core_type->editable_enters = editable_enters;
gnc_search_core_type->pass_parent = pass_parent;
gnc_search_core_type->grab_focus = grab_focus;
gnc_search_core_type->validate = gncs_validate;
gnc_search_core_type->get_widget = gncs_get_widget;
gnc_search_core_type->get_predicate = gncs_get_predicate;
gnc_search_core_type->clone = gncs_clone;
}
static void
gnc_search_string_init (GNCSearchString *o)
{
o->value = NULL;
o->how = SEARCH_STRING_CONTAINS;
o->ign_case = TRUE;
}
static void
gnc_search_string_finalize (GObject *obj)
{
GNCSearchString *o = (GNCSearchString *)obj;
g_assert (IS_GNCSEARCH_STRING (o));
g_free (o->value);
G_OBJECT_CLASS (gnc_search_string_parent_class)->finalize(obj);
}
/**
* gnc_search_string_new:
*
* Create a new GNCSearchString object.
*
* Return value: A new #GNCSearchString object.
**/
GNCSearchString *
gnc_search_string_new (void)
{
GNCSearchString *o = g_object_new(GNC_TYPE_SEARCH_STRING, NULL);
return o;
}
void
gnc_search_string_set_value (GNCSearchString *fi, const char *value)
{
g_return_if_fail (fi);
g_return_if_fail (IS_GNCSEARCH_STRING (fi));
if (fi->value)
g_free (fi->value);
fi->value = g_strdup (value);
}
void
gnc_search_string_set_how (GNCSearchString *fi, GNCSearchString_Type how)
{
g_return_if_fail (fi);
g_return_if_fail (IS_GNCSEARCH_STRING (fi));
fi->how = how;
}
void
gnc_search_string_set_case (GNCSearchString *fi, gboolean ignore_case)
{
g_return_if_fail (fi);
g_return_if_fail (IS_GNCSEARCH_STRING (fi));
fi->ign_case = ignore_case;
}
static gboolean
gncs_validate (GNCSearchCoreType *fe)
{
GNCSearchString *fi = (GNCSearchString *)fe;
GNCSearchStringPrivate *priv;
gboolean valid = TRUE;
g_return_val_if_fail (fi, FALSE);
g_return_val_if_fail (IS_GNCSEARCH_STRING (fi), FALSE);
priv = _PRIVATE(fi);
if (!fi->value || *(fi->value) == '\0')
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new (GTK_WINDOW(priv->parent),
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s",
_("You need to enter some search text."));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy(dialog);
return FALSE;
}
if (fi->how == SEARCH_STRING_MATCHES_REGEX ||
fi->how == SEARCH_STRING_NOT_MATCHES_REGEX)
{
regex_t regexpat; /* regex pattern */
gint regerr;
int flags = REG_EXTENDED;
if (fi->ign_case)
flags |= REG_ICASE;
regerr = regcomp (®expat, fi->value, flags);
if (regerr)
{
GtkWidget *dialog;
gchar *regmsg, *errmsg;
size_t reglen;
/* regerror gets called twice to get the full error string
length to do proper posix error reporting */
reglen = regerror (regerr, ®expat, 0, 0);
regmsg = g_malloc0 (reglen + 1);
regerror (regerr, ®expat, regmsg, reglen);
errmsg = g_strdup_printf (_("Error in regular expression '%s':\n%s"),
fi->value, regmsg);
g_free (regmsg);
dialog = gtk_message_dialog_new (GTK_WINDOW(priv->parent),
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", errmsg);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy(dialog);
g_free (errmsg);
valid = FALSE;
}
regfree (®expat);
}
return valid;
}
static void
toggle_changed (GtkToggleButton *button, GNCSearchString *fe)
{
fe->ign_case = !gtk_toggle_button_get_active (button);
}
static void
entry_changed (GtkEntry *entry, GNCSearchString *fe)
{
const char *new_str;
new_str = gtk_entry_get_text(entry);
gnc_search_string_set_value (fe, new_str);
}
static GtkWidget *
make_menu (GNCSearchCoreType *fe)
{
GNCSearchString *fi = (GNCSearchString *)fe;
GtkComboBox *combo;
combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
gnc_combo_box_search_add(combo, _("contains"), SEARCH_STRING_CONTAINS);
gnc_combo_box_search_add(combo, _("equals"), SEARCH_STRING_EQUAL);
gnc_combo_box_search_add(combo, _("matches regex"),
SEARCH_STRING_MATCHES_REGEX);
gnc_combo_box_search_add(combo, _("does not match regex"),
SEARCH_STRING_NOT_MATCHES_REGEX);
gnc_combo_box_search_changed(combo, &fi->how);
gnc_combo_box_search_set_active(combo, fi->how ? fi->how : SEARCH_STRING_CONTAINS);
return GTK_WIDGET(combo);
}
static void
grab_focus (GNCSearchCoreType *fe)
{
GNCSearchString *fi = (GNCSearchString *)fe;
GNCSearchStringPrivate *priv;
g_return_if_fail (fi);
g_return_if_fail (IS_GNCSEARCH_STRING (fi));
priv = _PRIVATE(fi);
if (priv->entry)
gtk_widget_grab_focus (priv->entry);
}
static void
editable_enters (GNCSearchCoreType *fe)
{
GNCSearchString *fi = (GNCSearchString *)fe;
GNCSearchStringPrivate *priv;
g_return_if_fail (fi);
g_return_if_fail (IS_GNCSEARCH_STRING (fi));
priv = _PRIVATE(fi);
if (priv->entry)
gtk_entry_set_activates_default(GTK_ENTRY (priv->entry), TRUE);
}
static void
pass_parent (GNCSearchCoreType *fe, gpointer parent)
{
GNCSearchString *fi = (GNCSearchString *)fe;
GNCSearchStringPrivate *priv;
g_return_if_fail (fi);
g_return_if_fail (IS_GNCSEARCH_STRING (fi));
priv = _PRIVATE(fi);
priv->parent = GTK_WINDOW(parent);
}
static GtkWidget *
gncs_get_widget (GNCSearchCoreType *fe)
{
GtkWidget *entry, *toggle, *menu, *box;
GNCSearchString *fi = (GNCSearchString *)fe;
GNCSearchStringPrivate *priv;
g_return_val_if_fail (fi, NULL);
g_return_val_if_fail (IS_GNCSEARCH_STRING (fi), NULL);
priv = _PRIVATE(fi);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
gtk_box_set_homogeneous (GTK_BOX (box), FALSE);
/* Build and connect the option menu */
menu = make_menu (fe);
gtk_box_pack_start (GTK_BOX (box), menu, FALSE, FALSE, 3);
/* Build and connect the entry window */
entry = gtk_entry_new ();
if (fi->value)
gtk_entry_set_text (GTK_ENTRY (entry), fi->value);
g_signal_connect (G_OBJECT (entry), "changed", G_CALLBACK (entry_changed), fe);
gtk_box_pack_start (GTK_BOX (box), entry, FALSE, FALSE, 3);
priv->entry = entry;
/* Build and connect the case-sensitive check button; defaults to off */
toggle = gtk_check_button_new_with_label (_("Match case"));
g_signal_connect (G_OBJECT(toggle), "toggled", G_CALLBACK (toggle_changed), fe);
gtk_box_pack_start (GTK_BOX (box), toggle, FALSE, FALSE, 3);
/* And return the box */
return box;
}
static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe)
{
GNCSearchString *ss = (GNCSearchString *)fe;
QofQueryCompare how;
QofStringMatch options = QOF_STRING_MATCH_NORMAL;
gboolean is_regex = FALSE;
g_return_val_if_fail (ss, NULL);
g_return_val_if_fail (IS_GNCSEARCH_STRING (ss), NULL);
switch (ss->how)
{
case SEARCH_STRING_MATCHES_REGEX:
is_regex = TRUE;
/* FALL THROUGH */
case SEARCH_STRING_CONTAINS:
how = QOF_COMPARE_CONTAINS;
break;
case SEARCH_STRING_EQUAL:
how = QOF_COMPARE_EQUAL;
break;
case SEARCH_STRING_NOT_MATCHES_REGEX:
is_regex = TRUE;
/* FALL THROUGH */
case SEARCH_STRING_NOT_CONTAINS:
how = QOF_COMPARE_NCONTAINS;
break;
case SEARCH_STRING_NOT_EQUAL:
how = QOF_COMPARE_NEQ;
break;
default:
g_warning ("invalid string choice: %d", ss->how);
return NULL;
}
if (ss->ign_case)
options = QOF_STRING_MATCH_CASEINSENSITIVE;
return qof_query_string_predicate (how, ss->value, options, is_regex);
}
static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe)
{
GNCSearchString *se, *fse = (GNCSearchString *)fe;
g_return_val_if_fail (fse, NULL);
g_return_val_if_fail (IS_GNCSEARCH_STRING (fse), NULL);
se = gnc_search_string_new ();
gnc_search_string_set_value (se, fse->value);
gnc_search_string_set_how (se, fse->how);
gnc_search_string_set_case (se, fse->ign_case);
return (GNCSearchCoreType *)se;
}