mrp-old-xml.c 35.8 KB
Newer Older
Maurice van der Pot's avatar
Maurice van der Pot committed
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
Richard Hult's avatar
Richard Hult committed
2
/*
3
 * Copyright (C) 2004-2005 Imendio AB
Richard Hult's avatar
Richard Hult committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * Copyright (C) 2002-2003 CodeFactory AB
 * Copyright (C) 2002-2003 Richard Hult <richard@imendio.com>
 * Copyright (C) 2002 Mikael Hallendal <micke@imendio.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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
Josué Abarca's avatar
Josué Abarca committed
20 21
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Richard Hult's avatar
Richard Hult committed
22 23 24 25 26 27 28 29 30 31
 */

#include <config.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include "mrp-private.h"
#include "mrp-error.h"
32
#include <glib/gi18n.h>
Richard Hult's avatar
Richard Hult committed
33 34 35 36 37 38 39 40 41 42
#include "mrp-task.h"
#include "mrp-resource.h"
#include "mrp-group.h"
#include "mrp-relation.h"
#include "mrp-old-xml.h"

typedef struct {
        xmlDocPtr       doc;

	gint            version;
43

Richard Hult's avatar
Richard Hult committed
44
	MrpProject     *project;
45

Richard Hult's avatar
Richard Hult committed
46 47 48 49 50 51 52 53
	MrpTask        *root_task;
	GList          *resources;
	GList          *groups;
	GList          *assignments;

	mrptime         project_start;

	gint            project_calendar_id;
54

Richard Hult's avatar
Richard Hult committed
55 56 57
	MrpGroup       *default_group;

	gint            last_id;
58

Richard Hult's avatar
Richard Hult committed
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
	GHashTable     *task_hash;
	GHashTable     *resource_hash;
	GHashTable     *group_hash;
	GHashTable     *day_hash;
	GHashTable     *calendar_hash;
	GList          *delayed_relations;
} MrpParser;

typedef struct {
	gint            predecessor_id;
	gint            successor_id;
	gint            lag;
	MrpRelationType type;
} DelayedRelation;

typedef struct {
	xmlNodePtr node;
	gint       id;
} NodeEntry;

static gchar           *old_xml_get_string            (xmlNodePtr   node,
						       const char  *name);
static gint             old_xml_get_int               (xmlNodePtr   node,
						       const char  *name);
static gint             old_xml_get_int_with_default  (xmlNodePtr   node,
						       const char  *name,
						       gint         def);
static mrptime          old_xml_get_date              (xmlNodePtr   node,
						       const char  *name);
static gfloat           old_xml_get_float             (xmlNodePtr   node,
						       const char  *name);
static MrpTaskType      old_xml_get_task_type         (xmlNodePtr   node,
						       const char  *name);
static MrpTaskSched     old_xml_get_task_sched        (xmlNodePtr   node,
						       const char  *name);
94
static xmlNodePtr       old_xml_search_child          (xmlNodePtr   node,
Richard Hult's avatar
Richard Hult committed
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
						       const gchar *name);
static MrpPropertyType
old_xml_property_type_from_string                     (const gchar *str);
static void
old_xml_set_property_from_node                        (MrpProject  *project,
                                                       MrpObject   *object,
                                                       xmlNodePtr   node);

static void
old_xml_read_predecessor (MrpParser  *parser,
			  gint        task_id,
			  xmlNodePtr  tree)
{
	gint             predecessor_id;
	DelayedRelation *relation;
	gchar           *str;
	MrpRelationType  type;

	if (strcmp (tree->name, "predecessor")){
		/*g_warning ("Got %s, expected 'predecessor'.", tree->name);*/
		return;
	}

	predecessor_id = old_xml_get_int (tree, "predecessor-id");
	if (predecessor_id == 0) {
		g_warning ("Invalid predecessor read.");
		return;
	}

	str = old_xml_get_string (tree, "type");
	if (!strcmp (str, "FS")) {
		type = MRP_RELATION_FS;
	}
	else if (!strcmp (str, "FF")) {
		type = MRP_RELATION_FF;
	}
	else if (!strcmp (str, "SS")) {
		type = MRP_RELATION_SS;
	}
	else if (!strcmp (str, "SF")) {
		type = MRP_RELATION_SF;
	}
	else {
		g_warning ("Invalid dependency type.");
		g_free (str);
		return;
	}

	g_free (str);

	relation = g_new0 (DelayedRelation, 1);
	relation->successor_id = task_id;
	relation->predecessor_id = predecessor_id;
	relation->type = type;
	relation->lag = old_xml_get_int (tree, "lag");
150

Richard Hult's avatar
Richard Hult committed
151 152 153 154 155 156 157 158
	parser->delayed_relations = g_list_prepend (parser->delayed_relations,
						    relation);
}

static gboolean
old_xml_read_constraint (xmlNodePtr node, MrpConstraint *constraint)
{
	gchar *str;
159

Richard Hult's avatar
Richard Hult committed
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
	str = old_xml_get_string (node, "type");

	if (str == NULL) {
		g_warning ("Invalid constraint read.");
		return FALSE;
	}

	if (!strcmp (str, "must-start-on")) {
		constraint->type = MRP_CONSTRAINT_MSO;
	} else if (!strcmp (str, "start-no-earlier-than")) {
		constraint->type = MRP_CONSTRAINT_SNET;
	} else if (!strcmp (str, "finish-no-later-than")) {
		constraint->type = MRP_CONSTRAINT_FNLT;
	} else {
		g_warning ("Cant handle constraint '%s'", str);
		g_free (str);

		return FALSE;
	}

	constraint->time = old_xml_get_date (node, "time");

	g_free (str);

	return TRUE;
}

static void
old_xml_read_custom_properties (MrpParser  *parser,
				xmlNodePtr  node,
				MrpObject  *object)
{
	xmlNodePtr child;

	for (child = node->children; child; child = child->next) {
		if (strcmp (child->name, "property")) {
			continue;
		}
198

Richard Hult's avatar
Richard Hult committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		old_xml_set_property_from_node (parser->project,
						object,
						child);
	}
}

