~charlesk/indicator-power/lp-1224931

« back to all changes in this revision

Viewing changes to src/device.c

  • Committer: Tarmac
  • Author(s): Charles Kerr
  • Date: 2013-08-23 14:45:19 UTC
  • mfrom: (187.1.6 lp-811777)
  • Revision ID: tarmac-20130823144519-t5n7n1oi83mg4bp9
Updates the power indicator to match the spec changes at <https://wiki.ubuntu.com/Power?action=diff&rev2=37&rev1=36>.

This patch is based from hloeung's nice patch, fixes a few edge cases, adds unit tests for labels, headers, & accessible text for all combinations of show time & show percentage, and cleans up the bindings between GSettings and the checkbox actions.

. Fixes: https://bugs.launchpad.net/bugs/811777.

Approved by Ted Gould, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
449
449
****
450
450
***/
451
451
 
 
452
/* Format time remaining for reading ("H:MM") and speech ("H hours, MM minutes") */
452
453
static void
453
454
get_timestring (guint64   time_secs,
454
 
                gchar   **short_timestring,
455
 
                gchar   **detailed_timestring)
 
455
                gchar   **readable_timestring,
 
456
                gchar   **accessible_timestring)
456
457
{
457
458
  gint  hours;
458
459
  gint  minutes;
462
463
 
463
464
  if (minutes == 0)
464
465
    {
465
 
      *short_timestring = g_strdup (_("Unknown time"));
466
 
      *detailed_timestring = g_strdup (_("Unknown time"));
 
466
      *readable_timestring = g_strdup (_("Unknown time"));
 
467
      *accessible_timestring = g_strdup (_("Unknown time"));
467
468
 
468
469
      return;
469
470
    }
470
471
 
471
472
  if (minutes < 60)
472
473
    {
473
 
      *short_timestring = g_strdup_printf ("0:%.2i", minutes);
474
 
      *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute",
 
474
      *readable_timestring = g_strdup_printf ("0:%.2i", minutes);
 
475
      *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute",
475
476
                                              "%i minutes",
476
477
                                              minutes), minutes);
477
478
      return;
480
481
  hours = minutes / 60;
481
482
  minutes = minutes % 60;
482
483
 
483
 
  *short_timestring = g_strdup_printf ("%i:%.2i", hours, minutes);
 
484
  *readable_timestring = g_strdup_printf ("%i:%.2i", hours, minutes);
484
485
 
485
486
  if (minutes == 0)
486
487
    {
487
 
      *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, 
 
488
      *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
488
489
                                              "%i hour",
489
490
                                              "%i hours",
490
491
                                              hours), hours);
493
494
    {
494
495
      /* TRANSLATOR: "%i %s %i %s" are "%i hours %i minutes"
495
496
       * Swap order with "%2$s %2$i %1$s %1$i if needed */
496
 
      *detailed_timestring = g_strdup_printf (_("%i %s %i %s"),
 
497
      *accessible_timestring = g_strdup_printf (_("%i %s %i %s"),
497
498
                                              hours, g_dngettext (GETTEXT_PACKAGE, "hour", "hours", hours),
498
499
                                              minutes, g_dngettext (GETTEXT_PACKAGE, "minute", "minutes", minutes));
499
500
    }
561
562
  return text;
562
563
}
563
564
 
564
 
void
565
 
indicator_power_device_get_time_details (const IndicatorPowerDevice * device,
566
 
                                         gchar ** short_details,
567
 
                                         gchar ** details,
568
 
                                         gchar ** accessible_name)
 
565
static char *
 
566
join_strings (const char * name, const char * time, const char * percent)
 
