Update Spanish translation
[gnumeric.git] / src / ssindex.c
blob14274fd0d82e917d89432b5722e1d495065b01f1
1 /*
2 * ssindex.c: A wrapper application to index spreadsheets
4 * Author:
5 * Jody Goldberg <jody@gnome.org>
7 * Copyright (C) 2004 Jody Goldberg
8 * Copyright (C) 2008-2009 Morten Welinder (terra@gnome.org)
9 */
10 #include <gnumeric-config.h>
11 #include <glib/gi18n.h>
12 #include <gnumeric.h>
13 #include <libgnumeric.h>
14 #include <gutils.h>
15 #include <gnumeric-paths.h>
16 #include <goffice/goffice.h>
17 #include <command-context-stderr.h>
18 #include <workbook-view.h>
19 #include <workbook.h>
20 #include <sheet.h>
21 #include <cell.h>
22 #include <expr-name.h>
23 #include <value.h>
24 #include <mstyle.h>
25 #include <sheet-style.h>
26 #include <hlink.h>
27 #include <validation.h>
28 #include <sheet-object-graph.h>
29 #include <gnm-plugin.h>
30 #include <gnumeric-conf.h>
32 #include <gsf/gsf-utils.h>
33 #include <gsf/gsf-libxml.h>
34 #include <gsf/gsf-output-stdio.h>
36 static gboolean ssindex_show_version = FALSE;
37 static gboolean ssindex_list_mime_types = FALSE;
38 static gboolean ssindex_run_indexer = FALSE;
39 static char *ssindex_import_encoding = NULL;
41 static GOptionEntry const ssindex_options [] = {
43 "version", 'v',
44 0, G_OPTION_ARG_NONE, &ssindex_show_version,
45 N_("Display program version"),
46 NULL
50 "list-mime-types", 'm',
51 0, G_OPTION_ARG_NONE, &ssindex_list_mime_types,
52 N_("List MIME types which ssindex is able to read"),
53 NULL
57 "index", 'i',
58 0, G_OPTION_ARG_NONE, &ssindex_run_indexer,
59 N_("Index the given files"),
60 NULL
64 "encoding", 'E',
65 0, G_OPTION_ARG_NONE, &ssindex_import_encoding,
66 N_("Optionally specify an encoding for imported content"),
67 N_("ENCODING")
70 /* ---------------------------------------- */
72 { NULL }
76 typedef struct {
77 GOIOContext *context;
78 WorkbookView const *wb_view;
79 Workbook const *wb;
80 Sheet *sheet;
81 GsfXMLOut *output;
82 } IndexerState;
84 static void
85 ssindex_hlink (IndexerState *state, GnmHLink const *lnk)
87 gchar const *str;
89 str = gnm_hlink_get_target (lnk);
90 if (str)
91 gsf_xml_out_simple_element (state->output, "data", str);
93 str = gnm_hlink_get_tip (lnk);
94 if (str)
95 gsf_xml_out_simple_element (state->output, "data", str);
98 static void
99 ssindex_validation (IndexerState *state, GnmValidation const *valid)
101 if (valid->title) {
102 const char *str = valid->title->str;
103 if (str && *str)
104 gsf_xml_out_simple_element (state->output, "data", str);
107 if (valid->msg) {
108 const char *str = valid->msg->str;
109 if (str && *str)
110 gsf_xml_out_simple_element (state->output, "data", str);
114 static void
115 cb_index_cell (G_GNUC_UNUSED gpointer ignore,
116 GnmCell const *cell, IndexerState *state)
118 if (cell->value != NULL && VALUE_IS_STRING (cell->value)) {
119 char const *str = value_peek_string (cell->value);
120 if (str != NULL && *str)
121 gsf_xml_out_simple_element (state->output, "data", str);
125 static void
126 cb_index_styles (GnmStyle *style, IndexerState *state)
128 if (gnm_style_is_element_set (style, MSTYLE_HLINK)) {
129 GnmHLink const *lnk = gnm_style_get_hlink (style);
130 if (lnk != NULL)
131 ssindex_hlink (state, lnk);
134 if (gnm_style_is_element_set (style, MSTYLE_VALIDATION)) {
135 GnmValidation const *valid = gnm_style_get_validation (style);
136 if (valid)
137 ssindex_validation (state, valid);
140 /* Input Msg? */
143 static void
144 ssindex_chart (IndexerState *state, GogObject *obj)
146 GSList *ptr;
148 /* TODO */
149 for (ptr = obj->children ; ptr != NULL ; ptr = ptr->next)
150 ssindex_chart (state, ptr->data);
153 static void
154 cb_index_name (G_GNUC_UNUSED gconstpointer key,
155 GnmNamedExpr const *nexpr, IndexerState *state)
157 gsf_xml_out_simple_element (state->output,
158 "data", expr_name_name (nexpr));
163 * Other things we could index
164 * - The names of external refernces
165 * - functions used
166 * - plugins used
168 static int
169 ssindex (char const *file, GOIOContext *ioc)
171 int i, res = 0;
172 GSList *objs, *ptr;
173 char *str = go_shell_arg_to_uri (file);
174 IndexerState state;
175 GsfOutput *gsf_stdout;
176 Workbook *wb;
178 state.wb_view = workbook_view_new_from_uri (str, NULL,
179 ioc, ssindex_import_encoding);
180 g_free (str);
182 if (state.wb_view == NULL)
183 return 1;
185 state.sheet = NULL;
187 gsf_stdout = gsf_output_stdio_new_FILE ("<stdout>", stdout, TRUE);
188 state.output = gsf_xml_out_new (gsf_stdout);
189 gsf_xml_out_start_element (state.output, "gnumeric");
190 state.wb = wb = wb_view_get_workbook (state.wb_view);
192 workbook_foreach_name (wb, TRUE, (GHFunc)cb_index_name, &state);
194 for (i = 0; i < workbook_sheet_count (wb); i++) {
195 state.sheet = workbook_sheet_by_index (wb, i);
196 gsf_xml_out_simple_element (state.output,
197 "data", state.sheet->name_unquoted);
199 /* cell content */
200 sheet_cell_foreach (state.sheet,
201 (GHFunc)&cb_index_cell, &state);
203 /* now the objects */
204 objs = sheet_objects_get (state.sheet, NULL, G_TYPE_NONE);
205 for (ptr = objs ; ptr != NULL ; ptr = ptr->next) {
206 GObject *obj = ptr->data;
207 char *str = NULL;
208 if (gnm_object_has_readable_prop (obj, "text",
209 G_TYPE_STRING, &str) &&
210 str) {
211 gsf_xml_out_simple_element (state.output,
212 "data", str);
213 g_free (str);
214 } else if (GNM_IS_SO_GRAPH (obj))
215 ssindex_chart (&state,
216 (GogObject *)sheet_object_graph_get_gog (GNM_SO (obj)));
218 g_slist_free (objs);
220 /* Various stuff in styles. */
221 sheet_style_foreach (state.sheet,
222 (GFunc)cb_index_styles, &state);
224 /* Local names. */
225 gnm_sheet_foreach_name (state.sheet,
226 (GHFunc)cb_index_name, &state);
229 gsf_xml_out_end_element (state.output); /* </gnumeric> */
230 gsf_output_close (gsf_stdout);
231 g_object_unref (gsf_stdout);
233 g_object_unref (wb);
235 return res;
239 main (int argc, char const **argv)
241 GOErrorInfo *plugin_errs;
242 int res = 0;
243 GOCmdContext *cc;
244 GOptionContext *ocontext;
245 GError *error = NULL;
247 /* No code before here, we need to init threads */
248 argv = gnm_pre_parse_init (argc, argv);
250 gnm_conf_set_persistence (FALSE);
252 ocontext = g_option_context_new (_("INFILE..."));
253 g_option_context_add_main_entries (ocontext, ssindex_options, GETTEXT_PACKAGE);
254 g_option_context_add_group (ocontext, gnm_get_option_group ());
255 g_option_context_parse (ocontext, &argc, (gchar ***)&argv, &error);
256 g_option_context_free (ocontext);
258 if (error) {
259 g_printerr (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
260 error->message, argv[0]);
261 g_error_free (error);
262 return 1;
265 if (ssindex_show_version) {
266 g_printerr (_("ssindex version '%s'\ndatadir := '%s'\nlibdir := '%s'\n"),
267 GNM_VERSION_FULL, gnm_sys_data_dir (), gnm_sys_lib_dir ());
268 return 0;
269 } else if (!ssindex_run_indexer && !ssindex_list_mime_types) {
270 g_printerr (_("Usage: %s [OPTION...] %s\n"),
271 g_get_prgname (),
272 _("INFILE..."));
273 return 1;
276 gnm_init ();
278 cc = gnm_cmd_context_stderr_new ();
279 gnm_plugins_init (GO_CMD_CONTEXT (cc));
280 go_plugin_db_activate_plugin_list (
281 go_plugins_get_available_plugins (), &plugin_errs);
282 if (plugin_errs) {
283 /* FIXME: What do we want to do here? */
284 go_error_info_free (plugin_errs);
286 go_component_set_default_command_context (cc);
288 if (ssindex_run_indexer) {
289 GOIOContext *ioc = go_io_context_new (cc);
290 int i;
292 go_io_context_set_num_files (ioc, argc - 1);
294 for (i = 1; i < argc; i++) {
295 char const *file = argv[i];
296 go_io_context_processing_file (ioc, file);
297 res |= ssindex (file, ioc);
299 g_object_unref (ioc);
300 } else if (ssindex_list_mime_types) {
301 GList *o;
302 for (o = go_get_file_openers (); o != NULL ; o = o->next) {
303 GSList const *mime = go_file_opener_get_mimes (o->data);
304 for (; mime != NULL ; mime = mime->next)
305 g_print ("%s\n", (char const *)mime->data);
309 go_component_set_default_command_context (NULL);
310 g_object_unref (cc);
311 gnm_shutdown ();
312 gnm_pre_parse_shutdown ();
314 return res;