gf-power-applet.c 9.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 2015 Alberts Muktupāvels
 *
 * 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 3 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/>.
 */

#include "config.h"

20
21
22
23
24
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <libupower-glib/upower.h>
#include <math.h>

25
#include "gf-power-applet.h"
26
27
28
29
#include "gf-upower-device.h"

#define UPOWER_DBUS_NAME "org.freedesktop.UPower"
#define UPOWER_DEVICE_DBUS_PATH "/org/freedesktop/UPower/devices/DisplayDevice"
30
31
32

struct _GfPowerApplet
{
33
34
35
36
  GObject         parent;

  GtkStatusIcon  *status_icon;
  GfUPowerDevice *device;
37
38
39
40
};

G_DEFINE_TYPE (GfPowerApplet, gf_power_applet, G_TYPE_OBJECT)

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
static void
statistics_cb (GtkMenuItem *item,
               gpointer     user_data)
{
  GAppInfo *app_info;
  GError *error;

  error = NULL;
  app_info = g_app_info_create_from_commandline ("gnome-power-statistics",
                                                 "Power Statistics",
                                                 G_APP_INFO_CREATE_NONE,
                                                 &error);

  if (error != NULL)
    {
      g_warning ("Failed to crete GAppInfo from commandline - %s",
                 error->message);
      g_error_free (error);
    }
  else
    {
      g_app_info_launch (app_info, NULL, NULL, &error);

      if (error != NULL)
        {
          g_warning ("Failed to start Power Statistics - %s", error->message);
          g_error_free (error);
        }
    }

  g_clear_object (&app_info);
}

static void
settings_cb (GtkMenuItem *item,
             gpointer     user_data)
{
  GAppInfo *app_info;
  GError *error;

  error = NULL;
  app_info = g_app_info_create_from_commandline ("gnome-control-center power",
                                                 "Power Settings",
                                                 G_APP_INFO_CREATE_NONE,
                                                 &error);

  if (error != NULL)
    {
      g_warning ("Failed to crete GAppInfo from commandline - %s",
                 error->message);
      g_error_free (error);
    }
  else
    {
      g_app_info_launch (app_info, NULL, NULL, &error);

      if (error != NULL)
        {
          g_warning ("Failed to start Power Settings - %s", error->message);
          g_error_free (error);
        }
    }

  g_clear_object (&app_info);
}

static void
popup_menu_cb (GtkStatusIcon *status_icon,
               guint          button,
               guint          activate_time,
               gpointer       user_data)
{
  const gchar *title;
  gchar *tooltip_text;
  gchar *label;
  GtkWidget *menu;
  GtkWidget *item;

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS

  title = gtk_status_icon_get_title (status_icon);
  tooltip_text = gtk_status_icon_get_tooltip_text (status_icon);
  label = g_strdup_printf ("%s: %s", title, tooltip_text);
  g_free (tooltip_text);

  menu = gtk_menu_new ();

  item = gtk_menu_item_new_with_label (label);
  g_free (label);
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
  g_signal_connect (item, "activate", G_CALLBACK (statistics_cb), NULL);

  item = gtk_menu_item_new_with_label (_("Power Settings"));
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
  g_signal_connect (item, "activate", G_CALLBACK (settings_cb), NULL);

  gtk_widget_show_all (menu);
  gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
                  gtk_status_icon_position_menu, status_icon,
                  button, activate_time);

  G_GNUC_END_IGNORE_DEPRECATIONS
}

static gchar *
get_icon_name (GfPowerApplet *applet)
{
  gchar *icon_name;
  gchar *symbolic;

  icon_name = g_strdup (gf_upower_device_get_icon_name (applet->device));

  if (icon_name == NULL || icon_name[0] == '\0')
    {
      g_free (icon_name);
      return g_strdup ("battery");
    }

  symbolic = g_strrstr (icon_name, "-symbolic");

  if (symbolic != NULL)
    g_strlcpy (symbolic, "", sizeof (symbolic));

  return icon_name;
}

