planner-property-dialog.c 23.9 KB
Newer Older
Richard Hult's avatar
Richard Hult committed
1 2 3 4 5 6
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2002 CodeFactory AB
 * Copyright (C) 2002 Richard Hult <richard@imendio.com>
 * Copyright (C) 2002 Mikael Hallendal <micke@imendio.com>
 * Copyright (C) 2002 Alvaro del Castillo <acs@barrapunto.com>
7
 * Copyright (C) 2004 Lincoln Phipps <lincoln.phipps@openmutual.net>
Richard Hult's avatar
Richard Hult committed
8
 * Copyright (C) 2004 Imendio AB
Richard Hult's avatar
Richard Hult committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
30
#include <glib/gi18n.h>
Richard Hult's avatar
Richard Hult committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <glade/glade.h>
#include <gtk/gtkdialog.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkmessagedialog.h>
#include <gtk/gtkoptionmenu.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtktreeview.h>
#include <gtk/gtkliststore.h>
#include <gtk/gtkcellrenderertext.h>
#include <gtk/gtkcellrenderertoggle.h>
#include <gtk/gtktreeselection.h>
#include <gtk/gtkmain.h>
#include <libplanner/mrp-object.h>
#include <libplanner/mrp-property.h>
45
#include "libplanner/mrp-paths.h"
Richard Hult's avatar
Richard Hult committed
46 47 48 49 50 51 52 53
#include "planner-cell-renderer-list.h"
#include "planner-property-dialog.h"
#include "planner-property-model.h"

#define GET_PRIV(d) (g_object_get_data (G_OBJECT (d), "priv"))


typedef struct {
54 55 56 57 58
	PlannerWindow    *main_window;
	MrpProject       *project;
	GtkTreeModel     *model;
	GtkWidget        *tree;
	GType             owner;
59
	MrpPropertyStore *shop;
Richard Hult's avatar
Richard Hult committed
60
} PlannerPropertyDialogPriv;
Richard Hult's avatar
Richard Hult committed
61

62
typedef struct {
63
	PlannerCmd        base;
64
	
65 66
	PlannerWindow	 *window;
	MrpProject       *project;
67 68 69 70 71 72
	gchar	 	 *name; 
	MrpPropertyType	  type;
	gchar	 	 *label_text;
	gchar	 	 *description;
	GType		  owner;
	gboolean	  user_defined;   
73
} PropertyCmdAdd;
74 75

typedef struct {
76
	PlannerCmd       base;
77
	
78
	MrpProject      *project;
79 80
	gchar	 	*name;
	MrpProperty	*property;
81 82 83 84 85
	MrpPropertyType	 type;
	gchar	 	*label_text;
	gchar	 	*description;
	GType		 owner;
	gboolean	 user_defined;   
86 87 88

	GHashTable      *tasks;
	GHashTable      *resources;	
89
} PropertyCmdRemove;
90 91

typedef struct {
92
	PlannerCmd     base;
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	PlannerWindow *window;
	MrpProject    *project;
	GType          owner;
	gchar	      *name;
	gchar         *old_text;
	gchar  	      *new_text;
} PropertyCmdLabelEdited;


static void        property_dialog_setup_option_menu    (GtkWidget           *option_menu,
							 GCallback            func,
							 gpointer             user_data,
							 gconstpointer        str1,
							 ...);
static gint        property_dialog_get_selected         (GtkWidget           *option_menu);
static void        property_dialog_close_cb             (GtkWidget           *button,
							 GtkWidget           *dialog);
static void        property_dialog_type_selected_cb     (GtkWidget           *widget,
							 GtkWidget           *dialog);
static gboolean    property_dialog_label_changed_cb     (GtkWidget           *label_entry,
							 GdkEvent            *event,
							 GtkWidget           *name_entry);
static void        property_dialog_add_cb               (GtkWidget           *button,
							 GtkWidget           *dialog);
static void        property_dialog_remove_cb            (GtkWidget           *button,
							 GtkWidget           *dialog);
