xml-io.c 87.3 KB
Newer Older
1
/* vim: set sw=8: */
2 3 4
/*
 * xml-io.c: save/read gnumeric Sheets using an XML encoding.
 *
5 6 7
 * Authors:
 *   Daniel Veillard <Daniel.Veillard@w3.org>
 *   Miguel de Icaza <miguel@gnu.org>
8
 *   Jody Goldberg <jgoldberg@home.com>
9 10
 */
#include <config.h>
Jody Goldberg's avatar
Jody Goldberg committed
11
#include "xml-io.h"
12
#include "style-color.h"
13
#include "style-border.h"
14
#include "style.h"
Jody Goldberg's avatar
Jody Goldberg committed
15 16
#include "sheet.h"
#include "sheet-style.h"
17
#include "sheet-object.h"
18
#include "sheet-object-cell-comment.h"
19
#include "print-info.h"
20
#include "file.h"
21
#include "expr.h"
22
#include "expr-name.h"
23
#include "cell.h"
24
#include "value.h"
25
#include "sheet-merge.h"
26 27 28
#include "io-context.h"
#include "command-context.h"
#include "workbook-control.h"
29
#include "workbook-view.h"
30
#include "workbook.h"
31
#include "selection.h"
32
#include "clipboard.h"
Jody Goldberg's avatar
Jody Goldberg committed
33
#include "format.h"
Jody Goldberg's avatar
Jody Goldberg committed
34
#include "ranges.h"
Chyla Zbigniew's avatar
Chyla Zbigniew committed
35
#include "file.h"
Jody Goldberg's avatar
Jody Goldberg committed
36
#include "str.h"
37
#include "plugin-util.h"
Jody Goldberg's avatar
Jody Goldberg committed
38

Jody Goldberg's avatar
Jody Goldberg committed
39 40 41
#include <gnome-xml/parser.h>
#include <gnome-xml/parserInternals.h>
#include <gnome-xml/xmlmemory.h>
42 43 44
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
45
#include <errno.h>
46
#include <zlib.h>
Jody Goldberg's avatar
Jody Goldberg committed
47
#include <gal/util/e-xml-utils.h>
48
#include <gal/widgets/e-colors.h>
Jody Goldberg's avatar
Jody Goldberg committed
49 50 51 52
#include <gnome.h>
#include <locale.h>
#include <math.h>
#include <limits.h>
53 54 55
#ifdef ENABLE_BONOBO
#include <bonobo/bonobo-exception.h>
#endif
56

57 58 59
/* Precision to use when saving point measures. */
#define POINT_SIZE_PRECISION 3

60
/* FIXME - tune the values below */
61 62
#define XML_INPUT_BUFFER_SIZE      4096
#define N_ELEMENTS_BETWEEN_UPDATES 20
63

64 65
static GnumFileOpener *xml_opener = NULL;
static GnumFileSaver  *xml_saver = NULL;
Chyla Zbigniew's avatar
Chyla Zbigniew committed
66

67
GnumFileOpener *
68
gnumeric_xml_get_opener (void)
Chyla Zbigniew's avatar
Chyla Zbigniew committed
69
{
70
	return xml_opener;
Chyla Zbigniew's avatar
Chyla Zbigniew committed
71 72
}

73
GnumFileSaver *
74
gnumeric_xml_get_saver (void)
Chyla Zbigniew's avatar
Chyla Zbigniew committed
75
{
76
	return xml_saver;
Chyla Zbigniew's avatar
Chyla Zbigniew committed
77 78
}

79 80 81
XmlParseContext *
xml_parse_ctx_new_full (xmlDocPtr             doc,
			xmlNsPtr              ns,
Michael Meeks's avatar
Michael Meeks committed
82
			GnumericXMLVersion    version,
83 84 85 86 87 88
			XmlSheetObjectReadFn  read_fn,
			XmlSheetObjectWriteFn write_fn,
			gpointer              user_data)
{
	XmlParseContext *ctxt = g_new0 (XmlParseContext, 1);

Michael Meeks's avatar
Michael Meeks committed
89
	ctxt->version   = version;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	ctxt->doc       = doc;
	ctxt->ns        = ns;
	ctxt->expr_map  = g_hash_table_new (g_direct_hash, g_direct_equal);

	ctxt->write_fn  = write_fn;
	ctxt->read_fn   = read_fn;
	ctxt->user_data = user_data;

	return ctxt;
}

