GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / ssindex.c
blobe5f168c7d05acb85c812ca7deb6378e90f326afd
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * ssindex.c: A wrapper application to index spreadsheets
5 * Author:
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>
13 #include "gnumeric.h"
14 #include "libgnumeric.h"
15 #include "gutils.h"
16 #include "gnumeric-paths.h"
17 #include <goffice/goffice.h>
18 #include "command-context-stderr.h"
19 #include "workbook-view.h"
20 #include "workbook.h"
21 #include "sheet.h"
22 #include "cell.h"
23 #include "expr-name.h"
24 #include "value.h"
25 #include "mstyle.h"
26 #include "sheet-style.h"
27 #include "hlink.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 [] = {
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 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);
256 if (error) {
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);
260 return 1;
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 ());
266 return 0;
267 } else if (!ssindex_run_indexer && !ssindex_list_mime_types) {
268 g_printerr (_("Usage: %s [OPTION...] %s\n"),
269 g_get_prgname (),
270 _("INFILE..."));
271 return 1;
274 gnm_init ();
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);
280 if (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);
288 int i;
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) {
299 GList *o;
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);
308 g_object_unref (cc);
309 gnm_shutdown ();
310 gnm_pre_parse_shutdown ();
312 return res;