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

39 40 41
XmlParseContext *
xml_parse_ctx_new_full (xmlDocPtr             doc,
			xmlNsPtr              ns,
Michael Meeks's avatar
Michael Meeks committed
42
			GnumericXMLVersion    version,
43 44 45 46 47 48
			XmlSheetObjectReadFn  read_fn,
			XmlSheetObjectWriteFn write_fn,
			gpointer              user_data)
{
	XmlParseContext *ctxt = g_new0 (XmlParseContext, 1);

Michael Meeks's avatar
Michael Meeks committed
49
	ctxt->version   = version;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	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
65 66
	return xml_parse_ctx_new_full (
		doc, ns, GNUM_XML_V5, NULL, NULL, NULL);
67
}
68

69 70 71 72 73 74 75 76
void
xml_parse_ctx_destroy (XmlParseContext *ctxt)
{
	g_return_if_fail (ctxt != NULL);

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

78 79 80
/*
 * Internal stuff: xml helper functions.
 */
81

82 83 84 85
static void
xml_arg_set (GtkArg *arg, gchar *string)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
86 87 88 89 90 91 92
	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
93
		if (!strcmp (string, "TRUE"))
JP Rosevear's avatar
JP Rosevear committed
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
			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;
119 120 121 122 123 124 125
	}
}

static char *
xml_arg_get (GtkArg *arg)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139
	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
140
		return g_strdup_printf("%li", GTK_VALUE_LONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
141
	case GTK_TYPE_ULONG:
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
142
		return g_strdup_printf("%lu", GTK_VALUE_ULONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
143 144 145 146 147 148
	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));
149 150 151 152
	}

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

154 155 156
/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
157 158
 *
 * Returns a g_malloc'ed string.  Caller must free.
159
 */
160
static char *
161
xml_value_get (xmlNodePtr node, const char *name)
162
{
163
	char *ret, *val;
164 165
	xmlNodePtr child;

166 167 168 169
	val = (char *) xmlGetProp (node, name);
	if (val != NULL) {
		ret = g_strdup (val);
		xmlFree (val);
Morten Welinder's avatar
Morten Welinder committed
170
		return ret;
171
	}
172 173 174
	child = node->childs;

	while (child != NULL) {
175 176 177 178
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
179 180 181 182 183 184
			val = xmlNodeGetContent(child);
			if (val != NULL) {
				ret = g_strdup (val);
				xmlFree (val);
				return ret;
			}
185
		}
186 187 188 189
		child = child->next;
	}

	return NULL;
190 191
}

192 193 194 195
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
196
String *
197
xml_get_value_string (xmlNodePtr node, const char *name)
198
{
199
	char *val;
200 201
	String *ret;

202 203 204 205 206
	val = xml_value_get (node, name);
	if (val == NULL) return NULL;
        ret = string_get (val);
	g_free (val);
	return ret;
207 208
}

209 210 211 212
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
213
gboolean
214
xml_get_value_int (xmlNodePtr node, const char *name, int *val)
215
{
216
	char *ret;
217
	int i;
218
	int res;
219

220
	ret = xml_value_get (node, name);
221
	if (ret == NULL) return 0;
222
	res = sscanf (ret, "%d", &i);
223 224
	g_free (ret);

225 226
	if (res == 1) {
	        *val = i;
227
		return TRUE;
228
	}
229
	return FALSE;
230 231
}

