Call g_list_model_items_changed as necessary in Purple.ConversationManager

Sun, 03 Nov 2024 00:07:13 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Sun, 03 Nov 2024 00:07:13 -0500
changeset 43046
eeab15f4576e
parent 43045
308e8d0ce68c
child 43047
4c23faf5dfb5

Call g_list_model_items_changed as necessary in Purple.ConversationManager

This was missed when implementing Gio.ListModel.

Testing Done:
Ran the tests under valgrind and called in the turtles for the rest.

Reviewed at https://reviews.imfreedom.org/r/3637/

libpurple/purpleconversationmanager.c file | annotate | diff | comparison | revisions
libpurple/tests/test_conversation_manager.c file | annotate | diff | comparison | revisions
--- a/libpurple/purpleconversationmanager.c	Sun Nov 03 00:05:44 2024 -0500
+++ b/libpurple/purpleconversationmanager.c	Sun Nov 03 00:07:13 2024 -0500
@@ -420,6 +420,8 @@
 purple_conversation_manager_add(PurpleConversationManager *manager,
                                 PurpleConversation *conversation)
 {
+	guint position = 0;
+
 	g_return_val_if_fail(PURPLE_IS_CONVERSATION_MANAGER(manager), FALSE);
 	g_return_val_if_fail(PURPLE_IS_CONVERSATION(conversation), FALSE);
 
@@ -427,6 +429,7 @@
 		return FALSE;
 	}
 
+	position = manager->conversations->len;
 	g_ptr_array_add(manager->conversations, g_object_ref(conversation));
 
 	/* Register our signals that need to be propagated. */
@@ -439,6 +442,7 @@
 
 	/* Tell everyone about the new conversation. */
 	g_signal_emit(manager, signals[SIG_ADDED], 0, conversation);
+	g_list_model_items_changed(G_LIST_MODEL(manager), position, 0, 1);
 
 	return TRUE;
 }
@@ -565,6 +569,7 @@
 
 	/* Tell everyone about the removed conversation. */
 	g_signal_emit(manager, signals[SIG_REMOVED], 0, conversation);
+	g_list_model_items_changed(G_LIST_MODEL(manager), position, 1, 0);
 
 	g_object_unref(conversation);
 
--- a/libpurple/tests/test_conversation_manager.c	Sun Nov 03 00:05:44 2024 -0500
+++ b/libpurple/tests/test_conversation_manager.c	Sun Nov 03 00:07:13 2024 -0500
@@ -36,6 +36,21 @@
 	*counter = *counter + 1;
 }
 
+static void
+test_purple_conversation_manager_items_changed_cb(GListModel *model,
+                                                  G_GNUC_UNUSED guint position,
+                                                  G_GNUC_UNUSED guint removed,
+                                                  G_GNUC_UNUSED guint added,
+                                                  gpointer data)
+{
+	guint *counter = data;
+
+	g_assert_true(PURPLE_IS_CONVERSATION_MANAGER(model));
+	g_assert_true(G_IS_LIST_MODEL(model));
+
+	*counter = *counter + 1;
+}
+
 /******************************************************************************
  * Tests
  *****************************************************************************/
@@ -76,9 +91,9 @@
 	PurpleAccount *account = NULL;
 	PurpleConversationManager *manager = NULL;
 	PurpleConversation *conversation = NULL;
-	GList *conversations = NULL;
 	guint added_counter = 0;
 	guint removed_counter = 0;
+	guint changed_counter = 0;
 	gboolean result = FALSE;
 
 	manager = g_object_new(PURPLE_TYPE_CONVERSATION_MANAGER, NULL);
@@ -92,6 +107,9 @@
 	g_signal_connect(manager, "removed",
 	                 G_CALLBACK(test_purple_conversation_manager_counter_cb),
 	                 &removed_counter);
+	g_signal_connect(manager, "items-changed",
+	                 G_CALLBACK(test_purple_conversation_manager_items_changed_cb),
+	                 &changed_counter);
 
 	/* Create our account. */
 	account = purple_account_new("test", "test");
@@ -103,27 +121,31 @@
 		NULL);
 
 	/* Add the conversation to the manager. */
+	added_counter = 0;
+	removed_counter = 0;
+	changed_counter = 0;
 	result = purple_conversation_manager_add(manager, conversation);
 	g_assert_true(result);
-
-	/* Make sure the conversation is available. */
-	conversations = purple_conversation_manager_get_all(manager);
-	g_assert_nonnull(conversations);
-	g_assert_cmpuint(g_list_length(conversations), ==, 1);
-	g_list_free(conversations);
-
-	/* Make sure the registered signal was called. */
 	g_assert_cmpuint(added_counter, ==, 1);
+	g_assert_cmpuint(removed_counter, ==, 0);
+	g_assert_cmpuint(changed_counter, ==, 1);
+	g_assert_cmpuint(g_list_model_get_n_items(G_LIST_MODEL(manager)), ==, 1);
 
 	/* Remove the contact. */
+	added_counter = 0;
+	removed_counter = 0;
+	changed_counter = 0;
 	result = purple_conversation_manager_remove(manager, conversation);
 	g_assert_true(result);
+	g_assert_cmpuint(added_counter, ==, 0);
 	g_assert_cmpuint(removed_counter, ==, 1);
+	g_assert_cmpuint(changed_counter, ==, 1);
+	g_assert_cmpuint(g_list_model_get_n_items(G_LIST_MODEL(manager)), ==, 0);
 
 	/* Clean up.*/
-	g_clear_object(&conversation);
-	g_clear_object(&account);
-	g_clear_object(&manager);
+	g_assert_finalize_object(conversation);
+	g_assert_finalize_object(account);
+	g_assert_finalize_object(manager);
 }
 
 /******************************************************************************

mercurial