static void
old_xml_read_task (MrpParser *parser, xmlNodePtr tree, MrpTask *parent)
{
	xmlNodePtr     tasks, predecessor;
	xmlNodePtr     child;
	gchar          *name;
	gint           id;
	mrptime        start = 0, end = 0;
	MrpTask       *task;
	MrpConstraint  constraint;
	guint          percent_complete = 0;
216
	gint          priority = 0;
Richard Hult's avatar
Richard Hult committed
217 218 219 220 221
	gchar         *note;
	gint           duration, work;
	gboolean       got_constraint = FALSE;
	MrpTaskType    type;
	MrpTaskSched   sched;
222

Richard Hult's avatar
Richard Hult committed
223 224 225 226 227 228 229 230 231
	if (strcmp (tree->name, "task")){
		/*g_warning ("Got %s, expected 'task'.", tree->name);*/
		return;
	}

	name = old_xml_get_string (tree, "name");
	note = old_xml_get_string (tree, "note");
	id = old_xml_get_int (tree, "id");
	percent_complete = old_xml_get_int (tree, "percent-complete");
232
	priority = old_xml_get_int (tree, "priority");
Richard Hult's avatar
Richard Hult committed
233 234
	type = old_xml_get_task_type (tree, "type");
	sched = old_xml_get_task_sched (tree, "scheduling");
235

Richard Hult's avatar
Richard Hult committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249
	if (parser->version == 1) {
		start = old_xml_get_date (tree, "start");
		end = old_xml_get_date (tree, "end");
		duration = end - start;

		if (parser->project_start == -1) {
			parser->project_start = start;
		} else {
			parser->project_start = MIN (parser->project_start, start);
		}

		constraint.type = MRP_CONSTRAINT_MSO;
		constraint.time = start;
		got_constraint = TRUE;
250

Richard Hult's avatar
Richard Hult committed
251 252 253 254 255
		task = g_object_new (MRP_TYPE_TASK,
				     "project", parser->project,
				     "name", name,
				     "duration", duration,
				     "percent_complete", percent_complete,
256
				     "priority", priority,
Richard Hult's avatar
Richard Hult committed
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
				     "note", note,
				     NULL);
	} else {
		/* Use work if available, otherwise use duration. */

		work = old_xml_get_int_with_default (tree, "work", -1);
		duration = old_xml_get_int_with_default (tree, "duration", -1);

		if (work == -1 && duration == -1) {
			g_warning ("The file is not correct, no work and no duration.");
			work = 8*60*60;
			duration = 8*60*60;
		}

		if (work == -1) {
			work = duration;
		}
		if (duration == -1) {
			duration = work;
		}

		if (type == MRP_TASK_TYPE_MILESTONE) {
			work = 0;
			duration = 0;
		}
282

Richard Hult's avatar
Richard Hult committed
283 284 285 286 287 288 289 290
		task = g_object_new (MRP_TYPE_TASK,
				     "project", parser->project,
				     "name", name,
				     "sched", sched,
				     "type", type,
				     "work", work,
				     "duration", duration,
				     "percent_complete", percent_complete,
291
				     "priority", priority,
Richard Hult's avatar
Richard Hult committed
292 293 294
				     "note", note,
				     NULL);
	}
295

Richard Hult's avatar
Richard Hult committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	g_free (name);
	g_free (note);

	/* Note: We should use mrp_project_insert_task here instead of the
	 * private imrp_task_insert_child, and not set the "project" property
	 * above. But then the project will emit signals to the views etc,
	 * maybe we should have freeze/thaw on the project or something.
	 */
	imrp_task_insert_child (parent, -1, task);

	/* Treat duration from old files as work. */
	if (parser->version == 1) {
		gint work;

		work = mrp_project_calculate_task_work (parser->project,
							task,
							start, end);
313

Richard Hult's avatar
Richard Hult committed
314 315 316 317
		g_object_set (task,
			      "work", work,
			      NULL);
	}
318

Richard Hult's avatar
Richard Hult committed
319 320 321 322 323 324 325
	g_hash_table_insert (parser->task_hash, GINT_TO_POINTER (id), task);

	for (child = tree->children; child; child = child->next) {
			if (!strcmp (child->name, "properties")) {
				old_xml_read_custom_properties (parser, child, MRP_OBJECT (task));
			}
		}
326

Richard Hult's avatar
Richard Hult committed
327 328
	for (tasks = tree->children; tasks; tasks = tasks->next) {
		if (!strcmp (tasks->name, "task")) {
329 330 331 332 333 334 335
			/* Silently correct old milestones with children to
			 * normal tasks.
			 */
			if (type == MRP_TASK_TYPE_MILESTONE) {
				type = MRP_TASK_TYPE_NORMAL;
				g_object_set (task, "type", type, NULL);
			}
336

Richard Hult's avatar
Richard Hult committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
			old_xml_read_task (parser, tasks, task);
		}
		else if (!strcmp (tasks->name, "predecessors")) {
			for (predecessor = tasks->children; predecessor; predecessor = predecessor->next) {
				old_xml_read_predecessor (parser, id, predecessor);
			}
		}
		else if (!strcmp (tasks->name, "constraint")) {
			got_constraint = old_xml_read_constraint (tasks, &constraint);
		}
	}

	if (got_constraint) {
		g_object_set (task,
			      "constraint", &constraint,
			      NULL);
	}
}

