[Planner Dev] Patch for Project phase undo/redo (with gbooleans)




Attached is patch to do the project phase undo/redo.

To reviewers -
Differences from previous efforts are...

Updated to new boolean cmd "do" functions. I return
the TRUE/FALSE correctly though the cmd manager doesn't
actually use these yet AFAIK.

I now put the phase back into the same position
in the dialog. This is nice (and easy to do) - we
should do this to Group, Resources and other things like
this.

Other changes as per dev list comments.

The dialogs won't accept a phase thats a blank string nor
starts with a blank. This is to protect us from future
phase work, though existing projects with blank or
prefixed with blanks phases can still be removed/undo/redo
on that - I just protect the input right now not cleanup
existing stuff.

Rgds,
Lincoln.
Index: data/glade/project-properties.glade
===================================================================
RCS file: /cvs/gnome/planner/data/glade/project-properties.glade,v
retrieving revision 1.4
diff -u -b -B -p -r1.4 project-properties.glade
--- a/data/glade/project-properties.glade	16 Dec 2003 18:31:20 -0000	1.4
+++ b/data/glade/project-properties.glade	3 May 2004 12:15:30 -0000
@@ -533,6 +533,7 @@
 	  <child>
 	    <widget class="GtkButton" id="remove_button">
 	      <property name="visible">True</property>
+	      <property name="sensitive">False</property>
 	      <property name="can_default">True</property>
 	      <property name="can_focus">True</property>
 	      <property name="label">gtk-remove</property>
@@ -543,7 +544,7 @@
 	  </child>
 
 	  <child>
-	    <widget class="GtkButton" id="button1">
+	    <widget class="GtkButton" id="add_button">
 	      <property name="visible">True</property>
 	      <property name="can_default">True</property>
 	      <property name="can_focus">True</property>
@@ -643,6 +644,7 @@
 	  <child>
 	    <widget class="GtkButton" id="ok_button">
 	      <property name="visible">True</property>
+	      <property name="sensitive">False</property>
 	      <property name="can_default">True</property>
 	      <property name="has_default">True</property>
 	      <property name="can_focus">True</property>
Index: src/planner-phase-dialog.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-phase-dialog.c,v
retrieving revision 1.2
diff -u -b -B -p -r1.2 planner-phase-dialog.c
--- a/src/planner-phase-dialog.c	11 Dec 2003 10:52:46 -0000	1.2
+++ b/src/planner-phase-dialog.c	3 May 2004 12:15:35 -0000
@@ -1,5 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
+ * Copyright (C) 2004 Lincoln Phipps <lincoln phipps openmutual net>
  * Copyright (C) 2002-2003 CodeFactory AB
  * Copyright (C) 2002-2003 Richard Hult <richard imendio com>
  * Copyright (C) 2002 Mikael Hallendal <micke imendio com>
@@ -37,6 +38,27 @@ enum {
 	NUM_COLS
 };
 
+/* UNDO/REDO Structures */
+
+typedef struct {
+	PlannerCmd   base;
+
+	MrpProject  *project;
+	gchar       *phase;
+	gint        position;
+} PhaseCmdInsert;
+
+typedef struct {
+	PlannerCmd   base;
+
+	MrpProject  *project;
+	gchar       *phase;
+	gboolean    is_assigned; 
+	gint	    position;
+} PhaseCmdRemove;
+
+/* END of UNDO/REDO Strutures */
+
 typedef struct {
 	PlannerWindow  *main_window;
 	MrpProject    *project;
@@ -87,6 +109,22 @@ static void     phase_dialog_remove_phas
 						     const gchar      *phase);
 static void     phase_dialog_new_dialog_run         (DialogData       *data);
 
