diff --git a/calendar/ChangeLog b/calendar/ChangeLog
index c75fad7..b809e12 100644
--- a/calendar/ChangeLog
+++ b/calendar/ChangeLog
@@ -1,3 +1,16 @@
+2009-03-09  Chenthill Palanisamy  <pchenthill@novell.com>
+
+	Fixes #561312
+	* calendar/gui/comp-util.c:
+	* calendar/gui/comp-util.h: Added a new function to
+	sanitize master recurrence event before modifying 
+	all instances.
+	* calendar/gui/dialogs/comp-editor.c:
+	* calendar/gui/e-day-view.c:
+	* calendar/gui/e-week-view.c: Used the new util api.
+	Do not invoke recurrence dialog while modifying detached
+	instances.
+
 2009-03-06  Milan Crha  <mcrha@redhat.com>
 
 	** Fix for bug #401346
diff --git a/calendar/gui/comp-util.c b/calendar/gui/comp-util.c
index 9a58a8c..57704da 100644
--- a/calendar/gui/comp-util.c
+++ b/calendar/gui/comp-util.c
@@ -796,3 +796,62 @@ cal_comp_process_source_list_drop (ECal *destination, icalcomponent *comp, GdkDr
 
 	return success;
 }
+
+void
+comp_util_sanitize_recurrence_master (ECalComponent *comp, ECal *client)
+{
+	ECalComponent *master = NULL;
+	icalcomponent *icalcomp = NULL;
+	ECalComponentRange rid;
+       	ECalComponentDateTime sdt;	
+	const char *uid;
+
+	/* Get the master component */
+	e_cal_component_get_uid (comp, &uid);
+	if (!e_cal_get_object (client, uid, NULL, &icalcomp, NULL)) {
+		g_warning ("Unable to get the master component \n");
+		return;
+	}
+
+	master = e_cal_component_new ();
+	e_cal_component_set_icalcomponent (master, icalcomp);
+
+	/* Compare recur id and start date */
+	e_cal_component_get_recurid (comp, &rid);
+	e_cal_component_get_dtstart (comp, &sdt);
+
+	if (icaltime_compare_date_only (*rid.datetime.value, *sdt.value) == 0)
+	{
+		ECalComponentDateTime msdt, medt, edt;
+		int *sequence;
+
+		e_cal_component_get_dtstart (master, &msdt);
+		e_cal_component_get_dtend (master, &medt);
+
+		e_cal_component_get_dtend (comp, &edt);
+
+		sdt.value->year = msdt.value->year;
+		sdt.value->month = msdt.value->month;
+		sdt.value->day = msdt.value->day;
+
+		edt.value->year = medt.value->year;
+		edt.value->month = medt.value->month;
+		edt.value->day = medt.value->day;
+
+		e_cal_component_set_dtstart (comp, &sdt);
+		e_cal_component_set_dtend (comp, &edt);
+		
+		e_cal_component_get_sequence (master, &sequence);
+		e_cal_component_set_sequence (comp, sequence);
+
+		e_cal_component_free_datetime (&msdt);
+		e_cal_component_free_datetime (&medt);
+		e_cal_component_free_datetime (&edt);
+	}
+
+	e_cal_component_free_datetime (&sdt);
+	e_cal_component_free_range (&rid);
+	e_cal_component_set_recurid (comp, NULL);
+
+	g_object_unref (master);
+}
diff --git a/calendar/gui/comp-util.h b/calendar/gui/comp-util.h
index 3225557..b93384f 100644
--- a/calendar/gui/comp-util.h
+++ b/calendar/gui/comp-util.h
@@ -58,5 +58,6 @@ void cal_comp_set_dtstart_with_oldzone (ECal *client, ECalComponent *comp, const
 void cal_comp_set_dtend_with_oldzone (ECal *client, ECalComponent *comp, const ECalComponentDateTime *pdate);
 
 gboolean cal_comp_process_source_list_drop (ECal *destination, icalcomponent *comp, GdkDragAction action, const char *source_uid, ESourceList *source_list);
+void comp_util_sanitize_recurrence_master (ECalComponent *comp, ECal *client);
 
 #endif
diff --git a/calendar/gui/dialogs/comp-editor.c b/calendar/gui/dialogs/comp-editor.c
index 93ae0c6..7a083a3 100644
--- a/calendar/gui/dialogs/comp-editor.c
+++ b/calendar/gui/dialogs/comp-editor.c
@@ -799,6 +799,11 @@ save_comp (CompEditor *editor)
 		if (result)
 			g_signal_emit_by_name (editor, "object_created");
 	} else {
+
+		if (e_cal_component_has_recurrences (priv->comp) && priv->mod == CALOBJ_MOD_ALL)
+			comp_util_sanitize_recurrence_master (priv->comp, priv->client);
+
+
 		if (priv->mod == CALOBJ_MOD_THIS) {
 			e_cal_component_set_rdate_list (priv->comp, NULL);
 			e_cal_component_set_rrule_list (priv->comp, NULL);
@@ -1159,11 +1164,15 @@ action_save_cb (GtkAction *action,
 	}
 
 	commit_all_fields (editor);
-	if (e_cal_component_is_instance (priv->comp))
+	if (e_cal_component_has_recurrences (priv->comp)) {
 		if (!recur_component_dialog (priv->client, priv->comp, &priv->mod, GTK_WINDOW (editor), delegated))
 			return;
 
+	} else if (e_cal_component_is_instance (priv->comp))
+		priv->mod = CALOBJ_MOD_THIS;
+
 	comp = comp_editor_get_current_comp (editor, &correct);
+		
 	e_cal_component_get_summary (comp, &text);
 	g_object_unref (comp);
 
diff --git a/calendar/gui/e-day-view.c b/calendar/gui/e-day-view.c
index 91da668..af812ed 100644
--- a/calendar/gui/e-day-view.c
+++ b/calendar/gui/e-day-view.c
@@ -4022,12 +4022,15 @@ e_day_view_finish_long_event_resize (EDayView *day_view)
 	}
 
 	e_cal_component_commit_sequence (comp);
- 	if (e_cal_component_is_instance (comp)) {
+ 	if (e_cal_component_has_recurrences (comp)) {
  		if (!recur_component_dialog (client, comp, &mod, NULL, FALSE)) {
  			gtk_widget_queue_draw (day_view->top_canvas);
 			goto out;
  		}
 
+		if (mod == CALOBJ_MOD_ALL)
+			comp_util_sanitize_recurrence_master (comp, client);
+
 		if (mod == CALOBJ_MOD_THIS) {
 			/* set the correct DTSTART/DTEND on the individual recurrence */
 			if (day_view->resize_drag_pos == E_CALENDAR_VIEW_POS_TOP_EDGE) {
@@ -4049,7 +4052,8 @@ e_day_view_finish_long_event_resize (EDayView *day_view)
 
 			e_cal_component_commit_sequence (comp);
 		}
-	}
+	} else if (e_cal_component_is_instance (comp))
+		mod = CALOBJ_MOD_THIS;
 
 	toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (day_view)));
 	e_calendar_view_modify_and_send (comp, client, mod, toplevel, TRUE);