static void
old_xml_read_resource (MrpParser *parser, xmlNodePtr tree)
{
	xmlNodePtr   child;
	gint         id;
	gint         type;
362
	gchar       *name, *short_name, *email;
363
	gchar       *note;
Richard Hult's avatar
Richard Hult committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	gint         gid;
	gint         units;
	gfloat       std_rate; /*, ovt_rate;*/
	gint         calendar_id;
	MrpResource *resource;
	MrpGroup    *group;
	MrpCalendar *calendar;

	if (strcmp (tree->name, "resource")){
		/*g_warning ("Got %s, expected 'resource'.", tree->name);*/
		return;
	}

	id          = old_xml_get_int (tree, "id");
	name        = old_xml_get_string (tree, "name");
379
	short_name  = old_xml_get_string (tree, "short-name");
Richard Hult's avatar
Richard Hult committed
380 381 382 383 384 385 386
	gid         = old_xml_get_int (tree, "group");
	type        = old_xml_get_int (tree, "type");
	units       = old_xml_get_int (tree, "units");
	std_rate    = old_xml_get_float (tree, "std-rate");
	/*ovt_rate = old_xml_get_float (tree, "ovt-rate");*/
	email       = old_xml_get_string (tree, "email");
	calendar_id = old_xml_get_int (tree, "calendar");
387
        note        = old_xml_get_string (tree, "note");
Richard Hult's avatar
Richard Hult committed
388

389 390 391
	if (short_name == NULL) {
		short_name = g_strdup ("");
	}
392

Richard Hult's avatar
Richard Hult committed
393 394 395 396
	if (email == NULL) {
		email = g_strdup ("");
	}

397 398 399 400
	if (note == NULL) {
		note = g_strdup ("");
	}

Richard Hult's avatar
Richard Hult committed
401 402
	group = g_hash_table_lookup (parser->group_hash, GINT_TO_POINTER (gid));
	calendar = g_hash_table_lookup (parser->calendar_hash, GINT_TO_POINTER (calendar_id));
403

Richard Hult's avatar
Richard Hult committed
404 405
	resource = g_object_new (MRP_TYPE_RESOURCE,
				 "name", name,
406
				 "short_name", short_name,
Richard Hult's avatar
Richard Hult committed
407 408 409 410 411
				 "type", type,
				 "group", group,
				 "units", units,
				 "email", email,
				 "calendar", calendar,
412
				 "note", note,
Richard Hult's avatar
Richard Hult committed
413 414 415 416 417
				 NULL);

	/* These are cost custom properties */
	/* We need first to register the resource in the project */
	mrp_project_add_resource (parser->project, resource);
418

Richard Hult's avatar
Richard Hult committed
419 420 421 422 423 424 425 426 427 428 429
	mrp_object_set (MRP_OBJECT (resource),
			"cost", std_rate,
			/*"cost_overtime", ovt_rate,*/
			NULL);

	for (child = tree->children; child; child = child->next) {
		if (!strcmp (child->name, "properties")) {
			old_xml_read_custom_properties (parser, child, MRP_OBJECT (resource));
		}
	}

430
	g_hash_table_insert (parser->resource_hash,
Richard Hult's avatar
Richard Hult committed
431 432 433
			     GINT_TO_POINTER (id), resource);

	parser->resources = g_list_prepend (parser->resources, resource);
434

Richard Hult's avatar
Richard Hult committed
435 436
	g_free (name);
	g_free (email);
437
	g_free (short_name);
438
	g_free (note);
Richard Hult's avatar
Richard Hult committed
439 440 441 442 443 444 445 446 447
}

static void
old_xml_read_group (MrpParser *parser, xmlNodePtr tree)
{
	gint      id;
	gchar    *name;
	gchar    *mgr_name, *mgr_phone, *mgr_email;
	MrpGroup *group;
448

Richard Hult's avatar
Richard Hult committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	if (strcmp (tree->name, "group")){
		/*g_warning ("Got %s, expected 'group'.", tree->name);*/
		return;
	}

	id = old_xml_get_int (tree, "id");

	name = old_xml_get_string (tree, "name");
	mgr_name  = old_xml_get_string (tree, "admin-name");
	mgr_phone = old_xml_get_string (tree, "admin-phone");
	mgr_email = old_xml_get_string (tree, "admin-email");

	group = g_object_new (MRP_TYPE_GROUP,
			      "name", name,
			      "manager_name", mgr_name,
			      "manager_phone", mgr_phone,
			      "manager_email", mgr_email,
			      NULL);

	g_free (name);
	g_free (mgr_name);
	g_free (mgr_phone);
	g_free (mgr_email);
472

Richard Hult's avatar
Richard Hult committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	g_hash_table_insert (parser->group_hash, GINT_TO_POINTER (id), group);

	parser->groups = g_list_prepend (parser->groups, group);
}

static void
old_xml_read_assignment (MrpParser *parser, xmlNodePtr tree)
{
	gint   task_id, resource_id, assigned_units;
	MrpAssignment *assignment;
	MrpTask       *task;
	MrpResource   *resource;

	if (strcmp (tree->name, "allocation")){
		/*g_warning ("Got %s, expected 'allocation'.", tree->name);*/
		return;
	}

	task_id = old_xml_get_int (tree, "task-id");
	resource_id = old_xml_get_int (tree, "resource-id");
	assigned_units = old_xml_get_int_with_default (tree, "units",100);
494 495

	task = g_hash_table_lookup (parser->task_hash,
Richard Hult's avatar
Richard Hult committed
496 497 498
				    GINT_TO_POINTER (task_id));
	resource = g_hash_table_lookup (parser->resource_hash,
					GINT_TO_POINTER (resource_id));
499

Richard Hult's avatar
Richard Hult committed
500
	if (!task) {
Richard Hult's avatar
Richard Hult committed
501 502
		g_warning ("Corrupt file? Task %d not found in hash.", task_id);
		goto fail;
Richard Hult's avatar
Richard Hult committed
503 504 505
	}

	if (!resource) {
Richard Hult's avatar
Richard Hult committed
506 507
		g_warning ("Corrupt file? Resource %d not found in hash.", resource_id);
		goto fail;
Richard Hult's avatar
Richard Hult committed
508 509 510 511 512 513 514 515 516
	}

	assignment = g_object_new (MRP_TYPE_ASSIGNMENT,
				   "task", task,
				   "resource", resource,
				   "units", assigned_units,
				   NULL);

	parser->assignments = g_list_prepend (parser->assignments, assignment);
Richard Hult's avatar
Richard Hult committed
517 518 519

 fail:
	; /* Avoid gcc warning. */
Richard Hult's avatar
Richard Hult committed
520 521 522 523 524 525 526 527
}

static void
old_xml_read_day_type (MrpParser *parser, xmlNodePtr tree)
{
	MrpDay  *day;
	gint     id;
	xmlChar *name, *desc;
528

Richard Hult's avatar
Richard Hult committed
529 530 531 532
	id = old_xml_get_int (tree, "id");
	if (id == MRP_DAY_WORK || id == MRP_DAY_NONWORK || id == MRP_DAY_USE_BASE) {
		return;
	}
533

Richard Hult's avatar
Richard Hult committed
534 535 536
	if (strcmp (tree->name, "day-type") != 0){
		return;
	}
537

Richard Hult's avatar
Richard Hult committed
538
	name = xmlGetProp (tree, "name");
539
	if (!name) {
Richard Hult's avatar
Richard Hult committed
540 541
		return;
	}
542

Richard Hult's avatar
Richard Hult committed
543 544 545 546 547 548 549
	desc = xmlGetProp (tree, "description");
	if (!desc) {
		xmlFree (name);
		return;
	}

	day = mrp_day_add (parser->project, name, desc);
550

Richard Hult's avatar
Richard Hult committed
551 552
	xmlFree (name);
	xmlFree (desc);
553

Richard Hult's avatar
Richard Hult committed
554 555 556 557
	g_hash_table_insert (parser->day_hash, GINT_TO_POINTER (id), day);
}

