559
join_strings (const char * name, const char * time, const char * percent)
562
const gboolean have_name = name && *name;
563
const gboolean have_time = time && *time;
564
const gboolean have_percent = percent && *percent;
566
if (have_name && have_time && have_percent)
567
str = g_strdup_printf (_("%s (%s, %s)"), name, time, percent);
568
else if (have_name && have_time)
569
str = g_strdup_printf (_("%s (%s)"), name, time);
570
else if (have_name && have_percent)
571
str = g_strdup_printf (_("%s (%s)"), name, percent);
573
str = g_strdup (name);
574
else if (have_time && have_percent)
575
str = g_strdup_printf (_("(%s, %s)"), time, percent);
577
str = g_strdup_printf (_("(%s)"), time);
578
else if (have_percent)
579
str = g_strdup_printf (_("(%s)"), percent);
587
indicator_power_device_get_text (const IndicatorPowerDevice * device,
588
gboolean show_time_in_header,
589
gboolean show_percentage_in_header,
594
if (!INDICATOR_IS_POWER_DEVICE(device))
596
if (a11y != NULL) *a11y = NULL;
597
if (label != NULL) *label = NULL;
598
if (header != NULL) *header = NULL;
599
g_warning ("%s: %p is not an IndicatorPowerDevice", G_STRFUNC, device);
603
const time_t time = indicator_power_device_get_time (device);
604
const UpDeviceState state = indicator_power_device_get_state (device);
605
const UpDeviceKind kind = indicator_power_device_get_kind (device);
606
const gchar * device_name = device_kind_to_localised_string (kind);
607
const gdouble percentage = indicator_power_device_get_percentage (device);
608
char pctstr[32] = { '\0' };
609
g_snprintf (pctstr, sizeof(pctstr), "%.0lf%%", percentage);
611
GString * terse_time = g_string_new (NULL);
612
GString * verbose_time = g_string_new (NULL);
613
GString * accessible_time = g_string_new (NULL);
617
char * readable_timestr = NULL;
618
char * accessible_timestr = NULL;
619
get_timestring (time, &readable_timestr, &accessible_timestr);
621
if (state == UP_DEVICE_STATE_CHARGING)
623
g_string_assign (terse_time, readable_timestr);
624
g_string_printf (verbose_time, _("%s to charge"), readable_timestr);
625
g_string_printf (accessible_time, _("%s to charge"), accessible_timestr);
627
else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time <= (60*60*24)))
629
g_string_assign (terse_time, readable_timestr);
630
g_string_printf (verbose_time, _("%s left"), readable_timestr);
631
g_string_printf (accessible_time, _("%s left"), accessible_timestr);
635
/* if there's more than 24 hours remaining, we don't show it */
638
g_free (readable_timestr);
639
g_free (accessible_timestr);
641
else if (state == UP_DEVICE_STATE_FULLY_CHARGED)
643
g_string_assign (verbose_time, _("charged"));
644
g_string_assign (accessible_time, _("charged"));
646
else if (percentage > 0)
648
g_string_assign (terse_time, _("estimating…"));
649
g_string_assign (verbose_time, _("estimating…"));
650
g_string_assign (accessible_time, _("estimating…"));
656
if (kind != UP_DEVICE_KIND_LINE_POWER)
658
g_string_assign (verbose_time, _("not present"));
659
g_string_assign (accessible_time, _("not present"));
664
*header = join_strings (NULL,
665
show_time_in_header ? terse_time->str : "",
666
show_percentage_in_header ? pctstr : "");
669
*label = join_strings (device_name,
674
*a11y = join_strings (device_name,
675
accessible_time->str,
678
g_string_free (terse_time, TRUE);
679
g_string_free (verbose_time, TRUE);
680
g_string_free (accessible_time, TRUE);
684
indicator_power_device_get_label (const IndicatorPowerDevice * device)
686
gchar * label = NULL;
687
indicator_power_device_get_text (device, FALSE, FALSE,
693
indicator_power_device_get_header (const IndicatorPowerDevice * device,
695
gboolean show_percentage,
699
indicator_power_device_get_text (device, show_time, show_percentage,
533
* The '''brief time-remaining string''' for a component should be:
534
* * the time remaining for it to empty or fully charge,
535
* if estimable, in H:MM format; otherwise
536
* * “estimating…” if the time remaining has been inestimable for
537
* less than 30 seconds; otherwise
538
* * “unknown” if the time remaining has been inestimable for
539
* between 30 seconds and one minute; otherwise
540
* * the empty string.
543
get_brief_time_remaining (const IndicatorPowerDevice * device,
547
const IndicatorPowerDevicePrivate * p = device->priv;
551
int minutes = p->time / 60;
552
int hours = minutes / 60;
555
g_snprintf (str, size, "%0d:%02d", hours, minutes);
557
else if (p->inestimable != NULL)
559
const double elapsed = g_timer_elapsed (p->inestimable, NULL);
563
g_snprintf (str, size, _("estimating…"));
565
else if (elapsed < 60)
567
g_snprintf (str, size, _("unknown"));
581
* The '''expanded time-remaining string''' for a component should
582
* be the same as the brief time-remaining string, except that if
583
* the time is estimable:
584
* * if the component is charging, it should be “H:MM to charge”
585
* * if the component is discharging, it should be “H:MM left”.
588
get_expanded_time_remaining (const IndicatorPowerDevice * device,
592
const IndicatorPowerDevicePrivate * p;
594
g_return_if_fail (str != NULL);
595
g_return_if_fail (size > 0);
597
g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
601
if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING)))
603
int minutes = p->time / 60;
604
int hours = minutes / 60;
607
if (p->state == UP_DEVICE_STATE_CHARGING)
609
g_snprintf (str, size, _("%0d:%02d to charge"), hours, minutes);
613
g_snprintf (str, size, _("%0d:%02d left"), hours, minutes);
618
get_brief_time_remaining (device, str, size);
623
* The '''accessible time-remaining string''' for a component
624
* should be the same as the expanded time-remaining string,
625
* except the H:MM time should be rendered as “''H'' hours ''M'' minutes”,
626
* or just as “''M'' * minutes” if the time is less than one hour.
629
get_accessible_time_remaining (const IndicatorPowerDevice * device,
633
const IndicatorPowerDevicePrivate * p;
635
g_return_if_fail (str != NULL);
636
g_return_if_fail (size > 0);
638
g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
642
if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING)))
644
int minutes = p->time / 60;
645
int hours = minutes / 60;
648
if (p->state == UP_DEVICE_STATE_CHARGING)
651
g_snprintf (str, size, _("%d %s %d %s to charge"),
652
hours, g_dngettext (NULL, "hour", "hours", hours),
653
minutes, g_dngettext (NULL, "minute", "minutes", minutes));
655
g_snprintf (str, size, _("%d %s to charge"),
656
minutes, g_dngettext (NULL, "minute", "minutes", minutes));
661
g_snprintf (str, size, _("%d %s %d %s left"),
662
hours, g_dngettext (NULL, "hour", "hours", hours),
663
minutes, g_dngettext (NULL, "minute", "minutes", minutes));
665
g_snprintf (str, size, _("%d %s left"),
666
minutes, g_dngettext (NULL, "minute", "minutes", minutes));
671
get_brief_time_remaining (device, str, size);
676
* The time is relevant for a device if either (a) the component is charging,
677
* or (b) the component is discharging and the estimated time is less than
678
* 24 hours. (A time greater than 24 hours is probably a mistaken calculation.)
681
time_is_relevant (const IndicatorPowerDevice * device)
683
const IndicatorPowerDevicePrivate * p = device->priv;
685
if (p->state == UP_DEVICE_STATE_CHARGING)
688
if ((p->state == UP_DEVICE_STATE_DISCHARGING) && (p->time<(24*60*60)))
695
* The menu item for each chargeable component should consist of ...
696
* Text representing the name of the component (“Battery”, “Mouse”,
697
* “UPS”, “Alejandra’s iPod”, etc) and the charge status in brackets:
699
* * “X (charged)” if it is fully charged and not discharging;
700
* * “X (expanded time-remaining string)” if it is charging,
701
* or discharging with less than 24 hours left;
702
* * “X” if it is discharging with 24 hours or more left.
704
* The accessible label for the menu item should be the same as the
705
* visible label, except with the accessible time-remaining string
706
* instead of the expanded time-remaining string.
709
get_menuitem_text (const IndicatorPowerDevice * device,
714
const IndicatorPowerDevicePrivate * p = device->priv;
715
const char * kind_str = device_kind_to_localised_string (p->kind);
717
if (p->state == UP_DEVICE_STATE_FULLY_CHARGED)
719
g_snprintf (str, size, _("%s (charged)"), kind_str);
725
if (time_is_relevant (device))
728
get_accessible_time_remaining (device, buf, sizeof(buf));
730
get_expanded_time_remaining (device, buf, sizeof(buf));
738
g_snprintf (str, size, _("%s (%s)"), kind_str, buf);
740
g_strlcpy (str, kind_str, size);
745
indicator_power_device_get_readable_text (const IndicatorPowerDevice * device,
749
g_return_if_fail (str != NULL);
750
g_return_if_fail (size > 0);
752
g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
754
get_menuitem_text (device, str, size, FALSE);
758
indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device,
762
g_return_if_fail (str != NULL);
763
g_return_if_fail (size > 0);
765
g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
767
get_menuitem_text (device, str, size, TRUE);
771
- If the time is relevant, the brackets should contain the time-remaining string for that component.
772
+ If the time is relevant, the brackets should contain the brief time-remaining string for that component.
774
- 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.
775
+ The accessible name for the whole menu title should be the same as the except using the accessible time-remaining string instead of the brief time-remaining string.
779
* If the time is relevant and/or “Show Percentage in Menu Bar” is checked,
780
* the icon should be followed by brackets.
782
* If the time is relevant, the brackets should contain the time-remaining
783
* string for that component.
785
* If “Show Percentage in Menu Bar” is checked (as it should not be by default),
786
* the brackets should contain the percentage charge for that device.
788
* If both conditions are true, the time and percentage should be separated by a space.
791
indicator_power_device_get_readable_title (const IndicatorPowerDevice * device,
795
gboolean want_percent)
798
const IndicatorPowerDevicePrivate * p;
800
g_return_if_fail (str != NULL);
801
g_return_if_fail (size > 0);
803
g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
807
if (want_time && !time_is_relevant (device))
810
if (p->percentage < 0.01)
811
want_percent = FALSE;
815
get_brief_time_remaining (device, tr, sizeof(tr));
821
if (want_time && want_percent)
823
g_snprintf (str, size, _("(%s, %.0lf%%)"), tr, p->percentage);
827
g_snprintf (str, size, _("(%s)"), tr);
829
else if (want_percent)
831
g_snprintf (str, size, _("(%.0lf%%)"), p->percentage);
840
* Regardless, the accessible name for the whole menu title should be the same
841
* as the accessible name for that thing’s component inside the menu itself.
844
indicator_power_device_get_accessible_title (const IndicatorPowerDevice * device,
847
gboolean want_time G_GNUC_UNUSED,
848
gboolean want_percent G_GNUC_UNUSED)
850
indicator_power_device_get_accessible_text (device, str, size);