@@ -4129,11 +4133,14 @@ e_day_view_finish_resize (EDayView *day_view)
 
 	day_view->resize_drag_pos = E_CALENDAR_VIEW_POS_NONE;
 
- 	if (e_cal_component_is_instance (comp)) {
+ 	if (e_cal_component_has_recurrences (comp)) {
  		if (!recur_component_dialog (client, comp, &mod, NULL, FALSE)) {
  			gtk_widget_queue_draw (day_view->top_canvas);
 			goto out;
  		}
+				
+		if (mod == CALOBJ_MOD_ALL)
+			comp_util_sanitize_recurrence_master (comp, client);
 
 		if (mod == CALOBJ_MOD_THIS) {
 			/* set the correct DTSTART/DTEND on the individual recurrence */
@@ -4154,7 +4161,8 @@ e_day_view_finish_resize (EDayView *day_view)
 			e_cal_component_set_exdate_list (comp, NULL);
 			e_cal_component_set_exrule_list (comp, NULL);
 		}
-	}
+	} else if (e_cal_component_is_instance (comp))
+		mod = CALOBJ_MOD_THIS;
 
 	toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (day_view)));
 
@@ -6138,19 +6146,23 @@ e_day_view_change_event_time (EDayView *day_view, time_t start_dt, time_t end_dt
 
 	day_view->resize_drag_pos = E_CALENDAR_VIEW_POS_NONE;
 
- 	if (e_cal_component_is_instance (comp)) {
+ 	if (e_cal_component_has_recurrences (comp)) {
  		if (!recur_component_dialog (client, comp, &mod, NULL, FALSE)) {
  			gtk_widget_queue_draw (day_view->top_canvas);
 			goto out;
  		}
 
+		if (mod == CALOBJ_MOD_ALL)
+			comp_util_sanitize_recurrence_master (comp, client);
+
 		if (mod == CALOBJ_MOD_THIS) {
 			e_cal_component_set_rdate_list (comp, NULL);
 			e_cal_component_set_rrule_list (comp, NULL);
 			e_cal_component_set_exdate_list (comp, NULL);
 			e_cal_component_set_exrule_list (comp, NULL);
 		}
-	}
+	} else if (e_cal_component_is_instance (comp))
+		mod = CALOBJ_MOD_THIS;
 
 	toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (day_view)));
 
