From ae19116af161ba6f053ebe7950b6d9ea46ce2d74 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pablo=20Correa=20G=C3=B3mez?= <ablocorrea@hotmail.com>
Date: Thu, 17 Jun 2021 01:01:32 +0200
Subject: [PATCH] daemon: Sync icon and warning for non-default low level

Before, the low level for changing the battery icon was hardcoded.
However, as the `low_percentage` property is settable by the user using
`PercentageLow` option. That can lead to inconsistencies when PercentageLow
is not the default. For example, if PercentageLow is set higher than 10,
the Low Battery level warning will be sent at the user-set level, but the
battery icon would not be updated to "caution" until the percentage
drops below 10%.

This issue is solved in this commit by using the `low_percentage` property
for the comparison instead of hardcoding the default.
---
 src/up-daemon.c | 34 ++++++++++++++++++++++++++++++++++
 src/up-daemon.h |  4 ++++
 src/up-device.c | 47 ++++++++++-------------------------------------
 3 files changed, 48 insertions(+), 37 deletions(-)

diff --git a/src/up-daemon.c b/src/up-daemon.c
index ef2cd6b..70b75dd 100644
--- a/src/up-daemon.c
+++ b/src/up-daemon.c
@@ -704,6 +704,40 @@ up_daemon_update_warning_level (UpDaemon *daemon)
 		up_daemon_set_warning_level (daemon, warning_level);
 }
 
+const gchar *
+up_daemon_get_charge_icon (UpDaemon     *daemon,
+			   gdouble       percentage,
+			   UpDeviceLevel battery_level,
+			   gboolean      charging)
+{
+	if (battery_level == UP_DEVICE_LEVEL_NONE && daemon != NULL) {
+		if (percentage <= daemon->priv->low_percentage)
+			return charging ? "battery-caution-charging-symbolic" : "battery-caution-symbolic";
+		else if (percentage < 30)
+			return charging ? "battery-low-charging-symbolic" : "battery-low-symbolic";
+		else if (percentage < 60)
+			return charging ? "battery-good-charging-symbolic" : "battery-good-symbolic";
+		return charging ? "battery-full-charging-symbolic" : "battery-full-symbolic";
+	} else {
+		switch (battery_level) {
+		case UP_DEVICE_LEVEL_UNKNOWN:
+			/* The lack of symmetry is on purpose */
+			return charging ? "battery-good-charging-symbolic" : "battery-caution-symbolic";
+		case UP_DEVICE_LEVEL_LOW:
+		case UP_DEVICE_LEVEL_CRITICAL:
+			return charging ? "battery-caution-charging-symbolic" : "battery-caution-symbolic";
+		case UP_DEVICE_LEVEL_NORMAL:
+			return charging ? "battery-low-charging-symbolic" : "battery-low-symbolic";
+		case UP_DEVICE_LEVEL_HIGH:
+			return charging ? "battery-good-charging-symbolic" : "battery-good-symbolic";
+		case UP_DEVICE_LEVEL_FULL:
+			return charging ? "battery-full-charging-symbolic" : "battery-full-symbolic";
+		default:
+			g_assert_not_reached ();
+		}
+	}
+}
+
 /**
  * up_daemon_device_changed_cb:
  **/
diff --git a/src/up-daemon.h b/src/up-daemon.h
index 7160e0e..76e7937 100644
--- a/src/up-daemon.h
+++ b/src/up-daemon.h
@@ -82,6 +82,10 @@ UpDeviceLevel	 up_daemon_compute_warning_level(UpDaemon		*daemon,
 						 gboolean		 power_supply,
 						 gdouble		 percentage,
 						 gint64			 time_to_empty);
+const gchar	*up_daemon_get_charge_icon	(UpDaemon		*daemon,
+						 gdouble		 percentage,
+						 UpDeviceLevel		 battery_level,
+						 gboolean		 charging);
 
 void		 up_daemon_start_poll		(GObject		*object,
 						 GSourceFunc		 callback);
diff --git a/src/up-device.c b/src/up-device.c
index 37ec129..0a56810 100644
--- a/src/up-device.c
+++ b/src/up-device.c
@@ -89,39 +89,6 @@ update_warning_level (UpDevice *device)
 	up_exported_device_set_warning_level (skeleton, warning_level);
 }
 
-static const gchar *
-get_device_charge_icon (gdouble       percentage,
-			UpDeviceLevel battery_level,
-			gboolean      charging)
-{
-	if (battery_level == UP_DEVICE_LEVEL_NONE) {
-		if (percentage < 10)
-			return charging ? "battery-caution-charging-symbolic" : "battery-caution-symbolic";
-		else if (percentage < 30)
-			return charging ? "battery-low-charging-symbolic" : "battery-low-symbolic";
-		else if (percentage < 60)
-			return charging ? "battery-good-charging-symbolic" : "battery-good-symbolic";
-		return charging ? "battery-full-charging-symbolic" : "battery-full-symbolic";
-	} else {
-		switch (battery_level) {
-		case UP_DEVICE_LEVEL_UNKNOWN:
-			/* The lack of symmetry is on purpose */
-			return charging ? "battery-good-charging-symbolic" : "battery-caution-symbolic";
-		case UP_DEVICE_LEVEL_LOW:
-		case UP_DEVICE_LEVEL_CRITICAL:
-			return charging ? "battery-caution-charging-symbolic" : "battery-caution-symbolic";
-		case UP_DEVICE_LEVEL_NORMAL:
-			return charging ? "battery-low-charging-symbolic" : "battery-low-symbolic";
-		case UP_DEVICE_LEVEL_HIGH:
-			return charging ? "battery-good-charging-symbolic" : "battery-good-symbolic";
-		case UP_DEVICE_LEVEL_FULL:
-			return charging ? "battery-full-charging-symbolic" : "battery-full-symbolic";
-		default:
-			g_assert_not_reached ();
-		}
-	}
-}
-
 /* This needs to be called when one of those properties changes:
  * type
  * state
@@ -134,6 +101,10 @@ update_icon_name (UpDevice *device)
 	const gchar *icon_name = NULL;
 	UpExportedDevice *skeleton = UP_EXPORTED_DEVICE (device);
 
+	/* Not finished setting up the object? */
+	if (device->priv->daemon == NULL)
+		return;
+
 	/* get the icon from some simple rules */
 	if (up_exported_device_get_type_ (skeleton) == UP_DEVICE_KIND_LINE_POWER) {
 		icon_name = "ac-adapter-symbolic";
@@ -152,14 +123,16 @@ update_icon_name (UpDevice *device)
 				break;
 			case UP_DEVICE_STATE_CHARGING:
 			case UP_DEVICE_STATE_PENDING_CHARGE:
-				icon_name = get_device_charge_icon (up_exported_device_get_percentage (skeleton),
-								    up_exported_device_get_battery_level (skeleton),
+				icon_name = up_daemon_get_charge_icon (device->priv->daemon,
+								       up_exported_device_get_percentage (skeleton),
+								       up_exported_device_get_battery_level (skeleton),
 								    TRUE);
 				break;
 			case UP_DEVICE_STATE_DISCHARGING:
 			case UP_DEVICE_STATE_PENDING_DISCHARGE:
-				icon_name = get_device_charge_icon (up_exported_device_get_percentage (skeleton),
-								    up_exported_device_get_battery_level (skeleton),
+				icon_name = up_daemon_get_charge_icon (device->priv->daemon,
+								       up_exported_device_get_percentage (skeleton),
+								       up_exported_device_get_battery_level (skeleton),
 								    FALSE);
 				break;
 			default:
-- 
2.17.1

