[libgweather] GWeatherLocation: add API to find a country by code



commit 99b2e7d2f0a66f97d5c745450275659ca6569c5e
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Thu Dec 22 16:40:03 2016 +0100

    GWeatherLocation: add API to find a country by code
    
    Using a new hashtable to cache the information at the world level.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=753332

 libgweather/gweather-location.c |   32 ++++++++++++++++++++++++++++++--
 libgweather/gweather-location.h |    2 ++
 libgweather/gweather-parser.c   |    7 ++++++-
 libgweather/gweather-parser.h   |    1 +
 libgweather/gweather-private.h  |    1 +
 5 files changed, 40 insertions(+), 3 deletions(-)
---
diff --git a/libgweather/gweather-location.c b/libgweather/gweather-location.c
index 40f5624..1721e0a 100644
--- a/libgweather/gweather-location.c
+++ b/libgweather/gweather-location.c
@@ -141,8 +141,10 @@ location_new_from_xml (GWeatherParser *parser, GWeatherLocationLevel level,
     loc->level = level;
     loc->ref_count = 1;
     loc->msgctxt = NULL;
-    if (level == GWEATHER_LOCATION_WORLD)
+    if (level == GWEATHER_LOCATION_WORLD) {
        loc->metar_code_cache = g_hash_table_ref (parser->metar_code_cache);
+       loc->country_code_cache = g_hash_table_ref (parser->country_code_cache);
+    }
     children = g_ptr_array_new ();
 
     if (xmlTextReaderRead (parser->xml) != 1)
@@ -275,6 +277,13 @@ location_new_from_xml (GWeatherParser *parser, GWeatherLocationLevel level,
            g_hash_table_replace (parser->metar_code_cache, loc->station_code, b);
     }
 
+    if (level == GWEATHER_LOCATION_COUNTRY) {
+       if (loc->country_code) {
+           g_hash_table_replace (parser->country_code_cache, loc->country_code,
+                                 gweather_location_ref (loc));
+       }
+    }
+
     if (children->len) {
        if (level == GWEATHER_LOCATION_CITY)
            g_ptr_array_sort_with_data (children, sort_locations_by_distance, loc);
@@ -411,6 +420,8 @@ gweather_location_unref (GWeatherLocation *loc)
        g_hash_table_unref (loc->metar_code_cache);
     if (loc->timezone_cache)
        g_hash_table_unref (loc->timezone_cache);
+    if (loc->country_code_cache)
+       g_hash_table_unref (loc->country_code_cache);
 
     g_slice_free (GWeatherLocation, loc);
 }
@@ -1125,7 +1136,7 @@ _gweather_location_update_weather_location (GWeatherLocation *gloc,
  *
  * See gweather_location_deserialize() to recover a stored #GWeatherLocation.
  *
- * Returns: a weather station level #GWeatherLocation for @station_code,
+ * Returns: (transfer none): a weather station level #GWeatherLocation for @station_code,
  *          or %NULL if none exists in the database.
  */
 GWeatherLocation *
@@ -1139,6 +1150,23 @@ gweather_location_find_by_station_code (GWeatherLocation *world,
 }
 
 /**
+ * gweather_location_find_by_country_code:
+ * @world: a #GWeatherLocation at the world
+ * @country_code: a country code
+ *
+ * Retrieves the country identified by the specified ISO 3166 code,
+ * if present in the database.
+ *
+ * Returns: (transfer none): a country level #GWeatherLocation, or %NULL.
+ */
+GWeatherLocation *
+gweather_location_find_by_country_code (GWeatherLocation *world,
+                                        const gchar      *country_code)
+{
+       return g_hash_table_lookup (world->country_code_cache, country_code);
+}
+
+/**
  * gweather_location_equal:
  * @one: a #GWeatherLocation
  * @two: another #GWeatherLocation
diff --git a/libgweather/gweather-location.h b/libgweather/gweather-location.h
index 9d1804a..2388772 100644
--- a/libgweather/gweather-location.h
+++ b/libgweather/gweather-location.h
@@ -103,6 +103,8 @@ char                  *gweather_location_get_country_name (GWeatherLocation  *lo
 
 GWeatherLocation      *gweather_location_find_by_station_code (GWeatherLocation *world,
                                                               const gchar      *station_code);
+GWeatherLocation      *gweather_location_find_by_country_code (GWeatherLocation *world,
+                                                              const gchar      *country_code);
 
 gboolean               gweather_location_equal          (GWeatherLocation  *one,
                                                         GWeatherLocation  *two);
diff --git a/libgweather/gweather-parser.c b/libgweather/gweather-parser.c
index e23c3e5..99bac07 100644
--- a/libgweather/gweather-parser.c
+++ b/libgweather/gweather-parser.c
@@ -186,6 +186,8 @@ _gweather_parser_new (void)
                                                      NULL, gweather_location_list_free);
     parser->timezone_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                    NULL, (GDestroyNotify) gweather_timezone_unref);
+    parser->country_code_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                       NULL, (GDestroyNotify) gweather_location_unref);
 
     return parser;
 
@@ -199,7 +201,10 @@ _gweather_parser_free (GWeatherParser *parser)
 {
     if (parser->xml)
        xmlFreeTextReader (parser->xml);
-    g_hash_table_unref (parser->metar_code_cache);
+    if (parser->metar_code_cache)
+       g_hash_table_unref (parser->metar_code_cache);
+    if (parser->country_code_cache)
+       g_hash_table_unref (parser->country_code_cache);
 
     g_slice_free (GWeatherParser, parser);
 }
diff --git a/libgweather/gweather-parser.h b/libgweather/gweather-parser.h
index 2d5fa9f..07c42e4 100644
--- a/libgweather/gweather-parser.h
+++ b/libgweather/gweather-parser.h
@@ -29,6 +29,7 @@ typedef struct {
     time_t year_start, year_end;
     GHashTable *metar_code_cache;
     GHashTable *timezone_cache;
+    GHashTable *country_code_cache;
 } GWeatherParser;
 
 GWeatherParser *_gweather_parser_new                 (void);
diff --git a/libgweather/gweather-private.h b/libgweather/gweather-private.h
index 0441dae..e2244bb 100644
--- a/libgweather/gweather-private.h
+++ b/libgweather/gweather-private.h
@@ -44,6 +44,7 @@ struct _GWeatherLocation {
     GWeatherTimezone **zones;
     GHashTable *metar_code_cache;
     GHashTable *timezone_cache;
+    GHashTable *country_code_cache;
 
     int ref_count;
 };


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