[Planner Dev] Undoing add and remove calendars



Hi guys!

Here goes the patch for undo adding and removing calendars. It has been
easy to implement (very similar to the work done in tasks) and I think
it is bug free with my tests, but more testing is needing ;-)

Cheers

-- 
Alvaro del Castillo San Félix
Lambdaux Software Services S.R.L.
Universidad Rey Juan Carlos
Centro de Apoyo Tecnológico
C/ Tulipán sn 28933 Mostoles, Madrid-Spain
www.lambdaux.com
acs lambdaux com
Index: libplanner/mrp-calendar.c
===================================================================
RCS file: /cvs/gnome/planner/libplanner/mrp-calendar.c,v
retrieving revision 1.2
diff -u -b -B -p -r1.2 mrp-calendar.c
--- libplanner/mrp-calendar.c	18 Jun 2004 23:55:42 -0000	1.2
+++ libplanner/mrp-calendar.c	19 Jun 2004 09:01:16 -0000
@@ -396,6 +396,24 @@ foreach_copy_days (gpointer     key,
 }
 
 /**
+ * mrp_calendar_add:
+ * @calendar: a #MrpCalendar to add
+ * @parent: a #MrpCalendar to inherit from 
+ * 
+ * Add @calendar to the project 
+ * 
+ * Return value:
+ **/
+void
+mrp_calendar_add (MrpCalendar *calendar, MrpCalendar *parent)
+{
+	calendar_add_child (parent, calendar);
+
+	imrp_project_signal_calendar_tree_changed (calendar->priv->project);
+	imrp_project_set_needs_saving (calendar->priv->project, TRUE);
+}
+
+/**
  * mrp_calendar_copy:
  * @name: the name of the new calendar
  * @calendar: a #MrpCalendar to copy
Index: libplanner/mrp-calendar.h
===================================================================
RCS file: /cvs/gnome/planner/libplanner/mrp-calendar.h,v
retrieving revision 1.1.1.1
diff -u -b -B -p -r1.1.1.1 mrp-calendar.h
--- libplanner/mrp-calendar.h	1 Dec 2003 17:36:22 -0000	1.1.1.1
+++ libplanner/mrp-calendar.h	19 Jun 2004 09:01:17 -0000
@@ -80,6 +80,8 @@ enum {
 GType        mrp_calendar_get_type                 (void) G_GNUC_CONST;
 MrpCalendar *mrp_calendar_new                      (const gchar *name,
 						    MrpProject  *project);
+void         mrp_calendar_add                      (MrpCalendar *calendar,
+						    MrpCalendar *parent);
 MrpCalendar *mrp_calendar_copy                     (const gchar *name,
 						    MrpCalendar *calendar);
 MrpCalendar *mrp_calendar_derive                   (const gchar *name,
Index: src/planner-calendar-dialog.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-calendar-dialog.c,v
retrieving revision 1.4
diff -u -b -B -p -r1.4 planner-calendar-dialog.c
--- src/planner-calendar-dialog.c	18 Jun 2004 04:47:12 -0000	1.4
+++ src/planner-calendar-dialog.c	19 Jun 2004 09:01:23 -0000
@@ -201,6 +201,228 @@ planner_cal_cmd_day_type (PlannerWindow 
 	return cmd_base;
 }
 
+typedef struct {
+	PlannerCmd   base;
+
+	MrpProject  *project;
+	MrpCalendar *calendar;
+	MrpCalendar *parent;
+	/* Resources that use this calendar */
+	GList       *resources;
+	/* Calendar that inherits from this calendar */
+	GList       *children;
+} CalCmdRemove;
+
+static gboolean
+cal_cmd_remove_do (PlannerCmd *cmd_base)
+{
+	GList *all_resources, *r;
+
+	CalCmdRemove *cmd = (CalCmdRemove*) cmd_base;
+
+	if (g_getenv ("PLANNER_DEBUG_UNDO_CAL")) {
+		g_message ("Removing a calendar ...");
+	}
+
+	all_resources = mrp_project_get_resources (cmd->project);
+	for (r = all_resources; r; r = r->next) {
+		MrpResource *resource = r->data;
+		MrpCalendar *tmp_cal;
+		
+		tmp_cal = mrp_resource_get_calendar (resource);
+		if (tmp_cal == cmd->calendar) {			
+			cmd->resources = g_list_append (cmd->resources, 
+							g_object_ref (resource));
+		} 
+	}
+
+	cmd->children = g_list_copy (mrp_calendar_get_children (cmd->calendar));
+	if (cmd->children) {
+		g_list_foreach (cmd->children, (GFunc) g_object_ref, NULL);
+	}
+		
+	mrp_calendar_remove (cmd->calendar);
+
+	return TRUE;
+}
+
+/* Reassign resources and calendar childs */
+static void
+cal_cmd_remove_undo (PlannerCmd *cmd_base)
+{
+	GList        *r;
+	GList        *c;
+	CalCmdRemove *cmd = (CalCmdRemove*) cmd_base;
+
+	if (g_getenv ("PLANNER_DEBUG_UNDO_CAL")) {
+		g_message ("Undo removing calendar ...");
+	}
+
+	mrp_calendar_add (cmd->calendar, cmd->parent);
+
+	if (cmd->resources != NULL) {
+		for (r = cmd->resources; r; r = r->next) {
+			mrp_resource_set_calendar (r->data, cmd->calendar);
+		}
+		g_list_foreach (cmd->resources, (GFunc) g_object_unref, NULL);
+		g_list_free (cmd->resources);
+		cmd->resources = NULL;
+	}
+
+	if (cmd->children != NULL) {
+		for (c = cmd->children; c; c = c->next) {
+			mrp_calendar_reparent (cmd->calendar, c->data);
+		}
+		g_list_foreach (cmd->children, (GFunc) g_object_unref, NULL);
+		g_list_free (cmd->children);
+		cmd->children = NULL;
+	}
+}
+
+
+static void
+cal_cmd_remove_free (PlannerCmd *cmd_base)
+{
+	CalCmdRemove *cmd = (CalCmdRemove*) cmd_base;
+
+	g_object_unref (cmd->calendar);
+	g_object_unref (cmd->project);
+
+	if (cmd->resources != NULL) {
+		g_list_foreach (cmd->resources, (GFunc) g_object_unref, NULL);
+		g_list_free (cmd->resources);
+		cmd->resources = NULL;
+	}
+	if (cmd->children != NULL) {
+		g_list_foreach (cmd->children, (GFunc) g_object_unref, NULL);
+		g_list_free (cmd->children);
+		cmd->children = NULL;
+	}
+}
+
+static PlannerCmd *
+planner_cal_cmd_remove (PlannerWindow  *main_window,
+			MrpProject     *project,
+			MrpCalendar    *calendar)
+{
+	PlannerCmd   *cmd_base;
+	CalCmdRemove *cmd;
+
+	cmd_base = planner_cmd_new (CalCmdRemove,
+				    _("Remove calendar"),
+				    cal_cmd_remove_do,
+				    cal_cmd_remove_undo,
+				    cal_cmd_remove_free);
+
+	cmd = (CalCmdRemove*) cmd_base;
+
+	cmd->project = g_object_ref (project);
+	cmd->calendar = g_object_ref (calendar);
+	cmd->parent = g_object_ref (mrp_calendar_get_parent (cmd->calendar));
+
+	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager 
+					   (main_window),
+					   cmd_base);
+	return cmd_base;
+}
+
+typedef struct {
+	PlannerCmd   base;
+
+	gchar       *name;
+	MrpProject  *project;
+	MrpCalendar *calendar;
+	MrpCalendar *parent;
+	MrpCalendar *copy;
+} CalCmdAdd;
+
+static gboolean
+cal_cmd_add_do (PlannerCmd *cmd_base)
+{
+	CalCmdAdd *cmd = (CalCmdAdd*) cmd_base;
+
+	if (g_getenv ("PLANNER_DEBUG_UNDO_CAL")) {
+		g_message ("Adding a new calendar ...");
+	}
+
+	if (cmd->calendar == NULL) {
+		if (cmd->parent != NULL && cmd->copy == NULL) {
+			cmd->calendar = mrp_calendar_derive (cmd->name, cmd->parent);
+			g_object_unref (cmd->parent);
+			cmd->parent = NULL;
+		} else if (cmd->parent == NULL && cmd->copy != NULL) {
+			cmd->calendar = mrp_calendar_copy (cmd->name, cmd->copy);
+			g_object_unref (cmd->copy);
+			cmd->copy = NULL;
+		} else if (cmd->parent == NULL && cmd->copy == NULL) {
+			cmd->calendar = mrp_calendar_new (cmd->name, cmd->project);
+			cmd->parent = g_object_ref (mrp_calendar_get_parent (cmd->calendar));
+		} else {
+			g_warning ("Incorrect use adding new calendar");
+		}
+		cmd->parent = g_object_ref (mrp_calendar_get_parent (cmd->calendar));
+	} else {
+		mrp_calendar_add (cmd->calendar, cmd->parent);
+	}
+
+	return TRUE;
+}
+
+static void
+cal_cmd_add_undo (PlannerCmd *cmd_base)
+{
+	CalCmdAdd *cmd = (CalCmdAdd*) cmd_base;
+
+	if (g_getenv ("PLANNER_DEBUG_UNDO_CAL")) {
+		g_message ("Undo adding calendar ...");
+	}
+	mrp_calendar_remove (cmd->calendar);
+}
+
+
+static void
+cal_cmd_add_free (PlannerCmd *cmd_base)
+{
+	CalCmdAdd *cmd = (CalCmdAdd*) cmd_base;
+
+	g_object_unref (cmd->calendar);
+	g_object_unref (cmd->parent);
+	g_object_unref (cmd->project);
+
+	g_free (cmd->name);
+}
+
+static PlannerCmd *
+planner_cal_cmd_add (PlannerWindow  *main_window,
+		     const gchar    *name,
+		     MrpCalendar    *parent,
+		     MrpCalendar    *copy)
+{
+	PlannerCmd *cmd_base;
+	CalCmdAdd  *cmd;
+
+	cmd_base = planner_cmd_new (CalCmdAdd,
+				    _("Add new calendar"),
+				    cal_cmd_add_do,
+				    cal_cmd_add_undo,
+				    cal_cmd_add_free);
+
+	cmd = (CalCmdAdd*) cmd_base;
+
+	cmd->project = g_object_ref (planner_window_get_project (main_window));
+	cmd->name = g_strdup (name);
+	if (parent != NULL) {
+		cmd->parent = g_object_ref (parent);
+	}
+	if (copy != NULL) {
+		cmd->copy = g_object_ref (copy);
+	}
+			
+	planner_cmd_manager_insert_and_do (planner_window_get_cmd_manager 
+					   (main_window),
+					   cmd_base);
+	return cmd_base;
+}
 
 static void
 cal_dialog_setup_option_menu (DialogData *data)