static void        property_dialog_label_edited         (GtkCellRendererText *cell,
121
							 gchar               *path_str,
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
							 gchar               *new_text,
							 GtkWidget           *dialog);
static void        property_dialog_setup_list           (GtkWidget           *dialog);
static void        property_dialog_setup_widgets        (GtkWidget           *dialog,
							 GladeXML            *glade);
static PlannerCmd *property_cmd_add                     (PlannerWindow       *window,
							 MrpProject          *project,
							 GType                owner,
							 const gchar         *name,
							 MrpPropertyType      type,
							 const gchar         *label_text,
							 const gchar         *description,
							 gboolean             user_defined);
static gboolean    property_cmd_add_do                  (PlannerCmd          *cmd_base);
static void        property_cmd_add_undo                (PlannerCmd          *cmd_base);
static void        property_cmd_add_free                (PlannerCmd          *cmd_base);
static PlannerCmd *property_cmd_remove                  (PlannerWindow       *window,
							 MrpProject          *project,
							 GType                owner,
							 const gchar         *name);
static gboolean    property_cmd_remove_do               (PlannerCmd          *cmd_base);
static void        property_cmd_remove_undo             (PlannerCmd          *cmd_base);
static void        property_cmd_remove_free             (PlannerCmd          *cmd_base);
static PlannerCmd *property_cmd_label_edited            (PlannerWindow       *window,
							 MrpProperty         *property,
							 MrpProject          *project,
							 GType                owner,
							 const gchar         *new_text);
static gboolean    property_cmd_label_edited_do         (PlannerCmd          *cmd_base);
static void        property_cmd_label_edited_undo       (PlannerCmd          *cmd_base);
static void        property_cmd_label_edited_free       (PlannerCmd          *cmd_base);

154

Richard Hult's avatar
Richard Hult committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
static void
property_dialog_setup_option_menu (GtkWidget     *option_menu,
				   GCallback      func,
				   gpointer       user_data,
				   gconstpointer  str1, ...)
{
	GtkWidget     *menu;
	GtkWidget     *menu_item;
	gint           i;
	va_list        args;
	gconstpointer  str;
	gint           type;

       	menu = gtk_option_menu_get_menu (GTK_OPTION_MENU (option_menu));
	if (menu) {
		gtk_widget_destroy (menu);
	}
	
	menu = gtk_menu_new ();

	va_start (args, str1);
	for (str = str1, i = 0; str != NULL; str = va_arg (args, gpointer), i++) {
		menu_item = gtk_menu_item_new_with_label (str);
		gtk_widget_show (menu_item);
		gtk_menu_append (GTK_MENU (menu), menu_item);

		type = va_arg (args, gint);
		
		g_object_set_data (G_OBJECT (menu_item),
				   "data",
				   GINT_TO_POINTER (type));
		if (func) {
			g_signal_connect (menu_item,
					  "activate",
					  func,
					  user_data);
		}
	}
	va_end (args);

	gtk_widget_show (menu);
	gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
}

static gint
property_dialog_get_selected (GtkWidget *option_menu)
{
	GtkWidget *menu;
	GtkWidget *item;
	gint       ret;
	
	menu = gtk_option_menu_get_menu (GTK_OPTION_MENU (option_menu));
		
	item = gtk_menu_get_active (GTK_MENU (menu));

	ret = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "data"));

	return ret;
}	

static void
property_dialog_close_cb (GtkWidget *button,
			  GtkWidget *dialog)
{
	gtk_widget_destroy (dialog);
}

static void
property_dialog_type_selected_cb (GtkWidget *widget,
				  GtkWidget *dialog)
{
	gint type;
	
	type = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "data"));

	g_object_set_data (G_OBJECT (dialog), "type", GINT_TO_POINTER (type));
}

static gboolean
property_dialog_label_changed_cb (GtkWidget *label_entry,
				  GdkEvent  *event,
				  GtkWidget *name_entry)
{
	const gchar *name;
	const gchar *label;

	name = gtk_entry_get_text (GTK_ENTRY (name_entry));

	if (name == NULL || name[0] == 0) {
		label = gtk_entry_get_text (GTK_ENTRY (label_entry));
		gtk_entry_set_text (GTK_ENTRY (name_entry), label);
	}

	return FALSE;
}

