comparison pui-backend.c @ 5:a4020e99e550

Do not check for updates if the battery is low
author Guido Berhoerster <guido+pui@berhoerster.name>
date Sun, 17 Jun 2018 17:05:16 +0200
parents 3d72ca76538d
children 2477a6151087
comparison
equal deleted inserted replaced
4:3d72ca76538d 5:a4020e99e550
25 #include <fcntl.h> 25 #include <fcntl.h>
26 #include <packagekit-glib2/packagekit.h> 26 #include <packagekit-glib2/packagekit.h>
27 #include <string.h> 27 #include <string.h>
28 #include <sys/stat.h> 28 #include <sys/stat.h>
29 #include <sys/types.h> 29 #include <sys/types.h>
30 #include <upower.h>
30 #include <utime.h> 31 #include <utime.h>
31 32
32 #include "pui-common.h" 33 #include "pui-common.h"
33 #include "pui-backend.h" 34 #include "pui-backend.h"
34 #include "pui-get-updates.h" 35 #include "pui-get-updates.h"
36
37 #define LOW_BATTERY_THRESHOLD 10.0
35 38
36 struct _PuiBackend { 39 struct _PuiBackend {
37 GObject parent_instance; 40 GObject parent_instance;
38 PkControl *pk_control; 41 PkControl *pk_control;
39 GCancellable *cancellable; 42 GCancellable *cancellable;
43 UpClient *up_client;
44 UpDevice *up_device;
40 gint64 last_check; 45 gint64 last_check;
41 PkNetworkEnum network_state; 46 PkNetworkEnum network_state;
42 gboolean inhibited; 47 gboolean inhibited;
48 gboolean is_battery_low;
43 guint periodic_check_id; 49 guint periodic_check_id;
44 guint refresh_interval; 50 guint refresh_interval;
45 gboolean use_mobile_connection; 51 gboolean use_mobile_connection;
46 guint important_updates; 52 guint important_updates;
47 guint normal_updates; 53 guint normal_updates;
188 guint elapsed_time; 194 guint elapsed_time;
189 guint remaining_time; 195 guint remaining_time;
190 196
191 inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) || 197 inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) ||
192 (!self->use_mobile_connection && 198 (!self->use_mobile_connection &&
193 (self->network_state == PK_NETWORK_ENUM_MOBILE))); 199 (self->network_state == PK_NETWORK_ENUM_MOBILE)) ||
200 self->is_battery_low);
194 if (self->inhibited == inhibited) { 201 if (self->inhibited == inhibited) {
195 return; 202 return;
196 } 203 }
197 204
198 self->inhibited = inhibited; 205 self->inhibited = inhibited;
291 298
292 if (self->pk_control != NULL) { 299 if (self->pk_control != NULL) {
293 g_clear_object(&self->pk_control); 300 g_clear_object(&self->pk_control);
294 } 301 }
295 302
303 if (self->up_device != NULL) {
304 g_clear_object(&self->up_device);
305 }
306
307 if (self->up_client != NULL) {
308 g_clear_object(&self->up_client);
309 }
310
296 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object); 311 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
297 } 312 }
298 313
299 static void 314 static void
300 pui_backend_class_init(PuiBackendClass *klass) 315 pui_backend_class_init(PuiBackendClass *klass)
341 356
342 static void 357 static void
343 pui_backend_init(PuiBackend *self) 358 pui_backend_init(PuiBackend *self)
344 { 359 {
345 self->pk_control = pk_control_new(); 360 self->pk_control = pk_control_new();
361
346 self->inhibited = TRUE; 362 self->inhibited = TRUE;
363
364 self->up_client = up_client_new();
365 if (self->up_client) {
366 self->up_device = up_client_get_display_device(self->up_client);
367 }
347 } 368 }
348 369
349 static void 370 static void
350 on_get_properties_finished(GObject *object, GAsyncResult *result, 371 on_get_properties_finished(GObject *object, GAsyncResult *result,
351 gpointer user_data) 372 gpointer user_data)
388 g_free(backend_name); 409 g_free(backend_name);
389 g_object_unref(task); 410 g_object_unref(task);
390 } 411 }
391 412
392 static void 413 static void
414 on_notify_device_charge_percentage(UpDevice *device, GParamSpec *pspec,
415 gpointer user_data)
416 {
417 PuiBackend *self = user_data;
418 UpDeviceKind kind;
419 gdouble percentage;
420
421 g_object_get(device, "kind", &kind, "percentage", &percentage, NULL);
422 if ((kind != UP_DEVICE_KIND_BATTERY) && (kind != UP_DEVICE_KIND_UPS)) {
423 return;
424 }
425 g_debug("charge percentage changed: %.0f%%\n", percentage);
426 if ((self->is_battery_low && (percentage > LOW_BATTERY_THRESHOLD)) ||
427 (!self->is_battery_low && (percentage < LOW_BATTERY_THRESHOLD))) {
428 self->is_battery_low = !self->is_battery_low;
429 check_inhibit(self);
430 }
431 }
432
433 static void
393 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec, 434 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
394 gpointer user_data) 435 gpointer user_data)
395 { 436 {
396 PuiBackend *self = user_data; 437 PuiBackend *self = user_data;
397 438
449 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result, 490 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result,
450 GError **errorp) 491 GError **errorp)
451 { 492 {
452 PuiBackend *self = PUI_BACKEND(initable); 493 PuiBackend *self = PUI_BACKEND(initable);
453 GTask *task = G_TASK(result); 494 GTask *task = G_TASK(result);
495 UpDeviceKind kind;
496 gdouble percentage;
454 497
455 if (!g_task_propagate_boolean(task, errorp)) { 498 if (!g_task_propagate_boolean(task, errorp)) {
456 return (FALSE); 499 return (FALSE);
500 }
501
502 if (self->up_device != NULL) {
503 /* get the kind of device and charge percentage */
504 g_object_get(self->up_device, "kind", &kind, "percentage",
505 &percentage, NULL);
506 if ((kind == UP_DEVICE_KIND_BATTERY) ||
507 (kind == UP_DEVICE_KIND_UPS)) {
508 self->is_battery_low =
509 (percentage < LOW_BATTERY_THRESHOLD);
510 }
511
512 /* get notification if the charge percentage changes */
513 g_signal_connect(self->up_device, "notify::percentage",
514 G_CALLBACK(on_notify_device_charge_percentage), self);
457 } 515 }
458 516
459 /* get notification when the network state changes */ 517 /* get notification when the network state changes */
460 g_signal_connect(self->pk_control, "notify::network-state", 518 g_signal_connect(self->pk_control, "notify::network-state",
461 G_CALLBACK(on_notify_network_state), self); 519 G_CALLBACK(on_notify_network_state), self);