@@ -6371,11 +6383,14 @@ e_day_view_on_editing_stopped (EDayView *day_view,
 		} else {
 			CalObjModType mod = CALOBJ_MOD_ALL;
 			GtkWindow *toplevel;
-			if (e_cal_component_is_instance (comp)) {
+			if (e_cal_component_has_recurrences (comp)) {
 				if (!recur_component_dialog (client, comp, &mod, NULL, FALSE)) {
 					goto out;
 				}
 
+				if (mod == CALOBJ_MOD_ALL)
+					comp_util_sanitize_recurrence_master (comp, client);
+
 				if (mod == CALOBJ_MOD_THIS) {
 					ECalComponentDateTime olddt, dt;
 					icaltimetype itt;
@@ -6423,7 +6438,8 @@ e_day_view_on_editing_stopped (EDayView *day_view,
 
 					e_cal_component_commit_sequence (comp);
 				}
-			}
+			} else if (e_cal_component_is_instance (comp))
+				mod = CALOBJ_MOD_THIS;
 
 			/* FIXME When sending here, what exactly should we send? */
 			toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (day_view)));
@@ -7524,11 +7540,14 @@ e_day_view_on_top_canvas_drag_data_received  (GtkWidget          *widget,
 				gnome_canvas_item_show (event->canvas_item);
 
 			e_cal_component_commit_sequence (comp);
-			if (e_cal_component_is_instance (comp)) {
+			if (e_cal_component_has_recurrences (comp)) {
 				if (!recur_component_dialog (client, comp, &mod, NULL, FALSE)) {
 					g_object_unref (comp);
 					return;
 				}
+				
+				if (mod == CALOBJ_MOD_ALL)
+					comp_util_sanitize_recurrence_master (comp, client);
 
 				if (mod == CALOBJ_MOD_THIS) {
 					e_cal_component_set_rdate_list (comp, NULL);
@@ -7536,7 +7555,8 @@ e_day_view_on_top_canvas_drag_data_received  (GtkWidget          *widget,
 					e_cal_component_set_exdate_list (comp, NULL);
 					e_cal_component_set_exrule_list (comp, NULL);
 				}
-			}
+			} else if (e_cal_component_is_instance (comp))
+				mod = CALOBJ_MOD_THIS;
 
 			toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (day_view)));
 			e_calendar_view_modify_and_send (comp, client, mod, toplevel, FALSE);
