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

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