changeset 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 2fa34d6272c6
children a4020e99e550
files org.guido-berhoerster.code.package-update-indicator.gschema.xml package-update-indicator-prefs.1.xml pui-application.c pui-backend.c pui-prefs-application.c pui-prefs-window.ui
diffstat 6 files changed, 109 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/org.guido-berhoerster.code.package-update-indicator.gschema.xml	Wed Jun 13 23:25:09 2018 +0200
+++ b/org.guido-berhoerster.code.package-update-indicator.gschema.xml	Sun Jun 17 11:05:28 2018 +0200
@@ -13,5 +13,11 @@
       <description>The interval in seconds for refreshing
       metadata.</description>
     </key>
+    <key name="use-mobile-connection" type="b">
+      <default>false</default>
+      <summary>Whether to use a mobile connection</summary>
+      <description>If enabled, use a mobile connection refreshing the package
+      cache.</description>
+    </key>
   </schema>
 </schemalist>
--- a/package-update-indicator-prefs.1.xml	Wed Jun 13 23:25:09 2018 +0200
+++ b/package-update-indicator-prefs.1.xml	Sun Jun 17 11:05:28 2018 +0200
@@ -33,7 +33,7 @@
       <email>guido+pui@berhoerster.name</email>
       <personblurb/>
     </author>
-    <date>5 June, 2018</date>
+    <date>17 June, 2018</date>
   </info>
   <refmeta>
     <refentrytitle>package-update-indicator-prefs</refentrytitle>
@@ -72,6 +72,13 @@
           <para>The command for installing updates.</para>
         </listitem>
       </varlistentry>
+      <varlistentry>
+        <term>Use mobile connection</term>
+        <listitem>
+          <para>Whether to use a mobile connection for refreshing the package
+            cache.</para>
+        </listitem>
+      </varlistentry>
     </variablelist>
   </refsect1>
   <refsect1>
--- a/pui-application.c	Wed Jun 13 23:25:09 2018 +0200
+++ b/pui-application.c	Sun Jun 17 11:05:28 2018 +0200
@@ -408,6 +408,8 @@
 
 	g_settings_bind(self->settings, "refresh-interval", self->backend,
 	    "refresh-interval", G_SETTINGS_BIND_GET);
+	g_settings_bind(self->settings, "use-mobile-connection", self->backend,
+	    "use-mobile-connection", G_SETTINGS_BIND_GET);
 
 	transition_state(self);
 
--- 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);
 }
--- a/pui-prefs-application.c	Wed Jun 13 23:25:09 2018 +0200
+++ b/pui-prefs-application.c	Sun Jun 17 11:05:28 2018 +0200
@@ -127,6 +127,7 @@
 	GtkTreeModel	*tree_model;
 	GtkWidget	*update_command_entry;
 	GtkWidget	*refresh_interval_combo_box;
+	GtkWidget	*use_mobile_check_button;
 
 	application_class->startup(application);
 
@@ -146,6 +147,8 @@
 	    "refresh-interval"));
 	tree_model =
 	    gtk_combo_box_get_model(GTK_COMBO_BOX(refresh_interval_combo_box));
+	use_mobile_check_button = GTK_WIDGET(gtk_builder_get_object(builder,
+	    "use-mobile-connection"));
 
 	/* bind settings to widgets */
 	self->settings = pui_settings_new();
@@ -155,6 +158,8 @@
 	    refresh_interval_combo_box, "active", G_SETTINGS_BIND_DEFAULT,
 	    map_refresh_interval_to_index, map_index_to_refresh_interval,
 	    tree_model, NULL);
+	g_settings_bind(self->settings, "use-mobile-connection",
+	    use_mobile_check_button, "active", G_SETTINGS_BIND_DEFAULT);
 
 	/* show window */
 	gtk_widget_show(window);
--- a/pui-prefs-window.ui	Wed Jun 13 23:25:09 2018 +0200
+++ b/pui-prefs-window.ui	Sun Jun 17 11:05:28 2018 +0200
@@ -125,6 +125,21 @@
                 <property name="top_attach">0</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkCheckButton" id="use-mobile-connection">
+                <property name="label" translatable="yes">Use _mobile connection</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="use_underline">True</property>
+                <property name="draw_indicator">True</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">2</property>
+                <property name="width">2</property>
+              </packing>
+            </child>
           </object>
           <packing>
             <property name="expand">True</property>