XmlParseContext *
xml_parse_ctx_new (xmlDocPtr doc,
		   xmlNsPtr  ns)
{
Michael Meeks's avatar
Michael Meeks committed
105
	return xml_parse_ctx_new_full (
Jody Goldberg's avatar
Jody Goldberg committed
106
		doc, ns, GNUM_XML_V6, NULL, NULL, NULL);
107
}
108

109 110 111 112 113 114 115 116
void
xml_parse_ctx_destroy (XmlParseContext *ctxt)
{
	g_return_if_fail (ctxt != NULL);

	g_hash_table_destroy (ctxt->expr_map);
	g_free (ctxt);
}
117

118 119 120
/*
 * Internal stuff: xml helper functions.
 */
121

122 123 124 125
static void
xml_arg_set (GtkArg *arg, gchar *string)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
126 127 128 129 130 131 132
	case GTK_TYPE_CHAR:
		GTK_VALUE_CHAR (*arg) = string[0];
		break;
	case GTK_TYPE_UCHAR:
		GTK_VALUE_UCHAR (*arg) = string[0];
		break;
	case GTK_TYPE_BOOL:
Jody Goldberg's avatar
Jody Goldberg committed
133
		if (!strcmp (string, "TRUE"))
JP Rosevear's avatar
JP Rosevear committed
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
			GTK_VALUE_BOOL (*arg) = TRUE;
		else
			GTK_VALUE_BOOL (*arg) = FALSE;
		break;
	case GTK_TYPE_INT:
		GTK_VALUE_INT (*arg) = atoi (string);
		break;
	case GTK_TYPE_UINT:
		GTK_VALUE_UINT (*arg) = atoi (string);
		break;
	case GTK_TYPE_LONG:
		GTK_VALUE_LONG (*arg) = atol (string);
		break;
	case GTK_TYPE_ULONG:
		GTK_VALUE_ULONG (*arg) = atol (string);
		break;
	case GTK_TYPE_FLOAT:
		GTK_VALUE_FLOAT (*arg) = atof (string);
		break;
	case GTK_TYPE_DOUBLE:
		GTK_VALUE_DOUBLE (*arg) = atof (string);
		break;
	case GTK_TYPE_STRING:
		GTK_VALUE_STRING (*arg) = g_strdup (string);
		break;
159 160 161 162 163 164 165
	}
}

static char *
xml_arg_get (GtkArg *arg)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179
	case GTK_TYPE_CHAR:
		return g_strdup (&GTK_VALUE_CHAR (*arg));
	case GTK_TYPE_UCHAR:
		return g_strdup (&GTK_VALUE_UCHAR (*arg));
	case GTK_TYPE_BOOL:
		if (GTK_VALUE_BOOL (*arg))
			return g_strdup ("TRUE");
		else
			return g_strdup ("FALSE");
	case GTK_TYPE_INT:
		return g_strdup_printf("%i", GTK_VALUE_INT (*arg));
	case GTK_TYPE_UINT:
		return g_strdup_printf("%u", GTK_VALUE_UINT (*arg));
	case GTK_TYPE_LONG:
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
180
		return g_strdup_printf("%li", GTK_VALUE_LONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
181
	case GTK_TYPE_ULONG:
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
182
		return g_strdup_printf("%lu", GTK_VALUE_ULONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
183 184 185 186 187 188
	case GTK_TYPE_FLOAT:
		return g_strdup_printf("%f", GTK_VALUE_FLOAT (*arg));
	case GTK_TYPE_DOUBLE:
		return g_strdup_printf("%f", GTK_VALUE_DOUBLE (*arg));
	case GTK_TYPE_STRING:
		return g_strdup (GTK_VALUE_STRING (*arg));
189 190 191 192
	}

	return NULL;
}
Jody Goldberg's avatar
Jody Goldberg committed
193

194 195 196
/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
197 198
 *
 * Returns a g_malloc'ed string.  Caller must free.
199
 */
