Define new 'pivot-prop-loadable' property
[nautilus-actions.git] / src / core / na-iprefs.c
blobe2e996073b71b9973cb66f7c537d51d3ef70297a
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_get_order_mode_by_label:
154 * @label: the label.
156 * This function converts a label (e.g. 'ManualOrder') stored in user preferences
157 * into the corresponding integer internally used. This is needed e.g. when
158 * monitoring the preferences changes.
160 * Returns: the order mode currently set.
162 guint
163 na_iprefs_get_order_mode_by_label( const gchar *label )
165 guint order_mode;
167 order_mode = enum_map_id_from_string( st_order_mode, label );
169 return( order_mode );
173 * na_iprefs_set_order_mode:
174 * @pivot: the #NAPivot application object.
175 * @mode: the new value to be written.
177 * Writes the current status of 'alphabetical order' to the GConf
178 * preference system.
180 void
181 na_iprefs_set_order_mode( const NAPivot *pivot, guint mode )
183 const gchar *order_str;
184 NASettings *settings;
186 settings = na_pivot_get_settings( pivot );
187 order_str = enum_map_string_from_id( st_order_mode, mode );
188 na_settings_set_string( settings, NA_IPREFS_ITEMS_LIST_ORDER_MODE, order_str );
192 * na_iprefs_get_export_format:
193 * @pivot: the #NAPivot application object.
194 * @name: name of the export format key to be read
195 * @mandatory: if not %NULL, a pointer to a boolean which will receive the
196 * mandatory property.
198 * Used to default to export as a GConfEntry.
199 * Starting with 3.1.0, defaults to Desktop1 (see. core/na-settings.h)
201 * Returns: the export format currently set as a #GQuark.
203 GQuark
204 na_iprefs_get_export_format( const NAPivot *pivot, const gchar *name, gboolean *mandatory )
206 GQuark export_format;
207 NASettings *settings;
208 gchar *format_str;
210 export_format = g_quark_from_static_string( NA_IPREFS_DEFAULT_EXPORT_FORMAT );
212 settings = na_pivot_get_settings( pivot );
213 format_str = na_settings_get_string( settings, name, NULL, mandatory );
215 if( format_str ){
216 export_format = g_quark_from_string( format_str );
217 g_free( format_str );
220 return( export_format );
224 * na_iprefs_set_export_format:
225 * @pivot: the #NAPivot application object.
226 * @format: the new value to be written.
228 * Writes the preferred export format' to the preference system.
230 void
231 na_iprefs_set_export_format( const NAPivot *pivot, const gchar *name, GQuark format )
233 NASettings *settings;
235 settings = na_pivot_get_settings( pivot );
236 na_settings_set_string( settings, name, g_quark_to_string( format ));
240 * na_iprefs_get_io_providers:
241 * @pivot: the #NAPivot application object.
243 * Searches in preferences system for all mentions of an i/o provider.
244 * This does not mean in any way that the i/o provider is active,
245 * available or so, but just that is mentioned here.
247 * I/o provider identifiers returned in the list are not supposed
248 * to be unique, nor sorted.
250 * Returns: a list of i/o provider identifiers found in preferences
251 * system; this list should be na_core_utils_slist_free() by the caller.
253 * since: 3.1.0
255 GSList *
256 na_iprefs_get_io_providers( const NAPivot *pivot )
258 GSList *providers;
259 NASettings *settings;
260 GSList *write_order, *groups;
261 GSList *it;
262 const gchar *name;
263 gchar *group_prefix;
264 guint prefix_len;
266 providers = NULL;
267 settings = na_pivot_get_settings( pivot );
269 write_order = na_settings_get_string_list( settings, NA_IPREFS_IO_PROVIDERS_WRITE_ORDER, NULL, NULL );
270 for( it = write_order ; it ; it = it->next ){
271 name = ( const gchar * ) it->data;
272 providers = g_slist_prepend( providers, g_strdup( name ));
274 na_core_utils_slist_free( write_order );
276 groups = na_settings_get_groups( settings );
278 group_prefix = g_strdup_printf( "%s ", NA_IPREFS_IO_PROVIDER_GROUP );
279 prefix_len = strlen( group_prefix );
280 for( it = groups ; it ; it = it->next ){
281 name = ( const gchar * ) it->data;
282 if( g_str_has_prefix( name, group_prefix )){
283 providers = g_slist_prepend( providers, g_strdup( name+prefix_len ));
286 g_free( group_prefix );
287 na_core_utils_slist_free( groups );
289 return( providers );
293 * na_iprefs_write_level_zero:
294 * @pivot: the #NAPivot application object.
295 * @items: the #GList of items whose first level is to be written.
296 * @messages: a pointer to a #GSList in which we will add happening
297 * error messages;
298 * the pointer may be %NULL;
299 * if not %NULL, the #GSList must have been initialized by the
300 * caller.
302 * Rewrite the level-zero items in GConf preferences.
304 * Returns: %TRUE if successfully written (i.e. writable, not locked,
305 * and so on), %FALSE else.
307 * @messages #GSList is only filled up in case of an error has occured.
308 * If there is no error (na_iprefs_write_level_zero() returns %TRUE), then
309 * the caller may safely assume that @messages is returned in the same
310 * state that it has been provided.
312 gboolean
313 na_iprefs_write_level_zero( const NAPivot *pivot, const GList *items, GSList **messages )
315 static const gchar *thisfn = "na_iprefs_write_level_zero";
316 gboolean written;
317 const GList *it;
318 gchar *id;
319 GSList *content;
321 g_return_val_if_fail( NA_IS_PIVOT( pivot ), FALSE );
323 written = FALSE;
325 g_debug( "%s: pivot=%p", thisfn, ( void * ) pivot);
327 content = NULL;
328 for( it = items ; it ; it = it->next ){
329 id = na_object_get_id( it->data );
330 content = g_slist_prepend( content, id );
332 content = g_slist_reverse( content );
334 written = na_settings_set_string_list(
335 na_pivot_get_settings( pivot ), NA_IPREFS_ITEMS_LEVEL_ZERO_ORDER, content );
337 na_core_utils_slist_free( content );
339 return( written );
342 static const gchar *
343 enum_map_string_from_id( const EnumMap *map, guint id )
345 const EnumMap *i = map;
347 while( i->id ){
348 if( i->id == id ){
349 return( i->str );
351 i++;
353 return( map->str );
356 static guint
357 enum_map_id_from_string( const EnumMap *map, const gchar *str )
359 const EnumMap *i = map;
361 while( i->id ){
362 if( !strcmp( i->str, str )){
363 return( i->id );
365 i++;
367 return( map->id );