Blame SOURCES/0015-subman-Clean-up-notification-behavior.patch

6486b0
From 3412be1f63df2a5967ef92c27028368df1646b5c Mon Sep 17 00:00:00 2001
6486b0
From: Ray Strode <rstrode@redhat.com>
6486b0
Date: Sun, 24 Jan 2021 12:41:20 -0500
6486b0
Subject: [PATCH 15/15] subman: Clean up notification behavior
6486b0
6486b0
Notifications were only displayed for some status transitions.
6486b0
6486b0
This commit introduces some booleans based on the old and new
6486b0
statuses to make the code clearer and to make it easier to hit
6486b0
all the cases.
6486b0
---
6486b0
 plugins/subman/gsd-subman-common.h        |   1 +
6486b0
 plugins/subman/gsd-subscription-manager.c | 141 ++++++++++++++++++----
6486b0
 2 files changed, 120 insertions(+), 22 deletions(-)
6486b0
6486b0
diff --git a/plugins/subman/gsd-subman-common.h b/plugins/subman/gsd-subman-common.h
6486b0
index 88226564..9397dbe4 100644
6486b0
--- a/plugins/subman/gsd-subman-common.h
6486b0
+++ b/plugins/subman/gsd-subman-common.h
6486b0
@@ -1,40 +1,41 @@
6486b0
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
6486b0
  *
6486b0
  * Copyright (C) 2019 Richard Hughes <rhughes@redhat.com>
6486b0
  *
6486b0
  * This program is free software; you can redistribute it and/or modify
6486b0
  * it under the terms of the GNU General Public License as published by
6486b0
  * the Free Software Foundation; either version 2 of the License, or
6486b0
  * (at your option) any later version.
6486b0
  *
6486b0
  * This program is distributed in the hope that it will be useful,
6486b0
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
6486b0
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6486b0
  * GNU General Public License for more details.
6486b0
  *
6486b0
  * You should have received a copy of the GNU General Public License
6486b0
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
6486b0
  *
6486b0
  */
6486b0
 
6486b0
 #ifndef __GSD_SUBMAN_COMMON_H
6486b0
 #define __GSD_SUBMAN_COMMON_H
6486b0
 
6486b0
 #include <glib-object.h>
6486b0
 
6486b0
 G_BEGIN_DECLS
6486b0
 
6486b0
 typedef enum {
6486b0
+	GSD_SUBMAN_SUBSCRIPTION_STATUS_NOT_READ = -1,
6486b0
 	GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN,
6486b0
 	GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID,
6486b0
 	GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID,
6486b0
 	GSD_SUBMAN_SUBSCRIPTION_STATUS_DISABLED,
6486b0
 	GSD_SUBMAN_SUBSCRIPTION_STATUS_PARTIALLY_VALID,
6486b0
 	GSD_SUBMAN_SUBSCRIPTION_STATUS_NO_INSTALLED_PRODUCTS,
6486b0
 } GsdSubmanSubscriptionStatus;
6486b0
 
6486b0
 const gchar	*gsd_subman_subscription_status_to_string	(GsdSubmanSubscriptionStatus	 status);
6486b0
 
6486b0
 G_END_DECLS
6486b0
 
6486b0
 #endif /* __GSD_SUBMAN_COMMON_H */
6486b0
diff --git a/plugins/subman/gsd-subscription-manager.c b/plugins/subman/gsd-subscription-manager.c
6486b0
index 6d80bfa9..aaccbbc6 100644
6486b0
--- a/plugins/subman/gsd-subscription-manager.c
6486b0
+++ b/plugins/subman/gsd-subscription-manager.c
6486b0
@@ -243,60 +243,61 @@ _client_installed_products_update (GsdSubscriptionManager *manager, GError **err
6486b0
 		product->version = g_strdup (json_array_get_string_element (json_product, 2));
6486b0
 		product->arch = g_strdup (json_array_get_string_element (json_product, 3));
6486b0
 		product->status = g_strdup (json_array_get_string_element (json_product, 4));
6486b0
 		product->starts = g_strdup (json_array_get_string_element (json_product, 6));
6486b0
 		product->ends = g_strdup (json_array_get_string_element (json_product, 7));
6486b0
 
6486b0
 		g_ptr_array_add (priv->installed_products, g_steal_pointer (&product));
6486b0
 	}
6486b0
 
6486b0
 	/* emit notification for g-c-c */
