~ubuntu-branches/ubuntu/precise/nautilus-actions/precise

« back to all changes in this revision

Viewing changes to src/common/na-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Christine Spang
  • Date: 2009-08-12 14:04:00 UTC
  • mfrom: (1.1.6 upstream) (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090812140400-ufe3o3jvr62lf0sf
Tags: 1.12.0-1
* New upstream stable release.
  - Actions can now be enabled/disabled: disabled actions will
    never show up in the nautilus context menu
  - Gnome Bugzilla bugs fixed:
    - #325519 asked by Frederic Ruaudel (enabled property)
    - #590398 reported by Pierre Wieser (install doc)
    - #590399 reported by Pierre Wieser (gtk_image_menu_item_set_image)
    - #590709 reported by Claude Paroz (markup in translatable strings)
    - #590711 reported by Claude Paroz (pipe char is ambiguous to translate)
 - Do not install GConf schemas if --disable-schemas-install option has
   been specified
 - New/updated es and fr translations

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Nautilus Actions
 
3
 * A Nautilus extension which offers configurable context menu actions.
 
4
 *
 
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)
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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.
 
23
 *
 
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)
 
29
 */
 
30
 
 
31
#ifdef HAVE_CONFIG_H
 
32
#include <config.h>
 
33
#endif
 
34
 
 
35
#include <glib-object.h>
 
36
#include <string.h>
 
37
 
 
38
#include "na-utils.h"
 
39
 
 
40
/**
 
41
 * Search for a string in a string list.
 
42
 *
 
43
 * @list: the GSList of strings to be searched.
 
44
 *
 
45
 * @str: the searched string.
 
46
 *
 
47
 * Returns TRUE if the string has been found in list.
 
48
 */
 
49
gboolean
 
50
na_utils_find_in_list( GSList *list, const gchar *str )
 
51
{
 
52
        GSList *il;
 
53
 
 
54
        for( il = list ; il ; il = il->next ){
 
55
                const gchar *istr = ( const gchar * ) il->data;
 
56
                if( !g_utf8_collate( str, istr )){
 
57
                        return( TRUE );
 
58
                }
 
59
        }
 
60
 
 
61
        return( FALSE );
 
62
}
 
63
 
 
64
/**
 
65
 * Compare two string lists.
 
66
 *
 
67
 * @first: a GSList of strings.
 
68
 *
 
69
 * @second: another GSList of strings to be compared with @first.
 
70
 *
 
71
 * Returns TRUE if the two lists have same content.
 
72
 */
 
73
gboolean
 
74
na_utils_string_lists_are_equal( GSList *first, GSList *second )
 
75
{
 
76
        GSList *il;
 
77
 
 
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 )){
 
81
                        return( FALSE );
 
82
                }
 
83
        }
 
84
 
 
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 )){
 
88
                        return( FALSE );
 
89
                }
 
90
        }
 
91
 
 
92
        return( TRUE );
 
93
}
 
94
 
 
95
/**
 
96
 * Duplicates a GSList of strings.
 
97
 *
 
98
 * @list: the GSList to be duplicated.
 
99
 */
 
100
GSList *
 
101
na_utils_duplicate_string_list( GSList *list )
 
102
{
 
103
        GSList *duplist = NULL;
 
104
        GSList *it;
 
105
        for( it = list ; it != NULL ; it = it->next ){
 
106
                gchar *dupstr = g_strdup(( gchar * ) it->data );
 
107
                duplist = g_slist_prepend( duplist, dupstr );
 
108
        }
 
109
        return( duplist );
 
110
}
 
111
 
 
112
/**
 
113
 * Frees a GSList of strings.
 
114
 *
 
115
 * @list: the GSList to be freed.
 
116
 */
 
117
void
 
118
na_utils_free_string_list( GSList *list )
 
119
{
 
120
        GSList *item;
 
121
        for( item = list ; item != NULL ; item = item->next ){
 
122
                g_free(( gchar * ) item->data );
 
123
        }
 
124
        g_slist_free( list );
 
125
}
 
