[gnome-settings-daemon/benzea/nightlight-fixes: 2/5] color: Define disabled until tomorrow as moving past sunrise



commit e3cba8d02cff2c3cb48a531f9467edd2d6b50653
Author: Benjamin Berg <bberg redhat com>
Date:   Tue Jun 5 00:25:30 2018 +0200

    color: Define disabled until tomorrow as moving past sunrise
    
    Reset disabled until tomorrow whenever it has been more than 24h or the
    sunrise edge lies between the time that it was disabled now now.

 plugins/color/gsd-night-light.c | 61 ++++++++++++++++++++++++++++-------------
 1 file changed, 42 insertions(+), 19 deletions(-)
---
diff --git a/plugins/color/gsd-night-light.c b/plugins/color/gsd-night-light.c
index d98570d9..cc602dff 100644
--- a/plugins/color/gsd-night-light.c
+++ b/plugins/color/gsd-night-light.c
@@ -34,10 +34,10 @@ struct _GsdNightLight {
         GObject            parent;
         GSettings         *settings;
         gboolean           disabled_until_tmw;
+        GDateTime         *disabled_until_tmw_dt;
         gboolean           geoclue_enabled;
         GSource           *source;
         guint              validate_id;
-        gint               disabled_day_of_month;
         GClueClient       *geoclue_client;
         GClueSimple       *geoclue_simple;
         GSettings         *location_settings;
@@ -259,22 +259,6 @@ night_light_recheck (GsdNightLight *self)
                 return;
         }
 
-        /* disabled until tomorrow */
-        if (self->disabled_until_tmw) {
-                gint tmp_day_of_month = g_date_time_get_day_of_month (dt_now);
-                if (tmp_day_of_month == self->disabled_day_of_month) {
-                        g_debug ("night light still day-disabled, resetting");
-                        gsd_night_light_set_temperature (self,
-                                                         GSD_COLOR_TEMPERATURE_DEFAULT);
-                        return;
-                }
-
-                /* no longer valid */
-                self->disabled_day_of_month = 0;
-                self->disabled_until_tmw = FALSE;
-                g_object_notify (G_OBJECT (self), "disabled-until-tmw");
-        }
-
         /* calculate the position of the sun */
         if (g_settings_get_boolean (self->settings, "night-light-schedule-automatic")) {
                 update_cached_sunrise_sunset (self);
@@ -296,6 +280,42 @@ night_light_recheck (GsdNightLight *self)
         frac_day = gsd_night_light_frac_day_from_dt (dt_now);
         g_debug ("fractional day = %.3f, limits = %.3f->%.3f",
                  frac_day, schedule_from, schedule_to);
+
+        /* disabled until tomorrow */
+        if (self->disabled_until_tmw) {
+                GTimeSpan time_span;
+                gboolean reset = FALSE;
+
+                time_span = g_date_time_difference (dt_now, self->disabled_until_tmw_dt);
+
+                /* Reset if disabled until tomorrow is more than 24h ago. */
+                if (time_span > (GTimeSpan) 24 * 60 * 60 * 1000000) {
+                        g_debug ("night light disabled until tomorrow is older than 24h, resetting disabled 
until tomorrow");
+                        reset = TRUE;
+                } else {
+                        /* Or if a sunrise lies between the time it was disabled and now. */
+                        gdouble frac_disabled;
+                        frac_disabled = gsd_night_light_frac_day_from_dt (self->disabled_until_tmw_dt);
+                        if (gsd_night_light_frac_day_is_between (schedule_to,
+                                                                 frac_disabled,
+                                                                 frac_day)) {
+                                g_debug ("night light sun rise happened, resetting disabled until tomorrow");
+                                reset = TRUE;
+                        }
+                }
+
+                if (reset) {
+                        self->disabled_until_tmw = FALSE;
+                        g_clear_pointer(&self->disabled_until_tmw_dt, g_date_time_unref);
+                        g_object_notify (G_OBJECT (self), "disabled-until-tmw");
+                } else {
+                        g_debug ("night light still day-disabled, resetting");
+                        gsd_night_light_set_temperature (self,
+                                                         GSD_COLOR_TEMPERATURE_DEFAULT);
+                        return;
+                }
+        }
+
         if (!gsd_night_light_frac_day_is_between (frac_day,
                                                     schedule_from - smear,
                                                     schedule_to)) {
@@ -505,7 +525,9 @@ gsd_night_light_set_disabled_until_tmw (GsdNightLight *self, gboolean value)
                 return;
 
         self->disabled_until_tmw = value;
-        self->disabled_day_of_month = g_date_time_get_day_of_month (dt);
+        g_clear_pointer (&self->disabled_until_tmw_dt, g_date_time_unref);
+        if (self->disabled_until_tmw)
+                self->disabled_until_tmw_dt = g_steal_pointer (&dt);
         night_light_recheck (self);
         g_object_notify (G_OBJECT (self), "disabled-until-tmw");
 }
@@ -575,6 +597,7 @@ gsd_night_light_finalize (GObject *object)
 
         g_clear_object (&self->settings);
         g_clear_pointer (&self->datetime_override, (GDestroyNotify) g_date_time_unref);
+        g_clear_pointer (&self->disabled_until_tmw_dt, g_date_time_unref);
 
         if (self->validate_id > 0) {
                 g_source_remove (self->validate_id);
@@ -604,7 +627,7 @@ gsd_night_light_set_property (GObject      *object,
                 self->cached_temperature = g_value_get_double (value);
                 break;
         case PROP_DISABLED_UNTIL_TMW:
-                self->disabled_until_tmw = g_value_get_boolean (value);
+                gsd_night_light_set_disabled_until_tmw (self, g_value_get_boolean (value));
                 break;
         default:
                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);


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