232
#if 0
233 234 235 236
/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
237
static int
238
xml_get_value_float (xmlNodePtr node, const char *name, float *val)
239
{
240 241
	int res;
	char *ret;
242 243
	float f;

244
	ret = xml_value_get (node, name);
245
	if (ret == NULL) return 0;
246
	res = sscanf (ret, "%f", &f);
247 248
	g_free (ret);

249 250
	if (res == 1) {
	        *val = f;
251 252 253
		return 1;
	}
	return 0;
254
}
255
#endif
256 257 258 259 260

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
261
static int
262
xml_get_value_double (xmlNodePtr node, const char *name, double *val)
263
{
264 265
	int res;
	char *ret;
266

267
	ret = xml_value_get (node, name);
268
	if (ret == NULL) return 0;
269
	res = sscanf (ret, "%lf", val);
270
	g_free (ret);
271

272
	return (res == 1);
273 274 275 276 277 278
}

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
279 280
void
xml_set_value_cstr (xmlNodePtr node, const char *name, const char *val)
281
{
282
	char *ret;
283 284 285 286
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
287
		xmlFree (ret);
288 289 290 291 292 293 294 295 296 297 298 299
		xmlSetProp (node, name, val);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, val);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, val);
300 301
}

302 303 304 305
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
306
void
307
xml_set_value_string (xmlNodePtr node, const char *name, const String *val)
308
{
309
	char *ret;
310 311 312
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
313
	if (ret != NULL) {
314
		xmlFree (ret);
315 316 317 318 319 320 321 322 323 324 325 326
		xmlSetProp (node, name, val->str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, val->str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, val->str);
327 328
}

329 330 331 332
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
333
void
334
xml_set_value_int (xmlNodePtr node, const char *name, int val)
335
{
336
	char *ret;
337
	xmlNodePtr child;
338
	char str[4 * sizeof (int)];
339

340
	sprintf (str, "%d", val);
341 342
	ret = xmlGetProp (node, name);
	if (ret != NULL){
343
		xmlFree (ret);
344 345 346 347 348 349 350 351 352 353 354 355
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
356 357 358 359 360 361
}

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
362
static void
363
xml_set_value_double (xmlNodePtr node, const char *name, double val)
364
{
365
	char *ret;
366
	xmlNodePtr child;
367
	char str[101 + DBL_DIG];
368

369 370 371 372
	if (fabs (val) < 1e9 && fabs (val) > 1e-5)
		snprintf (str, 100 + DBL_DIG, "%.*g", DBL_DIG, val);
	else
		snprintf (str, 100 + DBL_DIG, "%f", val);
373

374 375
	ret = xmlGetProp (node, name);
	if (ret != NULL){
376
		xmlFree (ret);
377 378 379 380 381 382 383 384 385 386 387 388
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
389 390
}

391 392 393 394 395
static void
xml_set_print_unit (xmlNodePtr node, const char *name,
		    const PrintUnit * const pu)
{
	xmlNodePtr  child;
Jody Goldberg's avatar
Jody Goldberg committed
396
	char       *txt = "points";
397
	char       *tstr;
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

	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;
	}

417 418 419 420
	tstr = xmlEncodeEntitiesReentrant (node->doc, name);
	child = xmlNewChild (node, NULL, "PrintUnit", tstr);
	if (tstr) xmlFree (tstr);

421
	xml_set_value_double (child, "Points", pu->points);
422 423

	tstr = xmlEncodeEntitiesReentrant (node->doc, txt);
424
	xml_set_value_cstr (child, "PrefUnit", tstr);
425
	if (tstr) xmlFree (tstr);
426 427 428 429 430 431
}

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

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	g_return_if_fail (pu != NULL);
	g_return_if_fail (node != NULL);
	g_return_if_fail (node->childs != 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;
		else if (!g_strcasecmp (txt, "mm"))
			pu->desired_display = UNIT_MILLIMETER;
		else if (!g_strcasecmp (txt, "cm"))
			pu->desired_display = UNIT_CENTIMETER;
		else if (!g_strcasecmp (txt, "in"))
			pu->desired_display = UNIT_INCH;
448
		g_free (txt);
449 450 451
	}
}

452
/*
453
 * Search a child by name, if needed go down the tree to find it.
454
 */
455
xmlNodePtr
456
xml_search_child (xmlNodePtr node, const char *name)
457 458 459 460 461 462 463 464 465 466 467 468
{
	xmlNodePtr ret;
	xmlNodePtr child;

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name))
			return child;
		child = child->next;
	}
	child = node->childs;
	while (child != NULL){
469
		ret = xml_search_child (child, name);
470 471 472 473 474
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
475 476
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
/*
 * xml_search_child_lang_list_with_score:
 *
 */
static xmlNodePtr
xml_search_child_lang_list_with_score (xmlNodePtr tree, const gchar *name, GList *lang_list, gint *best_lang_score)
{
	xmlNodePtr best_node = NULL, node;

	for (node = tree->childs; node != NULL; node = node->next) {
		xmlChar *lang;

		if (!node->name || strcmp (node->name, name) != 0) {
			continue;
		}
		lang = xmlGetProp (node, "xml:lang");
		if (lang != NULL) {
			GList *l;
			gint i;

			for (l = lang_list, i = 0; l != NULL && i < *best_lang_score; l = l->next, i++) {
				if (strcmp ((gchar *) l->data, lang) == 0) {
					best_node = node;
					*best_lang_score = i;
				}
			}
		} else {
			if (best_node == NULL) {
				best_node = node;
			}
		}
		xmlFree (lang);
		if (*best_lang_score == 0) {
			return best_node;
		} 
	}

	return best_node;
}

/*
 * xml_search_child_lang_list:
 *
 * Search a child by name.
 */
xmlNodePtr
xml_search_child_lang_list (xmlNodePtr tree, const gchar *name, GList *lang_list)
{
	gint best_lang_score = INT_MAX;

	g_return_val_if_fail (tree != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);

	if (lang_list == NULL) {
		lang_list = gnome_i18n_get_language_list ("LC_MESSAGES");
	}
	return xml_search_child_lang_list_with_score (tree, name, lang_list, &best_lang_score);
}

/*
 * xml_search_child_no_lang:
 *
 * Search a child by name.
 */
xmlNodePtr
xml_search_child_no_lang (xmlNodePtr tree, const gchar *name)
{
	xmlNodePtr node;

	for (node = tree->childs; node != NULL; node = node->next) {
		xmlChar *lang;

		if (!node->name || strcmp (node->name, name) != 0) {
			continue;
		}
		lang = xmlGetProp (node, "xml:lang");
		if (lang == NULL) {
			return node;
		}
		xmlFree (lang);
	}

	return NULL;
}

562
static gboolean
563
xml_read_range (xmlNodePtr tree, Range *r)
564
{
565 566 567 568 569 570 571 572 573 574 575 576
	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);

	/* Add some silent sanity checking to cleanup problems
	 * with older versions of gnumeric that had some boundary problems
	 */
	range_check_sanity (r);

	return res;
577 578 579
}