126
 
 
127
/**
 
128
 * Removes a string from a GSList of strings.
 
129
 *
 
130
 * @list: the GSList to be updated.
 
131
 *
 
132
 * @text: string to remove.
 
133
 */
 
134
GSList *
 
135
na_utils_remove_ascii_from_string_list( GSList *list, const gchar *text )
 
136
{
 
137
        GSList *il;
 
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 );
 
142
                        return( list );
 
143
                }
 
144
        }
 
145
        return( list );
 
146
}
 
147
 
 
148
/**
 
149
 * Concatenates a string list to a semi-colon-separated text.
 
150
 */
 
151
gchar *
 
152
na_utils_string_list_to_text( GSList *strlist )
 
153
{
 
154
        GSList *ib;
 
155
        gchar *tmp;
 
156
        gchar *text = g_strdup( "" );
 
157
 
 
158
        for( ib = strlist ; ib ; ib = ib->next ){
 
159
                if( strlen( text )){
 
160
                        tmp = g_strdup_printf( "%s; ", text );
 
161
                        g_free( text );
 
162
                        text = tmp;
 
163
                }
 
164
                tmp = g_strdup_printf( "%s%s", text, ( gchar * ) ib->data );
 
165
                g_free( text );
 
166
                text = tmp;
 
167
        }
 
168
 
 
169
        return( text );
 
170
}
 
171
 
 
172
/**
 
173
 * Extracts a list of strings from a semi-colon-separated text
 
174
 * (entry text).
 
175
 */
 
176
GSList *
 
177
na_utils_text_to_string_list( const gchar *text )
 
178
{
 
179
        GSList *strlist = NULL;
 
180
        gchar **tokens, **iter;
 
181
        gchar *tmp;
 
182
        gchar *source = g_strdup( text );
 
183
 
 
184
        tmp = g_strstrip( source );
 
185
        if( !strlen( tmp )){
 
186
                strlist = g_slist_append( strlist, g_strdup( "*" ));
 
187
 
 
188
        } else {
 
189
                tokens = g_strsplit( source, ";", -1 );
 
190
                iter = tokens;
 
191
 
 
192
                while( *iter ){
 
193
                        tmp = g_strstrip( *iter );
 
194
                        strlist = g_slist_append( strlist, g_strdup( tmp ));
 
195
                        iter++;
 
196
                }
 
197
 
 
198
                g_strfreev( tokens );
 
199
        }
 
200
 
 
201
        g_free( source );
 
202
        return( strlist );
 
203
}
 
204
 
 
205
void
 
206
na_utils_dump_string_list( GSList *list )
 
207
{
 
208
        static const gchar *thisfn = "na_utils_dump_string_list";
 
209
        GSList *i;
 
210
        int c;
 
211
 
 
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 );
 
216
        }
 
217
}
 
218
 
 
219
/**
 
220
 * Converts a list of strings to a comma-separated list of strings,
 
221
 * enclosed by brackets (dump format, GConf export format).
 
222
 */
 
223
gchar *
 
224
na_utils_gslist_to_schema( GSList *list )
 
225
{
 
226
        GSList *ib;
 
227
        gchar *tmp;
 
228
        gchar *text = g_strdup( "" );
 
229
 
 
230
        for( ib = list ; ib ; ib = ib->next ){
 
231
                if( strlen( text )){
 
232
                        tmp = g_strdup_printf( "%s,", text );
 
233
                        g_free( text );
 
234
                        text = tmp;
 
235
                }
 
236
                tmp = g_strdup_printf( "%s%s", text, ( gchar * ) ib->data );
 
237
                g_free( text );
 
238
                text = tmp;
 
239
        }
 
240
 
 
241
        tmp = g_strdup_printf( "[%s]", text );
 
242
        g_free( text );
 
243
        text = tmp;
 
244
 
 
245
        return( text );
 
246
}
 
247
 
 
248
/**
 
249
 * Converts a string representing a list of strings in a GConf format
 
250
 * to a list of strings.
 
251
 */
 
252
GSList *
 
253
na_utils_schema_to_gslist( const gchar *value )
 
