Add set_avatar_async and set_avatar_finish to PurpleProtocolConversation

Fri, 13 Oct 2023 02:34:55 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Fri, 13 Oct 2023 02:34:55 -0500
changeset 42354
b5caf6b7705a
parent 42353
5381c5c9affa
child 42355
6f7c6b84aaa0

Add set_avatar_async and set_avatar_finish to PurpleProtocolConversation

This allows end users to change the avatar for a conversation.

Testing Done:
Ran the unit tests under valgrind.

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

libpurple/purpleprotocolconversation.c file | annotate | diff | comparison | revisions
libpurple/purpleprotocolconversation.h file | annotate | diff | comparison | revisions
libpurple/tests/test_protocol_conversation.c file | annotate | diff | comparison | revisions
--- a/libpurple/purpleprotocolconversation.c	Thu Oct 12 22:56:49 2023 -0500
+++ b/libpurple/purpleprotocolconversation.c	Fri Oct 13 02:34:55 2023 -0500
@@ -189,3 +189,47 @@
 
 	return FALSE;
 }
+
+void
+purple_protocol_conversation_set_avatar_async(PurpleProtocolConversation *protocol,
+                                              PurpleConversation *conversation,
+                                              PurpleAvatar *avatar,
+                                              GCancellable *cancellable,
+                                              GAsyncReadyCallback callback,
+                                              gpointer data)
+{
+	PurpleProtocolConversationInterface *iface = NULL;
+
+	g_return_if_fail(PURPLE_IS_PROTOCOL_CONVERSATION(protocol));
+	g_return_if_fail(PURPLE_IS_CONVERSATION(conversation));
+
+	iface = PURPLE_PROTOCOL_CONVERSATION_GET_IFACE(protocol);
+	if(iface != NULL && iface->set_avatar_async != NULL) {
+		iface->set_avatar_async(protocol, conversation, avatar, cancellable,
+		                        callback, data);
+	} else {
+		g_warning("%s does not implement set_avatar_async",
+		          G_OBJECT_TYPE_NAME(protocol));
+	}
+}
+
+gboolean
+purple_protocol_conversation_set_avatar_finish(PurpleProtocolConversation *protocol,
+                                               GAsyncResult *result,
+                                               GError **error)
+{
+	PurpleProtocolConversationInterface *iface = NULL;
+
+	g_return_val_if_fail(PURPLE_IS_PROTOCOL_CONVERSATION(protocol), FALSE);
+	g_return_val_if_fail(G_IS_ASYNC_RESULT(result), FALSE);
+
+	iface = PURPLE_PROTOCOL_CONVERSATION_GET_IFACE(protocol);
+	if(iface != NULL && iface->set_avatar_finish != NULL) {
+		return iface->set_avatar_finish(protocol, result, error);
+	}
+
+	g_warning("%s does not implement set_avatar_finish",
+	          G_OBJECT_TYPE_NAME(protocol));
+
+	return FALSE;
+}
--- a/libpurple/purpleprotocolconversation.h	Thu Oct 12 22:56:49 2023 -0500
+++ b/libpurple/purpleprotocolconversation.h	Fri Oct 13 02:34:55 2023 -0500
@@ -31,6 +31,7 @@
 #include <glib-object.h>
 
 #include "purpleaccount.h"
+#include "purpleavatar.h"
 #include "purplechanneljoindetails.h"
 #include "purpleconversation.h"
 #include "purplemessage.h"
@@ -66,6 +67,9 @@
 	void (*join_channel_async)(PurpleProtocolConversation *protocol, PurpleAccount *account, PurpleChannelJoinDetails *details, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);
 	gboolean (*join_channel_finish)(PurpleProtocolConversation *protocol, GAsyncResult *result, GError **error);
 
+	void (*set_avatar_async)(PurpleProtocolConversation *protocol, PurpleConversation *conversation, PurpleAvatar *avatar, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);
+	gboolean (*set_avatar_finish)(PurpleProtocolConversation *protocol, GAsyncResult *result, GError **error);
+
 	/*< private >*/
 	gpointer reserved[8];
 };
@@ -199,6 +203,42 @@
  */
 gboolean purple_protocol_conversation_join_channel_finish(PurpleProtocolConversation *protocol, GAsyncResult *result, GError **error);
 
