File: openoffice-write.c

package info (click to toggle)
gnumeric 1.6.3-5.1%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 70,644 kB
  • ctags: 18,973
  • sloc: ansic: 204,271; xml: 47,128; sh: 8,917; makefile: 2,540; yacc: 1,137; perl: 179; python: 86
file content (322 lines) | stat: -rw-r--r-- 10,276 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * openoffice-write.c : export OpenOffice OASIS .sxc files
 *
 * Copyright (C) 2004 Jody Goldberg (jody@gnome.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

/*****************************************************************************/

#include <gnumeric-config.h>
#include <gnumeric.h>
#include <workbook-view.h>
#include <goffice/app/file.h>
#include <goffice/app/io-context.h>
#include <gnm-format.h>
#include <workbook.h>
#include <workbook-priv.h> /* Workbook::names */
#include <cell.h>
#include <sheet.h>
#include <sheet-view.h>
#include <sheet-style.h>
#include <summary.h>
#include <style-color.h>
#include <expr.h>
#include <expr-impl.h>
#include <expr-name.h>
#include <value.h>
#include <str.h>
#include <ranges.h>
#include <mstyle.h>
#include <style-border.h>
#include <validation.h>
#include <hlink.h>
#include <solver.h>
#include <sheet-filter.h>
#include <print-info.h>
#include <print-info.h>
#include <tools/scenarios.h>

#include <gsf/gsf-libxml.h>
#include <gsf/gsf-output.h>
#include <gsf/gsf-outfile.h>
#include <gsf/gsf-outfile-zip.h>
#include <gsf/gsf-utils.h>
#include <glib/gi18n.h>
#include <locale.h>

#define MANIFEST "manifest:"
#define OFFICE	 "office:"
#define STYLE	 "style:"
#define TABLE	 "table:"

typedef struct {
	GsfXMLOut *xml;
	IOContext *ioc;
	WorkbookView const *wbv;
	Workbook const	   *wb;
} GnmOOExport;

void	openoffice_file_save (GOFileSaver const *fs, IOContext *ioc,
			      WorkbookView const *wbv, GsfOutput *output);

static void
oo_write_mimetype (GnmOOExport *state, GsfOutput *child)
{
	gsf_output_puts (child, "application/vnd.sun.xml.calc");
}

/*****************************************************************************/

static unsigned oo_max_cols (void) { return 256; }
static unsigned oo_max_rows (void) { return 32000; }

static void
oo_start_style (GsfXMLOut *xml, char const *name, char const *family)
{
	gsf_xml_out_start_element (xml, STYLE "style");
	gsf_xml_out_add_cstr_unchecked (xml, STYLE "name", name);
	gsf_xml_out_add_cstr_unchecked (xml, STYLE "family", family);
}
static void
oo_write_table_styles (GnmOOExport *state)
{
	oo_start_style (state->xml, "ta1", "table");
	gsf_xml_out_add_cstr_unchecked (state->xml, STYLE "master-page-name", "Default");

	gsf_xml_out_start_element (state->xml, STYLE "properties");
	gsf_xml_out_add_bool (state->xml, TABLE "display", TRUE);
	gsf_xml_out_end_element (state->xml); /* </style:properties> */

	gsf_xml_out_end_element (state->xml); /* </style:style> */
}

static void
oo_write_sheet (GnmOOExport *state, Sheet const *sheet)
{
	GnmStyle *col_styles [SHEET_MAX_COLS];
	GnmRange  extent;
	int max_cols = oo_max_cols ();
	int max_rows = oo_max_rows ();
	int i;

	extent = sheet_get_extent (sheet, FALSE);
	if (extent.end.row >= max_rows) {
		gnm_io_warning (state->ioc,
			_("Some content will be lost when saving as OpenOffice .sxc. "
			  "It only supports %d rows, and sheet '%s' has %d"),
			max_rows, sheet->name_unquoted, extent.end.row);
		extent.end.row = max_rows;
	}
	if (extent.end.col >= max_cols) {
		gnm_io_warning (state->ioc,
			_("Some content will be lost when saving as OpenOffice .sxc. "
			  "It only supports %d columns, and sheet '%s' has %d"),
			max_cols, sheet->name_unquoted, extent.end.col);
		extent.end.col = max_cols;
	}

	sheet_style_get_extent (sheet, &extent, col_styles);

	/* include collapsed or hidden cols and rows */
	for (i = max_rows ; i-- > extent.end.row ; )
		if (!colrow_is_empty (sheet_row_get (sheet, i))) {
			extent.end.row = i;
			break;
		}
	for (i = max_cols ; i-- > extent.end.col ; )
		if (!colrow_is_empty (sheet_col_get (sheet, i))) {
			extent.end.col = i;
			break;
		}
	/* It is ok to have formatting out of range, we can disregard that. */
	if (extent.end.col > max_cols)
		extent.end.col = max_cols;
	if (extent.end.row > max_rows)
		extent.end.row = max_rows;
}

static void
oo_write_content (GnmOOExport *state, GsfOutput *child)
{
	static struct {
		char const *key;
		char const *url;
	} const ns[] = {
		{ "xmlns:office",	"http://openoffice.org/2000/office" },
		{ "xmlns:style",	"http://openoffice.org/2000/style" },
		{ "xmlns:text",		"http://openoffice.org/2000/text" },
		{ "xmlns:table",	"http://openoffice.org/2000/table" },
		{ "xmlns:draw",		"http://openoffice.org/2000/drawing" },
		{ "xmlns:fo",		"http://www.w3.org/1999/XSL/Format" },
		{ "xmlns:xlink",	"http://www.w3.org/1999/xlink" },
		{ "xmlns:number",	"http://openoffice.org/2000/datastyle" },
		{ "xmlns:svg",		"http://www.w3.org/2000/svg" },
		{ "xmlns:chart",	"http://openoffice.org/2000/chart" },
		{ "xmlns:dr3d",		"http://openoffice.org/2000/dr3d" },
		{ "xmlns:math",		"http://www.w3.org/1998/Math/MathML" },
		{ "xmlns:form",		"http://openoffice.org/2000/form" },
		{ "xmlns:script",	"http://openoffice.org/2000/script" },
	};
	int i;

	state->xml = gsf_xml_out_new (child);
	gsf_xml_out_set_doc_type (state->xml, 
		"<!DOCTYPE "
		   "office:document-content "
		   "PUBLIC "
		   "\"-//OpenOffice.org//DTD "
		   "OfficeDocument "
		   "1.0//EN\" \"office.dtd\">");
	gsf_xml_out_start_element (state->xml, OFFICE "document-content");

	for (i = 0 ; i < (int)G_N_ELEMENTS (ns) ; i++)
		gsf_xml_out_add_cstr_unchecked (state->xml, ns[i].key, ns[i].url);
	gsf_xml_out_add_cstr_unchecked (state->xml, OFFICE "class", "spreadsheet");
	gsf_xml_out_add_cstr_unchecked (state->xml, OFFICE "version", "1.0");

	gsf_xml_out_simple_element (state->xml, OFFICE "script", NULL);

	gsf_xml_out_start_element (state->xml, OFFICE "font-decls");
	gsf_xml_out_end_element (state->xml); /* </office:font-decls> */

	gsf_xml_out_start_element (state->xml, OFFICE "automatic-styles");
	oo_write_table_styles (state);
	gsf_xml_out_end_element (state->xml); /* </office:automatic-styles> */

	gsf_xml_out_start_element (state->xml, OFFICE "body");
	for (i = 0; i < workbook_sheet_count (state->wb); i++) {
		Sheet *sheet = workbook_sheet_by_index (state->wb, i);
#warning validate sheet name against OOo conventions
		gsf_xml_out_start_element (state->xml, TABLE "table");
		gsf_xml_out_add_cstr (state->xml, TABLE "name", sheet->name_unquoted);
		oo_write_sheet (state, sheet);
		gsf_xml_out_end_element (state->xml); /* </table:table> */
	}
	gsf_xml_out_end_element (state->xml); /* </office:automatic-styles> */

	gsf_xml_out_end_element (state->xml); /* </office:document-content> */
}

/*****************************************************************************/

static void
oo_write_styles (GnmOOExport *state, GsfOutput *child)
{
}

/*****************************************************************************/

static void
oo_write_meta (GnmOOExport *state, GsfOutput *child)
{
}

/*****************************************************************************/

static void
oo_write_settings (GnmOOExport *state, GsfOutput *child)
{
}

/**********************************************************************************/

static void
oo_file_entry (GsfXMLOut *out, char const *type, char const *name)
{
	gsf_xml_out_start_element (out, MANIFEST "file-entry");
	gsf_xml_out_add_cstr (out, MANIFEST "media-type", type);
	gsf_xml_out_add_cstr (out, MANIFEST "full-path", name);
	gsf_xml_out_end_element (out); /* </manifest:file-entry> */
}

static void
oo_write_manifest (GnmOOExport *state, GsfOutput *child)
{
	GsfXMLOut *xml = gsf_xml_out_new (child);
	gsf_xml_out_set_doc_type (xml, 
		"<!DOCTYPE "
		   "manifest:manifest "
		   "PUBLIC "
		   "\"-//OpenOffice.org//DTD "
		   "Manifest "
		   "1.0//EN\" \"Manifest.dtd\">");
	gsf_xml_out_start_element (xml, MANIFEST "manifest");
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns:manifest",
		"http://openoffice.org/2001/manifest");
	oo_file_entry (xml, "application/vnd.sun.xml.calc" ,"/");
	oo_file_entry (xml, "", "Pictures/");
	oo_file_entry (xml, "text/xml", "content.xml");
	oo_file_entry (xml, "text/xml", "styles.xml");
	oo_file_entry (xml, "text/xml", "meta.xml");
	oo_file_entry (xml, "text/xml", "settings.xml");
	gsf_xml_out_end_element (xml); /* </manifest:manifest> */
}

