[gnome-settings-daemon/benzea/no-tz-cache: 4/5] datetime: Query GWeather DB on the fly instead of caching




commit df6c69f028d27b53ac86829e11df103b25ed5a74
Author: Benjamin Berg <bberg redhat com>
Date:   Sat Apr 25 22:53:48 2020 +0200

    datetime: Query GWeather DB on the fly instead of caching
    
    We need the database only quite irregularly (i.e. when the location
    change). Just grab the required information from GWeather on the fly and
    destroy it immediately after. Also, use the API to only grab the cities
    of one country instead of filtering them later.
    
    See also: !168

 plugins/datetime/gsd-timezone-monitor.c | 17 ++++++++++-------
 plugins/datetime/weather-tz.c           | 34 ++++++++++-----------------------
 plugins/datetime/weather-tz.h           |  6 +-----
 3 files changed, 21 insertions(+), 36 deletions(-)
---
diff --git a/plugins/datetime/gsd-timezone-monitor.c b/plugins/datetime/gsd-timezone-monitor.c
index ca3e6a7f..b8ed108c 100644
--- a/plugins/datetime/gsd-timezone-monitor.c
+++ b/plugins/datetime/gsd-timezone-monitor.c
@@ -50,7 +50,6 @@ typedef struct
         GCancellable *geoclue_cancellable;
 
         TzDB *tzdb;
-        WeatherTzDB *weather_tzdb;
         gchar *current_timezone;
 
         GSettings *location_settings;
@@ -171,12 +170,14 @@ find_by_country (GList       *locations,
         return found;
 }
 
-static const gchar *
+static gchar *
 find_timezone (GsdTimezoneMonitor *self,
                GeocodeLocation    *location,
                const gchar        *country_code)
 {
+        gchar *res;
         GList *filtered;
+        GList *weather_locations;
         GList *locations;
         GsdTimezoneMonitorPrivate *priv = gsd_timezone_monitor_get_instance_private (self);
         TzLocation *closest_tz_location;
@@ -186,8 +187,9 @@ find_timezone (GsdTimezoneMonitor *self,
         g_return_val_if_fail (locations != NULL, NULL);
 
         /* ... and then add libgweather's locations as well */
+        weather_locations = weather_tz_db_get_locations (country_code);
         locations = g_list_concat (locations,
-                                   weather_tz_db_get_locations (priv->weather_tzdb));
+                                   g_list_copy (weather_locations));
 
         /* Filter tz locations by country */
         filtered = find_by_country (locations, country_code);
@@ -202,9 +204,12 @@ find_timezone (GsdTimezoneMonitor *self,
         locations = sort_by_closest_to (locations, location);
         closest_tz_location = (TzLocation *) locations->data;
 
+        res = g_strdup (closest_tz_location->zone);
+
         g_list_free (locations);
+        g_list_free_full (weather_locations, (GDestroyNotify) tz_location_free);
 
-        return closest_tz_location->zone;
+        return res;
 }
 
 static void
@@ -214,7 +219,7 @@ process_location (GsdTimezoneMonitor *self,
         GeocodeLocation *location;
         GsdTimezoneMonitorPrivate *priv = gsd_timezone_monitor_get_instance_private (self);
         const gchar *country_code;
-        const gchar *new_timezone;
+        g_autofree gchar *new_timezone = NULL;
 
         country_code = geocode_place_get_country_code (place);
         location = geocode_place_get_location (place);
@@ -387,7 +392,6 @@ gsd_timezone_monitor_finalize (GObject *obj)
         g_clear_object (&priv->permission);
         g_clear_pointer (&priv->current_timezone, g_free);
         g_clear_pointer (&priv->tzdb, tz_db_free);
-        g_clear_pointer (&priv->weather_tzdb, weather_tz_db_free);
 
         g_clear_object (&priv->location_settings);
 
@@ -460,7 +464,6 @@ gsd_timezone_monitor_init (GsdTimezoneMonitor *self)
 
         priv->current_timezone = timedate1_dup_timezone (priv->dtm);
         priv->tzdb = tz_load_db ();
-        priv->weather_tzdb = weather_tz_db_new ();
 
         priv->location_settings = g_settings_new ("org.gnome.system.location");
         g_signal_connect_swapped (priv->location_settings, "changed::enabled",
diff --git a/plugins/datetime/weather-tz.c b/plugins/datetime/weather-tz.c
index 26668463..f1de2b94 100644
--- a/plugins/datetime/weather-tz.c
+++ b/plugins/datetime/weather-tz.c
@@ -25,11 +25,6 @@
 #define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
 #include <libgweather/gweather.h>
 
-struct _WeatherTzDB
-{
-        GList *tz_locations;
-};
-
 static GList *
 location_get_cities (GWeatherLocation *parent_location)
 {
@@ -104,33 +99,24 @@ load_timezones (GList *cities)
 }
 
 GList *
-weather_tz_db_get_locations (WeatherTzDB *tzdb)
-{
-        return g_list_copy (tzdb->tz_locations);
-}
-
-WeatherTzDB *
-weather_tz_db_new (void)
+weather_tz_db_get_locations (const gchar *country_code)
 {
         GList *cities;
+        GList *tz_locations;
         GWeatherLocation *world;
-        WeatherTzDB *tzdb;
+        GWeatherLocation *country;
 
         world = gweather_location_get_world ();
-        cities = location_get_cities (world);
 
-        tzdb = g_new0 (WeatherTzDB, 1);
-        tzdb->tz_locations = load_timezones (cities);
+        country = gweather_location_find_by_country_code (world, country_code);
 
-        g_list_free (cities);
+        if (!country)
+                return NULL;
 
-        return tzdb;
-}
+        cities = location_get_cities (country);
+        tz_locations = load_timezones (cities);
 
-void
-weather_tz_db_free (WeatherTzDB *tzdb)
-{
-        g_list_free_full (tzdb->tz_locations, (GDestroyNotify) tz_location_free);
+        g_list_free (cities);
 
-        g_free (tzdb);
+        return tz_locations;
 }
diff --git a/plugins/datetime/weather-tz.h b/plugins/datetime/weather-tz.h
index 14de4493..15b1571b 100644
--- a/plugins/datetime/weather-tz.h
+++ b/plugins/datetime/weather-tz.h
@@ -22,10 +22,6 @@
 
 #include <glib.h>
 
-typedef struct _WeatherTzDB WeatherTzDB;
-
-WeatherTzDB     *weather_tz_db_new              (void);
-GList           *weather_tz_db_get_locations    (WeatherTzDB *db);
-void             weather_tz_db_free             (WeatherTzDB *db);
+GList           *weather_tz_db_get_locations    (const char *country);
 
 #endif /* __WEATHER_TZ_H */


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