@@ -7726,19 +7746,23 @@ e_day_view_on_main_canvas_drag_data_received  (GtkWidget          *widget,
 				gnome_canvas_item_show (event->canvas_item);
 
 			e_cal_component_commit_sequence (comp);
-			if (e_cal_component_is_instance (comp)) {
+			if (e_cal_component_has_recurrences (comp)) {
 				if (!recur_component_dialog (client, comp, &mod, NULL, FALSE)) {
 					g_object_unref (comp);
 					return;
 				}
 
+				if (mod == CALOBJ_MOD_ALL)
+					comp_util_sanitize_recurrence_master (comp, client);
+
 				if (mod == CALOBJ_MOD_THIS) {
 					e_cal_component_set_rdate_list (comp, NULL);
 					e_cal_component_set_rrule_list (comp, NULL);
 					e_cal_component_set_exdate_list (comp, NULL);
 					e_cal_component_set_exrule_list (comp, NULL);
 				}
-			}
+			} else if (e_cal_component_is_instance (comp))
+				mod = CALOBJ_MOD_THIS;
 
 			toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (day_view)));
 			e_calendar_view_modify_and_send (comp, client, mod, toplevel, FALSE);
diff --git a/calendar/gui/e-week-view.c b/calendar/gui/e-week-view.c
index 37d7dbe..3109094 100644
--- a/calendar/gui/e-week-view.c
+++ b/calendar/gui/e-week-view.c
@@ -3445,19 +3445,23 @@ e_week_view_change_event_time (EWeekView *week_view, time_t start_dt, time_t end
 	week_view->last_edited_comp_string = e_cal_component_get_as_string (comp);
 
 
- 	if (e_cal_component_is_instance (comp)) {
+ 	if (e_cal_component_has_recurrences (comp)) {
  		if (!recur_component_dialog (client, comp, &mod, NULL, FALSE)) {
  			gtk_widget_queue_draw (week_view->main_canvas);
 			goto out;
  		}
 
+		if (mod == CALOBJ_MOD_ALL)
+			comp_util_sanitize_recurrence_master (comp, client);
+
 		if (mod == CALOBJ_MOD_THIS) {
 			e_cal_component_set_rdate_list (comp, NULL);
 			e_cal_component_set_rrule_list (comp, NULL);
 			e_cal_component_set_exdate_list (comp, NULL);
 			e_cal_component_set_exrule_list (comp, NULL);
 		}
-	}
+	} else if (e_cal_component_is_instance (comp))
+		mod = CALOBJ_MOD_THIS;
 
 	toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (week_view)));
 
@@ -3578,10 +3582,13 @@ e_week_view_on_editing_stopped (EWeekView *week_view,
 			CalObjModType mod = CALOBJ_MOD_ALL;
 			GtkWindow *toplevel;
 
-			if (e_cal_component_is_instance (comp)) {
+			if (e_cal_component_has_recurrences (comp)) {
 				if (!recur_component_dialog (client, comp, &mod, NULL, FALSE)) {
 					goto out;
 				}
+		
+				if (mod == CALOBJ_MOD_ALL)
+					comp_util_sanitize_recurrence_master (comp, client);
 
 				if (mod == CALOBJ_MOD_THIS) {
 					ECalComponentDateTime dt;
@@ -3633,7 +3640,8 @@ e_week_view_on_editing_stopped (EWeekView *week_view,
 
 					e_cal_component_commit_sequence (comp);
 				}
-			}
+			} else if (e_cal_component_is_instance (comp))
+				mod = CALOBJ_MOD_THIS;
 
 			/* FIXME When sending here, what exactly should we send? */
 			toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (week_view)));
