README: add deprecation notice
[nautilus-actions.git] / src / utils / fma-delete-xmltree.c
blobd0b8d930b6cba252de35aa514429294f5779a54b
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.h>
35 #include <glib-object.h>
36 #include <glib/gi18n.h>
37 #include <glib/gprintf.h>
38 #include <locale.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <assert.h>
44 #include <libxml/tree.h>
45 #include <libxml/parser.h>
46 #include <libxml/xpath.h>
47 #include <libxml/xpathInternals.h>
50 * This program has been written because I do not have found any reliable
51 * way of removing a mandatory key (an action or a menu) from the GConf
52 * XML backend. It is based on http://xmlsoft.org/examples/xpath2.c
55 #include <api/fma-core-utils.h>
57 #include "console-utils.h"
59 #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_SAX1_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
61 static gchar *xpath = "";
62 static gchar *xmlfile = "";
63 static gboolean version = FALSE;
65 static GOptionEntry entries[] = {
67 { "path" , 'p', 0, G_OPTION_ARG_STRING , &xpath,
68 N_( "The XPath to the tree to be deleted" ), N_( "<STRING>" ) },
69 { "xml" , 'x', 0, G_OPTION_ARG_STRING , &xmlfile,
70 N_( "The filename of the XML backend" ), N_( "<STRING>" ) },
71 { NULL }
74 static GOptionEntry misc_entries[] = {
76 { "version" , 'v', 0, G_OPTION_ARG_NONE , &version,
77 N_( "Output the version number" ), NULL },
78 { NULL }
81 static GOptionContext *init_options( void );
82 static void exit_with_usage( void );
84 static void delete_path( const gchar *xmlfile, const xmlChar *xpath );
86 int
87 main( int argc, char** argv )
89 int status = EXIT_SUCCESS;
90 GOptionContext *context;
91 GError *error = NULL;
92 gchar *help;
93 gint errors;
95 #if !GLIB_CHECK_VERSION( 2,36, 0 )
96 g_type_init();
97 #endif
99 setlocale( LC_ALL, "" );
100 console_init_log_handler();
102 context = init_options();
104 if( argc == 1 ){
105 g_set_prgname( argv[0] );
106 help = g_option_context_get_help( context, FALSE, NULL );
107 g_print( "\n%s", help );
108 g_free( help );
109 exit( status );
112 if( !g_option_context_parse( context, &argc, &argv, &error )){
113 g_printerr( _( "Syntax error: %s\n" ), error->message );
114 g_error_free (error);
115 exit_with_usage();
118 g_option_context_free( context );
120 if( version ){
121 fma_core_utils_print_version();
122 exit( status );
125 errors = 0;
127 if( !xpath || !strlen( xpath )){
128 g_printerr( _( "Error: a XPath is mandatory.\n" ));
129 errors += 1;
132 if( !xmlfile || !strlen( xmlfile )){
133 g_printerr( _( "Error: a XML filename is mandatory.\n" ));
134 errors += 1;
137 if( errors ){
138 exit_with_usage();
141 /* Init libxml */
142 xmlInitParser();
143 LIBXML_TEST_VERSION
145 /* Do the main job */
146 delete_path( xmlfile, BAD_CAST( xpath ));
148 /* Shutdown libxml */
149 xmlCleanupParser();
151 exit( status );
155 * init options context
157 static GOptionContext *
158 init_options( void )
160 GOptionContext *context;
161 gchar* description;
162 GOptionGroup *misc_group;
164 context = g_option_context_new( _( "Delete a XPath from a XML document." ));
165 g_option_context_set_translation_domain( context, GETTEXT_PACKAGE );
167 #ifdef ENABLE_NLS
168 bindtextdomain( GETTEXT_PACKAGE, GNOMELOCALEDIR );
169 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
170 bind_textdomain_codeset( GETTEXT_PACKAGE, "UTF-8" );
171 # endif
172 textdomain( GETTEXT_PACKAGE );
173 g_option_context_add_main_entries( context, entries, GETTEXT_PACKAGE );
174 #else
175 g_option_context_add_main_entries( context, entries, NULL );
176 #endif
178 description = console_cmdline_get_description();
179 g_option_context_set_description( context, description );
180 g_free( description );
182 misc_group = g_option_group_new(
183 "misc", _( "Miscellaneous options" ), _( "Miscellaneous options" ), NULL, NULL );
184 g_option_group_add_entries( misc_group, misc_entries );
185 g_option_group_set_translation_domain( misc_group, GETTEXT_PACKAGE );
186 g_option_context_add_group( context, misc_group );
188 return( context );
192 * print a help message and exit with failure
194 static void
195 exit_with_usage( void )
197 g_printerr( _( "Try %s --help for usage.\n" ), g_get_prgname());
198 exit( EXIT_FAILURE );
201 static void
202 delete_path( const gchar *xmlfile, const xmlChar *xpathExpr )
204 xmlDocPtr doc;
205 xmlXPathContextPtr xpathCtx;
206 xmlXPathObjectPtr xpathObj;
207 xmlNodePtr nodePtr;
208 int size, i;
210 /* Load XML document */
211 doc = xmlParseFile( xmlfile );
212 if( doc == NULL ){
213 g_printerr( _( "Error: unable to parse file '%s'\n" ), xmlfile );
214 return;
217 /* Create xpath evaluation context */
218 xpathCtx = xmlXPathNewContext( doc );
219 if( xpathCtx == NULL ){
220 g_printerr( _( "Error: unable to create new XPath context\n" ));
221 xmlFreeDoc( doc );
222 return;
225 /* Evaluate xpath expression */
226 xpathObj = xmlXPathEvalExpression( xpathExpr, xpathCtx );
227 if( xpathObj == NULL ){
228 g_printerr( _( "Error: unable to evaluate XPath expression '%s'\n" ), xpathExpr );
229 xmlXPathFreeContext( xpathCtx );
230 xmlFreeDoc( doc );
231 return;
234 /* update selected nodes */
235 /*update_xpath_nodes(xpathObj->nodesetval, value);*/
236 size = ( xpathObj->nodesetval) ? xpathObj->nodesetval->nodeNr : 0;
237 /*g_print( "size=%d\n", size );*/
238 for( i=0 ; i<size ; ++i ){
239 nodePtr = xpathObj->nodesetval->nodeTab[i];
240 /*xmlElemDump( stdout, doc, nodePtr );*/
241 xmlUnlinkNode( nodePtr );
244 /* Cleanup of XPath data */
245 xmlXPathFreeObject( xpathObj );
246 xmlXPathFreeContext( xpathCtx );
248 /* dump the resulting document */
249 xmlDocDump( stdout, doc );
251 /* free the document */
252 xmlFreeDoc( doc );
255 #else
257 main( void ){
258 fprintf( stderr, "XPath support not compiled in\n" );
259 exit( 1 );
261 #endif