[Planner Dev] A patch with new ideas for the undo/redo system



Hi guys!

Last week we have worked hard in the undo/redo system. Finally we have
reached the decission to store in the the Command Remove object we use
to follow all the changes, clones of the objects when they are removed,
so we can easily recover the object data and restore them in the project
if the user "undo" the deletion.

To implement the idea we are adding new methods to all the objects we
use in planner (mrp_resource, mrp_task, mrp_group ...) to clone an
instance of the class. In this patch goes the clone method for
MrpResource and the do/undo Command using the clone resource operation.

Cheers

-- Alvaro
? custom-undo.diff
? hash_id.diff
? hash_id_data.diff
? resource_clone.diff
? dotnet/Makefile
? dotnet/Makefile.in
? dotnet/libplanner/Makefile
? dotnet/libplanner/Makefile.in
? dotnet/libplanner/generated
? dotnet/libplanner/generated-stamp
? dotnet/libplanner/libplanner.dll
? dotnet/samples/Makefile
? dotnet/samples/Makefile.in
Index: libplanner/mrp-resource.c
===================================================================
RCS file: /cvs/gnome/planner/libplanner/mrp-resource.c,v
retrieving revision 1.1.1.1
diff -u -b -B -p -r1.1.1.1 mrp-resource.c
--- libplanner/mrp-resource.c	1 Dec 2003 17:36:22 -0000	1.1.1.1
+++ libplanner/mrp-resource.c	1 Feb 2004 19:14:40 -0000
@@ -3,6 +3,7 @@
  * Copyright (C) 2002-2003 CodeFactory AB
  * Copyright (C) 2002-2003 Richard Hult <richard imendio com>
  * Copyright (C) 2002 Mikael Hallendal <micke imendio com>
+ * Copyright (C) 2004 Alvaro del Castillo <acs barrapunto com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -676,3 +677,59 @@ mrp_resource_set_calendar (MrpResource *
 
 	g_object_set (resource, "calendar", calendar, NULL);
 }
+
+/**
+ * mrp_resource_clone:
+ * @resource: an #MrpResource
+ *
+ * Returns a clone of the MrpResource 
+ * 
+ **/
+MrpResource *
+mrp_resource_clone (MrpResource *resource)
+{
+	MrpResource     *clone;
+	MrpResourcePriv *priv;
+	MrpProject      *project;
+	GList           *custom_prop, *l;
+
+	g_return_if_fail (MRP_IS_RESOURCE (resource));
+
+	priv = clone->priv;
+	
+	clone = g_object_new (MRP_TYPE_RESOURCE, NULL);
+	priv = clone->priv;
+
+	mrp_object_get (resource,
+			"name",     &priv->name,
+			"group",    &priv->group,
+			"type",     &priv->type,
+			"units",    &priv->units,
+			"email",    &priv->email,
+			"note",     &priv->note,
+			"calendar", &priv->calendar,
+			"project",  &project,
+			NULL);
+
+	mrp_object_set (clone, "project", project, NULL);
+
+	// Custom properties
+	custom_prop = mrp_project_get_properties_from_type (project,
+							    MRP_TYPE_RESOURCE);
+	for (l = custom_prop; l; l = l->next) {
+		MrpProperty *property;
+		GValue       value = { 0 };
+		GParamSpec  *pspec;
+
+		property = l->data;
+
+		pspec = G_PARAM_SPEC (property);
+		g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
+
+		mrp_object_get_property (MRP_OBJECT (resource), l->data, &value);
+		mrp_object_set_property (MRP_OBJECT (clone), l->data, &value);
+	}
+	// FIXME: assignments
+
+	return clone;
+}
Index: libplanner/mrp-resource.h
===================================================================
RCS file: /cvs/gnome/planner/libplanner/mrp-resource.h,v
retrieving revision 1.1.1.1
diff -u -b -B -p -r1.1.1.1 mrp-resource.h
--- libplanner/mrp-resource.h	1 Dec 2003 17:36:22 -0000	1.1.1.1
+++ libplanner/mrp-resource.h	1 Feb 2004 19:14:41 -0000
@@ -76,5 +76,8 @@ MrpCalendar *   mrp_resource_get_calenda
 void            mrp_resource_set_calendar       (MrpResource     *resource,
 						 MrpCalendar     *calendar);
 
+MrpResource *   mrp_resource_clone              (MrpResource *resource);
+
+
 
 #endif /* __MRP_RESOURCE_H__ */