254
{
 
255
        GSList *list = NULL;
 
256
        const gchar *ptr = value;
 
257
        const gchar *start = NULL;
 
258
        gchar *str_list = NULL;
 
259
        gchar **str_list_splited = NULL;
 
260
        int i;
 
261
 
 
262
        /* first remove the surrounding brackets [] */
 
263
        while( *ptr != '[' ){
 
264
                ptr++;
 
265
        }
 
266
 
 
267
        if( *ptr == '[' ){
 
268
                ptr++;
 
269
                start = ptr;
 
270
                i = 0;
 
271
                while( *ptr != ']' ){
 
272
                        i++;
 
273
                        ptr++;
 
274
                }
 
275
                if( *ptr == ']' ){
 
276
                        str_list = g_strndup( start, i );
 
277
                }
 
278
        }
 
279
 
 
280
        /* split the result and fill the list */
 
281
        if( str_list != NULL ){
 
282
 
 
283
                str_list_splited = g_strsplit( str_list, ",", -1 );
 
284
                i = 0;
 
285
                while( str_list_splited[i] != NULL ){
 
286
                        list = g_slist_append( list, g_strdup( str_list_splited[i] ));
 
287
                        i++;
 
288
                }
 
289
                g_strfreev( str_list_splited );
 
290
        }
 
291
 
 
292
        return( list );
 
293
}
 
294
 
 
295
/**
 
296
 * Converts a boolean to the suitable string for a GConf schema
 
297
 */
 
298
gchar *
 
299
na_utils_boolean_to_schema( gboolean b )
 
300
{
 
301
        gchar *text = g_strdup_printf( "%s", b ? "true" : "false" );
 
302
        return( text );
 
303
}
 
304
 
 
305
/**
 
306
 * Converts a string to a boolean
 
307
 * Not case sensitive, accepts abbreviations
 
308
 */
 
309
gboolean
 
310
na_utils_schema_to_boolean( const gchar *value, gboolean default_value )
 
311
{
 
312
        if( !g_ascii_strcasecmp( value, "true" )){
 
313
                /*g_debug( "na_utils_schema_to_boolean: value=%s, returning TRUE", value );*/
 
314
                return( TRUE );
 
315
        }
 
316
        if( !g_ascii_strcasecmp( value, "false" )){
 
317
                /*g_debug( "na_utils_schema_to_boolean: value=%s, returning FALSE", value );*/
 
318
                return( FALSE );
 
319
        }
 
320
        /*g_debug( "na_utils_schema_to_boolean: value=%s, returning default_value", value );*/
 
321
        return( default_value );
 
322
}
 
323
 
 
324
/**
 
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
 
327
 */
 
328
gchar *
 
329
na_utils_path_to_key( const gchar *path )
 
330
{
 
331
        gchar **split = g_strsplit( path, "/", -1 );
 
332
        guint count = g_strv_length( split );
 
333
        gchar *key = g_strdup( split[count-1] );
 
334
        g_strfreev( split );
 
335
        return( key );
 
336
}
 
337
 
 
338
/**
 
339
 * Concatenates a gchar **list of strings to a GString.
 
340
 */
 
341
gchar *
 
342
na_utils_gstring_joinv( const gchar *start, const gchar *separator, gchar **list )
 
343
{
 
344
        GString *tmp_string = g_string_new( "" );
 
345
        int i;
 
346
 
 
347
        g_return_val_if_fail( list != NULL, NULL );
 
348
 
 
349
        if( start != NULL ){
 
350
                tmp_string = g_string_append( tmp_string, start );
 
351
        }
 
352
 
 
353
        if( list[0] != NULL ){
 
354
                tmp_string = g_string_append( tmp_string, list[0] );
 
355
        }
 
356
 
 
357
        for( i = 1 ; list[i] != NULL ; i++ ){
 
358
                if( separator ){
 
359
                        tmp_string = g_string_append( tmp_string, separator );
 
360
                }
 
361
                tmp_string = g_string_append( tmp_string, list[i] );
 
362
        }
 
363
 
 
364
        return( g_string_free( tmp_string, FALSE ));
 
365
}