[Planner Dev] Patch for undoing Insert tasks/resources dialogs



Hi!

Here goes another patch to undo the dialog that allow massive tasks and
resources insertion. With this patch goes some changes in how the undo
commands are located. We need to undo similar commands in differents
places so I have started to move some of the undo commands code to new
file. For the moment:

planner-task-cmd.[ch]
planner-resource-cmd.[ch]

We need to add this new files to the repository for this patch to work.
I attach the new files to this email also. Having all the code in one
file and with the new to reuse it in several places has made me to
change a little the interface in order it can be used from different
files, specially the task insertion undo.

With this patch, only the calendar dialog undo remains to be done.

Cheers

-- Alvaro
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2004 Alvaro del Castillo <acs barrapunto 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <libgnome/gnome-i18n.h>
#include "planner-task-cmd.h"
#include "planner-task-tree.h"

typedef struct {
	PlannerCmd        base;

	MrpProject       *project;
	MrpTask          *before;
	MrpTask          *after;
	MrpRelationType   relationship;
	glong             lag;
	GError           *error;
} TaskCmdLink;

static gboolean
task_cmd_link_do (PlannerCmd *cmd_base)
{
	TaskCmdLink *cmd;
	GError      *error = NULL;
	MrpRelation *relation;
	gboolean     retval;

	cmd = (TaskCmdLink *) cmd_base;

	relation = mrp_task_add_predecessor (cmd->after,
					     cmd->before,
					     cmd->relationship,
					     cmd->lag,
					     &error);
	if (!error) {
		retval = TRUE;
	} else {
		cmd->error = error;
		retval = FALSE;		
	} 

	return retval;
}

static void
task_cmd_link_undo (PlannerCmd *cmd_base)
{
	TaskCmdLink *cmd;
	
	cmd = (TaskCmdLink*) cmd_base;
	
	mrp_task_remove_predecessor (cmd->after, cmd->before);
}

static void
task_cmd_link_free (PlannerCmd *cmd_base)
{
	TaskCmdLink *cmd;

	cmd = (TaskCmdLink *) cmd_base;

	g_object_unref (cmd->project);
	g_object_unref (cmd->before);
	g_object_unref (cmd->after);
}


PlannerCmd *
planner_task_cmd_link (PlannerWindow   *main_window,
		       MrpTask         *before,
		       MrpTask         *after,
		       MrpRelationType  relationship,
		       glong            lag,
		       GError         **error)
{
	PlannerCmd          *cmd_base;
	TaskCmdLink         *cmd;

	cmd_base = planner_cmd_new (TaskCmdLink,
				    _("Link task"),
				    task_cmd_link_do,
				    task_cmd_link_undo,
				    task_cmd_link_free);

	cmd = (TaskCmdLink *) cmd_base;

	cmd->project = g_object_ref (planner_window_get_project (main_window));

	cmd->before = g_object_ref (before);
	cmd->after = g_object_ref (after);
	cmd->relationship = relationship;
	cmd->lag = lag;
			
	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager 
					   (main_window),
					   cmd_base);

	if (cmd->error) {
		g_propagate_error (error, cmd->error);
		/* FIXME: who clean the cmd memory? */
		return NULL;
	}

	return cmd_base;	
}

static gboolean
task_cmd_unlink_do (PlannerCmd *cmd_base)
{
	TaskCmdLink *cmd;
	
	cmd = (TaskCmdLink*) cmd_base;

	if (g_getenv ("PLANNER_DEBUG_UNDO_TASK")) {
		g_message ("Removing the link ...");
	}
	
	mrp_task_remove_predecessor (cmd->after, cmd->before);

	return TRUE;
}

static void
task_cmd_unlink_undo (PlannerCmd *cmd_base)
{
	TaskCmdLink *cmd;
	GError      *error = NULL;
	MrpRelation *relation;

	cmd = (TaskCmdLink *) cmd_base;

	relation = mrp_task_add_predecessor (cmd->after,
					     cmd->before,
					     cmd->relationship,
					     cmd->lag,
					     &error);
	g_assert (relation);
}

static void
task_cmd_unlink_free (PlannerCmd *cmd_base)
{
	TaskCmdLink *cmd;

	cmd = (TaskCmdLink *) cmd_base;

	g_object_unref (cmd->project);
	g_object_unref (cmd->before);
	g_object_unref (cmd->after);
}

PlannerCmd *
planner_task_cmd_unlink (PlannerWindow   *main_window,
			 MrpRelation     *relation)
{
	PlannerCmd          *cmd_base;
	TaskCmdLink         *cmd;

	cmd_base = planner_cmd_new (TaskCmdLink,
				    _("Unkink task"),
				    task_cmd_unlink_do,
				    task_cmd_unlink_undo,
				    task_cmd_unlink_free);
	
	cmd = (TaskCmdLink *) cmd_base;

	cmd->project = g_object_ref (planner_window_get_project (main_window));

	cmd->before = g_object_ref (mrp_relation_get_predecessor (relation));
	cmd->after = g_object_ref (mrp_relation_get_successor (relation));
	cmd->relationship = mrp_relation_get_relation_type (relation);
	cmd->lag = mrp_relation_get_lag (relation);
			
	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager 
					   (main_window),
					   cmd_base);
	return cmd_base;
}

