[nautilus-actions] na-set-conf: new program



commit 43ddbf3dcdc62c6708e1636fd26f08964f3044bf
Author: Pierre Wieser <pwieser trychlos org>
Date:   Tue Jan 3 21:49:50 2012 +0100

    na-set-conf: new program
    
    Update the N-A configuration file during the migration process
    to be sure that GConf I/O provider will actually be read-only.

 ChangeLog               |    8 ++
 src/core/na-settings.c  |   43 ++++++++
 src/core/na-settings.h  |    2 +
 src/utils/Makefile.am   |   11 ++
 src/utils/na-set-conf.c |  253 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 317 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 8a8aa26..32b135b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2012-01-03 Pierre Wieser <pwieser trychlos org>
 
+	* src/utils/na-set-conf.c: New program.
+
+	* src/core/na-settings.c:
+	* src/core/na-settings.h
+	(na_settings_set_string_ex,	na_settings_set_int_ex): New functions.
+
+	* src/utils/Makefile.am: Updated accordingly.
+
 	* src/utils/Makefile.am: Factorize library declarations.
 
 2012-01-02 Pierre Wieser <pwieser trychlos org>
diff --git a/src/core/na-settings.c b/src/core/na-settings.c
index 8084663..5a8f356 100644
--- a/src/core/na-settings.c
+++ b/src/core/na-settings.c
@@ -780,6 +780,24 @@ na_settings_set_string( const gchar *key, const gchar *value )
 }
 
 /**
+ * na_settings_set_string_ex:
+ * @group: the group in the keyed file;
+ * @key: the key whose value is to be returned.
+ * @value: the string to be written.
+ *
+ * This function writes @value as a user preference.
+ *
+ * Returns: %TRUE is the writing has been successful, %FALSE else.
+ *
+ * Since: 3.2
+ */
+gboolean
+na_settings_set_string_ex( const gchar *group, const gchar *key, const gchar *value )
+{
+	return( set_key_value( group, key, value ));
+}
+
+/**
  * na_settings_set_string_list:
  * @key: the key whose value is to be returned.
  * @value: the list of strings to be written.
@@ -811,6 +829,31 @@ na_settings_set_string_list( const gchar *key, const GSList *value )
 }
 
 /**
+ * na_settings_set_int_ex:
+ * @group: the group in the keyed file;
+ * @key: the key whose value is to be returned.
+ * @value: the unsigned integer to be written.
+ *
+ * This function writes @value as a user preference.
+ *
+ * Returns: %TRUE is the writing has been successful, %FALSE else.
+ *
+ * Since: 3.2
+ */
+gboolean
+na_settings_set_int_ex( const gchar *group, const gchar *key, int value )
+{
+	gchar *string;
+	gboolean ok;
+
+	string = g_strdup_printf( "%d", value );
+	ok = set_key_value( group, key, string );
+	g_free( string );
+
+	return( ok );
+}
+
+/**
  * na_settings_set_uint:
  * @key: the key whose value is to be returned.
  * @value: the unsigned integer to be written.
diff --git a/src/core/na-settings.h b/src/core/na-settings.h
index 139cc26..c3e3a6d 100644
--- a/src/core/na-settings.h
+++ b/src/core/na-settings.h
@@ -157,7 +157,9 @@ GList    *na_settings_get_uint_list        ( const gchar *key, gboolean *found,
 gboolean  na_settings_set_boolean          ( const gchar *key, gboolean value );
 gboolean  na_settings_set_boolean_ex       ( const gchar *group, const gchar *key, gboolean value );
 gboolean  na_settings_set_string           ( const gchar *key, const gchar *value );
+gboolean  na_settings_set_string_ex        ( const gchar *group, const gchar *key, const gchar *value );
 gboolean  na_settings_set_string_list      ( const gchar *key, const GSList *value );
+gboolean  na_settings_set_int_ex           ( const gchar *group, const gchar *key, int value );
 gboolean  na_settings_set_uint             ( const gchar *key, guint value );
 gboolean  na_settings_set_uint_list        ( const gchar *key, const GList *value );
 
diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am
index 0fd17c3..11bad4f 100644
--- a/src/utils/Makefile.am
+++ b/src/utils/Makefile.am
@@ -39,6 +39,7 @@ bin_PROGRAMS = \
 
 pkglibexec_PROGRAMS = \
 	na-print-schemas											\
+	na-set-conf													\
 	$(NULL)
 
 AM_CPPFLAGS += \
@@ -96,6 +97,16 @@ na_print_schemas_LDADD = \
 	$(NA_UTILS_LDADD)											\
 	$(NULL)
 
+na_set_conf_SOURCES = \
+	na-set-conf.c												\
+	console-utils.c												\
+	console-utils.h												\
+	$(NULL)
+
+na_set_conf_LDADD = \
+	$(NA_UTILS_LDADD)											\
+	$(NULL)
+
 EXTRA_DIST = \
 	na-gconf2key.sh.in											\
 	$(NULL)
diff --git a/src/utils/na-set-conf.c b/src/utils/na-set-conf.c
new file mode 100644
index 0000000..ce94e2f
--- /dev/null
+++ b/src/utils/na-set-conf.c
@@ -0,0 +1,253 @@
+/*
+ * Nautilus-Actions
+ * A Nautilus extension which offers configurable context menu actions.
+ *
+ * Copyright (C) 2005 The GNOME Foundation
+ * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
+ * Copyright (C) 2009, 2010, 2011, 2012 Pierre Wieser and others (see AUTHORS)
+ *
+ * This Program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This Program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this Library; see the file COPYING.  If not,
+ * write to the Free Software Foundation, Inc., 59 Temple Place,
+ * Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Authors:
+ *   Frederic Ruaudel <grumz grumz net>
+ *   Rodrigo Moya <rodrigo gnome-db org>
+ *   Pierre Wieser <pwieser trychlos org>
+ *   ... and many others (see AUTHORS)
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib-object.h>
+#include <glib/gi18n.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <api/na-core-utils.h>
+
+#include "console-utils.h"
+
+/*
+ * A program to update the N-A configuration files
+ */
+
+#include <core/na-settings.h>
+
+static gchar       *st_option_context = N_( "Set a key=value pair in a key file." );
+
+static gchar       *st_group     = "";
+static gchar       *st_key       = "";
+static gchar       *st_type      = "";
+static gchar       *st_value     = "";
+static gboolean     st_version   = FALSE;
+
+static GOptionEntry st_entries[] = {
+
+	{ "group"                , 'g', 0, G_OPTION_ARG_STRING        , &st_group,
+			N_( "The group to be updated" ), N_( "<STRING>" ) },
+	{ "key"                  , 'k', 0, G_OPTION_ARG_STRING        , &st_key,
+			N_( "The key to be updated" ), N_( "<STRING>" ) },
+	{ "type"                 , 't', 0, G_OPTION_ARG_STRING        , &st_type,
+			/* i18n: 'str', 'int' and 'bool' are literal values: do not translate */
+			N_( "The type of the value to be set, may be 'str', 'int' or 'bool'" ), N_( "<STRING>" ) },
+	{ "value"                , 'v', 0, G_OPTION_ARG_STRING        , &st_value,
+			N_( "The value to be set" ), N_( "<STRING>" ) },
+	{ NULL }
+};
+
+static GOptionEntry misc_entries[] = {
+
+	{ "version"              , 'v', 0, G_OPTION_ARG_NONE          , &st_version,
+			N_( "Output the version number and exit gracefully" ), NULL },
+	{ NULL }
+};
+
+typedef enum {
+	TYPE_NO_TYPE = 0,
+	TYPE_STR,
+	TYPE_INT,
+	TYPE_BOOL
+}
+	NAType;
+
+static GOptionContext *init_options( void );
+static void            exit_with_usage( void );
+static int             do_update_conf( const gchar *group, const gchar *key, NAType type, const gchar *value );
+
+int
+main( int argc, char** argv )
+{
+	int status = EXIT_SUCCESS;
+	GOptionContext *context;
+	GError *error = NULL;
+	gchar *help;
+	gint errors;
+	NAType type;
+	gchar *msgerr;
+
+	g_type_init();
+	setlocale( LC_ALL, "" );
+	console_init_log_handler();
+
+	context = init_options();
+
+	if( argc == 1 ){
+		g_set_prgname( argv[0] );
+		help = g_option_context_get_help( context, FALSE, NULL );
+		g_print( "\n%s", help );
+		g_free( help );
+		exit( status );
+	}
+
+	if( !g_option_context_parse( context, &argc, &argv, &error )){
+		g_printerr( _( "Syntax error: %s\n" ), error->message );
+		g_error_free (error);
+		exit_with_usage();
+	}
+
+	g_option_context_free( context );
+
+	if( st_version ){
+		na_core_utils_print_version();
+		exit( status );
+	}
+
+	errors = 0;
+	type = TYPE_NO_TYPE;
+
+	if( !st_group || !g_utf8_strlen( st_group, -1 )){
+		g_printerr( _( "Error: a group is mandatory.\n" ));
+		errors += 1;
+	}
+
+	if( !st_key || !g_utf8_strlen( st_key, -1 )){
+		g_printerr( _( "Error: a key is mandatory.\n" ));
+		errors += 1;
+	}
+
+	if( !st_type || !g_utf8_strlen( st_type, -1 )){
+		g_printerr( _( "Error: a type is mandatory for setting/updating a value.\n" ));
+		errors += 1;
+
+	} else if( !g_utf8_collate( st_type, "str" )){
+		type = TYPE_STR;
+	} else if( !g_utf8_collate( st_type, "int" )){
+		type = TYPE_INT;
+	} else if( !g_utf8_collate( st_type, "bool" )){
+		type = TYPE_BOOL;
+	} else {
+		/* i18n: 'str', 'int' and 'bool' are literal values: do not translate */
+		msgerr = g_strdup_printf( _( "Error: unknown type: %s. Use 'str', 'int' or bool'.\n" ), st_type );
+		g_printerr( "%s", msgerr );
+		g_free( msgerr );
+		errors += 1;
+	}
+
+	if( !st_value || !g_utf8_strlen( st_value, -1 )){
+		g_printerr( _( "Error: a value is mandatory.\n" ));
+		errors += 1;
+	}
+
+	if( errors ){
+		exit_with_usage();
+	}
+
+	g_return_val_if_fail( type != TYPE_NO_TYPE, EXIT_FAILURE );
+
+	status = do_update_conf( st_group, st_key, type, st_value );
+
+	exit( status );
+}
+
+/*
+ * init options context
+ */
+static GOptionContext *
+init_options( void )
+{
+	GOptionContext *context;
+	gchar* description;
+	GOptionGroup *misc_group;
+
+	context = g_option_context_new( gettext( st_option_context ));
+	g_option_context_set_translation_domain( context, GETTEXT_PACKAGE );
+
+#ifdef ENABLE_NLS
+	bindtextdomain( GETTEXT_PACKAGE, GNOMELOCALEDIR );
+# ifdef HAVE_BIND_TEXTDOMAIN_CODESET
+	bind_textdomain_codeset( GETTEXT_PACKAGE, "UTF-8" );
+# endif
+	textdomain( GETTEXT_PACKAGE );
+	g_option_context_add_main_entries( context, st_entries, GETTEXT_PACKAGE );
+#else
+	g_option_context_add_main_entries( context, st_entries, NULL );
+#endif
+
+	description = console_cmdline_get_description();
+	g_option_context_set_description( context, description );
+	g_free( description );
+
+	misc_group = g_option_group_new(
+			"misc", _( "Miscellaneous options" ), _( "Miscellaneous options" ), NULL, NULL );
+	g_option_group_add_entries( misc_group, misc_entries );
+	g_option_group_set_translation_domain( misc_group, GETTEXT_PACKAGE );
+	g_option_context_add_group( context, misc_group );
+
+	return( context );
+}
+
+/*
+ * print a help message and exit with failure
+ */
+static void
+exit_with_usage( void )
+{
+	g_printerr( _( "Try %s --help for usage.\n" ), g_get_prgname());
+	exit( EXIT_FAILURE );
+}
+
+static int
+do_update_conf( const gchar *group, const gchar *key, NAType type, const gchar *value )
+{
+	gboolean ok;
+	gboolean bvalue;
+	int ivalue;
+
+	ok = FALSE;
+
+	switch( type ){
+		case TYPE_BOOL:
+			bvalue = na_core_utils_boolean_from_string( value );
+			ok = na_settings_set_boolean_ex( group, key, bvalue );
+			break;
+
+		case TYPE_INT:
+			ivalue = atoi( value );
+			ok = na_settings_set_int_ex( group, key, ivalue );
+			break;
+
+		case TYPE_STR:
+			ok = na_settings_set_string_ex( group, key, value );
+			break;
+
+		default:
+			break;
+	}
+
+	return( ok ? EXIT_SUCCESS : EXIT_FAILURE );
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]