static void
558
old_xml_read_default_day (MrpParser   *parser,
Richard Hult's avatar
Richard Hult committed
559 560 561 562 563 564 565
			  xmlNodePtr   node,
			  MrpCalendar *calendar,
			  gint         day_id,
			  const gchar *day_name)
{
	gint    id;
	MrpDay *day;
566

Richard Hult's avatar
Richard Hult committed
567
	id = old_xml_get_int (node, day_name);
568
	day = g_hash_table_lookup (parser->day_hash,
Richard Hult's avatar
Richard Hult committed
569 570 571 572 573 574
				   GINT_TO_POINTER (id));
	mrp_calendar_set_default_days (calendar, day_id, day, -1);
}

static void
old_xml_read_overridden_day_type (MrpParser   *parser,
575
				  MrpCalendar *calendar,
Richard Hult's avatar
Richard Hult committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
				  xmlNodePtr   day)
{
	gint        id;
	MrpDay     *mrp_day;
	GList      *intervals = NULL;
	xmlNodePtr  child;

	if (strcmp (day->name, "overridden-day-type") != 0){
		return;
	}

	id = old_xml_get_int (day, "id");
	mrp_day = g_hash_table_lookup (parser->day_hash, GINT_TO_POINTER (id));

	for (child = day->children; child; child = child->next) {
		if (strcmp (child->name, "interval") == 0) {
			MrpInterval *interval;
			gchar       *str;
			gint         hour, min;
			mrptime      start, end;

			str = old_xml_get_string (child, "start");
			if (sscanf (str, "%02d%02d", &hour, &min) != 2) {
				g_free (str);
				continue;
			}
			start = hour * 60 * 60 + min * 60;

			str = old_xml_get_string (child, "end");
			if (sscanf (str, "%02d%02d", &hour, &min) != 2) {
				g_free (str);
				continue;
			}
			end = hour * 60 * 60 + min * 60;

			interval = mrp_interval_new (start, end);
			intervals = g_list_append (intervals, interval);
		}
	}

	mrp_calendar_day_set_intervals (calendar, mrp_day, intervals);
617

Richard Hult's avatar
Richard Hult committed
618 619 620 621
	g_list_foreach (intervals, (GFunc) mrp_interval_unref, NULL);
	g_list_free (intervals);
}

622
static void
Richard Hult's avatar
Richard Hult committed
623 624 625 626 627 628 629 630 631
old_xml_read_overridden_day (MrpParser   *parser,
			     MrpCalendar *calendar,
			     xmlNodePtr   day)
{
	gint     id;
	mrptime  date;
	xmlChar *xml_str;
	MrpDay  *mrp_day;
	gint     y, m, d;
632

Richard Hult's avatar
Richard Hult committed
633 634 635
	if (strcmp (day->name, "day") != 0){
		return;
	}
636

Richard Hult's avatar
Richard Hult committed
637 638 639
	xml_str = xmlGetProp (day, "type");
	if (!xml_str) {
		return;
640
	}
Richard Hult's avatar
Richard Hult committed
641 642 643 644 645
	if (strcmp (xml_str, "day-type") != 0) {
		xmlFree (xml_str);
		return;
	}
	xmlFree (xml_str);
646

Richard Hult's avatar
Richard Hult committed
647
	id = old_xml_get_int (day, "id");
648

Richard Hult's avatar
Richard Hult committed
649 650 651 652 653
	mrp_day = g_hash_table_lookup (parser->day_hash, GINT_TO_POINTER (id));

	xml_str = xmlGetProp (day, "date");
	if (!xml_str) {
		return;
654 655
	}

Richard Hult's avatar
Richard Hult committed
656 657
	if (sscanf (xml_str, "%04d%02d%02d", &y, &m, &d) == 3) {
		date = mrp_time_compose (y, m, d, 0, 0, 0);
658
		mrp_calendar_set_days (calendar, date, mrp_day, (mrptime) -1);
Richard Hult's avatar
Richard Hult committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672
	} else {
		g_warning ("Invalid time format for overridden day.");
	}

	xmlFree (xml_str);
}

static void
old_xml_read_calendar (MrpParser *parser, MrpCalendar *parent, xmlNodePtr tree)
{
	MrpCalendar *calendar = NULL;
	xmlChar     *name;
	xmlNodePtr   child;
	gint         id;
673

Richard Hult's avatar
Richard Hult committed
674 675 676
	if (strcmp (tree->name, "calendar") != 0){
		return;
	}
677

Richard Hult's avatar
Richard Hult committed
678
	name = xmlGetProp (tree, "name");
679
	if (!name) {
Richard Hult's avatar
Richard Hult committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
		return;
	}

	if (parent) {
		calendar = mrp_calendar_derive (name, parent);
	} else {
		calendar = mrp_calendar_new (name, parser->project);
	}

 	xmlFree (name);

	id = old_xml_get_int (tree, "id");
	g_hash_table_insert (parser->calendar_hash,
			     GINT_TO_POINTER (id),
			     calendar);

	for (child = tree->children; child; child = child->next) {
		if (strcmp (child->name, "calendar") == 0) {
			old_xml_read_calendar (parser, calendar, child);
		}
		else if (strcmp (child->name, "default-week") == 0) {
			old_xml_read_default_day (parser, child, calendar,
						  MRP_CALENDAR_DAY_MON, "mon");
			old_xml_read_default_day (parser, child, calendar,
						  MRP_CALENDAR_DAY_TUE, "tue");
			old_xml_read_default_day (parser, child, calendar,
						  MRP_CALENDAR_DAY_WED, "wed");
			old_xml_read_default_day (parser, child, calendar,
						  MRP_CALENDAR_DAY_THU, "thu");
			old_xml_read_default_day (parser, child, calendar,
						  MRP_CALENDAR_DAY_FRI, "fri");
			old_xml_read_default_day (parser, child, calendar,
						  MRP_CALENDAR_DAY_SAT, "sat");
			old_xml_read_default_day (parser, child, calendar,
						  MRP_CALENDAR_DAY_SUN, "sun");
		}
		else if (strcmp (child->name, "overridden-day-types") == 0) {
			xmlNodePtr day;
718

Richard Hult's avatar
Richard Hult committed
719 720
			for (day = child->children; day; day = day->next) {
				old_xml_read_overridden_day_type (parser,
721
								  calendar,
Richard Hult's avatar
Richard Hult committed
722 723 724 725 726 727 728 729
								  day);
			}
		}
		else if (strcmp (child->name, "days") == 0) {
			xmlNodePtr day;

			for (day = child->children; day; day = day->next) {
				old_xml_read_overridden_day (parser,
730
							     calendar,
Richard Hult's avatar
Richard Hult committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
							     day);
			}
		}
	}
}

