Fix string errors reported by Christian Kirbach
[nautilus-actions.git] / src / core / na-gconf-utils.c
blob4bc345078dbf637d65875998bacc6a2e5fc59b06
1 /*
2 * Nautilus-Actions
3 * A Nautilus extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009, 2010, 2011 Pierre Wieser and others (see AUTHORS)
9 * This Program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This Program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this Library; see the file COPYING. If not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307, USA.
24 * Authors:
25 * Frederic Ruaudel <grumz@grumz.net>
26 * Rodrigo Moya <rodrigo@gnome-db.org>
27 * Pierre Wieser <pwieser@trychlos.org>
28 * ... and many others (see AUTHORS)
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <string.h>
37 #include <api/na-core-utils.h>
38 #include <api/na-gconf-utils.h>
40 static void dump_entry( GConfEntry *entry, void *user_data );
41 static GConfValue *read_value( GConfClient *gconf, const gchar *path, gboolean use_schema, GConfValueType type );
42 static gboolean sync_gconf( GConfClient *gconf, gchar **message );
44 /**
45 * na_gconf_utils_get_subdirs:
46 * @gconf: a GConfClient instance.
47 * @path: a full path to be read.
49 * Returns: a list of full path subdirectories.
51 * The returned list should be na_gconf_utils_free_subdirs() by the caller.
53 * Since: 2.30
55 * Deprecated: 3.1.0
57 GSList *
58 na_gconf_utils_get_subdirs( GConfClient *gconf, const gchar *path )
60 static const gchar *thisfn = "na_gconf_utils_get_subdirs";
61 GError *error = NULL;
62 GSList *list_subdirs;
64 list_subdirs = gconf_client_all_dirs( gconf, path, &error );
66 if( error ){
67 g_warning( "%s: path=%s, error=%s", thisfn, path, error->message );
68 g_error_free( error );
69 return(( GSList * ) NULL );
72 return( list_subdirs );
75 /**
76 * na_gconf_utils_free_subdirs:
77 * @subdirs: the subdirectory list as returned from na_gconf_utils_get_subdirs().
79 * Release the list.
81 * Since: 2.30
83 * Deprecated: 3.1.0
85 void
86 na_gconf_utils_free_subdirs( GSList *subdirs )
88 na_core_utils_slist_free( subdirs );
91 /**
92 * na_gconf_utils_has_entry:
93 * @entries: the list of entries as returned by na_gconf_utils_get_entries().
94 * @entry: the entry to be tested.
96 * Returns: %TRUE if the given @entry exists in the specified @entries,
97 * %FALSE else.
99 * Since: 2.30
101 * Deprecated: 3.1.0
103 gboolean
104 na_gconf_utils_has_entry( GSList *entries, const gchar *entry )
106 GSList *ie;
108 for( ie = entries ; ie ; ie = ie->next ){
109 gchar *key = g_path_get_basename( gconf_entry_get_key( ( GConfEntry * ) ie->data));
110 int res = strcmp( key, entry );
111 g_free( key );
112 if( res == 0 ){
113 return( TRUE );
117 return( FALSE );
121 * na_gconf_utils_get_entries:
122 * @gconf: a GConfClient instance.
123 * @path: a full path to be read.
125 * Loads all the key=value pairs of the specified key.
127 * Returns: a list of #GConfEntry.
129 * The returned list is not recursive : it contains only the immediate
130 * children of @path. To free the returned list, call
131 * na_gconf_utils_free_entries().
133 * Since: 2.30
135 * Deprecated: 3.1.0
137 GSList *
138 na_gconf_utils_get_entries( GConfClient *gconf, const gchar *path )
140 static const gchar *thisfn = "na_gconf_utils_get_entries";
141 GError *error = NULL;
142 GSList *list_entries;
144 list_entries = gconf_client_all_entries( gconf, path, &error );
146 if( error ){
147 g_warning( "%s: path=%s, error=%s", thisfn, path, error->message );
148 g_error_free( error );
149 return(( GSList * ) NULL );
152 return( list_entries );
156 * na_gconf_utils_get_bool_from_entries:
157 * @entries: a list of #GConfEntry as returned by na_gconf_utils_get_entries().
158 * @entry: the searched entry.
159 * @value: a pointer to a gboolean to be set to the found value.
161 * Returns: %TRUE if the entry was found, %FALSE else.
163 * If the entry was not found, or was not of boolean type, @value is set
164 * to %FALSE.
166 * Since: 2.30
168 * Deprecated: 3.1.0
170 gboolean
171 na_gconf_utils_get_bool_from_entries( GSList *entries, const gchar *entry, gboolean *value )
173 GSList *ip;
174 GConfEntry *gconf_entry;
175 GConfValue *gconf_value;
176 gchar *key;
177 gboolean found;
179 g_return_val_if_fail( value, FALSE );
181 *value = FALSE;
182 found = FALSE;
184 for( ip = entries ; ip && !found ; ip = ip->next ){
185 gconf_entry = ( GConfEntry * ) ip->data;
186 key = g_path_get_basename( gconf_entry_get_key( gconf_entry ));
188 if( !strcmp( key, entry )){
189 gconf_value = gconf_entry_get_value( gconf_entry );
191 if( gconf_value &&
192 gconf_value->type == GCONF_VALUE_BOOL ){
194 found = TRUE;
195 *value = gconf_value_get_bool( gconf_value );
198 g_free( key );
201 return( found );
205 * na_gconf_utils_get_string_from_entries:
206 * @entries: a list of #GConfEntry as returned by na_gconf_utils_get_entries().
207 * @entry: the searched entry.
208 * @value: a pointer to a gchar * to be set to the found value.
210 * Returns: %TRUE if the entry was found, %FALSE else.
212 * If the entry was not found, or was not of string type, @value is set
213 * to %NULL.
215 * If @value is returned not NULL, it should be g_free() by the caller.
217 * Since: 2.30
219 * Deprecated: 3.1.0
221 gboolean
222 na_gconf_utils_get_string_from_entries( GSList *entries, const gchar *entry, gchar **value )
224 GSList *ip;
225 GConfEntry *gconf_entry;
226 GConfValue *gconf_value;
227 gchar *key;
228 gboolean found;
230 g_return_val_if_fail( value, FALSE );
232 *value = NULL;
233 found = FALSE;
235 for( ip = entries ; ip && !found ; ip = ip->next ){
236 gconf_entry = ( GConfEntry * ) ip->data;
237 key = g_path_get_basename( gconf_entry_get_key( gconf_entry ));
239 if( !strcmp( key, entry )){
240 gconf_value = gconf_entry_get_value( gconf_entry );
242 if( gconf_value &&
243 gconf_value->type == GCONF_VALUE_STRING ){
245 found = TRUE;
246 *value = g_strdup( gconf_value_get_string( gconf_value ));
249 g_free( key );
252 return( found );
256 * na_gconf_utils_get_string_list_from_entries:
257 * @entries: a list of #GConfEntry as returned by na_gconf_utils_get_entries().
258 * @entry: the searched entry.
259 * @value: a pointer to a GSList * to be set to the found value.
261 * Returns: %TRUE if the entry was found, %FALSE else.
263 * If the entry was not found, or was not of string list type, @value
264 * is set to %NULL.
266 * If @value is returned not NULL, it should be na_core_utils_slist_free()
267 * by the caller.
269 * Since: 2.30
271 * Deprecated: 3.1.0
273 gboolean
274 na_gconf_utils_get_string_list_from_entries( GSList *entries, const gchar *entry, GSList **value )
276 GSList *ip, *iv;
277 GConfEntry *gconf_entry;
278 GConfValue *gconf_value;
279 gchar *key;
280 gboolean found;
281 GSList *list_values;
283 g_return_val_if_fail( value, FALSE );
285 *value = NULL;
286 found = FALSE;
288 for( ip = entries ; ip && !found ; ip = ip->next ){
289 gconf_entry = ( GConfEntry * ) ip->data;
290 key = g_path_get_basename( gconf_entry_get_key( gconf_entry ));
292 if( !strcmp( key, entry )){
293 gconf_value = gconf_entry_get_value( gconf_entry );
295 if( gconf_value &&
296 gconf_value->type == GCONF_VALUE_LIST ){
298 found = TRUE;
299 list_values = gconf_value_get_list( gconf_value );
300 for( iv = list_values ; iv ; iv = iv->next ){
301 *value = g_slist_append( *value, g_strdup( gconf_value_get_string(( GConfValue * ) iv->data )));
305 g_free( key );
308 return( found );
312 * na_gconf_utils_dump_entries:
313 * @entries: a list of #GConfEntry as returned by na_gconf_utils_get_entries().
315 * Dumps the content of the entries.
317 * Since: 2.30
319 * Deprecated: 3.1.0
321 void
322 na_gconf_utils_dump_entries( GSList *entries )
324 g_slist_foreach( entries, ( GFunc ) dump_entry, NULL );
327 static void
328 dump_entry( GConfEntry *entry, void *user_data )
330 static const gchar *thisfn = "na_gconf_utils_dump_entry";
331 gchar *str = NULL;
332 gboolean str_free = FALSE;
334 gchar *key = g_path_get_basename( gconf_entry_get_key( entry ));
335 GConfValue *value = gconf_entry_get_value( entry );
337 if( value ){
338 switch( value->type ){
339 case GCONF_VALUE_STRING:
340 str = ( gchar * ) gconf_value_get_string( value );
341 break;
343 case GCONF_VALUE_INT:
344 str = g_strdup_printf( "%d", gconf_value_get_int( value ));
345 str_free = TRUE;
346 break;
348 case GCONF_VALUE_FLOAT:
349 str = g_strdup_printf( "%f", gconf_value_get_float( value ));
350 str_free = TRUE;
351 break;
353 case GCONF_VALUE_BOOL:
354 str = g_strdup_printf( "%s", gconf_value_get_bool( value ) ? "True":"False" );
355 str_free = TRUE;
356 break;
358 default:
359 str = g_strdup( "(undetermined value)" );
360 str_free = TRUE;
364 g_debug( "%s: key=%s, value=%s", thisfn, key, str );
366 if( str_free ){
367 g_free( str );
370 g_free( key );
374 * na_gconf_utils_free_entries:
375 * @entries: a list of #GConfEntry as returned by na_gconf_utils_get_entries().
377 * Releases the provided list.
379 * Since: 2.30
381 * Deprecated: 3.1.0
383 void
384 na_gconf_utils_free_entries( GSList *entries )
386 g_slist_foreach( entries, ( GFunc ) gconf_entry_unref, NULL );
387 g_slist_free( entries );
391 * na_gconf_utils_read_bool:
392 * @gconf: a GConfClient instance.
393 * @path: the full path to the key.
394 * @use_schema: whether to use the default value from schema, or not.
395 * @default_value: default value to be used if schema is not used or
396 * doesn't exist.
398 * Returns: the required boolean value.
400 * Since: 2.30
402 * Deprecated: 3.1.0
404 gboolean
405 na_gconf_utils_read_bool( GConfClient *gconf, const gchar *path, gboolean use_schema, gboolean default_value )
407 GConfValue *value;
408 gboolean ret;
410 g_return_val_if_fail( GCONF_IS_CLIENT( gconf ), FALSE );
412 ret = default_value;
414 value = read_value( gconf, path, use_schema, GCONF_VALUE_BOOL );
415 if( value ){
416 ret = gconf_value_get_bool( value );
417 gconf_value_free( value );
420 return( ret );
424 * na_gconf_utils_read_int:
425 * @gconf: a GConfClient instance.
426 * @path: the full path to the key.
427 * @use_schema: whether to use the default value from schema, or not.
428 * @default_value: default value to be used if schema is not used or
429 * doesn't exist.
431 * Returns: the required integer value.
433 * Since: 2.30
435 * Deprecated: 3.1.0
437 gint
438 na_gconf_utils_read_int( GConfClient *gconf, const gchar *path, gboolean use_schema, gint default_value )
440 GConfValue *value = NULL;
441 gint ret;
443 g_return_val_if_fail( GCONF_IS_CLIENT( gconf ), FALSE );
445 ret = default_value;
447 value = read_value( gconf, path, use_schema, GCONF_VALUE_INT );
449 if( value ){
450 ret = gconf_value_get_int( value );
451 gconf_value_free( value );
454 return( ret );
458 * na_gconf_utils_read_string:
459 * @gconf: a GConfClient instance.
460 * @path: the full path to the key.
461 * @use_schema: whether to use the default value from schema, or not.
462 * @default_value: default value to be used if schema is not used or
463 * doesn't exist.
465 * Returns: the required string value in a newly allocated string which
466 * should be g_free() by the caller.
468 * Since: 2.30
470 * Deprecated: 3.1.0
472 gchar *
473 na_gconf_utils_read_string( GConfClient *gconf, const gchar *path, gboolean use_schema, const gchar *default_value )
475 GConfValue *value = NULL;
476 gchar *result;
478 g_return_val_if_fail( GCONF_IS_CLIENT( gconf ), NULL );
480 result = g_strdup( default_value );
482 value = read_value( gconf, path, use_schema, GCONF_VALUE_STRING );
484 if( value ){
485 g_free( result );
486 result = g_strdup( gconf_value_get_string( value ));
487 gconf_value_free( value );
490 return( result );
494 * na_gconf_utils_read_string_list:
495 * @gconf: a GConfClient instance.
496 * @path: the full path to the key to be read.
498 * Returns: a list of strings,
499 * or %NULL if the entry was not found or was not of string list type.
501 * The returned list must be released with na_core_utils_slist_free().
503 * Since: 2.30
505 * Deprecated: 3.1.0
507 GSList *
508 na_gconf_utils_read_string_list( GConfClient *gconf, const gchar *path )
510 static const gchar *thisfn = "na_gconf_utils_read_string_list";
511 GError *error = NULL;
512 GSList *list_strings;
514 g_return_val_if_fail( GCONF_IS_CLIENT( gconf ), NULL );
516 list_strings = gconf_client_get_list( gconf, path, GCONF_VALUE_STRING, &error );
518 if( error ){
519 g_warning( "%s: path=%s, error=%s", thisfn, path, error->message );
520 g_error_free( error );
521 return( NULL );
524 return( list_strings );
528 * na_gconf_utils_write_bool:
529 * @gconf: a GConfClient instance.
530 * @path: the full path to the key.
531 * @value: the value to be written.
532 * @message: a pointer to a gchar * which will be allocated if needed.
534 * Writes a boolean at the given @path.
536 * Returns: %TRUE if the writing has been successful, %FALSE else.
538 * If returned not NULL, the @message contains an error message.
539 * It should be g_free() by the caller.
541 * Since: 2.30
543 * Deprecated: 3.1.0
545 gboolean
546 na_gconf_utils_write_bool( GConfClient *gconf, const gchar *path, gboolean value, gchar **message )
548 static const gchar *thisfn = "na_gconf_utils_write_bool";
549 gboolean ret = TRUE;
550 GError *error = NULL;
552 g_return_val_if_fail( GCONF_IS_CLIENT( gconf ), FALSE );
554 if( !gconf_client_set_bool( gconf, path, value, &error )){
555 if( message ){
556 *message = g_strdup( error->message );
558 g_warning( "%s: path=%s, value=%s, error=%s", thisfn, path, value ? "True":"False", error->message );
559 g_error_free( error );
560 ret = FALSE;
563 return( ret );
567 * na_gconf_utils_write_int:
568 * @gconf: a GConfClient instance.
569 * @path: the full path to the key.
570 * @value: the value to be written.
571 * @message: a pointer to a gchar * which will be allocated if needed.
573 * Writes an integer at the given @path.
575 * Returns: %TRUE if the writing has been successful, %FALSE else.
577 * If returned not NULL, the @message contains an error message.
578 * It should be g_free() by the caller.
580 * Since: 2.30
582 * Deprecated: 3.1.0
584 gboolean
585 na_gconf_utils_write_int( GConfClient *gconf, const gchar *path, gint value, gchar **message )
587 static const gchar *thisfn = "na_gconf_utils_write_int";
588 gboolean ret = TRUE;
589 GError *error = NULL;
591 g_return_val_if_fail( GCONF_IS_CLIENT( gconf ), FALSE );
593 if( !gconf_client_set_int( gconf, path, value, &error )){
594 if( message ){
595 *message = g_strdup( error->message );
597 g_warning( "%s: path=%s, value=%d, error=%s", thisfn, path, value, error->message );
598 g_error_free( error );
599 ret = FALSE;
602 return( ret );
606 * na_gconf_utils_write_string:
607 * @gconf: a GConfClient instance.
608 * @path: the full path to the key.
609 * @value: the value to be written.
610 * @message: a pointer to a gchar * which will be allocated if needed.
612 * Writes a string at the given @path.
614 * Returns: %TRUE if the writing has been successful, %FALSE else.
616 * If returned not NULL, the @message contains an error message.
617 * It should be g_free() by the caller.
619 * Since: 2.30
621 * Deprecated: 3.1.0
623 gboolean
624 na_gconf_utils_write_string( GConfClient *gconf, const gchar *path, const gchar *value, gchar **message )
626 static const gchar *thisfn = "na_gconf_utils_write_string";
627 gboolean ret = TRUE;
628 GError *error = NULL;
630 g_return_val_if_fail( GCONF_IS_CLIENT( gconf ), FALSE );
632 if( !gconf_client_set_string( gconf, path, value, &error )){
633 if( message ){
634 *message = g_strdup( error->message );
636 g_warning( "%s: path=%s, value=%s, error=%s", thisfn, path, value, error->message );
637 g_error_free( error );
638 ret = FALSE;
641 return( ret );
645 * na_gconf_utils_write_string_list:
646 * @gconf: a GConfClient instance.
647 * @path: the full path to the key.
648 * @value: the list of values to be written.
649 * @message: a pointer to a gchar * which will be allocated if needed.
651 * Writes a list of strings at the given @path.
653 * Returns: %TRUE if the writing has been successful, %FALSE else.
655 * If returned not NULL, the @message contains an error message.
656 * It should be g_free() by the caller.
658 * Since: 2.30
660 * Deprecated: 3.1.0
662 gboolean
663 na_gconf_utils_write_string_list( GConfClient *gconf, const gchar *path, GSList *value, gchar **message )
665 static const gchar *thisfn = "na_gconf_utils_write_string_list";
666 gboolean ret = TRUE;
667 GError *error = NULL;
669 g_return_val_if_fail( GCONF_IS_CLIENT( gconf ), FALSE );
671 if( !gconf_client_set_list( gconf, path, GCONF_VALUE_STRING, value, &error )){
672 if( message ){
673 *message = g_strdup( error->message );
675 g_warning( "%s: path=%s, value=%p (count=%d), error=%s",
676 thisfn, path, ( void * ) value, g_slist_length( value ), error->message );
677 g_error_free( error );
678 ret = FALSE;
681 if( ret ){
682 ret = sync_gconf( gconf, message );
685 return( ret );
689 * na_gconf_utils_remove_entry:
690 * @gconf: a GConfClient instance.
691 * @path: the full path to the entry.
692 * @message: a pointer to a gchar * which will be allocated if needed.
694 * Removes an entry from user preferences.
696 * Returns: %TRUE if the operation was successful, %FALSE else.
698 * Since: 2.30
700 * Deprecated: 3.1.0
702 gboolean
703 na_gconf_utils_remove_entry( GConfClient *gconf, const gchar *path, gchar **message )
705 static const gchar *thisfn = "na_gconf_utils_remove_entry";
706 gboolean ret;
707 GError *error = NULL;
709 g_return_val_if_fail( GCONF_IS_CLIENT( gconf ), FALSE );
711 ret = gconf_client_unset( gconf, path, &error );
712 if( !ret ){
713 if( message ){
714 *message = g_strdup( error->message );
716 g_warning( "%s: path=%s, error=%s", thisfn, path, error->message );
717 g_error_free( error );
720 if( ret ){
721 ret = sync_gconf( gconf, message );
724 return( ret );
728 * na_gconf_utils_slist_from_string:
729 * @value: a string of the form [xxx,yyy,...] as read from GConf.
731 * Converts a string representing a list of strings in a GConf format
732 * to a list of strings.
734 * Returns: a newly allocated list of strings, which should be
735 * na_core_utils_slist_free() by the caller, or %NULL if the provided
736 * string was not of the GConf form.
738 * Since: 2.30
740 * Deprecated: 3.1.0
742 GSList *
743 na_gconf_utils_slist_from_string( const gchar *value )
745 GSList *slist;
746 gchar *tmp_string;
748 tmp_string = g_strdup( value );
749 g_strstrip( tmp_string );
751 if( !tmp_string || strlen( tmp_string ) < 3 ){
752 g_free( tmp_string );
753 return( NULL );
756 if( tmp_string[0] != '[' || tmp_string[strlen(tmp_string)-1] != ']' ){
757 g_free( tmp_string );
758 return( NULL );
761 tmp_string += 1;
762 tmp_string[strlen(tmp_string)-1] = '\0';
763 slist = na_core_utils_slist_from_split( tmp_string, "," );
765 return( slist );
769 * na_gconf_utils_slist_to_string:
770 * @slist: a #GSList to be displayed.
772 * Returns: the content of @slist, with the GConf format, as a newly
773 * allocated string which should be g_free() by the caller.
775 * Since: 2.30
777 * Deprecated: 3.1.0
779 gchar *
780 na_gconf_utils_slist_to_string( GSList *slist )
782 GSList *is;
783 GString *str = g_string_new( "[" );
784 gboolean first;
786 first = TRUE;
787 for( is = slist ; is ; is = is->next ){
788 if( !first ){
789 str = g_string_append( str, "," );
791 str = g_string_append( str, ( const gchar * ) is->data );
792 first = FALSE;
795 str = g_string_append( str, "]" );
797 return( g_string_free( str, FALSE ));
800 static GConfValue *
801 read_value( GConfClient *gconf, const gchar *path, gboolean use_schema, GConfValueType type )
803 static const gchar *thisfn = "na_gconf_utils_read_value";
804 GError *error = NULL;
805 GConfValue *value = NULL;
807 if( use_schema ){
808 value = gconf_client_get( gconf, path, &error );
809 } else {
810 value = gconf_client_get_without_default( gconf, path, &error );
813 if( error ){
814 g_warning( "%s: path=%s, error=%s", thisfn, path, error->message );
815 g_error_free( error );
816 if( value ){
817 gconf_value_free( value );
818 value = NULL;
822 if( value ){
823 if( value->type != type ){
824 g_warning( "%s: path=%s, found type '%u' while waiting for type '%u'", thisfn, path, value->type, type );
825 gconf_value_free( value );
826 value = NULL;
830 return( value );
833 static gboolean
834 sync_gconf( GConfClient *gconf, gchar **message )
836 static const gchar *thisfn = "na_gconf_utils_sync_gconf";
837 gboolean ret = TRUE;
838 GError *error = NULL;
840 gconf_client_suggest_sync( gconf, &error );
841 if( error ){
842 if( message ){
843 *message = g_strdup( error->message );
845 g_warning( "%s: error=%s", thisfn, error->message );
846 g_error_free( error );
847 ret = FALSE;
850 return( ret );