6486b0
 	_emit_property_changed (manager, "InstalledProducts",
6486b0
 			       _make_installed_products_variant (priv->installed_products));
6486b0
 
6486b0
 	return TRUE;
6486b0
 }
6486b0
 
6486b0
 static gboolean
6486b0
 _client_subscription_status_update (GsdSubscriptionManager *manager, GError **error)
6486b0
 {
6486b0
 	GsdSubscriptionManagerPrivate *priv = manager->priv;
6486b0
 	g_autoptr(GVariant) uuid = NULL;
6486b0
 	const gchar *uuid_txt = NULL;
6486b0
 	JsonNode *json_root;
6486b0
 	JsonObject *json_obj;
6486b0
 	const gchar *json_txt = NULL;
6486b0
 	g_autoptr(GVariant) status = NULL;
6486b0
 	g_autoptr(JsonParser) json_parser = json_parser_new ();
6486b0
 
6486b0
 	/* save old value */
6486b0
 	priv->subscription_status_last = priv->subscription_status;
6486b0
+
6486b0
 	if (!_client_installed_products_update (manager, error))
6486b0
 		goto out;
6486b0
 
6486b0
 	if (priv->installed_products->len == 0) {
6486b0
 		priv->subscription_status = GSD_SUBMAN_SUBSCRIPTION_STATUS_NO_INSTALLED_PRODUCTS;
6486b0
 		goto out;
6486b0
 	}
6486b0
 
6486b0
 	uuid = g_dbus_proxy_call_sync (priv->proxies[_RHSM_INTERFACE_CONSUMER],
6486b0
 				       "GetUuid",
6486b0
 				       g_variant_new ("(s)",
6486b0
 				                      "C.UTF-8"),
6486b0
 				       G_DBUS_CALL_FLAGS_NONE,
6486b0
 				       -1, NULL, error);
6486b0
 	if (uuid == NULL)
6486b0
 		return FALSE;
6486b0
 
6486b0
 	g_variant_get (uuid, "(&s)", &uuid_txt);
6486b0
 
6486b0
 	if (uuid_txt[0] == '\0') {
6486b0
 		priv->subscription_status = GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN;
6486b0
 		goto out;
6486b0
 	}
6486b0
 
6486b0
 	status = g_dbus_proxy_call_sync (priv->proxies[_RHSM_INTERFACE_ENTITLEMENT],
6486b0
 				         "GetStatus",
6486b0
 				         g_variant_new ("(ss)",
6486b0
 						        "", /* assumed as 'now' */
6486b0
 						        "C.UTF-8"),
6486b0
 				         G_DBUS_CALL_FLAGS_NONE,
6486b0
@@ -485,109 +486,203 @@ typedef enum {
6486b0
 static void
6486b0
 _show_notification (GsdSubscriptionManager *manager, _NotifyKind notify_kind)
6486b0
 {
6486b0
 	GsdSubscriptionManagerPrivate *priv = manager->priv;
6486b0
 	switch (notify_kind) {
6486b0
 	case _NOTIFY_EXPIRED:
6486b0
 		notify_notification_close (priv->notification_registered, NULL);
6486b0
 		notify_notification_close (priv->notification_registration_required, NULL);
6486b0
 		notify_notification_show (priv->notification_expired, NULL);
6486b0
 		break;
6486b0
 	case _NOTIFY_REGISTRATION_REQUIRED:
6486b0
 		notify_notification_close (priv->notification_registered, NULL);
6486b0
 		notify_notification_close (priv->notification_expired, NULL);
6486b0
 		notify_notification_show (priv->notification_registration_required, NULL);
6486b0
 		break;
6486b0
 	case _NOTIFY_REGISTERED:
6486b0
 		notify_notification_close (priv->notification_expired, NULL);
6486b0
 		notify_notification_close (priv->notification_registration_required, NULL);
6486b0
 		notify_notification_show (priv->notification_registered, NULL);
6486b0
 		break;
6486b0
 	default:
6486b0
 		break;
6486b0
 	}
6486b0
 	g_timer_reset (priv->timer_last_notified);
6486b0
 }
6486b0
 
6486b0
 static void
6486b0
 _client_maybe__show_notification (GsdSubscriptionManager *manager)
6486b0
 {
6486b0
 	GsdSubscriptionManagerPrivate *priv = manager->priv;
6486b0
+	gboolean was_read, was_registered, had_subscriptions, needed_subscriptions;
6486b0
+	gboolean is_read, is_registered, has_subscriptions, needs_subscriptions;
6486b0
+
6486b0
+	switch (priv->subscription_status_last) {
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_NOT_READ:
6486b0
+			was_read = FALSE;
6486b0
+			was_registered = FALSE;
6486b0
+			needed_subscriptions = TRUE;
6486b0
+			had_subscriptions = FALSE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN:
6486b0
+			was_read = TRUE;
6486b0
+			was_registered = FALSE;
6486b0
+			needed_subscriptions = TRUE;
6486b0
+			had_subscriptions = FALSE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID:
6486b0
+			was_read = TRUE;
6486b0
+			was_registered = TRUE;
6486b0
+			needed_subscriptions = TRUE;
6486b0
+			had_subscriptions = TRUE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID:
6486b0
+			was_read = TRUE;
6486b0
+			was_registered = TRUE;
6486b0
+			needed_subscriptions = TRUE;
6486b0
+			had_subscriptions = FALSE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_DISABLED:
6486b0
+			was_read = TRUE;
6486b0
+			was_registered = TRUE;
6486b0
+			needed_subscriptions = FALSE;
6486b0
+			had_subscriptions = FALSE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_PARTIALLY_VALID:
6486b0
+			was_read = TRUE;
6486b0
+			was_registered = TRUE;
6486b0
+			needed_subscriptions = TRUE;
6486b0
+			had_subscriptions = FALSE;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_NO_INSTALLED_PRODUCTS:
6486b0
+			was_read = TRUE;
6486b0
+			was_registered = FALSE;
6486b0
+			needed_subscriptions = FALSE;
6486b0
+			had_subscriptions = FALSE;
6486b0
+			break;
6486b0
+	}
6486b0
+
6486b0
+	switch (priv->subscription_status) {
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_NOT_READ:
6486b0
+			is_read = FALSE;
6486b0
+			is_registered = FALSE;
6486b0
+			needs_subscriptions = TRUE;
6486b0
+			has_subscriptions = FALSE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN:
6486b0
+			is_read = TRUE;
6486b0
+			is_registered = FALSE;
6486b0
+			needs_subscriptions = TRUE;
6486b0
+			has_subscriptions = FALSE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID:
6486b0
+			is_read = TRUE;
6486b0
+			is_registered = TRUE;
6486b0
+			needs_subscriptions = TRUE;
6486b0
+			has_subscriptions = TRUE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_PARTIALLY_VALID:
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID:
6486b0
+			is_read = TRUE;
6486b0
+			is_registered = TRUE;
6486b0
+			needs_subscriptions = TRUE;
6486b0
+			has_subscriptions = FALSE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_DISABLED:
6486b0
+			is_read = TRUE;
6486b0
+			is_registered = TRUE;
6486b0
+			needs_subscriptions = FALSE;
6486b0
+			has_subscriptions = FALSE;
6486b0
+			break;
6486b0
+		case GSD_SUBMAN_SUBSCRIPTION_STATUS_NO_INSTALLED_PRODUCTS:
6486b0
+			is_read = TRUE;
6486b0
+			is_registered = FALSE;
6486b0
+			needs_subscriptions = FALSE;
6486b0
+			has_subscriptions = FALSE;
6486b0
+			break;
6486b0
+	}
6486b0
 
6486b0
 	/* startup */
6486b0
-	if (priv->subscription_status_last == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN &&
6486b0
-	    priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN) {
6486b0
+	if (!was_read && is_read && priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN) {
6486b0
 		_show_notification (manager, _NOTIFY_REGISTRATION_REQUIRED);
6486b0
 		return;
6486b0
 	}
6486b0
 
6486b0
 	/* something changed */
6486b0
-	if (priv->subscription_status_last != priv->subscription_status) {
6486b0
+	if (was_read && is_read && priv->subscription_status_last != priv->subscription_status) {
6486b0
 		g_debug ("transisition from subscription status '%s' to '%s'",
6486b0
 			 gsd_subman_subscription_status_to_string (priv->subscription_status_last),
6486b0
 			 gsd_subman_subscription_status_to_string (priv->subscription_status));
6486b0
 
6486b0
-		/* needs registration */
6486b0
-		if (priv->subscription_status_last == GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID &&
6486b0
-		    priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID) {
6486b0
-			_show_notification (manager, _NOTIFY_REGISTRATION_REQUIRED);
6486b0
+		/* needs subscription */
6486b0
+		if (is_registered && needs_subscriptions && !has_subscriptions) {
6486b0
+			_show_notification (manager, _NOTIFY_EXPIRED);
6486b0
 			return;
6486b0
 		}
6486b0
 
6486b0
 		/* was unregistered */
6486b0
-		if (priv->subscription_status_last == GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID &&
6486b0
-		    priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN) {
6486b0
+		if (was_registered && !is_registered) {
6486b0
 			_show_notification (manager, _NOTIFY_REGISTRATION_REQUIRED);
6486b0
 			return;
6486b0
 		}
6486b0
 
6486b0
-		/* registered */
6486b0
-		if (priv->subscription_status_last == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN &&
6486b0
-		    priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID &&
6486b0
-		    g_timer_elapsed (priv->timer_last_notified, NULL) > 60) {
6486b0
-			_show_notification (manager, _NOTIFY_REGISTERED);
6486b0
-			return;
6486b0
+		/* just registered */
6486b0
+		if (!was_registered && is_registered) {
6486b0
+			if (!needs_subscriptions || has_subscriptions) {
6486b0
+				_show_notification (manager, _NOTIFY_REGISTERED);
6486b0
+				return;
6486b0
+			}
6486b0
+		}
6486b0
+
6486b0
+		/* subscriptions changed */
6486b0
+		if (was_registered && is_registered) {
6486b0
+			/* subscribed */
6486b0
+			if (!had_subscriptions &&
6486b0
+			    needs_subscriptions && has_subscriptions) {
6486b0
+				_show_notification (manager, _NOTIFY_REGISTERED);
6486b0
+				return;
6486b0
+			}
6486b0
+
6486b0
+			/* simple content access enabled */
6486b0
+			if (needed_subscriptions && !had_subscriptions && !needs_subscriptions) {
6486b0
+				_show_notification (manager, _NOTIFY_REGISTERED);
6486b0
+				return;
6486b0
+			}
6486b0
 		}
6486b0
 	}
6486b0
 
6486b0
 	/* nag again */
6486b0
-	if (priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN &&
6486b0
+	if (!is_registered &&
6486b0
 	    g_timer_elapsed (priv->timer_last_notified, NULL) > 60 * 60 * 24) {
6486b0
 		_show_notification (manager, _NOTIFY_REGISTRATION_REQUIRED);
6486b0
 		return;
6486b0
 	}
6486b0
-	if (priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID &&
6486b0
-	    g_timer_elapsed (priv->timer_last_notified, NULL) > 60 * 60 * 24) {
6486b0
-		_show_notification (manager, _NOTIFY_EXPIRED);
6486b0
-		return;
6486b0
-	}
6486b0
-	if (priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_PARTIALLY_VALID &&
6486b0
+	if (is_registered && !has_subscriptions && needs_subscriptions &&
6486b0
 	    g_timer_elapsed (priv->timer_last_notified, NULL) > 60 * 60 * 24) {
6486b0
 		_show_notification (manager, _NOTIFY_EXPIRED);
6486b0
 		return;
6486b0
 	}
6486b0
 }
6486b0
 
6486b0
 static gboolean
6486b0
 _client_register_with_keys (GsdSubscriptionManager *manager,
6486b0
 				  const gchar *hostname,
6486b0
 				  const gchar *organisation,
6486b0
 				  const gchar *activation_key,
6486b0
 				  GError **error)
6486b0
 {
6486b0
 	GsdSubscriptionManagerPrivate *priv = manager->priv;
6486b0
 	g_autoptr(GSubprocess) subprocess = NULL;
6486b0
 	g_autoptr(GBytes) stdin_buf = g_bytes_new (activation_key, strlen (activation_key) + 1);
6486b0
 	g_autoptr(GBytes) stderr_buf = NULL;
6486b0
 	gint rc;
6486b0
 
6486b0
 	/* apparently: "we can't send registration credentials over the regular
6486b0
 	 * system or session bus since those aren't really locked down..." */
6486b0
 	if (!_client_register_start (manager, error))
6486b0
 		return FALSE;
6486b0
 	g_debug ("spawning %s", LIBEXECDIR "/gsd-subman-helper");
6486b0
 	subprocess = g_subprocess_new (G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDERR_PIPE, error,
6486b0
 				       "pkexec", LIBEXECDIR "/gsd-subman-helper",
6486b0
 				       "--kind", "register-with-key",
6486b0
 				       "--address", priv->address,
6486b0
 				       "--hostname", hostname,
6486b0
 				       "--organisation", organisation,
6486b0
@@ -914,60 +1009,62 @@ gsd_subscription_manager_class_init (GsdSubscriptionManagerClass *klass)
6486b0
 static void
6486b0
 _launch_info_overview (void)
6486b0
 {
6486b0
 	const gchar *argv[] = { "gnome-control-center", "info-overview", NULL };
6486b0
 	g_debug ("Running gnome-control-center info-overview");
6486b0
 	g_spawn_async (NULL, (gchar **) argv, NULL, G_SPAWN_SEARCH_PATH,
6486b0
 		       NULL, NULL, NULL, NULL);
6486b0
 }
6486b0
 
6486b0
 static void
6486b0
 _notify_closed_cb (NotifyNotification *notification, gpointer user_data)
6486b0
 {
6486b0
 	/* FIXME: only launch when clicking on the main body, not the window close */
6486b0
 	if (notify_notification_get_closed_reason (notification) == 0x400)
6486b0
 		_launch_info_overview ();
6486b0
 }
6486b0
 
6486b0
 static void
6486b0
 _notify_clicked_cb (NotifyNotification *notification, char *action, gpointer user_data)
6486b0
 {
6486b0
 	_launch_info_overview ();
6486b0
 }
6486b0
 
6486b0
 static void
6486b0
 gsd_subscription_manager_init (GsdSubscriptionManager *manager)
6486b0
 {
6486b0
 	GsdSubscriptionManagerPrivate *priv = manager->priv = GSD_SUBSCRIPTION_MANAGER_GET_PRIVATE (manager);
6486b0
 
6486b0
 	priv->installed_products = g_ptr_array_new_with_free_func ((GDestroyNotify) product_data_free);
6486b0
 	priv->timer_last_notified = g_timer_new ();
6486b0
+	priv->subscription_status = GSD_SUBMAN_SUBSCRIPTION_STATUS_NOT_READ;
6486b0
+	priv->subscription_status_last = GSD_SUBMAN_SUBSCRIPTION_STATUS_NOT_READ;
6486b0
 
6486b0
 	/* expired */
6486b0
 	priv->notification_expired =
6486b0
 		notify_notification_new (_("Subscription Has Expired"),
6486b0
 					 _("Add or renew a subscription to continue receiving software updates."),
6486b0
 					 NULL);
6486b0
 	notify_notification_set_app_name (priv->notification_expired, _("Subscription"));
6486b0
 	notify_notification_set_hint_string (priv->notification_expired, "desktop-entry", "subman-panel");
6486b0
 	notify_notification_set_hint_string (priv->notification_expired, "x-gnome-privacy-scope", "system");
6486b0
 	notify_notification_set_urgency (priv->notification_expired, NOTIFY_URGENCY_CRITICAL);
6486b0
 	notify_notification_add_action (priv->notification_expired,
6486b0
 					"info-overview", _("Subscribe System…"),
6486b0
 					_notify_clicked_cb,
6486b0
 					manager, NULL);
6486b0
 	g_signal_connect (priv->notification_expired, "closed",
6486b0
 			  G_CALLBACK (_notify_closed_cb), manager);
6486b0
 
6486b0
 	/* registered */
6486b0
 	priv->notification_registered =
6486b0
 		notify_notification_new (_("Registration Successful"),
6486b0
 					 _("The system has been registered and software updates have been enabled."),
6486b0
 					 NULL);
6486b0
 	notify_notification_set_app_name (priv->notification_registered, _("Subscription"));
6486b0
 	notify_notification_set_hint_string (priv->notification_registered, "desktop-entry", "subman-panel");
6486b0
 	notify_notification_set_hint_string (priv->notification_registered, "x-gnome-privacy-scope", "system");
6486b0
 	notify_notification_set_urgency (priv->notification_registered, NOTIFY_URGENCY_CRITICAL);
6486b0
 	g_signal_connect (priv->notification_registered, "closed",
6486b0
 			  G_CALLBACK (_notify_closed_cb), manager);
6486b0
 
6486b0
 	/* registration required */
6486b0
-- 
6486b0
2.30.0
6486b0