summaryrefslogtreecommitdiffstats
path: root/panels/power/cc-battery-row.c
blob: fe806224dcdf8118094bf49fdd0d2d51d5b879e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* cc-brightness-scale.c
 *
 * Copyright (C) 2010 Red Hat, Inc
 * Copyright (C) 2008 William Jon McCann <jmccann@redhat.com>
 * Copyright (C) 2010,2015 Richard Hughes <richard@hughsie.com>
 * Copyright (C) 2020 System76, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <glib/gi18n.h>

#include "cc-battery-row.h"

struct _CcBatteryRow {
  GtkListBoxRow parent_instance;

  GtkBox       *battery_box;
  GtkLabel     *details_label;
  GtkImage     *icon;
  GtkLevelBar  *levelbar;
  GtkLabel     *name_label;
  GtkLabel     *percentage_label;
  GtkBox       *primary_bottom_box;
  GtkLabel     *primary_percentage_label;

  UpDeviceKind  kind;
  gboolean      primary;
};

G_DEFINE_TYPE (CcBatteryRow, cc_battery_row, GTK_TYPE_LIST_BOX_ROW)

static void
cc_battery_row_class_init (CcBatteryRowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/power/cc-battery-row.ui");

  gtk_widget_class_bind_template_child (widget_class, CcBatteryRow, battery_box);
  gtk_widget_class_bind_template_child (widget_class, CcBatteryRow, details_label);
  gtk_widget_class_bind_template_child (widget_class, CcBatteryRow, icon);
  gtk_widget_class_bind_template_child (widget_class, CcBatteryRow, levelbar);
  gtk_widget_class_bind_template_child (widget_class, CcBatteryRow, name_label);
  gtk_widget_class_bind_template_child (widget_class, CcBatteryRow, percentage_label);
  gtk_widget_class_bind_template_child (widget_class, CcBatteryRow, primary_bottom_box);
  gtk_widget_class_bind_template_child (widget_class, CcBatteryRow, primary_percentage_label);
}

static void
cc_battery_row_init (CcBatteryRow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
}

static gchar *
get_timestring (guint64 time_secs)
{
  gchar* timestring = NULL;
  gint  hours;
  gint  minutes;

  /* Add 0.5 to do rounding */
  minutes = (int) ( ( time_secs / 60.0 ) + 0.5 );

  if (minutes == 0)
    return g_strdup (_("Unknown time"));

  if (minutes < 60)
    return timestring = g_strdup_printf (ngettext ("%i minute",
                                         "%i minutes",
                                         minutes), minutes);

  hours = minutes / 60;
  minutes = minutes % 60;

  if (minutes == 0)
    return timestring = g_strdup_printf (ngettext (
                                         "%i hour",
                                         "%i hours",
                                         hours), hours);

  /* TRANSLATOR: "%i %s %i %s" are "%i hours %i minutes"
   * Swap order with "%2$s %2$i %1$s %1$i if needed */
  return timestring = g_strdup_printf (_("%i %s %i %s"),
                                       hours, ngettext ("hour", "hours", hours),
                                       minutes, ngettext ("minute", "minutes", minutes));
}

