[libgweather/wip/forecasts] GWeather: avoid opening and parsing the database for each forecast element



commit cc75314ab4e347b75f491a95c335a5892c043bed
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sat Dec 1 18:50:33 2012 +0100

    GWeather: avoid opening and parsing the database for each forecast element
    
    Retrieve the world GWeatherLocation from the one passed in, if none is
    provided explicitly. This avoids creating a new world for each GWeatherInfo
    created by cloning the master one.

 libgweather/gweather-location.c |   20 ++++++++++++++++++--
 libgweather/gweather-location.h |    2 ++
 libgweather/weather.c           |    3 +++
 3 files changed, 23 insertions(+), 2 deletions(-)
---
diff --git a/libgweather/gweather-location.c b/libgweather/gweather-location.c
index e40961b..0729ed9 100644
--- a/libgweather/gweather-location.c
+++ b/libgweather/gweather-location.c
@@ -205,9 +205,13 @@ location_new_from_xml (GWeatherParser *parser, GWeatherLocationLevel level,
 		g_ptr_array_add (children, child);
 	    else {
 		if (child->children) {
-		    for (i = 0; child->children[i]; i++)
-			g_ptr_array_add (children, gweather_location_ref (child->children[i]));
+		    for (i = 0; child->children[i]; i++) {
+			/* Correct back pointers */
+			child->children[i]->parent = loc;
+			g_ptr_array_add (children, child->children[i]);
+		    }
 		}
+		child->children = NULL;
 		gweather_location_unref (child);
 	    }
 	} else if (!strcmp (tagname, "country")) {
@@ -774,3 +778,15 @@ gweather_location_find_by_station_code (GWeatherLocation *world,
 {
     return g_hash_table_lookup (world->metar_code_cache, station_code);
 }
+
+GWeatherLocation *
+gweather_location_ref_world (GWeatherLocation *loc)
+{
+    while (loc &&
+	   loc->level != GWEATHER_LOCATION_WORLD)
+	loc = loc->parent;
+
+    if (loc)
+	gweather_location_ref (loc);
+    return loc;
+}
diff --git a/libgweather/gweather-location.h b/libgweather/gweather-location.h
index 85265b5..fb3b7ef 100644
--- a/libgweather/gweather-location.h
+++ b/libgweather/gweather-location.h
@@ -82,6 +82,8 @@ char                  *gweather_location_get_city_name  (GWeatherLocation  *loc)
 GWeatherLocation      *gweather_location_find_by_station_code (GWeatherLocation *world,
 							       const gchar *code);
 
+GWeatherLocation      *gweather_location_ref_world      (GWeatherLocation  *loc);
+
 G_END_DECLS
 
 #endif /* __GWEATHER_LOCATIONS_H__ */
diff --git a/libgweather/weather.c b/libgweather/weather.c
index 61f0a3d..7d37977 100644
--- a/libgweather/weather.c
+++ b/libgweather/weather.c
@@ -1795,6 +1795,9 @@ gweather_info_set_location_internal (GWeatherInfo     *info,
     if (priv->location)
 	_weather_location_free (priv->location);
 
+    if (!priv->world && location)
+	priv->world = gweather_location_ref_world (location);
+
     if (!priv->world)
 	priv->world = gweather_location_new_world (FALSE);
 



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