static void
old_xml_read_project_properties (MrpParser *parser)
{
	xmlNodePtr  node;
	gchar      *name;
	gchar      *org;
	gchar      *manager;
	gchar      *phase;

	node = parser->doc->children;

	parser->version = old_xml_get_int_with_default (node,
							"mrproject-version",
							1);

	if (parser->version > 1) {
		parser->project_start = old_xml_get_date (node,
							  "project-start");
	}
756

Richard Hult's avatar
Richard Hult committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
	name = old_xml_get_string (node, "name");
	org = old_xml_get_string (node, "company");
	manager = old_xml_get_string (node, "manager");
	phase = old_xml_get_string (node, "phase");

	parser->project_calendar_id = old_xml_get_int_with_default (node, "calendar", 0);

	g_object_set (parser->project,
		      "name", name,
		      "organization", org,
		      "manager", manager,
		      "phase", phase,
		      NULL);

	g_free (name);
	g_free (org);
	g_free (manager);
	g_free (phase);
}

static GValueArray *
old_xml_read_string_list (xmlNodePtr  node,
			  MrpObject  *object)
{
	GValueArray *array;
	GValue       value = { 0 };
	xmlNodePtr   child;
	gchar       *str;

	if (!node->children) {
		return NULL;
	}
789

Richard Hult's avatar
Richard Hult committed
790
	array = g_value_array_new (0);
791

Richard Hult's avatar
Richard Hult committed
792 793
	g_value_init (&value, G_TYPE_STRING);

794

Richard Hult's avatar
Richard Hult committed
795 796 797 798 799 800 801 802
	for (child = node->children; child; child = child->next) {
		if (!strcmp (child->name, "list-item")) {
			str = old_xml_get_string (child, "value");

			if (str && str[0]) {
				g_value_set_string (&value, str);
				g_value_array_append (array, &value);
			}
803

Richard Hult's avatar
Richard Hult committed
804 805 806 807 808
			g_free (str);
		}
	}

	g_value_unset (&value);
809

Richard Hult's avatar
Richard Hult committed
810 811 812 813 814
	return array;
}

/* Note: this is a hack to read phases as custom properties from files created
 * with 0.8pre, 0.8, 0.9pre.
815
 */
Richard Hult's avatar
Richard Hult committed
816 817 818 819 820 821 822 823 824 825
static GList *
old_xml_read_crufty_phases (xmlNodePtr  node)
{
	xmlNodePtr  child;
	gchar      *str;
	GList      *phases = NULL;

	if (!node->children) {
		return NULL;
	}
826

Richard Hult's avatar
Richard Hult committed
827 828 829 830 831 832 833 834 835 836 837
	for (child = node->children; child; child = child->next) {
		if (!strcmp (child->name, "list-item")) {
			str = old_xml_get_string (child, "value");

			if (str && str[0]) {
				phases = g_list_prepend (phases, str);
			}
		}
	}

	phases = g_list_reverse (phases);
838

Richard Hult's avatar
Richard Hult committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	return phases;
}

static void
old_xml_read_property_specs (MrpParser *parser)
{
	xmlNodePtr       node;
	xmlNodePtr       child;
	gchar           *name;
	gchar           *label;
	gchar           *description;
	gchar           *owner_str;
	gchar           *type_str;
	MrpProperty     *property;
	MrpPropertyType  type;
	GType            owner;
855 856

	node = old_xml_search_child (parser->doc->children, "properties");
Richard Hult's avatar
Richard Hult committed
857 858 859 860

	if (node == NULL) {
		return;
	}
861

Richard Hult's avatar
Richard Hult committed
862 863 864 865
	for (child = node->children; child; child = child->next) {
		if (strcmp (child->name, "property")) {
			continue;
		}
866

Richard Hult's avatar
Richard Hult committed
867 868 869 870 871 872
		name = old_xml_get_string (child, "name");

		if (!strcmp (name, "phases") || !strcmp (name, "phase")) {
			g_free (name);
			continue;
		}
873

Richard Hult's avatar
Richard Hult committed
874 875 876 877 878 879
		label       = old_xml_get_string (child, "label");
		description = old_xml_get_string (child, "description");
		owner_str   = old_xml_get_string (child, "owner");
		type_str    = old_xml_get_string (child, "type");

		type = old_xml_property_type_from_string (type_str);
880

Richard Hult's avatar
Richard Hult committed
881 882 883 884 885 886
		property = mrp_property_new (name,
					     type,
					     label,
					     description,
					     TRUE);

887 888 889 890 891 892 893 894 895 896 897
		/* If we could't create the property, this means that we are
		 * trying to use a name that is already taken.
		 */
		if (!property) {
			g_free (name);
			g_free (type_str);
			g_free (owner_str);
			g_free (label);
			g_free (description);
			continue;
		}
898

Richard Hult's avatar
Richard Hult committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
		if (!strcmp (owner_str, "task")) {
			owner = MRP_TYPE_TASK;
		}
		else if (!strcmp (owner_str, "resource")) {
			owner = MRP_TYPE_RESOURCE;
		}
		else if (!strcmp (owner_str, "project")) {
			owner = MRP_TYPE_PROJECT;
		} else {
			g_warning ("Invalid owners %s.", owner_str);
			continue;
		}

		/* Check if the property already exists. I'm not sure this is
		 * the best solution, but a quick solution is needed to get
		 * project phases working now.
		 */
		if (!mrp_project_has_property (parser->project, owner, name)) {
			mrp_project_add_property (parser->project,
						  owner,
						  property,
920
						  TRUE /* FIXME: user_defined, should
Richard Hult's avatar
Richard Hult committed
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
							  be read from the file */);
		}

		g_free (name);
		g_free (type_str);
		g_free (owner_str);
		g_free (label);
		g_free (description);
	}
}

