[gnome-settings-daemon] color: Add a NaturalLightActive property to the D-Bus interface



commit 1e1fccb80e576b313f6730b26f385e0d6426ed4d
Author: Richard Hughes <richard hughsie com>
Date:   Fri Feb 10 12:46:17 2017 +0000

    color: Add a NaturalLightActive property to the D-Bus interface
    
    This makes the shell UI easy to write as we don't need to compare the
    temperature property against 6500K.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742149

 plugins/color/gcm-self-test.c     |   15 +++++++++++++
 plugins/color/gsd-color-manager.c |   17 +++++++++++++++
 plugins/color/gsd-natural-light.c |   40 +++++++++++++++++++++++++++++++++---
 plugins/color/gsd-natural-light.h |    2 +
 4 files changed, 70 insertions(+), 4 deletions(-)
---
diff --git a/plugins/color/gcm-self-test.c b/plugins/color/gcm-self-test.c
index efb2101..695754e 100644
--- a/plugins/color/gcm-self-test.c
+++ b/plugins/color/gcm-self-test.c
@@ -43,6 +43,7 @@ static void
 gcm_test_natural_light (void)
 {
         gboolean ret;
+        guint active_cnt = 0;
         guint disabled_until_tmw_cnt = 0;
         guint sunrise_cnt = 0;
         guint sunset_cnt = 0;
@@ -54,6 +55,8 @@ gcm_test_natural_light (void)
 
         nlight = gsd_natural_light_new ();
         g_assert (GSD_IS_NATURAL_LIGHT (nlight));
+        g_signal_connect (nlight, "notify::active",
+                          G_CALLBACK (on_notify), &active_cnt);
         g_signal_connect (nlight, "notify::sunset",
                           G_CALLBACK (on_notify), &sunset_cnt);
         g_signal_connect (nlight, "notify::sunrise",
@@ -75,6 +78,7 @@ gcm_test_natural_light (void)
         g_settings_set_boolean (settings, "natural-light-enabled", FALSE);
 
         /* check default values */
+        g_assert (!gsd_natural_light_get_active (nlight));
         g_assert_cmpint ((gint) gsd_natural_light_get_sunrise (nlight), ==, -1);
         g_assert_cmpint ((gint) gsd_natural_light_get_sunset (nlight), ==, -1);
         g_assert_cmpint (gsd_natural_light_get_temperature (nlight), ==, GSD_COLOR_TEMPERATURE_DEFAULT);
@@ -84,6 +88,8 @@ gcm_test_natural_light (void)
         ret = gsd_natural_light_start (nlight, &error);
         g_assert_no_error (error);
         g_assert (ret);
+        g_assert (!gsd_natural_light_get_active (nlight));
+        g_assert_cmpint (active_cnt, ==, 0);
         g_assert_cmpint (sunset_cnt, ==, 0);
         g_assert_cmpint (sunrise_cnt, ==, 0);
         g_assert_cmpint (temperature_cnt, ==, 0);
@@ -94,6 +100,8 @@ gcm_test_natural_light (void)
                               g_variant_new ("(dd)", 51.5, -0.1278));
         g_settings_set_boolean (settings, "natural-light-schedule-automatic", TRUE);
         g_settings_set_boolean (settings, "natural-light-enabled", TRUE);
+        g_assert (gsd_natural_light_get_active (nlight));
+        g_assert_cmpint (active_cnt, ==, 1);
         g_assert_cmpint (sunset_cnt, ==, 1);
         g_assert_cmpint (sunrise_cnt, ==, 1);
         g_assert_cmpint (temperature_cnt, ==, 1);
@@ -107,6 +115,7 @@ gcm_test_natural_light (void)
         gsd_natural_light_set_disabled_until_tmw (nlight, TRUE);
         gsd_natural_light_set_disabled_until_tmw (nlight, TRUE);
         g_assert_cmpint (gsd_natural_light_get_temperature (nlight), ==, GSD_COLOR_TEMPERATURE_DEFAULT);
+        g_assert (gsd_natural_light_get_active (nlight));
         g_assert (gsd_natural_light_get_disabled_until_tmw (nlight));
         g_assert_cmpint (temperature_cnt, ==, 2);
         g_assert_cmpint (disabled_until_tmw_cnt, ==, 1);
@@ -114,7 +123,9 @@ gcm_test_natural_light (void)
         /* change our mind */
         gsd_natural_light_set_disabled_until_tmw (nlight, FALSE);
         g_assert_cmpint (gsd_natural_light_get_temperature (nlight), ==, 4000);
+        g_assert (gsd_natural_light_get_active (nlight));
         g_assert (!gsd_natural_light_get_disabled_until_tmw (nlight));
+        g_assert_cmpint (active_cnt, ==, 1);
         g_assert_cmpint (temperature_cnt, ==, 3);
         g_assert_cmpint (disabled_until_tmw_cnt, ==, 2);
 
@@ -122,10 +133,12 @@ gcm_test_natural_light (void)
         g_settings_set_double (settings, "natural-light-schedule-from", 4.0);
         g_settings_set_double (settings, "natural-light-schedule-to", 16.f);
         g_settings_set_boolean (settings, "natural-light-schedule-automatic", FALSE);
+        g_assert_cmpint (active_cnt, ==, 2);
         g_assert_cmpint (sunset_cnt, ==, 1);
         g_assert_cmpint (sunrise_cnt, ==, 1);
         g_assert_cmpint (temperature_cnt, ==, 4);
         g_assert_cmpint (disabled_until_tmw_cnt, ==, 2);
+        g_assert (!gsd_natural_light_get_active (nlight));
         g_assert_cmpint ((gint) gsd_natural_light_get_sunrise (nlight), ==, 7);
         g_assert_cmpint ((gint) gsd_natural_light_get_sunset (nlight), ==, 17);
         g_assert_cmpint (gsd_natural_light_get_temperature (nlight), ==, GSD_COLOR_TEMPERATURE_DEFAULT);
@@ -133,6 +146,8 @@ gcm_test_natural_light (void)
 
         /* finally disable, with no changes */
         g_settings_set_boolean (settings, "natural-light-enabled", FALSE);
+        g_assert (!gsd_natural_light_get_active (nlight));
+        g_assert_cmpint (active_cnt, ==, 2);
         g_assert_cmpint (sunset_cnt, ==, 1);
         g_assert_cmpint (sunrise_cnt, ==, 1);
         g_assert_cmpint (temperature_cnt, ==, 4);
diff --git a/plugins/color/gsd-color-manager.c b/plugins/color/gsd-color-manager.c
index 0834af5..58676ad 100644
--- a/plugins/color/gsd-color-manager.c
+++ b/plugins/color/gsd-color-manager.c
@@ -42,6 +42,7 @@
 static const gchar introspection_xml[] =
 "<node>"
 "  <interface name='org.gnome.SettingsDaemon.Color'>"
+"    <property name='NaturalLightActive' type='b' access='read'/>"
 "    <property name='Temperature' type='u' access='readwrite'/>"
 "    <property name='DisabledUntilTomorrow' type='b' access='readwrite'/>"
 "    <property name='Sunrise' type='d' access='read'/>"
@@ -162,6 +163,17 @@ emit_property_changed (GsdColorManager *manager,
 }
 
 static void
+on_active_notify (GsdNaturalLight *nlight,
+                  GParamSpec      *pspec,
+                  gpointer         user_data)
+{
+        GsdColorManager *manager = GSD_COLOR_MANAGER (user_data);
+        GsdColorManagerPrivate *priv = manager->priv;
+        emit_property_changed (manager, "NaturalLightActive",
+                               g_variant_new_boolean (gsd_natural_light_get_active (priv->nlight)));
+}
+
+static void
 on_sunset_notify (GsdNaturalLight *nlight,
                   GParamSpec      *pspec,
                   gpointer         user_data)
@@ -220,6 +232,8 @@ gsd_color_manager_init (GsdColorManager *manager)
 
         /* natural light features */
         priv->nlight = gsd_natural_light_new ();
+        g_signal_connect (priv->nlight, "notify::active",
+                          G_CALLBACK (on_active_notify), manager);
         g_signal_connect (priv->nlight, "notify::sunset",
                           G_CALLBACK (on_sunset_notify), manager);
         g_signal_connect (priv->nlight, "notify::sunrise",
@@ -280,6 +294,9 @@ handle_get_property (GDBusConnection *connection,
                 return NULL;
         }
 
+        if (g_strcmp0 (property_name, "NaturalLightActive") == 0)
+                return g_variant_new_boolean (gsd_natural_light_get_active (priv->nlight));
+
         if (g_strcmp0 (property_name, "Temperature") == 0) {
                 guint temperature;
                 temperature = gsd_color_state_get_temperature (priv->state);
diff --git a/plugins/color/gsd-natural-light.c b/plugins/color/gsd-natural-light.c
index 091c27b..181011a 100644
--- a/plugins/color/gsd-natural-light.c
+++ b/plugins/color/gsd-natural-light.c
@@ -43,12 +43,14 @@ struct _GsdNaturalLight {
         gdouble            cached_sunrise;
         gdouble            cached_sunset;
         gdouble            cached_temperature;
+        gboolean           cached_active;
         GCancellable      *cancellable;
         GDateTime         *datetime_override;
 };
 
 enum {
         PROP_0,
+        PROP_ACTIVE,
         PROP_SUNRISE,
         PROP_SUNSET,
         PROP_TEMPERATURE,
@@ -143,6 +145,20 @@ gsd_natural_light_set_temperature (GsdNaturalLight *self, gdouble temperature)
 }
 
 static void
+gsd_natural_light_set_active (GsdNaturalLight *self, gboolean active)
+{
+        if (self->cached_active == active)
+                return;
+        self->cached_active = active;
+
+        /* ensure set to unity temperature */
+        if (!active)
+                gsd_natural_light_set_temperature (self, GSD_COLOR_TEMPERATURE_DEFAULT);
+
+        g_object_notify (G_OBJECT (self), "active");
+}
+
+static void
 natural_light_recheck (GsdNaturalLight *self)
 {
         gdouble frac_day;
@@ -156,8 +172,7 @@ natural_light_recheck (GsdNaturalLight *self)
         /* enabled */
         if (!g_settings_get_boolean (self->settings, "natural-light-enabled")) {
                 g_debug ("natural light disabled, resetting");
-                gsd_natural_light_set_temperature (self,
-                                                   GSD_COLOR_TEMPERATURE_DEFAULT);
+                gsd_natural_light_set_active (self, FALSE);
                 return;
         }
 
@@ -202,8 +217,7 @@ natural_light_recheck (GsdNaturalLight *self)
                                                     schedule_from - smear,
                                                     schedule_to)) {
                 g_debug ("not time for natural-light");
-                gsd_natural_light_set_temperature (self,
-                                                 GSD_COLOR_TEMPERATURE_DEFAULT);
+                gsd_natural_light_set_active (self, FALSE);
                 return;
         }
 
@@ -235,6 +249,7 @@ natural_light_recheck (GsdNaturalLight *self)
         }
         g_debug ("natural light mode on, using temperature of %uK (aiming for %uK)",
                  temp_smeared, temperature);
+        gsd_natural_light_set_active (self, TRUE);
         gsd_natural_light_set_temperature (self, temp_smeared);
 }
 
