[gnome-control-center/gnome-3-14] power: Fix "When battery power is critical" label with UPower 1.0



commit b54c6e6ef36537e3068e2b1c364254719b4eb8a2
Author: Bastien Nocera <hadess hadess net>
Date:   Fri May 15 16:51:16 2015 +0200

    power: Fix "When battery power is critical" label with UPower 1.0
    
    With UPower 1.0, the critical action to take when the battery level is
    too low isn't in the hands of the user anymore, but in the hands of the
    system.
    
    It's now handled by UPower directly, through the
    CriticalPowerAction configuration option. Instead of asking logind
    whether the system supports hibernation, ask UPower directly what the
    action will be when we're nearly out of battery.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=749436
    
    Conflicts:
        panels/power/cc-power-panel.c

 panels/power/cc-power-panel.c |  100 ++++++++++++----------------------------
 panels/power/power.ui         |   18 -------
 2 files changed, 30 insertions(+), 88 deletions(-)
---
diff --git a/panels/power/cc-power-panel.c b/panels/power/cc-power-panel.c
index 96a2d52..ee4993d 100644
--- a/panels/power/cc-power-panel.c
+++ b/panels/power/cc-power-panel.c
@@ -92,7 +92,6 @@ struct _CcPowerPanelPrivate
   GtkWidget     *automatic_suspend_row;
   GtkWidget     *automatic_suspend_label;
   GtkWidget     *critical_battery_row;
-  GtkWidget     *critical_battery_combo;
 
   GDBusProxy    *bt_rfkill;
   GDBusProxy    *bt_properties;
@@ -1028,30 +1027,6 @@ combo_time_changed_cb (GtkWidget *widget, CcPowerPanel *self)
 }
 
 static void