static void
old_xml_read_phases (MrpParser *parser)
{
	xmlNodePtr  node;
	xmlNodePtr  child;
	GList      *phases = NULL;
	gchar      *name;
939 940

	node = old_xml_search_child (parser->doc->children, "phases");
Richard Hult's avatar
Richard Hult committed
941 942 943
	if (node == NULL) {
		return;
	}
944

Richard Hult's avatar
Richard Hult committed
945 946 947 948
	for (child = node->children; child; child = child->next) {
		if (strcmp (child->name, "phase")) {
			continue;
		}
949

Richard Hult's avatar
Richard Hult committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
		name = old_xml_get_string (child, "name");
		phases = g_list_prepend (phases, name);
	}

	phases = g_list_reverse (phases);
	g_object_set (parser->project, "phases", phases, NULL);
	mrp_string_list_free (phases);
}

static gboolean
old_xml_read_project (MrpParser *parser)
{
	xmlNodePtr   child;
	xmlNodePtr   tasks, resources, groups, assignments, calendars;
	gint         gid;
	MrpCalendar *calendar;
966

Richard Hult's avatar
Richard Hult committed
967 968 969 970 971
	old_xml_read_project_properties (parser);

	parser->root_task = mrp_task_new ();

	child = parser->doc->children;
972

Richard Hult's avatar
Richard Hult committed
973
	child = child->children;
974

Richard Hult's avatar
Richard Hult committed
975 976 977 978
	/* Get the first "properties" node, i.e. specs. */
	while (child) {
		if (!strcmp (child->name, "properties")) {
			old_xml_read_property_specs (parser);
979

Richard Hult's avatar
Richard Hult committed
980 981 982
			child = child->next;
			break;
		}
983

Richard Hult's avatar
Richard Hult committed
984 985
		child = child->next;
	}
986

Richard Hult's avatar
Richard Hult committed
987 988 989 990 991 992 993 994
	/* Get the second "properties" node, i.e. values. */
	while (child) {
		if (!strcmp (child->name, "properties")) {
			old_xml_read_custom_properties (parser,
							child,
							MRP_OBJECT (parser->project));
			break;
		}
995

Richard Hult's avatar
Richard Hult committed
996 997 998 999
		child = child->next;
	}

	old_xml_read_phases (parser);
1000

Richard Hult's avatar
Richard Hult committed
1001 1002 1003 1004 1005 1006 1007 1008 1009
	/* Load calendars */
	calendars = old_xml_search_child (parser->doc->children, "calendars");
	if (calendars != NULL) {
		xmlNodePtr day;

 		child = old_xml_search_child (calendars, "day-types");

		/* Insert hardcoded days. */
		g_hash_table_insert (parser->day_hash,
1010
				     GINT_TO_POINTER (MRP_DAY_WORK),
Richard Hult's avatar
Richard Hult committed
1011 1012 1013 1014 1015 1016 1017
				     mrp_day_ref (mrp_day_get_work ()));
		g_hash_table_insert (parser->day_hash,
				     GINT_TO_POINTER (MRP_DAY_NONWORK),
				     mrp_day_ref (mrp_day_get_nonwork ()));
		g_hash_table_insert (parser->day_hash,
				     GINT_TO_POINTER (MRP_DAY_USE_BASE),
				     mrp_day_ref (mrp_day_get_use_base ()));
1018

Maurice van der Pot's avatar
Maurice van der Pot committed
1019 1020 1021 1022 1023
		if (child != NULL) {
		        /* Read day-types */
        		for (day = child->children; day; day = day->next) {
        			old_xml_read_day_type (parser, day);
        		}
Richard Hult's avatar
Richard Hult committed
1024
		}
1025

Richard Hult's avatar
Richard Hult committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
		for (child = calendars->children; child; child = child->next) {
			if (strcmp (child->name, "calendar") == 0) {
				old_xml_read_calendar (parser, NULL, child);
			}
		}
	}

	/* Set project calendar now that we have calendars. */
	if (parser->project_calendar_id) {
		calendar = g_hash_table_lookup (parser->calendar_hash,
						GINT_TO_POINTER (parser->project_calendar_id));

		g_object_set (parser->project, "calendar", calendar, NULL);
	}
1040

Richard Hult's avatar
Richard Hult committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	/* Load tasks. */
	child = old_xml_search_child (parser->doc->children, "tasks");
	if (child != NULL) {
		for (tasks = child->children; tasks; tasks = tasks->next) {
			old_xml_read_task (parser, tasks, parser->root_task);
		}
	}

	/* Note: if we read a G1 file, there was no project-start set, so make
	 * sure the one we faked from the earliest task starts on at 00:00.
	 */
	if (parser->version == 1) {
		mrp_time_align_day (parser->project_start);
	}
1055

Richard Hult's avatar
Richard Hult committed
1056
	/* Load resource groups. */
1057 1058 1059
	child = old_xml_search_child (parser->doc->children,
				      "resource-groups");
	if (child != NULL) {
Richard Hult's avatar
Richard Hult committed
1060
		for (groups = child->children; groups; groups = groups->next) {
1061
			old_xml_read_group (parser, groups);
Richard Hult's avatar
Richard Hult committed
1062
		}
1063 1064 1065 1066 1067

		/* Gah, why underscore?! */
		gid = old_xml_get_int (child, "default_group");
		parser->default_group = g_hash_table_lookup (parser->group_hash,
							     GINT_TO_POINTER (gid));
Richard Hult's avatar
Richard Hult committed
1068
	}
1069

Richard Hult's avatar
Richard Hult committed
1070
	/* Load resources. */
1071
	child = old_xml_search_child (parser->doc->children, "resources");
Richard Hult's avatar
Richard Hult committed
1072 1073
	if (child != NULL) {
		for (resources = child->children; resources; resources = resources->next) {
1074
			old_xml_read_resource (parser, resources);
Richard Hult's avatar
Richard Hult committed
1075 1076 1077
		}
	}
	parser->resources = g_list_reverse (parser->resources);
1078

Richard Hult's avatar
Richard Hult committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
	/* Load assignments. */
	child = old_xml_search_child (parser->doc->children, "allocations");
	if (child != NULL) {
		for (assignments = child->children; assignments; assignments = assignments->next) {
			old_xml_read_assignment (parser, assignments);
		}
	}

	return TRUE;
}


