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
27 struct _GtkQueryPrivate
28 {
29 gchar *text;
30 GFile *location;
31 GList *mime_types;
32 gchar **words;
33 };
34
G_DEFINE_TYPE_WITH_PRIVATE(GtkQuery,gtk_query,G_TYPE_OBJECT)35 G_DEFINE_TYPE_WITH_PRIVATE (GtkQuery, gtk_query, G_TYPE_OBJECT)
36
37 static void
38 finalize (GObject *object)
39 {
40 GtkQuery *query;
41
42 query = GTK_QUERY (object);
43
44 g_clear_object (&query->priv->location);
45 g_free (query->priv->text);
46 g_strfreev (query->priv->words);
47
48 G_OBJECT_CLASS (gtk_query_parent_class)->finalize (object);
49 }
50
51 static void
gtk_query_class_init(GtkQueryClass * class)52 gtk_query_class_init (GtkQueryClass *class)
53 {
54 GObjectClass *gobject_class;
55
56 gobject_class = G_OBJECT_CLASS (class);
57 gobject_class->finalize = finalize;
58 }
59
60 static void
gtk_query_init(GtkQuery * query)61 gtk_query_init (GtkQuery *query)
62 {
63 query->priv = gtk_query_get_instance_private (query);
64 }
65
66 GtkQuery *
gtk_query_new(void)67 gtk_query_new (void)
68 {
69 return g_object_new (GTK_TYPE_QUERY, NULL);
70 }
71
72
73 const gchar *
gtk_query_get_text(GtkQuery * query)74 gtk_query_get_text (GtkQuery *query)
75 {
76 return query->priv->text;
77 }
78
79 void
gtk_query_set_text(GtkQuery * query,const gchar * text)80 gtk_query_set_text (GtkQuery *query,
81 const gchar *text)
82 {
83 g_free (query->priv->text);
84 query->priv->text = g_strdup (text);
85
86 g_strfreev (query->priv->words);
87 query->priv->words = NULL;
88 }
89
90 GFile *
gtk_query_get_location(GtkQuery * query)91 gtk_query_get_location (GtkQuery *query)
92 {
93 return query->priv->location;
94 }
95
96 void
gtk_query_set_location(GtkQuery * query,GFile * file)97 gtk_query_set_location (GtkQuery *query,
98 GFile *file)
99 {
100 g_set_object (&query->priv->location, file);
101 }
102
103 static gchar *
prepare_string_for_compare(const gchar * string)104 prepare_string_for_compare (const gchar *string)
105 {
106 gchar *normalized, *res;
107
108 normalized = g_utf8_normalize (string, -1, G_NORMALIZE_NFD);
109 res = g_utf8_strdown (normalized, -1);
110 g_free (normalized);
111
112 return res;
113 }
114
115 gboolean
gtk_query_matches_string(GtkQuery * query,const gchar * string)116 gtk_query_matches_string (GtkQuery *query,
117 const gchar *string)
118 {
119 gchar *prepared;
120 gboolean found;
121 gint i;
122
123 if (!query->priv->text)
124 return FALSE;
125
126 if (!query->priv->words)
127 {
128 prepared = prepare_string_for_compare (query->priv->text);
129 query->priv->words = g_strsplit (prepared, " ", -1);
130 g_free (prepared);
131 }
132
133 prepared = prepare_string_for_compare (string);
134
135 found = TRUE;
136 for (i = 0; query->priv->words[i]; i++)
137 {
138 if (strstr (prepared, query->priv->words[i]) == NULL)
139 {
140 found = FALSE;
141 break;
142 }
143 }
144
145 g_free (prepared);
146
147 return found;
148 }
149