@@ -292,7 +514,8 @@ cal_dialog_response_cb (GtkWidget  *dial
 	
 	switch (response) {
 	case RESPONSE_REMOVE:
-		mrp_calendar_remove (calendar);
+		/* mrp_calendar_remove (calendar); */
+		planner_cal_cmd_remove (data->main_window, data->project, calendar);
 		break;
 
 	case RESPONSE_ADD:
@@ -1026,7 +1249,6 @@ cal_dialog_new_dialog_run (DialogData *d
 	GladeXML         *glade;
 	GtkWidget        *dialog;
 	MrpCalendar      *parent;
-	MrpCalendar      *calendar;
 	GtkTreeSelection *selection;
 	GtkWidget        *entry;
 	GtkWidget        *tree_view;
@@ -1081,13 +1303,17 @@ cal_dialog_new_dialog_run (DialogData *d
 		parent = cal_dialog_get_selected_calendar (GTK_TREE_VIEW (tree_view));
 
 		if (parent && gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (data->new_copy_radiobutton))) {
-			calendar = mrp_calendar_copy (name, parent);
+			/* calendar = mrp_calendar_copy (name, parent); */
+			planner_cal_cmd_add (data->main_window, name, NULL, parent);
 		}
 		else if (parent && gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (data->new_derive_radiobutton))) {
-			calendar = mrp_calendar_derive (name, parent);
+			/* calendar = mrp_calendar_derive (name, parent); */
+			planner_cal_cmd_add (data->main_window, name, parent, NULL);
 		}
 		else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (data->new_empty_radiobutton))) {
-			calendar = mrp_calendar_new (name, data->project);
+			/* calendar = mrp_calendar_new (name, data->project); */
+			planner_cal_cmd_add (data->main_window, name, NULL, NULL);
+			   
 		}
 	}
 


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