static void
property_dialog_add_cb (GtkWidget *button,
			GtkWidget *dialog)
{
Richard Hult's avatar
Richard Hult committed
255
	PlannerPropertyDialogPriv *priv;
256 257 258 259 260 261 262 263 264 265 266 267
	MrpPropertyType            type;
	const gchar               *label;
	const gchar               *name;
	const gchar               *description;
	GladeXML                  *glade;
	GtkWidget                 *label_entry;
	GtkWidget                 *name_entry;
	GtkWidget                 *add_dialog;
	GtkWidget                 *w;
	gint                       response;
	gboolean                   finished = FALSE;
	gunichar                   c;
268
	gchar                     *filename;
269
				
Richard Hult's avatar
Richard Hult committed
270 271
	priv = GET_PRIV (dialog);

272 273
	filename = mrp_paths_get_glade_dir ("new-property.glade");
	glade = glade_xml_new (filename,
Richard Hult's avatar
Richard Hult committed
274 275
			       NULL,
			       NULL);
276
	g_free (filename);
Richard Hult's avatar
Richard Hult committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		
	add_dialog = glade_xml_get_widget (glade, "add_dialog");

	label_entry = glade_xml_get_widget (glade, "label_entry");
	name_entry = glade_xml_get_widget (glade, "name_entry");
	
	g_signal_connect (label_entry,
			  "focus_out_event",
			  G_CALLBACK (property_dialog_label_changed_cb),
			  name_entry);

	property_dialog_setup_option_menu (
		glade_xml_get_widget (glade, "type_menu"),
		G_CALLBACK (property_dialog_type_selected_cb),
		add_dialog,
		mrp_property_type_as_string (MRP_PROPERTY_TYPE_STRING),
		MRP_PROPERTY_TYPE_STRING,
		mrp_property_type_as_string (MRP_PROPERTY_TYPE_INT),
		MRP_PROPERTY_TYPE_INT,
		mrp_property_type_as_string (MRP_PROPERTY_TYPE_FLOAT),
		MRP_PROPERTY_TYPE_FLOAT,
/*  		mrp_property_type_as_string (MRP_PROPERTY_TYPE_DATE), */
/*  		MRP_PROPERTY_TYPE_DATE, */
/*  		mrp_property_type_as_string (MRP_PROPERTY_TYPE_DURATION), */
/*  		MRP_PROPERTY_TYPE_DURATION, */
/* 		mrp_property_type_as_string (MRP_PROPERTY_TYPE_COST), */
/*  		MRP_PROPERTY_TYPE_COST, */
		NULL);

	while (!finished) {
		response = gtk_dialog_run (GTK_DIALOG (add_dialog));

		switch (response) {
		case GTK_RESPONSE_OK:
			label = gtk_entry_get_text (GTK_ENTRY (label_entry));
			if (label == NULL || label[0] == 0) {
				finished = FALSE;
				break;
			}
			
			name = gtk_entry_get_text (GTK_ENTRY (name_entry));
			if (name == NULL || name[0] == 0) {
				finished = FALSE;
				break;
			}
322 323 324

			c = g_utf8_get_char (name);
			if (!g_unichar_isalpha (c)) {
Richard Hult's avatar
Richard Hult committed
325 326
				GtkWidget *msg_dialog;
				
327 328 329 330 331 332 333 334
				msg_dialog = gtk_message_dialog_new (
					GTK_WINDOW (add_dialog),
					GTK_DIALOG_MODAL |
					GTK_DIALOG_DESTROY_WITH_PARENT,
					GTK_MESSAGE_WARNING,
					GTK_BUTTONS_OK,
					_("The name of the custom property needs to start with a letter."));

Richard Hult's avatar
Richard Hult committed
335 336 337 338 339 340 341 342 343 344 345 346
				gtk_dialog_run (GTK_DIALOG (msg_dialog));
				gtk_widget_destroy (msg_dialog);
				
				finished = FALSE;
				break;
			}
		
			w = glade_xml_get_widget (glade, "description_entry");
			description = gtk_entry_get_text (GTK_ENTRY (w));
			
			w = glade_xml_get_widget (glade, "type_menu");
			type = property_dialog_get_selected (w);
347
			
Richard Hult's avatar
Richard Hult committed
348
			if (type != MRP_PROPERTY_TYPE_NONE) {
349 350 351 352 353 354 355 356
				property_cmd_add (priv->main_window,
						  priv->project, 
						  priv->owner, 
						  name, 
						  type,
						  label,
						  description,
						  TRUE);
Richard Hult's avatar
Richard Hult committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
			}

			finished = TRUE;
			break;
			
		case GTK_RESPONSE_DELETE_EVENT:
		case GTK_RESPONSE_CANCEL:
			finished = TRUE;
			break;
			
		default:
			break;
		}
	}
	
 	gtk_widget_destroy (add_dialog);
373
	g_object_unref (glade);
Richard Hult's avatar
Richard Hult committed
374 375 376 377 378
}