/**********************************************************************************/

void
openoffice_file_save (GOFileSaver const *fs, IOContext *ioc,
		      WorkbookView const *wbv, GsfOutput *output)
{
	static struct {
		void (*func) (GnmOOExport *state, GsfOutput *child);
		char const *name;
	} const streams[] = {
		{ oo_write_mimetype,	"mimetype" },
		{ oo_write_content,	"content.xml" },
		{ oo_write_styles,	"styles.xml" },
		{ oo_write_meta,	"meta.xml" },
		{ oo_write_settings,	"settings.xml" },
		{ oo_write_manifest,	"META-INF/manifest.xml" }
	};

	GnmOOExport state;
	char *old_num_locale, *old_monetary_locale;
	GsfOutfile *outfile = NULL;
	GsfOutput  *child;
	GError *err;
	unsigned i;

	old_num_locale = g_strdup (go_setlocale (LC_NUMERIC, NULL));
	go_setlocale (LC_NUMERIC, "C");
	old_monetary_locale = g_strdup (go_setlocale (LC_MONETARY, NULL));
	go_setlocale (LC_MONETARY, "C");
	go_set_untranslated_bools ();

	outfile = gsf_outfile_zip_new (output, &err);

	state.ioc = ioc;
	state.wbv = wbv;
	state.wb  = wb_view_workbook (wbv);
	for (i = 0 ; i < G_N_ELEMENTS (streams); i++) {
		child = gsf_outfile_new_child  (outfile, streams[i].name, FALSE);
		streams[i].func (&state, child);
		gsf_output_close (child);
		g_object_unref (G_OBJECT (child));
	}

	gsf_output_close (GSF_OUTPUT (outfile));
	g_object_unref (G_OBJECT (outfile));

	/* go_setlocale restores bools to locale translation */
	go_setlocale (LC_MONETARY, old_monetary_locale);
	g_free (old_monetary_locale);
	go_setlocale (LC_NUMERIC, old_num_locale);
	g_free (old_num_locale);
}