567
{
 
568
  char * str;
 
569
  const gboolean have_name = name && *name;
 
570
  const gboolean have_time = time && *time;
 
571
  const gboolean have_percent = percent && *percent;
 
572
 
 
573
  if (have_name && have_time && have_percent)
 
574
    str = g_strdup_printf (_("%s (%s, %s)"), name, time, percent);
 
575
  else if (have_name && have_time)
 
576
    str = g_strdup_printf (_("%s (%s)"), name, time);
 
577
  else if (have_name && have_percent)
 
578
    str = g_strdup_printf (_("%s (%s)"), name, percent);
 
579
  else if (have_name)
 
580
    str = g_strdup (name);
 
581
  else if (have_time && have_percent)
 
582
    str = g_strdup_printf (_("(%s, %s)"), time, percent);
 
583
  else if (have_time)
 
584
    str = g_strdup_printf (_("(%s)"), time);
 
585
  else if (have_percent)
 
586
    str = g_strdup_printf (_("(%s)"), percent);
 
587
  else
 
588
    str = g_strdup ("");
 
589
 
 
590
  return str;
 
591
}
 
592
 
 
593
static void
 
594
indicator_power_device_get_text (const IndicatorPowerDevice * device,
 
595
                                 gboolean show_time_in_header,
 
596
                                 gboolean show_percentage_in_header,
 
597
                                 gchar ** header,
 
598
                                 gchar ** label,
 
599
                                 gchar ** a11y)
569
600
{
570
601
  if (!INDICATOR_IS_POWER_DEVICE(device))
571
602
    {
572
 
      *short_details = NULL;
573
 
      *details = NULL;
574
 
      *accessible_name = NULL;
 
603
      if (a11y != NULL) *a11y = NULL;
 
604
      if (label != NULL) *label = NULL;
 
605
      if (header != NULL) *header = NULL;
575
606
      g_warning ("%s: %p is not an IndicatorPowerDevice", G_STRFUNC, device);
576
607
      return;
577
608
    }
578
609
 
579
610
  const time_t time = indicator_power_device_get_time (device);
580
611
  const UpDeviceState state = indicator_power_device_get_state (device);
 
612
  const UpDeviceKind kind = indicator_power_device_get_kind (device);
 
613
  const gchar * device_name = device_kind_to_localised_string (kind);
581
614
  const gdouble percentage = indicator_power_device_get_percentage (device);
582
 
  const UpDeviceKind kind = indicator_power_device_get_kind (device);
583
 
  const gchar * device_name = device_kind_to_localised_string (kind);
 
615
  char pctstr[32] = { '\0' };
 
616
  g_snprintf (pctstr, sizeof(pctstr), "%.0lf%%", percentage);
 
617
 
 
618
  GString * terse_time = g_string_new (NULL);
 
619
  GString * verbose_time = g_string_new (NULL);
 
620
  GString * accessible_time = g_string_new (NULL);
584
621
 
585
622
  if (time > 0)
586
623
    {
587
 
      gchar *short_timestring = NULL;
588
 
      gchar *detailed_timestring = NULL;
589
 
 
590
 
      get_timestring (time,
591
 
                      &short_timestring,
592
 
                      &detailed_timestring);
 
624
      char * readable_timestr = NULL;
 
625
      char * accessible_timestr = NULL;
 
626
      get_timestring (time, &readable_timestr, &accessible_timestr);
593
627
 
594
628
      if (state == UP_DEVICE_STATE_CHARGING)
595
629
        {
596
 
          /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */
597
 
          *accessible_name = g_strdup_printf (_("%s (%s to charge (%.0lf%%))"), device_name, detailed_timestring, percentage);
598
 
          *details = g_strdup_printf (_("%s (%s to charge)"), device_name, short_timestring);
599
 
          *short_details = g_strdup_printf ("(%s)", short_timestring);
 
630
          g_string_assign (terse_time, readable_timestr);
 
631
          g_string_printf (verbose_time, _("%s to charge"), readable_timestr);
 
632
          g_string_printf (accessible_time, _("%s to charge"), accessible_timestr);
600
633
        }
601
 
      else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time > (60*60*12)))
 
634
      else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time <= (60*60*12)))
602
635
        {
603
 
          *accessible_name = g_strdup_printf (_("%s"), device_name);
604
 
          *details = g_strdup_printf (_("%s"), device_name);
605
 
          *short_details = g_strdup (short_timestring);
 
636
          g_string_assign (terse_time, readable_timestr);
 
637
          g_string_printf (verbose_time, _("%s left"), readable_timestr);
 
638
          g_string_printf (accessible_time, _("%s left"), accessible_timestr);
606
639
        }
607
640
      else