200
static char *
201
xml_value_get (xmlNodePtr node, const char *name)
202
{
203
	char *ret, *val;
204 205
	xmlNodePtr child;

206 207 208 209
	val = (char *) xmlGetProp (node, name);
	if (val != NULL) {
		ret = g_strdup (val);
		xmlFree (val);
Morten Welinder's avatar
Morten Welinder committed
210
		return ret;
211
	}
212
	child = node->xmlChildrenNode;
213 214

	while (child != NULL) {
215 216 217 218
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
219 220 221 222 223 224
			val = xmlNodeGetContent(child);
			if (val != NULL) {
				ret = g_strdup (val);
				xmlFree (val);
				return ret;
			}
225
		}
226 227 228 229
		child = child->next;
	}

	return NULL;
230 231
}

232 233 234 235
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
236
String *
237
xml_get_value_string (xmlNodePtr node, const char *name)
238
{
239
	char *val;
240 241
	String *ret;

242 243 244 245 246
	val = xml_value_get (node, name);
	if (val == NULL) return NULL;
        ret = string_get (val);
	g_free (val);
	return ret;
247 248
}

249 250 251 252
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
253
gboolean
254
xml_get_value_int (xmlNodePtr node, const char *name, int *val)
255
{
256
	char *ret;
257
	int i;
258
	int res;
259

260
	ret = xml_value_get (node, name);
261
	if (ret == NULL) return 0;
262
	res = sscanf (ret, "%d", &i);
263 264
	g_free (ret);

265 266
	if (res == 1) {
	        *val = i;
267
		return TRUE;
268
	}
269
	return FALSE;
270 271
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static gboolean
xml_get_prop_cellpos (xmlNodePtr node, char const *name, CellPos *val)
{
	char *str;
	int dummy;
	gboolean res;

	str = (char *) xmlGetProp (node, name);
	if (val == NULL)
		return FALSE;
	res = parse_cell_name (str, &val->col, &val->row, TRUE, &dummy);
	xmlFree (str);
	return res;
}

static void
xml_set_prop_cellpos (xmlNodePtr node, char const *name, CellPos const *val)
{
	xmlSetProp (node, name, cell_pos_name (val));
}

293
#if 0
294 295 296 297
/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
298
static int
299
xml_get_value_float (xmlNodePtr node, const char *name, float *val)
300
{
301 302
	int res;
	char *ret;
303 304
	float f;

305
	ret = xml_value_get (node, name);
306
	if (ret == NULL) return 0;
307
	res = sscanf (ret, "%f", &f);
308 309
	g_free (ret);

310 311
	if (res == 1) {
	        *val = f;
312 313 314
		return 1;
	}
	return 0;
315
}
316
#endif
317 318 319 320 321

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
322
gboolean
323
xml_get_value_double (xmlNodePtr node, const char *name, double *val)
324
{
325 326
	int res;
	char *ret;
327

328
	ret = xml_value_get (node, name);
329 330
	if (ret == NULL)
		return FALSE;
331
	res = sscanf (ret, "%lf", val);
332
	g_free (ret);
333

334
	return (res == 1);
335 336 337 338 339 340
}

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
341 342
void
xml_set_value_cstr (xmlNodePtr node, const char *name, const char *val)
343
{
344
	char *ret;
345 346 347 348
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
349
		xmlFree (ret);
350 351 352
		xmlSetProp (node, name, val);
		return;
	}
353
	child = node->xmlChildrenNode;
354 355 356 357 358 359 360 361
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, val);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, val);
362 363 364 365 366 367
}

/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
368
void
369
xml_set_value_int (xmlNodePtr node, const char *name, int val)
370
{
371
	char *ret;
372
	xmlNodePtr child;
373
	char str[4 * sizeof (int)];
374

375
	sprintf (str, "%d", val);
376 377
	ret = xmlGetProp (node, name);
	if (ret != NULL){
378
		xmlFree (ret);
379 380 381
		xmlSetProp (node, name, str);
		return;
	}
382
	child = node->xmlChildrenNode;
383 384 385 386 387 388 389 390
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
391 392 393 394 395 396
}

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
397 398
void
xml_set_value_double (xmlNodePtr node, const char *name, double val,
399
		     int precision)
400
{
401
	char str[101 + DBL_DIG];
402

403 404
	if (precision < 0 || precision > DBL_DIG)
		precision = DBL_DIG;
405

406
	if (fabs (val) < 1e9 && fabs (val) > 1e-5)
407
		snprintf (str, 100 + DBL_DIG, "%.*g", precision, val);
408 409
	else
		snprintf (str, 100 + DBL_DIG, "%f", val);
410

411
	xmlSetProp (node, name, str);
412 413
}

