projects/package-update-indicator

changeset 4:3d72ca76538d

Add setting to control whether to use a mobile connection
author Guido Berhoerster <guido+pui@berhoerster.name>
date Sun Jun 17 11:05:28 2018 +0200 (24 months ago)
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
line diff
     1.1 --- a/org.guido-berhoerster.code.package-update-indicator.gschema.xml	Wed Jun 13 23:25:09 2018 +0200
     1.2 +++ b/org.guido-berhoerster.code.package-update-indicator.gschema.xml	Sun Jun 17 11:05:28 2018 +0200
     1.3 @@ -13,5 +13,11 @@
     1.4        <description>The interval in seconds for refreshing
     1.5        metadata.</description>
     1.6      </key>
     1.7 +    <key name="use-mobile-connection" type="b">
     1.8 +      <default>false</default>
     1.9 +      <summary>Whether to use a mobile connection</summary>
    1.10 +      <description>If enabled, use a mobile connection refreshing the package
    1.11 +      cache.</description>
    1.12 +    </key>
    1.13    </schema>
    1.14  </schemalist>
     2.1 --- a/package-update-indicator-prefs.1.xml	Wed Jun 13 23:25:09 2018 +0200
     2.2 +++ b/package-update-indicator-prefs.1.xml	Sun Jun 17 11:05:28 2018 +0200
     2.3 @@ -33,7 +33,7 @@
     2.4        <email>guido+pui@berhoerster.name</email>
     2.5        <personblurb/>
     2.6      </author>
     2.7 -    <date>5 June, 2018</date>
     2.8 +    <date>17 June, 2018</date>
     2.9    </info>
    2.10    <refmeta>
    2.11      <refentrytitle>package-update-indicator-prefs</refentrytitle>
    2.12 @@ -72,6 +72,13 @@
    2.13            <para>The command for installing updates.</para>
    2.14          </listitem>
    2.15        </varlistentry>
    2.16 +      <varlistentry>
    2.17 +        <term>Use mobile connection</term>
    2.18 +        <listitem>
    2.19 +          <para>Whether to use a mobile connection for refreshing the package
    2.20 +            cache.</para>
    2.21 +        </listitem>
    2.22 +      </varlistentry>
    2.23      </variablelist>
    2.24    </refsect1>
    2.25    <refsect1>
     3.1 --- a/pui-application.c	Wed Jun 13 23:25:09 2018 +0200
     3.2 +++ b/pui-application.c	Sun Jun 17 11:05:28 2018 +0200
     3.3 @@ -408,6 +408,8 @@
     3.4  
     3.5  	g_settings_bind(self->settings, "refresh-interval", self->backend,
     3.6  	    "refresh-interval", G_SETTINGS_BIND_GET);
     3.7 +	g_settings_bind(self->settings, "use-mobile-connection", self->backend,
     3.8 +	    "use-mobile-connection", G_SETTINGS_BIND_GET);
     3.9  
    3.10  	transition_state(self);
    3.11  
     4.1 --- a/pui-backend.c	Wed Jun 13 23:25:09 2018 +0200
     4.2 +++ b/pui-backend.c	Sun Jun 17 11:05:28 2018 +0200
     4.3 @@ -39,8 +39,10 @@
     4.4  	GCancellable	*cancellable;
     4.5  	gint64		last_check;
     4.6  	PkNetworkEnum	network_state;
     4.7 +	gboolean	inhibited;
     4.8  	guint		periodic_check_id;
     4.9  	guint		refresh_interval;
    4.10 +	gboolean	use_mobile_connection;
    4.11  	guint		important_updates;
    4.12  	guint		normal_updates;
    4.13  };
    4.14 @@ -62,6 +64,7 @@
    4.15      PROP_IMPORTANT_UPDATES,
    4.16      PROP_NORMAL_UPDATES,
    4.17      PROP_REFRESH_INTERVAL,
    4.18 +    PROP_USE_MOBILE_CONNECTION,
    4.19      PROP_LAST
    4.20  };
    4.21  
    4.22 @@ -147,8 +150,10 @@
    4.23  	self->last_check = g_get_monotonic_time();
    4.24  
    4.25  out:
    4.26 +	g_clear_object(&self->cancellable);
    4.27 +
    4.28  	/* reschedule periodic check */
    4.29 -	if (self->network_state != PK_NETWORK_ENUM_OFFLINE) {
    4.30 +	if (!self->inhibited) {
    4.31  		self->periodic_check_id =
    4.32  		    g_timeout_add_seconds(PUI_CHECK_UPDATES_INTERVAL,
    4.33  		    periodic_check, self);
    4.34 @@ -166,6 +171,7 @@
    4.35  
    4.36  	g_debug("running periodic check");
    4.37  
    4.38 +	self->cancellable = g_cancellable_new();
    4.39  	pui_get_updates_async(self->pk_control, self->refresh_interval,
    4.40  	    self->cancellable, on_get_updates_finished, self);
    4.41  
    4.42 @@ -176,6 +182,50 @@
    4.43  }
    4.44  
    4.45  static void
    4.46 +check_inhibit(PuiBackend *self)
    4.47 +{
    4.48 +	gboolean	inhibited;
    4.49 +	guint		elapsed_time;
    4.50 +	guint		remaining_time;
    4.51 +
    4.52 +	inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) ||
    4.53 +	    (!self->use_mobile_connection &&
    4.54 +	    (self->network_state == PK_NETWORK_ENUM_MOBILE)));
    4.55 +	if (self->inhibited == inhibited) {
    4.56 +		return;
    4.57 +	}
    4.58 +
    4.59 +	self->inhibited = inhibited;
    4.60 +	if (inhibited) {
    4.61 +		/* cancel periodic checks */
    4.62 +		if (self->periodic_check_id != 0) {
    4.63 +			g_source_remove(self->periodic_check_id);
    4.64 +		}
    4.65 +
    4.66 +		/* cancel running operation */
    4.67 +		if ((self->cancellable != NULL) &&
    4.68 +		    !g_cancellable_is_cancelled(self->cancellable)) {
    4.69 +			g_cancellable_cancel(self->cancellable);
    4.70 +			g_clear_object(&self->cancellable);
    4.71 +		}
    4.72 +	} else {
    4.73 +		/* schedule periodic checks when no longer inhibited */
    4.74 +		elapsed_time = (g_get_monotonic_time() - self->last_check) /
    4.75 +		    G_USEC_PER_SEC;
    4.76 +		/*
    4.77 +		 * if more time that the check interval has passed since the
    4.78 +		 * last check, schedule a check after a short delay, otherwise
    4.79 +		 * wait until the interval has passed
    4.80 +		 */
    4.81 +		remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
    4.82 +		    PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
    4.83 +		    PUI_STARTUP_DELAY;
    4.84 +		self->periodic_check_id = g_timeout_add_seconds(remaining_time,
    4.85 +		    periodic_check, self);
    4.86 +	}
    4.87 +}
    4.88 +
    4.89 +static void
    4.90  pui_backend_set_property(GObject *object, guint property_id,
    4.91      const GValue *value, GParamSpec *pspec)
    4.92  {
    4.93 @@ -187,6 +237,12 @@
    4.94  		g_debug("property \"refresh-interval\" set to %u",
    4.95  		    self->refresh_interval);
    4.96  		break;
    4.97 +	case PROP_USE_MOBILE_CONNECTION:
    4.98 +		self->use_mobile_connection = g_value_get_boolean(value);
    4.99 +		g_debug("property \"use-mobile-connection\" set to %s",
   4.100 +		    self->use_mobile_connection ? "true" : "false");
   4.101 +		check_inhibit(self);
   4.102 +		break;
   4.103  	default:
   4.104  		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
   4.105  		break;
   4.106 @@ -209,6 +265,9 @@
   4.107  	case PROP_REFRESH_INTERVAL:
   4.108  		g_value_set_uint(value, self->refresh_interval);
   4.109  		break;
   4.110 +	case PROP_USE_MOBILE_CONNECTION:
   4.111 +		g_value_set_boolean(value, self->use_mobile_connection);
   4.112 +		break;
   4.113  	default:
   4.114  		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
   4.115  		break;
   4.116 @@ -261,6 +320,12 @@
   4.117  	    "Interval in seconds for refreshing the package cache", 0,
   4.118  	    G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
   4.119  
   4.120 +	properties[PROP_USE_MOBILE_CONNECTION] =
   4.121 +	    g_param_spec_boolean("use-mobile-connection",
   4.122 +	    "Whether to use a mobile connection", "Whether to use a mobile "
   4.123 +	    "connection for refreshing the package cache", FALSE,
   4.124 +	    G_PARAM_READWRITE);
   4.125 +
   4.126  	g_object_class_install_properties(object_class, PROP_LAST, properties);
   4.127  
   4.128  	signals[STATE_CHANGED] = g_signal_new("state-changed",
   4.129 @@ -277,8 +342,8 @@
   4.130  static void
   4.131  pui_backend_init(PuiBackend *self)
   4.132  {
   4.133 -	self->cancellable = g_cancellable_new();
   4.134  	self->pk_control = pk_control_new();
   4.135 +	self->inhibited = TRUE;
   4.136  }
   4.137  
   4.138  static void
   4.139 @@ -329,36 +394,11 @@
   4.140      gpointer user_data)
   4.141  {
   4.142  	PuiBackend	*self = user_data;
   4.143 -	PkNetworkEnum	network_state;
   4.144 -	guint		elapsed_time;
   4.145 -	guint		remaining_time;
   4.146  
   4.147 -	g_object_get(pk_control, "network-state", &network_state, NULL);
   4.148 +	g_object_get(pk_control, "network-state", &self->network_state, NULL);
   4.149  	g_debug("network state changed: %s",
   4.150 -	    pk_network_enum_to_string(network_state));
   4.151 -	if ((self->network_state == PK_NETWORK_ENUM_OFFLINE) &&
   4.152 -	    (network_state != PK_NETWORK_ENUM_OFFLINE)) {
   4.153 -		/* schedule periodic checks when coming back online */
   4.154 -		elapsed_time = (g_get_monotonic_time() - self->last_check) /
   4.155 -		    G_USEC_PER_SEC;
   4.156 -		/*
   4.157 -		 * if more time that the check interval has passed since the
   4.158 -		 * last check, schedule a check after a short delay, otherwise
   4.159 -		 * wait until the interval has passed
   4.160 -		 */
   4.161 -		remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
   4.162 -		    PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
   4.163 -		    PUI_STARTUP_DELAY;
   4.164 -		self->periodic_check_id = g_timeout_add_seconds(remaining_time,
   4.165 -		    periodic_check, self);
   4.166 -	} else if ((self->network_state != PK_NETWORK_ENUM_OFFLINE) &&
   4.167 -	    (network_state == PK_NETWORK_ENUM_OFFLINE)) {
   4.168 -		/* cancel periodic checks while offline */
   4.169 -		if (self->periodic_check_id != 0) {
   4.170 -			g_source_remove(self->periodic_check_id);
   4.171 -		}
   4.172 -	}
   4.173 -	self->network_state = network_state;
   4.174 +	    pk_network_enum_to_string(self->network_state));
   4.175 +	check_inhibit(self);
   4.176  }
   4.177  
   4.178  static void
   4.179 @@ -370,7 +410,7 @@
   4.180  	 * schedule a check after a short delay so that a rapid succession of
   4.181  	 * signals is coalesced
   4.182  	 */
   4.183 -	if (self->network_state != PK_NETWORK_ENUM_OFFLINE) {
   4.184 +	if (!self->inhibited) {
   4.185  		if (self->periodic_check_id != 0) {
   4.186  			g_source_remove(self->periodic_check_id);
   4.187  		}
   4.188 @@ -425,9 +465,8 @@
   4.189  	/* get notifications when an application restart is required */
   4.190  	g_signal_connect(self->pk_control, "restart-schedule",
   4.191  	    G_CALLBACK(on_restart_schedule), self);
   4.192 -	/* schedule first check after a small delay */
   4.193 -	self->periodic_check_id = g_timeout_add_seconds(PUI_STARTUP_DELAY,
   4.194 -	    periodic_check, self);
   4.195 +
   4.196 +	check_inhibit(self);
   4.197  
   4.198  	return (TRUE);
   4.199  }
     5.1 --- a/pui-prefs-application.c	Wed Jun 13 23:25:09 2018 +0200
     5.2 +++ b/pui-prefs-application.c	Sun Jun 17 11:05:28 2018 +0200
     5.3 @@ -127,6 +127,7 @@
     5.4  	GtkTreeModel	*tree_model;
     5.5  	GtkWidget	*update_command_entry;
     5.6  	GtkWidget	*refresh_interval_combo_box;
     5.7 +	GtkWidget	*use_mobile_check_button;
     5.8  
     5.9  	application_class->startup(application);
    5.10  
    5.11 @@ -146,6 +147,8 @@
    5.12  	    "refresh-interval"));
    5.13  	tree_model =
    5.14  	    gtk_combo_box_get_model(GTK_COMBO_BOX(refresh_interval_combo_box));
    5.15 +	use_mobile_check_button = GTK_WIDGET(gtk_builder_get_object(builder,
    5.16 +	    "use-mobile-connection"));
    5.17  
    5.18  	/* bind settings to widgets */
    5.19  	self->settings = pui_settings_new();
    5.20 @@ -155,6 +158,8 @@
    5.21  	    refresh_interval_combo_box, "active", G_SETTINGS_BIND_DEFAULT,
    5.22  	    map_refresh_interval_to_index, map_index_to_refresh_interval,
    5.23  	    tree_model, NULL);
    5.24 +	g_settings_bind(self->settings, "use-mobile-connection",
    5.25 +	    use_mobile_check_button, "active", G_SETTINGS_BIND_DEFAULT);
    5.26  
    5.27  	/* show window */
    5.28  	gtk_widget_show(window);
     6.1 --- a/pui-prefs-window.ui	Wed Jun 13 23:25:09 2018 +0200
     6.2 +++ b/pui-prefs-window.ui	Sun Jun 17 11:05:28 2018 +0200
     6.3 @@ -125,6 +125,21 @@
     6.4                  <property name="top_attach">0</property>
     6.5                </packing>
     6.6              </child>
     6.7 +            <child>
     6.8 +              <object class="GtkCheckButton" id="use-mobile-connection">
     6.9 +                <property name="label" translatable="yes">Use _mobile connection</property>
    6.10 +                <property name="visible">True</property>
    6.11 +                <property name="can_focus">True</property>
    6.12 +                <property name="receives_default">False</property>
    6.13 +                <property name="use_underline">True</property>
    6.14 +                <property name="draw_indicator">True</property>
    6.15 +              </object>
    6.16 +              <packing>
    6.17 +                <property name="left_attach">0</property>
    6.18 +                <property name="top_attach">2</property>
    6.19 +                <property name="width">2</property>
    6.20 +              </packing>
    6.21 +            </child>
    6.22            </object>
    6.23            <packing>
    6.24              <property name="expand">True</property>