[evolution/gnome-2-28] Bug #555901 - Preserve Start/End/Due timezone when editing in list view



commit 4c1910f395d03843f54e776d4716661e6d77a04a
Author: Milan Crha <mcrha redhat com>
Date:   Mon Dec 14 09:55:23 2009 +0530

    Bug #555901 - Preserve Start/End/Due timezone when editing in list view
    
    This keeps the previously set timezone on the property (DTSTART/DTEND/DUE) and
    converts time properly from a timezone used for showing the time.

 calendar/gui/e-cal-model-calendar.c |   54 ++----------------------
 calendar/gui/e-cal-model-tasks.c    |   54 ++----------------------
 calendar/gui/e-cal-model.c          |   77 ++++++++++++++++++++++++++---------
 calendar/gui/e-cal-model.h          |    7 +++
 4 files changed, 72 insertions(+), 120 deletions(-)
---
diff --git a/calendar/gui/e-cal-model-calendar.c b/calendar/gui/e-cal-model-calendar.c
index fbf3235..b6ee14d 100644
--- a/calendar/gui/e-cal-model-calendar.c
+++ b/calendar/gui/e-cal-model-calendar.c
@@ -224,55 +224,9 @@ ecmc_value_at (ETableModel *etm, gint col, gint row)
 }
 
 static void
-set_dtend (ECalModelComponent *comp_data, gconstpointer value)
+set_dtend (ECalModel* model, ECalModelComponent *comp_data, gconstpointer value)
 {
-	ECellDateEditValue *dv = (ECellDateEditValue *) value;
-	icalproperty *prop;
-	icalparameter *param;
-	const gchar *tzid;
-
-	prop = icalcomponent_get_first_property (comp_data->icalcomp, ICAL_DTEND_PROPERTY);
-	if (prop)
-		param = icalproperty_get_first_parameter (prop, ICAL_TZID_PARAMETER);
-	else
-		param = NULL;
-
-	/* If we are setting the property to NULL (i.e. removing it), then
-	   we remove it if it exists. */
-	if (!dv) {
-		if (prop) {
-			icalcomponent_remove_property (comp_data->icalcomp, prop);
-			icalproperty_free (prop);
-		}
-
-		return;
-	}
-
-	/* If the TZID is set to "UTC", we set the is_utc flag. */
-	tzid = dv->zone ? icaltimezone_get_tzid (dv->zone) : "UTC";
-	if (tzid && !strcmp (tzid, "UTC"))
-		dv->tt.is_utc = 1;
-	else
-		dv->tt.is_utc = 0;
-
-	if (prop) {
-		icalproperty_set_dtend (prop, dv->tt);
-	} else {
-		prop = icalproperty_new_dtend (dv->tt);
-		icalcomponent_add_property (comp_data->icalcomp, prop);
-	}
-
-	/* If the TZID is set to "UTC", we don't want to save the TZID. */
-	if (tzid && strcmp (tzid, "UTC")) {
-		if (param) {
-			icalparameter_set_tzid (param, (gchar *) tzid);
-		} else {
-			param = icalparameter_new_tzid ((gchar *) tzid);
-			icalproperty_add_parameter (prop, param);
-		}
-	} else if (param) {
-		icalproperty_remove_parameter (prop, ICAL_TZID_PARAMETER);
-	}
+	e_cal_model_update_comp_time (model, comp_data, value, ICAL_DTEND_PROPERTY, icalproperty_set_dtend, icalproperty_new_dtend);
 }
 
 static void
