diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2012-05-31 14:00:12 -0500 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2012-05-31 14:00:12 -0500 |
commit | 8476e802d84ee91e50dd6556d8293ece77033201 (patch) | |
tree | 4b7a0ae968a467501c41574432dbd48731075835 | |
parent | 97867c31fd3f4767fe891de31786b7b32d6dd35a (diff) | |
download | ayatana-indicator-power-8476e802d84ee91e50dd6556d8293ece77033201.tar.gz ayatana-indicator-power-8476e802d84ee91e50dd6556d8293ece77033201.tar.bz2 ayatana-indicator-power-8476e802d84ee91e50dd6556d8293ece77033201.zip |
Move private indicator-power function build_device_time_details() to device.c to public function indicator_power_device_get_time_details() so that we can unit test the user-visible strings.
-rw-r--r-- | po/POTFILES.in | 2 | ||||
-rw-r--r-- | src/device.c | 195 | ||||
-rw-r--r-- | src/device.h | 5 | ||||
-rw-r--r-- | src/indicator-power.c | 201 |
4 files changed, 205 insertions, 198 deletions
diff --git a/po/POTFILES.in b/po/POTFILES.in index 7c8c8a1..3061414 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,2 +1,4 @@ data/com.canonical.indicator.power.gschema.xml.in +src/device.c +src/dbus-listener.c src/indicator-power.c diff --git a/src/device.c b/src/device.c index cd35c9b..dc8db3a 100644 --- a/src/device.c +++ b/src/device.c @@ -21,6 +21,12 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. */ +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +#include <glib/gi18n.h> + #include "device.h" struct _IndicatorPowerDevicePrivate @@ -391,6 +397,195 @@ indicator_power_device_get_gicon (const IndicatorPowerDevice * device) return icon; } +/*** +**** +***/ + +static void +get_timestring (guint64 time_secs, + gchar **short_timestring, + gchar **detailed_timestring) +{ + gint hours; + gint minutes; + + /* Add 0.5 to do rounding */ + minutes = (int) ( ( time_secs / 60.0 ) + 0.5 ); + + if (minutes == 0) + { + *short_timestring = g_strdup (_("Unknown time")); + *detailed_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", + "%i minutes", + minutes), minutes); + return; + } + + hours = minutes / 60; + minutes = minutes % 60; + + *short_timestring = g_strdup_printf ("%i:%.2i", hours, minutes); + + if (minutes == 0) + { + *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, + "%i hour", + "%i hours", + hours), hours); + } + else + { + /* 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"), + hours, g_dngettext (GETTEXT_PACKAGE, "hour", "hours", hours), + minutes, g_dngettext (GETTEXT_PACKAGE, "minute", "minutes", minutes)); + } +} + +static const gchar * +device_kind_to_localised_string (UpDeviceKind kind) +{ + const gchar *text = NULL; + + switch (kind) { + case UP_DEVICE_KIND_LINE_POWER: + /* TRANSLATORS: system power cord */ + text = _("AC adapter"); + break; + case UP_DEVICE_KIND_BATTERY: + /* TRANSLATORS: laptop primary battery */ + text = _("Battery"); + break; + case UP_DEVICE_KIND_UPS: + /* TRANSLATORS: battery-backed AC power source */ + text = _("UPS"); + break; + case UP_DEVICE_KIND_MONITOR: + /* TRANSLATORS: a monitor is a device to measure voltage and current */ + text = _("Monitor"); + break; + case UP_DEVICE_KIND_MOUSE: + /* TRANSLATORS: wireless mice with internal batteries */ + text = _("Mouse"); + break; + case UP_DEVICE_KIND_KEYBOARD: + /* TRANSLATORS: wireless keyboard with internal battery */ + text = _("Keyboard"); + break; + case UP_DEVICE_KIND_PDA: + /* TRANSLATORS: portable device */ + text = _("PDA"); + break; + case UP_DEVICE_KIND_PHONE: + /* TRANSLATORS: cell phone (mobile...) */ + text = _("Cell phone"); + break; + case UP_DEVICE_KIND_MEDIA_PLAYER: + /* TRANSLATORS: media player, mp3 etc */ + text = _("Media player"); + break; + case UP_DEVICE_KIND_TABLET: + /* TRANSLATORS: tablet device */ + text = _("Tablet"); + break; + case UP_DEVICE_KIND_COMPUTER: + /* TRANSLATORS: tablet device */ + text = _("Computer"); + break; + default: + g_warning ("enum unrecognised: %i", kind); + text = up_device_kind_to_string (kind); + } + + return text; +} + +void +indicator_power_device_get_time_details (const IndicatorPowerDevice * device, + gchar ** short_details, + gchar ** details, + gchar ** accessible_name) +{ + 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 gchar * device_name = device_kind_to_localised_string (indicator_power_device_get_kind(device)); + + gchar *short_timestring = NULL; + gchar *detailed_timestring = NULL; + + if (time > 0) + { + get_timestring (time, + &short_timestring, + &detailed_timestring); + + 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); + } + else if (state == UP_DEVICE_STATE_DISCHARGING) + { + *short_details = g_strdup_printf ("%s", short_timestring); + + if (time > 43200) /* 12 hours */ + { + *accessible_name = g_strdup_printf (_("%s"), device_name); + *details = g_strdup_printf (_("%s"), device_name); + } + 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); + } + } + + g_free (short_timestring); + g_free (detailed_timestring); + } + 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 (""); + } + 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 + { + *details = g_strdup_printf (_("%s (not present)"), device_name); + *accessible_name = g_strdup (*details); + *short_details = g_strdup (_("(not present)")); + } + } +} /*** **** Instantiation diff --git a/src/device.h b/src/device.h index 11cd83b..566b196 100644 --- a/src/device.h +++ b/src/device.h @@ -94,6 +94,11 @@ time_t indicator_power_device_get_time (const IndicatorPowerDevice GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device); GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device); +void indicator_power_device_get_time_details (const IndicatorPowerDevice * device, + gchar ** short_details, + gchar ** details, + gchar ** accessible_name); + diff --git a/src/indicator-power.c b/src/indicator-power.c index 96c0f1d..d5735ee 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -202,190 +202,6 @@ show_preferences_cb (GtkMenuItem *item, spawn_command_line_async ("gnome-control-center power"); } -static void -get_timestring (guint64 time_secs, - gchar **short_timestring, - gchar **detailed_timestring) -{ - gint hours; - gint minutes; - - /* Add 0.5 to do rounding */ - minutes = (int) ( ( time_secs / 60.0 ) + 0.5 ); - - if (minutes == 0) - { - *short_timestring = g_strdup (_("Unknown time")); - *detailed_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", - "%i minutes", - minutes), minutes); - return; - } - - hours = minutes / 60; - minutes = minutes % 60; - - *short_timestring = g_strdup_printf ("%i:%.2i", hours, minutes); - - if (minutes == 0) - { - *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, - "%i hour", - "%i hours", - hours), hours); - } - else - { - /* 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"), - hours, g_dngettext (GETTEXT_PACKAGE, "hour", "hours", hours), - minutes, g_dngettext (GETTEXT_PACKAGE, "minute", "minutes", minutes)); - } -} - -static const gchar * -device_kind_to_localised_string (UpDeviceKind kind) -{ - const gchar *text = NULL; - - switch (kind) { - case UP_DEVICE_KIND_LINE_POWER: - /* TRANSLATORS: system power cord */ - text = _("AC adapter"); - break; - case UP_DEVICE_KIND_BATTERY: - /* TRANSLATORS: laptop primary battery */ - text = _("Battery"); - break; - case UP_DEVICE_KIND_UPS: - /* TRANSLATORS: battery-backed AC power source */ - text = _("UPS"); - break; - case UP_DEVICE_KIND_MONITOR: - /* TRANSLATORS: a monitor is a device to measure voltage and current */ - text = _("Monitor"); - break; - case UP_DEVICE_KIND_MOUSE: - /* TRANSLATORS: wireless mice with internal batteries */ - text = _("Mouse"); - break; - case UP_DEVICE_KIND_KEYBOARD: - /* TRANSLATORS: wireless keyboard with internal battery */ - text = _("Keyboard"); - break; - case UP_DEVICE_KIND_PDA: - /* TRANSLATORS: portable device */ - text = _("PDA"); - break; - case UP_DEVICE_KIND_PHONE: - /* TRANSLATORS: cell phone (mobile...) */ - text = _("Cell phone"); - break; - case UP_DEVICE_KIND_MEDIA_PLAYER: - /* TRANSLATORS: media player, mp3 etc */ - text = _("Media player"); - break; - case UP_DEVICE_KIND_TABLET: - /* TRANSLATORS: tablet device */ - text = _("Tablet"); - break; - case UP_DEVICE_KIND_COMPUTER: - /* TRANSLATORS: tablet device */ - text = _("Computer"); - break; - default: - g_warning ("enum unrecognised: %i", kind); - text = up_device_kind_to_string (kind); - } - - return text; -} - -static void -build_device_time_details (const gchar *device_name, - guint64 time, - UpDeviceState state, - gdouble percentage, - gchar **short_details, - gchar **details, - gchar **accessible_name) -{ - gchar *short_timestring = NULL; - gchar *detailed_timestring = NULL; - - if (time > 0) - { - get_timestring (time, - &short_timestring, - &detailed_timestring); - - 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); - } - else if (state == UP_DEVICE_STATE_DISCHARGING) - { - *short_details = g_strdup_printf ("%s", short_timestring); - - if (time > 43200) /* 12 hours */ - { - *accessible_name = g_strdup_printf (_("%s"), device_name); - *details = g_strdup_printf (_("%s"), device_name); - } - 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); - } - } - - g_free (short_timestring); - g_free (detailed_timestring); - } - 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 (""); - } - 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 - { - *details = g_strdup_printf (_("%s (not present)"), device_name); - *accessible_name = g_strdup (*details); - *short_details = g_strdup (_("(not present)")); - } - } -} - /* ensure that the entry is using self's accessible description */ static void refresh_entry_accessible_desc (IndicatorPower * self, IndicatorObjectEntry * entry) @@ -444,23 +260,17 @@ menu_add_device (GtkMenu * menu, const IndicatorPowerDevice * device) GtkWidget *details_label; GtkWidget *grid; GIcon *device_gicon; - const gchar *device_name; gchar *short_details = NULL; gchar *details = NULL; gchar *accessible_name = NULL; AtkObject *atk_object; - 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); /* Process the data */ device_gicon = indicator_power_device_get_gicon (device); icon = gtk_image_new_from_gicon (device_gicon, GTK_ICON_SIZE_SMALL_TOOLBAR); g_clear_object (&device_gicon); - device_name = device_kind_to_localised_string (kind); - - build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accessible_name); + indicator_power_device_get_time_details (device, &short_details, &details, &accessible_name); /* Create menu item */ item = gtk_image_menu_item_new (); @@ -634,12 +444,7 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) gchar *short_details = NULL; gchar *details = NULL; gchar *accessible_name = NULL; - const gchar *device_name; IndicatorPowerPrivate * priv = self->priv; - const time_t time = indicator_power_device_get_time (device); - const UpDeviceKind kind = indicator_power_device_get_kind (device); - const UpDeviceState state = indicator_power_device_get_state (device); - const gdouble percentage = indicator_power_device_get_percentage (device); /* set icon */ device_gicon = indicator_power_device_get_gicon (device); @@ -649,10 +454,10 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) /* get the device name */ - device_name = device_kind_to_localised_string (kind); + //device_name = device_kind_to_localised_string (kind); /* get the description */ - build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accessible_name); + indicator_power_device_get_time_details (device, &short_details, &details, &accessible_name); gtk_label_set_label (GTK_LABEL (priv->label), short_details); |