diff pui-backend.c @ 4:3d72ca76538d

Add setting to control whether to use a mobile connection
author Guido Berhoerster <guido+pui@berhoerster.name>
date Sun, 17 Jun 2018 11:05:28 +0200
parents 6884bb8130ca
children a4020e99e550
line wrap: on
line diff
--- a/pui-backend.c	Wed Jun 13 23:25:09 2018 +0200
+++ b/pui-backend.c	Sun Jun 17 11:05:28 2018 +0200
@@ -39,8 +39,10 @@
 	GCancellable	*cancellable;
 	gint64		last_check;
 	PkNetworkEnum	network_state;
+	gboolean	inhibited;
 	guint		periodic_check_id;
 	guint		refresh_interval;
+	gboolean	use_mobile_connection;
 	guint		important_updates;
 	guint		normal_updates;
 };
@@ -62,6 +64,7 @@
     PROP_IMPORTANT_UPDATES,
     PROP_NORMAL_UPDATES,
     PROP_REFRESH_INTERVAL,
+    PROP_USE_MOBILE_CONNECTION,
     PROP_LAST
 };
 
@@ -147,8 +150,10 @@
 	self->last_check = g_get_monotonic_time();
 
 out:
+	g_clear_object(&self->cancellable);
+
 	/* reschedule periodic check */
-	if (self->network_state != PK_NETWORK_ENUM_OFFLINE) {
+	if (!self->inhibited) {
 		self->periodic_check_id =
 		    g_timeout_add_seconds(PUI_CHECK_UPDATES_INTERVAL,
 		    periodic_check, self);
@@ -166,6 +171,7 @@
 
 	g_debug("running periodic check");
 
+	self->cancellable = g_cancellable_new();
 	pui_get_updates_async(self->pk_control, self->refresh_interval,
 	    self->cancellable, on_get_updates_finished, self);
 
@@ -176,6 +182,50 @@
 }
 
 static void
+check_inhibit(PuiBackend *self)
+{
+	gboolean	inhibited;
+	guint		elapsed_time;
+	guint		remaining_time;
+
+	inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) ||
+	    (!self->use_mobile_connection &&
+	    (self->network_state == PK_NETWORK_ENUM_MOBILE)));
+	if (self->inhibited == inhibited) {
+		return;
+	}
+
+	self->inhibited = inhibited;
+	if (inhibited) {
+		/* cancel periodic checks */
+		if (self->periodic_check_id != 0) {
+			g_source_remove(self->periodic_check_id);
+		}
+
+		/* cancel running operation */
+		if ((self->cancellable != NULL) &&
+		    !g_cancellable_is_cancelled(self->cancellable)) {
+			g_cancellable_cancel(self->cancellable);
+			g_clear_object(&self->cancellable);
+		}
+	} else {
+		/* schedule periodic checks when no longer inhibited */
+		elapsed_time = (g_get_monotonic_time() - self->last_check) /
+		    G_USEC_PER_SEC;
+		/*
+		 * if more time that the check interval has passed since the
+		 * last check, schedule a check after a short delay, otherwise
+		 * wait until the interval has passed
+		 */
+		remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
+		    PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
+		    PUI_STARTUP_DELAY;
+		self->periodic_check_id = g_timeout_add_seconds(remaining_time,
+		    periodic_check, self);
+	}
+}
+
+static void
 pui_backend_set_property(GObject *object, guint property_id,
     const GValue *value, GParamSpec *pspec)
 {
@@ -187,6 +237,12 @@
 		g_debug("property \"refresh-interval\" set to %u",
 		    self->refresh_interval);
 		break;
+	case PROP_USE_MOBILE_CONNECTION:
+		self->use_mobile_connection = g_value_get_boolean(value);
+		g_debug("property \"use-mobile-connection\" set to %s",
+		    self->use_mobile_connection ? "true" : "false");
+		check_inhibit(self);
+		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
 		break;
@@ -209,6 +265,9 @@
 	case PROP_REFRESH_INTERVAL:
 		g_value_set_uint(value, self->refresh_interval);
 		break;
+	case PROP_USE_MOBILE_CONNECTION:
+		g_value_set_boolean(value, self->use_mobile_connection);
+		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
 		break;
@@ -261,6 +320,12 @@
 	    "Interval in seconds for refreshing the package cache", 0,
 	    G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
 
+	properties[PROP_USE_MOBILE_CONNECTION] =
+	    g_param_spec_boolean("use-mobile-connection",
+	    "Whether to use a mobile connection", "Whether to use a mobile "
+	    "connection for refreshing the package cache", FALSE,
+	    G_PARAM_READWRITE);
+
 	g_object_class_install_properties(object_class, PROP_LAST, properties);
 
 	signals[STATE_CHANGED] = g_signal_new("state-changed",
@@ -277,8 +342,8 @@
 static void
 pui_backend_init(PuiBackend *self)
 {
-	self->cancellable = g_cancellable_new();
 	self->pk_control = pk_control_new();
+	self->inhibited = TRUE;
 }
 
 static void
@@ -329,36 +394,11 @@
     gpointer user_data)
 {
 	PuiBackend	*self = user_data;
-	PkNetworkEnum	network_state;
-	guint		elapsed_time;
-	guint		remaining_time;
 
-	g_object_get(pk_control, "network-state", &network_state, NULL);
+	g_object_get(pk_control, "network-state", &self->network_state, NULL);
 	g_debug("network state changed: %s",
-	    pk_network_enum_to_string(network_state));
-	if ((self->network_state == PK_NETWORK_ENUM_OFFLINE) &&
-	    (network_state != PK_NETWORK_ENUM_OFFLINE)) {
-		/* schedule periodic checks when coming back online */
-		elapsed_time = (g_get_monotonic_time() - self->last_check) /
-		    G_USEC_PER_SEC;
-		/*
-		 * if more time that the check interval has passed since the
-		 * last check, schedule a check after a short delay, otherwise
-		 * wait until the interval has passed
-		 */
-		remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
-		    PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
-		    PUI_STARTUP_DELAY;
-		self->periodic_check_id = g_timeout_add_seconds(remaining_time,
-		    periodic_check, self);
-	} else if ((self->network_state != PK_NETWORK_ENUM_OFFLINE) &&
-	    (network_state == PK_NETWORK_ENUM_OFFLINE)) {
-		/* cancel periodic checks while offline */
-		if (self->periodic_check_id != 0) {
-			g_source_remove(self->periodic_check_id);
-		}
-	}
-	self->network_state = network_state;
+	    pk_network_enum_to_string(self->network_state));
+	check_inhibit(self);
 }
 
 static void
@@ -370,7 +410,7 @@
 	 * schedule a check after a short delay so that a rapid succession of
 	 * signals is coalesced
 	 */
-	if (self->network_state != PK_NETWORK_ENUM_OFFLINE) {
+	if (!self->inhibited) {
 		if (self->periodic_check_id != 0) {
 			g_source_remove(self->periodic_check_id);
 		}
@@ -425,9 +465,8 @@
 	/* get notifications when an application restart is required */
 	g_signal_connect(self->pk_control, "restart-schedule",
 	    G_CALLBACK(on_restart_schedule), self);
-	/* schedule first check after a small delay */
-	self->periodic_check_id = g_timeout_add_seconds(PUI_STARTUP_DELAY,
-	    periodic_check, self);
+
+	check_inhibit(self);
 
 	return (TRUE);
 }