aboutsummaryrefslogtreecommitdiff
path: root/src/device.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/device.c')
-rw-r--r--src/device.c492
1 files changed, 310 insertions, 182 deletions
diff --git a/src/device.c b/src/device.c
index 7f1b14f..eff76d1 100644
--- a/src/device.c
+++ b/src/device.c
@@ -37,9 +37,12 @@ struct _IndicatorPowerDevicePrivate
gchar * object_path;
gdouble percentage;
time_t time;
-};
-#define INDICATOR_POWER_DEVICE_GET_PRIVATE(o) (INDICATOR_POWER_DEVICE(o)->priv)
+ /* Timestamp of when when we first noticed that upower couldn't estimate
+ the time-remaining field for this device, or 0 if not applicable.
+ This is used when generating the time-remaining string. */
+ GTimer * inestimable;
+};
/* Properties */
/* Enum for the properties so that they can be quickly found and looked up. */
@@ -64,7 +67,7 @@ static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* );
static void get_property (GObject*, guint prop_id, GValue*, GParamSpec* );
/* LCOV_EXCL_START */
-G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT);
+G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT)
/* LCOV_EXCL_STOP */
static void
@@ -136,6 +139,11 @@ indicator_power_device_init (IndicatorPowerDevice *self)
static void
indicator_power_device_dispose (GObject *object)
{
+ IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(object);
+ IndicatorPowerDevicePrivate * p = self->priv;
+
+ g_clear_pointer (&p->inestimable, g_timer_destroy);
+
G_OBJECT_CLASS (indicator_power_device_parent_class)->dispose (object);
}
@@ -179,7 +187,7 @@ get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec)
break;
case PROP_TIME:
- g_value_set_uint64 (value, priv->time);
+ g_value_set_uint64 (value, (guint64)priv->time);
break;
default:
@@ -192,35 +200,54 @@ static void
set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec)
{
IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o);
- IndicatorPowerDevicePrivate * priv = self->priv;
+ IndicatorPowerDevicePrivate * p = self->priv;
switch (prop_id)
{
case PROP_KIND:
- priv->kind = g_value_get_int (value);
+ p->kind = (UpDeviceKind) g_value_get_int (value);
break;
case PROP_STATE:
- priv->state = g_value_get_int (value);
+ p->state = (UpDeviceState) g_value_get_int (value);
break;
case PROP_OBJECT_PATH:
- g_free (priv->object_path);
- priv->object_path = g_value_dup_string (value);
+ g_free (p->object_path);
+ p->object_path = g_value_dup_string (value);
break;
case PROP_PERCENTAGE:
- priv->percentage = g_value_get_double (value);
+ p->percentage = g_value_get_double (value);
break;
case PROP_TIME:
- priv->time = g_value_get_uint64(value);
+ p->time = (time_t) g_value_get_uint64(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, prop_id, pspec);
break;
}
+
+ /**
+ * Check to see if the time-remaining value is estimable.
+ * When it first becomes inestimable, kick off a timer because
+ * we need to track that to generate the appropriate title text.
+ */
+
+ const gboolean is_inestimable = (p->time == 0)
+ && (p->state != UP_DEVICE_STATE_FULLY_CHARGED)
+ && (p->percentage > 0);
+
+ if (!is_inestimable)
+ {
+ g_clear_pointer (&p->inestimable, g_timer_destroy);
+ }
+ else if (p->inestimable == NULL)
+ {
+ p->inestimable = g_timer_new ();
+ }
}
/***
@@ -326,13 +353,6 @@ device_kind_to_string (UpDeviceKind kind)
indicator_power_device_get_icon_names:
@device: #IndicatorPowerDevice from which to generate the icon names
- This function's logic differs from GSD's power plugin in some ways:
-
- 1. For discharging batteries, we decide whether or not to use the 'caution'
- icon based on whether or not we have <= 30 minutes remaining, rather than
- looking at the battery's percentage left.
- <https://bugs.launchpad.net/indicator-power/+bug/743823>
-
See also indicator_power_device_get_gicon().
Return value: (array zero-terminated=1) (transfer full):
@@ -387,21 +407,15 @@ indicator_power_device_get_icon_names (const IndicatorPowerDevice * device)
case UP_DEVICE_STATE_CHARGING:
suffix_str = get_device_icon_suffix (percentage);
index_str = get_device_icon_index (percentage);
- g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging-symbolic", kind_str, suffix_str));
+ g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging", kind_str, index_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 ("%s-%s-charging-symbolic", kind_str, suffix_str));
g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging", kind_str, suffix_str));
break;
case UP_DEVICE_STATE_PENDING_CHARGE:
case UP_DEVICE_STATE_DISCHARGING:
case UP_DEVICE_STATE_PENDING_DISCHARGE:
- /* Don't show the caution/red icons unless we have <=30 min left.
- <https://bugs.launchpad.net/indicator-power/+bug/743823>
- 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 ("%s-%s", kind_str, index_str));
@@ -442,57 +456,6 @@ 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,
- gchar **accessible_timestring)
-{
- gint hours;
- gint minutes;
-
- /* Add 0.5 to do rounding */
- minutes = (int) ( ( time_secs / 60.0 ) + 0.5 );
-
- if (minutes == 0)
- {
- *readable_timestring = g_strdup (_("Unknown time"));
- *accessible_timestring = g_strdup (_("Unknown time"));
-
- return;
- }
-
- if (minutes < 60)
- {
- *readable_timestring = g_strdup_printf ("0:%.2i", minutes);
- *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute",
- "%i minutes",
- minutes), minutes);
- return;
- }
-
- hours = minutes / 60;
- minutes = minutes % 60;
-
- *readable_timestring = g_strdup_printf ("%i:%.2i", hours, minutes);
-
- if (minutes == 0)
- {
- *accessible_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 */
- *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));
- }
-}
-
static const gchar *
device_kind_to_localised_string (UpDeviceKind kind)
{
@@ -555,149 +518,314 @@ device_kind_to_localised_string (UpDeviceKind kind)
return text;
}
+/**
+ * The '''brief time-remaining string''' for a component should be:
+ * * the time remaining for it to empty or fully charge,
+ * if estimable, in H:MM format; otherwise
+ * * “estimating…” if the time remaining has been inestimable for
+ * less than 30 seconds; otherwise
+ * * “unknown” if the time remaining has been inestimable for
+ * between 30 seconds and one minute; otherwise
+ * * the empty string.
+ */
static char *
-join_strings (const char * name, const char * time, const char * percent)
+get_brief_time_remaining (const IndicatorPowerDevice * device)
{
- 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 ("");
+ gchar * str = NULL;
+ const IndicatorPowerDevicePrivate * p = device->priv;
+
+ if (p->time > 0)
+ {
+ int minutes = p->time / 60;
+ const int hours = minutes / 60;
+ minutes %= 60;
+
+ str = g_strdup_printf("%0d:%02d", hours, minutes);
+ }
+ else if (p->inestimable != NULL)
+ {
+ const double elapsed = g_timer_elapsed (p->inestimable, NULL);
+
+ if (elapsed < 30)
+ {
+ str = g_strdup_printf (_("estimating…"));
+ }
+ else if (elapsed < 60)
+ {
+ str = g_strdup_printf (_("unknown"));
+ }
+ }
return str;
}
-static void
-indicator_power_device_get_text (const IndicatorPowerDevice * device,
- gboolean show_time_in_header,
- gboolean show_percentage_in_header,
- gchar ** header,
- gchar ** label,
- gchar ** a11y)
+/**
+ * The '''expanded time-remaining string''' for a component should
+ * be the same as the brief time-remaining string, except that if
+ * the time is estimable:
+ * * if the component is charging, it should be “H:MM to charge”
+ * * if the component is discharging, it should be “H:MM left”.
+ */
+static char*
+get_expanded_time_remaining (const IndicatorPowerDevice * device)
{
- if (!INDICATOR_IS_POWER_DEVICE(device))
+ char * str = NULL;
+ const IndicatorPowerDevicePrivate * p = device->priv;
+
+ if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING)))
+ {
+ int minutes = p->time / 60;
+ const int hours = minutes / 60;
+ minutes %= 60;
+
+ 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);
+ }
+ }
+ else
{
- 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;
+ str = get_brief_time_remaining (device);
}
- const time_t time = indicator_power_device_get_time (device);
- const UpDeviceState state = indicator_power_device_get_state (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);
+ return str;
+}
- GString * terse_time = g_string_new (NULL);
- GString * verbose_time = g_string_new (NULL);
- GString * accessible_time = g_string_new (NULL);
+/**
+ * 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.
+ */
+static char *
+get_accessible_time_remaining (const IndicatorPowerDevice * device)
+{
+ char * str = NULL;
+ const IndicatorPowerDevicePrivate * p = device->priv;
- if (time > 0)
+ if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING)))
{
- char * readable_timestr = NULL;
- char * accessible_timestr = NULL;
- get_timestring (time, &readable_timestr, &accessible_timestr);
+ guint minutes = (guint)p->time / 60u;
+ const guint hours = minutes / 60u;
+ minutes %= 60;
- if (state == UP_DEVICE_STATE_CHARGING)
- {
- 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*24)))
+ if (p->state == UP_DEVICE_STATE_CHARGING)
{
- 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);
+ if (hours > 0)
+ {
+ /* 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
+ {
+ /* 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
+ else // discharging
{
- /* if there's more than 24 hours remaining, we don't show it */
+ if (hours > 0)
+ {
+ /* 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));
+ }
}
-
- g_free (readable_timestr);
- g_free (accessible_timestr);
}
- else if (state == UP_DEVICE_STATE_FULLY_CHARGED)
+ else
{
- g_string_assign (verbose_time, _("charged"));
- g_string_assign (accessible_time, _("charged"));
+ str = get_brief_time_remaining (device);
}
- else if (percentage > 0)
+
+ return str;
+}
+
+/**
+ * The time is relevant for a device if either (a) the component is charging,
+ * or (b) the component is discharging and the estimated time is less than
+ * 24 hours. (A time greater than 24 hours is probably a mistaken calculation.)
+ */
+static gboolean
+time_is_relevant (const IndicatorPowerDevice * device)
+{
+ const IndicatorPowerDevicePrivate * p = device->priv;
+
+ if (p->state == UP_DEVICE_STATE_CHARGING)
+ return TRUE;
+
+ if ((p->state == UP_DEVICE_STATE_DISCHARGING) && (p->time<(24*60*60)))
+ return TRUE;
+
+ return FALSE;
+}
+
+/**
+ * The menu item for each chargeable component should consist of ...
+ * Text representing the name of the component (“Battery”, “Mouse”,
+ * “UPS”, “Alejandra’s iPod”, etc) and the charge status in brackets:
+ *
+ * * “X (charged)” if it is fully charged and not discharging;
+ * * “X (expanded time-remaining string)” if it is charging,
+ * or discharging with less than 24 hours left;
+ * * “X” if it is discharging with 24 hours or more left.
+ *
+ * The accessible label for the menu item should be the same as the
+ * visible label, except with the accessible time-remaining string
+ * instead of the expanded time-remaining string.
+ */
+static char *
+get_menuitem_text (const IndicatorPowerDevice * device,
+ 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)
{
- g_string_assign (terse_time, _("estimating…"));
- g_string_assign (verbose_time, _("estimating…"));
- g_string_assign (accessible_time, _("estimating…"));
+ /* TRANSLATORS: example: "battery (charged)" */
+ str = g_strdup_printf (_("%s (charged)"), kind_str);
}
else
{
- *pctstr = '\0';
+ char * time_str = NULL;
+
+ if (time_is_relevant (device))
+ {
+ if (accessible)
+ time_str = get_accessible_time_remaining (device);
+ else
+ time_str = get_expanded_time_remaining (device);
+ }
- if (kind != UP_DEVICE_KIND_LINE_POWER)
+ if (time_str && *time_str)
{
- g_string_assign (verbose_time, _("not present"));
- g_string_assign (accessible_time, _("not present"));
+ /* TRANSLATORS: example: "battery (time remaining)" */
+ str = g_strdup_printf (_("%s (%s)"), kind_str, time_str);
}
+ else
+ {
+ str = g_strdup (kind_str);
+ }
+
+ g_free (time_str);
}
- if (header != NULL)
- *header = join_strings (NULL,
- show_time_in_header ? terse_time->str : "",
- show_percentage_in_header ? pctstr : "");
+ return str;
+}
+
+char *
+indicator_power_device_get_readable_text (const IndicatorPowerDevice * device)
+{
+ g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
- if (label != NULL)
- *label = join_strings (device_name,
- verbose_time->str,
- NULL);
+ return get_menuitem_text (device, FALSE);
+}
- if (a11y != NULL)
- *a11y = join_strings (device_name,
- accessible_time->str,
- pctstr);
+char *
+indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device)
+{
+ g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
- g_string_free (terse_time, TRUE);
- g_string_free (verbose_time, TRUE);
- g_string_free (accessible_time, TRUE);
+ return get_menuitem_text (device, TRUE);
}
-gchar *
-indicator_power_device_get_label (const IndicatorPowerDevice * device)
+/**
+ * If the time is relevant and/or “Show Percentage in Menu Bar” is checked,
+ * the icon should be followed by brackets.
+ *
+ * If the time is relevant, the brackets should contain the time-remaining
+ * string for that component.
+ *
+ * If “Show Percentage in Menu Bar” is checked (as it should not be by default),
+ * the brackets should contain the percentage charge for that device.
+ *
+ * If both conditions are true, the time and percentage should be separated by a space.
+ */
+char*
+indicator_power_device_get_readable_title (const IndicatorPowerDevice * device,
+ gboolean want_time,
+ gboolean want_percent)
{
- gchar * label = NULL;
- indicator_power_device_get_text (device, FALSE, FALSE,
- NULL, &label, NULL);
- return label;
+ char * str = NULL;
+ char * time_str = NULL;
+ const IndicatorPowerDevicePrivate * p;
+
+ g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
+
+ p = device->priv;
+
+ // if we can't provide time-remaining, turn off the time flag
+ if (want_time && !time_is_relevant (device))
+ want_time = FALSE;
+
+ // if we can't provide percent, turn off the percent flag
+ if (p->percentage < 0.01)
+ want_percent = FALSE;
+
+ // try to build the time-remaining string
+ if (want_time)
+ {
+ 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%)" */
+ 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
+ {
+ str = NULL;
+ }
+
+ g_free (time_str);
+ return str;
}
-void
-indicator_power_device_get_header (const IndicatorPowerDevice * device,
- gboolean show_time,
- gboolean show_percentage,
- gchar ** header,
- gchar ** a11y)
+/**
+ * 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.
+ */
+char *
+indicator_power_device_get_accessible_title (const IndicatorPowerDevice * device,
+ gboolean want_time G_GNUC_UNUSED,
+ gboolean want_percent G_GNUC_UNUSED)
{
- indicator_power_device_get_text (device, show_time, show_percentage,
- header, NULL, a11y);
+ g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
+
+ return indicator_power_device_get_accessible_text (device);
}
/***
@@ -745,5 +873,5 @@ indicator_power_device_new_from_variant (GVariant * v)
kind,
percentage,
state,
- time);
+ (time_t)time);
}