static void
property_dialog_remove_cb (GtkWidget *button, GtkWidget *dialog)
{
Richard Hult's avatar
Richard Hult committed
379
	PlannerPropertyDialogPriv *priv;
380 381 382 383 384
	GtkTreeSelection          *selection;
	GtkTreeIter                iter;
	gchar                     *name;
	GtkWidget                 *remove_dialog;
	gint                       response;
385
		
Richard Hult's avatar
Richard Hult committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
	priv = GET_PRIV (dialog);

	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree));

	if (!gtk_tree_selection_get_selected (selection, NULL, &iter)) {
		return;
	}

	gtk_tree_model_get (priv->model, &iter,
			    COL_NAME, &name,
			    -1);

	remove_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
						GTK_DIALOG_MODAL |
						GTK_DIALOG_DESTROY_WITH_PARENT,
						GTK_MESSAGE_QUESTION,
						GTK_BUTTONS_YES_NO,
						_("Do you really want to remove the property '%s' from "
						  "the project?"),
						name);
	
	response = gtk_dialog_run (GTK_DIALOG (remove_dialog));

	switch (response) {
	case GTK_RESPONSE_YES:
411 412 413 414
		property_cmd_remove (priv->main_window,
				     priv->project, 
				     priv->owner, 
				     name);
Richard Hult's avatar
Richard Hult committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
		break;

	case GTK_RESPONSE_DELETE_EVENT:
	case GTK_RESPONSE_CANCEL:
		break;

	default:
		break;
	}

	gtk_widget_destroy (remove_dialog);
	g_free (name);
}

static void  
property_dialog_label_edited (GtkCellRendererText *cell, 
			      gchar               *path_str,
			      gchar               *new_text, 
			      GtkWidget           *dialog)
{
Richard Hult's avatar
Richard Hult committed
435
	PlannerPropertyDialogPriv *priv;
436 437 438 439
	GtkTreePath               *path;
	GtkTreeIter                iter;
	GtkTreeModel              *model;
	MrpProperty               *property;
Richard Hult's avatar
Richard Hult committed
440 441 442 443 444 445 446 447 448 449 450 451
	
	priv = GET_PRIV (dialog);

	model = priv->model;
	path = gtk_tree_path_new_from_string (path_str);

	gtk_tree_model_get_iter (model, &iter, path);

	gtk_tree_model_get (priv->model, &iter,
			    COL_PROPERTY, &property,
			    -1);

452 453 454 455 456 457 458
	if (strcmp (new_text, mrp_property_get_label (property)) != 0 ) {
		property_cmd_label_edited (priv->main_window, 
					   property, 
					   priv->project,
					   priv->owner,
					   new_text);
	}
Richard Hult's avatar
Richard Hult committed
459 460 461 462 463

	gtk_tree_path_free (path);
}