-combo_enum_changed_cb (GtkWidget *widget, CcPowerPanel *self)
-{
-  GtkTreeIter iter;
-  GtkTreeModel *model;
-  gint value;
-  gboolean ret;
-  const gchar *key = (const gchar *)g_object_get_data (G_OBJECT(widget), "_gsettings_key");
-
-  /* no selection */
-  ret = gtk_combo_box_get_active_iter (GTK_COMBO_BOX(widget), &iter);
-  if (!ret)
-    return;
-
-  /* get entry */
-  model = gtk_combo_box_get_model (GTK_COMBO_BOX(widget));
-  gtk_tree_model_get (model, &iter,
-                      1, &value,
-                      -1);
-
-  /* set both battery and ac keys */
-  g_settings_set_enum (self->priv->gsd_settings, key, value);
-}
-
-static void
 set_value_for_combo (GtkComboBox *combo_box, gint value)
 {
   GtkTreeIter iter;
@@ -1725,15 +1700,6 @@ on_suspend_settings_changed (GSettings    *settings,
                              const char   *key,
                              CcPowerPanel *self)
 {
-  CcPowerPanelPrivate *priv = self->priv;
-  gint value;
-
-  if (g_strcmp0 (key, "critical-battery-action") == 0 &&
-      priv->critical_battery_combo != NULL)
-    {
-      value = g_settings_get_enum (settings, "critical-battery-action");
-      set_value_for_combo (GTK_COMBO_BOX (priv->critical_battery_combo), value);
-    }
   if (g_str_has_prefix (key, "sleep-inactive-"))
     {
       update_automatic_suspend_label (self);
@@ -1808,12 +1774,11 @@ add_automatic_suspend_section (CcPowerPanel *self)
   GtkWidget *sw, *row;
   gchar *s;
   gint value;
-  GtkTreeModel *model;
   GtkWidget *dialog;
   GtkWidget *combo;
-  GtkCellRenderer *cell;
   GVariant *result;
   GDBusConnection *connection;
+  const char *critical_battery_str;
 
   /* The default values for these settings are unfortunate for us;
    * timeout == 0, action == suspend means 'do nothing' - just
@@ -1908,14 +1873,12 @@ add_automatic_suspend_section (CcPowerPanel *self)
   gtk_widget_set_margin_bottom (label, 6);
   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
 
-  value = 0;
-
   connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
   result = g_dbus_connection_call_sync (connection,
-                                        "org.freedesktop.login1",
-                                        "/org/freedesktop/login1",
-                                        "org.freedesktop.login1.Manager",
-                                        "CanHibernate",
+                                        "org.freedesktop.UPower",
+                                        "/org/freedesktop/UPower",
+                                        "org.freedesktop.UPower",
+                                        "GetCriticalAction",
                                         NULL,
                                         NULL,
                                         G_DBUS_CALL_FLAGS_NONE,
@@ -1926,42 +1889,39 @@ add_automatic_suspend_section (CcPowerPanel *self)
 
   if (result)
     {
-      g_variant_get (result, "(s)", &s);
-      if (g_strcmp0 (s, "yes") == 0)
-        value = 1;
-      g_variant_unref(result);
-    }
+      GVariant *result_variant;
+      const char *str;
 
-  if (value)
-    {
-      model = (GtkTreeModel*)gtk_builder_get_object (priv->builder, "liststore_critical");
-      priv->critical_battery_combo = sw = gtk_combo_box_new_with_model (model);
-      gtk_label_set_mnemonic_widget (GTK_LABEL (label), sw);
-      cell = gtk_cell_renderer_text_new ();
-      gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (sw), cell, TRUE);
-      gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (sw), cell, "text", 0);
-      gtk_widget_set_margin_start (sw, 20);
-      gtk_widget_set_margin_end (sw, 20);
-      gtk_widget_set_valign (sw, GTK_ALIGN_CENTER);
+      result_variant = g_variant_get_child_value (result, 0);
+      str = g_variant_get_string (result_variant, NULL);
 
-      g_object_set_data (G_OBJECT (sw), "_gsettings_key", "critical-battery-action");
-      value = g_settings_get_enum (priv->gsd_settings, "critical-battery-action");
-      set_value_for_combo (GTK_COMBO_BOX (sw), value);
-      g_signal_connect (sw, "changed",
-                        G_CALLBACK (combo_enum_changed_cb), self);
+      if (g_strcmp0 (str, "Hibernate") == 0 ||
+          g_strcmp0 (str, "HybridSleep") == 0)
+        critical_battery_str = _("Hibernate");
+      else if (g_strcmp0 (str, "PowerOff") == 0)
+        critical_battery_str = _("Power Off");
+      else
+        {
+          g_debug ("Unexpected value returned by UPower for GetCriticalAction(): %s", str);
+          critical_battery_str = _("Power Off");
+        }
 
-      gtk_box_pack_start (GTK_BOX (box), sw, FALSE, TRUE, 0);
+      g_variant_unref(result_variant);
+      g_variant_unref(result);
     }
   else
     {
-      label = gtk_label_new (_("Power Off"));
-      gtk_widget_set_margin_start (label, 20);
-      gtk_widget_set_margin_end (label, 20);
-      gtk_widget_set_margin_top (label, 6);
-      gtk_widget_set_margin_bottom (label, 6);
-      gtk_box_pack_start (GTK_BOX (box), label, FALSE, TRUE, 0);
+      g_debug ("GetCriticalAction() call failed, is UPower correctly installed?");
+      critical_battery_str = _("Power Off");
     }
 
+  label = gtk_label_new (critical_battery_str);
+  gtk_widget_set_margin_start (label, 20);
+  gtk_widget_set_margin_end (label, 20);
+  gtk_widget_set_margin_top (label, 6);
+  gtk_widget_set_margin_bottom (label, 6);
+  gtk_box_pack_start (GTK_BOX (box), label, FALSE, TRUE, 0);
+
   gtk_container_add (GTK_CONTAINER (widget), row);
   gtk_size_group_add_widget (priv->row_sizegroup, row);
   gtk_widget_show_all (widget);
diff --git a/panels/power/power.ui b/panels/power/power.ui
index b2aa3d6..dc3eddf 100644
--- a/panels/power/power.ui
+++ b/panels/power/power.ui
@@ -1,24 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
-  <object class="GtkListStore" id="liststore_critical">
-    <columns>
-      <!-- column-name name -->
-      <column type="gchararray"/>
-      <!-- column-name value -->
-      <column type="gint"/>
-    </columns>
-    <data>
-      <row>
-        <col id="0" translatable="yes">Hibernate</col>
-        <col id="1">3</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes">Power off</col>
-        <col id="1">2</col>
-      </row>
-    </data>
-  </object>
   <object class="GtkListStore" id="liststore_time">
     <columns>
       <!-- column-name name -->


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