+/**
+ * purple_protocol_conversation_set_avatar_async:
+ * @protocol: The instance.
+ * @conversation: The conversation instance.
+ * @avatar: (nullable): The new avatar.
+ * @cancellable: (nullable): optional GCancellable object, %NULL to ignore.
+ * @callback: (nullable) (scope async): The callback to call after the message
+ *            has been sent.
+ * @data: (nullable): Optional user data to pass to @callback.
+ *
+ * Sets the avatar for @conversation to @pixbuf. Pass %NULL to clear the
+ * current avatar.
+ *
+ * Since: 3.0.0
+ */
+void purple_protocol_conversation_set_avatar_async(PurpleProtocolConversation *protocol, PurpleConversation *conversation, PurpleAvatar *avatar, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);
+
+/**
+ * purple_protocol_conversation_set_avatar_finish:
+ * @protocol: The instance.
+ * @result: The [iface@Gio.AsyncResult] from the previous
+ *          [method@ProtocolConversation.set_avatar_async] call.
+ * @error: Return address for a #GError, or %NULL.
+ *
+ * Finishes a previous call to
+ * [method@ProtocolConversation.set_avatar_async]. This should be called from
+ * the callback of that function to get the result of whether or not the
+ * avatar was set successfully.
+ *
+ * Returns: %TRUE if the avatar was set successfully, otherwise %FALSE with
+ *          @error possibly set.
+ *
+ * Since: 3.0.0
+ */
+gboolean purple_protocol_conversation_set_avatar_finish(PurpleProtocolConversation *protocol, GAsyncResult *result, GError **error);
+
 G_END_DECLS
 
 #endif /* PURPLE_PROTOCOL_CONVERSATION_H */
--- a/libpurple/tests/test_protocol_conversation.c	Thu Oct 12 22:56:49 2023 -0500
+++ b/libpurple/tests/test_protocol_conversation.c	Fri Oct 13 02:34:55 2023 -0500
@@ -22,6 +22,8 @@
 
 #include <glib.h>
 
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
 #include <purple.h>
 
 #include "test_ui.h"
@@ -201,7 +203,6 @@
 	g_test_trap_assert_stderr("*Purple-WARNING*TestPurpleProtocolConversationEmpty*set_topic_finish*");
 }
 
-
 static void
 test_purple_protocol_conversation_empty_get_channel_join_details(void) {
 	if(g_test_subprocess()) {
@@ -278,6 +279,62 @@
 	g_test_trap_assert_stderr("*Purple-WARNING*TestPurpleProtocolConversationEmpty*join_channel_finish*");
 }
 
+static void
+test_purple_protocol_conversation_empty_set_avatar_async(void) {
+	if(g_test_subprocess()) {
+		PurpleAccount *account = NULL;
+		PurpleConversation *conversation = NULL;
+		PurpleProtocolConversation *protocol = NULL;
+
+		protocol = g_object_new(test_purple_protocol_conversation_empty_get_type(),
+		                        NULL);
+		account = purple_account_new("test", "test");
+		conversation = g_object_new(
+			PURPLE_TYPE_CONVERSATION,
+			"account", account,
+			"name", "this is required at the moment",
+			"type", PurpleConversationTypeDM,
+			NULL);
+
+		purple_protocol_conversation_set_avatar_async(protocol, conversation,
+		                                              NULL, NULL, NULL, NULL);
+
+		g_clear_object(&account);
+		g_clear_object(&conversation);
+		g_clear_object(&protocol);
+	}
+
+	g_test_trap_subprocess(NULL, 0, 0);
+	g_test_trap_assert_stderr("*Purple-WARNING*TestPurpleProtocolConversationEmpty*set_avatar_async*");
+}
+
+static void
+test_purple_protocol_conversation_empty_set_avatar_finish(void) {
+	if(g_test_subprocess()) {
+		PurpleProtocolConversation *protocol = NULL;
+		GError *error = NULL;
+		GTask *task = NULL;
+		gboolean result = FALSE;
+
+		protocol = g_object_new(test_purple_protocol_conversation_empty_get_type(),
+		                        NULL);
+
+		task = g_task_new(protocol, NULL, NULL, NULL);
+
+		result = purple_protocol_conversation_set_avatar_finish(protocol,
+		                                                        G_ASYNC_RESULT(task),
+		                                                        &error);
+		g_assert_no_error(error);
+		g_assert_false(result);
+
+		g_clear_object(&task);
+		g_clear_object(&protocol);
+	}
+
+	g_test_trap_subprocess(NULL, 0, 0);
+	g_test_trap_assert_stderr("*Purple-WARNING*TestPurpleProtocolConversationEmpty*set_avatar_finish*");
+}
+
 /******************************************************************************
  * TestProtocolConversation Implementation
  *****************************************************************************/
@@ -299,6 +356,9 @@
 	guint get_channel_join_details;
 	guint join_channel_async;
 	guint join_channel_finish;
+
+	guint set_avatar_async;
+	guint set_avatar_finish;
 };
 
 static void
