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 | char *text; |
30 | GFile *location; |
31 | GList *mime_types; |
32 | char **words; |
33 | }; |
34 | |
35 | G_DEFINE_TYPE_WITH_PRIVATE (GtkQuery, gtk_query, G_TYPE_OBJECT) |
36 | |
37 | static void |
38 | finalize (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 | |
50 | static void |
51 | gtk_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 | |
59 | static void |
60 | gtk_query_init (GtkQuery *query) |
61 | { |
62 | } |
63 | |
64 | GtkQuery * |
65 | gtk_query_new (void) |
66 | { |
67 | return g_object_new (GTK_TYPE_QUERY, NULL); |
68 | } |
69 | |
70 | |
71 | const char * |
72 | gtk_query_get_text (GtkQuery *query) |
73 | { |
74 | GtkQueryPrivate *priv = gtk_query_get_instance_private (self: query); |
75 | |
76 | return priv->text; |
77 | } |
78 | |
79 | void |
80 | gtk_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 | |
92 | GFile * |
93 | gtk_query_get_location (GtkQuery *query) |
94 | { |
95 | GtkQueryPrivate *priv = gtk_query_get_instance_private (self: query); |
96 | |
97 | return priv->location; |
98 | } |
99 | |
100 | void |
101 | gtk_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 | |
109 | static char * |
110 | prepare_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 | |
121 | gboolean |
122 | gtk_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 | |