static void
464
property_dialog_setup_list (GtkWidget *dialog)
Richard Hult's avatar
Richard Hult committed
465
{
Richard Hult's avatar
Richard Hult committed
466
	PlannerPropertyDialogPriv *priv;
467 468 469 470
	GtkTreeView               *tree;
	GtkTreeViewColumn         *col;
	GtkCellRenderer           *cell;
	GtkTreeModel              *model;
Richard Hult's avatar
Richard Hult committed
471 472 473 474 475 476 477 478 479 480

	priv = g_object_get_data (G_OBJECT (dialog), "priv");

	tree = GTK_TREE_VIEW (priv->tree);

	gtk_tree_selection_set_mode (gtk_tree_view_get_selection (tree),
				     GTK_SELECTION_SINGLE);
 
	gtk_tree_view_set_headers_visible (tree, TRUE);

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
	/* Name */
	cell = gtk_cell_renderer_text_new ();
	g_object_set (cell, "editable", FALSE, NULL);
	
	col = gtk_tree_view_column_new_with_attributes (_("Name"),
							cell,
							"text", COL_NAME,
							NULL);
	gtk_tree_view_column_set_resizable (col, TRUE);
	gtk_tree_view_column_set_min_width (col, 100);
	gtk_tree_view_append_column (tree, col);
	
	/* Label */
	cell = gtk_cell_renderer_text_new ();
	g_object_set (cell, "editable", TRUE, NULL);
	
	g_signal_connect (cell,
			  "edited",
			  G_CALLBACK (property_dialog_label_edited),
			  dialog);
	
	col = gtk_tree_view_column_new_with_attributes (_("Label"),
							cell,
							"text", COL_LABEL,
							NULL);
	gtk_tree_view_column_set_resizable (col, TRUE);
	gtk_tree_view_column_set_min_width (col, 200);
	gtk_tree_view_append_column (tree, col);

	/* Type */
	cell = gtk_cell_renderer_text_new ();
	g_object_set (cell, "editable", FALSE, NULL);
	
	col = gtk_tree_view_column_new_with_attributes (_("Type"),
							cell,
							"text", COL_TYPE,
							NULL);
	gtk_tree_view_column_set_resizable (col, TRUE);
	/* gtk_tree_view_column_set_min_width (col, 200); */
	gtk_tree_view_append_column (tree, col);

	/* Create the shop (a type of store). */
523
	priv->shop = g_new0 (MrpPropertyStore, 1);
Richard Hult's avatar
Richard Hult committed
524
	
525
	model = planner_property_model_new (priv->project, priv->owner, priv->shop);
Richard Hult's avatar
Richard Hult committed
526 527 528 529 530 531 532 533 534
	priv->model = model;
	
	gtk_tree_view_set_model (tree, model);
}

static void
property_dialog_setup_widgets (GtkWidget *dialog,
			       GladeXML  *glade)
{
Richard Hult's avatar
Richard Hult committed
535
	PlannerPropertyDialogPriv *priv;
Richard Hult's avatar
Richard Hult committed
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
	GtkWidget            *w;

	priv = GET_PRIV (dialog);

	w = glade_xml_get_widget (glade, "close_button");
	g_signal_connect (w,
			  "clicked",
			  G_CALLBACK (property_dialog_close_cb),
			  dialog);

	w = glade_xml_get_widget (glade, "add_button");
	g_signal_connect (w,
			  "clicked",
			  G_CALLBACK (property_dialog_add_cb),
			  dialog);

	w = glade_xml_get_widget (glade, "remove_button");
	g_signal_connect (w,
			  "clicked",
			  G_CALLBACK (property_dialog_remove_cb),
			  dialog);

	w = glade_xml_get_widget (glade, "treeview");
	priv->tree = w;

561
	property_dialog_setup_list (dialog);
Richard Hult's avatar
Richard Hult committed
562 563 564
}

GtkWidget *
565 566 567 568
planner_property_dialog_new (PlannerWindow *main_window,
			     MrpProject    *project,
			     GType          owner,
			     const gchar   *title)