+/* UNDO/REDO prototypes */
+static PlannerCmd *phase_cmd_insert 		(PlannerWindow *window,
+						const gchar 	*phase,
+						gint	position_hint);
+				
+static gboolean	phase_cmd_insert_do 		(PlannerCmd *cmd_base);
+static void	phase_cmd_insert_undo 		(PlannerCmd *cmd_base);
+static void	phase_cmd_insert_free 		(PlannerCmd *cmd_base);
+
+static PlannerCmd *phase_cmd_remove 		(PlannerWindow *window, 
+						const gchar *phase);
+static gboolean	phase_cmd_remove_do 		(PlannerCmd *cmd_base);
+static void	phase_cmd_remove_undo 		(PlannerCmd *cmd_base);
+static void	phase_cmd_remove_free 		(PlannerCmd *cmd_base);
+
+/* End of UNDO/REDO prototypes */
 
 static void
 phase_dialog_notify_phases (MrpProject  *project,  
@@ -369,33 +407,11 @@ static void
 phase_dialog_remove_phase (DialogData  *data,
 			   const gchar *phase)
 {
-	GList        *list, *l;
-	GtkTreeView  *tree_view;
-	GtkListStore *store;
-	gboolean      found = FALSE;
 
-	tree_view = GTK_TREE_VIEW (data->tree_view);
-	store = GTK_LIST_STORE (gtk_tree_view_get_model (tree_view));
+	PhaseCmdRemove *cmd;
 
-	/* Get all phases from the project, remove the selected one, re-set the
-	 * list.
-	 */
-	g_object_get (data->project, "phases", &list, NULL);
+		cmd = (PhaseCmdRemove*) phase_cmd_remove (data->main_window, phase); 
 
-	for (l = list; l; l = l->next) {
-		if (!strcmp (phase, l->data)) {
-			g_free (l->data);
-			list = g_list_remove_link (list, l);
-			found = TRUE;
-			break;
-		}
-	}
-
-	if (found) {
-		g_object_set (data->project, "phases", list, NULL);
-	}
-
-	mrp_string_list_free (list);
 }
 
 /* Handle the little "new phase" dialog. */
@@ -408,7 +424,7 @@ phase_dialog_new_name_changed_cb (GtkEnt
 
 	name = gtk_entry_get_text (entry);
 	
-	sensitive = name && name[0];
+	sensitive = name && name[0] && strncmp (name, " ", 1) ;
 	gtk_widget_set_sensitive (data->new_ok_button, sensitive);
 }
 
@@ -419,7 +435,7 @@ phase_dialog_new_dialog_run (DialogData 
 	GtkWidget   *dialog;
 	GtkWidget   *entry;
 	const gchar *name;
-	GList       *list;
+	PhaseCmdInsert *cmd;
 	
 	glade = glade_xml_new (GLADEDIR "/project-properties.glade",
 			       "new_phase_dialog",
@@ -437,17 +453,231 @@ phase_dialog_new_dialog_run (DialogData 
 
 	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
 		name = gtk_entry_get_text (GTK_ENTRY (entry));
+		cmd = (PhaseCmdInsert*) phase_cmd_insert (data->main_window, (gchar *) name ,-1);	
 
-		/* Get all phases from the project, add the new one, re-set the
-		 * list.
+	}
+	g_object_unref (glade);
+	gtk_widget_destroy (dialog);
+}
+
+
+
+/* Start of UNDO/REDO routines */
+static gboolean
+phase_cmd_insert_do (PlannerCmd *cmd_base)
+{
+	PhaseCmdInsert *cmd;
+	GList          *list, *l;
+	gboolean	found;
+
+	cmd = (PhaseCmdInsert*) cmd_base;
+
+	if (cmd->phase == NULL) {
+		return FALSE;
+	}
+		
+	g_object_get (cmd->project, "phases", &list, NULL);
+	
+	found = FALSE;
+	
+	for (l = list; l ; l = l->next) {
+		if (strcmp (cmd->phase, (char *) l->data) == 0 ) { 
+			found = TRUE; /* Will be a duplicated name which is messy */
+		}
+	}
+	if (found == FALSE ) {   /* If here then not a blank nor a duplicated name */
+		list = g_list_insert (list, g_strdup (cmd->phase), cmd->position); 
+		g_object_set (cmd->project, "phases", list, NULL);
+	}
+	
+	mrp_string_list_free (list);
+	
+	if (found == FALSE) {
+		return TRUE; /* I.e. we must have done something */
+	} else {
+		return FALSE;  /* i.e. we did nothing */
+	}
+}
+
+static void
+phase_cmd_insert_undo (PlannerCmd *cmd_base)
+{
+	PhaseCmdInsert *cmd;
+	cmd = (PhaseCmdInsert*) cmd_base;
+	
+	GList        *list, *l;
+	gboolean      found = FALSE;
+	
+
+	g_object_get (cmd->project, "phases", &list, NULL);
+
+	for (l = list; l; l = l->next) {
+		if (!strcmp (cmd->phase, l->data)) {
+			g_free (l->data);
+			cmd->position = g_list_position (list, l);
+			list = g_list_remove_link (list, l);
+			found = TRUE;
+			break;
+		}
+	}
+
+	if (found) {
+		g_object_set (cmd->project, "phases", list, NULL);
+	}
+
+	mrp_string_list_free (list);
+		
+}
+
+
+static void
+phase_cmd_insert_free (PlannerCmd *cmd_base)
+{
+	PhaseCmdInsert  *cmd;
+
+	cmd = (PhaseCmdInsert*) cmd_base;	
+
+	g_free (cmd->phase);   
+	cmd->project = NULL;
+	cmd->position = -1;
+}
+
+static PlannerCmd *
+phase_cmd_insert (PlannerWindow *main_window, 
+		  const gchar 	*phase,
+		  gint		position_hint) 
+{
+	PlannerCmd      *cmd_base;
+	PhaseCmdInsert  *cmd;
+
+	cmd = g_new0 (PhaseCmdInsert, 1);
+
+	cmd_base = (PlannerCmd*) cmd;
+
+	cmd_base->label = g_strdup (_("Insert phase"));
+	cmd_base->do_func =  phase_cmd_insert_do; 
+	cmd_base->undo_func = phase_cmd_insert_undo;
+	cmd_base->free_func = phase_cmd_insert_free;
+
+	cmd->project = planner_window_get_project (main_window);
+
+	cmd->phase = g_strdup (phase);
+	
+	cmd->position = position_hint;
+
+	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (main_window),
+					   cmd_base);
+
+	return cmd_base;
+}
+
+static gboolean
+phase_cmd_remove_do (PlannerCmd *cmd_base)
+{
+	PhaseCmdRemove *cmd;
+	GList          *list, *l;
+	gchar          *assigned_phase;
+	gboolean      found;
+
+	cmd = (PhaseCmdRemove*) cmd_base;
+
+	mrp_object_get (cmd->project, "phase", &assigned_phase, NULL);
+	if (assigned_phase == cmd->phase) {
+		cmd->is_assigned = TRUE;
+	}
+	
+	/* Insert undo taken from phase_dialog_remove_phase
 		 */
-		g_object_get (data->project, "phases", &list, NULL);
-		list = g_list_append (list, g_strdup (name)); 
-		g_object_set (data->project, "phases", list, NULL);
+	g_object_get (cmd->project, "phases", &list, NULL);
+
+	found = FALSE;
+	for (l = list; l; l = l->next) {
+		if (!strcmp (cmd->phase, l->data)) {
+			g_free (l->data);
+			cmd->position = g_list_position (list, l);  /* Remember where we removed it */
+			list = g_list_remove_link (list, l);
+			found = TRUE;
+			break;
+		}
+	}
+
+	if (found) {
+		g_object_set (cmd->project, "phases", list, NULL);
+	}
+
 		mrp_string_list_free (list);
+	
+	if (found == TRUE) {
+		return FALSE;
+	} else {
+		return TRUE;
 	}
+}
 
-	g_object_unref (glade);
-	gtk_widget_destroy (dialog);
+static void
+phase_cmd_remove_undo (PlannerCmd *cmd_base)
+{
+	PhaseCmdRemove *cmd;
+	GList          *list;
+	
+	cmd = (PhaseCmdRemove*) cmd_base;
+
+	/* We need to recover the phase deleted */
+	
+	g_assert (cmd->phase);
+	
+	/* Insert do code added from phase_dialog_new_dialog_run() with changes.
+	 */
+
+	g_object_get (cmd->project, "phases", &list, NULL);
+	list = g_list_insert (list, g_strdup (cmd->phase), cmd->position); 
+	
+	g_object_set (cmd->project, "phases", list, NULL);
+	mrp_string_list_free (list);	
+	
+
+	if (cmd->is_assigned) {
+		mrp_object_set (cmd->project, "phase", cmd->phase, NULL);
+	}
+}
+
+static void
+phase_cmd_remove_free (PlannerCmd *cmd_base)
+{
+	PhaseCmdRemove *cmd;
+
+	cmd = (PhaseCmdRemove*) cmd_base;
+
+	cmd->project = NULL;
+	g_free (cmd->phase);
+	cmd->is_assigned = FALSE;
+	cmd->position = -1;
+}
+
+static PlannerCmd *
+phase_cmd_remove (PlannerWindow *window, 
+		  const gchar *phase)
+{
+	PlannerCmd      *cmd_base;
+	PhaseCmdRemove  *cmd;
+
+	cmd = g_new0 (PhaseCmdRemove, 1);
+
+	cmd_base = (PlannerCmd*) cmd;
+
+	cmd_base->label = g_strdup (_("Remove phase"));
+	cmd_base->do_func =   phase_cmd_remove_do;
+	cmd_base->undo_func = phase_cmd_remove_undo;
+	cmd_base->free_func = phase_cmd_remove_free;
+
+	cmd->project = planner_window_get_project (window);
+	cmd->phase = g_strdup (phase);
+
+	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager (window),
+					   cmd_base);
+
+	return cmd_base;
 }
 
+
+/* END of UNDO/REDO routines */


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