static gchar *
get_details_string (gdouble percentage, UpDeviceState state, guint64 time)
{
  g_autofree gchar *details = NULL;

  if (time > 0)
    {
      g_autofree gchar *time_string = NULL;

      time_string = get_timestring (time);
      switch (state)
        {
          case UP_DEVICE_STATE_CHARGING:
            /* TRANSLATORS: %1 is a time string, e.g. "1 hour 5 minutes" */
            details = g_strdup_printf (_("%s until fully charged"), time_string);
            break;
          case UP_DEVICE_STATE_DISCHARGING:
          case UP_DEVICE_STATE_PENDING_DISCHARGE:
            if (percentage < 20)
              {
                /* TRANSLATORS: %1 is a time string, e.g. "1 hour 5 minutes" */
                details = g_strdup_printf (_("Caution: %s remaining"), time_string);
              }
            else
              {
                /* TRANSLATORS: %1 is a time string, e.g. "1 hour 5 minutes" */
                details = g_strdup_printf (_("%s remaining"), time_string);
              }
            break;
          case UP_DEVICE_STATE_FULLY_CHARGED:
            /* TRANSLATORS: primary battery */
            details = g_strdup (_("Fully charged"));
            break;
          case UP_DEVICE_STATE_PENDING_CHARGE:
            /* TRANSLATORS: primary battery */
            details = g_strdup (_("Not charging"));
            break;
          case UP_DEVICE_STATE_EMPTY:
            /* TRANSLATORS: primary battery */
            details = g_strdup (_("Empty"));
            break;
          default:
            details = g_strdup_printf ("error: %s", up_device_state_to_string (state));
            break;
        }
    }
  else
    {
      switch (state)
        {
          case UP_DEVICE_STATE_CHARGING:
            /* TRANSLATORS: primary battery */
            details = g_strdup (_("Charging"));
            break;
          case UP_DEVICE_STATE_DISCHARGING:
          case UP_DEVICE_STATE_PENDING_DISCHARGE:
            /* TRANSLATORS: primary battery */
            details = g_strdup (_("Discharging"));
            break;
          case UP_DEVICE_STATE_FULLY_CHARGED:
            /* TRANSLATORS: primary battery */
            details = g_strdup (_("Fully charged"));
            break;
          case UP_DEVICE_STATE_PENDING_CHARGE:
            /* TRANSLATORS: primary battery */
            details = g_strdup (_("Not charging"));
            break;
          case UP_DEVICE_STATE_EMPTY:
            /* TRANSLATORS: primary battery */
            details = g_strdup (_("Empty"));
            break;
          default:
            details = g_strdup_printf ("error: %s",
                                       up_device_state_to_string (state));
            break;
        }
    }

  return g_steal_pointer (&details);
}

static const char *
kind_to_description (UpDeviceKind kind)
{
  switch (kind)
    {
      case UP_DEVICE_KIND_MOUSE:
        /* TRANSLATORS: secondary battery */
        return N_("Wireless mouse");
      case UP_DEVICE_KIND_KEYBOARD:
        /* TRANSLATORS: secondary battery */
        return N_("Wireless keyboard");
      case UP_DEVICE_KIND_UPS:
        /* TRANSLATORS: secondary battery */
        return N_("Uninterruptible power supply");
      case UP_DEVICE_KIND_PDA:
        /* TRANSLATORS: secondary battery */
        return N_("Personal digital assistant");
      case UP_DEVICE_KIND_PHONE:
        /* TRANSLATORS: secondary battery */
        return N_("Cellphone");
      case UP_DEVICE_KIND_MEDIA_PLAYER:
        /* TRANSLATORS: secondary battery */
        return N_("Media player");
      case UP_DEVICE_KIND_TABLET:
        /* TRANSLATORS: secondary battery */
        return N_("Tablet");
      case UP_DEVICE_KIND_COMPUTER:
        /* TRANSLATORS: secondary battery */
        return N_("Computer");
      case UP_DEVICE_KIND_GAMING_INPUT:
        /* TRANSLATORS: secondary battery */
        return N_("Gaming input device");
      default:
        /* TRANSLATORS: secondary battery, misc */
        return N_("Battery");
    }

  g_assert_not_reached ();
}