typedef struct {
	PlannerCmd   base;

	MrpProject  *project;

	gint         position; 
	gint         work;
	gint         duration;
	
	MrpTask     *task; 	/* The inserted task */
	MrpTask     *parent;
} TaskCmdInsert;


static gboolean
task_cmd_insert_do (PlannerCmd *cmd_base)
{
	TaskCmdInsert *cmd;

	cmd = (TaskCmdInsert *) cmd_base;

	if (cmd->task == NULL) {
		cmd->task = g_object_new (MRP_TYPE_TASK,
					  "work", cmd->work,
					  "duration", cmd->duration,
					  NULL);
	}

	mrp_project_insert_task (cmd->project,
				 cmd->parent,
				 cmd->position,
				 cmd->task);

	return TRUE;
}

static void
task_cmd_insert_undo (PlannerCmd *cmd_base)
{
	TaskCmdInsert *cmd;
	
	cmd = (TaskCmdInsert *) cmd_base;

	mrp_project_remove_task (cmd->project, cmd->task);
}

static void
task_cmd_insert_free (PlannerCmd *cmd_base)
{
	TaskCmdInsert *cmd;

	cmd = (TaskCmdInsert*) cmd_base;

	g_object_unref (cmd->task);
	if (cmd->parent != NULL) {
		g_object_unref (cmd->parent);
	}
	g_object_unref (cmd->project);
	cmd->task = NULL;
}

PlannerCmd *
planner_task_cmd_insert (PlannerWindow  *main_window,
			 MrpTask        *parent,
			 gint            position,
			 gint            work,
			 gint            duration,
			 MrpTask        *new_task)
{
	PlannerCmd     *cmd_base;
	TaskCmdInsert  *cmd;

	cmd_base = planner_cmd_new (TaskCmdInsert,
				    _("Insert task"),
				    task_cmd_insert_do,
				    task_cmd_insert_undo,
				    task_cmd_insert_free);

	cmd = (TaskCmdInsert *) cmd_base;

	if (parent != NULL) {
		cmd->parent = g_object_ref (parent);
	}
	cmd->project = g_object_ref (planner_window_get_project (main_window));

	cmd->position = position;
	cmd->work = work;
	cmd->duration = duration;
	if (new_task != NULL) {
		cmd->task = g_object_ref (new_task);
	}
	
	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (main_window),
					   cmd_base);

	return cmd_base;
}
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2004 Alvaro del Castillo <acs barrapunto 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef __PLANNER_TASK_CMD_H__
#define __PLANNER_TASK_CMD_H__

#include "planner-window.h"
#include "planner-task-tree.h"

PlannerCmd * planner_task_cmd_link   (PlannerWindow   *main_window,
				      MrpTask         *before,
				      MrpTask         *after,
				      MrpRelationType  relationship,
				      glong            lag,
				      GError         **error);

PlannerCmd * planner_task_cmd_unlink (PlannerWindow   *main_window,
				      MrpRelation     *relation);

PlannerCmd * planner_task_cmd_insert (PlannerWindow  *main_window,
				      MrpTask        *parent,
				      gint            position,
				      gint            work,
				      gint            duration,
				      MrpTask        *new_task);

#endif /* __PLANNER_TASK_CMD_H__ */
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2004 Alvaro del Castillo <acs barrapunto 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <libgnome/gnome-i18n.h>
#include "planner-resource-cmd.h"

typedef struct {
	PlannerCmd   base;

	MrpProject  *project;
	const gchar *name;
	MrpResource *resource;
	MrpGroup    *group;
} ResourceCmdInsert;

static gboolean
resource_cmd_insert_do (PlannerCmd *cmd_base)
{
	ResourceCmdInsert *cmd;

	cmd = (ResourceCmdInsert*) cmd_base;

	mrp_project_add_resource (cmd->project, cmd->resource);

	return TRUE;
}

static void
resource_cmd_insert_undo (PlannerCmd *cmd_base)
{
	ResourceCmdInsert *cmd;
	
	cmd = (ResourceCmdInsert*) cmd_base;

	mrp_project_remove_resource (cmd->project,
				     cmd->resource);
}

static void
resource_cmd_insert_free (PlannerCmd  *cmd_base)
{
	ResourceCmdInsert *cmd;
	cmd = (ResourceCmdInsert*) cmd_base;

	g_object_unref (cmd->resource);
}


