# HG changeset patch # User Guido Berhoerster # Date 1529226328 -7200 # Node ID 3d72ca76538d0ff07e4db8645e922abbf90480b0 # Parent 2fa34d6272c6f03b47833b4115efff03c90a60f8 Add setting to control whether to use a mobile connection diff -r 2fa34d6272c6 -r 3d72ca76538d org.guido-berhoerster.code.package-update-indicator.gschema.xml --- 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 @@ The interval in seconds for refreshing metadata. + + false + Whether to use a mobile connection + If enabled, use a mobile connection refreshing the package + cache. + diff -r 2fa34d6272c6 -r 3d72ca76538d package-update-indicator-prefs.1.xml --- 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 @@ guido+pui@berhoerster.name - 5 June, 2018 + 17 June, 2018 package-update-indicator-prefs @@ -72,6 +72,13 @@ The command for installing updates. + + Use mobile connection + + Whether to use a mobile connection for refreshing the package + cache. + + diff -r 2fa34d6272c6 -r 3d72ca76538d pui-application.c --- 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); diff -r 2fa34d6272c6 -r 3d72ca76538d pui-backend.c --- 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); } diff -r 2fa34d6272c6 -r 3d72ca76538d pui-prefs-application.c --- 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); diff -r 2fa34d6272c6 -r 3d72ca76538d pui-prefs-window.ui --- 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 @@ 0 + + + Use _mobile connection + True + True + False + True + True + + + 0 + 2 + 2 + + True