[libgweather] Remove WeatherLocation as a pointer type



commit fdd771f355d2b9649d5835d4ddcf549c274ef353
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sun Dec 2 19:43:07 2012 +0100

    Remove WeatherLocation as a pointer type
    
    WeatherLocation is now embedded inside GWeatherInfo, which was it's only
    user. It exists as a structure to avoid passing GWeatherInfo around, and
    to avoid changing the existing code too much, but it is no longer separately
    allocated.

 libgweather/gweather-location.c |   31 ++++++----
 libgweather/test_sun_moon.c     |   27 ++++-----
 libgweather/weather-bom.c       |    2 +-
 libgweather/weather-iwin.c      |    2 +-
 libgweather/weather-met.c       |    2 +-
 libgweather/weather-metar.c     |    8 +--
 libgweather/weather-priv.h      |   26 +++-----
 libgweather/weather-wx.c        |    3 +-
 libgweather/weather-yahoo.c     |    4 +-
 libgweather/weather-yrno.c      |    8 +-
 libgweather/weather.c           |  126 +++++++--------------------------------
 11 files changed, 72 insertions(+), 167 deletions(-)
---
diff --git a/libgweather/gweather-location.c b/libgweather/gweather-location.c
index 2929b66..0a78f96 100644
--- a/libgweather/gweather-location.c
+++ b/libgweather/gweather-location.c
@@ -745,23 +745,21 @@ gweather_location_get_city_name (GWeatherLocation *loc)
 	return NULL;
 }
 
