diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2014-03-25 14:48:57 -0500 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2014-03-25 14:48:57 -0500 |
commit | 8fcb63c1e8fed2988f811c3352efa0077c950162 (patch) | |
tree | b821822f4100b53b803261722aa4aa4716e2ba3b /src | |
parent | 9b33101ad8a9fc093e226c25e497fd8b1d51aee5 (diff) | |
parent | 6b9e3d4fb5e11d5d09bc376aa2a56db6a02b55ee (diff) | |
download | ayatana-indicator-power-8fcb63c1e8fed2988f811c3352efa0077c950162.tar.gz ayatana-indicator-power-8fcb63c1e8fed2988f811c3352efa0077c950162.tar.bz2 ayatana-indicator-power-8fcb63c1e8fed2988f811c3352efa0077c950162.zip |
sync to lp:indicator-power
Diffstat (limited to 'src')
-rw-r--r-- | src/device.c | 169 | ||||
-rw-r--r-- | src/device.h | 16 | ||||
-rw-r--r-- | src/service.c | 64 |
3 files changed, 109 insertions, 140 deletions
diff --git a/src/device.c b/src/device.c index 8b97ef5..e3b655a 100644 --- a/src/device.c +++ b/src/device.c @@ -543,20 +543,19 @@ device_kind_to_localised_string (UpDeviceKind kind) * between 30 seconds and one minute; otherwise * * the empty string. */ -static void -get_brief_time_remaining (const IndicatorPowerDevice * device, - char * str, - gulong size) +static char * +get_brief_time_remaining (const IndicatorPowerDevice * device) { + gchar * str = NULL; const IndicatorPowerDevicePrivate * p = device->priv; if (p->time > 0) { int minutes = p->time / 60; - int hours = minutes / 60; + const int hours = minutes / 60; minutes %= 60; - g_snprintf (str, size, "%0d:%02d", hours, minutes); + str = g_strdup_printf("%0d:%02d", hours, minutes); } else if (p->inestimable != NULL) { @@ -564,21 +563,15 @@ get_brief_time_remaining (const IndicatorPowerDevice * device, if (elapsed < 30) { - g_snprintf (str, size, _("estimating…")); + str = g_strdup_printf (_("estimating…")); } else if (elapsed < 60) { - g_snprintf (str, size, _("unknown")); - } - else - { - *str = '\0'; + str = g_strdup_printf (_("unknown")); } } - else - { - *str = '\0'; - } + + return str; } /** @@ -588,19 +581,11 @@ get_brief_time_remaining (const IndicatorPowerDevice * device, * * if the component is charging, it should be “H:MM to charge” * * if the component is discharging, it should be “H:MM left”. */ -static void -get_expanded_time_remaining (const IndicatorPowerDevice * device, - char * str, - gulong size) +static char* +get_expanded_time_remaining (const IndicatorPowerDevice * device) { - const IndicatorPowerDevicePrivate * p; - - g_return_if_fail (str != NULL); - g_return_if_fail (size > 0); - *str = '\0'; - g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device)); - - p = device->priv; + char * str = NULL; + const IndicatorPowerDevicePrivate * p = device->priv; if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING))) { @@ -611,39 +596,33 @@ get_expanded_time_remaining (const IndicatorPowerDevice * device, if (p->state == UP_DEVICE_STATE_CHARGING) { /* TRANSLATORS: H:MM (hours, minutes) to charge the battery. Example: "1:30 to charge" */ - g_snprintf (str, size, _("%0d:%02d to charge"), hours, minutes); + str = g_strdup_printf (_("%0d:%02d to charge"), hours, minutes); } else // discharging { /* TRANSLATORS: H:MM (hours, minutes) to discharge the battery. Example: "1:30 left"*/ - g_snprintf (str, size, _("%0d:%02d left"), hours, minutes); + str = g_strdup_printf (_("%0d:%02d left"), hours, minutes); } } else { - get_brief_time_remaining (device, str, size); + str = get_brief_time_remaining (device); } + + return str; } /** * The '''accessible time-remaining string''' for a component * should be the same as the expanded time-remaining string, * except the H:MM time should be rendered as “''H'' hours ''M'' minutes”, - * or just as “''M'' * minutes” if the time is less than one hour. + * or just as “''M'' minutes” if the time is less than one hour. */ -static void -get_accessible_time_remaining (const IndicatorPowerDevice * device, - char * str, - gulong size) +static char * +get_accessible_time_remaining (const IndicatorPowerDevice * device) { - const IndicatorPowerDevicePrivate * p; - - g_return_if_fail (str != NULL); - g_return_if_fail (size > 0); - *str = '\0'; - g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device)); - - p = device->priv; + char * str = NULL; + const IndicatorPowerDevicePrivate * p = device->priv; if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING))) { @@ -657,7 +636,7 @@ get_accessible_time_remaining (const IndicatorPowerDevice * device, { /* TRANSLATORS: "X (hour,hours) Y (minute,minutes) to charge" the battery. Example: "1 hour 10 minutes to charge" */ - g_snprintf (str, size, _("%d %s %d %s to charge"), + str = g_strdup_printf (_("%d %s %d %s to charge"), hours, g_dngettext (NULL, "hour", "hours", hours), minutes, g_dngettext (NULL, "minute", "minutes", minutes)); } @@ -665,7 +644,7 @@ get_accessible_time_remaining (const IndicatorPowerDevice * device, { /* TRANSLATORS: "Y (minute,minutes) to charge" the battery. Example: "59 minutes to charge" */ - g_snprintf (str, size, _("%d %s to charge"), + str = g_strdup_printf (_("%d %s to charge"), minutes, g_dngettext (NULL, "minute", "minutes", minutes)); } } @@ -675,7 +654,7 @@ get_accessible_time_remaining (const IndicatorPowerDevice * device, { /* TRANSLATORS: "X (hour,hours) Y (minute,minutes) left" until the battery's empty. Example: "1 hour 10 minutes left" */ - g_snprintf (str, size, _("%d %s %d %s left"), + str = g_strdup_printf (_("%d %s %d %s left"), hours, g_dngettext (NULL, "hour", "hours", hours), minutes, g_dngettext (NULL, "minute", "minutes", minutes)); } @@ -683,15 +662,17 @@ get_accessible_time_remaining (const IndicatorPowerDevice * device, { /* TRANSLATORS: "Y (minute,minutes) left" until the battery's empty. Example: "59 minutes left" */ - g_snprintf (str, size, _("%d %s left"), + str = g_strdup_printf (_("%d %s left"), minutes, g_dngettext (NULL, "minute", "minutes", minutes)); } } } else { - get_brief_time_remaining (device, str, size); + str = get_brief_time_remaining (device); } + + return str; } /** @@ -727,72 +708,61 @@ time_is_relevant (const IndicatorPowerDevice * device) * visible label, except with the accessible time-remaining string * instead of the expanded time-remaining string. */ -static void +static char * get_menuitem_text (const IndicatorPowerDevice * device, - gchar * str, - gulong size, gboolean accessible) { + char * str = NULL; const IndicatorPowerDevicePrivate * p = device->priv; const char * kind_str = device_kind_to_localised_string (p->kind); if (p->state == UP_DEVICE_STATE_FULLY_CHARGED) { /* TRANSLATORS: example: "battery (charged)" */ - g_snprintf (str, size, _("%s (charged)"), kind_str); + str = g_strdup_printf (_("%s (charged)"), kind_str); } else { - char buf[64]; + char * time_str = NULL; if (time_is_relevant (device)) { if (accessible) - get_accessible_time_remaining (device, buf, sizeof(buf)); + time_str = get_accessible_time_remaining (device); else - get_expanded_time_remaining (device, buf, sizeof(buf)); - } - else - { - *buf = '\0'; + time_str = get_expanded_time_remaining (device); } - if (*buf) + if (time_str && *time_str) { /* TRANSLATORS: example: "battery (time remaining)" */ - g_snprintf (str, size, _("%s (%s)"), kind_str, buf); + str = g_strdup_printf (_("%s (%s)"), kind_str, time_str); } else { - g_strlcpy (str, kind_str, size); + str = g_strdup (kind_str); } + + g_free (time_str); } + + return str; } -void -indicator_power_device_get_readable_text (const IndicatorPowerDevice * device, - gchar * str, - gulong size) +char * +indicator_power_device_get_readable_text (const IndicatorPowerDevice * device) { - g_return_if_fail (str != NULL); - g_return_if_fail (size > 0); - *str = '\0'; - g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device)); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); - get_menuitem_text (device, str, size, FALSE); + return get_menuitem_text (device, FALSE); } -void -indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device, - gchar * str, - gulong size) +char * +indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device) { - g_return_if_fail (str != NULL); - g_return_if_fail (size > 0); - *str = '\0'; - g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device)); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); - get_menuitem_text (device, str, size, TRUE); + return get_menuitem_text (device, TRUE); } /** @@ -807,20 +777,16 @@ indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device, * * If both conditions are true, the time and percentage should be separated by a space. */ -void +char* indicator_power_device_get_readable_title (const IndicatorPowerDevice * device, - gchar * str, - gulong size, gboolean want_time, gboolean want_percent) { - char tr[64]; + char * str = NULL; + char * time_str = NULL; const IndicatorPowerDevicePrivate * p; - g_return_if_fail (str != NULL); - g_return_if_fail (size > 0); - *str = '\0'; - g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device)); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); p = device->priv; @@ -835,45 +801,46 @@ indicator_power_device_get_readable_title (const IndicatorPowerDevice * device, // try to build the time-remaining string if (want_time) { - get_brief_time_remaining (device, tr, sizeof(tr)); - - if (!*tr) - want_time = FALSE; + time_str = get_brief_time_remaining (device); + want_time = time_str && *time_str; } if (want_time && want_percent) { /* TRANSLATORS: after the icon, a time-remaining string + battery %. Example: "(0:59, 33%)" */ - g_snprintf (str, size, _("(%s, %.0lf%%)"), tr, p->percentage); + str = g_strdup_printf (_("(%s, %.0lf%%)"), time_str, p->percentage); } else if (want_time) { /* TRANSLATORS: after the icon, a time-remaining string Example: "(0:59)" */ - g_snprintf (str, size, _("(%s)"), tr); + str = g_strdup_printf (_("(%s)"), time_str); } else if (want_percent) { /* TRANSLATORS: after the icon, a battery %. Example: "(33%)" */ - g_snprintf (str, size, _("(%.0lf%%)"), p->percentage); + str = g_strdup_printf (_("(%.0lf%%)"), p->percentage); } else { - *str = '\0'; + str = NULL; } + + g_free (time_str); + return str; } /** * Regardless, the accessible name for the whole menu title should be the same * as the accessible name for that thing’s component inside the menu itself. */ -void +char * indicator_power_device_get_accessible_title (const IndicatorPowerDevice * device, - gchar * str, - gulong size, gboolean want_time G_GNUC_UNUSED, gboolean want_percent G_GNUC_UNUSED) { - indicator_power_device_get_accessible_text (device, str, size); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); + + return indicator_power_device_get_accessible_text (device); } /*** diff --git a/src/device.h b/src/device.h index 65c6767..3a10f89 100644 --- a/src/device.h +++ b/src/device.h @@ -126,23 +126,15 @@ GStrv indicator_power_device_get_icon_names (const IndicatorPower GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device); -void indicator_power_device_get_readable_text (const IndicatorPowerDevice * device, - gchar * str, - gulong size); +char * indicator_power_device_get_readable_text (const IndicatorPowerDevice * device); -void indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device, - gchar * str, - gulong size); +char * indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device); -void indicator_power_device_get_readable_title (const IndicatorPowerDevice * device, - gchar * str, - gulong size, +char * indicator_power_device_get_readable_title (const IndicatorPowerDevice * device, gboolean want_time, gboolean want_percent); -void indicator_power_device_get_accessible_title (const IndicatorPowerDevice * device, - gchar * str, - gulong size, +char * indicator_power_device_get_accessible_title (const IndicatorPowerDevice * device, gboolean want_time, gboolean want_percent); diff --git a/src/service.c b/src/service.c index e042e9e..36e705c 100644 --- a/src/service.c +++ b/src/service.c @@ -323,26 +323,32 @@ create_header_state (IndicatorPowerService * self) if (p->primary_device != NULL) { - char buf[128]; + char * title; GIcon * icon; const gboolean want_time = g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S); const gboolean want_percent = g_settings_get_boolean (p->settings, SETTINGS_SHOW_PERCENTAGE_S); - indicator_power_device_get_readable_title (p->primary_device, - buf, sizeof(buf), - want_time, - want_percent); - if (*buf) - g_variant_builder_add (&b, "{sv}", "label", g_variant_new_string (buf)); - - - indicator_power_device_get_accessible_title (p->primary_device, - buf, sizeof(buf), - want_time, - want_percent); - if (*buf) - g_variant_builder_add (&b, "{sv}", "accessible-desc", g_variant_new_string (buf)); + title = indicator_power_device_get_readable_title (p->primary_device, + want_time, + want_percent); + if (title) + { + if (*title) + g_variant_builder_add (&b, "{sv}", "label", g_variant_new_take_string (title)); + else + g_free (title); + } + title = indicator_power_device_get_accessible_title (p->primary_device, + want_time, + want_percent); + if (title) + { + if (*title) + g_variant_builder_add (&b, "{sv}", "accessible-desc", g_variant_new_take_string (title)); + else + g_free (title); + } if ((icon = indicator_power_device_get_gicon (p->primary_device))) { @@ -369,18 +375,19 @@ create_header_state (IndicatorPowerService * self) ***/ static void -append_device_to_menu (GMenu * menu, const IndicatorPowerDevice * device) +append_device_to_menu (GMenu * menu, const IndicatorPowerDevice * device, int profile) { const UpDeviceKind kind = indicator_power_device_get_kind (device); if (kind != UP_DEVICE_KIND_LINE_POWER) { - char buf[128]; + char * label; GMenuItem * item; GIcon * icon; - indicator_power_device_get_readable_text (device, buf, sizeof(buf)); - item = g_menu_item_new (buf, "indicator.activate-statistics"); + label = indicator_power_device_get_readable_text (device); + item = g_menu_item_new (label, NULL); + g_free (label); if ((icon = indicator_power_device_get_gicon (device))) { @@ -397,8 +404,11 @@ append_device_to_menu (GMenu * menu, const IndicatorPowerDevice * device) g_object_unref (icon); } - g_menu_item_set_action_and_target(item, "indicator.activate-statistics", "s", - indicator_power_device_get_object_path (device)); + if (profile == PROFILE_DESKTOP) + { + g_menu_item_set_action_and_target(item, "indicator.activate-statistics", "s", + indicator_power_device_get_object_path (device)); + } g_menu_append_item (menu, item); g_object_unref (item); @@ -407,13 +417,13 @@ append_device_to_menu (GMenu * menu, const IndicatorPowerDevice * device) static GMenuModel * -create_desktop_devices_section (IndicatorPowerService * self) +create_desktop_devices_section (IndicatorPowerService * self, int profile) { GList * l; GMenu * menu = g_menu_new (); for (l=self->priv->devices; l!=NULL; l=l->next) - append_device_to_menu (menu, l->data); + append_device_to_menu (menu, l->data, profile); return G_MENU_MODEL (menu); } @@ -588,8 +598,8 @@ rebuild_now (IndicatorPowerService * self, guint sections) if (sections & SECTION_DEVICES) { - rebuild_section (desktop->submenu, 0, create_desktop_devices_section (self)); - rebuild_section (greeter->submenu, 0, create_desktop_devices_section (self)); + rebuild_section (desktop->submenu, 0, create_desktop_devices_section (self, PROFILE_DESKTOP)); + rebuild_section (greeter->submenu, 0, create_desktop_devices_section (self, PROFILE_DESKTOP_GREETER)); } if (sections & SECTION_SETTINGS) @@ -640,12 +650,12 @@ create_menu (IndicatorPowerService * self, int profile) break; case PROFILE_DESKTOP: - sections[n++] = create_desktop_devices_section (self); + sections[n++] = create_desktop_devices_section (self, PROFILE_DESKTOP); sections[n++] = create_desktop_settings_section (self); break; case PROFILE_DESKTOP_GREETER: - sections[n++] = create_desktop_devices_section (self); + sections[n++] = create_desktop_devices_section (self, PROFILE_DESKTOP_GREETER); break; } |