414 415 416 417 418 419
/*
 * Set a double value for a node with POINT_SIZE_PRECISION digits precision.
 */
static void
xml_set_value_points (xmlNodePtr node, const char *name, double val)
{
420
	xml_set_value_double (node, name, val, POINT_SIZE_PRECISION);
421 422
}

423 424 425 426 427
static void
xml_set_print_unit (xmlNodePtr node, const char *name,
		    const PrintUnit * const pu)
{
	xmlNodePtr  child;
Jody Goldberg's avatar
Jody Goldberg committed
428
	char       *txt = "points";
429
	char       *tstr;
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

	if (pu == NULL || name == NULL)
		return;

	switch (pu->desired_display) {
	case UNIT_POINTS:
		txt = "points";
		break;
	case UNIT_MILLIMETER:
		txt = "mm";
		break;
	case UNIT_CENTIMETER:
		txt = "cm";
		break;
	case UNIT_INCH:
		txt = "in";
		break;
	}

449
	child = xmlNewChild (node, NULL, name, NULL);
450

451
	xml_set_value_points (child, "Points", pu->points);
452 453

	tstr = xmlEncodeEntitiesReentrant (node->doc, txt);
454
	xml_set_value_cstr (child, "PrefUnit", tstr);
455
	if (tstr) xmlFree (tstr);
456 457 458 459 460 461
}

static void
xml_get_print_unit (xmlNodePtr node, PrintUnit * const pu)
{
	char       *txt;
462

463 464 465 466 467 468 469 470
	g_return_if_fail (pu != NULL);
	g_return_if_fail (node != NULL);

	xml_get_value_double (node, "Points", &pu->points);
	txt = xml_value_get  (node, "PrefUnit");
	if (txt) {
		if (!g_strcasecmp (txt, "points"))
			pu->desired_display = UNIT_POINTS;
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
471
		else if (!strcmp (txt, "mm"))
472
			pu->desired_display = UNIT_MILLIMETER;
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
473
		else if (!strcmp (txt, "cm"))
474
			pu->desired_display = UNIT_CENTIMETER;
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
475
		else if (!strcmp (txt, "in"))
476
			pu->desired_display = UNIT_INCH;
477
		g_free (txt);
478 479 480
	}
}

481
static gboolean
482
xml_read_range (xmlNodePtr tree, Range *r)
483
{
484 485 486 487 488 489
	gboolean res =
	    xml_get_value_int (tree, "startCol", &r->start.col) &&
	    xml_get_value_int (tree, "startRow", &r->start.row) &&
	    xml_get_value_int (tree, "endCol",   &r->end.col) &&
	    xml_get_value_int (tree, "endRow",   &r->end.row);

490 491
	/* Older versions of gnumeric had some boundary problems */
	range_ensure_sanity (r);
492 493

	return res;
494 495 496
}

static void
Jody Goldberg's avatar
Jody Goldberg committed
497
xml_write_range (xmlNodePtr tree, Range const *r)
498
{
Jody Goldberg's avatar
Jody Goldberg committed
499 500 501 502 503 504
	g_return_if_fail (range_is_sane (r));

	xml_set_value_int (tree, "startCol", r->start.col);
	xml_set_value_int (tree, "startRow", r->start.row);
	xml_set_value_int (tree, "endCol",   r->end.col);
	xml_set_value_int (tree, "endRow",   r->end.row);
505 506 507
}

static void
508
xml_read_selection_info (XmlParseContext *ctxt, xmlNodePtr tree)
509 510 511
{
	Range r;
	int row, col;
512
	Sheet *sheet = ctxt->sheet;
513
	xmlNodePtr sel, selections = e_xml_get_child_by_name (tree, "Selections");
514

515 516 517
	if (selections == NULL)
		return;

518
	sheet_selection_reset (sheet);
519
	for (sel = selections->xmlChildrenNode; sel; sel = sel->next)
520
		if (xml_read_range (sel, &r))
521 522 523 524
			sheet_selection_add_range (sheet,
						   r.start.col, r.start.row,
						   r.start.col, r.start.row,
						   r.end.col, r.end.row);
525

526 527
	if (xml_get_value_int (selections, "CursorCol", &col) &&
	    xml_get_value_int (selections, "CursorRow", &row))
528
		sheet_set_edit_pos (sheet, col, row);
529 530 531
}

