projects/package-update-indicator
changeset 5:a4020e99e550
Do not check for updates if the battery is low
author | Guido Berhoerster <guido+pui@berhoerster.name> |
---|---|
date | Sun Jun 17 17:05:16 2018 +0200 (2018-06-17) |
parents | 3d72ca76538d |
children | 2477a6151087 |
files | Makefile README pui-backend.c |
line diff
1.1 --- a/Makefile Sun Jun 17 11:05:28 2018 +0200 1.2 +++ b/Makefile Sun Jun 17 17:05:16 2018 +0200 1.3 @@ -143,9 +143,9 @@ 1.4 -DSETTINGS_SCHEMA_ID=\"$(APPLICATION_ID)\" \ 1.5 -DI_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE 1.6 $(PACKAGE): XCFLAGS = $(shell pkg-config --cflags gtk+-3.0 \ 1.7 - appindicator3-0.1 packagekit-glib2) 1.8 + appindicator3-0.1 packagekit-glib2 upower-glib) 1.9 $(PACKAGE): LDLIBS = $(shell pkg-config --libs gtk+-3.0 \ 1.10 - appindicator3-0.1 packagekit-glib2) 1.11 + appindicator3-0.1 packagekit-glib2 upower-glib) 1.12 1.13 $(PACKAGE)-prefs: XCPPFLAGS = -DPACKAGE=\"$(PACKAGE)\" \ 1.14 -DAPPLICATION_ID=\"$(PREFS_APPLICATION_ID)\" \
2.1 --- a/README Sun Jun 17 11:05:28 2018 +0200 2.2 +++ b/README Sun Jun 17 17:05:16 2018 +0200 2.3 @@ -23,6 +23,7 @@ 2.4 - GTK+ version 3.18 or later 2.5 - libappindicator 12.10.0 or later 2.6 - PackageKit-glib2 0.8.17 or later 2.7 +- upower-glib 0.99.0 or later 2.8 - the xsltproc tool from libxml2 2.9 2.10 Before building package-update-indicator check the commented macros in the
3.1 --- a/pui-backend.c Sun Jun 17 11:05:28 2018 +0200 3.2 +++ b/pui-backend.c Sun Jun 17 17:05:16 2018 +0200 3.3 @@ -27,19 +27,25 @@ 3.4 #include <string.h> 3.5 #include <sys/stat.h> 3.6 #include <sys/types.h> 3.7 +#include <upower.h> 3.8 #include <utime.h> 3.9 3.10 #include "pui-common.h" 3.11 #include "pui-backend.h" 3.12 #include "pui-get-updates.h" 3.13 3.14 +#define LOW_BATTERY_THRESHOLD 10.0 3.15 + 3.16 struct _PuiBackend { 3.17 GObject parent_instance; 3.18 PkControl *pk_control; 3.19 GCancellable *cancellable; 3.20 + UpClient *up_client; 3.21 + UpDevice *up_device; 3.22 gint64 last_check; 3.23 PkNetworkEnum network_state; 3.24 gboolean inhibited; 3.25 + gboolean is_battery_low; 3.26 guint periodic_check_id; 3.27 guint refresh_interval; 3.28 gboolean use_mobile_connection; 3.29 @@ -190,7 +196,8 @@ 3.30 3.31 inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) || 3.32 (!self->use_mobile_connection && 3.33 - (self->network_state == PK_NETWORK_ENUM_MOBILE))); 3.34 + (self->network_state == PK_NETWORK_ENUM_MOBILE)) || 3.35 + self->is_battery_low); 3.36 if (self->inhibited == inhibited) { 3.37 return; 3.38 } 3.39 @@ -293,6 +300,14 @@ 3.40 g_clear_object(&self->pk_control); 3.41 } 3.42 3.43 + if (self->up_device != NULL) { 3.44 + g_clear_object(&self->up_device); 3.45 + } 3.46 + 3.47 + if (self->up_client != NULL) { 3.48 + g_clear_object(&self->up_client); 3.49 + } 3.50 + 3.51 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object); 3.52 } 3.53 3.54 @@ -343,7 +358,13 @@ 3.55 pui_backend_init(PuiBackend *self) 3.56 { 3.57 self->pk_control = pk_control_new(); 3.58 + 3.59 self->inhibited = TRUE; 3.60 + 3.61 + self->up_client = up_client_new(); 3.62 + if (self->up_client) { 3.63 + self->up_device = up_client_get_display_device(self->up_client); 3.64 + } 3.65 } 3.66 3.67 static void 3.68 @@ -390,6 +411,26 @@ 3.69 } 3.70 3.71 static void 3.72 +on_notify_device_charge_percentage(UpDevice *device, GParamSpec *pspec, 3.73 + gpointer user_data) 3.74 +{ 3.75 + PuiBackend *self = user_data; 3.76 + UpDeviceKind kind; 3.77 + gdouble percentage; 3.78 + 3.79 + g_object_get(device, "kind", &kind, "percentage", &percentage, NULL); 3.80 + if ((kind != UP_DEVICE_KIND_BATTERY) && (kind != UP_DEVICE_KIND_UPS)) { 3.81 + return; 3.82 + } 3.83 + g_debug("charge percentage changed: %.0f%%\n", percentage); 3.84 + if ((self->is_battery_low && (percentage > LOW_BATTERY_THRESHOLD)) || 3.85 + (!self->is_battery_low && (percentage < LOW_BATTERY_THRESHOLD))) { 3.86 + self->is_battery_low = !self->is_battery_low; 3.87 + check_inhibit(self); 3.88 + } 3.89 +} 3.90 + 3.91 +static void 3.92 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec, 3.93 gpointer user_data) 3.94 { 3.95 @@ -451,11 +492,28 @@ 3.96 { 3.97 PuiBackend *self = PUI_BACKEND(initable); 3.98 GTask *task = G_TASK(result); 3.99 + UpDeviceKind kind; 3.100 + gdouble percentage; 3.101 3.102 if (!g_task_propagate_boolean(task, errorp)) { 3.103 return (FALSE); 3.104 } 3.105 3.106 + if (self->up_device != NULL) { 3.107 + /* get the kind of device and charge percentage */ 3.108 + g_object_get(self->up_device, "kind", &kind, "percentage", 3.109 + &percentage, NULL); 3.110 + if ((kind == UP_DEVICE_KIND_BATTERY) || 3.111 + (kind == UP_DEVICE_KIND_UPS)) { 3.112 + self->is_battery_low = 3.113 + (percentage < LOW_BATTERY_THRESHOLD); 3.114 + } 3.115 + 3.116 + /* get notification if the charge percentage changes */ 3.117 + g_signal_connect(self->up_device, "notify::percentage", 3.118 + G_CALLBACK(on_notify_device_charge_percentage), self); 3.119 + } 3.120 + 3.121 /* get notification when the network state changes */ 3.122 g_signal_connect(self->pk_control, "notify::network-state", 3.123 G_CALLBACK(on_notify_network_state), self);