Blame SOURCES/evolution-data-server-3.12.11-weather-calendar-merge-day-forecasts.patch

5e6360
diff -up evolution-data-server-3.12.11/calendar/backends/weather/e-cal-backend-weather.c.weather-calendar-merge-day-forecasts evolution-data-server-3.12.11/calendar/backends/weather/e-cal-backend-weather.c
5e6360
--- evolution-data-server-3.12.11/calendar/backends/weather/e-cal-backend-weather.c.weather-calendar-merge-day-forecasts	2014-03-24 10:07:52.000000000 +0100
5e6360
+++ evolution-data-server-3.12.11/calendar/backends/weather/e-cal-backend-weather.c	2015-05-21 16:07:41.897961486 +0200
5e6360
@@ -46,7 +46,8 @@ static ECalComponent *
5e6360
 		create_weather			(ECalBackendWeather *cbw,
5e6360
 						 GWeatherInfo *report,
5e6360
 						 GWeatherTemperatureUnit unit,
5e6360
-						 gboolean is_forecast);
5e6360
+						 gboolean is_forecast,
5e6360
+						 GSList *same_day_forecasts);
5e6360
 static void	e_cal_backend_weather_add_timezone
5e6360
 						(ECalBackendSync *backend,
5e6360
 						 EDataCal *cal,
5e6360
@@ -157,6 +158,27 @@ put_component_to_store (ECalBackendWeath
5e6360
 	e_cal_backend_store_put_component_with_time_range (priv->store, comp, time_start, time_end);
5e6360
 }
5e6360
 
5e6360
+static gint
5e6360
+compare_weather_info_by_date (gconstpointer a,
5e6360
+			      gconstpointer b)
5e6360
+{
5e6360
+	GWeatherInfo *nfoa = (GWeatherInfo *) a, *nfob = (GWeatherInfo *) b;
5e6360
+
5e6360
+	if (nfoa && nfob) {
5e6360
+		time_t tma, tmb;
5e6360
+
5e6360
+		if (!gweather_info_get_value_update (nfoa, &tma))
5e6360
+			tma = 0;
5e6360
+
5e6360
+		if (!gweather_info_get_value_update (nfob, &tmb))
5e6360
+			tmb = 0;
5e6360
+
5e6360
+		return tma == tmb ? 0 : (tma < tmb ? -1 : 1);
5e6360
+	}
5e6360
+
5e6360
+	return nfoa == nfob ? 0 : (nfoa ? 1 : -1);
5e6360
+}
5e6360
+
5e6360
 static void
5e6360
 finished_retrieval_cb (GWeatherInfo *info,
5e6360
                        ECalBackendWeather *cbw)
5e6360
@@ -202,31 +224,83 @@ finished_retrieval_cb (GWeatherInfo *inf
5e6360
 	g_slist_free (comps);
5e6360
 	e_cal_backend_store_clean (priv->store);
5e6360
 
5e6360
-	comp = create_weather (cbw, info, unit, FALSE);
5e6360
+	comp = create_weather (cbw, info, unit, FALSE, NULL);
5e6360
 	if (comp) {
5e6360
-		GSList *forecasts;
5e6360
+		GSList *orig_forecasts;
5e6360
 
5e6360
 		put_component_to_store (cbw, comp);
5e6360
 		e_cal_backend_notify_component_created (E_CAL_BACKEND (cbw), comp);
5e6360
 		g_object_unref (comp);
5e6360
 
5e6360
-		forecasts = gweather_info_get_forecast_list (info);
5e6360
-		if (forecasts) {
5e6360
-			GSList *f;
5e6360
+		orig_forecasts = gweather_info_get_forecast_list (info);
5e6360
+		if (orig_forecasts) {
5e6360
+			time_t info_day = 0;
5e6360
+			GSList *forecasts, *f;
5e6360
+
5e6360
+			if (!gweather_info_get_value_update (info, &info_day))
5e6360
+				info_day = 0;
5e6360
+
5e6360
+			info_day = info_day / (24 * 60 * 60);
5e6360
 
5e6360
 			/* skip the first one, it's for today, which has been added above */
5e6360
-			for (f = forecasts->next; f; f = f->next) {
5e6360
+			forecasts = g_slist_copy (orig_forecasts->next);
5e6360
+			forecasts = g_slist_sort (forecasts, compare_weather_info_by_date);
5e6360
+
5e6360
+			f = forecasts;
5e6360
+			while (f) {
5e6360
+				time_t current_day;
5e6360
 				GWeatherInfo *nfo = f->data;
5e6360
 
5e6360
-				if (nfo) {
5e6360
-					comp = create_weather (cbw, nfo, unit, TRUE);
5e6360
+				f = g_slist_next (f);
5e6360
+
5e6360
+				if (nfo && gweather_info_get_value_update (nfo, &current_day)) {
5e6360
+					GSList *same_day_forecasts = NULL;
5e6360
+					gint current_hour;
5e6360
+
5e6360
+					current_hour = current_day % (24 * 60 * 60);
5e6360
+					current_day = current_day / (24 * 60 * 60);
5e6360
+
5e6360
+					if (current_day == info_day)
5e6360
+						continue;
5e6360
+
5e6360
+					while (f) {
5e6360
+						GWeatherInfo *test_nfo = f->data;
5e6360
+						time_t test_day;
5e6360
+
5e6360
+						if (test_nfo && gweather_info_get_value_update (test_nfo, &test_day)) {
5e6360
+							time_t test_hour;
5e6360
+
5e6360
+							test_hour = test_day % (24 * 60 * 60);
5e6360
+
5e6360
+							if (test_day / (24 * 60 * 60) != current_day)
5e6360
+								break;
5e6360
+
5e6360
+							same_day_forecasts = g_slist_prepend (same_day_forecasts, test_nfo);
5e6360
+
5e6360
+							/* Use the main GWeatherInfo the one closest to noon */
5e6360
+							if (ABS (test_hour - (12 * 60 * 60)) < ABS (current_hour - (12 * 60 * 60))) {
5e6360
+								nfo = test_nfo;
5e6360
+								current_hour = test_hour;
5e6360
+							}
5e6360
+						}
5e6360
+
5e6360
+						f = g_slist_next (f);
5e6360
+					}
5e6360
+
5e6360
+					same_day_forecasts = g_slist_reverse (same_day_forecasts);
5e6360
+
5e6360
+					comp = create_weather (cbw, nfo, unit, TRUE, same_day_forecasts);
5e6360
 					if (comp) {
5e6360
 						put_component_to_store (cbw, comp);
5e6360
 						e_cal_backend_notify_component_created (E_CAL_BACKEND (cbw), comp);
5e6360
 						g_object_unref (comp);
5e6360
 					}
5e6360
+
5e6360
+					g_slist_free (same_day_forecasts);
5e6360
 				}
5e6360
 			}
5e6360
+
5e6360
+			g_slist_free (forecasts);
5e6360
 		}
5e6360
 	}
5e6360
 
5e6360
@@ -341,11 +415,53 @@ cal_backend_weather_get_temp (gdouble va
5e6360
 	return g_strdup_printf (_("%.1f"), value);
5e6360
 }
5e6360
 
5e6360
+static gchar *
5e6360
+describe_forecast (GWeatherInfo *nfo,
5e6360
+		   GWeatherTemperatureUnit unit)
5e6360
+{
5e6360
+	gchar *str, *date, *summary, *temp;
5e6360
+	gdouble tmin = 0.0, tmax = 0.0, temp1 = 0.0;
5e6360
+
5e6360
+	date = gweather_info_get_update (nfo);
5e6360
+	summary = gweather_info_get_conditions (nfo);
5e6360
+	if (g_str_equal (summary, "-")) {
5e6360
+	    g_free (summary);
5e6360
+	    summary = gweather_info_get_sky (nfo);
5e6360
+	}
5e6360
+
5e6360
+	if (gweather_info_get_value_temp_min (nfo, unit, &tmin) &&
5e6360
+	    gweather_info_get_value_temp_max (nfo, unit, &tmax) &&
5e6360
+	    tmin != tmax) {
5e6360
+		gchar *min, *max;
5e6360
+
5e6360
+		min = cal_backend_weather_get_temp (tmin, unit);
5e6360
+		max = cal_backend_weather_get_temp (tmax, unit);
5e6360
+
5e6360
+		temp = g_strdup_printf ("%s / %s", min, max);
5e6360
+
5e6360
+		g_free (min);
5e6360
+		g_free (max);
5e6360
+	} else if (gweather_info_get_value_temp (nfo, unit, &temp1)) {
5e6360
+		temp = cal_backend_weather_get_temp (temp1, unit);
5e6360
+	} else {
5e6360
+		temp = gweather_info_get_temp (nfo);
5e6360
+	}
5e6360
+
5e6360
+	str = g_strdup_printf (" * %s: %s, %s", date, summary, temp);
5e6360
+
5e6360
+	g_free (date);
5e6360
+	g_free (summary);
5e6360
+	g_free (temp);
5e6360
+
5e6360
+	return str;
5e6360
+}
5e6360
+
5e6360
 static ECalComponent *
5e6360
 create_weather (ECalBackendWeather *cbw,
5e6360
                 GWeatherInfo *report,
5e6360
                 GWeatherTemperatureUnit unit,
5e6360
-                gboolean is_forecast)
5e6360
+                gboolean is_forecast,
5e6360
+		GSList *same_day_forecasts)
5e6360
 {
5e6360
 	ECalComponent             *cal_comp;
5e6360
 	ECalComponentText          comp_summary;
5e6360
@@ -353,7 +469,7 @@ create_weather (ECalBackendWeather *cbw,
5e6360
 	struct icaltimetype        itt;
5e6360
 	ECalComponentDateTime      dt;
5e6360
 	gchar			  *uid;
5e6360
-	GSList                    *text_list = NULL;
5e6360
+	GSList                    *text_list = NULL, *link;
5e6360
 	ECalComponentText         *description;
5e6360
 	gchar                     *tmp, *city_name;
5e6360
 	time_t			   update_time;
5e6360
@@ -435,12 +551,12 @@ create_weather (ECalBackendWeather *cbw,
5e6360
 	e_cal_component_set_summary (cal_comp, &comp_summary);
5e6360
 	g_free ((gchar *) comp_summary.value);
5e6360
 
5e6360
-	tmp = gweather_info_get_forecast (report);
5e6360
 	comp_summary.value = gweather_info_get_weather_summary (report);
5e6360
 
5e6360
 	description = g_new0 (ECalComponentText, 1);
5e6360
 	{
5e6360
 		GString *builder;
5e6360
+		gboolean has_forecast_word = FALSE;
5e6360
 
5e6360
 		builder = g_string_new (NULL);
5e6360
 
5e6360
@@ -448,12 +564,60 @@ create_weather (ECalBackendWeather *cbw,
5e6360
 			g_string_append (builder, comp_summary.value);
5e6360
 			g_string_append_c (builder, '\n');
5e6360
 		}
5e6360
-		if (tmp) {
5e6360
-			g_string_append (builder, _("Forecast"));
5e6360
-			g_string_append_c (builder, ':');
5e6360
-			if (!is_forecast)
5e6360
-				g_string_append_c (builder, '\n');
5e6360
-			g_string_append (builder, tmp);
5e6360
+
5e6360
+		tmp = NULL;
5e6360
+
5e6360
+		for (link = gweather_info_get_forecast_list (report); link; link = g_slist_next (link)) {
5e6360
+			GWeatherInfo *nfo = link->data;
5e6360
+
5e6360
+			if (nfo) {
5e6360
+				tmp = describe_forecast (nfo, unit);
5e6360
+
5e6360
+				if (tmp && *tmp) {
5e6360
+					if (!has_forecast_word) {
5e6360
+						has_forecast_word = TRUE;
5e6360
+
5e6360
+						g_string_append (builder, _("Forecast"));
5e6360
+						g_string_append_c (builder, ':');
5e6360
+						g_string_append_c (builder, '\n');
5e6360
+					}
5e6360
+
5e6360
+					g_string_append (builder, tmp);
5e6360
+					g_string_append_c (builder, '\n');
5e6360
+				}
5e6360
+
5e6360
+				g_free (tmp);
5e6360
+				tmp = NULL;
5e6360
+			}
5e6360
+		}
5e6360
+
5e6360
+		if (same_day_forecasts) {
5e6360
+			g_free (tmp);
5e6360
+			tmp = NULL;
5e6360
+
5e6360
+			for (link = same_day_forecasts; link; link = g_slist_next (link)) {
5e6360
+				GWeatherInfo *nfo = link->data;
5e6360
+
5e6360
+				if (nfo) {
5e6360
+					tmp = describe_forecast (nfo, unit);
5e6360
+
5e6360
+					if (tmp && *tmp) {
5e6360
+						if (!has_forecast_word) {
5e6360
+							has_forecast_word = TRUE;
5e6360
+
5e6360
+							g_string_append (builder, _("Forecast"));
5e6360
+							g_string_append_c (builder, ':');
5e6360
+							g_string_append_c (builder, '\n');
5e6360
+						}
5e6360
+
5e6360
+						g_string_append (builder, tmp);
5e6360
+						g_string_append_c (builder, '\n');
5e6360
+					}
5e6360
+
5e6360
+					g_free (tmp);
5e6360
+					tmp = NULL;
5e6360
+				}
5e6360
+			}
5e6360
 		}
5e6360
 
5e6360
 		description->value = g_string_free (builder, FALSE);