[evolution-data-server] weather: update for newer GWeather



commit c51c56fec9f9d45a5e36619ad650c534293191e3
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sat Mar 24 01:06:02 2012 +0100

    weather: update for newer GWeather
    
    GWeather 3.6 changed API to be properly namespaced and GObject based.
    Also, WeatherLocation was removed and replaced with GWeatherLocation
    in all public API (with additionally the possibility to retrieve one
    particular GWeatherLocation by station code)
    At the same time, remove a old and now useless abstration by folding
    EWeatherSourceCCF into EWeatherSource.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=672805

 calendar/backends/weather/Makefile.am             |    4 +-
 calendar/backends/weather/e-cal-backend-weather.c |  112 +++---
 calendar/backends/weather/e-weather-source-ccf.c  |  468 ---------------------
 calendar/backends/weather/e-weather-source-ccf.h  |   76 ----
 calendar/backends/weather/e-weather-source.c      |   93 ++++-
 calendar/backends/weather/e-weather-source.h      |   44 +--
 configure.ac                                      |    7 +-
 7 files changed, 139 insertions(+), 665 deletions(-)
---
diff --git a/calendar/backends/weather/Makefile.am b/calendar/backends/weather/Makefile.am
index ed41cf2..25ee7a9 100644
--- a/calendar/backends/weather/Makefile.am
+++ b/calendar/backends/weather/Makefile.am
@@ -25,9 +25,7 @@ libecalbackendweather_la_SOURCES =	\
 	e-source-weather.c		\
 	e-source-weather.h		\
 	e-weather-source.c		\
-	e-weather-source.h		\
-	e-weather-source-ccf.c		\
-	e-weather-source-ccf.h
+	e-weather-source.h
 
 libecalbackendweather_la_LIBADD =					\
 	$(top_builddir)/calendar/libecal/libecal-1.2.la			\
diff --git a/calendar/backends/weather/e-cal-backend-weather.c b/calendar/backends/weather/e-cal-backend-weather.c
index 62357ae..632c5f2 100644
--- a/calendar/backends/weather/e-cal-backend-weather.c
+++ b/calendar/backends/weather/e-cal-backend-weather.c
@@ -27,7 +27,7 @@
 #include "e-weather-source.h"
 
 #define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
-#include <libgweather/weather.h>
+#include <libgweather/gweather-weather.h>
 #undef GWEATHER_I_KNOW_THIS_IS_UNSTABLE
 
 #define WEATHER_UID_EXT "-weather"