static void
580
xml_write_range (xmlNodePtr tree, const Range *value)
581 582 583 584 585 586 587 588
{
	xml_set_value_int (tree, "startCol", value->start.col);
	xml_set_value_int (tree, "startRow", value->start.row);
	xml_set_value_int (tree, "endCol",   value->end.col);
	xml_set_value_int (tree, "endRow",   value->end.row);
}

static void
589
xml_read_selection_info (XmlParseContext *ctxt, Sheet *sheet, xmlNodePtr tree)
590 591 592 593 594 595 596 597 598 599
{
	Range r;
	int row, col;
	xmlNodePtr sel, selections = xml_search_child (tree, "Selections");
	if (selections == NULL)
		return;

	sheet_selection_reset_only (sheet);
	for (sel = selections->childs; sel; sel = sel->next) {
		if (xml_read_range (sel, &r))
600 601 602 603
			sheet_selection_add_range (sheet,
						   r.start.col, r.start.row,
						   r.start.col, r.start.row,
						   r.end.col, r.end.row);
604 605 606 607 608 609 610 611
	}

	if (xml_get_value_int (tree, "CursorCol", &col) &&
	    xml_get_value_int (tree, "CursorRow", &row))
		sheet_cursor_set (sheet, col, row, col, row, col, row);
}

