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);
537
* The '''brief time-remaining string''' for a component should be:
538
* * the time remaining for it to empty or fully charge,
539
* if estimable, in H:MM format; otherwise
540
* * “estimating…” if the time remaining has been inestimable for
541
* less than 30 seconds; otherwise
542
* * “unknown” if the time remaining has been inestimable for
543
* between 30 seconds and one minute; otherwise
544
* * the empty string.
547
get_brief_time_remaining (const IndicatorPowerDevice * device)
550
const IndicatorPowerDevicePrivate * p = device->priv;
554
int minutes = p->time / 60;
555
const int hours = minutes / 60;
558
str = g_strdup_printf("%0d:%02d", hours, minutes);
560
else if (p->inestimable != NULL)
562
const double elapsed = g_timer_elapsed (p->inestimable, NULL);
566
str = g_strdup_printf (_("estimating…"));
568
else if (elapsed < 60)
570
str = g_strdup_printf (_("unknown"));
578
* The '''expanded time-remaining string''' for a component should
579
* be the same as the brief time-remaining string, except that if
580
* the time is estimable:
581
* * if the component is charging, it should be “H:MM to charge”
582
* * if the component is discharging, it should be “H:MM left”.
585
get_expanded_time_remaining (const IndicatorPowerDevice * device)
588
const IndicatorPowerDevicePrivate * p = device->priv;
590
if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING)))
592
int minutes = p->time / 60;
593
const int hours = minutes / 60;
596
if (p->state == UP_DEVICE_STATE_CHARGING)
598
str = g_strdup_printf (_("%0d:%02d to charge"), hours, minutes);
602
str = g_strdup_printf (_("%0d:%02d left"), hours, minutes);
607
str = get_brief_time_remaining (device);
614
* The '''accessible time-remaining string''' for a component
615
* should be the same as the expanded time-remaining string,
616
* except the H:MM time should be rendered as “''H'' hours ''M'' minutes”,
617
* or just as “''M'' minutes” if the time is less than one hour.
620
get_accessible_time_remaining (const IndicatorPowerDevice * device)
623
const IndicatorPowerDevicePrivate * p = device->priv;
625
if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING)))
627
int minutes = p->time / 60;
628
const int hours = minutes / 60;
631
if (p->state == UP_DEVICE_STATE_CHARGING)
634
str = g_strdup_printf (_("%d %s %d %s to charge"),
635
hours, g_dngettext (NULL, "hour", "hours", hours),
636
minutes, g_dngettext (NULL, "minute", "minutes", minutes));
638
str = g_strdup_printf (_("%d %s to charge"),
639
minutes, g_dngettext (NULL, "minute", "minutes", minutes));
644
str = g_strdup_printf (_("%d %s %d %s left"),
645
hours, g_dngettext (NULL, "hour", "hours", hours),
646
minutes, g_dngettext (NULL, "minute", "minutes", minutes));
648
str = g_strdup_printf (_("%d %s left"),
649
minutes, g_dngettext (NULL, "minute", "minutes", minutes));
654
str = get_brief_time_remaining (device);
661
* The time is relevant for a device if either (a) the component is charging,
662
* or (b) the component is discharging and the estimated time is less than
663
* 24 hours. (A time greater than 24 hours is probably a mistaken calculation.)
666
time_is_relevant (const IndicatorPowerDevice * device)
668
const IndicatorPowerDevicePrivate * p = device->priv;
670
if (p->state == UP_DEVICE_STATE_CHARGING)
673
if ((p->state == UP_DEVICE_STATE_DISCHARGING) && (p->time<(24*60*60)))
680
* The menu item for each chargeable component should consist of ...
681
* Text representing the name of the component (“Battery”, “Mouse”,
682
* “UPS”, “Alejandra’s iPod”, etc) and the charge status in brackets:
684
* * “X (charged)” if it is fully charged and not discharging;
685
* * “X (expanded time-remaining string)” if it is charging,
686
* or discharging with less than 24 hours left;
687
* * “X” if it is discharging with 24 hours or more left.
689
* The accessible label for the menu item should be the same as the
690
* visible label, except with the accessible time-remaining string
691
* instead of the expanded time-remaining string.
694
get_menuitem_text (const IndicatorPowerDevice * device,
698
const IndicatorPowerDevicePrivate * p = device->priv;
699
const char * kind_str = device_kind_to_localised_string (p->kind);
701
if (p->state == UP_DEVICE_STATE_FULLY_CHARGED)
703
str = g_strdup_printf (_("%s (charged)"), kind_str);
707
char * time_str = NULL;
709
if (time_is_relevant (device))
712
time_str = get_accessible_time_remaining (device);
714
time_str = get_expanded_time_remaining (device);
717
if (time_str && *time_str)
718
str = g_strdup_printf (_("%s (%s)"), kind_str, time_str);
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…"));
720
str = g_strdup (kind_str);
729
indicator_power_device_get_readable_text (const IndicatorPowerDevice * device)
731
g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
733
return get_menuitem_text (device, FALSE);
737
indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device)
739
g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
741
return get_menuitem_text (device, TRUE);
745
* If the time is relevant and/or “Show Percentage in Menu Bar” is checked,
746
* the icon should be followed by brackets.
748
* If the time is relevant, the brackets should contain the time-remaining
749
* string for that component.
751
* If “Show Percentage in Menu Bar” is checked (as it should not be by default),
752
* the brackets should contain the percentage charge for that device.
754
* If both conditions are true, the time and percentage should be separated by a space.
757
indicator_power_device_get_readable_title (const IndicatorPowerDevice * device,
759
gboolean want_percent)
762
char * time_str = NULL;
763
const IndicatorPowerDevicePrivate * p;
765
g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
769
// if we can't provide time-remaining, turn off the time flag
770
if (want_time && !time_is_relevant (device))
773
// if we can't provide percent, turn off the percent flag
774
if (p->percentage < 0.01)
775
want_percent = FALSE;
777
// try to build the time-remaining string
780
time_str = get_brief_time_remaining (device);
781
want_time = time_str && *time_str;
784
if (want_time && want_percent)
786
str = g_strdup_printf (_("(%s, %.0lf%%)"), time_str, p->percentage);
790
str = g_strdup_printf (_("(%s)"), time_str);
792
else if (want_percent)
794
str = g_strdup_printf (_("(%.0lf%%)"), p->percentage);
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,
806
* Regardless, the accessible name for the whole menu title should be the same
807
* as the accessible name for that thing’s component inside the menu itself.
810
indicator_power_device_get_accessible_title (const IndicatorPowerDevice * device,
811
gboolean want_time G_GNUC_UNUSED,
812
gboolean want_percent G_GNUC_UNUSED)
814
g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
816
return indicator_power_device_get_accessible_text (device);