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/>.
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)
34 #include <glib-object.h>
35 #include <glib/gi18n.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>" ) },
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
},
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
);
92 main( int argc
, char** argv
)
94 int status
= EXIT_SUCCESS
;
95 GOptionContext
*context
;
102 #if !GLIB_CHECK_VERSION( 2,36, 0 )
106 setlocale( LC_ALL
, "" );
107 console_init_log_handler();
109 context
= init_options();
112 g_set_prgname( argv
[0] );
113 help
= g_option_context_get_help( context
, FALSE
, NULL
);
114 g_print( "\n%s", help
);
119 if( !g_option_context_parse( context
, &argc
, &argv
, &error
)){
120 g_printerr( _( "Syntax error: %s\n" ), error
->message
);
121 g_error_free (error
);
125 g_option_context_free( context
);
128 fma_core_utils_print_version();
135 if( !st_group
|| !g_utf8_strlen( st_group
, -1 )){
136 g_printerr( _( "Error: a group is mandatory.\n" ));
140 if( !st_key
|| !g_utf8_strlen( st_key
, -1 )){
141 g_printerr( _( "Error: a key is mandatory.\n" ));
145 if( !st_type
|| !g_utf8_strlen( st_type
, -1 )){
146 g_printerr( _( "Error: a type is mandatory for setting/updating a value.\n" ));
149 } else if( !g_utf8_collate( st_type
, "str" )){
151 } else if( !g_utf8_collate( st_type
, "int" )){
153 } else if( !g_utf8_collate( st_type
, "bool" )){
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
);
163 if( !st_value
|| !g_utf8_strlen( st_value
, -1 )){
164 g_printerr( _( "Error: a value is mandatory.\n" ));
172 g_return_val_if_fail( type
!= TYPE_NO_TYPE
, EXIT_FAILURE
);
174 status
= do_update_conf( st_group
, st_key
, type
, st_value
);
180 * init options context
182 static GOptionContext
*
185 GOptionContext
*context
;
187 GOptionGroup
*misc_group
;
189 context
= g_option_context_new( gettext( st_option_context
));
190 g_option_context_set_translation_domain( context
, GETTEXT_PACKAGE
);
193 bindtextdomain( GETTEXT_PACKAGE
, GNOMELOCALEDIR
);
194 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
195 bind_textdomain_codeset( GETTEXT_PACKAGE
, "UTF-8" );
197 textdomain( GETTEXT_PACKAGE
);
198 g_option_context_add_main_entries( context
, st_entries
, GETTEXT_PACKAGE
);
200 g_option_context_add_main_entries( context
, st_entries
, NULL
);
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
);
217 * print a help message and exit with failure
220 exit_with_usage( void )
222 g_printerr( _( "Try %s --help for usage.\n" ), g_get_prgname());
223 exit( EXIT_FAILURE
);
227 do_update_conf( const gchar
*group
, const gchar
*key
, NAType type
, const gchar
*value
)
237 bvalue
= fma_core_utils_boolean_from_string( value
);
238 ok
= fma_settings_set_boolean_ex( group
, key
, bvalue
);
242 ivalue
= atoi( value
);
243 ok
= fma_settings_set_int_ex( group
, key
, ivalue
);
247 ok
= fma_settings_set_string_ex( group
, key
, value
);
254 return( ok
? EXIT_SUCCESS
: EXIT_FAILURE
);