static void
612
xml_write_selection_info (XmlParseContext *ctxt, Sheet *sheet, xmlNodePtr tree)
613 614 615 616 617 618 619 620
{
	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) {
621
		Range const *r = ptr->data;
622
		xmlNodePtr child = xmlNewChild (tree, ctxt->ns, "Selection", NULL);
623
		xml_write_range (child, r);
624 625 626
	}
	g_list_free (copy);

627 628
	xml_set_value_int (tree, "CursorCol", sheet->edit_pos.col);
	xml_set_value_int (tree, "CursorRow", sheet->edit_pos.row);
629 630
}

631 632 633 634 635 636 637 638
/*
 * 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 ...
 */
639
static int
640
xml_get_color_value (xmlNodePtr node, const char *name, StyleColor **color)
641
{
642
	char *ret;
643 644
	int red, green, blue;

645
	ret = xml_value_get (node, name);
646
	if (ret == NULL) return 0;
647
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
648
		*color = style_color_new (red, green, blue);
649
		g_free (ret);
650 651
		return 1;
	}
652
	g_free (ret);
653
	return 0;
654 655 656 657 658 659
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
660 661
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
662
{
663
	char *ret;
664
	xmlNodePtr child;
665
	char str[4 * sizeof (val->color)];
666

667
	sprintf (str, "%X:%X:%X", val->color.red, val->color.green, val->color.blue);
668 669
	ret = xmlGetProp (node, name);
	if (ret != NULL){
670
		xmlFree (ret);
671 672 673 674 675 676 677 678 679 680 681 682
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
683
}
684 685 686 687 688

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
689
 **/
690
#if 0
691 692
static int
style_is_default_fore (StyleColor *color)
693
{
694 695
	if (!color)
		return TRUE;
696

697 698 699 700
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
701 702
}

703 704
static int
style_is_default_back (StyleColor *color)
705
{
706 707
	if (!color)
		return TRUE;
708

709 710 711 712
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
713
}
714
#endif
715 716 717 718

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

Michael Meeks's avatar
Michael Meeks committed
720
static char *StyleSideNames[6] =
721 722 723 724
{
 	"Top",
 	"Bottom",
 	"Left",
Michael Meeks's avatar
Michael Meeks committed
725 726 727
 	"Right",
	"Diagonal",
	"Rev-Diagonal"
Arturo Espinosa's avatar
Arturo Espinosa committed
728
};
729

730
static xmlNodePtr
731
xml_write_style_border (XmlParseContext *ctxt,
732
			const MStyle *style)
733 734 735
{
	xmlNodePtr cur;
	xmlNodePtr side;
736
	int        i;
737

738 739 740
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
		MStyleBorder const *border;
		if (mstyle_is_element_set (style, i) &&
741
		    NULL != (border = mstyle_get_border (style, i))) {
Arturo Espinosa's avatar
Arturo Espinosa committed
742
			break;
743
		}
744
	}
745
	if (i > MSTYLE_BORDER_REV_DIAGONAL)
Arturo Espinosa's avatar
Arturo Espinosa committed
746
		return NULL;
747

748
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
749

Michael Meeks's avatar
Michael Meeks committed
750
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
Jody Goldberg's avatar
Jody Goldberg committed
751 752
		MStyleBorder const *border;
		if (mstyle_is_element_set (style, i) &&
753
		    NULL != (border = mstyle_get_border (style, i))) {
Jody Goldberg's avatar
Jody Goldberg committed
754 755
			StyleBorderType t = border->line_type;
			StyleColor *col   = border->color;
756 757
 			side = xmlNewChild (cur, ctxt->ns,
					    StyleSideNames [i - MSTYLE_BORDER_TOP],
758 759
 					    NULL);
			xml_set_value_int (side, "Style", t);
760 761
			if (t != STYLE_BORDER_NONE)
				xml_set_color_value (side, "Color", col);
762
 		}
763 764
	}
	return cur;
