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)
35 #include <glib-object.h>
36 #include <glib/gi18n.h>
37 #include <glib/gprintf.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>" ) },
74 static GOptionEntry misc_entries
[] = {
76 { "version" , 'v', 0, G_OPTION_ARG_NONE
, &version
,
77 N_( "Output the version number" ), 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
);
87 main( int argc
, char** argv
)
89 int status
= EXIT_SUCCESS
;
90 GOptionContext
*context
;
95 #if !GLIB_CHECK_VERSION( 2,36, 0 )
99 setlocale( LC_ALL
, "" );
100 console_init_log_handler();
102 context
= init_options();
105 g_set_prgname( argv
[0] );
106 help
= g_option_context_get_help( context
, FALSE
, NULL
);
107 g_print( "\n%s", help
);
112 if( !g_option_context_parse( context
, &argc
, &argv
, &error
)){
113 g_printerr( _( "Syntax error: %s\n" ), error
->message
);
114 g_error_free (error
);
118 g_option_context_free( context
);
121 fma_core_utils_print_version();
127 if( !xpath
|| !strlen( xpath
)){
128 g_printerr( _( "Error: a XPath is mandatory.\n" ));
132 if( !xmlfile
|| !strlen( xmlfile
)){
133 g_printerr( _( "Error: a XML filename is mandatory.\n" ));
145 /* Do the main job */
146 delete_path( xmlfile
, BAD_CAST( xpath
));
148 /* Shutdown libxml */
155 * init options context
157 static GOptionContext
*
160 GOptionContext
*context
;
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
);
168 bindtextdomain( GETTEXT_PACKAGE
, GNOMELOCALEDIR
);
169 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
170 bind_textdomain_codeset( GETTEXT_PACKAGE
, "UTF-8" );
172 textdomain( GETTEXT_PACKAGE
);
173 g_option_context_add_main_entries( context
, entries
, GETTEXT_PACKAGE
);
175 g_option_context_add_main_entries( context
, entries
, NULL
);
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
);
192 * print a help message and exit with failure
195 exit_with_usage( void )
197 g_printerr( _( "Try %s --help for usage.\n" ), g_get_prgname());
198 exit( EXIT_FAILURE
);
202 delete_path( const gchar
*xmlfile
, const xmlChar
*xpathExpr
)
205 xmlXPathContextPtr xpathCtx
;
206 xmlXPathObjectPtr xpathObj
;
210 /* Load XML document */
211 doc
= xmlParseFile( xmlfile
);
213 g_printerr( _( "Error: unable to parse file '%s'\n" ), xmlfile
);
217 /* Create xpath evaluation context */
218 xpathCtx
= xmlXPathNewContext( doc
);
219 if( xpathCtx
== NULL
){
220 g_printerr( _( "Error: unable to create new XPath context\n" ));
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
);
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 */
258 fprintf( stderr
, "XPath support not compiled in\n" );