PlannerCmd *
planner_resource_cmd_insert (PlannerWindow *main_window,
			     MrpResource   *resource)
{
	PlannerCmd          *cmd_base;
	ResourceCmdInsert   *cmd;

	cmd = g_new0 (ResourceCmdInsert, 1);

	cmd_base = (PlannerCmd*) cmd;

	cmd_base->label = g_strdup (_("Insert resource"));
	cmd_base->do_func = resource_cmd_insert_do;
	cmd_base->undo_func = resource_cmd_insert_undo;
	cmd_base->free_func = resource_cmd_insert_free;

	if (resource == NULL) {
		cmd->resource = g_object_new (MRP_TYPE_RESOURCE, NULL);
	} else {
		cmd->resource = g_object_ref (resource);	
	}

	cmd->project = planner_window_get_project (main_window);

	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (main_window),
					   cmd_base);

	return cmd_base;
}
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2004 Alvaro del Castillo <acs barrapunto 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef __PLANNER_RESOURCE_CMD_H__
#define __PLANNER_RESOURCE_CMD_H__

#include "planner-window.h"

PlannerCmd * planner_resource_cmd_insert (PlannerWindow   *main_window,
					  MrpResource     *resource);

#endif /* __PLANNER_RESOURCE_CMD_H__ */
Index: po/ChangeLog
===================================================================
RCS file: /cvs/gnome/planner/po/ChangeLog,v
retrieving revision 1.144
diff -u -b -B -p -r1.144 ChangeLog
--- po/ChangeLog	11 Jun 2004 02:47:30 -0000	1.144
+++ po/ChangeLog	13 Jun 2004 08:58:40 -0000
@@ -1,3 +1,8 @@
+2004-06-13 Alvaro del Castillo <acs barrapunto com>
+
+	* POTFILES.in: Added new files planner-task-cmd.c and
+	planner-resource-cmd.c.
+
 2004-06-10  Adam Weinberger  <adamw gnome org>
 
 	* en_CA.po: Updated Canadian English translation.
Index: po/POTFILES.in
===================================================================
RCS file: /cvs/gnome/planner/po/POTFILES.in,v
retrieving revision 1.8
diff -u -b -B -p -r1.8 POTFILES.in
--- po/POTFILES.in	3 May 2004 19:05:57 -0000	1.8
+++ po/POTFILES.in	13 Jun 2004 08:58:40 -0000
@@ -80,12 +80,14 @@ src/planner-project-properties.c
 src/planner-property-dialog.c
 src/planner-python-plugin.c
 src/planner-relation-arrow.c
+src/planner-resource-cmd.c
 src/planner-resource-dialog.c
 src/planner-resource-input-dialog.c
 src/planner-resource-view.c
 src/planner-scale-utils.c
 src/planner-sidebar.c
 src/planner-sql-plugin.c
+src/planner-task-cmd.c
 src/planner-task-dialog.c
 src/planner-task-popup.c
 src/planner-task-tree.c
Index: src/Makefile.am
===================================================================
RCS file: /cvs/gnome/planner/src/Makefile.am,v
retrieving revision 1.14
diff -u -b -B -p -r1.14 Makefile.am
--- src/Makefile.am	12 May 2004 17:17:08 -0000	1.14
+++ src/Makefile.am	13 Jun 2004 08:58:40 -0000
@@ -76,6 +76,8 @@ planner_SOURCES = \
 	planner-property-dialog.h	\
 	planner-property-model.c	\
 	planner-property-model.h	\
+	planner-resource-cmd.c		\
+	planner-resource-cmd.h		\
 	planner-resource-dialog.c	\
 	planner-resource-dialog.h	\
 	planner-resource-input-dialog.c	\
@@ -84,6 +86,8 @@ planner_SOURCES = \
 	planner-sidebar.h		\
 	planner-table-print-sheet.c	\
 	planner-table-print-sheet.h	\
+	planner-task-cmd.c		\
+	planner-task-cmd.h		\
 	planner-task-dialog.c		\
 	planner-task-dialog.h		\
 	planner-task-input-dialog.c	\
Index: src/planner-gantt-row.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-gantt-row.c,v
retrieving revision 1.12
diff -u -b -B -p -r1.12 planner-gantt-row.c
--- src/planner-gantt-row.c	12 Jun 2004 06:26:10 -0000	1.12
+++ src/planner-gantt-row.c	13 Jun 2004 08:58:48 -0000
@@ -39,7 +39,7 @@
 #include "planner-scale-utils.h"
 #include "planner-task-tree.h"
 #include "planner-task-popup.h"
-#include "planner-task-dialog.h"
+#include "planner-task-cmd.h"
 
 /* The padding between the gantt bar and the text. */
 #define TEXT_PADDING 10.0
Index: src/planner-resource-input-dialog.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-resource-input-dialog.c,v
retrieving revision 1.2
diff -u -b -B -p -r1.2 planner-resource-input-dialog.c
--- src/planner-resource-input-dialog.c	15 Mar 2004 21:02:31 -0000	1.2
+++ src/planner-resource-input-dialog.c	13 Jun 2004 08:58:49 -0000
@@ -26,10 +26,12 @@
 #include <libgnome/gnome-i18n.h>
 #include "planner-marshal.h"
 #include "planner-resource-input-dialog.h"