765 766 767 768 769
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
770
static void
771
xml_read_style_border (XmlParseContext *ctxt, xmlNodePtr tree, MStyle *mstyle)
772 773
{
	xmlNodePtr side;
774
	int        i;
775 776 777

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
778
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
779 780
			 tree->name);
	}
781

Michael Meeks's avatar
Michael Meeks committed
782
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
783 784
 		if ((side = xml_search_child (tree,
					      StyleSideNames [i - MSTYLE_BORDER_TOP])) != NULL) {
785 786 787 788
			int		 t;
			StyleColor      *color = NULL;
			MStyleBorder    *border;
			xml_get_value_int (side, "Style", &t);
789 790
			if (t != STYLE_BORDER_NONE)
				xml_get_color_value (side, "Color", &color);
791
			border = style_border_fetch ((StyleBorderType)t, color,
Michael Meeks's avatar
Michael Meeks committed
792
						     style_border_get_orientation (i));
793
			mstyle_set_border (mstyle, i, border);
794
 		}
795
	}
796 797 798 799 800
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
801
xmlNodePtr
802
xml_write_style (XmlParseContext *ctxt,
803
		 MStyle *style)
804
{
805
	xmlNodePtr  cur, child;
806
	char       *tstr;
807

808
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
809

810 811 812 813 814 815 816 817 818 819 820 821
	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));
	if (mstyle_is_element_set (style, MSTYLE_FIT_IN_CELL))
		xml_set_value_int (cur, "Fit", mstyle_get_fit_in_cell (style));
	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));

	if (mstyle_is_element_set (style, MSTYLE_COLOR_FORE)) {
822
/*		if (!style_is_default_fore (mstyle_get_color (style, MSTYLE_COLOR_FORE)))*/
823 824 825
			xml_set_color_value (cur, "Fore", mstyle_get_color (style, MSTYLE_COLOR_FORE));
	}
	if (mstyle_is_element_set (style, MSTYLE_COLOR_BACK)) {
826
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_BACK)))*/
827 828
			xml_set_color_value (cur, "Back", mstyle_get_color (style, MSTYLE_COLOR_BACK));
	}
829
	if (mstyle_is_element_set (style, MSTYLE_COLOR_PATTERN)) {
830
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_PATTERN)))*/
831 832
			xml_set_color_value (cur, "PatternColor", mstyle_get_color (style, MSTYLE_COLOR_PATTERN));
	}
Jody Goldberg's avatar
Jody Goldberg committed
833 834
	if (mstyle_is_element_set (style, MSTYLE_FORMAT)) {
		char *fmt = style_format_as_XL (mstyle_get_format (style), FALSE);
835
		xml_set_value_cstr (cur, "Format", fmt);
Jody Goldberg's avatar
Jody Goldberg committed
836 837
		g_free (fmt);
	}
838 839

	if (mstyle_is_element_set (style, MSTYLE_FONT_NAME) ||
840
	    mstyle_is_element_set (style, MSTYLE_FONT_SIZE) ||
841 842
	    mstyle_is_element_set (style, MSTYLE_FONT_BOLD) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_ITALIC) ||
843 844
	    mstyle_is_element_set (style, MSTYLE_FONT_UNDERLINE) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH)) {
845 846 847 848 849 850
		const char *fontname;

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

852 853 854 855
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, fontname);
		child = xmlNewChild (cur, ctxt->ns, "Font", tstr);
		if (tstr) xmlFree (tstr);

856 857 858 859 860 861 862 863 864
		if (mstyle_is_element_set (style, MSTYLE_FONT_SIZE))
			xml_set_value_double (child, "Unit",
					      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
865 866 867
		if (mstyle_is_element_set (style, MSTYLE_FONT_UNDERLINE))
			xml_set_value_int (child, "Underline",
					   (int)mstyle_get_font_uline (style));
868 869 870
		if (mstyle_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH))
			xml_set_value_int (child, "StrikeThrough",
					   mstyle_get_font_strike (style));