@@ -371,7 +325,7 @@ ecmc_set_value_at (ETableModel *etm, gint col, gint row, gconstpointer value)
 
 	switch (col) {
 	case E_CAL_MODEL_CALENDAR_FIELD_DTEND :
-		set_dtend (comp_data, value);
+		set_dtend ((ECalModel *)model, comp_data, value);
 		break;
 	case E_CAL_MODEL_CALENDAR_FIELD_LOCATION :
 		set_location (comp_data, value);
@@ -559,7 +513,7 @@ ecmc_fill_component_from_model (ECalModel *model, ECalModelComponent *comp_data,
 	g_return_if_fail (comp_data != NULL);
 	g_return_if_fail (E_IS_TABLE_MODEL (source_model));
 
-	set_dtend (comp_data,
+	set_dtend (model, comp_data,
 		   e_table_model_value_at (source_model, E_CAL_MODEL_CALENDAR_FIELD_DTEND, row));
 	set_location (comp_data,
 		      e_table_model_value_at (source_model, E_CAL_MODEL_CALENDAR_FIELD_LOCATION, row));
diff --git a/calendar/gui/e-cal-model-tasks.c b/calendar/gui/e-cal-model-tasks.c
index d951d13..985cf66 100644
--- a/calendar/gui/e-cal-model-tasks.c
+++ b/calendar/gui/e-cal-model-tasks.c
@@ -562,55 +562,9 @@ set_complete (ECalModelComponent *comp_data, gconstpointer value)
 }
 
 static void
-set_due (ECalModelComponent *comp_data, gconstpointer value)
+set_due (ECalModel* model, ECalModelComponent *comp_data, gconstpointer value)
 {
-	ECellDateEditValue *dv = (ECellDateEditValue *) value;
-	icalproperty *prop;
-	icalparameter *param;
-	const gchar *tzid;
-
-	prop = icalcomponent_get_first_property (comp_data->icalcomp, ICAL_DUE_PROPERTY);
-	if (prop)
-		param = icalproperty_get_first_parameter (prop, ICAL_TZID_PARAMETER);
-	else
-		param = NULL;
-
-	/* If we are setting the property to NULL (i.e. removing it), then
-	   we remove it if it exists. */
-	if (!dv) {
-		if (prop) {
-			icalcomponent_remove_property (comp_data->icalcomp, prop);
-			icalproperty_free (prop);
-		}
-
-		return;
-	}
-
-	/* If the TZID is set to "UTC", we set the is_utc flag. */
-	tzid = dv->zone ? icaltimezone_get_tzid (dv->zone) : "UTC";
-	if (tzid && !strcmp (tzid, "UTC"))
-		dv->tt.is_utc = 1;
-	else
-		dv->tt.is_utc = 0;
-
-	if (prop) {
-		icalproperty_set_due (prop, dv->tt);
-	} else {
-		prop = icalproperty_new_due (dv->tt);
-		icalcomponent_add_property (comp_data->icalcomp, prop);
-	}
-
-	/* If the TZID is set to "UTC", we don't want to save the TZID. */
-	if (tzid && strcmp (tzid, "UTC")) {
-		if (param) {
-			icalparameter_set_tzid (param, (gchar *) tzid);
-		} else {
-			param = icalparameter_new_tzid ((gchar *) tzid);
-			icalproperty_add_parameter (prop, param);
-		}
-	} else if (param) {
-		icalproperty_remove_parameter (prop, ICAL_TZID_PARAMETER);
-	}
+	e_cal_model_update_comp_time (model, comp_data, value, ICAL_DUE_PROPERTY, icalproperty_set_due, icalproperty_new_due);
 }
 
 /* FIXME: We need to set the "transient_for" property for the dialog, but the
@@ -827,7 +781,7 @@ ecmt_set_value_at (ETableModel *etm, gint col, gint row, gconstpointer value)
 		set_complete (comp_data, value);
 		break;
 	case E_CAL_MODEL_TASKS_FIELD_DUE :
-		set_due (comp_data, value);
+		set_due ((ECalModel*) model, comp_data, value);
 		break;
 	case E_CAL_MODEL_TASKS_FIELD_GEO :
 		set_geo (comp_data, value);
@@ -1093,7 +1047,7 @@ ecmt_fill_component_from_model (ECalModel *model, ECalModelComponent *comp_data,
 			set_status (comp_data, e_table_model_value_at (source_model, E_CAL_MODEL_TASKS_FIELD_STATUS, row));
 	}
 
-	set_due (comp_data,
+	set_due (model, comp_data,
 		 e_table_model_value_at (source_model, E_CAL_MODEL_TASKS_FIELD_DUE, row));
 	set_geo (comp_data,
 		 e_table_model_value_at (source_model, E_CAL_MODEL_TASKS_FIELD_GEO, row));
diff --git a/calendar/gui/e-cal-model.c b/calendar/gui/e-cal-model.c
index b212dcf..42b3241 100644
--- a/calendar/gui/e-cal-model.c
+++ b/calendar/gui/e-cal-model.c
@@ -678,14 +678,45 @@ set_description (ECalModelComponent *comp_data, const gchar *value)
 }
 
 static void
-set_dtstart (ECalModel *model, ECalModelComponent *comp_data, gconstpointer value)
+datetime_to_zone (ECal *client, struct icaltimetype *tt, icaltimezone *tt_zone, const gchar *tzid)
 {
-	ECellDateEditValue *dv = (ECellDateEditValue *) value;
+	icaltimezone *from, *to;
+	const gchar *tt_tzid = NULL;
+
+	g_return_if_fail (tt != NULL);
+
+	if (tt_zone)
+		tt_tzid = icaltimezone_get_tzid (tt_zone);
+
+	if (tt_tzid == NULL || tzid == NULL ||
+	    tt_tzid == tzid || g_str_equal (tt_tzid, tzid))
+		return;
+
+	from = tt_zone;
+	to = icaltimezone_get_builtin_timezone_from_tzid (tzid);
+	if (!to) {
+		/* do not check failure here, maybe the zone is not available there */
+		e_cal_get_timezone (client, tzid, &to, NULL);
+	}
+
+	icaltimezone_convert_time (tt, from, to);
+}
+
+/* updates time in a component, and keeps the timezone used in it, if exists */
+void
+e_cal_model_update_comp_time (ECalModel *model, ECalModelComponent *comp_data, gconstpointer time_value, icalproperty_kind kind, void (*set_func)(icalproperty *prop, struct icaltimetype v), icalproperty * (*new_func)(struct icaltimetype v))
+{
+	ECellDateEditValue *dv = (ECellDateEditValue *) time_value;
 	icalproperty *prop;
 	icalparameter *param;
-	const gchar *tzid;
+	struct icaltimetype tt;
 
-	prop = icalcomponent_get_first_property (comp_data->icalcomp, ICAL_DTSTART_PROPERTY);
+	g_return_if_fail (model != NULL);
+	g_return_if_fail (comp_data != NULL);
+	g_return_if_fail (set_func != NULL);
+	g_return_if_fail (new_func != NULL);
+
+	prop = icalcomponent_get_first_property (comp_data->icalcomp, kind);
 	if (prop)
 		param = icalproperty_get_first_parameter (prop, ICAL_TZID_PARAMETER);
 	else
@@ -702,34 +733,40 @@ set_dtstart (ECalModel *model, ECalModelComponent *comp_data, gconstpointer valu
 		return;
 	}
 
-	/* If the TZID is set to "UTC", we set the is_utc flag. */
-	tzid = dv->zone ? icaltimezone_get_tzid (dv->zone) : "UTC";
-	if (tzid && !strcmp (tzid, "UTC"))
-		dv->tt.is_utc = 1;
-	else
-		dv->tt.is_utc = 0;
+	tt = dv->tt;
+	datetime_to_zone (comp_data->client, &tt, e_cal_model_get_timezone (model), param ? icalparameter_get_tzid (param) : NULL);
 
 	if (prop) {
-		icalproperty_set_dtstart (prop, dv->tt);
+		set_func (prop, tt);
 	} else {
-		prop = icalproperty_new_dtstart (dv->tt);
+		prop = new_func (tt);
 		icalcomponent_add_property (comp_data->icalcomp, prop);
 	}
 
-	/* If the TZID is set to "UTC", we don't want to save the TZID. */
-	if (tzid && strcmp (tzid, "UTC")) {
-		if (param) {
-			icalparameter_set_tzid (param, (gchar *) tzid);
+	if (param) {
+		const gchar *tzid = icalparameter_get_tzid (param);
+
+		/* If the TZID is set to "UTC", we don't want to save the TZID. */
+		if (tzid && strcmp (tzid, "UTC")) {
+			if (param) {
+				icalparameter_set_tzid (param, (gchar *) tzid);
+			} else {
+				param = icalparameter_new_tzid ((gchar *) tzid);
+				icalproperty_add_parameter (prop, param);
+			}
 		} else {
-			param = icalparameter_new_tzid ((gchar *) tzid);
-			icalproperty_add_parameter (prop, param);
+			icalproperty_remove_parameter (prop, ICAL_TZID_PARAMETER);
 		}
-	} else if (param) {
-		icalproperty_remove_parameter (prop, ICAL_TZID_PARAMETER);
 	}
 }
 
 static void
+set_dtstart (ECalModel *model, ECalModelComponent *comp_data, gconstpointer value)
+{
+	e_cal_model_update_comp_time (model, comp_data, value, ICAL_DTSTART_PROPERTY, icalproperty_set_dtstart, icalproperty_new_dtstart);
+}
+
+static void
 set_summary (ECalModelComponent *comp_data, const gchar *value)
 {
 	icalproperty *prop;
diff --git a/calendar/gui/e-cal-model.h b/calendar/gui/e-cal-model.h
index adaf736..95cfd83 100644
--- a/calendar/gui/e-cal-model.h
+++ b/calendar/gui/e-cal-model.h
@@ -192,6 +192,13 @@ gboolean e_cal_model_test_row_editable (ECalModel *model, gint row);
 
 void e_cal_model_set_default_time_func (ECalModel *model, ECalModelDefaultTimeFunc func, gpointer user_data);
 
+void		e_cal_model_update_comp_time	(ECalModel *model,
+						 ECalModelComponent *comp_data,
+						 gconstpointer time_value,
+						 icalproperty_kind kind,
+						 void (*set_func)(icalproperty *prop, struct icaltimetype v),
+						 icalproperty * (*new_func)(struct icaltimetype v));
+
 G_END_DECLS
 
 #endif



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