static gchar *
get_tooltip_text (GfPowerApplet *applet)
{
  UpDeviceState state;
  gint64 seconds;
  gdouble time;
  gdouble minutes;
  gdouble hours;
  gdouble percentage;

  state = gf_upower_device_get_state (applet->device);

  if (state == UP_DEVICE_STATE_FULLY_CHARGED)
    return g_strdup (_("Fully Charged"));
  else if (state == UP_DEVICE_STATE_EMPTY)
    return g_strdup (_("Empty"));
  else if (state == UP_DEVICE_STATE_CHARGING)
    seconds = gf_upower_device_get_time_to_full (applet->device);
  else if (state == UP_DEVICE_STATE_DISCHARGING)
    seconds = gf_upower_device_get_time_to_empty (applet->device);
  else
    return g_strdup (_("Estimating..."));

  time = round (seconds / 60);

  if (time == 0)
    return g_strdup (_("Estimating..."));

  minutes = fmod (time, 60);
  hours = floor (time / 60);
  percentage = gf_upower_device_get_percentage (applet->device);

  if (state == UP_DEVICE_STATE_DISCHARGING)
    {
      /* Translators: this is <hours>:<minutes> Remaining (<percentage>) */
      return g_strdup_printf (_("%.0f:%02.0f Remaining (%.0f%%)"),
                              hours, minutes, percentage);
    }

  if (state == UP_DEVICE_STATE_CHARGING)
    {
      /* Translators: this is <hours>:<minutes> Until Full (<percentage>) */
Piotr Drąg's avatar
Piotr Drąg committed
209
      return g_strdup_printf (_("%.0f:%02.0f Until Full (%.0f%%)"),
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
                              hours, minutes, percentage);
    }

  return NULL;
}

static const gchar *
get_title (GfPowerApplet *applet)
{
  UpDeviceKind type;

  type = gf_upower_device_get_type_ (applet->device);

  if (type == UP_DEVICE_KIND_UPS)
    return _("UPS");

  return _("Battery");
}

static void
gf_power_applet_sync (GfPowerApplet *applet)
{
  gchar *icon_name;
  gchar *tooltip_text;
  const gchar *title;
  gboolean is_present;

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS

  icon_name = get_icon_name (applet);
  gtk_status_icon_set_from_icon_name (applet->status_icon, icon_name);
  g_free (icon_name);

  tooltip_text = get_tooltip_text (applet);
  gtk_status_icon_set_tooltip_text (applet->status_icon, tooltip_text);
  g_free (tooltip_text);

  title = get_title (applet);
  gtk_status_icon_set_title (applet->status_icon, title);

  is_present = gf_upower_device_get_is_present (applet->device);
  gtk_status_icon_set_visible (applet->status_icon, is_present);

  G_GNUC_END_IGNORE_DEPRECATIONS
}

static void
properties_changed_cb (GDBusProxy *proxy,
                       GVariant   *changed_properties,
                       GStrv       invalidated_properties,
                       gpointer    user_data)
{
  GfPowerApplet *applet;

  applet = GF_POWER_APPLET (user_data);

  gf_power_applet_sync (applet);
}

static void
device_proxy_ready_cb (GObject      *source_object,
                       GAsyncResult *res,
                       gpointer      user_data)
{
  GfPowerApplet *applet;
  GError *error;

  applet = GF_POWER_APPLET (user_data);

  error = NULL;
  applet->device = gf_upower_device_proxy_new_for_bus_finish (res, &error);

  if (error != NULL)
    {
      g_warning ("Failed to get UPower device proxy - %s", error->message);
      g_error_free (error);

      return;
    }

  g_signal_connect (applet->device, "g-properties-changed",
                    G_CALLBACK (properties_changed_cb), applet);

  gf_power_applet_sync (applet);
}

static void
gf_power_applet_dispose (GObject *object)
{
  GfPowerApplet *applet;

  applet = GF_POWER_APPLET (object);

  g_clear_object (&applet->status_icon);
  g_clear_object (&applet->device);

  G_OBJECT_CLASS (gf_power_applet_parent_class)->dispose (object);
}

309
310
311
static void
gf_power_applet_class_init (GfPowerAppletClass *applet_class)
{
312
313
314
315
316
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (applet_class);

  object_class->dispose = gf_power_applet_dispose;
317
318
319
320
321
}

static void
gf_power_applet_init (GfPowerApplet *applet)
{
322
323
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS

324
  applet->status_icon = gtk_status_icon_new_from_icon_name ("battery");
325

326
  gtk_status_icon_set_visible (applet->status_icon, FALSE);
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
  gtk_status_icon_set_title (applet->status_icon, _("Power status"));
  gtk_status_icon_set_tooltip_text (applet->status_icon, _("Power"));

  G_GNUC_END_IGNORE_DEPRECATIONS

  g_signal_connect (applet->status_icon, "popup-menu",
                    G_CALLBACK (popup_menu_cb), applet);

  gf_upower_device_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                                      G_DBUS_PROXY_FLAGS_NONE,
                                      UPOWER_DBUS_NAME,
                                      UPOWER_DEVICE_DBUS_PATH,
                                      NULL,
                                      device_proxy_ready_cb,
                                      applet);
342
343
344
345
346
347
348
}

GfPowerApplet *
gf_power_applet_new (void)
{
  return g_object_new (GF_TYPE_POWER_APPLET, NULL);
}