aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2014-03-28 16:10:23 +0000
committerCI bot <ps-jenkins@lists.canonical.com>2014-03-28 16:10:23 +0000
commit71ef36253e67a4383222b473be3f63188a0452a8 (patch)
tree2c67a4df2c3afb8201490a9b155cc9cb81e210cf /src
parent39ab6b3e3687a944f2c41be46bdb5c6259fe838c (diff)
parent8fcb63c1e8fed2988f811c3352efa0077c950162 (diff)
downloadayatana-indicator-power-71ef36253e67a4383222b473be3f63188a0452a8.tar.gz
ayatana-indicator-power-71ef36253e67a4383222b473be3f63188a0452a8.tar.bz2
ayatana-indicator-power-71ef36253e67a4383222b473be3f63188a0452a8.zip
If there are two batteries detected, combine their percentages and their time-remainings as per the revised spec. Fixes: 880881, 1297466
Diffstat (limited to 'src')
-rw-r--r--src/device.c53
-rw-r--r--src/service.c130
2 files changed, 165 insertions, 18 deletions
diff --git a/src/device.c b/src/device.c
index ed3c399..e3b655a 100644
--- a/src/device.c
+++ b/src/device.c
@@ -595,10 +595,12 @@ 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" */
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"*/
str = g_strdup_printf (_("%0d:%02d left"), hours, minutes);
}
}
@@ -631,22 +633,38 @@ get_accessible_time_remaining (const IndicatorPowerDevice * device)
if (p->state == UP_DEVICE_STATE_CHARGING)
{
if (hours > 0)
- str = g_strdup_printf (_("%d %s %d %s to charge"),
- hours, g_dngettext (NULL, "hour", "hours", hours),
- minutes, g_dngettext (NULL, "minute", "minutes", minutes));
+ {
+ /* TRANSLATORS: "X (hour,hours) Y (minute,minutes) to charge" the battery.
+ Example: "1 hour 10 minutes 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));
+ }
else
- str = g_strdup_printf (_("%d %s to charge"),
- minutes, g_dngettext (NULL, "minute", "minutes", minutes));
+ {
+ /* TRANSLATORS: "Y (minute,minutes) to charge" the battery.
+ Example: "59 minutes to charge" */
+ str = g_strdup_printf (_("%d %s to charge"),
+ minutes, g_dngettext (NULL, "minute", "minutes", minutes));
+ }
}
else // discharging
{
if (hours > 0)
- str = g_strdup_printf (_("%d %s %d %s left"),
- hours, g_dngettext (NULL, "hour", "hours", hours),
- minutes, g_dngettext (NULL, "minute", "minutes", minutes));
- else
- str = g_strdup_printf (_("%d %s left"),
- minutes, g_dngettext (NULL, "minute", "minutes", minutes));
+ {
+ /* TRANSLATORS: "X (hour,hours) Y (minute,minutes) left" until the battery's empty.
+ Example: "1 hour 10 minutes left" */
+ str = g_strdup_printf (_("%d %s %d %s left"),
+ hours, g_dngettext (NULL, "hour", "hours", hours),
+ minutes, g_dngettext (NULL, "minute", "minutes", minutes));
+ }
+ else
+ {
+ /* TRANSLATORS: "Y (minute,minutes) left" until the battery's empty.
+ Example: "59 minutes left" */
+ str = g_strdup_printf (_("%d %s left"),
+ minutes, g_dngettext (NULL, "minute", "minutes", minutes));
+ }
}
}
else
@@ -700,6 +718,7 @@ get_menuitem_text (const IndicatorPowerDevice * device,
if (p->state == UP_DEVICE_STATE_FULLY_CHARGED)
{
+ /* TRANSLATORS: example: "battery (charged)" */
str = g_strdup_printf (_("%s (charged)"), kind_str);
}
else
@@ -715,9 +734,14 @@ get_menuitem_text (const IndicatorPowerDevice * device,
}
if (time_str && *time_str)
- str = g_strdup_printf (_("%s (%s)"), kind_str, time_str);
+ {
+ /* TRANSLATORS: example: "battery (time remaining)" */
+ str = g_strdup_printf (_("%s (%s)"), kind_str, time_str);
+ }
else
- str = g_strdup (kind_str);
+ {
+ str = g_strdup (kind_str);
+ }
g_free (time_str);
}
@@ -783,14 +807,17 @@ indicator_power_device_get_readable_title (const IndicatorPowerDevice * device,
if (want_time && want_percent)
{
+ /* TRANSLATORS: after the icon, a time-remaining string + battery %. Example: "(0:59, 33%)" */
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)" */
str = g_strdup_printf (_("(%s)"), time_str);
}
else if (want_percent)
{
+ /* TRANSLATORS: after the icon, a battery %. Example: "(33%)" */
str = g_strdup_printf (_("(%.0lf%%)"), p->percentage);
}
else
diff --git a/src/service.c b/src/service.c
index d3e143e..b01128e 100644
--- a/src/service.c
+++ b/src/service.c
@@ -1175,6 +1175,129 @@ indicator_power_service_set_device_provider (IndicatorPowerService * self,
}
}
+/* If a device has multiple batteries and uses only one of them at a time,
+ they should be presented as separate items inside the battery menu,
+ but everywhere else they should be aggregated (bug 880881).
+ Their percentages should be averaged. If any are discharging,
+ the aggregated time remaining should be the maximum of the times
+ for all those that are discharging, plus the sum of the times
+ for all those that are idle. Otherwise, the aggregated time remaining
+ should be the the maximum of the times for all those that are charging. */
+static IndicatorPowerDevice *
+create_totalled_battery_device (const GList * devices)
+{
+ const GList * l;
+ guint n_charged = 0;
+ guint n_charging = 0;
+ guint n_discharging = 0;
+ guint n_batteries = 0;
+ double sum_percent = 0;
+ time_t max_discharge_time = 0;
+ time_t max_charge_time = 0;
+ time_t sum_charged_time = 0;
+ IndicatorPowerDevice * device = NULL;
+
+ for (l=devices; l!=NULL; l=l->next)
+ {
+ const IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE(l->data);
+
+ if (indicator_power_device_get_kind(device) == UP_DEVICE_KIND_BATTERY)
+ {
+ const double percent = indicator_power_device_get_percentage (device);
+ const time_t t = indicator_power_device_get_time (device);
+ const UpDeviceState state = indicator_power_device_get_state (device);
+
+ ++n_batteries;
+
+ if (percent > 0.01)
+ sum_percent += percent;
+
+ if (state == UP_DEVICE_STATE_CHARGING)
+ {
+ ++n_charging;
+ max_charge_time = MAX(max_charge_time, t);
+ }
+ else if (state == UP_DEVICE_STATE_DISCHARGING)
+ {
+ ++n_discharging;
+ max_discharge_time = MAX(max_discharge_time, t);
+ }
+ else if (state == UP_DEVICE_STATE_FULLY_CHARGED)
+ {
+ ++n_charged;
+ sum_charged_time += t;
+ }
+ }
+ }
+
+ if (n_batteries > 1)
+ {
+ const double percent = sum_percent / n_batteries;
+ UpDeviceState state;
+ time_t time_left;
+
+ if (n_discharging > 0)
+ {
+ state = UP_DEVICE_STATE_DISCHARGING;
+ time_left = max_discharge_time + sum_charged_time;
+ }
+ else if (n_charging > 0)
+ {
+ state = UP_DEVICE_STATE_CHARGING;
+ time_left = max_charge_time;
+ }
+ else if (n_charged > 0)
+ {
+ state = UP_DEVICE_STATE_FULLY_CHARGED;
+ time_left = 0;
+ }
+ else
+ {
+ state = UP_DEVICE_STATE_UNKNOWN;
+ time_left = 0;
+ }
+
+ device = indicator_power_device_new (NULL,
+ UP_DEVICE_KIND_BATTERY,
+ percent,
+ state,
+ time_left);
+ }
+
+ return device;
+}
+
+/**
+ * If there are multiple UP_DEVICE_KIND_BATTERY devices in the list,
+ * they're merged into a new 'totalled' device representing the sum of them.
+ *
+ * Returns: (element-type IndicatorPowerDevice)(transfer full): a list of devices
+ */
+static GList*
+merge_batteries_together (GList * devices)
+{
+ GList * ret;
+ IndicatorPowerDevice * merged_device;
+
+ if ((merged_device = create_totalled_battery_device (devices)))
+ {
+ GList * l;
+
+ ret = g_list_append (NULL, merged_device);
+
+ for (l=devices; l!=NULL; l=l->next)
+ if (indicator_power_device_get_kind(INDICATOR_POWER_DEVICE(l->data)) != UP_DEVICE_KIND_BATTERY)
+ ret = g_list_append (ret, g_object_ref(l->data));
+ }
+ else /* not enough batteries to merge */
+ {
+ ret = g_list_copy (devices);
+ g_list_foreach (ret, (GFunc)g_object_ref, NULL);
+ }
+
+ return ret;
+}
+
IndicatorPowerDevice *
indicator_power_service_choose_primary_device (GList * devices)
{
@@ -1182,13 +1305,10 @@ indicator_power_service_choose_primary_device (GList * devices)
if (devices != NULL)
{
- GList * tmp;
-
- tmp = g_list_copy (devices);
+ GList * tmp = merge_batteries_together (devices);
tmp = g_list_sort (tmp, device_compare_func);
primary = g_object_ref (tmp->data);
-
- g_list_free (tmp);
+ g_list_free_full (tmp, (GDestroyNotify)g_object_unref);
}
return primary;