diff pui-application.c @ 55:24cba399757f

Add new setting "always-active" The setting determines whether the indicator status is set to "active" when packages are up to date, i.e. whether it is visible and/or can be interacted with. It defaults to true which reflects the previous behavior.
author Guido Berhoerster <guido+pui@berhoerster.name>
date Wed, 17 Aug 2022 16:55:17 +0200
parents c4b8785d0b75
children 08747f3a718b
line wrap: on
line diff
--- a/pui-application.c	Sun Feb 27 22:31:02 2022 +0100
+++ b/pui-application.c	Wed Aug 17 16:55:17 2022 +0200
@@ -48,6 +48,7 @@
 	PuiState	state;
 	gchar		*update_command;
 	gchar		*error_message;
+	gboolean	always_active;
 };
 
 G_DEFINE_TYPE(PuiApplication, pui_application, G_TYPE_APPLICATION)
@@ -55,6 +56,7 @@
 enum {
     PROP_0,
     PROP_UPDATE_COMMAND,
+    PROP_ALWAYS_ACTIVE,
     PROP_LAST
 };
 
@@ -189,6 +191,76 @@
 }
 
 static void
+update_indicator(PuiApplication *self, const gchar *title)
+{
+	switch (self->state) {
+	case PUI_STATE_INITIAL:
+		app_indicator_set_status(self->indicator,
+		    APP_INDICATOR_STATUS_PASSIVE);
+		break;
+	case PUI_STATE_UP_TO_DATE:
+		app_indicator_set_status(self->indicator, self->always_active ?
+		    APP_INDICATOR_STATUS_ACTIVE :
+		    APP_INDICATOR_STATUS_PASSIVE);
+		break;
+	case PUI_STATE_NORMAL_UPDATES_AVAILABLE:	/* FALLTHGROUGH */
+	case PUI_STATE_IMPORTANT_UPDATES_AVAILABLE:	/* FALLTHGROUGH */
+	case PUI_STATE_SESSION_RESTART_REQUIRED:	/* FALLTHGROUGH */
+	case PUI_STATE_SYSTEM_RESTART_REQUIRED:		/* FALLTHGROUGH */
+	case PUI_STATE_ERROR:
+		app_indicator_set_status(self->indicator,
+		    APP_INDICATOR_STATUS_ACTIVE);
+		break;
+	}
+	app_indicator_set_icon_full(self->indicator, icon_names[self->state],
+	    title);
+}
+
+static void
+update_notification(PuiApplication *self, const gchar *title, const gchar *body)
+{
+	GSimpleAction	*install_updates_action;
+	GApplication	*application = G_APPLICATION(self);
+	GNotification	*notification = NULL;
+
+	install_updates_action =
+	    G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(self),
+	    "install-updates"));
+
+	switch (self->state) {
+	case PUI_STATE_INITIAL:				/* FALLTHGROUGH */
+	case PUI_STATE_UP_TO_DATE:			/* FALLTHGROUGH */
+	case PUI_STATE_ERROR:
+		/* withdraw exisiting notification */
+		g_application_withdraw_notification(application,
+		    "package-updates-or-restart-required");
+		break;
+	case PUI_STATE_NORMAL_UPDATES_AVAILABLE:	/* FALLTHGROUGH */
+	case PUI_STATE_IMPORTANT_UPDATES_AVAILABLE:	/* FALLTHGROUGH */
+	case PUI_STATE_SESSION_RESTART_REQUIRED:	/* FALLTHGROUGH */
+	case PUI_STATE_SYSTEM_RESTART_REQUIRED:
+		/* create notification */
+		notification = g_notification_new(title);
+		g_notification_set_body(notification, body);
+		g_notification_set_icon(notification, self->icons[self->state]);
+		g_notification_set_priority(notification,
+		    G_NOTIFICATION_PRIORITY_NORMAL);
+		if (g_action_get_enabled(G_ACTION(install_updates_action))) {
+			g_notification_add_button(notification,
+			    _("Install Updates"),
+			    "app.install-updates");
+		}
+		g_application_send_notification(application,
+		    "package-updates-or-restart-required", notification);
+		break;
+	}
+
+	if (notification != NULL) {
+		g_object_unref(notification);
+	}
+}
+
+static void
 update_ui(PuiApplication *self)
 {
 	GSimpleAction	*install_updates_action;
@@ -197,7 +269,6 @@
 	gchar		*title = NULL;
 	gchar		*body = NULL;
 	GApplication	*application = G_APPLICATION(self);
-	GNotification	*notification = NULL;
 
 	install_updates_action =
 	    G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(self),
@@ -285,57 +356,8 @@
 		break;
 	}
 