/***************
 * XML helpers.
 */


/* Get a value for a node either carried as an attibute or as
 * the content of a child.
 *
 * Returns xmlChar, must be xmlFreed.
 */
static gchar *
old_xml_get_value (xmlNodePtr node, const char *name)
{
	gchar      *val;
	xmlNodePtr  child;

	val = (gchar *) xmlGetProp (node, name);

	if (val != NULL) {
		return val;
	}

	child = node->children;

	while (child != NULL) {
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
			val = xmlNodeGetContent(child);
			if (val != NULL) {
				return val;
			}
		}
		child = child->next;
	}

	return NULL;
}

/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
 *
 * Returns a g_malloc'ed string.  Caller must free.
 */
static gchar *
old_xml_get_string (xmlNodePtr node, const char *name)
{
	char *ret, *val;

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

	val = old_xml_get_value (node, name);
	ret = g_strdup (val);
	xmlFree (val);

	return ret;
}

static mrptime
old_xml_get_date (xmlNodePtr node, const char *name)
{
	gchar   *val;
	mrptime  t;

	g_return_val_if_fail (node != NULL, MRP_TIME_INVALID);
	g_return_val_if_fail (name != NULL, MRP_TIME_INVALID);

	val = old_xml_get_value (node, name);

	t = mrp_time_from_string (val, NULL);
1164

Richard Hult's avatar
Richard Hult committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	xmlFree (val);

	return t;
}

static gint
old_xml_get_int (xmlNodePtr node, const char *name)
{
	gchar *val;
	gint   ret;

	g_return_val_if_fail (node != NULL, 0);
	g_return_val_if_fail (name != NULL, 0);

	val = old_xml_get_value (node, name);
	if (val == NULL) {
		return 0;
	}

	ret = atoi (val);
1185

Richard Hult's avatar
Richard Hult committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
	xmlFree (val);

	return ret;
}

static gint
old_xml_get_int_with_default (xmlNodePtr node, const char *name, gint def)
{
	gchar *val;
	gint   ret;

	g_return_val_if_fail (node != NULL, def);
	g_return_val_if_fail (name != NULL, def);

	val = old_xml_get_value (node, name);
	if (val == NULL) {
		return def;
	}

	ret = atoi (val);
1206

Richard Hult's avatar
Richard Hult committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	xmlFree (val);

	return ret;
}

static gfloat
old_xml_get_float (xmlNodePtr node, const char *name)
{
	gchar *val;
	gfloat ret;

	g_return_val_if_fail (node != NULL, 0);
	g_return_val_if_fail (name != NULL, 0);

	val = old_xml_get_value (node, name);
	if (val == NULL) {
		return 0;
	}

	ret = g_ascii_strtod (val, NULL);
1227

Richard Hult's avatar
Richard Hult committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	xmlFree (val);

	return ret;
}

static MrpTaskType
old_xml_get_task_type (xmlNodePtr node, const char *name)
{
	gchar       *val;
	MrpTaskType  type;

	g_return_val_if_fail (node != NULL, 0);
	g_return_val_if_fail (name != NULL, 0);

	val = old_xml_get_value (node, name);
	if (val == NULL) {
		return MRP_TASK_TYPE_NORMAL;
	}

	if (!strcmp (val, "milestone")) {
		type = MRP_TASK_TYPE_MILESTONE;
	}
	else if (!strcmp (val, "normal")) {
		type = MRP_TASK_TYPE_NORMAL;
	} else {
		type = MRP_TASK_TYPE_NORMAL;
	}
1255

Richard Hult's avatar
Richard Hult committed
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
	xmlFree (val);

	return type;
}

static MrpTaskSched
old_xml_get_task_sched (xmlNodePtr node, const char *name)
{
	gchar        *val;
	MrpTaskSched  sched;

	g_return_val_if_fail (node != NULL, 0);
	g_return_val_if_fail (name != NULL, 0);

	val = old_xml_get_value (node, name);
	if (val == NULL) {
		return MRP_TASK_SCHED_FIXED_WORK;
	}

	if (!strcmp (val, "fixed-duration")) {
		sched = MRP_TASK_SCHED_FIXED_DURATION;
	}
	else if (!strcmp (val, "fixed-work")) {
		sched = MRP_TASK_SCHED_FIXED_WORK;
	} else {
		sched = MRP_TASK_SCHED_FIXED_WORK;
	}
1283

Richard Hult's avatar
Richard Hult committed
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
	xmlFree (val);

	return sched;
}

/* Search a child by name, if needed go down the tree to find it.
 */
