1/*
2 * Copyright (C) 2005 Novell, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Anders Carlsson <andersca@imendio.com>
18 *
19 * Based on nautilus-query.c
20 */
21
22#include "config.h"
23#include <string.h>
24
25#include "gtkquery.h"
26
27struct _GtkQueryPrivate
28{
29 char *text;
30 GFile *location;
31 GList *mime_types;
32 char **words;
33};
34
35G_DEFINE_TYPE_WITH_PRIVATE (GtkQuery, gtk_query, G_TYPE_OBJECT)
36
37static void
38finalize (GObject *object)
39{
40 GtkQuery *query = GTK_QUERY (object);
41 GtkQueryPrivate *priv = gtk_query_get_instance_private (self: query);
42
43 g_clear_object (&priv->location);
44 g_free (mem: priv->text);
45 g_strfreev (str_array: priv->words);
46
47 G_OBJECT_CLASS (gtk_query_parent_class)->finalize (object);
48}
49
50static void
51gtk_query_class_init (GtkQueryClass *class)
52{
53 GObjectClass *gobject_class;
54
55 gobject_class = G_OBJECT_CLASS (class);
56 gobject_class->finalize = finalize;
57}
58
59static void
60gtk_query_init (GtkQuery *query)
61{
62}
63
64GtkQuery *
65gtk_query_new (void)
66{
67 return g_object_new (GTK_TYPE_QUERY, NULL);
68}
69
70
71const char *
72gtk_query_get_text (GtkQuery *query)
73{
74 GtkQueryPrivate *priv = gtk_query_get_instance_private (self: query);
75
76 return priv->text;
77}
78
79void
80gtk_query_set_text (GtkQuery *query,
81 const char *text)
82{
83 GtkQueryPrivate *priv = gtk_query_get_instance_private (self: query);
84
85 g_free (mem: priv->text);
86 priv->text = g_strdup (str: text);
87
88 g_strfreev (str_array: priv->words);
89 priv->words = NULL;
90}
91
92GFile *
93gtk_query_get_location (GtkQuery *query)
94{
95 GtkQueryPrivate *priv = gtk_query_get_instance_private (self: query);
96
97 return priv->location;
98}
99
100void
101gtk_query_set_location (GtkQuery *query,
102 GFile *file)
103{
104 GtkQueryPrivate *priv = gtk_query_get_instance_private (self: query);
105
106 g_set_object (&priv->location, file);
107}
108
109static char *
110prepare_string_for_compare (const char *string)
111{
112 char *normalized, *res;
113
114 normalized = g_utf8_normalize (str: string, len: -1, mode: G_NORMALIZE_NFD);
115 res = g_utf8_strdown (str: normalized, len: -1);
116 g_free (mem: normalized);
117
118 return res;
119}
120
121gboolean
122gtk_query_matches_string (GtkQuery *query,
123 const char *string)
124{
125 GtkQueryPrivate *priv = gtk_query_get_instance_private (self: query);
126 char *prepared;
127 gboolean found;
128 int i;
129
130 if (!priv->text)
131 return FALSE;
132
133 if (!priv->words)
134 {
135 prepared = prepare_string_for_compare (string: priv->text);
136 priv->words = g_strsplit (string: prepared, delimiter: " ", max_tokens: -1);
137 g_free (mem: prepared);
138 }
139
140 prepared = prepare_string_for_compare (string);
141
142 found = TRUE;
143 for (i = 0; priv->words[i]; i++)
144 {
145 if (strstr (haystack: prepared, needle: priv->words[i]) == NULL)
146 {
147 found = FALSE;
148 break;
149 }
150 }
151
152 g_free (mem: prepared);
153
154 return found;
155}
156

source code of gtk/gtk/gtkquery.c