static void
532
xml_write_selection_info (XmlParseContext *ctxt, Sheet const *sheet, xmlNodePtr tree)
533 534 535 536 537 538 539 540
{
	GList *ptr, *copy;
	tree = xmlNewChild (tree, ctxt->ns, "Selections", NULL);

	/* Insert the selections in REVERSE order */
	copy = g_list_copy (sheet->selections);
	ptr = g_list_reverse (copy);
	for (; ptr != NULL ; ptr = ptr->next) {
541
		Range const *r = ptr->data;
542
		xmlNodePtr child = xmlNewChild (tree, ctxt->ns, "Selection", NULL);
543
		xml_write_range (child, r);
544 545 546
	}
	g_list_free (copy);

547 548
	xml_set_value_int (tree, "CursorCol", sheet->edit_pos_real.col);
	xml_set_value_int (tree, "CursorRow", sheet->edit_pos_real.row);
549 550
}

551 552 553 554 555 556 557 558
/*
 * Get a color value for a node either carried as an attibute or as
 * the content of a child.
 *
 * TODO PBM: at parse time one doesn't have yet a widget, so we have
 *           to retrieve the default colormap, but this may be a bad
 *           option ...
 */
559
static int
560
xml_get_color_value (xmlNodePtr node, const char *name, StyleColor **color)
561
{
562
	char *ret;
563 564
	int red, green, blue;

565
	ret = xml_value_get (node, name);
566
	if (ret == NULL) return 0;
567
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
568
		*color = style_color_new (red, green, blue);
569
		g_free (ret);
570 571
		return 1;
	}
572
	g_free (ret);
573
	return 0;
574 575
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
GdkColor *xml_get_value_color (xmlNodePtr node, const char *name)
{
	GdkColor   *color;
	StyleColor *style_color;

	if (xml_get_color_value (node, name, &style_color)) {
		color = g_new0 (GdkColor, 1);
		color->red   = style_color->color.red;
		color->green = style_color->color.green;
		color->blue  = style_color->color.blue;
		e_color_alloc_gdk (color);
		style_color_unref (style_color);
		return (color);
	} else
		return (NULL);
}

593 594 595 596
/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
597 598
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
599
{
600
	char *ret;
601
	xmlNodePtr child;
602
	char str[4 * sizeof (val->color)];
603

604
	sprintf (str, "%X:%X:%X", val->color.red, val->color.green, val->color.blue);
605 606
	ret = xmlGetProp (node, name);
	if (ret != NULL){
607
		xmlFree (ret);
608 609 610
		xmlSetProp (node, name, str);
		return;
	}
611
	child = node->xmlChildrenNode;
612 613 614 615 616 617 618 619
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
620
}
621

622 623 624 625 626 627 628 629 630 631 632 633 634
void
xml_set_value_color (xmlNodePtr node, const char *name, const GdkColor *color)
{
	StyleColor *style_color;

	if (!color)
		return;

	style_color = style_color_new (color->red, color->green, color->blue);
	xml_set_color_value (node, name, style_color);
	style_color_unref (style_color);
}

635 636 637 638
/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
639
 **/
640
#if 0
641 642
static int
style_is_default_fore (StyleColor *color)
643
{
644 645
	if (!color)
		return TRUE;
646

647 648 649 650
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
651 652
}

653 654
static int
style_is_default_back (StyleColor *color)
655
{
656 657
	if (!color)
		return TRUE;
658

659 660 661 662
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
663
}
664
#endif
665 666 667 668

/*
 * Create an XML subtree of doc equivalent to the given StyleBorder.
 */
669

Michael Meeks's avatar
Michael Meeks committed
670
static char *StyleSideNames[6] =
671 672 673 674
{
 	"Top",
 	"Bottom",
 	"Left",
Michael Meeks's avatar
Michael Meeks committed
675 676 677
 	"Right",
	"Diagonal",
	"Rev-Diagonal"
Arturo Espinosa's avatar
Arturo Espinosa committed
678
};
679

680
static xmlNodePtr
681
xml_write_style_border (XmlParseContext *ctxt,
682
			const MStyle *style)
