Mon, 28 Nov 2022 20:37:34 -0600
Add purple_tags_add_with_value to help people not have to construct their own tags
Testing Done:
Ran the unit tests.
Reviewed at https://reviews.imfreedom.org/r/2102/
--- a/libpurple/purpletags.c Sat Nov 26 04:13:38 2022 -0600 +++ b/libpurple/purpletags.c Mon Nov 28 20:37:34 2022 -0600 @@ -126,6 +126,26 @@ tags->tags = g_list_append(tags->tags, g_strdup(tag)); } +void +purple_tags_add_with_value(PurpleTags *tags, const char *name, + const char *value) +{ + char *tag = NULL; + + g_return_if_fail(PURPLE_IS_TAGS(tags)); + g_return_if_fail(name != NULL); + + if(value != NULL) { + tag = g_strdup_printf("%s:%s", name, value); + } else { + tag = g_strdup(name); + } + + purple_tags_add(tags, tag); + + g_free(tag); +} + gboolean purple_tags_remove(PurpleTags *tags, const gchar *tag) { g_return_val_if_fail(PURPLE_IS_TAGS(tags), FALSE);
--- a/libpurple/purpletags.h Sat Nov 26 04:13:38 2022 -0600 +++ b/libpurple/purpletags.h Mon Nov 28 20:37:34 2022 -0600 @@ -103,6 +103,19 @@ void purple_tags_add(PurpleTags *tags, const gchar *tag); /** + * purple_tags_add_with_value: + * @tags: The instance. + * @name: The name of the tag. + * @value: (nullable): The value of the tag. + * + * Formats @name and @value into a tag and adds it to @tags. If the tag already + * exists, the existing tag will be replaced. + * + * Since: 3.0.0 + */ +void purple_tags_add_with_value(PurpleTags *tags, const char *name, const char *value); + +/** * purple_tags_remove: * @tags: The instance. * @tag: The tag data.
--- a/libpurple/tests/test_tags.c Sat Nov 26 04:13:38 2022 -0600 +++ b/libpurple/tests/test_tags.c Mon Nov 28 20:37:34 2022 -0600 @@ -128,6 +128,38 @@ } static void +test_purple_tags_add_with_value(void) { + PurpleTags *tags = purple_tags_new(); + const char *value = NULL; + gboolean found = FALSE; + + purple_tags_add_with_value(tags, "tag1", "purple"); + g_assert_cmpuint(purple_tags_get_count(tags), ==, 1); + + value = purple_tags_lookup(tags, "tag1", &found); + g_assert_cmpstr(value, ==, "purple"); + g_assert_true(found); + + g_clear_object(&tags); +} + +static void +test_purple_tags_add_with_value_null(void) { + PurpleTags *tags = purple_tags_new(); + const char *value = NULL; + gboolean found = FALSE; + + purple_tags_add_with_value(tags, "tag1", NULL); + g_assert_cmpuint(purple_tags_get_count(tags), ==, 1); + + value = purple_tags_lookup(tags, "tag1", &found); + g_assert_null(value); + g_assert_true(found); + + g_clear_object(&tags); +} + +static void test_purple_tags_remove_non_existent_with_value(void) { PurpleTags *tags = purple_tags_new(); @@ -303,6 +335,11 @@ g_test_add_func("/tags/remove-non-existent-bare", test_purple_tags_remove_non_existent_bare); + g_test_add_func("/tags/add-with-value", + test_purple_tags_add_with_value); + g_test_add_func("/tags/add-with-value-null", + test_purple_tags_add_with_value_null); + g_test_add_func("/tags/add-remove-with-value", test_purple_tags_add_remove_with_value); g_test_add_func("/tags/add-duplicate-with-value",