@@ -43,7 +43,7 @@ G_DEFINE_TYPE (ECalBackendWeather, e_cal_backend_weather, E_TYPE_CAL_BACKEND_SYN
 
 static gboolean reload_cb (ECalBackendWeather *cbw);
 static gboolean begin_retrieval_cb (ECalBackendWeather *cbw);
-static ECalComponent * create_weather (ECalBackendWeather *cbw, WeatherInfo *report, gboolean is_forecast);
+static ECalComponent * create_weather (ECalBackendWeather *cbw, GWeatherInfo *report, gboolean is_forecast);
 static void e_cal_backend_weather_add_timezone (ECalBackendSync *backend, EDataCal *cal, GCancellable *cancellable, const gchar *tzobj, GError **perror);
 
 /* Private part of the ECalBackendWeather structure */
@@ -158,7 +158,7 @@ put_component_to_store (ECalBackendWeather *cb,
 }
 
 static void
-finished_retrieval_cb (WeatherInfo *info,
+finished_retrieval_cb (GWeatherInfo *info,
                        ECalBackendWeather *cbw)
 {
 	ECalBackendWeatherPrivate *priv;
@@ -197,13 +197,13 @@ finished_retrieval_cb (WeatherInfo *info,
 		e_cal_backend_notify_component_created (E_CAL_BACKEND (cbw), comp);
 		g_object_unref (comp);
 
-		forecasts = weather_info_get_forecast_list (info);
+		forecasts = gweather_info_get_forecast_list (info);
 		if (forecasts) {
 			GSList *f;
 
 			/* skip the first one, it's for today, which has been added above */
 			for (f = forecasts->next; f; f = f->next) {
-				WeatherInfo *nfo = f->data;
+				GWeatherInfo *nfo = f->data;
 
 				if (nfo) {
 					comp = create_weather (cbw, nfo, TRUE);
@@ -267,7 +267,7 @@ begin_retrieval_cb (ECalBackendWeather *cbw)
 }
 
 static const gchar *
-getCategory (WeatherInfo *report)
+getCategory (GWeatherInfo *report)
 {
 	struct {
 		const gchar *description;
@@ -286,7 +286,7 @@ getCategory (WeatherInfo *report)
 	};
 
 	gint i;
-	const gchar *icon_name = weather_info_get_icon_name (report);
+	const gchar *icon_name = gweather_info_get_icon_name (report);
 
 	if (!icon_name)
 		return NULL;
@@ -302,7 +302,7 @@ getCategory (WeatherInfo *report)
 
 static ECalComponent *
 create_weather (ECalBackendWeather *cbw,
-                WeatherInfo *report,
+                GWeatherInfo *report,
                 gboolean is_forecast)
 {
 	ECalBackendWeatherPrivate *priv;
@@ -314,34 +314,19 @@ create_weather (ECalBackendWeather *cbw,
 	gchar			  *uid;
 	GSList                    *text_list = NULL;
 	ECalComponentText         *description;
-	ESource                   *source;
-	const gchar               *tmp;
+	gchar                     *tmp;
 	time_t			   update_time;
 	icaltimezone		  *update_zone = NULL;
-	ESourceWeather            *extension;
-	const gchar               *extension_name;
-	const WeatherLocation     *location;
-	ESourceWeatherUnits        units;
+	const GWeatherLocation    *location;
+	const GWeatherTimezone    *w_timezone;
 
 	g_return_val_if_fail (E_IS_CAL_BACKEND_WEATHER (cbw), NULL);
 
-	if (!weather_info_get_value_update (report, &update_time))
+	if (!gweather_info_get_value_update (report, &update_time))
 		return NULL;
 
 	priv = cbw->priv;
 
-	source = e_backend_get_source (E_BACKEND (cbw));
-
-	extension_name = E_SOURCE_EXTENSION_WEATHER_BACKEND;
-	extension = e_source_get_extension (source, extension_name);
-	units = e_source_weather_get_units (extension);
-
-	/* Prefer metric if units is invalid. */
-	if (units == E_SOURCE_WEATHER_UNITS_IMPERIAL)
-		weather_info_to_imperial (report);
-	else
-		weather_info_to_metric (report);
-
 	/* create the component and event object */
 	ical_comp = icalcomponent_new (ICAL_VEVENT_COMPONENT);
 	cal_comp = e_cal_component_new ();
@@ -353,9 +338,9 @@ create_weather (ECalBackendWeather *cbw,
 	g_free (uid);
 
 	/* use timezone of the location to determine date for which this is set */
-	location = weather_info_get_location (report);
-	if (location && location->tz_hint && *location->tz_hint)
-		update_zone = icaltimezone_get_builtin_timezone (location->tz_hint);
+	location = gweather_info_get_location (report);
+	if (location && (w_timezone = gweather_location_get_timezone ((GWeatherLocation *)location)))
+		update_zone = icaltimezone_get_builtin_timezone (gweather_timezone_get_tzid ((GWeatherTimezone*)w_timezone));
 
 	if (!update_zone)
 		update_zone = icaltimezone_get_utc_timezone ();
@@ -380,53 +365,56 @@ create_weather (ECalBackendWeather *cbw,
 	/* We have to add 1 day to DTEND, as it is not inclusive. */
 	e_cal_component_set_dtend (cal_comp, &dt);
 
-	if (is_forecast) {
+	{
 		gdouble tmin = 0.0, tmax = 0.0;
 
-		if (weather_info_get_value_temp_min (report, TEMP_UNIT_DEFAULT, &tmin) &&
-		    weather_info_get_value_temp_max (report, TEMP_UNIT_DEFAULT, &tmax) &&
+		if (gweather_info_get_value_temp_min (report, GWEATHER_TEMP_UNIT_DEFAULT, &tmin) &&
+		    gweather_info_get_value_temp_max (report, GWEATHER_TEMP_UNIT_DEFAULT, &tmax) &&
 		    tmin != tmax) {
-			/* because weather_info_get_temp* uses one internal buffer, thus finally
-			 * the last value is shown for both, which is obviously wrong */
-			GString *str = g_string_new (priv->city);
+			gchar *min, *max;
 
-			g_string_append (str, " : ");
-			g_string_append (str, weather_info_get_temp_min (report));
-			g_string_append (str, "/");
-			g_string_append (str, weather_info_get_temp_max (report));
+			min = gweather_info_get_temp_min (report);
+			max = gweather_info_get_temp_max (report);
+			comp_summary.value = g_strdup_printf ("%s : %s / %s", priv->city, min, max);
 
-			comp_summary.value = g_string_free (str, FALSE);
+			g_free (min); g_free (max);
 		} else {
-			comp_summary.value = g_strdup_printf ("%s : %s", priv->city, weather_info_get_temp (report));
-		}
-	} else {
-		gdouble tmin = 0.0, tmax = 0.0;
-		/* because weather_info_get_temp* uses one internal buffer, thus finally
-		 * the last value is shown for both, which is obviously wrong */
-		GString *str = g_string_new (priv->city);
+			gchar *temp;
 
-		g_string_append (str, " : ");
-		if (weather_info_get_value_temp_min (report, TEMP_UNIT_DEFAULT, &tmin) &&
-		    weather_info_get_value_temp_max (report, TEMP_UNIT_DEFAULT, &tmax) &&
-		    tmin != tmax) {
-			g_string_append (str, weather_info_get_temp_min (report));
-			g_string_append (str, "/");
-			g_string_append (str, weather_info_get_temp_max (report));
-		} else {
-			g_string_append (str, weather_info_get_temp (report));
-		}
+			temp = gweather_info_get_temp (report);
+			comp_summary.value = g_strdup_printf ("%s : %s", priv->city, temp);
 
-		comp_summary.value = g_string_free (str, FALSE);
+			g_free (temp);
+		}
 	}
 	comp_summary.altrep = NULL;
 	e_cal_component_set_summary (cal_comp, &comp_summary);
 	g_free ((gchar *) comp_summary.value);
 
-	tmp = weather_info_get_forecast (report);
-	comp_summary.value = weather_info_get_weather_summary (report);
+	tmp = gweather_info_get_forecast (report);
+	comp_summary.value = gweather_info_get_weather_summary (report);
 
 	description = g_new0 (ECalComponentText, 1);
-	description->value = g_strconcat (is_forecast ? "" : comp_summary.value, is_forecast ? "" : "\n", tmp ? _("Forecast") : "", tmp ? ":" : "", tmp && !is_forecast ? "\n" : "", tmp ? tmp : "", NULL);
+	{
+		GString *builder;
+
+		builder = g_string_new (NULL);
+
+		if (!is_forecast) {
+			g_string_append (builder, comp_summary.value);
+			g_string_append_c (builder, '\n');
+		}
+		if (tmp) {
+			g_string_append (builder, _("Forecast"));
+			g_string_append_c (builder, ':');
+			if (!is_forecast)
+				g_string_append_c (builder, '\n');
+			g_string_append (builder, tmp);
+		}
+
+		description->value = g_string_free (builder, FALSE);
+		g_free (tmp);
+	}
 	description->altrep = "";
 	text_list = g_slist_append (text_list, description);
 	e_cal_component_set_description_list (cal_comp, text_list);
diff --git a/calendar/backends/weather/e-weather-source.c b/calendar/backends/weather/e-weather-source.c
index 3dd3431..2da852a 100644
--- a/calendar/backends/weather/e-weather-source.c
+++ b/calendar/backends/weather/e-weather-source.c
@@ -19,43 +19,108 @@
  */
 
 #include "e-weather-source.h"
-#include "e-weather-source-ccf.h"
 
 #include <string.h>
 
 G_DEFINE_TYPE (EWeatherSource, e_weather_source, G_TYPE_OBJECT)
 
+static void
+parse_done (GWeatherInfo *info,
+            gpointer      data)
+{
+	EWeatherSource *source = (EWeatherSource *) data;
+
+	if (!source)
+		return;
+
+	if (!info || !gweather_info_is_valid (info)) {
+		source->done (NULL, source->finished_data);
+		return;
+	}
+
+	source->done (info, source->finished_data);
+}
+
 void
-e_weather_source_parse (EWeatherSource *source,
-                        EWeatherSourceFinished done,
-                        gpointer data)
+e_weather_source_parse (EWeatherSource         *source,
+			EWeatherSourceFinished  done,
+			gpointer                data)
 {
-	EWeatherSourceClass *class;
+	source->finished_data = data;
+	source->done = done;
+
+	if (!source->info) {
+		source->info = gweather_info_new (source->location, GWEATHER_FORECAST_LIST);
+		g_signal_connect (source->info, "updated", G_CALLBACK (parse_done), source);
+	} else {
+		gweather_info_update (source->info);
+	}
+}
 
-	g_return_if_fail (source != NULL);
+static void
+e_weather_source_finalize (GObject *object)
+{
+	EWeatherSource *self = (EWeatherSource*) object;
 
-	class = E_WEATHER_SOURCE_GET_CLASS (source);
-	g_return_if_fail (class->parse != NULL);
+	if (self->location)
+		gweather_location_unref (self->location);
+	g_clear_object (&self->info);
 
-	class->parse (source, done, data);
+	G_OBJECT_CLASS (e_weather_source_parent_class)->finalize (object);
 }
 
 static void
-e_weather_source_class_init (EWeatherSourceClass *class)
+e_weather_source_class_init (EWeatherSourceClass *klass)
 {
-	/* nothing to do here */
+	GObjectClass *gobject_class;
+
+	gobject_class = G_OBJECT_CLASS (klass);
+	gobject_class->finalize = e_weather_source_finalize;
 }
 
 static void
 e_weather_source_init (EWeatherSource *source)
 {
-	/* nothing to do here */
 }
 
 EWeatherSource *
 e_weather_source_new (const gchar *location)
 {
-	g_return_val_if_fail (location != NULL, NULL);
+	GWeatherLocation *world, *glocation;
+	EWeatherSource *source;
+	char **tokens;
+
+	/* Old location is formatted as ccf/AAA[/BBB] - AAA is the 3-letter station
+	 * code for identifying the providing station (subdirectory within the crh data
+	 * repository). BBB is an optional additional station ID for the station within
+	 * the CCF file. If not present, BBB is assumed to be the same station as AAA.
+	 * But the new location is code/name, where code is 4-letter code.
+	 * So if got the old format, then migrate to the new one, if possible.
+	 */
+
+	if (!location)
+		return NULL;
+
+	world = gweather_location_new_world (FALSE);
+
+	if (strncmp (location, "ccf/", 4) == 0)
+		location += 4;
+
+	tokens = g_strsplit (location, "/", 2);
+
+	glocation = gweather_location_find_by_station_code (world, tokens[0]);
+	if (glocation)
+		gweather_location_ref (glocation);
+
+	gweather_location_unref (world);
+	g_strfreev (tokens);
+
+	if (!glocation)
+		return NULL;
+
+	source = E_WEATHER_SOURCE (g_object_new (e_weather_source_get_type (), NULL));
+	source->location = glocation;
+	source->info = NULL;
 
-	return e_weather_source_ccf_new (location);
+	return source;
 }
diff --git a/calendar/backends/weather/e-weather-source.h b/calendar/backends/weather/e-weather-source.h
index e36793e..793c2fa 100644
--- a/calendar/backends/weather/e-weather-source.h
+++ b/calendar/backends/weather/e-weather-source.h
@@ -25,7 +25,7 @@
 #include <time.h>
 
 #define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
-#include <libgweather/weather.h>
+#include <libgweather/gweather-weather.h>
 #undef GWEATHER_I_KNOW_THIS_IS_UNSTABLE
 
 /* Standard GObject macros */
@@ -49,35 +49,7 @@
 
 G_BEGIN_DECLS
 
-typedef enum {
-	WEATHER_FAIR,
-	WEATHER_SNOW_SHOWERS,
-	WEATHER_SNOW,
-	WEATHER_PARTLY_CLOUDY,
-	WEATHER_SMOKE,
-	WEATHER_THUNDERSTORMS,
-	WEATHER_CLOUDY,
-	WEATHER_DRIZZLE,
-	WEATHER_SUNNY,
-	WEATHER_DUST,
-	WEATHER_CLEAR,
-	WEATHER_MOSTLY_CLOUDY,
-	WEATHER_WINDY,
-	WEATHER_RAIN_SHOWERS,
-	WEATHER_FOGGY,
-	WEATHER_RAIN_OR_SNOW_MIXED,
-	WEATHER_SLEET,
-	WEATHER_VERY_HOT_OR_HOT_HUMID,
-	WEATHER_BLIZZARD,
-	WEATHER_FREEZING_RAIN,
-	WEATHER_HAZE,
-	WEATHER_BLOWING_SNOW,
-	WEATHER_FREEZING_DRIZZLE,
-	WEATHER_VERY_COLD_WIND_CHILL,
-	WEATHER_RAIN,
-} WeatherConditions;
-
-typedef void (*EWeatherSourceFinished)(WeatherInfo *result, gpointer data);
+typedef void (*EWeatherSourceFinished)(GWeatherInfo *result, gpointer data);
 
 typedef struct _EWeatherSource EWeatherSource;
 typedef struct _EWeatherSourceClass EWeatherSourceClass;
@@ -87,16 +59,16 @@ typedef struct _EWeatherSourceClass EWeatherSourceClass;
  * to know how to do is parse the specific format. */
 struct _EWeatherSource {
 	GObject parent;
+
+	GWeatherLocation *location;
+	GWeatherInfo *info;
+
+	EWeatherSourceFinished done;
+	gpointer finished_data;
 };
 
 struct _EWeatherSourceClass {
 	GObjectClass parent_class;
-
-	/* Returns a list of WeatherForecast objects containing the
-	 * data for the forecast. */
-	void		(*parse)	(EWeatherSource *source,
-					 EWeatherSourceFinished done,
-					 gpointer data);
 };
 
 GType		e_weather_source_get_type	(void);
diff --git a/configure.ac b/configure.ac
index e7e770f..2a68525 100644
--- a/configure.ac
+++ b/configure.ac
@@ -46,7 +46,7 @@ m4_define([libical_minimum_version], [0.43])
 
 dnl Optional Packages
 m4_define([goa_minimum_version], [3.2])
-m4_define([gweather_minimum_version], [2.90.0])
+m4_define([gweather_minimum_version], [3.5.0])
 
 AC_SUBST([BASE_VERSION],[base_version])
 AC_SUBST([API_VERSION],[api_version])
@@ -1103,11 +1103,6 @@ AC_MSG_RESULT([$use_gweather])
 if test "x$use_gweather" = "xyes"; then
 	PKG_CHECK_MODULES([LIBGWEATHER], [gweather-3.0 >= gweather_minimum_version],[],
 	[AC_MSG_ERROR([The weather calendar backend requires GWeather >= gweather_minimum_version. Alternatively, you may specify --disable-weather as a configure option to avoid building the backend.])])
-
-	dnl gweather-3.5 introduces API changes we do not yet support.
-	if `$PKG_CONFIG --atleast-version=3.5 gweather-3.0`; then
-		AC_MSG_ERROR([gweather-3.5 is not yet supported.  Install gweather-3.4 or specify --disable-weather as a configure option to avoid building the backend.])
-	fi
 fi
 AM_CONDITIONAL(ENABLE_WEATHER, [test $use_gweather = yes])
 



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