Index: src/planner-resource-view.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-resource-view.c,v
retrieving revision 1.10
diff -u -b -B -p -r1.10 planner-resource-view.c
--- src/planner-resource-view.c	1 Feb 2004 11:49:58 -0000	1.10
+++ src/planner-resource-view.c	1 Feb 2004 19:14:45 -0000
@@ -271,14 +271,11 @@ typedef struct {
 
 	MrpProject  *project;
 	guint        project_id;
-	const gchar *name;
 	MrpResource *resource;
 	guint        resource_id;
-	MrpResourceType  type;
-	MrpGroup    *group;
 	guint        group_id;
-	const gchar     *email;
-	/* FIXME: custom properties */
+	
+	MrpResource *clone;
 } ResourceCmdRemove;
 
 typedef struct {
@@ -875,22 +872,22 @@ resource_cmd_remove_do (PlannerCmd *cmd_
 {
 	ResourceCmdRemove *cmd;
 	guint              resource_removed_id;
-	MrpGroup          *current_group;
+	MrpGroup          *group;
 
 	cmd = (ResourceCmdRemove*) cmd_base;
 
 	resource_removed_id = mrp_object_get_id (MRP_OBJECT(cmd->resource));
 
-	g_message ("Id for the resource to be removed: %d", resource_removed_id); 
-
+	cmd->project_id = mrp_object_get_id (MRP_OBJECT(cmd->project));
 	cmd->resource_id = resource_removed_id;
-	mrp_object_get (cmd->resource, 
-			"name", &cmd->name, 
-			"email", &cmd->email,
-			"type", &cmd->type,
-			"group", &current_group,
-			NULL);
-	cmd->group_id = mrp_object_get_id (MRP_OBJECT (current_group));
+
+	cmd->clone = mrp_resource_clone (cmd->resource);
+
+	mrp_object_get (cmd->clone, "group", &group, NULL);
+
+	if (group != NULL) { 
+		cmd->group_id = mrp_object_get_id (MRP_OBJECT (group));
+	}
 
 	mrp_project_remove_resource (cmd->project, cmd->resource);
 
@@ -901,25 +898,24 @@ static void
 resource_cmd_remove_undo (PlannerCmd *cmd_base)
 {
 	ResourceCmdRemove *cmd;
-	MrpResource       *resource;
+	gpointer          *data;
 	
 	cmd = (ResourceCmdRemove*) cmd_base;
 
-	resource = g_object_new (MRP_TYPE_RESOURCE, NULL);
-	mrp_object_set (resource, "project", 
-			MRP_OBJECT(mrp_application_id_get_data (cmd->project_id)), NULL);
-	mrp_object_set (resource, "name", cmd->name, NULL);
-	mrp_object_set (resource, "type", cmd->type, NULL);
-	mrp_object_set (resource, "email", cmd->email, NULL);
-	mrp_object_set (resource, "group", 
-			MRP_OBJECT(mrp_application_id_get_data (cmd->group_id)), NULL);
-	mrp_object_set_id (MRP_OBJECT (resource), cmd->resource_id);
-	mrp_project_add_resource (cmd->project, resource);
-	cmd->resource = resource;
+	data = mrp_application_id_get_data (cmd->project_id);
+
+	g_assert (MRP_IS_PROJECT (data));
+
+	mrp_object_set (cmd->clone, "project", MRP_OBJECT (data), NULL);
+
+	if (!mrp_object_set_id (MRP_OBJECT (cmd->clone), cmd->resource_id))
+		g_warning ("Could't set the id to the object: %d", cmd->resource_id);
+
+	mrp_project_add_resource (cmd->project, cmd->clone);
+	cmd->resource = cmd->clone;
 }
 
 
-/* FIXME: add all the attributes from a resource to the UNDO system */
 static PlannerCmd *
 resource_cmd_remove (PlannerView *view, MrpResource *resource)
 {
@@ -937,7 +933,6 @@ resource_cmd_remove (PlannerView *view, 
 
 	cmd->project = planner_window_get_project (view->main_window);
 	cmd->resource = resource;
-	mrp_object_get (resource, "name", &cmd->name, NULL);
 
 	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (view->main_window),
 					   cmd_base);
@@ -1241,7 +1236,7 @@ resource_view_setup_tree_view (PlannerVi
 			  G_CALLBACK (resource_view_cell_email_edited),
 			  view);
 
-	/* Custom property for cost added by default */
+	/* Custom property for cost added by default to a resource */
 	project = planner_window_get_project (view->main_window);
 	properties = mrp_project_get_properties_from_type (project, 
 							   MRP_TYPE_RESOURCE);
@@ -1584,33 +1579,13 @@ resource_view_cell_group_edited (Planner
 	gtk_tree_path_free (path);
 }
 
-static void    
-resource_view_property_value_edited (GtkCellRendererText *cell,
-				     gchar               *path_str,
-				     gchar               *new_text,
-				     ColPropertyData     *data)
+static GValue
+resource_view_custom_property_set_value (MrpProperty *property,
+					 gchar       *new_text) 
 {
-	PlannerView        *view;
-	PlannerCmd         *cmd;
-	GtkTreePath        *path;
-	GtkTreeIter         iter;
-	GtkTreeModel       *model;
-	MrpProperty        *property;
+	GValue              value = { 0 };
 	MrpPropertyType     type;
-	MrpResource        *resource;
 	gfloat              fvalue;
-	GValue              value = { 0 };
-
-	view = data->view;
-	model = gtk_tree_view_get_model (view->priv->tree_view);
-	property = data->property;
-
-	path = gtk_tree_path_new_from_string (path_str);
-	gtk_tree_model_get_iter (model, &iter, path);
-
-	gtk_tree_model_get (model, &iter,
-			    COL_RESOURCE, &resource,
-			    -1);
 
 	/* FIXME: implement mrp_object_set_property like
 	 * g_object_set_property that takes a GValue. 
@@ -1621,75 +1596,36 @@ resource_view_property_value_edited (Gtk
 	case MRP_PROPERTY_TYPE_STRING:
 		g_value_init (&value, G_TYPE_STRING);
 		g_value_set_string (&value, new_text);
-		cmd = resource_cmd_edit_custom_property (view, resource,
-							 property, 
-							 &value);
-		g_value_unset (&value);
-		/* mrp_object_set (MRP_OBJECT (resource),
-				mrp_property_get_name (property), 
-				new_text,
-				NULL); */
+
 		break;
 	case MRP_PROPERTY_TYPE_INT:
 		g_value_init (&value, G_TYPE_INT);
 		g_value_set_int (&value, atoi (new_text));
-		cmd = resource_cmd_edit_custom_property (view, resource,
-							 property, 
-							 &value);
-		g_value_unset (&value);
-		/* mrp_object_set (MRP_OBJECT (resource),
-				mrp_property_get_name (property), 
-				atoi (new_text),
-				NULL); */
+
 		break;
 	case MRP_PROPERTY_TYPE_FLOAT:
 		fvalue = g_ascii_strtod (new_text, NULL);
 		g_value_init (&value, G_TYPE_FLOAT);
 		g_value_set_float (&value, fvalue);
-		cmd = resource_cmd_edit_custom_property (view, resource,
-							 property, 
-							 &value);
-		g_value_unset (&value);
-		/* mrp_object_set (MRP_OBJECT (resource),
-				mrp_property_get_name (property), 
-				fvalue,
-				NULL); */
+
 		break;
 
 	case MRP_PROPERTY_TYPE_DURATION:
 		/* FIXME: support reading units etc... */
 		g_value_init (&value, G_TYPE_INT);
 		g_value_set_int (&value, atoi (new_text) *8*60*60);
-		cmd = resource_cmd_edit_custom_property (view, resource,
-							 property, 
-							 &value);
-		g_value_unset (&value);
-		/* mrp_object_set (MRP_OBJECT (resource),
-				mrp_property_get_name (property), 
-				atoi (new_text) *8*60*60,
-				NULL); */
+
 		break;
 		
 
 	case MRP_PROPERTY_TYPE_DATE:
-/* 		date = PLANNER_CELL_RENDERER_DATE (cell); */
-/* 		mrp_object_set (MRP_OBJECT (resource), */
-/* 				mrp_property_get_name (property),  */
-/* 				&(date->time), */
-/* 				NULL); */
+
 		break;
 	case MRP_PROPERTY_TYPE_COST:
 		fvalue = g_ascii_strtod (new_text, NULL);
 		g_value_init (&value, G_TYPE_FLOAT);
 		g_value_set_float (&value, fvalue);
-		cmd = resource_cmd_edit_custom_property (view, resource,
-							 property, 
-							 &value);
-		g_value_unset (&value);
-		/* mrp_object_set (MRP_OBJECT (resource),
-				mrp_property_get_name (property), 
-				fvalue,
-				NULL); */
+
 		break;	
 				
 	default:
@@ -1697,6 +1633,42 @@ resource_view_property_value_edited (Gtk
 		break;
 	}
 
+	return value;
+}
+
+static void    
+resource_view_property_value_edited (GtkCellRendererText *cell,
+				     gchar               *path_str,
+				     gchar               *new_text,
+				     ColPropertyData     *data)
+{
+	PlannerView        *view;
+	PlannerCmd         *cmd;
+	GtkTreePath        *path;
+	GtkTreeIter         iter;
+	GtkTreeModel       *model;
+	MrpProperty        *property;
+	MrpResource        *resource;
+	GValue              value;
+
+	view = data->view;
+	model = gtk_tree_view_get_model (view->priv->tree_view);
+	property = data->property;
+
+	path = gtk_tree_path_new_from_string (path_str);
+	gtk_tree_model_get_iter (model, &iter, path);
+
+	gtk_tree_model_get (model, &iter,
+			    COL_RESOURCE, &resource,
+			    -1);
+
+	value = resource_view_custom_property_set_value (property, new_text);
+
+	cmd = resource_cmd_edit_custom_property (view, resource,
+						 property, 
+						 &value);
+	g_value_unset (&value);
+
 	gtk_tree_path_free (path);
 }
 

Attachment: signature.asc
Description: Esta parte del mensaje =?ISO-8859-1?Q?est=E1?= firmada digitalmente



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]