Fix string errors reported by Christian Kirbach
[nautilus-actions.git] / src / core / na-iprefs.c
blob65ce8fbca25525896daa3e067d84f1635db1954e
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-iimporter.h>
40 #include "na-iprefs.h"
41 #include "na-settings.h"
43 typedef struct {
44 guint id;
45 const gchar *str;
47 EnumMap;
49 /* import mode: what to do when the imported id already exists ?
50 * enum is defined in api/na-iimporter.h
52 #define IMPORT_MODE_NOIMPORT_STR "NoImport"
53 #define IMPORT_MODE_RENUMBER_STR "Renumber"
54 #define IMPORT_MODE_OVERRIDE_STR "Override"
55 #define IMPORT_MODE_ASK_STR "Ask"
57 static EnumMap st_import_mode[] = {
58 { IMPORTER_MODE_NO_IMPORT, IMPORT_MODE_NOIMPORT_STR },
59 { IMPORTER_MODE_RENUMBER, IMPORT_MODE_RENUMBER_STR },
60 { IMPORTER_MODE_OVERRIDE, IMPORT_MODE_OVERRIDE_STR },
61 { IMPORTER_MODE_ASK, IMPORT_MODE_ASK_STR },
62 { 0 }
65 /* sort mode of the items in the file manager context menu
66 * enum is defined in core/na-iprefs.h
68 #define ORDER_ALPHA_ASC_STR "AscendingOrder"
69 #define ORDER_ALPHA_DESC_STR "DescendingOrder"
70 #define ORDER_MANUAL_STR "ManualOrder"
72 static EnumMap st_order_mode[] = {
73 { IPREFS_ORDER_ALPHA_ASCENDING, ORDER_ALPHA_ASC_STR },
74 { IPREFS_ORDER_ALPHA_DESCENDING, ORDER_ALPHA_DESC_STR },
75 { IPREFS_ORDER_MANUAL, ORDER_MANUAL_STR },
76 { 0 }
79 static const gchar *enum_map_string_from_id( const EnumMap *map, guint id );
80 static guint enum_map_id_from_string( const EnumMap *map, const gchar *str );
83 * na_iprefs_get_import_mode:
84 * @pivot: the #NAPivot application object.
85 * @pref: name of the import key to be read.
86 * @mandatory: if not %NULL, a pointer to a boolean which will receive the
87 * mandatory property.
89 * This preference defines what to do when an imported item has the same
90 * identifier that an already existing one. Default value is defined in
91 * core/na-settings.h.
93 * Returns: the import mode currently set.
95 guint
96 na_iprefs_get_import_mode( const NAPivot *pivot, const gchar *pref, gboolean *mandatory )
98 gchar *import_mode_str;
99 guint import_mode;
100 NASettings *settings;
102 settings = na_pivot_get_settings( pivot );
103 import_mode_str = na_settings_get_string( settings, pref, NULL, mandatory );
104 import_mode = enum_map_id_from_string( st_import_mode, import_mode_str );
105 g_free( import_mode_str );
107 return( import_mode );
111 * na_iprefs_set_import_mode:
112 * @pivot: the #NAPivot application object.
113 * @pref: name of the import key to be written.
114 * @mode: the new value to be written.
116 * Writes the current status of 'import mode' to the preferences system.
118 void
119 na_iprefs_set_import_mode( const NAPivot *pivot, const gchar *pref, guint mode )
121 const gchar *import_str;
122 NASettings *settings;
124 settings = na_pivot_get_settings( pivot );
125 import_str = enum_map_string_from_id( st_import_mode, mode );
126 na_settings_set_string( settings, pref, import_str );
130 * na_iprefs_get_order_mode:
131 * @pivot: the #NAPivot application object.
132 * @mandatory: if not %NULL, a pointer to a boolean which will receive the
133 * mandatory property.
135 * Returns: the order mode currently set.
137 guint
138 na_iprefs_get_order_mode( const NAPivot *pivot, gboolean *mandatory )
140 gchar *order_mode_str;
141 guint order_mode;
142 NASettings *settings;
144 settings = na_pivot_get_settings( pivot );
145 order_mode_str = na_settings_get_string( settings, NA_IPREFS_ITEMS_LIST_ORDER_MODE, NULL, mandatory );
146 order_mode = enum_map_id_from_string( st_order_mode, order_mode_str );
147 g_free( order_mode_str );
149 return( order_mode );
153 * na_iprefs_set_order_mode:
154 * @pivot: the #NAPivot application object.
155 * @mode: the new value to be written.
157 * Writes the current status of 'alphabetical order' to the GConf
158 * preference system.
160 void
161 na_iprefs_set_order_mode( const NAPivot *pivot, guint mode )
163 const gchar *order_str;
164 NASettings *settings;
166 settings = na_pivot_get_settings( pivot );
167 order_str = enum_map_string_from_id( st_order_mode, mode );
168 na_settings_set_string( settings, NA_IPREFS_ITEMS_LIST_ORDER_MODE, order_str );
172 * na_iprefs_get_export_format:
173 * @pivot: the #NAPivot application object.
174 * @name: name of the export format key to be read
175 * @mandatory: if not %NULL, a pointer to a boolean which will receive the
176 * mandatory property.
178 * Used to default to export as a GConfEntry.
179 * Starting with 3.1.0, defaults to Desktop1 (see. core/na-settings.h)
181 * Returns: the export format currently set as a #GQuark.
183 GQuark
184 na_iprefs_get_export_format( const NAPivot *pivot, const gchar *name, gboolean *mandatory )
186 GQuark export_format;
187 NASettings *settings;
188 gchar *format_str;
190 export_format = g_quark_from_static_string( NA_IPREFS_DEFAULT_EXPORT_FORMAT );
192 settings = na_pivot_get_settings( pivot );
193 format_str = na_settings_get_string( settings, name, NULL, mandatory );
195 if( format_str ){
196 export_format = g_quark_from_string( format_str );
197 g_free( format_str );
200 return( export_format );
204 * na_iprefs_set_export_format:
205 * @pivot: the #NAPivot application object.
206 * @format: the new value to be written.
208 * Writes the preferred export format' to the preference system.
210 void
211 na_iprefs_set_export_format( const NAPivot *pivot, const gchar *name, GQuark format )
213 NASettings *settings;
215 settings = na_pivot_get_settings( pivot );
216 na_settings_set_string( settings, name, g_quark_to_string( format ));
220 * na_iprefs_get_io_providers:
221 * @pivot: the #NAPivot application object.
223 * Searches in preferences system for all mentions of an i/o provider.
224 * This does not mean in any way that the i/o provider is active,
225 * available or so, but just that is mentioned here.
227 * I/o provider identifiers returned in the list are not supposed
228 * to be unique, nor sorted.
230 * Returns: a list of i/o provider identifiers found in preferences
231 * system; this list should be na_core_utils_slist_free() by the caller.
233 * since: 3.1.0
235 GSList *
236 na_iprefs_get_io_providers( const NAPivot *pivot )
238 GSList *providers;
239 NASettings *settings;
240 GSList *write_order, *groups;
241 GSList *it;
242 const gchar *name;
243 gchar *group_prefix;
244 guint prefix_len;
246 providers = NULL;
247 settings = na_pivot_get_settings( pivot );
249 write_order = na_settings_get_string_list( settings, NA_IPREFS_IO_PROVIDERS_WRITE_ORDER, NULL, NULL );
250 for( it = write_order ; it ; it = it->next ){
251 name = ( const gchar * ) it->data;
252 providers = g_slist_prepend( providers, g_strdup( name ));
254 na_core_utils_slist_free( write_order );
256 groups = na_settings_get_groups( settings );
258 group_prefix = g_strdup_printf( "%s ", NA_IPREFS_IO_PROVIDER_GROUP );
259 prefix_len = strlen( group_prefix );
260 for( it = groups ; it ; it = it->next ){
261 name = ( const gchar * ) it->data;
262 if( g_str_has_prefix( name, group_prefix )){
263 providers = g_slist_prepend( providers, g_strdup( name+prefix_len ));
266 g_free( group_prefix );
267 na_core_utils_slist_free( groups );
269 return( providers );
273 * na_iprefs_is_level_zero_writable:
274 * @pivot: the #NAPivot application object.
276 * The level-zero order may not be writable if:
277 * - all the configuration has been locked down by an admin
278 * - the preferences has been locked down by an admin
279 * - the level-zero order is a mandatory preference
280 * - the user configuration file is not writable.
282 * All these conditions are subject to runtime modifications. The caller
283 * should not keep the result, but rather re-call this function each time
284 * it needs this status.
286 * Each condition is also subject to race conditions. So the returned
287 * status may not be more valid when the caller tries to actually write
288 * the level-zero preference.
290 * Returns: %TRUE if we are able to update the level-zero list of items,
291 * %FALSE else.
293 * As of 3.1.0, level-zero order is written as a user preference.
295 gboolean
296 na_iprefs_is_level_zero_writable( const NAPivot *pivot )
298 g_return_val_if_fail( NA_IS_PIVOT( pivot ), FALSE );
300 if( na_pivot_is_configuration_locked_by_admin( pivot )){
301 return( FALSE );
304 return( TRUE );
308 * na_iprefs_write_level_zero:
309 * @pivot: the #NAPivot application object.
310 * @items: the #GList of items whose first level is to be written.
311 * @messages: a pointer to a #GSList in which we will add happening
312 * error messages;
313 * the pointer may be %NULL;
314 * if not %NULL, the #GSList must have been initialized by the
315 * caller.
317 * Rewrite the level-zero items in GConf preferences.
319 * Returns: %TRUE if successfully written (i.e. writable, not locked,
320 * and so on), %FALSE else.
322 * @messages #GSList is only filled up in case of an error has occured.
323 * If there is no error (na_iprefs_write_level_zero() returns %TRUE), then
324 * the caller may safely assume that @messages is returned in the same
325 * state that it has been provided.
327 gboolean
328 na_iprefs_write_level_zero( const NAPivot *pivot, const GList *items, GSList **messages )
330 static const gchar *thisfn = "na_iprefs_write_level_zero";
331 gboolean written;
332 const GList *it;
333 gchar *id;
334 GSList *content;
336 g_return_val_if_fail( NA_IS_PIVOT( pivot ), FALSE );
338 written = FALSE;
340 g_debug( "%s: pivot=%p", thisfn, ( void * ) pivot);
342 if( na_iprefs_is_level_zero_writable( pivot )){
344 content = NULL;
345 for( it = items ; it ; it = it->next ){
346 id = na_object_get_id( it->data );
347 content = g_slist_prepend( content, id );
349 content = g_slist_reverse( content );
351 na_settings_set_string_list( na_pivot_get_settings( pivot ), NA_IPREFS_ITEMS_LEVEL_ZERO_ORDER, content );
352 written = TRUE;
354 na_core_utils_slist_free( content );
357 return( written );
360 static const gchar *
361 enum_map_string_from_id( const EnumMap *map, guint id )
363 const EnumMap *i = map;
365 while( i->id ){
366 if( i->id == id ){
367 return( i->str );
369 i++;
371 return( map->str );
374 static guint
375 enum_map_id_from_string( const EnumMap *map, const gchar *str )
377 const EnumMap *i = map;
379 while( i->id ){
380 if( !strcmp( i->str, str )){
381 return( i->id );
383 i++;
385 return( map->id );