Richard Hult's avatar
Richard Hult committed
569
{
570 571
	GladeXML                  *glade;
	GtkWidget                 *dialog;
Richard Hult's avatar
Richard Hult committed
572
	PlannerPropertyDialogPriv *priv;
573
	gchar                     *filename;
Richard Hult's avatar
Richard Hult committed
574 575 576

	g_return_val_if_fail (MRP_IS_PROJECT (project), NULL);

Richard Hult's avatar
Richard Hult committed
577
	priv = g_new0 (PlannerPropertyDialogPriv, 1);
Richard Hult's avatar
Richard Hult committed
578
	
579 580
	filename = mrp_paths_get_glade_dir ("property-dialog.glade");
	glade = glade_xml_new (filename,
Richard Hult's avatar
Richard Hult committed
581
			       NULL, NULL);
582
	g_free (filename);
Richard Hult's avatar
Richard Hult committed
583 584 585 586 587 588 589
		
	dialog = glade_xml_get_widget (glade, "dialog");

	gtk_window_set_title (GTK_WINDOW (dialog), title);
	
	g_object_set_data (G_OBJECT (dialog), "priv", priv);

590
	priv->main_window = main_window;
Richard Hult's avatar
Richard Hult committed
591
	priv->project = project;
592
	priv->owner = owner;
Richard Hult's avatar
Richard Hult committed
593 594 595
	
	property_dialog_setup_widgets (dialog, glade);

596
	g_object_unref (glade);
Richard Hult's avatar
Richard Hult committed
597

598
	return dialog;
Richard Hult's avatar
Richard Hult committed
599
}
600 601

static gboolean
602
property_cmd_add_do (PlannerCmd *cmd_base)
603
{
604 605
	PropertyCmdAdd *cmd;
	MrpProperty    *property;
606
		
607
	cmd = (PropertyCmdAdd*) cmd_base;
608

609
	/* Why is this check here? */
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
	if (cmd->name == NULL) {
		return FALSE;
	}

	property = mrp_property_new (cmd->name, 
				     cmd->type,
				     cmd->label_text,
				     cmd->description,
				     cmd->user_defined);
			
	mrp_project_add_property (cmd->project, 
				  cmd->owner,
				  property,
				  cmd->user_defined);	
		
	if (!mrp_project_has_property (cmd->project, cmd->owner, cmd->name)) {
		return FALSE;
	}
	
	return TRUE; 
}

static void
633
property_cmd_add_undo (PlannerCmd *cmd_base)
634
{
635
	PropertyCmdAdd *cmd;
636
		
637
	cmd = (PropertyCmdAdd*) cmd_base;
638

639 640 641
	mrp_project_remove_property (cmd->project, 
				     cmd->owner,
				     cmd->name);
642 643 644 645
}


static void
646
property_cmd_add_free (PlannerCmd *cmd_base)
647
{
648
	PropertyCmdAdd *cmd;
649

650
	cmd = (PropertyCmdAdd*) cmd_base;	
651 652 653 654 655 656

	g_free (cmd->name);
	g_free (cmd->label_text);
	g_free (cmd->description);
}

657 658 659 660 661 662 663 664 665
static PlannerCmd *
property_cmd_add (PlannerWindow   *window,
		  MrpProject      *project,
		  GType	           owner,
		  const gchar     *name,
		  MrpPropertyType  type,
		  const gchar     *label_text,
		  const gchar     *description,
		  gboolean         user_defined)
666
{
667 668
	PlannerCmd     *cmd_base;
	PropertyCmdAdd *cmd;
669

670 671 672 673 674
	cmd_base = planner_cmd_new (PropertyCmdAdd,
				    _("Add property"),
				    property_cmd_add_do,
				    property_cmd_add_undo,
				    property_cmd_add_free);
675

676
	cmd = (PropertyCmdAdd *) cmd_base;
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

	cmd->window = window;
	cmd->project = project;
	cmd->owner = owner;
	cmd->name = g_strdup (name);
	cmd->type = type;
	cmd->label_text = g_strdup (label_text);
	cmd->description = g_strdup (description);
	cmd->user_defined = user_defined;

	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (window),
					   cmd_base);

	return cmd_base;
}


