tracker-0.16.2/src/libtracker-common/tracker-keyfile-object.c

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
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  */
 19 
 20 #include "config.h"
 21 
 22 #include <libtracker-common/tracker-file-utils.h>
 23 #include <libtracker-common/tracker-type-utils.h>
 24 
 25 #include "tracker-keyfile-object.h"
 26 
 27 static GSList *
 28 directory_string_list_to_gslist (const gchar **value)
 29 {
 30 	GSList *list = NULL;
 31 	gint    i;
 32 
 33 	if (!value) {
 34 		return NULL;
 35 	}
 36 
 37 	for (i = 0; value[i]; i++) {
 38 		const gchar *str;
 39 		gchar       *validated;
 40 
 41 		str = value[i];
 42 		if (!str || str[0] == '\0') {
 43 			continue;
 44 		}
 45 
 46 		/* For directories we validate any special characters,
 47 		 * for example '~' and '../../'
 48 		 */
 49 		validated = tracker_path_evaluate_name (str);
 50 		if (validated) {
 51 			list = g_slist_prepend (list, validated);
 52 		}
 53 	}
 54 
 55 	return g_slist_reverse (list);
 56 }
 57 
 58 const gchar *
 59 tracker_keyfile_object_blurb (gpointer     object,
 60                               const gchar *property)
 61 {
 62 	GObjectClass *klass;
 63 	GParamSpec *spec;
 64 
 65 	g_return_val_if_fail (G_IS_OBJECT (object), NULL);
 66 	g_return_val_if_fail (property != NULL, NULL);
 67 
 68 	klass = G_OBJECT_GET_CLASS (object);
 69 	spec = g_object_class_find_property (G_OBJECT_CLASS (klass), property);
 70 	g_return_val_if_fail (spec != NULL, NULL);
 71 
 72 	return g_param_spec_get_blurb (spec);
 73 }
 74 
 75 gboolean
 76 tracker_keyfile_object_default_boolean (gpointer     object,
 77                                         const gchar *property)
 78 {
 79 	GObjectClass *klass;
 80 	GParamSpec *spec;
 81 	GParamSpecBoolean *bspec;
 82 
 83 	g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
 84 	g_return_val_if_fail (property != NULL, FALSE);
 85 
 86 	klass = G_OBJECT_GET_CLASS (object);
 87 	spec = g_object_class_find_property (G_OBJECT_CLASS (klass), property);
 88 	g_return_val_if_fail (spec != NULL, FALSE);
 89 
 90 	bspec = G_PARAM_SPEC_BOOLEAN (spec);
 91 	g_return_val_if_fail (bspec != NULL, FALSE);
 92 
 93 	return bspec->default_value;
 94 }
 95 
 96 const gchar*
 97 tracker_keyfile_object_default_string (gpointer     object,
 98                                        const gchar *property)
 99 {
100 	GObjectClass *klass;
101 	GParamSpec *spec;
102 	GParamSpecString *bspec;
103 
104 	g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
105 	g_return_val_if_fail (property != NULL, FALSE);
106 
107 	klass = G_OBJECT_GET_CLASS (object);
108 	spec = g_object_class_find_property (G_OBJECT_CLASS (klass), property);
109 	g_return_val_if_fail (spec != NULL, FALSE);
110 
111 	bspec = G_PARAM_SPEC_STRING (spec);
112 	g_return_val_if_fail (bspec != NULL, FALSE);
113 
114 	return bspec->default_value;
115 }
116 
117 gint
118 tracker_keyfile_object_default_int (gpointer     object,
119                                     const gchar *property)
120 {
121 	GObjectClass *klass;
122 	GParamSpec *spec;
123 	GParamSpecInt *ispec;
124 
125 	g_return_val_if_fail (G_IS_OBJECT (object), 0);
126 	g_return_val_if_fail (property != NULL, 0);
127 
128 	klass = G_OBJECT_GET_CLASS (object);
129 	spec = g_object_class_find_property (G_OBJECT_CLASS (klass), property);
130 	g_return_val_if_fail (spec != NULL, 0);
131 
132 	ispec = G_PARAM_SPEC_INT (spec);
133 	g_return_val_if_fail (ispec != NULL, 0);
134 
135 	return ispec->default_value;
136 }
137 
138 gboolean
139 tracker_keyfile_object_validate_int (gpointer     object,
140                                      const gchar *property,
141                                      gint         value)
142 {
143 #ifdef G_DISABLE_CHECKS
144 	GParamSpec *spec;
145 	GValue      gvalue = { 0 };
146 	gboolean    valid;
147 #endif
148 
149 	g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
150 	g_return_val_if_fail (property != NULL, FALSE);
151 
152 #ifdef G_DISABLE_CHECKS
153 	spec = g_object_class_find_property (G_OBJECT_CLASS (object), property);
154 	g_return_val_if_fail (spec != NULL, FALSE);
155 
156 	g_value_init (&gvalue, spec->value_type);
157 	g_value_set_int (&gvalue, value);
158 	valid = g_param_value_validate (spec, &gvalue);
159 	g_value_unset (&gvalue);
160 
161 	g_return_val_if_fail (valid != TRUE, FALSE);
162 #endif
163 
164 	return TRUE;
165 }
166 
167 void
168 tracker_keyfile_object_load_int (gpointer     object,
169                                  const gchar *property,
170                                  GKeyFile    *key_file,
171                                  const gchar *group,
172                                  const gchar *key)
173 {
174 	GError *error = NULL;
175 	gint    value;
176 
177 	g_return_if_fail (G_IS_OBJECT (object));
178 	g_return_if_fail (property != NULL);
179 	g_return_if_fail (key_file != NULL);
180 	g_return_if_fail (group != NULL);
181 	g_return_if_fail (key != NULL);
182 
183 	value = g_key_file_get_integer (key_file, group, key, &error);
184 	if (!error) {
185 		g_object_set (G_OBJECT (object), property, value, NULL);
186 	} else {
187 		g_message ("Couldn't load object property '%s' (int) in group '%s', %s",
188 		           property, group, error->message);
189 		g_error_free (error);
190 	}
191 }
192 
193 void
194 tracker_keyfile_object_load_boolean (gpointer     object,
195                                      const gchar *property,
196                                      GKeyFile    *key_file,
197                                      const gchar *group,
198                                      const gchar *key)
199 {
200 	GError   *error = NULL;
201 	gboolean  value;
202 
203 	g_return_if_fail (G_IS_OBJECT (object));
204 	g_return_if_fail (property != NULL);
205 	g_return_if_fail (key_file != NULL);
206 	g_return_if_fail (group != NULL);
207 	g_return_if_fail (key != NULL);
208 
209 	value = g_key_file_get_boolean (key_file, group, key, &error);
210 	if (!error) {
211 		g_object_set (G_OBJECT (object), property, value, NULL);
212 	} else {
213 		g_message ("Couldn't load object property '%s' (bool) in group '%s', %s",
214 		           property, group, error->message);
215 		g_error_free (error);
216 	}
217 }
218 
219 void
220 tracker_keyfile_object_load_string (gpointer     object,
221                                     const gchar *property,
222                                     GKeyFile    *key_file,
223                                     const gchar *group,
224                                     const gchar *key)
225 {
226 	GError *error = NULL;
227 	gchar  *value;
228 
229 	g_return_if_fail (G_IS_OBJECT (object));
230 	g_return_if_fail (property != NULL);
231 	g_return_if_fail (key_file != NULL);
232 	g_return_if_fail (group != NULL);
233 	g_return_if_fail (key != NULL);
234 
235 	value = g_key_file_get_string (key_file, group, key, &error);
236 	if (!error) {
237 		g_object_set (G_OBJECT (object), property, value, NULL);
238 	} else {
239 		g_message ("Couldn't load object property '%s' (string) in group '%s', %s",
240 		           property, group, error->message);
241 		g_error_free (error);
242 	}
243 
244 	g_free (value);
245 }
246 
247 void
248 tracker_keyfile_object_load_string_list (gpointer      object,
249                                          const gchar  *property,
250                                          GKeyFile     *key_file,
251                                          const gchar  *group,
252                                          const gchar  *key,
253                                          GSList      **return_instead)
254 {
255 	GSList *l;
256 	gchar **value;
257 
258 	g_return_if_fail (G_IS_OBJECT (object));
259 	g_return_if_fail (property != NULL);
260 	g_return_if_fail (key_file != NULL);
261 	g_return_if_fail (group != NULL);
262 	g_return_if_fail (key != NULL);
263 
264 	value = g_key_file_get_string_list (key_file, group, key, NULL, NULL);
265 	l = tracker_string_list_to_gslist (value, -1);
266 	g_strfreev (value);
267 
268 	if (G_LIKELY (!return_instead)) {
269 		g_object_set (G_OBJECT (object), property, l, NULL);
270 
271 		/* List is copied internally */
272 		g_slist_foreach (l, (GFunc) g_free, NULL);
273 		g_slist_free (l);
274 	} else {
275 		*return_instead = l;
276 	}
277 }
278 
279 void
280 tracker_keyfile_object_load_directory_list (gpointer      object,
281                                             const gchar  *property,
282                                             GKeyFile     *key_file,
283                                             const gchar  *group,
284                                             const gchar  *key,
285                                             gboolean      is_recursive,
286                                             GSList      **return_instead)
287 {
288 	GSList *l;
289 	gchar **value;
290 
291 	g_return_if_fail (G_IS_OBJECT (object));
292 	g_return_if_fail (property != NULL);
293 	g_return_if_fail (key_file != NULL);
294 	g_return_if_fail (group != NULL);
295 	g_return_if_fail (key != NULL);
296 
297 	value = g_key_file_get_string_list (key_file, group, key, NULL, NULL);
298 	l = directory_string_list_to_gslist ((const gchar **) value);
299 	g_strfreev (value);
300 
301 	if (l) {
302 		GSList *filtered;
303 
304 		/* Should we make the basename (2nd argument) here
305 		 * part of this function's API?
306 		 */
307 		filtered = tracker_path_list_filter_duplicates (l, ".", is_recursive);
308 
309 		g_slist_foreach (l, (GFunc) g_free, NULL);
310 		g_slist_free (l);
311 
312 		l = filtered;
313 	}
314 
315 	if (G_LIKELY (!return_instead)) {
316 		g_object_set (G_OBJECT (object), property, l, NULL);
317 
318 		/* List is copied internally */
319 		g_slist_foreach (l, (GFunc) g_free, NULL);
320 		g_slist_free (l);
321 	} else {
322 		*return_instead = l;
323 	}
324 }
325 
326 void
327 tracker_keyfile_object_save_int (gpointer     object,
328                                  const gchar *property,
329                                  GKeyFile    *key_file,
330                                  const gchar *group,
331                                  const gchar *key)
332 {
333 	gint value;
334 
335 	g_return_if_fail (G_IS_OBJECT (object));
336 	g_return_if_fail (property != NULL);
337 	g_return_if_fail (key_file != NULL);
338 	g_return_if_fail (group != NULL);
339 	g_return_if_fail (key != NULL);
340 
341 	g_object_get (G_OBJECT (object), property, &value, NULL);
342 	g_key_file_set_integer (key_file, group, key, value);
343 }
344 
345 void
346 tracker_keyfile_object_save_boolean (gpointer     object,
347                                      const gchar *property,
348                                      GKeyFile    *key_file,
349                                      const gchar *group,
350                                      const gchar *key)
351 {
352 	gboolean value;
353 
354 	g_return_if_fail (G_IS_OBJECT (object));
355 	g_return_if_fail (property != NULL);
356 	g_return_if_fail (key_file != NULL);
357 	g_return_if_fail (group != NULL);
358 	g_return_if_fail (key != NULL);
359 
360 	g_object_get (G_OBJECT (object), property, &value, NULL);
361 	g_key_file_set_boolean (key_file, group, key, value);
362 }
363 
364 void
365 tracker_keyfile_object_save_string (gpointer     object,
366                                     const gchar *property,
367                                     GKeyFile    *key_file,
368                                     const gchar *group,
369                                     const gchar         *key)
370 {
371 	gchar *value;
372 
373 	g_return_if_fail (G_IS_OBJECT (object));
374 	g_return_if_fail (property != NULL);
375 	g_return_if_fail (key_file != NULL);
376 	g_return_if_fail (group != NULL);
377 	g_return_if_fail (key != NULL);
378 
379 	g_object_get (G_OBJECT (object), property, &value, NULL);
380 	g_key_file_set_string (key_file, group, key, value);
381 	g_free (value);
382 }
383 
384 void
385 tracker_keyfile_object_save_string_list (gpointer     object,
386                                          const gchar *property,
387                                          GKeyFile    *key_file,
388                                          const gchar *group,
389                                          const gchar *key)
390 {
391 	GSList *list;
392 	gchar **value;
393 
394 	g_return_if_fail (G_IS_OBJECT (object));
395 	g_return_if_fail (property != NULL);
396 	g_return_if_fail (key_file != NULL);
397 	g_return_if_fail (group != NULL);
398 	g_return_if_fail (key != NULL);
399 
400 	g_object_get (G_OBJECT (object), property, &list, NULL);
401 
402 	value = tracker_gslist_to_string_list (list);
403 	g_key_file_set_string_list (key_file,
404 	                            group,
405 	                            key,
406 	                            (const gchar * const *) value,
407 	                            (gsize) g_slist_length (list));
408 	g_strfreev (value);
409 }
410 
411 void
412 tracker_keyfile_object_save_directory_list (gpointer     object,
413                                             const gchar *property,
414                                             GKeyFile    *key_file,
415                                             const gchar *group,
416                                             const gchar *key)
417 {
418 	GSList *list;
419 	gchar **value;
420 
421 	g_return_if_fail (G_IS_OBJECT (object));
422 	g_return_if_fail (property != NULL);
423 	g_return_if_fail (key_file != NULL);
424 	g_return_if_fail (group != NULL);
425 	g_return_if_fail (key != NULL);
426 
427 	g_object_get (G_OBJECT (object), property, &list, NULL);
428 
429 	value = tracker_gslist_to_string_list (list);
430 	g_key_file_set_string_list (key_file,
431 	                            group,
432 	                            key,
433 	                            (const gchar * const *) value,
434 	                            (gsize) g_slist_length (list));
435 	g_strfreev (value);
436 }