Update Spanish translation
[gnumeric.git] / src / libgnumeric.c
blob051d96d1efdf8039282d4fc84d221f5fc8301430
1 /*
2 * libgnumeric.c: global initialization and management code
4 * Copyright (C) 2000-2007 Jody Goldberg (jody@gnome.org)
5 * Copyright (C) 1997-1999 Miguel de Icaza (miguel@kernel.org)
6 * Copyright (C) 2006-2009 Morten Welinder (terra@gnome.org)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) version 3.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 * USA
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
25 #include <gnumeric.h>
26 #include <libgnumeric.h>
27 #include <gutils.h>
29 #include <application.h>
30 #include <stf.h>
31 #include <gnm-format.h>
32 #include <command-context.h>
33 #include <command-context-stderr.h>
34 #include <workbook.h>
35 #include <sheet-object.h>
36 #include <number-match.h>
37 #include <expr-name.h>
38 #include <func.h>
39 #include <print-info.h>
40 #include <style-font.h>
41 #include <mstyle.h>
42 #include <style-border.h>
43 #include <style-color.h>
44 #include <print.h>
45 #include <dependent.h>
46 #include <sheet-autofill.h>
47 #include <sheet-private.h>
48 #include <xml-sax.h>
49 #include <clipboard.h>
50 #include <gui-clipboard.h>
51 #include <value.h>
52 #include <expr.h>
53 #include <expr-deriv.h>
54 #include <parse-util.h>
55 #include <rendered-value.h>
56 #include <gnumeric-conf.h>
57 #include <gnm-plugin.h>
58 #include <mathfunc.h>
59 #include <hlink.h>
60 #include <wbc-gtk-impl.h>
61 #include <gnmresources.h>
62 #include <goffice/goffice.h>
64 #ifdef HAVE_SYS_RESOURCE_H
65 #include <sys/resource.h>
66 #endif
67 #include <locale.h>
69 #ifndef HAVE_GTK_ICON_THEME_ADD_RESOURCE_PATH
70 #define gtk_icon_theme_add_resource_path fake_gtk_icon_theme_add_resource_path
73 static void
74 walk_resource_path (const char *path, int level, int size)
76 char **children = g_resources_enumerate_children (path, 0, NULL);
77 int i;
79 if (!children)
80 return;
82 for (i = 0; children[i]; i++) {
83 const char *child = children[i];
84 char *subpath;
85 GBytes *data;
87 if (level == 0) {
88 size = atol (child);
89 if (size <= 0)
90 continue;
93 subpath = g_build_path ("/", path, child, NULL);
95 data = g_resources_lookup_data (subpath, 0, NULL);
96 if (data) {
97 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_resource (subpath, NULL);
98 if (pixbuf && size > 0 && strchr (child, '.')) {
99 char *iconname = g_strdup (child);
100 strchr(iconname, '.')[0] = 0;
101 if (gnm_debug_flag ("icons"))
102 g_printerr ("Defining icon %s at size %d\n", iconname, size);
103 gtk_icon_theme_add_builtin_icon (iconname,
104 size,
105 pixbuf);
107 g_object_unref (pixbuf);
108 g_free (iconname);
111 g_bytes_unref (data);
112 } else
113 walk_resource_path (subpath, level + 1, size);
114 g_free (subpath);
117 g_strfreev (children);
120 static void
121 fake_gtk_icon_theme_add_resource_path (GtkIconTheme G_GNUC_UNUSED *theme,
122 const char *path)
124 walk_resource_path (path, 0, -1);
127 #endif
129 static gboolean param_show_version = FALSE;
130 static char *param_lib_dir = NULL;
131 static char *param_data_dir = NULL;
133 static GOptionEntry const libspreadsheet_options [] = {
134 /*********************************
135 * Public Actions */
137 "version", 'v',
138 0, G_OPTION_ARG_NONE, &param_show_version,
139 N_("Display Gnumeric's version"),
140 NULL
143 /*********************************
144 * Public Variables */
146 "lib-dir", 'L',
147 0, G_OPTION_ARG_FILENAME, &param_lib_dir,
148 N_("Set the root library directory"),
149 N_("DIR")
152 "data-dir", 'D',
153 0, G_OPTION_ARG_FILENAME, &param_data_dir,
154 N_("Adjust the root data directory"),
155 N_("DIR")
158 /**************************************
159 * Hidden debugging flags */
161 { NULL }
164 static gboolean
165 cb_gnm_option_group_post_parse (GOptionContext *context,
166 GOptionGroup *group,
167 gpointer data,
168 GError **error)
170 if (param_show_version) {
171 g_print (_("gnumeric version '%s'\ndatadir := '%s'\nlibdir := '%s'\n"),
172 GNM_VERSION_FULL, gnm_sys_data_dir (), gnm_sys_lib_dir ());
173 exit (0);
175 return TRUE;
179 * gnm_get_option_group: (skip)
181 * Returns a #GOptionGroup for the commandline arguments recognized
182 * by libspreadsheet. You should add this group to your #GOptionContext with
183 * g_option_context_add_group(), if you are using g_option_context_parse() to
184 * parse your commandline arguments.
186 * Returns a #GOptionGroup for the commandline arguments recognized
187 * by libspreadsheet
189 * Since: 1.8
191 GOptionGroup *
192 gnm_get_option_group (void)
194 GOptionGroup *group = g_option_group_new ("libspreadsheet",
195 _("Gnumeric Options"), _("Show Gnumeric Options"), NULL, NULL);
196 g_option_group_add_entries (group, libspreadsheet_options);
197 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
198 g_option_group_set_parse_hooks (group, NULL,
199 &cb_gnm_option_group_post_parse);
200 return group;
204 * gnm_pre_parse_init:
205 * @argc:
206 * @argv:
208 * Initialization to be done before cmd line arguments are handled.
209 * Needs to be called first, before any other initialization.
211 * Returns: (transfer none): the arguments in UTF-8 encoding.
213 gchar const **
214 gnm_pre_parse_init (int argc, gchar const **argv)
217 * NO CODE BEFORE THIS POINT, PLEASE!
219 * Using threads (by way of libraries) makes our stack too small in some
220 * circumstances. It is hard to control directly, but setting the stack
221 * limit to something not unlimited seems to work.
223 * See http://bugzilla.gnome.org/show_bug.cgi?id=92131
225 #ifdef HAVE_SYS_RESOURCE_H
226 struct rlimit rlim;
228 if (getrlimit (RLIMIT_STACK, &rlim) == 0) {
229 rlim_t our_lim = 64 * 1024 * 1024;
230 if (rlim.rlim_max != RLIM_INFINITY)
231 our_lim = MIN (our_lim, rlim.rlim_max);
232 if (rlim.rlim_cur != RLIM_INFINITY &&
233 rlim.rlim_cur < our_lim) {
234 rlim.rlim_cur = our_lim;
235 (void)setrlimit (RLIMIT_STACK, &rlim);
238 #endif
240 /* Default value is bogus, see 732184. */
241 if (!getenv ("G_ENABLE_DIAGNOSTIC")) {
242 #ifdef HAVE_SETENV
243 setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE);
244 #elif defined(HAVE_PUTENV)
245 putenv ("G_ENABLE_DIAGNOSTIC=0");
246 #else
247 /* No big deal. */
248 #endif
251 /* On win32 argv contains 'ansi' encoded args. We need to manually
252 * pull in the real versions and convert them to utf-8 */
253 argv = go_shell_argv_to_glib_encoding (argc, argv);
255 g_set_prgname (argv[0]);
257 /* Make stdout line buffered - we only use it for debug info */
258 setvbuf (stdout, NULL, _IOLBF, 0);
260 gutils_init ();
262 bindtextdomain (GETTEXT_PACKAGE, gnm_locale_dir ());
263 bindtextdomain (GETTEXT_PACKAGE "-functions", gnm_locale_dir ());
264 textdomain (GETTEXT_PACKAGE);
266 /* Force all of the locale segments to update from the environment.
267 * Unless we do this they will default to C
269 setlocale (LC_ALL, "");
271 return argv;
274 void
275 gnm_pre_parse_shutdown (void)
277 go_shell_argv_to_glib_encoding_free ();
280 void
281 gnm_init (void)
283 static gboolean inited = FALSE;
284 if (inited)
285 return;
286 inited = TRUE;
288 // Might be duplicate call, but that's ok
289 gutils_init ();
291 libgoffice_init ();
292 _gnm_register_resource ();
293 if (gdk_screen_get_default ()) {
294 /* Only when we have a gui. */
295 gtk_icon_theme_add_resource_path (gtk_icon_theme_get_default (),
296 "/org/gnumeric/gnumeric/icons");
299 gnm_plugins_service_init ();
301 g_object_new (GNM_APP_TYPE, NULL);
302 mathfunc_init ();
304 gnm_style_init ();
305 gnm_conf_init ();
306 gnm_color_init ();
307 gnm_font_init (); /* requires config */
309 value_init ();
310 parse_util_init ();
311 gnm_expr_init_ ();
312 gnm_sheet_cell_init ();
313 clipboard_init ();
314 gui_clipboard_init ();
315 dependent_types_init ();
316 gnm_rendered_value_init ();
317 gnm_func_init_ ();
318 print_init ();
319 gnm_autofill_init ();
320 sheet_objects_init ();
321 gnm_hlink_init_ ();
323 /* The statically linked in file formats */
324 gnm_xml_sax_read_init ();
325 gnm_xml_sax_write_init ();
326 stf_init ();
328 /* Make sure that images will be displayed with the correct
329 resolution, see #628472 */
330 go_image_set_default_dpi (gnm_app_display_dpi_get (TRUE),
331 gnm_app_display_dpi_get (FALSE));
334 void
335 gnm_shutdown (void)
337 GSList *plugin_states;
339 gnm_app_clipboard_clear (TRUE);
341 plugin_states = go_plugins_shutdown ();
342 if (NULL != plugin_states) {
343 gnm_conf_set_plugins_file_states (plugin_states);
344 g_slist_free_full (plugin_states, g_free);
347 stf_shutdown ();
348 gnm_xml_sax_write_shutdown ();
349 gnm_xml_sax_read_shutdown ();
351 gnm_autofill_shutdown ();
352 print_shutdown ();
353 gnm_func_shutdown_ ();
355 gnm_rendered_value_shutdown ();
356 dependent_types_shutdown ();
357 gui_clipboard_shutdown ();
358 clipboard_shutdown ();
359 gnm_sheet_cell_shutdown ();
360 gnm_expr_deriv_shutdown_ ();
361 gnm_expr_shutdown_ ();
362 parse_util_shutdown ();
363 value_shutdown ();
365 // The style leak printer may access font/border/color info so
366 // shut styles down first.
367 gnm_style_shutdown ();
368 gnm_font_shutdown ();
369 gnm_border_shutdown ();
370 gnm_color_shutdown ();
372 gnm_conf_shutdown ();
374 _gnm_unregister_resource ();
375 libgoffice_shutdown ();
376 go_plugin_services_shutdown ();
377 g_object_unref (gnm_app_get_app ());
378 gutils_shutdown ();