From: faultline <me@ctsk.xyz>
Date: Sun, 5 Feb 2023 13:48:03 +0100
Subject: ui-utils: avoid unnecessary filling password

Consumers of libnma (gnome-control-center and nm-connection-editor)
re-fill a connections settings back into the UI, whenever a "changed"
signal gets emitted and the change results in a valid configuration.
Thus we need to carefully avoid triggering more "changed" signals
while (automatically) filling data into the password field.

In gtk3, gtk_editable_set_text took care of it automatically. Here, it
doesn't trigger a signal if the new text is equal to the old text.

In gtk4 however, the "changed" signal is triggered even when the old and
the new text match. Therefore we add explicit checks before setting the
text in the password field to account for this updated behaviour.

https://gitlab.gnome.org/GNOME/gnome-control-center/-/issues/2109
https://gitlab.gnome.org/GNOME/libnma/-/merge_requests/52
(cherry picked from commit d48911ca5f7ad4fbc44fef08df57e5fd844ba862)
---
 src/nma-ui-utils.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/nma-ui-utils.c b/src/nma-ui-utils.c
index 41d9468..bd0819c 100644
--- a/src/nma-ui-utils.c
+++ b/src/nma-ui-utils.c
@@ -58,6 +58,7 @@ change_password_storage_icon (GtkWidget *passwd_entry, MenuItem item)
 {
 	PasswordStorageData *data;
 	const char *old_pwd;
+	int changed;
 
 	g_return_if_fail (item >= 0 && item <= ITEM_STORAGE_MAX);
 
@@ -83,14 +84,19 @@ change_password_storage_icon (GtkWidget *passwd_entry, MenuItem item)
 		if (old_pwd && *old_pwd)
 			g_object_set_data_full (G_OBJECT (passwd_entry), "password-old",
 		                                g_strdup (old_pwd), g_free_str0);
-		gtk_editable_set_text (GTK_EDITABLE (passwd_entry), "");
+
+		changed = g_strcmp0 (gtk_editable_get_text (GTK_EDITABLE (passwd_entry)), "") != 0;
+		if (changed)
+			gtk_editable_set_text (GTK_EDITABLE (passwd_entry), "");
+
 		if (gtk_widget_is_focus (passwd_entry))
 			gtk_widget_child_focus (((GtkWidget *)gtk_widget_get_root (passwd_entry)), GTK_DIR_TAB_BACKWARD);
 		gtk_widget_set_can_focus (passwd_entry, FALSE);
 	} else {
 		/* Set the old password to the entry */
 		old_pwd = g_object_get_data (G_OBJECT (passwd_entry), "password-old");
-		if (old_pwd && *old_pwd)
+		changed = g_strcmp0 (gtk_editable_get_text (GTK_EDITABLE (passwd_entry)), old_pwd) != 0;
+		if (old_pwd && *old_pwd && changed)
 			gtk_editable_set_text (GTK_EDITABLE (passwd_entry), old_pwd);
 		g_object_set_data (G_OBJECT (passwd_entry), "password-old", NULL);
 