871 872 873 874 875
	}

	child = xml_write_style_border (ctxt, style);
	if (child)
		xmlAddChild (cur, child);
876 877

	return cur;
878 879
}

880
static xmlNodePtr
881
xml_write_names (XmlParseContext *ctxt, GList *names)
882
{
883
	xmlNodePtr  cur;
884
	char       *tstr;
885

886 887 888 889
#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
	   */
890 891
	if (!names)
		return NULL;
892
#endif	
893 894 895 896 897

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

	while (names) {
		xmlNodePtr   tmp;
898
		NamedExpression    *expr_name = names->data;
899 900 901 902 903
		char        *text;

		g_return_val_if_fail (expr_name != NULL, NULL);

		tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Name", NULL);
904 905 906
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, expr_name->name->str);
		xmlNewChild (tmp, ctxt->ns, "name", tstr);
		if (tstr) xmlFree (tstr);
907 908

		text = expr_name_value (expr_name);
909 910 911
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
		xmlNewChild (tmp, ctxt->ns, "value", tstr);
		if (tstr) xmlFree (tstr);
912 913 914 915 916
		g_free (text);

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

918 919 920 921
	return cur;
}

static void
922
xml_read_names (XmlParseContext *ctxt, xmlNodePtr tree, Workbook *wb,
923
		Sheet *sheet)
924 925 926 927 928 929 930 931
{
	xmlNodePtr child;

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

	child = tree->childs;
	while (child) {
932
		char *name  = NULL;
933 934 935 936 937
		if (child->name && !strcmp (child->name, "Name")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
938

939 940 941 942 943 944 945 946 947 948 949
				if (!strcmp (bits->name, "name")) {
					name = xmlNodeGetContent (bits);
				} else {
					char     *txt;
					char     *error;
					g_return_if_fail (name != NULL);

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

950
					if (!expr_name_create (wb, sheet, name, txt, &error))
951 952
						g_warning (error);

Daniel Veillard's avatar
Daniel Veillard committed
953
					xmlFree (txt);
954
				}
955
				bits = bits->next;
956 957 958 959 960 961
			}
		}
		child = child->next;
	}
}

962
static xmlNodePtr
963
xml_write_summary (XmlParseContext *ctxt, SummaryInfo *summary_info)
964 965
{
	GList *items, *m;
966
	char *tstr;
967 968
	xmlNodePtr cur;

969
	if (!summary_info)
970 971
		return NULL;

972
	m = items = summary_info_as_list (summary_info);
973 974 975 976 977 978 979 980 981 982 983 984 985

	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);
986 987 988
			tstr = xmlEncodeEntitiesReentrant (ctxt->doc, sit->name);
			xmlNewChild (tmp, ctxt->ns, "name", tstr);
			if (tstr) xmlFree (tstr);
989 990 991

			if (sit->type == SUMMARY_INT) {
				text = g_strdup_printf ("%d", sit->v.i);
992 993 994
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-int", tstr);
				if (tstr) xmlFree (tstr);
995 996
			} else {
				text = summary_item_as_text (sit);
997 998 999
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-string", tstr);
				if (tstr) xmlFree (tstr);
1000 1001
			}
			g_free (text);
1002
			xmlAddChild (cur, tmp);
1003 1004 1005 1006 1007 1008 1009 1010
		}
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
1011
xml_read_summary (XmlParseContext *ctxt, xmlNodePtr tree, SummaryInfo *summary_info)
1012
{
1013
	xmlNodePtr child;
1014

1015 1016 1017
	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (summary_info != NULL);
1018 1019 1020

	child = tree->childs;
	while (child) {
1021
		char *name = NULL;
Miguel de Icaza's avatar
Miguel de Icaza committed
1022

1023 1024 1025 1026 1027 1028
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
				SummaryItem *sit = NULL;
1029

1030
				if (!strcmp (bits->name, "name")) {
1031
					name = xmlNodeGetContent (bits);
1032 1033 1034 1035 1036
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
1037 1038 1039 1040
					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
1041
							sit = summary_item_new_int (name, atoi (txt));
1042

1043
						if (sit)
1044
							summary_info_add (summary_info, sit);
Daniel Veillard's avatar
Daniel Veillard committed
1045
						xmlFree (txt);
1046
					}
1047
				}
1048
				bits = bits->next;
1049 1050
			}
		}