CcBatteryRow*
cc_battery_row_new (UpDevice *device,
                    gboolean  primary)
{
  g_autofree gchar *details = NULL;
  gdouble percentage;
  UpDeviceKind kind;
  UpDeviceState state;
  g_autofree gchar *s = NULL;
  g_autofree gchar *icon_name = NULL;
  const gchar *name;
  CcBatteryRow *self;
  guint64 time_empty, time_full, time;
  gdouble energy_full, energy_rate;
  gboolean is_kind_battery;
  UpDeviceLevel battery_level;

  self = g_object_new (CC_TYPE_BATTERY_ROW, NULL);

  g_object_get (device,
                "kind", &kind,
                "state", &state,
                "model", &name,
                "percentage", &percentage,
                "icon-name", &icon_name,
                "time-to-empty", &time_empty,
                "time-to-full", &time_full,
                "energy-full", &energy_full,
                "energy-rate", &energy_rate,
                "battery-level", &battery_level,
                NULL);
  if (state == UP_DEVICE_STATE_DISCHARGING)
    time = time_empty;
  else
    time = time_full;

  is_kind_battery = (kind == UP_DEVICE_KIND_BATTERY || kind == UP_DEVICE_KIND_UPS);

  /* Name label */
  if (is_kind_battery)
    {
      if (g_object_get_data (G_OBJECT (device), "is-main-battery") != NULL)
        name = C_("Battery name", "Main");
      else
        name = C_("Battery name", "Extra");
    }
  else if (name == NULL || name[0] == '\0')
    {
      name = _(kind_to_description (kind));
    }
  gtk_label_set_text (self->name_label, name);

  /* Icon */
  if (is_kind_battery && icon_name != NULL && icon_name[0] != '\0')
    {
      gtk_image_set_from_icon_name (self->icon, icon_name);
      gtk_widget_show (GTK_WIDGET (self->icon));
    }
  else
    gtk_widget_hide (GTK_WIDGET (self->icon));

  /* Percentage label */
  if (battery_level == UP_DEVICE_LEVEL_NONE)
  {
    s = g_strdup_printf ("%d%%", (int)percentage);
    gtk_label_set_text (self->percentage_label, s);
    gtk_label_set_text (self->primary_percentage_label, s);
  }

  /* Level bar */
  gtk_level_bar_set_value (self->levelbar, percentage / 100.0);

  /* Details label (primary only) */
  details = get_details_string (percentage, state, time);
  gtk_label_set_text (self->details_label, details);

  /* Handle "primary" row differently */
  gtk_widget_set_visible (GTK_WIDGET (self->battery_box), !primary);
  gtk_widget_set_visible (GTK_WIDGET (self->percentage_label), !primary);
  gtk_widget_set_visible (GTK_WIDGET (self->primary_bottom_box), primary);
  /*
  gtk_accessible_update_relation (GTK_ACCESSIBLE (self->levelbar),
                                  GTK_ACCESSIBLE_RELATION_LABELLED_BY, primary ? self->primary_percentage_label
                                                                               : self->percentage_label,
                                  NULL);
   */

  self->kind = kind;
  self->primary = primary;

  return self;
}



void
cc_battery_row_set_level_sizegroup (CcBatteryRow *self,
                                    GtkSizeGroup *sizegroup)
{
  gtk_size_group_add_widget (sizegroup, GTK_WIDGET (self->levelbar));
}

void
cc_battery_row_set_row_sizegroup (CcBatteryRow *self,
                                  GtkSizeGroup *sizegroup)
{
  gtk_size_group_add_widget (sizegroup, GTK_WIDGET (self));
}

void
cc_battery_row_set_charge_sizegroup (CcBatteryRow *self,
                                     GtkSizeGroup *sizegroup)
{
  gtk_size_group_add_widget (sizegroup, GTK_WIDGET (self->percentage_label));
}

void
cc_battery_row_set_battery_sizegroup (CcBatteryRow *self,
                                      GtkSizeGroup *sizegroup)
{
  gtk_size_group_add_widget (sizegroup, GTK_WIDGET (self->battery_box));
}

gboolean
cc_battery_row_get_primary (CcBatteryRow *self)
{
  return self->primary;
}

UpDeviceKind
cc_battery_row_get_kind (CcBatteryRow *self)
{
  return self->kind;
}