683 684 685
{
	xmlNodePtr cur;
	xmlNodePtr side;
686
	int        i;
687

688
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_DIAGONAL; i++) {
Jody Goldberg's avatar
Jody Goldberg committed
689
		StyleBorder const *border;
690
		if (mstyle_is_element_set (style, i) &&
691
		    NULL != (border = mstyle_get_border (style, i))) {
Arturo Espinosa's avatar
Arturo Espinosa committed
692
			break;
693
		}
694
	}
695
	if (i > MSTYLE_BORDER_DIAGONAL)
Arturo Espinosa's avatar
Arturo Espinosa committed
696
		return NULL;
697

698
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
699

700
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_DIAGONAL; i++) {
Jody Goldberg's avatar
Jody Goldberg committed
701
		StyleBorder const *border;
Jody Goldberg's avatar
Jody Goldberg committed
702
		if (mstyle_is_element_set (style, i) &&
703
		    NULL != (border = mstyle_get_border (style, i))) {
Jody Goldberg's avatar
Jody Goldberg committed
704 705
			StyleBorderType t = border->line_type;
			StyleColor *col   = border->color;
706 707
 			side = xmlNewChild (cur, ctxt->ns,
					    StyleSideNames [i - MSTYLE_BORDER_TOP],
708 709
 					    NULL);
			xml_set_value_int (side, "Style", t);
710 711
			if (t != STYLE_BORDER_NONE)
				xml_set_color_value (side, "Color", col);
712
 		}
713 714
	}
	return cur;
715 716 717 718 719
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
720
static void
721
xml_read_style_border (XmlParseContext *ctxt, xmlNodePtr tree, MStyle *mstyle)
722 723
{
	xmlNodePtr side;
724
	int        i;
725 726 727

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
728
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
729 730
			 tree->name);
	}
731

732
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_DIAGONAL; i++) {
733
 		if ((side = e_xml_get_child_by_name (tree,
734
					      StyleSideNames [i - MSTYLE_BORDER_TOP])) != NULL) {
735 736
			int		 t;
			StyleColor      *color = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
737
			StyleBorder    *border;
738
			xml_get_value_int (side, "Style", &t);
739 740
			if (t != STYLE_BORDER_NONE)
				xml_get_color_value (side, "Color", &color);
741
			border = style_border_fetch ((StyleBorderType)t, color,
Michael Meeks's avatar
Michael Meeks committed
742
						     style_border_get_orientation (i));
743
			mstyle_set_border (mstyle, i, border);
744
 		}
745
	}
746 747 748 749 750
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
751
xmlNodePtr
752
xml_write_style (XmlParseContext *ctxt,
753
		 MStyle *style)
754
{
755
	xmlNodePtr  cur, child;
756
	char       *tstr;
757

758
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
759

760 761 762 763
	if (mstyle_is_element_set (style, MSTYLE_ALIGN_H))
		xml_set_value_int (cur, "HAlign", mstyle_get_align_h (style));
	if (mstyle_is_element_set (style, MSTYLE_ALIGN_V))
		xml_set_value_int (cur, "VAlign", mstyle_get_align_v (style));
764 765
	if (mstyle_is_element_set (style, MSTYLE_WRAP_TEXT))
		xml_set_value_int (cur, "WrapText", mstyle_get_wrap_text (style));
766 767 768 769
	if (mstyle_is_element_set (style, MSTYLE_ORIENTATION))
		xml_set_value_int (cur, "Orient", mstyle_get_orientation (style));
	if (mstyle_is_element_set (style, MSTYLE_PATTERN))
		xml_set_value_int (cur, "Shade", mstyle_get_pattern (style));
Jody Goldberg's avatar
Jody Goldberg committed
770 771
	if (mstyle_is_element_set (style, MSTYLE_INDENT))
		xml_set_value_int (cur, "Indent", mstyle_get_indent (style));
772 773

	if (mstyle_is_element_set (style, MSTYLE_COLOR_FORE)) {
774
/*		if (!style_is_default_fore (mstyle_get_color (style, MSTYLE_COLOR_FORE)))*/
775 776 777
			xml_set_color_value (cur, "Fore", mstyle_get_color (style, MSTYLE_COLOR_FORE));
	}
	if (mstyle_is_element_set (style, MSTYLE_COLOR_BACK)) {
778
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_BACK)))*/
779 780
			xml_set_color_value (cur, "Back", mstyle_get_color (style, MSTYLE_COLOR_BACK));
	}
