Mon, 10 Jul 2017 03:17:11 +0000
Merged in rw_grim/pidgin (pull request #203)
Add path as a property to PurpleImage and have smiley's pass it in
Approved-by: Eion Robb <eionrobb@gmail.com>
Approved-by: Jakub Adam <jakub.adam@ktknet.cz>
Approved-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Approved-by: Mike Ruprecht <cmaiku@gmail.com>
--- a/libpurple/image.c Mon Jul 10 03:15:30 2017 +0000 +++ b/libpurple/image.c Mon Jul 10 03:17:11 2017 +0000 @@ -41,6 +41,7 @@ enum { PROP_0, + PROP_PATH, PROP_CONTENTS, PROP_SIZE, PROP_LAST @@ -52,6 +53,15 @@ * Helpers ******************************************************************************/ static void +_purple_image_set_path(PurpleImage *image, const gchar *path) { + PurpleImagePrivate *priv = PURPLE_IMAGE_GET_PRIVATE(image); + + g_free(priv->path); + + priv->path = g_strdup(path); +} + +static void _purple_image_set_contents(PurpleImage *image, GBytes *bytes) { PurpleImagePrivate *priv = PURPLE_IMAGE_GET_PRIVATE(image); @@ -91,6 +101,9 @@ PurpleImage *image = PURPLE_IMAGE(obj); switch (param_id) { + case PROP_PATH: + _purple_image_set_path(image, g_value_get_string(value)); + break; case PROP_CONTENTS: _purple_image_set_contents(image, g_value_get_boxed(value)); break; @@ -107,6 +120,9 @@ PurpleImage *image = PURPLE_IMAGE(obj); switch (param_id) { + case PROP_PATH: + g_value_set_string(value, purple_image_get_path(image)); + break; case PROP_CONTENTS: g_value_set_boxed(value, purple_image_get_contents(image)); break; @@ -127,6 +143,14 @@ gobj_class->get_property = purple_image_get_property; gobj_class->set_property = purple_image_set_property; + properties[PROP_PATH] = g_param_spec_string( + "path", + "path", + "The filepath for the image if one was provided", + NULL, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS + ); + properties[PROP_CONTENTS] = g_param_spec_boxed( "contents", "contents", @@ -173,7 +197,12 @@ bytes = g_bytes_new_take(contents, length); - image = purple_image_new_from_bytes(bytes); + image = g_object_new( + PURPLE_TYPE_IMAGE, + "contents", bytes, + "path", path, + NULL + ); g_bytes_unref(bytes); @@ -252,7 +281,7 @@ g_return_val_if_fail(priv != NULL, NULL); - return priv->path; + return priv->path ? priv->path : purple_image_generate_filename(image); } gsize @@ -347,28 +376,34 @@ const gchar * purple_image_generate_filename(PurpleImage *image) { - PurpleImagePrivate *priv = PURPLE_IMAGE_GET_PRIVATE(image); + PurpleImagePrivate *priv = NULL; gconstpointer data; gsize len; - const gchar *ext; + const gchar *ext = NULL; gchar *checksum; - g_return_val_if_fail(priv != NULL, NULL); + g_return_val_if_fail(PURPLE_IS_IMAGE(image), NULL); + + priv = PURPLE_IMAGE_GET_PRIVATE(image); if (priv->gen_filename) return priv->gen_filename; - ext = purple_image_get_extension(image); + /* grab the image's data and size of that data */ data = purple_image_get_data(image); len = purple_image_get_data_size(image); - g_return_val_if_fail(ext != NULL, NULL); - g_return_val_if_fail(data != NULL, NULL); - g_return_val_if_fail(len > 0, NULL); + /* create a checksum of it and use it as the start of our filename */ + checksum = g_compute_checksum_for_data(G_CHECKSUM_SHA1, data, len); - checksum = g_compute_checksum_for_data(G_CHECKSUM_SHA1, data, len); - priv->gen_filename = g_strdup_printf("%s.%s", checksum, ext); - g_free(checksum); + /* if the image has a known format, set the extension appropriately */ + ext = purple_image_get_extension(image); + if(ext != NULL) { + priv->gen_filename = g_strdup_printf("%s.%s", checksum, ext); + g_free(checksum); + } else { + priv->gen_filename = checksum; + } return priv->gen_filename; }
--- a/libpurple/smiley.c Mon Jul 10 03:15:30 2017 +0000 +++ b/libpurple/smiley.c Mon Jul 10 03:17:11 2017 +0000 @@ -146,6 +146,7 @@ smiley = g_object_new( PURPLE_TYPE_SMILEY, + "path", path, "contents", bytes, "shortcut", shortcut, NULL
--- a/libpurple/tests/test_image.c Mon Jul 10 03:15:30 2017 +0000 +++ b/libpurple/tests/test_image.c Mon Jul 10 03:17:11 2017 +0000 @@ -58,6 +58,7 @@ _test_image(PurpleImage *image, const guint8 *edata, gsize elen, + const gchar *path, const gchar *ext, const gchar *mimetype) { @@ -72,12 +73,45 @@ g_assert_cmpmem(adata, alen, edata, elen); g_bytes_unref(bytes); + /* if the caller provided a path, check it, otherwise just make sure we + * have something. + */ + if(path != NULL) { + g_assert_cmpstr(purple_image_get_path(image), ==, path); + } else { + const gchar *apath = purple_image_get_path(image); + + g_assert(apath); + g_assert_cmpstr(apath, !=, ""); + } + g_assert_cmpstr(purple_image_get_extension(image), ==, ext); g_assert_cmpstr(purple_image_get_mimetype(image), ==, mimetype); g_object_unref(G_OBJECT(image)); } +/****************************************************************************** + * Tests + *****************************************************************************/ +static void +test_image_new_from_bytes(void) { + GBytes *bytes = g_bytes_new(test_image_data, test_image_data_len); + PurpleImage *image = purple_image_new_from_bytes(bytes); + + _test_image( + image, + g_bytes_get_data(bytes, NULL), + g_bytes_get_size(bytes), + NULL, + "png", + "image/png" + ); + + g_bytes_unref(bytes); +} + + static void test_image_new_from_data(void) { PurpleImage *image = purple_image_new_from_data( @@ -89,6 +123,7 @@ image, test_image_data, test_image_data_len, + NULL, "png", "image/png" ); @@ -107,16 +142,18 @@ g_assert_no_error(error); g_file_get_contents(path, &edata, &elen, &error); - g_free(path); g_assert_no_error(error); _test_image( image, (guint8 *)edata, elen, + path, "png", "image/png" ); + + g_free(path); } /****************************************************************************** @@ -126,6 +163,11 @@ main(gint argc, gchar **argv) { g_test_init(&argc, &argv, NULL); + #if GLIB_CHECK_VERSION(2, 38, 0) + g_test_set_nonfatal_assertions(); + #endif /* GLIB_CHECK_VERSION(2, 38, 0) */ + + g_test_add_func("/image/new-from-bytes", test_image_new_from_bytes); g_test_add_func("/image/new-from-data", test_image_new_from_data); g_test_add_func("/image/new-from-file", test_image_new_from_file);
--- a/libpurple/tests/test_smiley.c Mon Jul 10 03:15:30 2017 +0000 +++ b/libpurple/tests/test_smiley.c Mon Jul 10 03:17:11 2017 +0000 @@ -56,6 +56,7 @@ *****************************************************************************/ static void _test_smiley(PurpleSmiley *smiley, + const gchar *path, const guint8 *edata, gsize elen, const gchar *ext, @@ -85,9 +86,15 @@ ); g_assert_cmpstr(purple_smiley_get_shortcut(smiley), ==, shortcut); + if(path) + g_assert_cmpstr(purple_image_get_path(PURPLE_IMAGE(smiley)), ==, path); + g_object_unref(G_OBJECT(smiley)); } +/****************************************************************************** + * Tests + *****************************************************************************/ static void test_smiley_new_from_data(void) { PurpleSmiley *smiley = purple_smiley_new_from_data( @@ -98,6 +105,7 @@ _test_smiley( smiley, + NULL, test_image_data, test_image_data_len, "png", @@ -119,17 +127,19 @@ g_assert_no_error(error); g_file_get_contents(path, &edata, &elen, &error); - g_free(path); g_assert_no_error(error); _test_smiley( smiley, + path, (guint8 *)edata, elen, "png", "image/png", "^_^" ); + + g_free(path); } /****************************************************************************** @@ -139,6 +149,10 @@ main(gint argc, gchar **argv) { g_test_init(&argc, &argv, NULL); + #if GLIB_CHECK_VERSION(2, 38, 0) + g_test_set_nonfatal_assertions(); + #endif /* GLIB_CHECK_VERSION(2, 38, 0) */ + g_test_add_func("/smiley/new-from-data", test_smiley_new_from_data); g_test_add_func("/smiley/new-from-file", test_smiley_new_from_file);
--- a/libpurple/tests/test_smiley_list.c Mon Jul 10 03:15:30 2017 +0000 +++ b/libpurple/tests/test_smiley_list.c Mon Jul 10 03:17:11 2017 +0000 @@ -87,6 +87,10 @@ main(gint argc, gchar **argv) { g_test_init(&argc, &argv, NULL); + #if GLIB_CHECK_VERSION(2, 38, 0) + g_test_set_nonfatal_assertions(); + #endif /* GLIB_CHECK_VERSION(2, 38, 0) */ + g_test_add_func("/smiley_list/new", test_smiley_list_new); g_test_add_func("/smiley_list/add-remove", test_smiley_list_add_remove);