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 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)
35
#include <glib-object.h>
41
* Search for a string in a string list.
43
* @list: the GSList of strings to be searched.
45
* @str: the searched string.
47
* Returns TRUE if the string has been found in list.
50
na_utils_find_in_list( GSList *list, const gchar *str )
54
for( il = list ; il ; il = il->next ){
55
const gchar *istr = ( const gchar * ) il->data;
56
if( !g_utf8_collate( str, istr )){
65
* Compare two string lists.
67
* @first: a GSList of strings.
69
* @second: another GSList of strings to be compared with @first.
71
* Returns TRUE if the two lists have same content.
74
na_utils_string_lists_are_equal( GSList *first, GSList *second )
78
for( il = first ; il ; il = il->next ){
79
const gchar *str = ( const gchar * ) il->data;
80
if( !na_utils_find_in_list( second, str )){
85
for( il = second ; il ; il = il->next ){
86
const gchar *str = ( const gchar * ) il->data;
87
if( !na_utils_find_in_list( first, str )){
96
* Duplicates a GSList of strings.
98
* @list: the GSList to be duplicated.
101
na_utils_duplicate_string_list( GSList *list )
103
GSList *duplist = NULL;
105
for( it = list ; it != NULL ; it = it->next ){
106
gchar *dupstr = g_strdup(( gchar * ) it->data );
107
duplist = g_slist_prepend( duplist, dupstr );
113
* Frees a GSList of strings.
115
* @list: the GSList to be freed.
118
na_utils_free_string_list( GSList *list )
121
for( item = list ; item != NULL ; item = item->next ){
122
g_free(( gchar * ) item->data );
124
g_slist_free( list );
128
* Removes a string from a GSList of strings.
130
* @list: the GSList to be updated.
132
* @text: string to remove.
135
na_utils_remove_ascii_from_string_list( GSList *list, const gchar *text )
138
for( il = list ; il ; il = il->next ){
139
const gchar *istr = ( const gchar * ) il->data;
140
if( !g_ascii_strcasecmp( text, istr )){
141
list = g_slist_remove( list, ( gconstpointer ) istr );
149
* Concatenates a string list to a semi-colon-separated text.
152
na_utils_string_list_to_text( GSList *strlist )
156
gchar *text = g_strdup( "" );
158
for( ib = strlist ; ib ; ib = ib->next ){
160
tmp = g_strdup_printf( "%s; ", text );
164
tmp = g_strdup_printf( "%s%s", text, ( gchar * ) ib->data );
173
* Extracts a list of strings from a semi-colon-separated text
177
na_utils_text_to_string_list( const gchar *text )
179
GSList *strlist = NULL;
180
gchar **tokens, **iter;
182
gchar *source = g_strdup( text );
184
tmp = g_strstrip( source );
186
strlist = g_slist_append( strlist, g_strdup( "*" ));
189
tokens = g_strsplit( source, ";", -1 );
193
tmp = g_strstrip( *iter );
194
strlist = g_slist_append( strlist, g_strdup( tmp ));
198
g_strfreev( tokens );
206
na_utils_dump_string_list( GSList *list )
208
static const gchar *thisfn = "na_utils_dump_string_list";
212
g_debug( "%s: list at %p has %d elements", thisfn, list, g_slist_length( list ));
213
for( i=list, c=0 ; i ; i=i->next, c++ ){
214
gchar *s = ( gchar * ) i->data;
215
g_debug( "%s: %2d - %s", thisfn, c, s );
220
* Converts a list of strings to a comma-separated list of strings,
221
* enclosed by brackets (dump format, GConf export format).
224
na_utils_gslist_to_schema( GSList *list )
228
gchar *text = g_strdup( "" );
230
for( ib = list ; ib ; ib = ib->next ){
232
tmp = g_strdup_printf( "%s,", text );
236
tmp = g_strdup_printf( "%s%s", text, ( gchar * ) ib->data );
241
tmp = g_strdup_printf( "[%s]", text );
249
* Converts a string representing a list of strings in a GConf format
250
* to a list of strings.
253
na_utils_schema_to_gslist( const gchar *value )
256
const gchar *ptr = value;
257
const gchar *start = NULL;
258
gchar *str_list = NULL;
259
gchar **str_list_splited = NULL;
262
/* first remove the surrounding brackets [] */
263
while( *ptr != '[' ){
271
while( *ptr != ']' ){
276
str_list = g_strndup( start, i );
280
/* split the result and fill the list */
281
if( str_list != NULL ){
283
str_list_splited = g_strsplit( str_list, ",", -1 );
285
while( str_list_splited[i] != NULL ){
286
list = g_slist_append( list, g_strdup( str_list_splited[i] ));
289
g_strfreev( str_list_splited );
296
* Converts a boolean to the suitable string for a GConf schema
299
na_utils_boolean_to_schema( gboolean b )
301
gchar *text = g_strdup_printf( "%s", b ? "true" : "false" );
306
* Converts a string to a boolean
307
* Not case sensitive, accepts abbreviations
310
na_utils_schema_to_boolean( const gchar *value, gboolean default_value )
312
if( !g_ascii_strcasecmp( value, "true" )){
313
/*g_debug( "na_utils_schema_to_boolean: value=%s, returning TRUE", value );*/
316
if( !g_ascii_strcasecmp( value, "false" )){
317
/*g_debug( "na_utils_schema_to_boolean: value=%s, returning FALSE", value );*/
320
/*g_debug( "na_utils_schema_to_boolean: value=%s, returning default_value", value );*/
321
return( default_value );
325
* extract the key part (the last part) of a full path
326
* returns a newly allocated string which must be g_free() by the caller
329
na_utils_path_to_key( const gchar *path )
331
gchar **split = g_strsplit( path, "/", -1 );
332
guint count = g_strv_length( split );
333
gchar *key = g_strdup( split[count-1] );
339
* Concatenates a gchar **list of strings to a GString.
342
na_utils_gstring_joinv( const gchar *start, const gchar *separator, gchar **list )
344
GString *tmp_string = g_string_new( "" );
347
g_return_val_if_fail( list != NULL, NULL );
350
tmp_string = g_string_append( tmp_string, start );
353
if( list[0] != NULL ){
354
tmp_string = g_string_append( tmp_string, list[0] );
357
for( i = 1 ; list[i] != NULL ; i++ ){
359
tmp_string = g_string_append( tmp_string, separator );
361
tmp_string = g_string_append( tmp_string, list[i] );
364
return( g_string_free( tmp_string, FALSE ));