781
	if (mstyle_is_element_set (style, MSTYLE_COLOR_PATTERN)) {
782
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_PATTERN)))*/
783 784
			xml_set_color_value (cur, "PatternColor", mstyle_get_color (style, MSTYLE_COLOR_PATTERN));
	}
Jody Goldberg's avatar
Jody Goldberg committed
785 786
	if (mstyle_is_element_set (style, MSTYLE_FORMAT)) {
		char *fmt = style_format_as_XL (mstyle_get_format (style), FALSE);
787
		xml_set_value_cstr (cur, "Format", fmt);
Jody Goldberg's avatar
Jody Goldberg committed
788 789
		g_free (fmt);
	}
790 791

	if (mstyle_is_element_set (style, MSTYLE_FONT_NAME) ||
792
	    mstyle_is_element_set (style, MSTYLE_FONT_SIZE) ||
793 794
	    mstyle_is_element_set (style, MSTYLE_FONT_BOLD) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_ITALIC) ||
795 796
	    mstyle_is_element_set (style, MSTYLE_FONT_UNDERLINE) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH)) {
797 798 799 800 801 802
		const char *fontname;

		if (mstyle_is_element_set (style, MSTYLE_FONT_NAME))
			fontname = mstyle_get_font_name (style);
		else /* backwards compatibility */
			fontname = "Helvetica";
803

804 805 806 807
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, fontname);
		child = xmlNewChild (cur, ctxt->ns, "Font", tstr);
		if (tstr) xmlFree (tstr);

808
		if (mstyle_is_element_set (style, MSTYLE_FONT_SIZE))
809
			xml_set_value_points (child, "Unit",
810 811 812 813 814 815 816
					      mstyle_get_font_size (style));
		if (mstyle_is_element_set (style, MSTYLE_FONT_BOLD))
			xml_set_value_int (child, "Bold",
					   mstyle_get_font_bold (style));
		if (mstyle_is_element_set (style, MSTYLE_FONT_ITALIC))
			xml_set_value_int (child, "Italic",
					   mstyle_get_font_italic (style));
Jody Goldberg's avatar
Jody Goldberg committed
817 818 819
		if (mstyle_is_element_set (style, MSTYLE_FONT_UNDERLINE))
			xml_set_value_int (child, "Underline",
					   (int)mstyle_get_font_uline (style));
820 821 822
		if (mstyle_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH))
			xml_set_value_int (child, "StrikeThrough",
					   mstyle_get_font_strike (style));
823 824 825 826 827
	}

	child = xml_write_style_border (ctxt, style);
	if (child)
		xmlAddChild (cur, child);
828 829

	return cur;
830 831
}

832
static xmlNodePtr
833
xml_write_names (XmlParseContext *ctxt, GList *names)
834
{
835
	xmlNodePtr  cur;
836
	char       *tstr;
837

838 839 840 841
#if 0  /* Don't return, We need to have a names node in the worksheet
	   * all the time becasue xml_search_child looks for a node down the
	   * tree and it will find the first "Names" node in the sheet #1
	   */
842 843
	if (!names)
		return NULL;
844
#endif
845 846 847 848 849

	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Names", NULL);

	while (names) {
		xmlNodePtr   tmp;
850
		NamedExpression    *expr_name = names->data;
851 852 853 854 855
		char        *text;

		g_return_val_if_fail (expr_name != NULL, NULL);

		tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Name", NULL);
856 857 858
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, expr_name->name->str);
		xmlNewChild (tmp, ctxt->ns, "name", tstr);
		if (tstr) xmlFree (tstr);
859 860

		text = expr_name_value (expr_name);
861 862 863
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
		xmlNewChild (tmp, ctxt->ns, "value", tstr);
		if (tstr) xmlFree (tstr);
864 865 866 867 868
		g_free (text);

		xmlAddChild (cur, tmp);
		names = g_list_next (names);
	}
869

870 871 872 873
	return cur;
}

static void
874
xml_read_names (XmlParseContext *ctxt, xmlNodePtr tree, Workbook *wb,
875
		Sheet *sheet)
876 877 878 879 880 881
{
	xmlNodePtr child;

	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);