static gboolean
695
property_cmd_remove_do (PlannerCmd *cmd_base)
696
{
697
	PropertyCmdRemove *cmd;
698

699
	cmd = (PropertyCmdRemove*) cmd_base;
700 701 702 703 704
	
	if (!mrp_project_has_property (cmd->project, cmd->owner, cmd->name)) {
		return FALSE;
	}

705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	/* First we need to save the value of the property */
	if (cmd->owner == MRP_TYPE_TASK && g_hash_table_size (cmd->tasks) == 0) {
		GList *l, *tasks = mrp_project_get_all_tasks (cmd->project);

		/* ref tasks */
		for (l = tasks; l; l = l->next) {
			GValue *value = g_new0 (GValue, 1);
			g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (cmd->property));
			mrp_object_get_property (l->data, cmd->property, value);
			g_hash_table_insert (cmd->tasks, g_object_ref (l->data), value);
		}
		
	}	
	else if (cmd->owner == MRP_TYPE_RESOURCE && g_hash_table_size (cmd->resources) == 0) {
		GList *l, *resources = mrp_project_get_resources (cmd->project);

		for (l = resources; l; l = l->next) {
			GValue *value = g_new0 (GValue, 1);
			g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (cmd->property));
			mrp_object_get_property (l->data, cmd->property, value); 
			g_hash_table_insert (cmd->resources, g_object_ref (l->data), value);
		}
		
	}

730 731 732 733 734 735 736
	mrp_project_remove_property (cmd->project,
				     cmd->owner,
				     cmd->name);

	return TRUE;
}

737 738 739 740 741 742 743 744 745 746 747 748 749 750
static void
property_hash_recover  (gpointer key,
			gpointer value,
			gpointer user_data)
{
	PropertyCmdRemove *cmd = (PropertyCmdRemove*) user_data;

	g_assert (MRP_IS_TASK (key) || MRP_IS_RESOURCE (key));
	g_assert (G_IS_VALUE (value));

	mrp_object_set_property (key, cmd->property, value);

}

751
static void
752
property_cmd_remove_undo (PlannerCmd *cmd_base)
753
{
754
	PropertyCmdRemove *cmd;
755
	
756
	cmd = (PropertyCmdRemove*) cmd_base;
757

758 759 760 761 762 763
	mrp_property_unref (cmd->property);

	cmd->property = mrp_property_new (cmd->name, 
					  cmd->type,
					  cmd->label_text,
					  cmd->description,
764
					  cmd->user_defined);
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
	
	mrp_project_add_property (cmd->project, 
				  cmd->owner,
				  cmd->property,
				  cmd->user_defined);

	g_hash_table_foreach (cmd->tasks, property_hash_recover, cmd);
	g_hash_table_foreach (cmd->resources, property_hash_recover, cmd);	
}

static void
property_hash_free  (gpointer key,
		     gpointer value,
		     gpointer user_data) 
{
	g_object_unref (key);
	g_value_unset ((GValue *) value);	
782 783 784
}

static void
785
property_cmd_remove_free (PlannerCmd *cmd_base)
786
{
787
	PropertyCmdRemove *cmd;
788

789
	cmd = (PropertyCmdRemove*) cmd_base;
790

791 792 793 794 795 796 797
	mrp_property_unref (cmd->property);

	g_hash_table_foreach (cmd->tasks, property_hash_free, NULL);
	g_hash_table_destroy (cmd->tasks);
	g_hash_table_foreach (cmd->resources, property_hash_free, NULL);
	g_hash_table_destroy (cmd->resources);

798 799 800
	g_free (cmd->name);
	g_free (cmd->label_text);
	g_free (cmd->description);
801 802

	g_object_unref (cmd->project);
803 804 805
}

