pidgin/pidgin

Parents 22f777ef5415
Children a3b862b8dcde
Add some "is" methods to PurpleConversation to make type checking easier

This just simplifies having to call purple_conversation_get_conversation_type
and checking it against a constant with a long name.

Testing Done:
Ran the unit tests under valgrind.

Reviewed at https://reviews.imfreedom.org/r/3052/
--- a/libpurple/purpleconversation.c Tue Apr 09 23:06:58 2024 -0500
+++ b/libpurple/purpleconversation.c Tue Apr 09 23:11:12 2024 -0500
@@ -1035,6 +1035,50 @@
/******************************************************************************
* Public API
*****************************************************************************/
+gboolean
+purple_conversation_is_dm(PurpleConversation *conversation) {
+ PurpleConversationPrivate *priv = NULL;
+
+ g_return_val_if_fail(PURPLE_IS_CONVERSATION(conversation), FALSE);
+
+ priv = purple_conversation_get_instance_private(conversation);
+
+ return priv->type == PURPLE_CONVERSATION_TYPE_DM;
+}
+
+gboolean
+purple_conversation_is_group_dm(PurpleConversation *conversation) {
+ PurpleConversationPrivate *priv = NULL;
+
+ g_return_val_if_fail(PURPLE_IS_CONVERSATION(conversation), FALSE);
+
+ priv = purple_conversation_get_instance_private(conversation);
+
+ return priv->type == PURPLE_CONVERSATION_TYPE_GROUP_DM;
+}
+
+gboolean
+purple_conversation_is_channel(PurpleConversation *conversation) {
+ PurpleConversationPrivate *priv = NULL;
+
+ g_return_val_if_fail(PURPLE_IS_CONVERSATION(conversation), FALSE);
+
+ priv = purple_conversation_get_instance_private(conversation);
+
+ return priv->type == PURPLE_CONVERSATION_TYPE_CHANNEL;
+}
+
+gboolean
+purple_conversation_is_thread(PurpleConversation *conversation) {
+ PurpleConversationPrivate *priv = NULL;
+
+ g_return_val_if_fail(PURPLE_IS_CONVERSATION(conversation), FALSE);
+
+ priv = purple_conversation_get_instance_private(conversation);
+
+ return priv->type == PURPLE_CONVERSATION_TYPE_THREAD;
+}
+
void
purple_conversation_present(PurpleConversation *conv) {
PurpleConversationUiOps *ops;
--- a/libpurple/purpleconversation.h Tue Apr 09 23:06:58 2024 -0500
+++ b/libpurple/purpleconversation.h Tue Apr 09 23:11:12 2024 -0500
@@ -162,6 +162,70 @@
G_BEGIN_DECLS
/**
+ * purple_conversation_is_dm:
+ * @conversation: The instance.
+ *
+ * Checks if @conversation is a direct message or not.
+ *
+ * This is a quick helper around manually checking the results of
+ * [method@Conversation.get_conversation_type].
+ *
+ * Returns: %TRUE if @conversation is a direct message, otherwise %FALSE.
+ *
+ * Since: 3.0
+ */
+PURPLE_AVAILABLE_IN_3_0
+gboolean purple_conversation_is_dm(PurpleConversation *conversation);
+
+/**
+ * purple_conversation_is_group_dm:
+ * @conversation: The instance.
+ *
+ * Checks if @conversation is a group direct message or not.
+ *
+ * This is a quick helper around manually checking the results of
+ * [method@Conversation.get_conversation_type].
+ *
+ * Returns: %TRUE if @conversation is a group direct message, otherwise %FALSE.
+ *
+ * Since: 3.0
+ */
+PURPLE_AVAILABLE_IN_3_0
+gboolean purple_conversation_is_group_dm(PurpleConversation *conversation);
+
+/**
+ * purple_conversation_is_channel:
+ * @conversation: The instance.
+ *
+ * Checks if @conversation is a channel or not.
+ *
+ * This is a quick helper around manually checking the results of
+ * [method@Conversation.get_conversation_type].
+ *
+ * Returns: %TRUE if @conversation is a channel, otherwise %FALSE.
+ *
+ * Since: 3.0
+ */
+PURPLE_AVAILABLE_IN_3_0
+gboolean purple_conversation_is_channel(PurpleConversation *conversation);
+
+/**
+ * purple_conversation_is_thread:
+ * @conversation: The instance.
+ *
+ * Checks if @conversation is a thread or not.
+ *
+ * This is a quick helper around manually checking the results of
+ * [method@Conversation.get_conversation_type].
+ *
+ * Returns: %TRUE if @conversation is a thread, otherwise %FALSE.
+ *
+ * Since: 3.0
+ */
+PURPLE_AVAILABLE_IN_3_0
+gboolean purple_conversation_is_thread(PurpleConversation *conversation);
+
+/**
* purple_conversation_present:
* @conv: The conversation to present
*
--- a/libpurple/tests/test_conversation.c Tue Apr 09 23:06:58 2024 -0500
+++ b/libpurple/tests/test_conversation.c Tue Apr 09 23:11:12 2024 -0500
@@ -203,6 +203,105 @@
}
/******************************************************************************
+ * "is" tests
+ *****************************************************************************/
+static void
+test_purple_conversation_is_dm(void) {
+ PurpleAccount *account = NULL;
+ PurpleConversation *conversation = NULL;
+
+ account = purple_account_new("test", "test");
+ conversation = g_object_new(
+ PURPLE_TYPE_CONVERSATION,
+ "account", account,
+ "type", PURPLE_CONVERSATION_TYPE_DM,
+ "name", "this is required for some reason",
+ NULL);
+
+ g_assert_true(PURPLE_IS_CONVERSATION(conversation));
+ g_assert_true(purple_conversation_is_dm(conversation));
+ g_assert_false(purple_conversation_is_group_dm(conversation));
+ g_assert_false(purple_conversation_is_channel(conversation));
+ g_assert_false(purple_conversation_is_thread(conversation));
+
+ g_assert_finalize_object(conversation);
+
+ g_clear_object(&account);
+}
+
+static void
+test_purple_conversation_is_group_dm(void) {
+ PurpleAccount *account = NULL;
+ PurpleConversation *conversation = NULL;
+
+ account = purple_account_new("test", "test");
+ conversation = g_object_new(
+ PURPLE_TYPE_CONVERSATION,
+ "account", account,
+ "type", PURPLE_CONVERSATION_TYPE_GROUP_DM,
+ "name", "this is required for some reason",
+ NULL);
+
+ g_assert_true(PURPLE_IS_CONVERSATION(conversation));
+ g_assert_false(purple_conversation_is_dm(conversation));
+ g_assert_true(purple_conversation_is_group_dm(conversation));
+ g_assert_false(purple_conversation_is_channel(conversation));
+ g_assert_false(purple_conversation_is_thread(conversation));
+
+ g_assert_finalize_object(conversation);
+
+ g_clear_object(&account);
+}
+
+static void
+test_purple_conversation_is_channel(void) {
+ PurpleAccount *account = NULL;
+ PurpleConversation *conversation = NULL;
+
+ account = purple_account_new("test", "test");
+ conversation = g_object_new(
+ PURPLE_TYPE_CONVERSATION,
+ "account", account,
+ "type", PURPLE_CONVERSATION_TYPE_CHANNEL,
+ "name", "this is required for some reason",
+ NULL);
+
+ g_assert_true(PURPLE_IS_CONVERSATION(conversation));
+ g_assert_false(purple_conversation_is_dm(conversation));
+ g_assert_false(purple_conversation_is_group_dm(conversation));
+ g_assert_true(purple_conversation_is_channel(conversation));
+ g_assert_false(purple_conversation_is_thread(conversation));
+
+ g_assert_finalize_object(conversation);
+
+ g_clear_object(&account);
+}
+
+static void
+test_purple_conversation_is_thread(void) {
+ PurpleAccount *account = NULL;
+ PurpleConversation *conversation = NULL;
+
+ account = purple_account_new("test", "test");
+ conversation = g_object_new(
+ PURPLE_TYPE_CONVERSATION,
+ "account", account,
+ "type", PURPLE_CONVERSATION_TYPE_THREAD,
+ "name", "this is required for some reason",
+ NULL);
+
+ g_assert_true(PURPLE_IS_CONVERSATION(conversation));
+ g_assert_false(purple_conversation_is_dm(conversation));
+ g_assert_false(purple_conversation_is_group_dm(conversation));
+ g_assert_false(purple_conversation_is_channel(conversation));
+ g_assert_true(purple_conversation_is_thread(conversation));
+
+ g_assert_finalize_object(conversation);
+
+ g_clear_object(&account);
+}
+
+/******************************************************************************
* Membership tests and helpers
*****************************************************************************/
static void
@@ -364,6 +463,14 @@
g_test_add_func("/conversation/set-topic-full",
test_purple_conversation_set_topic_full);
+ g_test_add_func("/conversation/is-dm", test_purple_conversation_is_dm);
+ g_test_add_func("/conversation/is-group-dm",
+ test_purple_conversation_is_group_dm);
+ g_test_add_func("/conversation/is-channel",
+ test_purple_conversation_is_channel);
+ g_test_add_func("/conversation/is-thread",
+ test_purple_conversation_is_thread);
+
g_test_add_func("/conversation/members/add-remove",
test_purple_conversation_members_add_remove);