README: add deprecation notice
[nautilus-actions.git] / src / utils / fma-set-conf.c
blobfc3ecf5e49dedd1b1bafed92f7360874b2ba69a6
1 /*
2 * FileManager-Actions
3 * A file-manager extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006-2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009-2015 Pierre Wieser and others (see AUTHORS)
9 * FileManager-Actions 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 * FileManager-Actions 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 GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FileManager-Actions; see the file COPYING. If not, see
21 * <http://www.gnu.org/licenses/>.
23 * Authors:
24 * Frederic Ruaudel <grumz@grumz.net>
25 * Rodrigo Moya <rodrigo@gnome-db.org>
26 * Pierre Wieser <pwieser@trychlos.org>
27 * ... and many others (see AUTHORS)
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
34 #include <glib-object.h>
35 #include <glib/gi18n.h>
36 #include <locale.h>
37 #include <stdio.h>
38 #include <stdlib.h>
40 #include <api/fma-core-utils.h>
42 #include "console-utils.h"
45 * A program to update the FMA configuration files
48 #include <core/fma-settings.h>
50 static gchar *st_option_context = N_( "Set a key=value pair in a key file." );
52 static gchar *st_group = "";
53 static gchar *st_key = "";
54 static gchar *st_type = "";
55 static gchar *st_value = "";
56 static gboolean st_version = FALSE;
58 static GOptionEntry st_entries[] = {
60 { "group" , 'g', 0, G_OPTION_ARG_STRING , &st_group,
61 N_( "The group to be updated" ), N_( "<STRING>" ) },
62 { "key" , 'k', 0, G_OPTION_ARG_STRING , &st_key,
63 N_( "The key to be updated" ), N_( "<STRING>" ) },
64 { "type" , 't', 0, G_OPTION_ARG_STRING , &st_type,
65 /* i18n: 'str', 'int' and 'bool' are literal values: do not translate */
66 N_( "The type of the value to be set, may be 'str', 'int' or 'bool'" ), N_( "<STRING>" ) },
67 { "value" , 'v', 0, G_OPTION_ARG_STRING , &st_value,
68 N_( "The value to be set" ), N_( "<STRING>" ) },
69 { NULL }
72 static GOptionEntry misc_entries[] = {
74 { "version" , 'v', 0, G_OPTION_ARG_NONE , &st_version,
75 N_( "Output the version number and exit gracefully" ), NULL },
76 { NULL }
79 typedef enum {
80 TYPE_NO_TYPE = 0,
81 TYPE_STR,
82 TYPE_INT,
83 TYPE_BOOL
85 NAType;
87 static GOptionContext *init_options( void );
88 static void exit_with_usage( void );
89 static int do_update_conf( const gchar *group, const gchar *key, NAType type, const gchar *value );
91 int
92 main( int argc, char** argv )
94 int status = EXIT_SUCCESS;
95 GOptionContext *context;
96 GError *error = NULL;
97 gchar *help;
98 gint errors;
99 NAType type;
100 gchar *msgerr;
102 #if !GLIB_CHECK_VERSION( 2,36, 0 )
103 g_type_init();
104 #endif
106 setlocale( LC_ALL, "" );
107 console_init_log_handler();
109 context = init_options();
111 if( argc == 1 ){
112 g_set_prgname( argv[0] );
113 help = g_option_context_get_help( context, FALSE, NULL );
114 g_print( "\n%s", help );
115 g_free( help );
116 exit( status );
119 if( !g_option_context_parse( context, &argc, &argv, &error )){
120 g_printerr( _( "Syntax error: %s\n" ), error->message );
121 g_error_free (error);
122 exit_with_usage();
125 g_option_context_free( context );
127 if( st_version ){
128 fma_core_utils_print_version();
129 exit( status );
132 errors = 0;
133 type = TYPE_NO_TYPE;
135 if( !st_group || !g_utf8_strlen( st_group, -1 )){
136 g_printerr( _( "Error: a group is mandatory.\n" ));
137 errors += 1;
140 if( !st_key || !g_utf8_strlen( st_key, -1 )){
141 g_printerr( _( "Error: a key is mandatory.\n" ));
142 errors += 1;
145 if( !st_type || !g_utf8_strlen( st_type, -1 )){
146 g_printerr( _( "Error: a type is mandatory for setting/updating a value.\n" ));
147 errors += 1;
149 } else if( !g_utf8_collate( st_type, "str" )){
150 type = TYPE_STR;
151 } else if( !g_utf8_collate( st_type, "int" )){
152 type = TYPE_INT;
153 } else if( !g_utf8_collate( st_type, "bool" )){
154 type = TYPE_BOOL;
155 } else {
156 /* i18n: 'str', 'int' and 'bool' are literal values: do not translate */
157 msgerr = g_strdup_printf( _( "Error: unknown type: %s. Use 'str', 'int' or 'bool'.\n" ), st_type );
158 g_printerr( "%s", msgerr );
159 g_free( msgerr );
160 errors += 1;
163 if( !st_value || !g_utf8_strlen( st_value, -1 )){
164 g_printerr( _( "Error: a value is mandatory.\n" ));
165 errors += 1;
168 if( errors ){
169 exit_with_usage();
172 g_return_val_if_fail( type != TYPE_NO_TYPE, EXIT_FAILURE );
174 status = do_update_conf( st_group, st_key, type, st_value );
176 exit( status );
180 * init options context
182 static GOptionContext *
183 init_options( void )
185 GOptionContext *context;
186 gchar* description;
187 GOptionGroup *misc_group;
189 context = g_option_context_new( gettext( st_option_context ));
190 g_option_context_set_translation_domain( context, GETTEXT_PACKAGE );
192 #ifdef ENABLE_NLS
193 bindtextdomain( GETTEXT_PACKAGE, GNOMELOCALEDIR );
194 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
195 bind_textdomain_codeset( GETTEXT_PACKAGE, "UTF-8" );
196 # endif
197 textdomain( GETTEXT_PACKAGE );
198 g_option_context_add_main_entries( context, st_entries, GETTEXT_PACKAGE );
199 #else
200 g_option_context_add_main_entries( context, st_entries, NULL );
201 #endif
203 description = console_cmdline_get_description();
204 g_option_context_set_description( context, description );
205 g_free( description );
207 misc_group = g_option_group_new(
208 "misc", _( "Miscellaneous options" ), _( "Miscellaneous options" ), NULL, NULL );
209 g_option_group_add_entries( misc_group, misc_entries );
210 g_option_group_set_translation_domain( misc_group, GETTEXT_PACKAGE );
211 g_option_context_add_group( context, misc_group );
213 return( context );
217 * print a help message and exit with failure
219 static void
220 exit_with_usage( void )
222 g_printerr( _( "Try %s --help for usage.\n" ), g_get_prgname());
223 exit( EXIT_FAILURE );
226 static int
227 do_update_conf( const gchar *group, const gchar *key, NAType type, const gchar *value )
229 gboolean ok;
230 gboolean bvalue;
231 int ivalue;
233 ok = FALSE;
235 switch( type ){
236 case TYPE_BOOL:
237 bvalue = fma_core_utils_boolean_from_string( value );
238 ok = fma_settings_set_boolean_ex( group, key, bvalue );
239 break;
241 case TYPE_INT:
242 ivalue = atoi( value );
243 ok = fma_settings_set_int_ex( group, key, ivalue );
244 break;
246 case TYPE_STR:
247 ok = fma_settings_set_string_ex( group, key, value );
248 break;
250 default:
251 break;
254 return( ok ? EXIT_SUCCESS : EXIT_FAILURE );