608
641
        {
609
 
          /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */
610
 
          *accessible_name = g_strdup_printf (_("%s (%s left (%.0lf%%))"), device_name, detailed_timestring, percentage);
611
 
          *details = g_strdup_printf (_("%s (%s left)"), device_name, short_timestring);
612
 
          *short_details = g_strdup (short_timestring);
 
642
          /* if there's more than 12 hours remaining, we don't show it */
613
643
        }
614
644
 
615
 
      g_free (short_timestring);
616
 
      g_free (detailed_timestring);
 
645
      g_free (readable_timestr);
 
646
      g_free (accessible_timestr);
617
647
    }
618
648
  else if (state == UP_DEVICE_STATE_FULLY_CHARGED)
619
649
    {
620
 
      *details = g_strdup_printf (_("%s (charged)"), device_name);
621
 
      *accessible_name = g_strdup (*details);
622
 
      *short_details = g_strdup ("");
 
650
      g_string_assign (verbose_time,    _("charged"));
 
651
      g_string_assign (accessible_time, _("charged"));
623
652
    }
624
653
  else if (percentage > 0)
625
654
    {
626
 
      /* TRANSLATORS: %2 is a percentage value. Note: this string is only
627
 
       * used when we don't have a time value */
628
 
      *details = g_strdup_printf (_("%s (%.0lf%%)"), device_name, percentage);
629
 
      *accessible_name = g_strdup (*details);
630
 
      *short_details = g_strdup_printf (_("(%.0lf%%)"), percentage);
631
 
    }
632
 
  else if (kind == UP_DEVICE_KIND_LINE_POWER)
633
 
    {
634
 
      *details         = g_strdup (device_name);
635
 
      *accessible_name = g_strdup (device_name);
636
 
      *short_details   = g_strdup ("");
 
655
      g_string_assign (terse_time,      _("estimating…"));
 
656
      g_string_assign (verbose_time,    _("estimating…"));
 
657
      g_string_assign (accessible_time, _("estimating…"));
637
658
    }
638
659
  else
639
660
    {
640
 
      *details = g_strdup_printf (_("%s (not present)"), device_name);
641
 
      *accessible_name = g_strdup (*details);
642
 
      *short_details = g_strdup (_("(not present)"));
 
661
      *pctstr = '\0';
 
662
 
 
663
      if (kind != UP_DEVICE_KIND_LINE_POWER)
 
664
        {
 
665
          g_string_assign (verbose_time,    _("not present"));
 
666
          g_string_assign (accessible_time, _("not present"));
 
667
        }
643
668
    }
 
669
 
 
670
  if (header != NULL)
 
671
    *header = join_strings (NULL,
 
672
                            show_time_in_header ? terse_time->str : "",
 
673
                            show_percentage_in_header ? pctstr : "");
 
674
 
 
675
  if (label != NULL)
 
676
    *label  = join_strings (device_name,
 
677
                            verbose_time->str,
 
678
                            NULL);
 
679
 
 
680
  if (a11y != NULL)
 
681
    *a11y   = join_strings (device_name,
 
682
                            accessible_time->str,
 
683
                            pctstr);
 
684
 
 
685
  g_string_free (terse_time, TRUE);
 
686
  g_string_free (verbose_time, TRUE);
 
687
  g_string_free (accessible_time, TRUE);
 
688
}
 
689
 
 
690
gchar *
 
691
indicator_power_device_get_label (const IndicatorPowerDevice * device)
 
692
{
 
693
  gchar * label = NULL;
 
694
  indicator_power_device_get_text (device, FALSE, FALSE,
 
695
                                   NULL, &label, NULL);
 
696
  return label;
 
697
}
 
698
 
 
699
void
 
700
indicator_power_device_get_header (const IndicatorPowerDevice * device,
 
701
                                   gboolean                     show_time,
 
702
                                   gboolean                     show_percentage,
 
703
                                   gchar                     ** header,
 
704
                                   gchar                     ** a11y)
 
705
{
 
706
  indicator_power_device_get_text (device, show_time, show_percentage,
 
707
                                   header, NULL, a11y);
644
708
}
645
709
 
646
710
/***