From 9091bba8ac0ac70107bfe27276b1d6e9e3471fc8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 16:00:06 -0500 Subject: reimplement indicator_power_device_get_icon_names() since our CA is incompatible with reusing code from GSD. --- src/device.c | 153 ++++++++++++++++++++++++++------------------------- tests/test-device.cc | 34 ++++++------ 2 files changed, 94 insertions(+), 93 deletions(-) diff --git a/src/device.c b/src/device.c index e9e228a..c79e145 100644 --- a/src/device.c +++ b/src/device.c @@ -281,34 +281,31 @@ indicator_power_device_get_time (const IndicatorPowerDevice * device) **** ***/ -/* taken from GSD's power plugin, (c) Richard Hughes and licensed GPL >=2 */ static const gchar * -gpm_upower_get_device_icon_suffix (gdouble percentage) +get_device_icon_suffix (gdouble percentage) { - if (percentage < 10) return "caution"; - if (percentage < 30) return "low"; - if (percentage < 60) return "good"; - return "full"; + if (percentage >= 60) return "full"; + if (percentage >= 30) return "good"; + if (percentage >= 10) return "low"; + return "caution"; } -/* taken from GSD's power plugin, (c) Richard Hughes and licensed GPL >=2 */ static const gchar * -gpm_upower_get_device_icon_index (gdouble percentage) +get_device_icon_index (gdouble percentage) { - if (percentage < 10) return "000"; - if (percentage < 30) return "020"; - if (percentage < 50) return "040"; - if (percentage < 70) return "060"; - if (percentage < 90) return "080"; - return "100"; + if (percentage >= 90) return "100"; + if (percentage >= 70) return "080"; + if (percentage >= 50) return "060"; + if (percentage >= 30) return "040"; + if (percentage >= 10) return "020"; + return "000"; } /** indicator_power_device_get_icon_names: @device: #IndicatorPowerDevice from which to generate the icon names - Based on GSD's power plugin, (c) Richard Hughes and licensed GPL >= 2. - It differs in these ways: + This function's logic differs from GSD's power plugin in some ways: 1. All charging batteries use the same icon regardless of progress. @@ -327,7 +324,6 @@ gpm_upower_get_device_icon_index (gdouble percentage) GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device) { - gchar ** ret = NULL; const gchar *suffix_str; const gchar *index_str; @@ -340,68 +336,73 @@ indicator_power_device_get_icon_names (const IndicatorPowerDevice * device) const UpDeviceState state = indicator_power_device_get_state (device); const gchar * kind_str = kind_str = up_device_kind_to_string (kind); - /* get correct icon prefix */ - GString * filename = g_string_new (NULL); - - /* get the icon from some simple rules */ - if (kind == UP_DEVICE_KIND_LINE_POWER) { - g_string_append (filename, "ac-adapter-symbolic;"); - g_string_append (filename, "ac-adapter;"); - } else if (kind == UP_DEVICE_KIND_MONITOR) { - g_string_append (filename, "gpm-monitor-symbolic;"); - g_string_append (filename, "gpm-monitor;"); - } else switch (state) { - case UP_DEVICE_STATE_EMPTY: - g_string_append (filename, "battery-empty-symbolic;"); - g_string_append_printf (filename, "gpm-%s-empty;", kind_str); - g_string_append_printf (filename, "gpm-%s-000;", kind_str); - g_string_append (filename, "battery-empty;"); - break; - case UP_DEVICE_STATE_FULLY_CHARGED: - g_string_append (filename, "battery-full-charged-symbolic;"); - g_string_append (filename, "battery-full-charging-symbolic;"); - g_string_append_printf (filename, "gpm-%s-full;", kind_str); - g_string_append_printf (filename, "gpm-%s-100;", kind_str); - g_string_append (filename, "battery-full-charged;"); - g_string_append (filename, "battery-full-charging;"); - break; - case UP_DEVICE_STATE_CHARGING: - case UP_DEVICE_STATE_PENDING_CHARGE: - /* When charging, always use the same icon regardless of percentage. - */ - percentage = 0; - - suffix_str = gpm_upower_get_device_icon_suffix (percentage); - index_str = gpm_upower_get_device_icon_index (percentage); - g_string_append_printf (filename, "battery-%s-charging-symbolic;", suffix_str); - g_string_append_printf (filename, "gpm-%s-%s-charging;", kind_str, index_str); - g_string_append_printf (filename, "battery-%s-charging;", suffix_str); - break; - case UP_DEVICE_STATE_DISCHARGING: - case UP_DEVICE_STATE_PENDING_DISCHARGE: { - /* Don't show the caution/red icons unless we have <=30 min left. - - Themes use the caution color when the percentage is 0% or 20%, - so if we have >30 min left, use 30% as the icon's percentage floor */ - if (indicator_power_device_get_time (device) > (30*60)) - percentage = MAX(percentage, 30); - - suffix_str = gpm_upower_get_device_icon_suffix (percentage); - index_str = gpm_upower_get_device_icon_index (percentage); - g_string_append_printf (filename, "battery-%s-symbolic;", suffix_str); - g_string_append_printf (filename, "gpm-%s-%s;", kind_str, index_str); - g_string_append_printf (filename, "battery-%s;", suffix_str); - break; + GPtrArray * names = g_ptr_array_new (); + + if (kind == UP_DEVICE_KIND_LINE_POWER) + { + g_ptr_array_add (names, g_strdup("ac-adapter-symbolic")); + g_ptr_array_add (names, g_strdup("ac-adapter")); } - default: - g_string_append (filename, "battery-missing-symbolic;"); - g_string_append (filename, "gpm-battery-missing;"); - g_string_append (filename, "battery-missing;"); + else if (kind == UP_DEVICE_KIND_MONITOR) + { + g_ptr_array_add (names, g_strdup("gpm-monitor-symbolic")); + g_ptr_array_add (names, g_strdup("gpm-monitor")); + } + else switch (state) + { + case UP_DEVICE_STATE_EMPTY: + g_ptr_array_add (names, g_strdup("battery-empty-symbolic")); + g_ptr_array_add (names, g_strdup_printf("gpm-%s-empty", kind_str)); + g_ptr_array_add (names, g_strdup_printf("gpm-%s-000", kind_str)); + g_ptr_array_add (names, g_strdup("battery-empty")); + break; + + case UP_DEVICE_STATE_FULLY_CHARGED: + g_ptr_array_add (names, g_strdup("battery-full-charged-symbolic")); + g_ptr_array_add (names, g_strdup("battery-full-charging-symbolic")); + g_ptr_array_add (names, g_strdup_printf("gpm-%s-full", kind_str)); + g_ptr_array_add (names, g_strdup_printf("gpm-%s-100", kind_str)); + g_ptr_array_add (names, g_strdup("battery-full-charged")); + g_ptr_array_add (names, g_strdup("battery-full-charging")); + break; + + case UP_DEVICE_STATE_CHARGING: + case UP_DEVICE_STATE_PENDING_CHARGE: + /* When charging, always use the same icon regardless of percentage. + */ + percentage = 0; + + suffix_str = get_device_icon_suffix (percentage); + index_str = get_device_icon_index (percentage); + g_ptr_array_add (names, g_strdup_printf ("battery-%s-charging-symbolic", suffix_str)); + g_ptr_array_add (names, g_strdup_printf ("gpm-%s-%s-charging", kind_str, index_str)); + g_ptr_array_add (names, g_strdup_printf ("battery-%s-charging", suffix_str)); + break; + + case UP_DEVICE_STATE_DISCHARGING: + case UP_DEVICE_STATE_PENDING_DISCHARGE: + /* Don't show the caution/red icons unless we have <=30 min left. + + Themes use the caution color when the percentage is 0% or 20%, + so if we have >30 min left, use 30% as the icon's percentage floor */ + if (indicator_power_device_get_time (device) > (30*60)) + percentage = MAX(percentage, 30); + + suffix_str = get_device_icon_suffix (percentage); + index_str = get_device_icon_index (percentage); + g_ptr_array_add (names, g_strdup_printf ("battery-%s-symbolic", suffix_str)); + g_ptr_array_add (names, g_strdup_printf ("gpm-%s-%s", kind_str, index_str)); + g_ptr_array_add (names, g_strdup_printf ("battery-%s", suffix_str)); + break; + + default: + g_ptr_array_add (names, g_strdup("battery-missing-symbolic")); + g_ptr_array_add (names, g_strdup("gpm-battery-missing")); + g_ptr_array_add (names, g_strdup("battery-missing")); } - ret = g_strsplit (filename->str, ";", -1); - g_string_free (filename, TRUE); - return ret; + g_ptr_array_add (names, NULL); /* terminates the strv */ + return (GStrv) g_ptr_array_free (names, FALSE); } /** diff --git a/tests/test-device.cc b/tests/test-device.cc index cc0d9c5..2c88ea1 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -231,13 +231,13 @@ TEST_F(DeviceTest, IconNames) g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER, NULL); check_icon_names (device, "ac-adapter-symbolic;" - "ac-adapter;"); + "ac-adapter"); // monitor g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_MONITOR, NULL); check_icon_names (device, "gpm-monitor-symbolic;" - "gpm-monitor;"); + "gpm-monitor"); // empty battery g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -246,7 +246,7 @@ TEST_F(DeviceTest, IconNames) check_icon_names (device, "battery-empty-symbolic;" "gpm-battery-empty;" "gpm-battery-000;" - "battery-empty;"); + "battery-empty"); // charged battery g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -254,7 +254,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-full-charged-symbolic;battery-full-charging-symbolic;" "gpm-battery-full;gpm-battery-100;" - "battery-full-charged;battery-full-charging;"); + "battery-full-charged;battery-full-charging"); // charging battery, 95% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -263,7 +263,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // charging battery, 85% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -272,7 +272,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // charging battery, 50% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -281,7 +281,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // charging battery, 25% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -290,7 +290,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // charging battery, 5% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -299,7 +299,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // discharging battery, 95% @@ -309,7 +309,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-full-symbolic;" "gpm-battery-100;" - "battery-full;"); + "battery-full"); // discharging battery, 85% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -318,7 +318,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-full-symbolic;" "gpm-battery-080;" - "battery-full;"); + "battery-full"); // discharging battery, 50% -- 1 hour left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -328,7 +328,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-good-symbolic;" "gpm-battery-060;" - "battery-good;"); + "battery-good"); // discharging battery, 25% -- 1 hour left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -338,7 +338,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-good-symbolic;" "gpm-battery-040;" - "battery-good;"); + "battery-good"); // discharging battery, 25% -- 15 minutes left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -348,7 +348,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-low-symbolic;" "gpm-battery-020;" - "battery-low;"); + "battery-low"); // discharging battery, 5% -- 1 hour left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -358,7 +358,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-good-symbolic;" "gpm-battery-040;" - "battery-good;"); + "battery-good"); // discharging battery, 5% -- 15 minutes left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -368,14 +368,14 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-symbolic;" "gpm-battery-000;" - "battery-caution;"); + "battery-caution"); // state unknown g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, NULL); check_icon_names (device, "battery-missing-symbolic;" "gpm-battery-missing;" - "battery-missing;"); + "battery-missing"); // cleanup g_object_unref(o); -- cgit v1.2.3