882
	child = tree->xmlChildrenNode;
883
	while (child) {
884
		char *name  = NULL;
885 886 887
		if (child->name && !strcmp (child->name, "Name")) {
			xmlNodePtr bits;

888
			bits = child->xmlChildrenNode;
889
			while (bits) {
890

891 892 893
				if (!strcmp (bits->name, "name")) {
					name = xmlNodeGetContent (bits);
				} else {
894
					char     *txt;
895
					ParseError  perr;
896 897 898 899 900 901
					g_return_if_fail (name != NULL);

					txt = xmlNodeGetContent (bits);
					g_return_if_fail (txt != NULL);
					g_return_if_fail (!strcmp (bits->name, "value"));

902 903 904
					if (!expr_name_create (wb, sheet, name, txt, &perr))
						g_warning (perr.message);
					parse_error_free (&perr);
905

Daniel Veillard's avatar
Daniel Veillard committed
906
					xmlFree (txt);
907
				}
908
				bits = bits->next;
909 910 911 912 913 914
			}
		}
		child = child->next;
	}
}

915
static xmlNodePtr
916
xml_write_summary (XmlParseContext *ctxt, SummaryInfo *summary_info)
917 918
{
	GList *items, *m;
919
	char *tstr;
920 921
	xmlNodePtr cur;

922
	if (!summary_info)
923 924
		return NULL;

925
	m = items = summary_info_as_list (summary_info);
926 927 928 929 930 931 932 933 934 935 936 937 938

	if (!items)
		return NULL;

	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Summary", NULL);

	while (items) {
		xmlNodePtr   tmp;
		SummaryItem *sit = items->data;
		if (sit) {
			char *text;

			tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Item", NULL);
939 940 941
			tstr = xmlEncodeEntitiesReentrant (ctxt->doc, sit->name);
			xmlNewChild (tmp, ctxt->ns, "name", tstr);
			if (tstr) xmlFree (tstr);
942 943 944

			if (sit->type == SUMMARY_INT) {
				text = g_strdup_printf ("%d", sit->v.i);
945 946 947
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-int", tstr);
				if (tstr) xmlFree (tstr);
948 949
			} else {
				text = summary_item_as_text (sit);
950 951 952
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-string", tstr);
				if (tstr) xmlFree (tstr);
953 954
			}
			g_free (text);
955
			xmlAddChild (cur, tmp);
956 957 958 959 960 961 962 963
		}
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
964
xml_read_summary (XmlParseContext *ctxt, xmlNodePtr tree, SummaryInfo *summary_info)
965
{
966
	xmlNodePtr child;
967

968 969 970
	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (summary_info != NULL);
971

972
	child = tree->xmlChildrenNode;
973
	while (child) {
974
		char *name = NULL;
Miguel de Icaza's avatar
Miguel de Icaza committed
975

976 977 978
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

979
			bits = child->xmlChildrenNode;
980 981
			while (bits) {
				SummaryItem *sit = NULL;
982

983
				if (!strcmp (bits->name, "name")) {
984
					name = xmlNodeGetContent (bits);
985 986 987 988 989
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
990 991 992 993
					if (txt != NULL){
						if (!strcmp (bits->name, "val-string"))
							sit = summary_item_new_string (name, txt);
						else if (!strcmp (bits->name, "val-int"))
Miguel de Icaza's avatar
Miguel de Icaza committed
994
							sit = summary_item_new_int (name, atoi (txt));
995

996
						if (sit)
997
							summary_info_add (summary_info, sit);
Daniel Veillard's avatar
Daniel Veillard committed
998
						xmlFree (txt);
999
					}
1000
				}
1001
				bits = bits->next;
1002 1003
			}
		}
Miguel de Icaza's avatar
Miguel de Icaza committed
1004
		if (name){
1005
			xmlFree (name);
Miguel de Icaza's avatar
Miguel de Icaza committed
1006 1007
			name = NULL;
		}
1008 1009 1010 1011
		child = child->next;
	}
}

Michael Meeks's avatar
Michael Meeks committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
static void
xml_set_print_hf (xmlNodePtr node, const char *name,
		  const PrintHF * const hf)
{
	xmlNodePtr  child;

	if (hf == NULL || name == NULL)
		return;

	child = xmlNewChild (node, NULL, name, NULL);