From c15ff1e02ea0a7ae1f68a66a31dc6ce7d8955575 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Aug 2013 23:51:20 -0500 Subject: add show-percentage feature, basing off Haw Loeung's patch. --- src/device.c | 173 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 116 insertions(+), 57 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 02c9e65..69336eb 100644 --- a/src/device.c +++ b/src/device.c @@ -451,8 +451,8 @@ indicator_power_device_get_gicon (const IndicatorPowerDevice * device) static void get_timestring (guint64 time_secs, - gchar **short_timestring, - gchar **detailed_timestring) + gchar **readable_timestring, + gchar **accessible_timestring) { gint hours; gint minutes; @@ -462,16 +462,16 @@ get_timestring (guint64 time_secs, if (minutes == 0) { - *short_timestring = g_strdup (_("Unknown time")); - *detailed_timestring = g_strdup (_("Unknown time")); + *readable_timestring = g_strdup (_("Unknown time")); + *accessible_timestring = g_strdup (_("Unknown time")); return; } if (minutes < 60) { - *short_timestring = g_strdup_printf ("0:%.2i", minutes); - *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute", + *readable_timestring = g_strdup_printf ("0:%.2i", minutes); + *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute", "%i minutes", minutes), minutes); return; @@ -480,11 +480,11 @@ get_timestring (guint64 time_secs, hours = minutes / 60; minutes = minutes % 60; - *short_timestring = g_strdup_printf ("%i:%.2i", hours, minutes); + *readable_timestring = g_strdup_printf ("%i:%.2i", hours, minutes); if (minutes == 0) { - *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, + *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i hour", "%i hours", hours), hours); @@ -493,7 +493,7 @@ get_timestring (guint64 time_secs, { /* TRANSLATOR: "%i %s %i %s" are "%i hours %i minutes" * Swap order with "%2$s %2$i %1$s %1$i if needed */ - *detailed_timestring = g_strdup_printf (_("%i %s %i %s"), + *accessible_timestring = g_strdup_printf (_("%i %s %i %s"), hours, g_dngettext (GETTEXT_PACKAGE, "hour", "hours", hours), minutes, g_dngettext (GETTEXT_PACKAGE, "minute", "minutes", minutes)); } @@ -561,86 +561,145 @@ device_kind_to_localised_string (UpDeviceKind kind) return text; } -void +static char * +join_strings (const char * name, const char * time, const char * percent) +{ + char * str; + const gboolean have_name = name && *name; + const gboolean have_time = time && *time; + const gboolean have_percent = percent && *percent; + + if (have_name && have_time && have_percent) + str = g_strdup_printf (_("%s (%s, %s)"), name, time, percent); + else if (have_name && have_time) + str = g_strdup_printf (_("%s (%s)"), name, time); + else if (have_name && have_percent) + str = g_strdup_printf (_("%s (%s)"), name, percent); + else if (have_name) + str = g_strdup (name); + else if (have_time && have_percent) + str = g_strdup_printf (_("(%s, %s)"), time, percent); + else if (have_time) + str = g_strdup_printf (_("(%s)"), time); + else if (have_percent) + str = g_strdup_printf (_("(%s)"), percent); + else + str = g_strdup (""); + + return str; +} + +static void indicator_power_device_get_time_details (const IndicatorPowerDevice * device, - gchar ** short_details, - gchar ** details, - gchar ** accessible_name) + gboolean show_time_in_header, + gboolean show_percentage_in_header, + gchar ** header, + gchar ** label, + gchar ** a11y) { if (!INDICATOR_IS_POWER_DEVICE(device)) { - *short_details = NULL; - *details = NULL; - *accessible_name = NULL; + if (a11y != NULL) *a11y = NULL; + if (label != NULL) *label = NULL; + if (header != NULL) *header = NULL; g_warning ("%s: %p is not an IndicatorPowerDevice", G_STRFUNC, device); return; } const time_t time = indicator_power_device_get_time (device); const UpDeviceState state = indicator_power_device_get_state (device); - const gdouble percentage = indicator_power_device_get_percentage (device); const UpDeviceKind kind = indicator_power_device_get_kind (device); const gchar * device_name = device_kind_to_localised_string (kind); + const gdouble percentage = indicator_power_device_get_percentage (device); + char pctstr[32] = { '\0' }; + g_snprintf (pctstr, sizeof(pctstr), "%.0lf%%", percentage); + + GString * terse_time = g_string_new (NULL); + GString * verbose_time = g_string_new (NULL); + GString * accessible_time = g_string_new (NULL); if (time > 0) { - gchar *short_timestring = NULL; - gchar *detailed_timestring = NULL; - - get_timestring (time, - &short_timestring, - &detailed_timestring); + char * readable_timestr = NULL; + char * accessible_timestr = NULL; + get_timestring (time, &readable_timestr, &accessible_timestr); if (state == UP_DEVICE_STATE_CHARGING) { - /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ - *accessible_name = g_strdup_printf (_("%s (%s to charge (%.0lf%%))"), device_name, detailed_timestring, percentage); - *details = g_strdup_printf (_("%s (%s to charge)"), device_name, short_timestring); - *short_details = g_strdup_printf ("(%s)", short_timestring); + g_string_assign (terse_time, readable_timestr); + g_string_printf (verbose_time, _("%s to charge"), readable_timestr); + g_string_printf (accessible_time, _("%s to charge"), accessible_timestr); } - else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time > (60*60*12))) + else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time < (60*60*12))) { - *accessible_name = g_strdup_printf (_("%s"), device_name); - *details = g_strdup_printf (_("%s"), device_name); - *short_details = g_strdup (short_timestring); - } - else - { - /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ - *accessible_name = g_strdup_printf (_("%s (%s left (%.0lf%%))"), device_name, detailed_timestring, percentage); - *details = g_strdup_printf (_("%s (%s left)"), device_name, short_timestring); - *short_details = g_strdup (short_timestring); + g_string_assign (terse_time, readable_timestr); + g_string_printf (verbose_time, _("%s left"), readable_timestr); + g_string_printf (accessible_time, _("%s left"), accessible_timestr); } - g_free (short_timestring); - g_free (detailed_timestring); + g_free (readable_timestr); + g_free (accessible_timestr); } else if (state == UP_DEVICE_STATE_FULLY_CHARGED) { - *details = g_strdup_printf (_("%s (charged)"), device_name); - *accessible_name = g_strdup (*details); - *short_details = g_strdup (""); + g_string_assign (verbose_time, _("charged")); + g_string_assign (accessible_time, _("charged")); } else if (percentage > 0) { - /* TRANSLATORS: %2 is a percentage value. Note: this string is only - * used when we don't have a time value */ - *details = g_strdup_printf (_("%s (%.0lf%%)"), device_name, percentage); - *accessible_name = g_strdup (*details); - *short_details = g_strdup_printf (_("(%.0lf%%)"), percentage); - } - else if (kind == UP_DEVICE_KIND_LINE_POWER) - { - *details = g_strdup (device_name); - *accessible_name = g_strdup (device_name); - *short_details = g_strdup (""); + g_string_assign (terse_time, _("estimating…")); + g_string_assign (verbose_time, _("estimating…")); + g_string_assign (accessible_time, _("estimating…")); } else { - *details = g_strdup_printf (_("%s (not present)"), device_name); - *accessible_name = g_strdup (*details); - *short_details = g_strdup (_("(not present)")); + *pctstr = '\0'; + + if (kind != UP_DEVICE_KIND_LINE_POWER) + { + g_string_assign (verbose_time, _("not present")); + g_string_assign (accessible_time, _("not present")); + } } + + if (header != NULL) + *header = join_strings (NULL, + show_time_in_header ? terse_time->str : "", + show_percentage_in_header ? pctstr : ""); + + if (label != NULL) + *label = join_strings (device_name, + verbose_time->str, + NULL); + + if (a11y != NULL) + *a11y = join_strings (device_name, + accessible_time->str, + pctstr); + + g_string_free (terse_time, TRUE); + g_string_free (verbose_time, TRUE); + g_string_free (accessible_time, TRUE); +} + +gchar * +indicator_power_device_get_label (const IndicatorPowerDevice * device) +{ + gchar * label = NULL; + indicator_power_device_get_time_details (device, FALSE, FALSE, + NULL, &label, NULL); + return label; +} + +void +indicator_power_device_get_header (const IndicatorPowerDevice * device, + gboolean show_time, + gboolean show_percentage, + gchar ** header, + gchar ** a11y) +{ + indicator_power_device_get_time_details (device, show_time, show_percentage, + header, NULL, a11y); } /*** -- cgit v1.2.3 From cfda45be70c9d7877d23f3bfdc70672be1fa71f2 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Aug 2013 23:58:00 -0500 Subject: copyediting: remove trailing spaces in source code --- src/device-provider.c | 2 +- src/device.c | 2 +- src/service.c | 2 +- tests/test-dbus-listener.cc | 22 +++++++++++----------- tests/test-device.cc | 22 +++++++++++----------- tests/test-service.cc | 6 +++--- 6 files changed, 28 insertions(+), 28 deletions(-) (limited to 'src/device.c') diff --git a/src/device-provider.c b/src/device-provider.c index 5ccf588..81a8eec 100644 --- a/src/device-provider.c +++ b/src/device-provider.c @@ -54,7 +54,7 @@ indicator_power_device_provider_default_init (IndicatorPowerDeviceProviderInterf * An easy way to free the list properly in one step is as follows: * * g_slist_free_full (list, (GDestroyNotify)g_object_unref); - * + * * Return value: (element-type IndicatorPowerDevice) * (transfer full): * list of devices diff --git a/src/device.c b/src/device.c index 69336eb..4777675 100644 --- a/src/device.c +++ b/src/device.c @@ -484,7 +484,7 @@ get_timestring (guint64 time_secs, if (minutes == 0) { - *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, + *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i hour", "%i hours", hours), hours); diff --git a/src/service.c b/src/service.c index 8269448..9484e65 100644 --- a/src/service.c +++ b/src/service.c @@ -937,7 +937,7 @@ my_get_property (GObject * o, { IndicatorPowerService * self = INDICATOR_POWER_SERVICE (o); priv_t * p = self->priv; - + switch (property_id) { case PROP_DEVICE_PROVIDER: diff --git a/tests/test-dbus-listener.cc b/tests/test-dbus-listener.cc index 7764498..4d44e97 100644 --- a/tests/test-dbus-listener.cc +++ b/tests/test-dbus-listener.cc @@ -4,16 +4,16 @@ Copyright 2012 Canonical Ltd. Authors: Charles Kerr -This program is free software: you can redistribute it and/or modify it -under the terms of the GNU General Public License version 3, as published +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published by the Free Software Foundation. -This program is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranties of -MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -You should have received a copy of the GNU General Public License along +You should have received a copy of the GNU General Public License along with this program. If not, see . */ @@ -40,7 +40,7 @@ class DbusListenerTest : public ::testing::Test int gsd_name_ownership_id; int gsd_power_registration_id; char * gsd_power_error_string; - + protected: static void @@ -234,7 +234,7 @@ TEST_F(DbusListenerTest, GSDHasPowerAndBattery) // cleanup g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls - g_object_unref (o); + g_object_unref (o); } TEST_F(DbusListenerTest, GSDHasNoDevices) @@ -261,7 +261,7 @@ TEST_F(DbusListenerTest, GSDHasNoDevices) // cleanup g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls - g_object_unref (o); + g_object_unref (o); } TEST_F(DbusListenerTest, GSDReturnsError) @@ -285,7 +285,7 @@ TEST_F(DbusListenerTest, GSDReturnsError) // cleanup g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls - g_object_unref (o); + g_object_unref (o); } /* This test emits a PropertiesChanged signal and confirms that @@ -347,5 +347,5 @@ TEST_F(DbusListenerTest, GSDPropChanged) ASSERT_EQ (g_slist_length(devices), 2); // cleanup - g_object_unref (o); + g_object_unref (o); } diff --git a/tests/test-device.cc b/tests/test-device.cc index e9a439d..130ef16 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -4,16 +4,16 @@ Copyright 2012 Canonical Ltd. Authors: Charles Kerr -This program is free software: you can redistribute it and/or modify it -under the terms of the GNU General Public License version 3, as published +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published by the Free Software Foundation. -This program is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranties of -MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -You should have received a copy of the GNU General Public License along +You should have received a copy of the GNU General Public License along with this program. If not, see . */ @@ -291,7 +291,7 @@ TEST_F(DeviceTest, IconNames) g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY, NULL); - + g_string_append_printf (expected, "%s-empty-symbolic;", kind_str); g_string_append_printf (expected, "gpm-%s-empty;", kind_str); g_string_append_printf (expected, "gpm-%s-000;", kind_str); @@ -459,7 +459,7 @@ TEST_F(DeviceTest, IconNames) // state unknown g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, NULL); g_string_append_printf (expected, "%s-missing-symbolic;", kind_str); g_string_append_printf (expected, "gpm-%s-missing;", kind_str); @@ -495,7 +495,7 @@ TEST_F(DeviceTest, Labels) /** *** **/ - + IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); o = G_OBJECT(device); @@ -531,7 +531,7 @@ TEST_F(DeviceTest, Labels) INDICATOR_POWER_DEVICE_TIME, guint64(60*60*13), NULL); check_label (device, "Battery"); - check_header (device, "(50%)", + check_header (device, "(50%)", "", "(50%)", "Battery (50%)"); @@ -651,7 +651,7 @@ TEST_F(DeviceTest, ChoosePrimary) ASSERT_EQ (a, indicator_power_service_choose_primary_device(device_list)); } } - + // cleanup g_list_free_full (device_list, g_object_unref); } diff --git a/tests/test-service.cc b/tests/test-service.cc index cae3021..b2d4dc4 100644 --- a/tests/test-service.cc +++ b/tests/test-service.cc @@ -65,7 +65,7 @@ class IndicatorTest : public ::testing::Test virtual void SetUp() { ensure_glib_initialized (); - + g_setenv( "GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE); ac_device = indicator_power_device_new ( @@ -90,7 +90,7 @@ class IndicatorTest : public ::testing::Test const char* GetAccessibleDesc (IndicatorPower * power) const { GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - g_assert (g_list_length(entries) == 1); + g_assert (g_list_length(entries) == 1); IndicatorObjectEntry * entry = static_cast(entries->data); const char * ret = entry->accessible_desc; g_list_free (entries); @@ -120,7 +120,7 @@ TEST_F(IndicatorTest, SetDevices) devices = g_slist_append (devices, ac_device); devices = g_slist_append (devices, battery_device); indicator_power_set_devices (power, devices); - g_slist_free (devices); + g_slist_free (devices); g_object_unref (power); } -- cgit v1.2.3 From b84432b9493a1b0680a93c9199d6cbbd938e95b4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 23 Aug 2013 08:57:22 -0500 Subject: copyediting: more descriptive comments when building label/header/a11y text --- src/device.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 4777675..26b4c04 100644 --- a/src/device.c +++ b/src/device.c @@ -449,6 +449,7 @@ indicator_power_device_get_gicon (const IndicatorPowerDevice * device) **** ***/ +/* Format time remaining for reading ("H:MM") and speech ("H hours, MM minutes") */ static void get_timestring (guint64 time_secs, gchar **readable_timestring, @@ -590,12 +591,12 @@ join_strings (const char * name, const char * time, const char * percent) } static void -indicator_power_device_get_time_details (const IndicatorPowerDevice * device, - gboolean show_time_in_header, - gboolean show_percentage_in_header, - gchar ** header, - gchar ** label, - gchar ** a11y) +indicator_power_device_get_text (const IndicatorPowerDevice * device, + gboolean show_time_in_header, + gboolean show_percentage_in_header, + gchar ** header, + gchar ** label, + gchar ** a11y) { if (!INDICATOR_IS_POWER_DEVICE(device)) { @@ -630,12 +631,16 @@ indicator_power_device_get_time_details (const IndicatorPowerDevice * device, g_string_printf (verbose_time, _("%s to charge"), readable_timestr); g_string_printf (accessible_time, _("%s to charge"), accessible_timestr); } - else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time < (60*60*12))) + else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time <= (60*60*12))) { g_string_assign (terse_time, readable_timestr); g_string_printf (verbose_time, _("%s left"), readable_timestr); g_string_printf (accessible_time, _("%s left"), accessible_timestr); } + else + { + /* if there's more than 12 hours remaining, we don't show it */ + } g_free (readable_timestr); g_free (accessible_timestr); @@ -686,8 +691,8 @@ gchar * indicator_power_device_get_label (const IndicatorPowerDevice * device) { gchar * label = NULL; - indicator_power_device_get_time_details (device, FALSE, FALSE, - NULL, &label, NULL); + indicator_power_device_get_text (device, FALSE, FALSE, + NULL, &label, NULL); return label; } @@ -698,8 +703,8 @@ indicator_power_device_get_header (const IndicatorPowerDevice * device, gchar ** header, gchar ** a11y) { - indicator_power_device_get_time_details (device, show_time, show_percentage, - header, NULL, a11y); + indicator_power_device_get_text (device, show_time, show_percentage, + header, NULL, a11y); } /*** -- cgit v1.2.3