+#include "planner-resource-cmd.h"
 
 typedef struct {
 	MrpProject *project;
 
+	PlannerWindow *main_window;
 	GtkWidget  *name_entry;
 	GtkWidget  *short_name_entry;
 	GtkWidget  *email_entry;
@@ -152,6 +154,7 @@ resource_input_dialog_free (gpointer use
 	DialogData *data = user_data;
 
 	g_object_unref (data->project);
+	g_object_unref (data->main_window);
 
 	g_free (data);
 }
@@ -185,7 +188,8 @@ resource_input_dialog_response_cb (GtkWi
 					 "group", group,
 				     NULL);
 		
-		mrp_project_add_resource (data->project, resource);
+		/* mrp_project_add_resource (data->project, resource); */
+		planner_resource_cmd_insert (data->main_window, resource);
 		
 		gtk_entry_set_text (GTK_ENTRY (data->name_entry), "");
 		gtk_entry_set_text (GTK_ENTRY (data->short_name_entry), "");
@@ -212,17 +216,19 @@ resource_input_dialog_activate_cb (GtkWi
 }
 
 GtkWidget *
-planner_resource_input_dialog_new (MrpProject *project)
+planner_resource_input_dialog_new (PlannerWindow *main_window)
 {
 	GtkWidget  *dialog;
 	DialogData *data;
 	GladeXML   *gui;
+	MrpProject *project;
 
-	g_return_val_if_fail (MRP_IS_PROJECT (project), NULL);
+	project = planner_window_get_project (main_window);
 	
 	data = g_new0 (DialogData, 1);
 
 	data->project = g_object_ref (project);
+	data->main_window = g_object_ref (main_window);
 	
 	gui = glade_xml_new (GLADEDIR "/resource-input-dialog.glade",
 			     NULL , NULL);
Index: src/planner-resource-input-dialog.h
===================================================================
RCS file: /cvs/gnome/planner/src/planner-resource-input-dialog.h,v
retrieving revision 1.2
diff -u -b -B -p -r1.2 planner-resource-input-dialog.h
--- src/planner-resource-input-dialog.h	11 Dec 2003 10:52:46 -0000	1.2
+++ src/planner-resource-input-dialog.h	13 Jun 2004 08:58:49 -0000
@@ -25,9 +25,9 @@
 #define __PLANNER_RESOURCE_INPUT_DIALOG_H__
 
 #include <gtk/gtkwidget.h>
-#include <libplanner/mrp-project.h>
+#include "planner-window.h"
 
-GtkWidget * planner_resource_input_dialog_new (MrpProject *project);
+GtkWidget * planner_resource_input_dialog_new (PlannerWindow *main_window);
 
 #endif /* __PLANNER_RESOURCE_INPUT_DIALOG_H__ */
 
Index: src/planner-resource-view.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-resource-view.c,v
retrieving revision 1.21
diff -u -b -B -p -r1.21 planner-resource-view.c
--- src/planner-resource-view.c	4 Jun 2004 05:10:20 -0000	1.21
+++ src/planner-resource-view.c	13 Jun 2004 08:58:53 -0000
@@ -52,6 +52,7 @@
 #include "planner-resource-input-dialog.h"
 #include "planner-table-print-sheet.h"
 #include "planner-property-dialog.h"
+#include "planner-resource-cmd.h"
 
 struct _PlannerViewPriv {
 	GtkItemFactory         *popup_factory;
@@ -760,64 +761,6 @@ resource_view_popup_edit_resource_cb    
 	resource_view_edit_resource_cb (NULL, callback_data, NULL);
 }
 
-static gboolean
-resource_cmd_insert_do (PlannerCmd *cmd_base)
-{
-	ResourceCmdInsert *cmd;
-
-	cmd = (ResourceCmdInsert*) cmd_base;
-
-	mrp_project_add_resource (cmd->project, cmd->resource);
-
-	return TRUE;
-}
-
-static void
-resource_cmd_insert_undo (PlannerCmd *cmd_base)
-{
-	ResourceCmdInsert *cmd;
-	
-	cmd = (ResourceCmdInsert*) cmd_base;
-
-	mrp_project_remove_resource (cmd->project,
-				     cmd->resource);
-}
-
-static void
-resource_cmd_insert_free (PlannerCmd  *cmd_base)
-{
-	ResourceCmdInsert *cmd;
-	cmd = (ResourceCmdInsert*) cmd_base;
-
-	g_object_unref (cmd->resource);
-}
-
-
-static PlannerCmd *
-resource_cmd_insert (PlannerView *view)
-{
-	PlannerCmd          *cmd_base;
-	ResourceCmdInsert   *cmd;
-
-	cmd = g_new0 (ResourceCmdInsert, 1);
-
-	cmd_base = (PlannerCmd*) cmd;
-
-	cmd_base->label = g_strdup (_("Insert resource"));
-	cmd_base->do_func = resource_cmd_insert_do;
-	cmd_base->undo_func = resource_cmd_insert_undo;
-	cmd_base->free_func = resource_cmd_insert_free;
-
-	cmd->resource = g_object_new (MRP_TYPE_RESOURCE, NULL);
-
-	cmd->project = planner_window_get_project (view->main_window);
-
-	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (view->main_window),
-					   cmd_base);
-
-	return cmd_base;
-}
-
 static void
 resource_view_insert_resource_cb (BonoboUIComponent *component, 
 				  gpointer           data, 
@@ -833,7 +776,7 @@ resource_view_insert_resource_cb (Bonobo
 	view = PLANNER_VIEW (data);
 	priv = view->priv;
 
-	cmd = (ResourceCmdInsert*) resource_cmd_insert (view);
+	cmd = (ResourceCmdInsert*) planner_resource_cmd_insert (view->main_window, NULL);
 
 	if (!GTK_WIDGET_HAS_FOCUS (priv->tree_view)) {
 		gtk_widget_grab_focus (GTK_WIDGET (priv->tree_view));
@@ -875,7 +818,7 @@ resource_view_insert_resources_cb (Bonob
 		return;
 	}
 
-	priv->resource_input_dialog = planner_resource_input_dialog_new (project);
+	priv->resource_input_dialog = planner_resource_input_dialog_new (view->main_window);
 
 	gtk_window_set_transient_for (GTK_WINDOW (priv->resource_input_dialog),
 				      GTK_WINDOW (view->main_window));
Index: src/planner-task-dialog.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-task-dialog.c,v
retrieving revision 1.16
diff -u -b -B -p -r1.16 planner-task-dialog.c
--- src/planner-task-dialog.c	13 Jun 2004 08:47:09 -0000	1.16
+++ src/planner-task-dialog.c	13 Jun 2004 08:58:59 -0000
@@ -1,4 +1,4 @@
- /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2002-2003 CodeFactory AB
  * Copyright (C) 2002-2003 Richard Hult <richard imendio com>
@@ -33,6 +33,7 @@
 #include "planner-cell-renderer-list.h"
 #include "planner-assignment-model.h"
 #include "planner-predecessor-model.h"
+#include "planner-task-cmd.h"
 #include "planner-task-dialog.h"
 
 /* FIXME: This should be read from the option menu when that works */
@@ -239,18 +240,6 @@ typedef struct {
 	GError          *error;
 } TaskCmdEditPredecessor;
 
-
-typedef struct {
-	PlannerCmd        base;
-
-	MrpProject       *project;
-	MrpTask          *before;
-	MrpTask          *after;
-	MrpRelationType   relationship;
-	glong             lag;
-	GError           *error;
-} TaskCmdLink;
-
 typedef struct {
 	PlannerCmd base;
 
@@ -798,167 +787,6 @@ task_cmd_assign_units (PlannerWindow *ma
 	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (main_window),
 					   cmd_base);
 
-	return cmd_base;
-}
-
-/* link and unlink shared with planner-task-tree.c */
-static gboolean
-task_cmd_link_do (PlannerCmd *cmd_base)
-{
-	TaskCmdLink *cmd;
-	GError      *error = NULL;
-	MrpRelation *relation;
-	gboolean     retval;
-
-	cmd = (TaskCmdLink *) cmd_base;
-
-	relation = mrp_task_add_predecessor (cmd->after,
-					     cmd->before,
-					     cmd->relationship,
-					     cmd->lag,
-					     &error);
-	if (!error) {
-		retval = TRUE;
-	} else {
-		cmd->error = error;
-		retval = FALSE;		
-	} 
-
-	return retval;
-}
-
-static void
-task_cmd_link_undo (PlannerCmd *cmd_base)
-{
-	TaskCmdLink *cmd;
-	
-	cmd = (TaskCmdLink*) cmd_base;
-	
-	mrp_task_remove_predecessor (cmd->after, cmd->before);
-}
-
-static void
-task_cmd_link_free (PlannerCmd *cmd_base)
-{
-	TaskCmdLink *cmd;
-
-	cmd = (TaskCmdLink *) cmd_base;
-
-	g_object_unref (cmd->project);
-	g_object_unref (cmd->before);
-	g_object_unref (cmd->after);
-}
-
-
-PlannerCmd *
-planner_task_cmd_link (PlannerWindow   *main_window,
-		       MrpTask         *before,
-		       MrpTask         *after,
-		       MrpRelationType  relationship,
-		       glong            lag,
-		       GError         **error)
-{
-	PlannerCmd          *cmd_base;
-	TaskCmdLink         *cmd;
-
-	cmd_base = planner_cmd_new (TaskCmdLink,
-				    _("Link task"),
-				    task_cmd_link_do,
-				    task_cmd_link_undo,
-				    task_cmd_link_free);
-
-	cmd = (TaskCmdLink *) cmd_base;
-
-	cmd->project = g_object_ref (planner_window_get_project (main_window));
-
-	cmd->before = g_object_ref (before);
-	cmd->after = g_object_ref (after);
-	cmd->relationship = relationship;
-	cmd->lag = lag;
-			
-	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager 
-					   (main_window),
-					   cmd_base);
-
-	if (cmd->error) {
-		g_propagate_error (error, cmd->error);
-		/* FIXME: who clean the cmd memory? */
-		return NULL;
-	}
-
-	return cmd_base;	
-}
-
-static gboolean
-task_cmd_unlink_do (PlannerCmd *cmd_base)
-{
-	TaskCmdLink *cmd;
-	
-	cmd = (TaskCmdLink*) cmd_base;
-
-	if (g_getenv ("PLANNER_DEBUG_UNDO_TASK")) {
-		g_message ("Removing the link ...");
-	}
-	
-	mrp_task_remove_predecessor (cmd->after, cmd->before);
-
-	return TRUE;
-}
-
-static void
-task_cmd_unlink_undo (PlannerCmd *cmd_base)
-{
-	TaskCmdLink *cmd;
-	GError      *error = NULL;
-	MrpRelation *relation;
-
-	cmd = (TaskCmdLink *) cmd_base;
-
-	relation = mrp_task_add_predecessor (cmd->after,
-					     cmd->before,
-					     cmd->relationship,
-					     cmd->lag,
-					     &error);
-	g_assert (relation);
-}
-
-static void
-task_cmd_unlink_free (PlannerCmd *cmd_base)
-{
-	TaskCmdLink *cmd;
-
-	cmd = (TaskCmdLink *) cmd_base;
-
-	g_object_unref (cmd->project);
-	g_object_unref (cmd->before);
-	g_object_unref (cmd->after);
-}
-
-PlannerCmd *
-planner_task_cmd_unlink (PlannerWindow   *main_window,
-			 MrpRelation     *relation)
-{
-	PlannerCmd          *cmd_base;
-	TaskCmdLink         *cmd;
-
-	cmd_base = planner_cmd_new (TaskCmdLink,
-				    _("Unkink task"),
-				    task_cmd_unlink_do,
-				    task_cmd_unlink_undo,
-				    task_cmd_unlink_free);
-	
-	cmd = (TaskCmdLink *) cmd_base;
-
-	cmd->project = g_object_ref (planner_window_get_project (main_window));
-
-	cmd->before = g_object_ref (mrp_relation_get_predecessor (relation));
-	cmd->after = g_object_ref (mrp_relation_get_successor (relation));
-	cmd->relationship = mrp_relation_get_relation_type (relation);
-	cmd->lag = mrp_relation_get_lag (relation);
-			
-	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager 
-					   (main_window),
-					   cmd_base);
 	return cmd_base;
 }
 
Index: src/planner-task-dialog.h
===================================================================
RCS file: /cvs/gnome/planner/src/planner-task-dialog.h,v
retrieving revision 1.4
diff -u -b -B -p -r1.4 planner-task-dialog.h
--- src/planner-task-dialog.h	10 Jun 2004 11:30:36 -0000	1.4
+++ src/planner-task-dialog.h	13 Jun 2004 08:58:59 -0000
@@ -38,17 +38,6 @@ GtkWidget * planner_task_dialog_new  (Pl
 				      MrpTask               *task,
 				      PlannerTaskDialogPage  page);
 
-PlannerCmd * planner_task_cmd_link   (PlannerWindow   *main_window,
-				      MrpTask         *before,
-				      MrpTask         *after,
-				      MrpRelationType  relationship,
-				      glong            lag,
-				      GError         **error);
-
-PlannerCmd * planner_task_cmd_unlink (PlannerWindow   *main_window,
-				      MrpRelation     *relation);
-
-
 #endif /* __PLANNER_TASK_DIALOG_H__ */
 
 
Index: src/planner-task-input-dialog.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-task-input-dialog.c,v
retrieving revision 1.1.1.1
diff -u -b -B -p -r1.1.1.1 planner-task-input-dialog.c
--- src/planner-task-input-dialog.c	1 Dec 2003 17:36:26 -0000	1.1.1.1
+++ src/planner-task-input-dialog.c	13 Jun 2004 08:59:00 -0000
@@ -25,9 +25,11 @@
 #include <gtk/gtk.h>
 #include "planner-marshal.h"
 #include "planner-task-input-dialog.h"
+#include "planner-task-cmd.h"
 
 typedef struct {
 	MrpProject *project;
+	PlannerWindow *main_window;
 
 	GtkWidget  *name_entry;
 	GtkWidget  *work_entry;
@@ -39,6 +41,7 @@ task_input_dialog_free (gpointer user_da
 	DialogData *data = user_data;
 
 	g_object_unref (data->project);
+	g_object_unref (data->main_window);
 
 	g_free (data);
 }
@@ -68,7 +71,9 @@ task_input_dialog_response_cb (GtkWidget
 				     "name", name,
 				     NULL);
 		
-		mrp_project_insert_task (data->project, NULL, -1, task);
+		/*mrp_project_insert_task (data->project, NULL, -1, task);*/
+		planner_task_cmd_insert (data->main_window, 
+					 NULL, -1, 0, 0, task);
 		
 		gtk_entry_set_text (GTK_ENTRY (data->name_entry), "");
 		gtk_entry_set_text (GTK_ENTRY (data->work_entry), "");
@@ -94,17 +99,19 @@ task_input_dialog_activate_cb (GtkWidget
 }
 
 GtkWidget *
-planner_task_input_dialog_new (MrpProject *project)
+planner_task_input_dialog_new (PlannerWindow *main_window)
 {
 	GtkWidget  *dialog;
 	DialogData *data;
 	GladeXML   *gui;
-
-	g_return_val_if_fail (MRP_IS_PROJECT (project), NULL);
+	MrpProject *project;
 	
 	data = g_new0 (DialogData, 1);
 
+	project = planner_window_get_project (main_window);
+
 	data->project = g_object_ref (project);
+	data->main_window = g_object_ref (main_window);
 	
 	gui = glade_xml_new (GLADEDIR "/task-input-dialog.glade",
 			     NULL , NULL);
Index: src/planner-task-input-dialog.h
===================================================================
RCS file: /cvs/gnome/planner/src/planner-task-input-dialog.h,v
retrieving revision 1.2
diff -u -b -B -p -r1.2 planner-task-input-dialog.h
--- src/planner-task-input-dialog.h	11 Dec 2003 10:52:46 -0000	1.2
+++ src/planner-task-input-dialog.h	13 Jun 2004 08:59:00 -0000
@@ -25,15 +25,11 @@
 #define __PLANNER_TASK_INPUT_DIALOG_H__
 
 #include <gtk/gtkwidget.h>
-#include <libplanner/mrp-project.h>
+#include "planner-window.h"
 
-GtkWidget * planner_task_input_dialog_new (MrpProject *project);
+GtkWidget * planner_task_input_dialog_new (PlannerWindow *main_window);
 
 #endif /* __PLANNER_TASK_INPUT_DIALOG_H__ */
-
-
-
-
 
 
 
Index: src/planner-task-tree.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-task-tree.c,v
retrieving revision 1.29
diff -u -b -B -p -r1.29 planner-task-tree.c
--- src/planner-task-tree.c	12 Jun 2004 06:26:10 -0000	1.29
+++ src/planner-task-tree.c	13 Jun 2004 08:59:06 -0000
@@ -46,6 +46,7 @@
 #include "planner-task-tree.h"
 #include "planner-gantt-model.h"
 #include "planner-task-popup.h"
+#include "planner-task-cmd.h"
 
 #define WARN_TASK_DIALOGS 10
 #define MAX_TASK_DIALOGS 25
@@ -172,114 +173,6 @@ typedef struct {
 	PlannerTaskTree *tree;
 	MrpProject      *project;
 	
-	gint             work;
-	gint             duration;
-	
-	GtkTreePath     *path;
-
-	MrpTask         *task; 	/* The inserted task */
-} TaskCmdInsert;
-
-static void
-task_cmd_insert_free (PlannerCmd *cmd_base)
-{
-	TaskCmdInsert *cmd;
-
-	cmd = (TaskCmdInsert*) cmd_base;
-
-	gtk_tree_path_free (cmd->path);
-	g_object_unref (cmd->task);
-	cmd->task = NULL;
-}
-
-static gboolean
-task_cmd_insert_do (PlannerCmd *cmd_base)
-{
-	TaskCmdInsert *cmd;
-	GtkTreePath   *path;
-	MrpTask       *parent;
-	gint           depth;
-	gint           position;
-
-	cmd = (TaskCmdInsert *) cmd_base;
-
-	path = gtk_tree_path_copy (cmd->path);
-
-	depth = gtk_tree_path_get_depth (path);
-	position = gtk_tree_path_get_indices (path)[depth - 1];
-
- 	if (depth > 1) {
-		gtk_tree_path_up (path);
-		parent = task_tree_get_task_from_path (cmd->tree, path);
-	} else {
-		parent = NULL;
-	}
-	
-	gtk_tree_path_free (path);
-	
-	if (cmd->task == NULL) {
-		cmd->task = g_object_new (MRP_TYPE_TASK,
-					  "work", cmd->work,
-					  "duration", cmd->duration,
-					  NULL);
-	}
-
-	mrp_project_insert_task (cmd->project,
-				 parent,
-				 position,
-				 cmd->task);
-
-	return TRUE;
-}
-
-static void
-task_cmd_insert_undo (PlannerCmd *cmd_base)
-{
-	TaskCmdInsert *cmd;
-	
-	cmd = (TaskCmdInsert *) cmd_base;
-
-	mrp_project_remove_task (cmd->project, cmd->task);
-}
-
-static PlannerCmd *
-task_cmd_insert (PlannerTaskTree *tree,
-		 GtkTreePath     *path,
-		 gint             work,
-		 gint             duration)
-{
-	PlannerTaskTreePriv *priv = tree->priv;
-	PlannerCmd          *cmd_base;
-	TaskCmdInsert       *cmd;
-
-	cmd = g_new0 (TaskCmdInsert, 1);
-
-	cmd_base = (PlannerCmd *) cmd;
-
-	cmd_base->label = g_strdup (_("Insert task"));
-	cmd_base->do_func = task_cmd_insert_do;
-	cmd_base->undo_func = task_cmd_insert_undo;
-	cmd_base->free_func = task_cmd_insert_free;
-
-	cmd->tree = tree;
-	cmd->project = task_tree_get_project (tree);
-
-	cmd->path = gtk_tree_path_copy (path);
-	cmd->work = work;
-	cmd->duration = duration;
-	
-	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (priv->main_window),
-					   cmd_base);
-
-	return cmd_base;
-}
-
-typedef struct {
-	PlannerCmd         base;
-
-	PlannerTaskTree   *tree;
-	MrpProject        *project;
-	
 	GtkTreePath       *path;
 	
 	gchar             *property;  
@@ -2576,8 +2469,9 @@ planner_task_tree_insert_subtask (Planne
 	MrpTask           *parent;
 	GList             *list;
 	gint               work;
+	gint               depth;
 	gint               position;
-	TaskCmdInsert     *cmd;
+	PlannerCmd        *cmd;
 	GtkTreeIter        iter;
 
 	list = planner_task_tree_get_selected_tasks (tree);
@@ -2603,7 +2497,20 @@ planner_task_tree_insert_subtask (Planne
 		mrp_project_get_calendar (tree->priv->project),
 		mrp_day_get_work ());
 
-	cmd = (TaskCmdInsert*) task_cmd_insert (tree, path, work, work);
+	depth = gtk_tree_path_get_depth (path);
+	position = gtk_tree_path_get_indices (path)[depth - 1];
+
+	if (depth > 1) {
+		gtk_tree_path_up (path);
+		parent = task_tree_get_task_from_path (tree, path);
+	} else {
+		parent = NULL;
+	}
+
+	cmd = planner_task_cmd_insert (tree->priv->main_window, 
+				       parent, position, work, work, NULL);
+
+	/* cmd = planner_task_cmd_insert (tree, path, work, work); */
 
 	if (!GTK_WIDGET_HAS_FOCUS (tree)) {
 		gtk_widget_grab_focus (GTK_WIDGET (tree));
@@ -2634,7 +2541,8 @@ planner_task_tree_insert_task (PlannerTa
 	GList               *list;
 	gint                 work;
 	gint                 position;
-	TaskCmdInsert       *cmd;
+	gint                 depth;
+	PlannerCmd          *cmd;
 
 	priv = tree->priv;
 	
@@ -2672,7 +2580,20 @@ planner_task_tree_insert_task (PlannerTa
 		mrp_project_get_calendar (priv->project),
 		mrp_day_get_work ());
 	
-	cmd = (TaskCmdInsert*) task_cmd_insert (tree, path, work, work);
+	/* cmd = planner_task_cmd_insert (tree, path, work, work); */
+	
+	depth = gtk_tree_path_get_depth (path);
+	position = gtk_tree_path_get_indices (path)[depth - 1];
+
+	if (depth > 1) {
+		gtk_tree_path_up (path);
+		parent = task_tree_get_task_from_path (tree, path);
+	} else {
+		parent = NULL;
+	}
+
+	cmd = planner_task_cmd_insert (tree->priv->main_window, 
+				       parent, position, work, work, NULL);
 	
 	if (!GTK_WIDGET_HAS_FOCUS (tree)) {
 		gtk_widget_grab_focus (GTK_WIDGET (tree));
@@ -2811,7 +2732,7 @@ planner_task_tree_insert_tasks (PlannerT
 		return;
 	}
 
-	widget = planner_task_input_dialog_new (priv->project);
+	widget = planner_task_input_dialog_new (priv->main_window);
 	gtk_window_set_transient_for (GTK_WINDOW (widget),
 				      GTK_WINDOW (priv->main_window));
 	gtk_widget_show (widget);
Index: src/planner-task-tree.h
===================================================================
RCS file: /cvs/gnome/planner/src/planner-task-tree.h,v
retrieving revision 1.9
diff -u -b -B -p -r1.9 planner-task-tree.h
--- src/planner-task-tree.h	12 Jun 2004 06:26:10 -0000	1.9
+++ src/planner-task-tree.h	13 Jun 2004 08:59:06 -0000
@@ -94,7 +94,7 @@ PlannerCmd*  planner_task_tree_task_cmd_
 						       glong                  lag,
 						       GError               **error);
 
-/* Temporal function until we do it in a clean way */
+/* Temporal functions. We need to approve them. */
 PlannerWindow * planner_task_tree_get_window          (PlannerTaskTree       *tree);
 
 #endif /* __PLANNER_TASK_TREE_H__ */

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]