Miguel de Icaza's avatar
Miguel de Icaza committed
1051
		if (name){
1052
			xmlFree (name);
Miguel de Icaza's avatar
Miguel de Icaza committed
1053 1054
			name = NULL;
		}
1055 1056 1057 1058
		child = child->next;
	}
}

Michael Meeks's avatar
Michael Meeks committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
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);
1069 1070 1071
	xml_set_value_cstr (child, "Left", hf->left_format);
	xml_set_value_cstr (child, "Middle", hf->middle_format);
	xml_set_value_cstr (child, "Right", hf->right_format);
Michael Meeks's avatar
Michael Meeks committed
1072 1073 1074
}

static void
Arturo Espinosa's avatar
Arturo Espinosa committed
1075
xml_get_print_hf (xmlNodePtr node, PrintHF *const hf)
Michael Meeks's avatar
Michael Meeks committed
1076 1077
{
	char *txt;
1078

Michael Meeks's avatar
Michael Meeks committed
1079 1080 1081 1082
	g_return_if_fail (hf != NULL);
	g_return_if_fail (node != NULL);

	txt = xml_value_get (node, "Left");
1083
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1084 1085
		if (hf->left_format)
			g_free (hf->left_format);
1086
		hf->left_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1087
	}
1088

Michael Meeks's avatar
Michael Meeks committed
1089
	txt = xml_value_get (node, "Middle");
1090
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1091 1092
		if (hf->middle_format)
			g_free (hf->middle_format);
1093
		hf->middle_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1094
	}
Michael Meeks's avatar
Michael Meeks committed
1095 1096

	txt = xml_value_get (node, "Right");
1097
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1098 1099
		if (hf->right_format)
			g_free (hf->right_format);
1100
		hf->right_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1101
	}
Michael Meeks's avatar
Michael Meeks committed
1102 1103
}

1104
static void
1105
xml_write_attribute (XmlParseContext *ctxt, xmlNodePtr attr, GtkArg *arg)
1106 1107 1108
{
	xmlChar *tstr;
	gchar *str;
Jody Goldberg's avatar
Jody Goldberg committed
1109

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
	switch (arg->type) {
	case GTK_TYPE_CHAR:
	case GTK_TYPE_UCHAR:
	case GTK_TYPE_BOOL:
	case GTK_TYPE_INT:
	case GTK_TYPE_UINT:
	case GTK_TYPE_LONG:
	case GTK_TYPE_ULONG:
	case GTK_TYPE_FLOAT:
	case GTK_TYPE_DOUBLE:
	case GTK_TYPE_STRING:
Jody Goldberg's avatar
Jody Goldberg committed
1121 1122
		str = xml_arg_get (arg);
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, str);
1123 1124 1125 1126 1127 1128
		xmlNewChild (attr, ctxt->ns, "value", tstr);
		if (tstr) {
			xmlFree (tstr);
		}
		g_free (str);
		break;
Jody Goldberg's avatar
Jody Goldberg committed
1129
	}
1130 1131
}

1132
static xmlNodePtr
1133
xml_write_attributes (XmlParseContext *ctxt, guint n_args, GtkArg *args)
1134 1135 1136
{
	xmlNodePtr cur;
	gint i;
Jody Goldberg's avatar
Jody Goldberg committed
1137

1138 1139 1140
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Attributes", NULL);

	for (i=0; i < n_args; args++, i++) {