-WeatherLocation *
-_weather_location_from_gweather_location (GWeatherLocation *gloc, const gchar *name)
+void
+_gweather_location_update_weather_location (GWeatherLocation *gloc,
+					    WeatherLocation  *loc)
 {
-    const char *code = NULL, *zone = NULL, *yahoo_id = NULL, *radar = NULL, *tz_hint = NULL;
+    const char *code = NULL, *zone = NULL, *yahoo_id = NULL, *radar = NULL, *tz_hint = NULL, *country = NULL;
     gboolean latlon_valid = FALSE;
     gdouble lat = DBL_MAX, lon = DBL_MAX;
     GWeatherLocation *l;
-    WeatherLocation *wloc;
-
-    g_return_val_if_fail (gloc != NULL, NULL);
 
     if (gloc->level == GWEATHER_LOCATION_CITY && gloc->children)
 	l = gloc->children[0];
     else
 	l = gloc;
 
-    while (l && (!code || !zone || !radar || !tz_hint || !latlon_valid)) {
+    while (l && (!code || !zone || !radar || !tz_hint || !latlon_valid || !country)) {
 	if (!code && l->station_code)
 	    code = l->station_code;
 	if (!zone && l->forecast_zone)
@@ -772,6 +770,8 @@ _weather_location_from_gweather_location (GWeatherLocation *gloc, const gchar *n
 	    radar = l->radar;
 	if (!tz_hint && l->tz_hint)
 	    tz_hint = l->tz_hint;
+	if (!country && l->country_code)
+	    country = l->country_code;
 	if (!latlon_valid && l->latlon_valid) {
 	    lat = l->latitude;
 	    lon = l->longitude;
@@ -780,12 +780,17 @@ _weather_location_from_gweather_location (GWeatherLocation *gloc, const gchar *n
 	l = l->parent;
     }
 
-    wloc = _weather_location_new (name ? name : gweather_location_get_name (gloc),
-				  code, zone, yahoo_id, radar,
-				  latlon_valid, lat, lon,
-				  gweather_location_get_country (gloc),
-				  tz_hint);
-    return wloc;
+    loc->name = g_strdup (gweather_location_get_name (gloc)),
+    loc->code = g_strdup (code);
+    loc->zone = g_strdup (zone);
+    loc->yahoo_id = g_strdup (yahoo_id);
+    loc->radar = g_strdup (radar);
+    loc->country_code = g_strdup (country);
+    loc->tz_hint = g_strdup (tz_hint);
+
+    loc->latlon_valid = latlon_valid;
+    loc->latitude = lat;
+    loc->longitude = lon;
 }
 
 /**
diff --git a/libgweather/test_sun_moon.c b/libgweather/test_sun_moon.c
index de6a7b9..f384298 100644
--- a/libgweather/test_sun_moon.c
+++ b/libgweather/test_sun_moon.c
@@ -11,14 +11,13 @@ int
 main (int argc, char **argv)
 {
     GWeatherInfo   *info;
+    GWeatherInfoPrivate *priv;
     GOptionContext* context;
     GError*         error = NULL;
     gdouble         latitude, longitude;
-    WeatherLocation location;
     gchar*          gtime = NULL;
     GDate           gdate;
     struct tm       tm;
-    gboolean        bmoon;
     time_t          phases[4];
     const GOptionEntry entries[] = {
 	{ "latitude", 0, 0, G_OPTION_ARG_DOUBLE, &latitude,
@@ -30,8 +29,6 @@ main (int argc, char **argv)
 	{ NULL }
     };
 
-    memset(&location, 0, sizeof(WeatherLocation));
-
     context = g_option_context_new ("- test libgweather sun/moon calculations");
     g_option_context_add_main_entries (context, entries, NULL);
     g_option_context_parse (context, &argc, &argv, &error);
@@ -48,12 +45,12 @@ main (int argc, char **argv)
 	return -1;
     }
 
-    location.latitude = DEGREES_TO_RADIANS(latitude);
-    location.longitude = DEGREES_TO_RADIANS(longitude);
-    location.latlon_valid = TRUE;
     info = g_object_new (GWEATHER_TYPE_INFO, NULL);
-    info->priv->location = _weather_location_clone(&location);
-    info->priv->valid = TRUE;
+    priv = info->priv;
+    priv->location.latitude = DEGREES_TO_RADIANS(latitude);
+    priv->location.longitude = DEGREES_TO_RADIANS(longitude);
+    priv->location.latlon_valid = TRUE;
+    priv->valid = TRUE;
 
     if (gtime != NULL) {
 	//	printf(" gtime=%s\n", gtime);
@@ -72,14 +69,14 @@ main (int argc, char **argv)
 	    fabs(longitude), (longitude >= 0. ? 'E' : 'W'),
 	    asctime(gmtime(&priv->current_time)));
     printf("sunrise:   %s",
-	   (info->priv->sunriseValid ? ctime(&info->priv->sunrise) : "(invalid)\n"));
+	   (priv->sunriseValid ? ctime(&priv->sunrise) : "(invalid)\n"));
     printf("sunset:    %s",
-	   (info->priv->sunsetValid ? ctime(&info->priv->sunset)  : "(invalid)\n"));
-    if (bmoon) {
-	printf("moonphase: %g\n", info->priv->moonphase);
-	printf("moonlat:   %g\n", info->priv->moonlatitude);
+	   (priv->sunsetValid ? ctime(&priv->sunset)  : "(invalid)\n"));
+    if (priv->moonValid) {
+	printf("moonphase: %g\n", priv->moonphase);
+	printf("moonlat:   %g\n", priv->moonlatitude);
 
-	if (calc_moon_phases(info, phases)) {
+	if (gweather_info_get_upcoming_moonphases(info, phases)) {
 	    printf("    New:   %s", asctime(gmtime(&phases[0])));
 	    printf("    1stQ:  %s", asctime(gmtime(&phases[1])));
 	    printf("    Full:  %s", asctime(gmtime(&phases[2])));
diff --git a/libgweather/weather-bom.c b/libgweather/weather-bom.c
index a556bda..4c1853e 100644
--- a/libgweather/weather-bom.c
+++ b/libgweather/weather-bom.c
@@ -63,7 +63,7 @@ bom_start_open (GWeatherInfo *info)
     SoupMessage *msg;
     WeatherLocation *loc;
 
-    loc = info->priv->location;
+    loc = &info->priv->location;
 
     url = g_strdup_printf ("http://www.bom.gov.au/fwo/%s.txt";,
 			   loc->zone + 1);
diff --git a/libgweather/weather-iwin.c b/libgweather/weather-iwin.c
index 3c27a67..82bdf03 100644
--- a/libgweather/weather-iwin.c
+++ b/libgweather/weather-iwin.c
@@ -393,7 +393,7 @@ iwin_start_open (GWeatherInfo *info)
     g_return_val_if_fail (info != NULL, FALSE);
 
     priv = info->priv;
-    loc = priv->location;
+    loc = &priv->location;
     g_return_val_if_fail (loc != NULL, FALSE);
 
     /* No zone (or -) means no weather information from national offices */
diff --git a/libgweather/weather-met.c b/libgweather/weather-met.c
index 9141f86..3ef88dc 100644
--- a/libgweather/weather-met.c
+++ b/libgweather/weather-met.c
@@ -171,7 +171,7 @@ metoffice_start_open (GWeatherInfo *info)
     SoupMessage *msg;
     WeatherLocation *loc;
 
-    loc = info->priv->location;
+    loc = &info->priv->location;
     url = g_strdup_printf ("http://www.metoffice.gov.uk/weather/uk/%s/%s_forecast_weather_noscript.html";, loc->zone + 1, loc->zone + 1);
 
     msg = soup_message_new ("GET", url);
diff --git a/libgweather/weather-metar.c b/libgweather/weather-metar.c
index 6048067..682eac7 100644
--- a/libgweather/weather-metar.c
+++ b/libgweather/weather-metar.c
@@ -527,7 +527,7 @@ metar_finish (SoupSession *session, SoupMessage *msg, gpointer data)
 	return;
     }
 
-    loc = priv->location;
+    loc = &priv->location;
 
     searchkey = g_strdup_printf ("\n%s", loc->code);
     p = strstr (msg->response_body->data, searchkey);
@@ -566,11 +566,7 @@ metar_start_open (GWeatherInfo *info)
     priv = info->priv;
 
     priv->valid = priv->network_error = FALSE;
-    loc = priv->location;
-    if (loc == NULL) {
-	g_warning (_("WeatherInfo missing location"));
-	return;
-    }
+    loc = &priv->location;
 
     msg = soup_form_request_new (
 	"GET", "http://weather.noaa.gov/cgi-bin/mgetmetar.pl";,
diff --git a/libgweather/weather-priv.h b/libgweather/weather-priv.h
index 84d94ce..ad25f6b 100644
--- a/libgweather/weather-priv.h
+++ b/libgweather/weather-priv.h
@@ -73,22 +73,14 @@ typedef struct {
     gchar *tz_hint;
 } WeatherLocation;
 
-WeatherLocation *       _weather_location_from_gweather_location (GWeatherLocation *gloc, const gchar *name);
-
-WeatherLocation *	_weather_location_new 	(const gchar *trans_name,
-						 const gchar *code,
-						 const gchar *zone,
-						 const gchar *yahoo_id,
-						 const gchar *radar,
-						 gboolean     latlon_valid,
-						 double       latitude,
-						 double       longitude,
-						 const gchar *country_code,
-						 const gchar *tz_hint);
-WeatherLocation *	_weather_location_clone	(const WeatherLocation *location);
-void			_weather_location_free	(WeatherLocation *location);
-gboolean		_weather_location_equal	(const WeatherLocation *location1,
-						 const WeatherLocation *location2);
+GWeatherLocation *_gweather_location_new_detached (GWeatherLocation *nearest_station,
+						   const char       *name,
+						   gboolean          latlon_valid,
+						   gdouble           latitude,
+						   gdouble           longitude);
+
+void              _gweather_location_update_weather_location (GWeatherLocation *gloc,
+							      WeatherLocation  *loc);
 
 /*
  * Weather information.
@@ -115,7 +107,7 @@ struct _GWeatherInfoPrivate {
     gboolean polarNight;
     gboolean moonValid;
     gboolean tempMinMaxValid;
-    WeatherLocation *location;
+    WeatherLocation location;
     GWeatherLocation *world;
     GWeatherLocation *glocation;
     GWeatherUpdate update;
diff --git a/libgweather/weather-wx.c b/libgweather/weather-wx.c
index 8ff75bc..711d80e 100644
--- a/libgweather/weather-wx.c
+++ b/libgweather/weather-wx.c
@@ -84,8 +84,7 @@ wx_start_open (GWeatherInfo *info)
 
     priv->radar = NULL;
     priv->radar_loader = gdk_pixbuf_loader_new ();
-    loc = priv->location;
-    g_return_if_fail (loc != NULL);
+    loc = &priv->location;
 
     if (priv->radar_url)
 	url = g_strdup (priv->radar_url);
diff --git a/libgweather/weather-yahoo.c b/libgweather/weather-yahoo.c
index bd937b6..c47bdc9 100644
--- a/libgweather/weather-yahoo.c
+++ b/libgweather/weather-yahoo.c
@@ -273,9 +273,9 @@ yahoo_start_open (GWeatherInfo *info)
     SoupMessage *message;
 
     priv = info->priv;
-    loc = priv->location;
+    loc = &priv->location;
 
-    if (!loc || !loc->yahoo_id)
+    if (!loc->yahoo_id)
 	return FALSE;
 
     /* Yahoo! Weather only supports forecast list
diff --git a/libgweather/weather-yrno.c b/libgweather/weather-yrno.c
index b5a2f79..40c2341 100644
--- a/libgweather/weather-yrno.c
+++ b/libgweather/weather-yrno.c
@@ -365,11 +365,11 @@ parse_forecast_xml_new (GWeatherInfo    *master_info,
 	node = xpath_result->nodesetval->nodeTab[i];
 
 	val = xmlGetProp (node, XC("from"));
-	from_time = date_to_time_t (val, priv->location->tz_hint);
+	from_time = date_to_time_t (val, priv->location.tz_hint);
 	xmlFree (val);
 
 	val = xmlGetProp (node, XC("to"));
-	to_time = date_to_time_t (val, priv->location->tz_hint);
+	to_time = date_to_time_t (val, priv->location.tz_hint);
 	xmlFree (val);
 
 	/* New API has forecast in a list of "master" elements
@@ -513,9 +513,9 @@ yrno_start_open_new (GWeatherInfo *info)
     gchar latstr[G_ASCII_DTOSTR_BUF_SIZE], lonstr[G_ASCII_DTOSTR_BUF_SIZE];
 
     priv = info->priv;
-    loc = priv->location;
+    loc = &priv->location;
 
-    if (loc == NULL || !loc->latlon_valid ||
+    if (!loc->latlon_valid ||
 	priv->forecast_type != GWEATHER_FORECAST_LIST)
 	return FALSE;
 
diff --git a/libgweather/weather.c b/libgweather/weather.c
index 7a087f8..6e1a9e4 100644
--- a/libgweather/weather.c
+++ b/libgweather/weather.c
@@ -108,102 +108,16 @@ gweather_dpgettext (const char *context,
     return g_dpgettext2 (GETTEXT_PACKAGE, context, str);
 }
 
-WeatherLocation *
-_weather_location_new (const gchar *name, const gchar *code,
-		       const gchar *zone, const gchar *yahoo_id,
-		       const gchar *radar,
-		       gboolean     latlon_valid,
-		       double       latitude,
-		       double       longitude,
-		       const gchar *country_code,
-		       const gchar *tz_hint)
-{
-    WeatherLocation *location;
-
-    _weather_internal_check ();
-
-    location = g_slice_new0 (WeatherLocation);
-
-    /* name and metar code must be set */
-    location->name = g_strdup (name);
-    location->code = g_strdup (code);
-
-    if (zone)
-        location->zone = g_strdup (zone);
-
-    if (radar)
-        location->radar = g_strdup (radar);
-
-    if (yahoo_id)
-	location->yahoo_id = g_strdup (yahoo_id);
-
-    location->latlon_valid = latlon_valid;
-    location->latitude = latitude;
-    location->longitude = longitude;
-    location->country_code = g_strdup (country_code);
-    location->tz_hint = g_strdup (tz_hint);
-
-    return location;
-}
-
-WeatherLocation *
-_weather_location_clone (const WeatherLocation *location)
-{
-    WeatherLocation *clone;
-
-    g_return_val_if_fail (location != NULL, NULL);
-
-    clone = g_slice_new0 (WeatherLocation);
-
-    clone->name = g_strdup (location->name);
-    clone->country_code = g_strdup (location->country_code);
-    clone->tz_hint = g_strdup (location->tz_hint);
-
-    if (location->zone)
-	clone->zone = g_strdup (location->zone);
-
-    if (location->radar)
-	clone->radar = g_strdup (location->radar);
-
-    if (location->yahoo_id)
-	clone->yahoo_id = g_strdup (location->yahoo_id);
-
-    clone->latlon_valid = location->latlon_valid;
-    clone->latitude = location->latitude;
-    clone->longitude = location->longitude;
-
-    return clone;
-}
-
-void
+static void
 _weather_location_free (WeatherLocation *location)
 {
-    if (location) {
-        g_free (location->name);
-        g_free (location->code);
-        g_free (location->zone);
-	g_free (location->yahoo_id);
-        g_free (location->radar);
-        g_free (location->country_code);
-        g_free (location->tz_hint);
-
-        g_slice_free (WeatherLocation, location);
-    }
-}
-
-gboolean
-_weather_location_equal (const WeatherLocation *location1, const WeatherLocation *location2)
-{
-    /* if something is NULL, then it's TRUE if and only if both are NULL) */
-    if (location1 == NULL || location2 == NULL)
-        return (location1 == location2);
-    if (!location1->code || !location2->code)
-        return (location1->code == location2->code);
-    if (!location1->name || !location2->name)
-        return (location1->name == location2->name);
-
-    return ((strcmp (location1->code, location2->code) == 0) &&
-	    (strcmp (location1->name, location2->name) == 0));
+    g_free (location->name);
+    g_free (location->code);
+    g_free (location->zone);
+    g_free (location->yahoo_id);
+    g_free (location->radar);
+    g_free (location->country_code);
+    g_free (location->tz_hint);
 }
 
 static const gchar *wind_direction_str[] = {
@@ -572,8 +486,7 @@ gweather_info_finalize (GObject *object)
     if (priv->session)
 	g_object_unref (priv->session);
 
-    _weather_location_free (priv->location);
-    priv->location = NULL;
+    _weather_location_free (&priv->location);
 
     if (priv->glocation)
 	gweather_location_unref (priv->glocation);
@@ -623,9 +536,7 @@ gweather_info_get_location_name (GWeatherInfo *info)
 {
     g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
 
-    if (!info->priv->location)
-	return NULL;
-    return g_strdup(info->priv->location->name);
+    return g_strdup(info->priv->location.name);
 }
 
 gchar *
@@ -1228,7 +1139,7 @@ gweather_info_get_icon_name (GWeatherInfo *info)
 	if (phase == MOON_PHASES) {
 	    phase = 0;
 	} else if (phase > 0 &&
-		   (RADIANS_TO_DEGREES(priv->location->latitude)
+		   (RADIANS_TO_DEGREES(priv->location.latitude)
 		    < moonLat)) {
 	    /*
 	     * Locations south of the moon's latitude will see the moon in the
@@ -1768,8 +1679,7 @@ gweather_info_set_location_internal (GWeatherInfo     *info,
 
     if (priv->glocation)
 	gweather_location_unref (priv->glocation);
-    if (priv->location)
-	_weather_location_free (priv->location);
+    _weather_location_free (&priv->location);
 
     if (!priv->world && location)
 	priv->world = gweather_location_ref_world (location);
@@ -1792,11 +1702,17 @@ gweather_info_set_location_internal (GWeatherInfo     *info,
 	priv->glocation = gweather_location_find_by_station_code (priv->world, station_code);
     }
 
-    priv->location = _weather_location_from_gweather_location (priv->glocation, name);
+    _gweather_location_update_weather_location (priv->glocation,
+						&priv->location);
+    if (name) {
+	g_free (priv->location.name);
+	priv->location.name = g_strdup (name);
+    }
 
     if (latlon_override) {
-	priv->location->latitude = DEGREES_TO_RADIANS (lat);
-	priv->location->longitude = DEGREES_TO_RADIANS (lon);
+	priv->location.latlon_valid = TRUE;
+	priv->location.latitude = DEGREES_TO_RADIANS (lat);
+	priv->location.longitude = DEGREES_TO_RADIANS (lon);
     }
 
     if (default_loc)



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