No issues found
1 /*
2 * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
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.1 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20 #include "config.h"
21
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <glib.h>
26 #include <gio/gio.h>
27
28 #include <libtracker-common/tracker-keyfile-object.h>
29
30 #include "tracker-fts-config.h"
31
32 /* GKeyFile defines */
33 #define GROUP_INDEXING "Indexing"
34
35 /* Default values */
36 #define DEFAULT_MAX_WORD_LENGTH 30 /* 0->200 */
37 #define DEFAULT_MAX_WORDS_TO_INDEX 10000
38 #define DEFAULT_IGNORE_NUMBERS TRUE
39 #define DEFAULT_IGNORE_STOP_WORDS TRUE
40 #define DEFAULT_ENABLE_STEMMER FALSE /* As per GB#526346, disabled */
41 #define DEFAULT_ENABLE_UNACCENT TRUE
42
43 static void config_set_property (GObject *object,
44 guint param_id,
45 const GValue *value,
46 GParamSpec *pspec);
47 static void config_get_property (GObject *object,
48 guint param_id,
49 GValue *value,
50 GParamSpec *pspec);
51 static void config_finalize (GObject *object);
52 static void config_constructed (GObject *object);
53
54 enum {
55 PROP_0,
56
57 /* Indexing */
58 PROP_MAX_WORD_LENGTH,
59 PROP_ENABLE_STEMMER,
60 PROP_ENABLE_UNACCENT,
61 PROP_IGNORE_NUMBERS,
62 PROP_IGNORE_STOP_WORDS,
63
64 /* Performance */
65 PROP_MAX_WORDS_TO_INDEX,
66 };
67
68 static TrackerConfigMigrationEntry migration[] = {
69 { G_TYPE_INT, GROUP_INDEXING, "MaxWordLength", "max-word-length" },
70 { G_TYPE_BOOLEAN, GROUP_INDEXING, "EnableStemmer" , "enable-stemmer" },
71 { G_TYPE_BOOLEAN, GROUP_INDEXING, "EnableUnaccent", "enable-unaccent" },
72 { G_TYPE_BOOLEAN, GROUP_INDEXING, "IgnoreNumbers", "ignore-numbers" },
73 { G_TYPE_BOOLEAN, GROUP_INDEXING, "IgnoreStopWords", "ignore-stop-words" },
74 { G_TYPE_INT, GROUP_INDEXING, "MaxWordsToIndex", "max-words-to-index" },
75 };
76
77 G_DEFINE_TYPE (TrackerFTSConfig, tracker_fts_config, G_TYPE_SETTINGS);
78
79 static void
80 tracker_fts_config_class_init (TrackerFTSConfigClass *klass)
81 {
82 GObjectClass *object_class = G_OBJECT_CLASS (klass);
83
84 object_class->set_property = config_set_property;
85 object_class->get_property = config_get_property;
86 object_class->finalize = config_finalize;
87 object_class->constructed = config_constructed;
88
89 /* Indexing */
90 g_object_class_install_property (object_class,
91 PROP_MAX_WORD_LENGTH,
92 g_param_spec_int ("max-word-length",
93 "Maximum word length",
94 " Set the maximum length of words to index (0->200, default=30)",
95 0,
96 200, /* Is this a reasonable limit? */
97 DEFAULT_MAX_WORD_LENGTH,
98 G_PARAM_READWRITE));
99 g_object_class_install_property (object_class,
100 PROP_ENABLE_STEMMER,
101 g_param_spec_boolean ("enable-stemmer",
102 "Enable Stemmer",
103 " Flag to enable word stemming utility (default=FALSE)",
104 DEFAULT_ENABLE_STEMMER,
105 G_PARAM_READWRITE));
106 g_object_class_install_property (object_class,
107 PROP_ENABLE_UNACCENT,
108 g_param_spec_boolean ("enable-unaccent",
109 "Enable Unaccent",
110 " Flag to enable word unaccenting (default=TRUE)",
111 DEFAULT_ENABLE_UNACCENT,
112 G_PARAM_READWRITE));
113 g_object_class_install_property (object_class,
114 PROP_IGNORE_NUMBERS,
115 g_param_spec_boolean ("ignore-numbers",
116 "Ignore numbers",
117 " Flag to ignore numbers in FTS (default=TRUE)",
118 DEFAULT_IGNORE_NUMBERS,
119 G_PARAM_READWRITE));
120 g_object_class_install_property (object_class,
121 PROP_IGNORE_STOP_WORDS,
122 g_param_spec_boolean ("ignore-stop-words",
123 "Ignore stop words",
124 " Flag to ignore stop words in FTS (default=TRUE)",
125 DEFAULT_IGNORE_STOP_WORDS,
126 G_PARAM_READWRITE));
127 g_object_class_install_property (object_class,
128 PROP_MAX_WORDS_TO_INDEX,
129 g_param_spec_int ("max-words-to-index",
130 "Maximum words to index",
131 " Maximum unique words to index from a file's content (default=10000)",
132 0,
133 G_MAXINT,
134 DEFAULT_MAX_WORDS_TO_INDEX,
135 G_PARAM_READWRITE));
136
137 }
138
139 static void
140 tracker_fts_config_init (TrackerFTSConfig *object)
141 {
142 }
143
144 static void
145 config_set_property (GObject *object,
146 guint param_id,
147 const GValue *value,
148 GParamSpec *pspec)
149 {
150 switch (param_id) {
151 /* Indexing */
152 case PROP_MAX_WORD_LENGTH:
153 tracker_fts_config_set_max_word_length (TRACKER_FTS_CONFIG (object),
154 g_value_get_int (value));
155 break;
156 case PROP_ENABLE_STEMMER:
157 tracker_fts_config_set_enable_stemmer (TRACKER_FTS_CONFIG (object),
158 g_value_get_boolean (value));
159 break;
160 case PROP_ENABLE_UNACCENT:
161 tracker_fts_config_set_enable_unaccent (TRACKER_FTS_CONFIG (object),
162 g_value_get_boolean (value));
163 break;
164 case PROP_IGNORE_NUMBERS:
165 tracker_fts_config_set_ignore_numbers (TRACKER_FTS_CONFIG (object),
166 g_value_get_boolean (value));
167 break;
168 case PROP_IGNORE_STOP_WORDS:
169 tracker_fts_config_set_ignore_stop_words (TRACKER_FTS_CONFIG (object),
170 g_value_get_boolean (value));
171 break;
172 case PROP_MAX_WORDS_TO_INDEX:
173 tracker_fts_config_set_max_words_to_index (TRACKER_FTS_CONFIG (object),
174 g_value_get_int (value));
175 break;
176
177 default:
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
179 break;
180 };
181 }
182
183 static void
184 config_get_property (GObject *object,
185 guint param_id,
186 GValue *value,
187 GParamSpec *pspec)
188 {
189 TrackerFTSConfig *config = TRACKER_FTS_CONFIG (object);
190
191 switch (param_id) {
192 /* Indexing */
193 case PROP_MAX_WORD_LENGTH:
194 g_value_set_int (value, tracker_fts_config_get_max_word_length (config));
195 break;
196 case PROP_ENABLE_STEMMER:
197 g_value_set_boolean (value, tracker_fts_config_get_enable_stemmer (config));
198 break;
199 case PROP_ENABLE_UNACCENT:
200 g_value_set_boolean (value, tracker_fts_config_get_enable_unaccent (config));
201 break;
202 case PROP_IGNORE_NUMBERS:
203 g_value_set_boolean (value, tracker_fts_config_get_ignore_numbers (config));
204 break;
205 case PROP_IGNORE_STOP_WORDS:
206 g_value_set_boolean (value, tracker_fts_config_get_ignore_stop_words (config));
207 break;
208 case PROP_MAX_WORDS_TO_INDEX:
209 g_value_set_int (value, tracker_fts_config_get_max_words_to_index (config));
210 break;
211
212 default:
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
214 break;
215 };
216 }
217
218 static void
219 config_finalize (GObject *object)
220 {
221 /* For now we do nothing here, we left this override in for
222 * future expansion.
223 */
224
225 (G_OBJECT_CLASS (tracker_fts_config_parent_class)->finalize) (object);
226 }
227
228 static void
229 config_constructed (GObject *object)
230 {
231 TrackerConfigFile *config_file;
232
233 (G_OBJECT_CLASS (tracker_fts_config_parent_class)->constructed) (object);
234
235 g_settings_delay (G_SETTINGS (object));
236
237 /* migrate keyfile-based configuration */
238 config_file = tracker_config_file_new ();
239 if (config_file) {
240 tracker_config_file_migrate (config_file, G_SETTINGS (object), migration);
241 g_object_unref (config_file);
242 }
243 }
244
245 TrackerFTSConfig *
246 tracker_fts_config_new (void)
247 {
248 return g_object_new (TRACKER_TYPE_FTS_CONFIG,
249 "schema", "org.freedesktop.Tracker.FTS",
250 "path", "/org/freedesktop/tracker/fts/",
251 NULL);
252 }
253
254 gboolean
255 tracker_fts_config_save (TrackerFTSConfig *config)
256 {
257 g_return_val_if_fail (TRACKER_IS_FTS_CONFIG (config), FALSE);
258
259 g_settings_apply (G_SETTINGS (config));
260
261 return TRUE;
262 }
263
264 gint
265 tracker_fts_config_get_max_word_length (TrackerFTSConfig *config)
266 {
267 g_return_val_if_fail (TRACKER_IS_FTS_CONFIG (config), DEFAULT_MAX_WORD_LENGTH);
268
269 return g_settings_get_int (G_SETTINGS (config), "max-word-length");
270 }
271
272 gboolean
273 tracker_fts_config_get_enable_stemmer (TrackerFTSConfig *config)
274 {
275 g_return_val_if_fail (TRACKER_IS_FTS_CONFIG (config), DEFAULT_ENABLE_STEMMER);
276
277 return g_settings_get_boolean (G_SETTINGS (config), "enable-stemmer");
278 }
279
280 gboolean
281 tracker_fts_config_get_enable_unaccent (TrackerFTSConfig *config)
282 {
283 g_return_val_if_fail (TRACKER_IS_FTS_CONFIG (config), DEFAULT_ENABLE_UNACCENT);
284
285 return g_settings_get_boolean (G_SETTINGS (config), "enable-unaccent");
286 }
287
288 gboolean
289 tracker_fts_config_get_ignore_numbers (TrackerFTSConfig *config)
290 {
291 g_return_val_if_fail (TRACKER_IS_FTS_CONFIG (config), DEFAULT_IGNORE_NUMBERS);
292
293 return g_settings_get_boolean (G_SETTINGS (config), "ignore-numbers");
294 }
295
296 gboolean
297 tracker_fts_config_get_ignore_stop_words (TrackerFTSConfig *config)
298 {
299 g_return_val_if_fail (TRACKER_IS_FTS_CONFIG (config), DEFAULT_IGNORE_STOP_WORDS);
300
301 return g_settings_get_boolean (G_SETTINGS (config), "ignore-stop-words");
302 }
303
304 gint
305 tracker_fts_config_get_max_words_to_index (TrackerFTSConfig *config)
306 {
307 g_return_val_if_fail (TRACKER_IS_FTS_CONFIG (config), DEFAULT_MAX_WORDS_TO_INDEX);
308
309 return g_settings_get_int (G_SETTINGS (config), "max-words-to-index");
310 }
311
312 void
313 tracker_fts_config_set_max_word_length (TrackerFTSConfig *config,
314 gint value)
315 {
316 g_return_if_fail (TRACKER_IS_FTS_CONFIG (config));
317
318 g_settings_set_int (G_SETTINGS (config), "max-word-length", value);
319 g_object_notify (G_OBJECT (config), "max-word-length");
320 }
321
322 void
323 tracker_fts_config_set_enable_stemmer (TrackerFTSConfig *config,
324 gboolean value)
325 {
326 g_return_if_fail (TRACKER_IS_FTS_CONFIG (config));
327
328 g_settings_set_boolean (G_SETTINGS (config), "enable-stemmer", value);
329 g_object_notify (G_OBJECT (config), "enable-stemmer");
330 }
331
332 void
333 tracker_fts_config_set_enable_unaccent (TrackerFTSConfig *config,
334 gboolean value)
335 {
336 g_return_if_fail (TRACKER_IS_FTS_CONFIG (config));
337
338 g_settings_set_boolean (G_SETTINGS (config), "enable-unaccent", value);
339 g_object_notify (G_OBJECT (config), "enable-unaccent");
340 }
341
342 void
343 tracker_fts_config_set_ignore_numbers (TrackerFTSConfig *config,
344 gboolean value)
345 {
346 g_return_if_fail (TRACKER_IS_FTS_CONFIG (config));
347
348 g_settings_set_boolean (G_SETTINGS (config), "ignore-numbers", value);
349 g_object_notify (G_OBJECT (config), "ignore-numbers");
350 }
351
352 void
353 tracker_fts_config_set_ignore_stop_words (TrackerFTSConfig *config,
354 gboolean value)
355 {
356 g_return_if_fail (TRACKER_IS_FTS_CONFIG (config));
357
358 g_settings_set_boolean (G_SETTINGS (config), "ignore-stop-words", value);
359 g_object_notify (G_OBJECT (config), "ignore-stop-words");
360 }
361
362 void
363 tracker_fts_config_set_max_words_to_index (TrackerFTSConfig *config,
364 gint value)
365 {
366 g_return_if_fail (TRACKER_IS_FTS_CONFIG (config));
367
368 g_settings_set_int (G_SETTINGS (config), "max-words-to-index", value);
369 g_object_notify (G_OBJECT (config), "max-words-to-index");
370 }