@@ -442,6 +502,46 @@
 	return g_task_propagate_boolean(G_TASK(result), error);
 }
 
+static void
+test_purple_protocol_conversation_set_avatar_async(PurpleProtocolConversation *protocol,
+                                                   PurpleConversation *conversation,
+                                                   G_GNUC_UNUSED PurpleAvatar *avatar,
+                                                   GCancellable *cancellable,
+                                                   GAsyncReadyCallback callback,
+                                                   gpointer data)
+{
+	TestPurpleProtocolConversation *test_protocol = NULL;
+	GTask *task = NULL;
+
+	g_assert_true(PURPLE_IS_CONVERSATION(conversation));
+
+	test_protocol = TEST_PURPLE_PROTOCOL_CONVERSATION(protocol);
+	test_protocol->set_avatar_async += 1;
+
+	task = g_task_new(protocol, cancellable, callback, data);
+	if(test_protocol->should_error) {
+		GError *error = g_error_new_literal(TEST_PURPLE_PROTOCOL_CONVERSATION_DOMAIN,
+		                                    0, "error");
+		g_task_return_error(task, error);
+	} else {
+		g_task_return_boolean(task, TRUE);
+	}
+
+	g_clear_object(&task);
+}
+
+static gboolean
+test_purple_protocol_conversation_set_avatar_finish(PurpleProtocolConversation *protocol,
+                                                    GAsyncResult *result,
+                                                    GError **error)
+{
+	TestPurpleProtocolConversation *test_protocol = NULL;
+
+	test_protocol = TEST_PURPLE_PROTOCOL_CONVERSATION(protocol);
+	test_protocol->set_avatar_finish += 1;
+
+	return g_task_propagate_boolean(G_TASK(result), error);
+}
 
 static void
 test_purple_protocol_conversation_iface_init(PurpleProtocolConversationInterface *iface) {
@@ -454,6 +554,9 @@
 	iface->get_channel_join_details = test_purple_protocol_conversation_get_channel_join_details;
 	iface->join_channel_async = test_purple_protocol_conversation_join_channel_async;
 	iface->join_channel_finish = test_purple_protocol_conversation_join_channel_finish;
+
+	iface->set_avatar_async = test_purple_protocol_conversation_set_avatar_async;
+	iface->set_avatar_finish = test_purple_protocol_conversation_set_avatar_finish;
 }
 
 G_DEFINE_TYPE_WITH_CODE(TestPurpleProtocolConversation, test_purple_protocol_conversation,
@@ -473,6 +576,9 @@
 	protocol->get_channel_join_details = 0;
 	protocol->join_channel_async = 0;
 	protocol->join_channel_finish = 0;
+
+	protocol->set_avatar_async = 0;
+	protocol->set_avatar_finish = 0;
 }
 
 static void