static xmlNodePtr
old_xml_search_child (xmlNodePtr node, const gchar *name)
{
	xmlNodePtr ret;
	xmlNodePtr child;

	child = node->children;
	while (child != NULL) {
		if (!strcmp (child->name, name))
			return child;
		child = child->next;
	}
	child = node->children;
	while (child != NULL) {
		ret = old_xml_search_child (child, name);
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
}

static MrpPropertyType
old_xml_property_type_from_string (const gchar *str)
{
	if (!strcmp (str, "int")) {
		return MRP_PROPERTY_TYPE_INT;
	}
	else if (!strcmp (str, "text")) {
		return MRP_PROPERTY_TYPE_STRING;
	}
	else if (!strcmp (str, "text-list")) {
		return MRP_PROPERTY_TYPE_STRING_LIST;
	}
	else if (!strcmp (str, "float")) {
		return MRP_PROPERTY_TYPE_FLOAT;
	}
	else if (!strcmp (str, "date")) {
		return MRP_PROPERTY_TYPE_DATE;
	}
	else if (!strcmp (str, "duration")) {
		return MRP_PROPERTY_TYPE_DURATION;
	}
	else if (!strcmp (str, "cost")) {
		/* FIXME: implement */
1336
	} else {
Richard Hult's avatar
Richard Hult committed
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
		g_warning ("Not implemented support for type");
	}

	return MRP_PROPERTY_TYPE_NONE;
}

static void
old_xml_set_property_from_node (MrpProject *project,
				MrpObject  *object,
				xmlNodePtr  node)
{
	MrpProperty     *property;
	MrpPropertyType  type;
	mrptime          date;
	gint             i;
	gfloat           f;
	gchar           *name;
	gchar           *str;
	GValueArray     *array;
	GList           *phases;

	name  = old_xml_get_string (node, "name");

	if (!strcmp (name, "phases")) {
		phases = old_xml_read_crufty_phases (node);
		g_object_set (project, "phases", phases, NULL);
		mrp_string_list_free (phases);

		g_free (name);
		return;
	}
	else if (!strcmp (name, "phase")) {
		str = old_xml_get_string (node, "value");
		g_object_set (project, "phase", str, NULL);

		g_free (str);
		g_free (name);
		return;
	}

	str = old_xml_get_string (node, "value");

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
	/* Check if we have the property first. This is needed both to be robust
	 * against broken input.
	 */
	if (!mrp_project_has_property (project,
				       G_OBJECT_TYPE (object),
				       name)) {
		g_free (name);
		g_free (str);
		return;
	}
1389

Richard Hult's avatar
Richard Hult committed
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
	property = mrp_project_get_property (project,
					     name,
					     G_OBJECT_TYPE (object));

	type = mrp_property_get_property_type (property);

	switch (type) {
	case MRP_PROPERTY_TYPE_STRING:
		mrp_object_set (object, name, str, NULL);
		break;
	case MRP_PROPERTY_TYPE_STRING_LIST:
		array = old_xml_read_string_list (node, object);
		if (array) {
			mrp_object_set (object, name, array, NULL);
			g_value_array_free (array);
		}
		break;
	case MRP_PROPERTY_TYPE_INT:
		i = atoi (str);
		mrp_object_set (object, name, i, NULL);
		break;
	case MRP_PROPERTY_TYPE_FLOAT:
		f = g_ascii_strtod (str, NULL);
		mrp_object_set (object, name, f, NULL);
		break;
	case MRP_PROPERTY_TYPE_DURATION:
		i = atoi (str);
		mrp_object_set (object, name, i, NULL);
		break;
	case MRP_PROPERTY_TYPE_DATE:
		date = mrp_time_from_string (str, NULL);
		mrp_object_set (object, name, &date, NULL);
		break;
	case MRP_PROPERTY_TYPE_COST:
		/* FIXME: implement. */
		break;
	default:
		g_warning ("Not implemented support for type.");
		break;
	}

	g_free (name);
	g_free (str);
}

static void
old_xml_process_delayed_relations (MrpParser *parser)
{
	GList           *l;
	MrpTask         *task, *predecessor_task;
	DelayedRelation *relation;
1441

Richard Hult's avatar
Richard Hult committed
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
	for (l = parser->delayed_relations; l; l = l->next) {
		relation = l->data;

		task = g_hash_table_lookup (parser->task_hash,
					    GINT_TO_POINTER (relation->successor_id));
		g_assert (task != NULL);

		predecessor_task = g_hash_table_lookup (parser->task_hash,
							GINT_TO_POINTER (relation->predecessor_id));
		g_assert (predecessor_task != NULL);

		mrp_task_add_predecessor (task,
					  predecessor_task,
					  relation->type,
					  relation->lag,
					  NULL);

		g_free (relation);
	}
}

gboolean
mrp_old_xml_parse (MrpProject *project, xmlDoc *doc, GError **error)
{
	MrpParser       parser;
	gboolean        success;
        MrpTaskManager *task_manager;
        MrpAssignment  *assignment;
        GList          *node;
1471

Richard Hult's avatar
Richard Hult committed
1472 1473 1474 1475 1476 1477 1478
	g_return_val_if_fail (MRP_IS_PROJECT (project), FALSE);
	g_return_val_if_fail (doc != NULL, FALSE);

	memset (&parser, 0, sizeof (parser));

	parser.project_start = -1;
	parser.project = g_object_ref (project);
1479

Richard Hult's avatar
Richard Hult committed
1480
	parser.doc = doc;
1481

Richard Hult's avatar
Richard Hult committed
1482 1483 1484 1485
	parser.task_hash = g_hash_table_new (NULL, NULL);
	parser.resource_hash = g_hash_table_new (NULL, NULL);
	parser.group_hash = g_hash_table_new (NULL, NULL);
	parser.day_hash = g_hash_table_new_full (NULL, NULL,
1486
						 NULL,
Richard Hult's avatar
Richard Hult committed
1487 1488
						 (GDestroyNotify) mrp_day_unref);
	parser.calendar_hash = g_hash_table_new (NULL, NULL);
1489

Richard Hult's avatar
Richard Hult committed
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	success = old_xml_read_project (&parser);

	g_hash_table_destroy (parser.resource_hash);
	g_hash_table_destroy (parser.group_hash);
	g_hash_table_destroy (parser.day_hash);
	g_hash_table_destroy (parser.calendar_hash);

	if (!success) {
		return FALSE;
	}
1500

Richard Hult's avatar
Richard Hult committed
1501 1502 1503
        task_manager = imrp_project_get_task_manager (project);
        mrp_task_manager_set_root (task_manager, parser.root_task);

1504
	parser.project_start = mrp_time_align_day (parser.project_start);
1505

Richard Hult's avatar
Richard Hult committed
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
	g_object_set (project,
                      "project-start", parser.project_start,
                      "default-group", parser.default_group,
                      NULL);

	old_xml_process_delayed_relations (&parser);

        g_object_set_data (G_OBJECT (project),
                           "version", GINT_TO_POINTER (parser.version));

        g_hash_table_destroy (parser.task_hash);
	g_list_free (parser.delayed_relations);

       	imrp_project_set_groups (project, parser.groups);

       	for (node = parser.assignments; node; node = node->next) {
		assignment = MRP_ASSIGNMENT (node->data);

		imrp_task_add_assignment (mrp_assignment_get_task (assignment),
					  assignment);
		imrp_resource_add_assignment (mrp_assignment_get_resource (assignment),
					      assignment);
		g_object_unref (assignment);
	}

        g_list_free (parser.assignments);
        g_list_free (parser.resources);
1533

Richard Hult's avatar
Richard Hult committed
1534 1535 1536
	return TRUE;
}