changeset 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
files Makefile README pui-backend.c
diffstat 3 files changed, 62 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun Jun 17 11:05:28 2018 +0200
+++ b/Makefile	Sun Jun 17 17:05:16 2018 +0200
@@ -143,9 +143,9 @@
 			-DSETTINGS_SCHEMA_ID=\"$(APPLICATION_ID)\" \
 			-DI_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE
 $(PACKAGE): XCFLAGS =	$(shell pkg-config --cflags gtk+-3.0 \
-			    appindicator3-0.1 packagekit-glib2)
+			    appindicator3-0.1 packagekit-glib2 upower-glib)
 $(PACKAGE): LDLIBS =	$(shell pkg-config --libs gtk+-3.0 \
-			    appindicator3-0.1 packagekit-glib2)
+			    appindicator3-0.1 packagekit-glib2 upower-glib)
 
 $(PACKAGE)-prefs: XCPPFLAGS = -DPACKAGE=\"$(PACKAGE)\" \
 			-DAPPLICATION_ID=\"$(PREFS_APPLICATION_ID)\" \
--- a/README	Sun Jun 17 11:05:28 2018 +0200
+++ b/README	Sun Jun 17 17:05:16 2018 +0200
@@ -23,6 +23,7 @@
 - GTK+ version 3.18 or later
 - libappindicator 12.10.0 or later
 - PackageKit-glib2 0.8.17 or later
+- upower-glib 0.99.0 or later
 - the xsltproc tool from libxml2
 
 Before building package-update-indicator check the commented macros in the
--- a/pui-backend.c	Sun Jun 17 11:05:28 2018 +0200
+++ b/pui-backend.c	Sun Jun 17 17:05:16 2018 +0200
@@ -27,19 +27,25 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <upower.h>
 #include <utime.h>
 
 #include "pui-common.h"
 #include "pui-backend.h"
 #include "pui-get-updates.h"
 
+#define	LOW_BATTERY_THRESHOLD	10.0
+
 struct _PuiBackend {
 	GObject		parent_instance;
 	PkControl	*pk_control;
 	GCancellable	*cancellable;
+	UpClient	*up_client;
+	UpDevice	*up_device;
 	gint64		last_check;
 	PkNetworkEnum	network_state;
 	gboolean	inhibited;
+	gboolean	is_battery_low;
 	guint		periodic_check_id;
 	guint		refresh_interval;
 	gboolean	use_mobile_connection;
@@ -190,7 +196,8 @@
 
 	inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) ||
 	    (!self->use_mobile_connection &&
-	    (self->network_state == PK_NETWORK_ENUM_MOBILE)));
+	    (self->network_state == PK_NETWORK_ENUM_MOBILE)) ||
+	    self->is_battery_low);
 	if (self->inhibited == inhibited) {
 		return;
 	}
@@ -293,6 +300,14 @@
 		g_clear_object(&self->pk_control);
 	}
 
+	if (self->up_device != NULL) {
+		g_clear_object(&self->up_device);
+	}
+
+	if (self->up_client != NULL) {
+		g_clear_object(&self->up_client);
+	}
+
 	G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
 }
 
@@ -343,7 +358,13 @@
 pui_backend_init(PuiBackend *self)
 {
 	self->pk_control = pk_control_new();
+
 	self->inhibited = TRUE;
+
+	self->up_client = up_client_new();
+	if (self->up_client) {
+		self->up_device = up_client_get_display_device(self->up_client);
+	}
 }
 
 static void
@@ -390,6 +411,26 @@
 }
 
 static void
+on_notify_device_charge_percentage(UpDevice *device, GParamSpec *pspec,
+    gpointer user_data)
+{
+	PuiBackend	*self = user_data;
+	UpDeviceKind	kind;
+	gdouble		percentage;
+
+	g_object_get(device, "kind", &kind, "percentage", &percentage, NULL);
+	if ((kind != UP_DEVICE_KIND_BATTERY) && (kind != UP_DEVICE_KIND_UPS)) {
+		return;
+	}
+	g_debug("charge percentage changed: %.0f%%\n", percentage);
+	if ((self->is_battery_low && (percentage > LOW_BATTERY_THRESHOLD)) ||
+	    (!self->is_battery_low && (percentage < LOW_BATTERY_THRESHOLD))) {
+		self->is_battery_low = !self->is_battery_low;
+		check_inhibit(self);
+	}
+}
+
+static void
 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
     gpointer user_data)
 {
@@ -451,11 +492,28 @@
 {
 	PuiBackend	*self = PUI_BACKEND(initable);
 	GTask		*task = G_TASK(result);
+	UpDeviceKind	kind;
+	gdouble		percentage;
 
 	if (!g_task_propagate_boolean(task, errorp)) {
 		return (FALSE);
 	}
 
+	if (self->up_device != NULL) {
+		/* get the kind of device and charge percentage */
+		g_object_get(self->up_device, "kind", &kind, "percentage",
+		    &percentage, NULL);
+		if ((kind == UP_DEVICE_KIND_BATTERY) ||
+		    (kind == UP_DEVICE_KIND_UPS)) {
+			self->is_battery_low =
+			    (percentage < LOW_BATTERY_THRESHOLD);
+		}
+
+		/* get notification if the charge percentage changes */
+		g_signal_connect(self->up_device, "notify::percentage",
+		    G_CALLBACK(on_notify_device_charge_percentage), self);
+	}
+
 	/* get notification when the network state changes */
 	g_signal_connect(self->pk_control, "notify::network-state",
 	    G_CALLBACK(on_notify_network_state), self);