@@ -738,6 +844,84 @@
 }
 
 /******************************************************************************
+ * TestProtocolConversation SetAvatar Tests
+ *****************************************************************************/
+static void
+test_purple_protocol_conversation_set_avatar_cb(GObject *obj,
+                                                GAsyncResult *res,
+                                                G_GNUC_UNUSED gpointer data)
+{
+	TestPurpleProtocolConversation *test_protocol = NULL;
+	PurpleProtocolConversation *protocol = NULL;
+	GError *error = NULL;
+	gboolean result = FALSE;
+
+	protocol = PURPLE_PROTOCOL_CONVERSATION(obj);
+	test_protocol = TEST_PURPLE_PROTOCOL_CONVERSATION(obj);
+
+	result = purple_protocol_conversation_set_avatar_finish(protocol, res,
+	                                                        &error);
+
+	if(test_protocol->should_error) {
+		g_assert_error(error, TEST_PURPLE_PROTOCOL_CONVERSATION_DOMAIN, 0);
+		g_clear_error(&error);
+		g_assert_false(result);
+	} else {
+		g_assert_no_error(error);
+		g_assert_true(result);
+	}
+
+	g_main_loop_quit(loop);
+}
+
+static gboolean
+test_purple_protocol_conversation_set_avatar_idle(gpointer data) {
+	PurpleAccount *account = NULL;
+	PurpleConversation *conversation = NULL;
+	PurpleProtocolConversation *protocol = data;
+
+	account = purple_account_new("test", "test");
+	g_object_set_data_full(G_OBJECT(protocol), "account", account,
+	                       g_object_unref);
+
+	conversation = g_object_new(
+		PURPLE_TYPE_CONVERSATION,
+		"account", account,
+		"name", "this is required at the moment",
+		"type", PurpleConversationTypeDM,
+		NULL);
+	g_object_set_data_full(G_OBJECT(protocol), "conversation", conversation,
+	                       g_object_unref);
+
+	purple_protocol_conversation_set_avatar_async(protocol, conversation,
+	                                              NULL, NULL,
+	                                              test_purple_protocol_conversation_set_avatar_cb,
+	                                              NULL);
+
+	return G_SOURCE_REMOVE;
+}
+
+static void
+test_purple_protocol_conversation_set_avatar_normal(gconstpointer data) {
+	TestPurpleProtocolConversation *protocol = NULL;
+
+	protocol = g_object_new(test_purple_protocol_conversation_get_type(),
+	                        NULL);
+	protocol->should_error = GPOINTER_TO_INT(data);
+
+	g_idle_add(test_purple_protocol_conversation_set_avatar_idle, protocol);
+	g_timeout_add_seconds(10, test_purple_protocol_conversation_timeout_cb,
+	                      loop);
+
+	g_main_loop_run(loop);
+
+	g_assert_cmpuint(protocol->set_avatar_async, ==, 1);
+	g_assert_cmpuint(protocol->set_avatar_finish, ==, 1);
+
+	g_clear_object(&protocol);
+}
+
+/******************************************************************************
  * Main
  *****************************************************************************/
 gint
@@ -759,6 +943,10 @@
 	                test_purple_protocol_conversation_empty_set_topic_async);
 	g_test_add_func("/protocol-conversation/empty/set-topic-finish",
 	                test_purple_protocol_conversation_empty_set_topic_finish);
+	g_test_add_func("/protocol-conversation/empty/set-avatar-async",
+	                test_purple_protocol_conversation_empty_set_avatar_async);
+	g_test_add_func("/protocol-conversation/empty/set-avatar-finish",
+	                test_purple_protocol_conversation_empty_set_avatar_finish);
 
 	/* Empty join channel tests. */
 	g_test_add_func("/protocol-conversation/empty/get-channel-join-details",
@@ -775,12 +963,6 @@
 	g_test_add_data_func("/protocol-conversation/normal/send-message-error",
 	                     GINT_TO_POINTER(TRUE),
 	                     test_purple_protocol_conversation_send_message_normal);
-	g_test_add_data_func("/protocol-contacts/normal/set-topic-normal",
-	                     GINT_TO_POINTER(FALSE),
-	                     test_purple_protocol_conversation_set_topic_normal);
-	g_test_add_data_func("/protocol-contacts/normal/set-topic-error",
-	                     GINT_TO_POINTER(TRUE),
-	                     test_purple_protocol_conversation_set_topic_normal);
 
 	/* Normal join channel tests. */
 	g_test_add_func("/protocol-conversation/normal/get-channel-join-details",
@@ -792,6 +974,22 @@
 	                     GINT_TO_POINTER(TRUE),
 	                     test_purple_protocol_conversation_join_channel_normal);
 
+	/* Normal set topic tests. */
+	g_test_add_data_func("/protocol-contacts/normal/set-topic-normal",
+	                     GINT_TO_POINTER(FALSE),
+	                     test_purple_protocol_conversation_set_topic_normal);
+	g_test_add_data_func("/protocol-contacts/normal/set-topic-error",
+	                     GINT_TO_POINTER(TRUE),
+	                     test_purple_protocol_conversation_set_topic_normal);
+
+	/* Normal set avatar tests. */
+	g_test_add_data_func("/protocol-contacts/normal/set-avatar-normal",
+	                     GINT_TO_POINTER(FALSE),
+	                     test_purple_protocol_conversation_set_avatar_normal);
+	g_test_add_data_func("/protocol-contacts/normal/set-avatar-error",
+	                     GINT_TO_POINTER(TRUE),
+	                     test_purple_protocol_conversation_set_avatar_normal);
+
 	ret = g_test_run();
 
 	g_main_loop_unref(loop);

mercurial