-	/* indicator */
-	switch (self->state) {
-	case PUI_STATE_INITIAL:
-		app_indicator_set_status(self->indicator,
-		    APP_INDICATOR_STATUS_PASSIVE);
-		break;
-	case PUI_STATE_UP_TO_DATE:			/* FALLTHGROUGH */
-	case PUI_STATE_NORMAL_UPDATES_AVAILABLE:	/* FALLTHGROUGH */
-	case PUI_STATE_IMPORTANT_UPDATES_AVAILABLE:	/* FALLTHGROUGH */
-	case PUI_STATE_SESSION_RESTART_REQUIRED:	/* FALLTHGROUGH */
-	case PUI_STATE_SYSTEM_RESTART_REQUIRED:		/* FALLTHGROUGH */
-	case PUI_STATE_ERROR:
-		app_indicator_set_status(self->indicator,
-		    APP_INDICATOR_STATUS_ACTIVE);
-		break;
-	}
-	app_indicator_set_icon_full(self->indicator, icon_names[self->state],
-	    title);
-
-	/* notification */
-	switch (self->state) {
-	case PUI_STATE_INITIAL:				/* FALLTHGROUGH */
-	case PUI_STATE_UP_TO_DATE:			/* FALLTHGROUGH */
-	case PUI_STATE_ERROR:
-		/* withdraw exisiting notification */
-		g_application_withdraw_notification(application,
-		    "package-updates-or-restart-required");
-		break;
-	case PUI_STATE_NORMAL_UPDATES_AVAILABLE:	/* FALLTHGROUGH */
-	case PUI_STATE_IMPORTANT_UPDATES_AVAILABLE:	/* FALLTHGROUGH */
-	case PUI_STATE_SESSION_RESTART_REQUIRED:	/* FALLTHGROUGH */
-	case PUI_STATE_SYSTEM_RESTART_REQUIRED:
-		/* create notification */
-		notification = g_notification_new(title);
-		g_notification_set_body(notification, body);
-		g_notification_set_icon(notification, self->icons[self->state]);
-		g_notification_set_priority(notification,
-		    G_NOTIFICATION_PRIORITY_NORMAL);
-		if (g_action_get_enabled(G_ACTION(install_updates_action))) {
-			g_notification_add_button(notification,
-			    _("Install Updates"),
-			    "app.install-updates");
-		}
-		g_application_send_notification(application,
-		    "package-updates-or-restart-required", notification);
-		break;
-	}
-
-	if (notification != NULL) {
-		g_object_unref(notification);
-	}
+	update_indicator(self, title);
+	update_notification(self, title, body);
 
 	g_debug("indicator icon: %s, notification title: \"%s\", "
 	    "notification body: \"%s\"", icon_names[self->state], title, body);
@@ -478,10 +500,8 @@
 		self->icons[i] = g_themed_icon_new(icon_names[i]);
 	}
 
-	/* create settings */
+	/* create settings backend */
 	self->settings = pui_settings_new();
-	g_settings_bind(self->settings, "update-command", self,
-	    "update-command", G_SETTINGS_BIND_GET);
 
 	/* start instantiating backend */
 	pui_backend_new_async(self->cancellable, on_pui_backend_finished, self);
@@ -500,6 +520,18 @@
 	gtk_widget_show_all(menu);
 	app_indicator_set_menu(self->indicator, GTK_MENU(menu));
 
+	/*
+	 * bind settings to properties after indicator with its menu are
+	 * initialized, if the setting "always-active" is set by the user the
+	 * corresponding property is set accordingly which in turn invokes
+	 * update_ui() so order matters here
+	 */
+	g_settings_bind(self->settings, "update-command", self,
+	    "update-command", G_SETTINGS_BIND_GET);
+	g_settings_bind(self->settings, "always-active", self,
+	    "always-active", G_SETTINGS_BIND_GET);
+
+	/* ensure the ui reflects the current state */
 	update_ui(self);
 
 	/* keep GApplication running */
@@ -603,6 +635,12 @@
 		g_debug("property \"update-command\" set to \"%s\"",
 		    self->update_command);
 		break;
+	case PROP_ALWAYS_ACTIVE:
+		self->always_active = g_value_get_boolean(value);
+		g_debug("property \"always-active\" set to \"%s\"",
+		    self->always_active ? "TRUE" : "FALSE");
+		update_ui(self);
+		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
 		break;
@@ -619,6 +657,9 @@
 	case PROP_UPDATE_COMMAND:
 		g_value_set_string(value, self->update_command);
 		break;
+	case PROP_ALWAYS_ACTIVE:
+		g_value_set_boolean(value, self->always_active);
+		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
 		break;
@@ -650,8 +691,7 @@
 	}
 
 	if (self->about_dialog != NULL) {
-		g_clear_pointer(&self->about_dialog,
-		    (GDestroyNotify)(gtk_widget_destroy));
+		g_clear_pointer(&self->about_dialog, gtk_widget_destroy);
 	}
 
 	for (i = 0; i < G_N_ELEMENTS(self->icons); i++) {
@@ -688,6 +728,10 @@
 	properties[PROP_UPDATE_COMMAND] = g_param_spec_string("update-command",
 	    "Update command", "Command for installing updates", NULL,
 	    G_PARAM_READWRITE);
+	properties[PROP_ALWAYS_ACTIVE] = g_param_spec_boolean("always-active",
+	    "Whether the indicator is always active",
+	    "Whether the indicator is active even when packages are up to date",
+	    TRUE, G_PARAM_READWRITE);
 
 	g_object_class_install_properties(object_class, PROP_LAST, properties);