[gnome-calendar/wip/flb/weather-forecast: 38/50] weather: Update on 00:00



commit ffc0cfa79b1341757e830cc09f4530d2b606c774
Author: Florian Brosch <flo brosch gmail com>
Date:   Mon Oct 23 22:39:27 2017 +0200

    weather: Update on 00:00

 src/gcal-weather-service.c |   89 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 88 insertions(+), 1 deletions(-)
---
diff --git a/src/gcal-weather-service.c b/src/gcal-weather-service.c
index c584e77..11977ac 100644
--- a/src/gcal-weather-service.c
+++ b/src/gcal-weather-service.c
@@ -52,6 +52,7 @@ typedef struct
  * @check_interval_new:      Amount of seconds to wait before fetching weather infos.
  * @check_interval_renew:    Amount of seconds to wait before re-fetching weather information.
  * @duration_timer           Timer used to request weather report updates.
+ * @midnight_timer           Timer used to update weather reports at midnight.
  * @network_changed_sid      "network-changed" signal ID.
  * @location_service:        Used to monitor location changes.
  *                           Initialized by gcal_weather_service_run(),
@@ -87,6 +88,7 @@ struct _GcalWeatherService
   guint            check_interval_new;
   guint            check_interval_renew;
   GcalTimer       *duration_timer;
+  GcalTimer       *midnight_timer;
 
   /* network monitoring */
   gulong           network_changed_sid;
@@ -133,6 +135,8 @@ G_DEFINE_TYPE (GcalWeatherService, gcal_weather_service, G_TYPE_OBJECT)
 /* Timer Helpers: */
 static void     update_timeout_interval                    (GcalWeatherService  *self);
 
+static void     schedule_midnight                          (GcalWeatherService  *self);
+
 static void     start_timer                                (GcalWeatherService  *self);
 
 static void     stop_timer                                 (GcalWeatherService  *self);
@@ -197,6 +201,9 @@ static gint     get_icon_name_sortkey                      (const gchar
 static void     on_duration_timer_timeout                  (GcalTimer           *timer,
                                                             GcalWeatherService  *self);
 
+static void     on_midnight_timer_timeout                  (GcalTimer           *timer,
+                                                            GcalWeatherService  *self);
+
 static gboolean get_time_day_start                         (GcalWeatherService  *self,
                                                             GDate               *ret_date,
                                                             gint64              *ret_unix,
@@ -231,6 +238,9 @@ gcal_weather_service_finalize (GObject *object)
   gcal_timer_free (self->duration_timer);
   self->duration_timer = NULL;
 
+  gcal_timer_free (self->midnight_timer);
+  self->midnight_timer = NULL;
+
   if (self->time_zone != NULL)
     {
       g_time_zone_unref (self->time_zone);
@@ -431,6 +441,8 @@ gcal_weather_service_init (GcalWeatherService *self)
 
   self->duration_timer = gcal_timer_new (GCAL_WEATHER_CHECK_INTERVAL_NEW_DFLT);
   gcal_timer_set_callback (self->duration_timer, (GCalTimerFunc) on_duration_timer_timeout, self, NULL);
+  self->midnight_timer = gcal_timer_new (24*60*60);
+  gcal_timer_set_callback (self->midnight_timer, (GCalTimerFunc) on_midnight_timer_timeout, self, NULL);
   self->time_zone = NULL;
   self->check_interval_new = GCAL_WEATHER_CHECK_INTERVAL_NEW_DFLT;
   self->check_interval_renew = GCAL_WEATHER_CHECK_INTERVAL_RENEW_DFLT;
@@ -1310,6 +1322,12 @@ on_gweather_update (GWeatherInfo       *info,
 
 
 
+/* update_timeout_interval:
+ * @self: The #GcalWeatherService instance.
+ *
+ * Selects the right duration timer timeout based
+ * on locally-stored weather information.
+ */
 static void
 update_timeout_interval (GcalWeatherService *self)
 {
@@ -1327,6 +1345,46 @@ update_timeout_interval (GcalWeatherService *self)
 
 
 
+/* schedule_midnight:
+ * @self: The #GcalWeatherService instance.
+ *
+ * Sets the midnight timer timeout to midnight.
+ * The timer needs to be reset when it
+ * emits.
+ */
+static void
+schedule_midnight (GcalWeatherService  *self)
+{
+  g_autoptr (GTimeZone) zone = NULL;
+  g_autoptr (GDateTime) now = NULL;
+  g_autoptr (GDateTime) tom = NULL;
+  g_autoptr (GDateTime) mid = NULL;
+  gint64 real_now;
+  gint64 real_mid;
+
+  g_return_if_fail (GCAL_IS_WEATHER_SERVICE (self));
+
+  zone = (self->time_zone == NULL)
+           ? g_time_zone_new_local ()
+           : g_time_zone_ref (self->time_zone);
+
+  now = g_date_time_new_now (zone);
+  tom = g_date_time_add_days (now, 1);
+  mid = g_date_time_new (zone,
+                         g_date_time_get_year (tom),
+                         g_date_time_get_month (tom),
+                         g_date_time_get_day_of_month (tom),
+                         0, 0, 0);
+
+  real_mid = g_date_time_to_unix (mid);
+  real_now = g_date_time_to_unix (now);
+
+  gcal_timer_set_default_duration (self->midnight_timer,
+                                   real_mid - real_now);
+}
+
+
+
 /* start_timer:
  * @self: The #GcalWeatherService instance.
  *
@@ -1344,6 +1402,9 @@ start_timer (GcalWeatherService  *self)
     {
       update_timeout_interval (self);
       gcal_timer_start (self->duration_timer);
+
+      schedule_midnight (self);
+      gcal_timer_start (self->duration_timer);
     }
 }
 
@@ -1360,6 +1421,7 @@ stop_timer (GcalWeatherService  *self)
   g_return_if_fail (GCAL_IS_WEATHER_SERVICE (self));
 
   gcal_timer_stop (self->duration_timer);
+  gcal_timer_stop (self->midnight_timer);
 }
 
 
@@ -1442,7 +1504,6 @@ gcal_weather_service_set_check_interval_renew (GcalWeatherService *self,
 
 
 
-
 /* on_duration_timer_timeout
  * @self: A #GcalWeatherService.
  *
@@ -1461,6 +1522,29 @@ on_duration_timer_timeout (GcalTimer          *timer,
 
 
 
+/* on_midnight_timer_timeout
+ * @self: A #GcalWeatherService.
+ *
+ * Handles scheduled weather report updates.
+ */
+static void
+on_midnight_timer_timeout (GcalTimer          *timer,
+                           GcalWeatherService *self)
+{
+  g_return_if_fail (timer != NULL);
+  g_return_if_fail (GCAL_IS_WEATHER_SERVICE (self));
+
+  if (self->gweather_info != NULL)
+    gweather_info_update (self->gweather_info);
+
+  if (gcal_timer_is_running (self->duration_timer))
+    gcal_timer_reset (self->duration_timer);
+
+  schedule_midnight (self);
+}
+
+
+
 /*************
  * < public >
  *************/
@@ -1655,6 +1739,9 @@ gcal_weather_service_set_time_zone (GcalWeatherService *self,
       /* make sure we provide correct weather infos */
       gweather_info_update (self->gweather_info);
 
+      /* make sure midnight is timed correctly: */
+      schedule_midnight (self);
+
       g_object_notify ((GObject*) self, "time-zone");
     }
 }


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