static PlannerCmd *
806 807 808 809
property_cmd_remove (PlannerWindow *window,
		     MrpProject	   *project,
		     GType          owner,
		     const gchar   *name)
810
{
811 812 813
	PlannerCmd        *cmd_base;
	PropertyCmdRemove *cmd;
	MrpProperty       *property;
814

815 816 817 818 819
	cmd_base = planner_cmd_new (PropertyCmdRemove,
				    _("Remove property"),
				    property_cmd_remove_do,
				    property_cmd_remove_undo,
				    property_cmd_remove_free);
820

821 822
	cmd = (PropertyCmdRemove *) cmd_base;
	
823
	cmd->project = g_object_ref (project);
824
	cmd->owner = owner;
825
	cmd->name = g_strdup (name);
826

827 828 829
	property = mrp_project_get_property (project,
					     name,
					     owner);
830 831

	cmd->property = mrp_property_ref (property);
832 833 834 835 836
	cmd->type = mrp_property_get_property_type (property);
	cmd->description = g_strdup (mrp_property_get_description (property));
	cmd->label_text = g_strdup ( mrp_property_get_label (property));
	cmd->user_defined = mrp_property_get_user_defined (property);

837 838 839
	cmd->tasks = g_hash_table_new (NULL, NULL);
	cmd->resources = g_hash_table_new (NULL, NULL);

840 841
	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (window),
					   cmd_base);
842
	
843 844 845 846
	return cmd_base;
}

static gboolean
847
property_cmd_label_edited_do (PlannerCmd *cmd_base)
848
{
849 850
	PropertyCmdLabelEdited *cmd;
	MrpProperty            *property;
851

852
	cmd = (PropertyCmdLabelEdited*) cmd_base;
853
	
854 855 856 857 858
	property = mrp_project_get_property (cmd->project,
					     cmd->name,
					     cmd->owner);
					     
	if (!property) {
859 860 861
		return FALSE;
	}
	
862
	mrp_property_set_label (property, cmd->new_text);
863 864 865 866 867 868
		
	return TRUE;
}


static void
869
property_cmd_label_edited_undo (PlannerCmd *cmd_base)
870
{
871 872
	PropertyCmdLabelEdited *cmd;
	MrpProperty            *property;
873
		
874
	cmd = (PropertyCmdLabelEdited*) cmd_base;
875

876 877 878
	property = mrp_project_get_property (cmd->project,
					     cmd->name,
					     cmd->owner);
879

880 881 882

	if (property != NULL) {
		mrp_property_set_label (property, cmd->old_text);	
883 884 885 886
	}
}

static void
887
property_cmd_label_edited_free (PlannerCmd *cmd_base)
888
{
889
	PropertyCmdLabelEdited *cmd;
890

891
	cmd = (PropertyCmdLabelEdited*) cmd_base;
892 893 894

	g_free (cmd->old_text);
	g_free (cmd->new_text);
895
	g_free (cmd->name);
896 897 898
}

static PlannerCmd *
899 900 901 902 903
property_cmd_label_edited (PlannerWindow *window,
			   MrpProperty	 *property,
			   MrpProject	 *project,
			   GType          owner,
			   const gchar 	 *new_text)
904
{
905 906
	PlannerCmd             *cmd_base;
	PropertyCmdLabelEdited *cmd;
907

908 909 910 911 912
	cmd_base = planner_cmd_new (PropertyCmdLabelEdited,
				    _("Edit property label"),
				    property_cmd_label_edited_do,
				    property_cmd_label_edited_undo,
				    property_cmd_label_edited_free);
913

914
	cmd = (PropertyCmdLabelEdited *) cmd_base;
915 916 917 918

	cmd->window = window;
	cmd->new_text = g_strdup (new_text);
		
919 920 921 922
	cmd->old_text = g_strdup (mrp_property_get_label (property));
	cmd->project = project;
	cmd->name = g_strdup (mrp_property_get_name (property));
	cmd->owner = owner;
923 924 925 926 927 928 929
	
	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (window),
					   cmd_base);

	return cmd_base;
}