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.
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)
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
);
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.
58 na_gconf_utils_get_subdirs( GConfClient
*gconf
, const gchar
*path
)
60 static const gchar
*thisfn
= "na_gconf_utils_get_subdirs";
64 list_subdirs
= gconf_client_all_dirs( gconf
, path
, &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
);
76 * na_gconf_utils_free_subdirs:
77 * @subdirs: the subdirectory list as returned from na_gconf_utils_get_subdirs().
86 na_gconf_utils_free_subdirs( GSList
*subdirs
)
88 na_core_utils_slist_free( subdirs
);
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,
104 na_gconf_utils_has_entry( GSList
*entries
, const gchar
*entry
)
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
);
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().
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
);
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
171 na_gconf_utils_get_bool_from_entries( GSList
*entries
, const gchar
*entry
, gboolean
*value
)
174 GConfEntry
*gconf_entry
;
175 GConfValue
*gconf_value
;
179 g_return_val_if_fail( value
, 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
);
192 gconf_value
->type
== GCONF_VALUE_BOOL
){
195 *value
= gconf_value_get_bool( gconf_value
);
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
215 * If @value is returned not NULL, it should be g_free() by the caller.
222 na_gconf_utils_get_string_from_entries( GSList
*entries
, const gchar
*entry
, gchar
**value
)
225 GConfEntry
*gconf_entry
;
226 GConfValue
*gconf_value
;
230 g_return_val_if_fail( value
, 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
);
243 gconf_value
->type
== GCONF_VALUE_STRING
){
246 *value
= g_strdup( gconf_value_get_string( gconf_value
));
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
266 * If @value is returned not NULL, it should be na_core_utils_slist_free()
274 na_gconf_utils_get_string_list_from_entries( GSList
*entries
, const gchar
*entry
, GSList
**value
)
277 GConfEntry
*gconf_entry
;
278 GConfValue
*gconf_value
;
283 g_return_val_if_fail( value
, 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
);
296 gconf_value
->type
== GCONF_VALUE_LIST
){
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
)));
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.
322 na_gconf_utils_dump_entries( GSList
*entries
)
324 g_slist_foreach( entries
, ( GFunc
) dump_entry
, NULL
);
328 dump_entry( GConfEntry
*entry
, void *user_data
)
330 static const gchar
*thisfn
= "na_gconf_utils_dump_entry";
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
);
338 switch( value
->type
){
339 case GCONF_VALUE_STRING
:
340 str
= ( gchar
* ) gconf_value_get_string( value
);
343 case GCONF_VALUE_INT
:
344 str
= g_strdup_printf( "%d", gconf_value_get_int( value
));
348 case GCONF_VALUE_FLOAT
:
349 str
= g_strdup_printf( "%f", gconf_value_get_float( value
));
353 case GCONF_VALUE_BOOL
:
354 str
= g_strdup_printf( "%s", gconf_value_get_bool( value
) ? "True":"False" );
359 str
= g_strdup( "(undetermined value)" );
364 g_debug( "%s: key=%s, value=%s", thisfn
, key
, str
);
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.
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
398 * Returns: the required boolean value.
405 na_gconf_utils_read_bool( GConfClient
*gconf
, const gchar
*path
, gboolean use_schema
, gboolean default_value
)
410 g_return_val_if_fail( GCONF_IS_CLIENT( gconf
), FALSE
);
414 value
= read_value( gconf
, path
, use_schema
, GCONF_VALUE_BOOL
);
416 ret
= gconf_value_get_bool( value
);
417 gconf_value_free( value
);
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
431 * Returns: the required integer value.
438 na_gconf_utils_read_int( GConfClient
*gconf
, const gchar
*path
, gboolean use_schema
, gint default_value
)
440 GConfValue
*value
= NULL
;
443 g_return_val_if_fail( GCONF_IS_CLIENT( gconf
), FALSE
);
447 value
= read_value( gconf
, path
, use_schema
, GCONF_VALUE_INT
);
450 ret
= gconf_value_get_int( value
);
451 gconf_value_free( value
);
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
465 * Returns: the required string value in a newly allocated string which
466 * should be g_free() by the caller.
473 na_gconf_utils_read_string( GConfClient
*gconf
, const gchar
*path
, gboolean use_schema
, const gchar
*default_value
)
475 GConfValue
*value
= NULL
;
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
);
486 result
= g_strdup( gconf_value_get_string( value
));
487 gconf_value_free( value
);
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().
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
);
519 g_warning( "%s: path=%s, error=%s", thisfn
, path
, error
->message
);
520 g_error_free( error
);
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.
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";
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
)){
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
);
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.
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";
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
)){
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
);
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.
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";
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
)){
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
);
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.
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";
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
)){
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
);
682 ret
= sync_gconf( gconf
, message
);
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.
703 na_gconf_utils_remove_entry( GConfClient
*gconf
, const gchar
*path
, gchar
**message
)
705 static const gchar
*thisfn
= "na_gconf_utils_remove_entry";
707 GError
*error
= NULL
;
709 g_return_val_if_fail( GCONF_IS_CLIENT( gconf
), FALSE
);
711 ret
= gconf_client_unset( gconf
, path
, &error
);
714 *message
= g_strdup( error
->message
);
716 g_warning( "%s: path=%s, error=%s", thisfn
, path
, error
->message
);
717 g_error_free( error
);
721 ret
= sync_gconf( gconf
, message
);
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.
743 na_gconf_utils_slist_from_string( const gchar
*value
)
748 tmp_string
= g_strdup( value
);
749 g_strstrip( tmp_string
);
751 if( !tmp_string
|| strlen( tmp_string
) < 3 ){
752 g_free( tmp_string
);
756 if( tmp_string
[0] != '[' || tmp_string
[strlen(tmp_string
)-1] != ']' ){
757 g_free( tmp_string
);
762 tmp_string
[strlen(tmp_string
)-1] = '\0';
763 slist
= na_core_utils_slist_from_split( tmp_string
, "," );
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.
780 na_gconf_utils_slist_to_string( GSList
*slist
)
783 GString
*str
= g_string_new( "[" );
787 for( is
= slist
; is
; is
= is
->next
){
789 str
= g_string_append( str
, "," );
791 str
= g_string_append( str
, ( const gchar
* ) is
->data
);
795 str
= g_string_append( str
, "]" );
797 return( g_string_free( str
, FALSE
));
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
;
808 value
= gconf_client_get( gconf
, path
, &error
);
810 value
= gconf_client_get_without_default( gconf
, path
, &error
);
814 g_warning( "%s: path=%s, error=%s", thisfn
, path
, error
->message
);
815 g_error_free( error
);
817 gconf_value_free( 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
);
834 sync_gconf( GConfClient
*gconf
, gchar
**message
)
836 static const gchar
*thisfn
= "na_gconf_utils_sync_gconf";
838 GError
*error
= NULL
;
840 gconf_client_suggest_sync( gconf
, &error
);
843 *message
= g_strdup( error
->message
);
845 g_warning( "%s: error=%s", thisfn
, error
->message
);
846 g_error_free( error
);