1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * ssindex.c: A wrapper application to index spreadsheets
6 * Jody Goldberg <jody@gnome.org>
8 * Copyright (C) 2004 Jody Goldberg
9 * Copyright (C) 2008-2009 Morten Welinder (terra@gnome.org)
11 #include <gnumeric-config.h>
12 #include <glib/gi18n.h>
14 #include "libgnumeric.h"
16 #include "gnumeric-paths.h"
17 #include <goffice/goffice.h>
18 #include "command-context-stderr.h"
19 #include "workbook-view.h"
23 #include "expr-name.h"
26 #include "sheet-style.h"
28 #include "validation.h"
29 #include "sheet-object-graph.h"
30 #include "gnm-plugin.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
[] = {
44 0, G_OPTION_ARG_NONE
, &ssindex_show_version
,
45 N_("Display program version"),
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"),
58 0, G_OPTION_ARG_NONE
, &ssindex_run_indexer
,
59 N_("Index the given files"),
65 0, G_OPTION_ARG_NONE
, &ssindex_import_encoding
,
66 N_("Optionally specify an encoding for imported content"),
70 /* ---------------------------------------- */
78 WorkbookView
const *wb_view
;
85 ssindex_hlink (IndexerState
*state
, GnmHLink
const *lnk
)
89 str
= gnm_hlink_get_target (lnk
);
91 gsf_xml_out_simple_element (state
->output
, "data", str
);
93 str
= gnm_hlink_get_tip (lnk
);
95 gsf_xml_out_simple_element (state
->output
, "data", str
);
99 ssindex_validation (IndexerState
*state
, GnmValidation
const *valid
)
102 const char *str
= valid
->title
->str
;
104 gsf_xml_out_simple_element (state
->output
, "data", str
);
108 const char *str
= valid
->msg
->str
;
110 gsf_xml_out_simple_element (state
->output
, "data", str
);
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
);
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
);
131 ssindex_hlink (state
, lnk
);
134 if (gnm_style_is_element_set (style
, MSTYLE_VALIDATION
)) {
135 GnmValidation
const *valid
= gnm_style_get_validation (style
);
137 ssindex_validation (state
, valid
);
144 ssindex_chart (IndexerState
*state
, GogObject
*obj
)
149 for (ptr
= obj
->children
; ptr
!= NULL
; ptr
= ptr
->next
)
150 ssindex_chart (state
, ptr
->data
);
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
169 ssindex (char const *file
, GOIOContext
*ioc
)
173 char *str
= go_shell_arg_to_uri (file
);
175 GsfOutput
*gsf_stdout
;
178 state
.wb_view
= workbook_view_new_from_uri (str
, NULL
,
179 ioc
, ssindex_import_encoding
);
182 if (state
.wb_view
== 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
);
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
;
208 if (gnm_object_has_readable_prop (obj
, "text",
209 G_TYPE_STRING
, &str
) &&
211 gsf_xml_out_simple_element (state
.output
,
214 } else if (GNM_IS_SO_GRAPH (obj
))
215 ssindex_chart (&state
,
216 (GogObject
*)sheet_object_graph_get_gog (GNM_SO (obj
)));
220 /* Various stuff in styles. */
221 sheet_style_foreach (state
.sheet
,
222 (GFunc
)cb_index_styles
, &state
);
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
);
239 main (int argc
, char const **argv
)
241 GOErrorInfo
*plugin_errs
;
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 ocontext
= g_option_context_new (_("INFILE..."));
251 g_option_context_add_main_entries (ocontext
, ssindex_options
, GETTEXT_PACKAGE
);
252 g_option_context_add_group (ocontext
, gnm_get_option_group ());
253 g_option_context_parse (ocontext
, &argc
, (gchar
***)&argv
, &error
);
254 g_option_context_free (ocontext
);
257 g_printerr (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
258 error
->message
, argv
[0]);
259 g_error_free (error
);
263 if (ssindex_show_version
) {
264 g_printerr (_("ssindex version '%s'\ndatadir := '%s'\nlibdir := '%s'\n"),
265 GNM_VERSION_FULL
, gnm_sys_data_dir (), gnm_sys_lib_dir ());
267 } else if (!ssindex_run_indexer
&& !ssindex_list_mime_types
) {
268 g_printerr (_("Usage: %s [OPTION...] %s\n"),
276 cc
= gnm_cmd_context_stderr_new ();
277 gnm_plugins_init (GO_CMD_CONTEXT (cc
));
278 go_plugin_db_activate_plugin_list (
279 go_plugins_get_available_plugins (), &plugin_errs
);
281 /* FIXME: What do we want to do here? */
282 go_error_info_free (plugin_errs
);
284 go_component_set_default_command_context (cc
);
286 if (ssindex_run_indexer
) {
287 GOIOContext
*ioc
= go_io_context_new (cc
);
290 go_io_context_set_num_files (ioc
, argc
- 1);
292 for (i
= 1; i
< argc
; i
++) {
293 char const *file
= argv
[i
];
294 go_io_context_processing_file (ioc
, file
);
295 res
|= ssindex (file
, ioc
);
297 g_object_unref (ioc
);
298 } else if (ssindex_list_mime_types
) {
300 for (o
= go_get_file_openers (); o
!= NULL
; o
= o
->next
) {
301 GSList
const *mime
= go_file_opener_get_mimes (o
->data
);
302 for (; mime
!= NULL
; mime
= mime
->next
)
303 g_print ("%s\n", (char const *)mime
->data
);
307 go_component_set_default_command_context (NULL
);
310 gnm_pre_parse_shutdown ();