@@ -393,6 +408,12 @@ gsd_natural_light_get_disabled_until_tmw (GsdNaturalLight *self)
         return self->disabled_until_tmw;
 }
 
+gboolean
+gsd_natural_light_get_active (GsdNaturalLight *self)
+{
+        return self->cached_active;
+}
+
 gdouble
 gsd_natural_light_get_sunrise (GsdNaturalLight *self)
 {
@@ -494,6 +515,9 @@ gsd_natural_light_get_property (GObject    *object,
         GsdNaturalLight *self = GSD_NATURAL_LIGHT (object);
 
         switch (prop_id) {
+        case PROP_ACTIVE:
+                g_value_set_boolean (value, self->cached_active);
+                break;
         case PROP_SUNRISE:
                 g_value_set_double (value, self->cached_sunrise);
                 break;
@@ -521,6 +545,14 @@ gsd_natural_light_class_init (GsdNaturalLightClass *klass)
         object_class->get_property = gsd_natural_light_get_property;
 
         g_object_class_install_property (object_class,
+                                         PROP_ACTIVE,
+                                         g_param_spec_boolean ("active",
+                                                               "Active",
+                                                               "If natural light functionality is active 
right now",
+                                                               FALSE,
+                                                               G_PARAM_READABLE));
+
+        g_object_class_install_property (object_class,
                                          PROP_SUNRISE,
                                          g_param_spec_double ("sunrise",
                                                               "Sunrise",
diff --git a/plugins/color/gsd-natural-light.h b/plugins/color/gsd-natural-light.h
index 8f5a295..9f91b3d 100644
--- a/plugins/color/gsd-natural-light.h
+++ b/plugins/color/gsd-natural-light.h
@@ -31,6 +31,8 @@ G_DECLARE_FINAL_TYPE (GsdNaturalLight, gsd_natural_light, GSD, NATURAL_LIGHT, GO
 GsdNaturalLight *gsd_natural_light_new                    (void);
 gboolean         gsd_natural_light_start                  (GsdNaturalLight *self,
                                                            GError         **error);
+
+gboolean         gsd_natural_light_get_active             (GsdNaturalLight *self);
 gdouble          gsd_natural_light_get_sunrise            (GsdNaturalLight *self);
 gdouble          gsd_natural_light_get_sunset             (GsdNaturalLight *self);
 gdouble          gsd_natural_light_get_temperature        (GsdNaturalLight *self);


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