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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
/*
An indicator to power related information in the menubar.
Copyright 2011 Canonical Ltd.
Authors:
Javier Jardon <javier.jardon@codethink.co.uk>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* GStuff */
#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>
/* upower */
#include <libupower-glib/upower.h>
/* Indicator Stuff */
#include <libindicator/indicator.h>
#include <libindicator/indicator-object.h>
#include <libindicator/indicator-image-helper.h>
#define DEFAULT_ICON "gpm-battery-empty"
#define INDICATOR_POWER_TYPE (indicator_power_get_type ())
#define INDICATOR_POWER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_POWER_TYPE, IndicatorPower))
#define INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_POWER_TYPE, IndicatorPowerClass))
#define IS_INDICATOR_POWER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_POWER_TYPE))
#define IS_INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_TYPE))
#define INDICATOR_POWER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_TYPE, IndicatorPowerClass))
typedef struct _IndicatorPower IndicatorPower;
typedef struct _IndicatorPowerClass IndicatorPowerClass;
typedef struct _IndicatorPowerPrivate IndicatorPowerPrivate;
struct _IndicatorPower
{
IndicatorObject parent_instance;
IndicatorPowerPrivate *priv;
};
struct _IndicatorPowerClass
{
IndicatorObjectClass parent_class;
};
GType indicator_power_get_type (void) G_GNUC_CONST;
struct _IndicatorPowerPrivate
{
GtkMenu *menu;
GtkLabel *label;
GtkImage *status_image;
gchar *accessible_desc;
GCancellable *proxy_cancel;
GDBusProxy *proxy;
GVariant *device;
};
/* Prototypes */
static void indicator_power_class_init (IndicatorPowerClass *klass);
static void indicator_power_init (IndicatorPower *self);
static void indicator_power_dispose (GObject *object);
static void indicator_power_finalize (GObject *object);
static GtkLabel* get_label (IndicatorObject * io);
static GtkImage* get_image (IndicatorObject * io);
static GtkMenu* get_menu (IndicatorObject * io);
static const gchar* get_accessible_desc (IndicatorObject * io);
G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE);
/* Indicator stuff */
INDICATOR_SET_VERSION
INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE)
static void
indicator_power_class_init (IndicatorPowerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
IndicatorObjectClass *io_class = INDICATOR_OBJECT_CLASS (klass);
object_class->dispose = indicator_power_dispose;
object_class->finalize = indicator_power_finalize;
io_class->get_label = get_label;
io_class->get_image = get_image;
io_class->get_menu = get_menu;
io_class->get_accessible_desc = get_accessible_desc;
g_type_class_add_private (klass, sizeof (IndicatorPowerPrivate));
}
static void
show_info_cb (GtkMenuItem *item,
gpointer data)
{
/*TODO: show the statistics of the specific device*/
const gchar *command = "gnome-power-statistics";
if (g_spawn_command_line_async (command, NULL) == FALSE)
g_warning ("Couldn't execute command: %s", command);
}
static void
option_toggled_cb (GtkCheckMenuItem *item,
gpointer user_data)
{
IndicatorPower *self = INDICATOR_POWER (user_data);
IndicatorPowerPrivate *priv = self->priv;
gtk_widget_set_visible (GTK_WIDGET (priv->label),
gtk_check_menu_item_get_active (item));
}
static void
show_preferences_cb (GtkMenuItem *item,
gpointer data)
{
const gchar *command = "gnome-control-center power";
if (g_spawn_command_line_async (command, NULL) == FALSE)
g_warning ("Couldn't execute command: %s", command);
}
static void
get_timestring (guint64 time_secs,
gchar **short_timestring,
gchar **detailed_timestring)
{
gint hours;
gint minutes;
/* Add 0.5 to do rounding */
minutes = (int) ( ( time_secs / 60.0 ) + 0.5 );
if (minutes == 0)
{
*short_timestring = g_strdup (_("Unknown time"));
*detailed_timestring = g_strdup (_("Unknown time"));
return;
}
if (minutes < 60)
{
*short_timestring = g_strdup_printf ("0:%.2i", minutes);
*detailed_timestring = g_strdup_printf (ngettext ("%i minute",
"%i minutes",
minutes), minutes);
return;
}
hours = minutes / 60;
minutes = minutes % 60;
*short_timestring = g_strdup_printf ("%i:%.2i", hours, minutes);
if (minutes == 0)
{
*detailed_timestring = g_strdup_printf (ngettext (
"%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 */
*detailed_timestring = g_strdup_printf (_("%i %s %i %s"),
hours, ngettext ("hour", "hours", hours),
minutes, ngettext ("minute", "minutes", minutes));
}
}
static const gchar *
device_kind_to_localised_string (UpDeviceKind kind)
{
const gchar *text = NULL;
switch (kind) {
case UP_DEVICE_KIND_LINE_POWER:
/* TRANSLATORS: system power cord */
text = gettext ("AC adapter");
break;
case UP_DEVICE_KIND_BATTERY:
/* TRANSLATORS: laptop primary battery */
text = gettext ("Battery");
break;
case UP_DEVICE_KIND_UPS:
/* TRANSLATORS: battery-backed AC power source */
text = gettext ("UPS");
break;
case UP_DEVICE_KIND_MONITOR:
/* TRANSLATORS: a monitor is a device to measure voltage and current */
text = gettext ("Monitor");
break;
case UP_DEVICE_KIND_MOUSE:
/* TRANSLATORS: wireless mice with internal batteries */
text = gettext ("Mouse");
break;
case UP_DEVICE_KIND_KEYBOARD:
/* TRANSLATORS: wireless keyboard with internal battery */
text = gettext ("Keyboard");
break;
case UP_DEVICE_KIND_PDA:
/* TRANSLATORS: portable device */
text = gettext ("PDA");
break;
case UP_DEVICE_KIND_PHONE:
/* TRANSLATORS: cell phone (mobile...) */
text = gettext ("Cell phone");
break;
case UP_DEVICE_KIND_MEDIA_PLAYER:
/* TRANSLATORS: media player, mp3 etc */
text = gettext ("Media player");
break;
case UP_DEVICE_KIND_TABLET:
/* TRANSLATORS: tablet device */
text = gettext ("Tablet");
break;
case UP_DEVICE_KIND_COMPUTER:
/* TRANSLATORS: tablet device */
text = gettext ("Computer");
break;
default:
g_warning ("enum unrecognised: %i", kind);
text = up_device_kind_to_string (kind);
}
return text;
}
static void
build_device_time_details (const gchar *device_name,
guint64 time,
UpDeviceState state,
gdouble percentage,
gchar **short_details,
gchar **details)
{
gchar *short_timestring = NULL;
gchar *detailed_timestring = NULL;
if (time > 0)
{
get_timestring (time,
&short_timestring,
&detailed_timestring);
*short_details = g_strdup_printf ("(%s)",
short_timestring);
if (state == UP_DEVICE_STATE_CHARGING)
{
/* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */
*details = g_strdup_printf (_("%s (%s until charged (%.0lf%%))"),
device_name, detailed_timestring, percentage);
}
else if (state == UP_DEVICE_STATE_DISCHARGING)
{
/* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */
*details = g_strdup_printf (_("%s (%s until empty (%.0lf%%))"),
device_name, detailed_timestring, percentage);
}
}
else
{
/* TRANSLATORS: %2 is a percentage value. Note: this string is only
* used when we don't have a time value */
*details = g_strdup_printf (_("%s (%.0lf%%)"),
device_name, percentage);
*short_details = g_strdup_printf (_("(%.0lf%%)"),
percentage);
}
}
static void
set_accessible_desc (IndicatorPower *self,
const gchar *desc)
{
IndicatorPowerPrivate *priv = self->priv;
if (desc == NULL || strlen(desc) == 0)
return;
g_free (priv->accessible_desc);
priv->accessible_desc = g_strdup (desc);
}
static guint
menu_add_device (GtkMenu *menu,
GVariant *device)
{
UpDeviceKind kind;
UpDeviceState state;
GtkWidget *icon;
GtkWidget *item;
gchar *device_icon = NULL;
gchar **device_icons;
gchar *object_path = NULL;
gdouble percentage;
guint64 time;
const gchar *device_name;
gchar *short_details = NULL;
gchar *details = NULL;
guint n_devices = 0;
if (device == NULL)
return n_devices;
g_variant_get (device,
"((susdut))",
&object_path,
&kind,
&device_icon,
&percentage,
&state,
&time);
g_debug ("%s: got data from object %s", G_STRFUNC, object_path);
n_devices++;
device_icons = g_strsplit (device_icon, " ", -1);
icon = gtk_image_new_from_icon_name (device_icons[3], GTK_ICON_SIZE_SMALL_TOOLBAR);
g_strfreev (device_icons);
device_name = device_kind_to_localised_string (kind);
build_device_time_details (device_name, time, state, percentage, &short_details, &details);
item = gtk_image_menu_item_new ();
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), icon);
gtk_menu_item_set_label (GTK_MENU_ITEM (item), details);
gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
g_signal_connect (G_OBJECT (item), "activate",
G_CALLBACK (show_info_cb), NULL);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
return n_devices;
}
static void
build_menu (IndicatorPower *self)
{
IndicatorPowerPrivate *priv = self->priv;
GtkWidget *item;
GtkWidget *image;
GList *children;
guint n_devices = 0;
if (priv->menu == NULL)
priv->menu = GTK_MENU (gtk_menu_new ());
children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL);
/* devices */
n_devices += menu_add_device (priv->menu, priv->device);
/* only do the separator if we have at least one device */
if (n_devices != 0)
{
item = gtk_separator_menu_item_new ();
gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item);
}
/* options */
item = gtk_check_menu_item_new_with_label (_("Show Time Remaining"));
g_object_set (item, "draw-as-radio", TRUE, NULL);
g_signal_connect (G_OBJECT (item), "toggled",
G_CALLBACK (option_toggled_cb), self);
gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item);
/* separator */
item = gtk_separator_menu_item_new ();
gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item);
/* preferences */
item = gtk_image_menu_item_new_with_mnemonic (_("Power Settings ..."));
image = gtk_image_new_from_icon_name (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_MENU);
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
g_signal_connect (G_OBJECT (item), "activate",
G_CALLBACK (show_preferences_cb), NULL);
gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item);
/* show the menu */
gtk_widget_show_all (GTK_WIDGET (priv->menu));
}
static void
get_primary_device_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
IndicatorPower *self = INDICATOR_POWER (user_data);
IndicatorPowerPrivate *priv = self->priv;
UpDeviceKind kind;
UpDeviceState state;
GError *error = NULL;
gchar *short_details = NULL;
gchar *details = NULL;
gchar **device_icons;
gchar *device_icon = NULL;
gchar *object_path = NULL;
gdouble percentage;
guint64 time;
const gchar *device_name;
gchar *short_timestring = NULL;
gchar *detailed_timestring = NULL;
priv->device = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error);
if (priv->device == NULL)
{
g_printerr ("Error getting primary device: %s\n", error->message);
g_error_free (error);
return;
}
/* set the icon and text */
g_variant_get (priv->device,
"((susdut))",
&object_path,
&kind,
&device_icon,
&percentage,
&state,
&time);
g_debug ("%s: got data from object %s", G_STRFUNC, object_path);
/* set icon */
device_icons = g_strsplit (device_icon, " ", -1);
indicator_image_helper_update (priv->status_image,
device_icons[3]);
g_strfreev (device_icons);
gtk_widget_show (GTK_WIDGET (priv->status_image));
/* get the device name */
device_name = device_kind_to_localised_string (kind);
/* get the description */
build_device_time_details (device_name, time, state, percentage, &short_details, &details);
gtk_label_set_label (GTK_LABEL (priv->label),
short_details);
set_accessible_desc (self, details);
build_menu (self);
g_free (short_details);
g_free (details);
g_free (device_icon);
g_free (short_timestring);
g_free (detailed_timestring);
g_free (object_path);
}
static void
receive_signal (GDBusProxy *proxy,
gchar *sender_name,
gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
IndicatorPower *self = INDICATOR_POWER (user_data);
IndicatorPowerPrivate *priv = self->priv;
if (g_strcmp0 (signal_name, "Changed") == 0)
{
/* get the new state */
g_dbus_proxy_call (priv->proxy,
"GetPrimaryDevice",
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
priv->proxy_cancel,
get_primary_device_cb,
user_data);
}
}
static void
service_proxy_cb (GObject *object,
GAsyncResult *res,
gpointer user_data)
{
IndicatorPower *self = INDICATOR_POWER (user_data);
IndicatorPowerPrivate *priv = self->priv;
GError *error = NULL;
priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
if (priv->proxy_cancel != NULL)
{
g_object_unref (priv->proxy_cancel);
priv->proxy_cancel = NULL;
}
if (error != NULL)
{
g_error ("Error creating proxy: %s", error->message);
g_error_free (error);
return;
}
/* we want to change the primary device changes */
g_signal_connect (priv->proxy,
"g-signal",
G_CALLBACK (receive_signal),
user_data);
/* get the initial state */
g_dbus_proxy_call (priv->proxy,
"GetPrimaryDevice",
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
priv->proxy_cancel,
get_primary_device_cb,
user_data);
}
static void
indicator_power_init (IndicatorPower *self)
{
IndicatorPowerPrivate *priv;
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
INDICATOR_POWER_TYPE,
IndicatorPowerPrivate);
priv = self->priv;
/* Init variables */
priv->menu = NULL;
priv->accessible_desc = NULL;
priv->proxy_cancel = g_cancellable_new();
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.gnome.PowerManager",
"/org/gnome/PowerManager",
"org.gnome.PowerManager",
priv->proxy_cancel,
service_proxy_cb,
self);
}
static void
indicator_power_dispose (GObject *object)
{
G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object);
}
static void
indicator_power_finalize (GObject *object)
{
G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object);
}
/* Grabs the label. Creates it if it doesn't
exist already */
static GtkLabel *
get_label (IndicatorObject *io)
{
IndicatorPower *self = INDICATOR_POWER (io);
IndicatorPowerPrivate *priv = self->priv;
if (priv->label == NULL)
{
/* Create the label if it doesn't exist already */
priv->label = GTK_LABEL (gtk_label_new (""));
}
return priv->label;
}
static GtkImage *
get_image (IndicatorObject *io)
{
IndicatorPower *self = INDICATOR_POWER (io);
IndicatorPowerPrivate *priv = self->priv;
if (priv->status_image == NULL)
{
/* Will create the status icon if it doesn't exist already */
priv->status_image = indicator_image_helper (DEFAULT_ICON);
gtk_widget_show (GTK_WIDGET (priv->status_image));
}
return priv->status_image;
}
static GtkMenu *
get_menu (IndicatorObject *io)
{
IndicatorPower *self = INDICATOR_POWER (io);
IndicatorPowerPrivate *priv = self->priv;
build_menu (self);
return GTK_MENU (priv->menu);
}
static const gchar *
get_accessible_desc (IndicatorObject *io)
{
IndicatorPower *self = INDICATOR_POWER (io);
IndicatorPowerPrivate *priv = self->priv;
return priv->accessible_desc;
}
|