diff options
author | Richard Hughes <richard@hughsie.com> | 2014-05-16 15:02:03 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2014-05-16 15:14:13 +0100 |
commit | e0748af5348fda0b61d158a7d5a7975296643e8e (patch) | |
tree | 34bda320d8dd95aad45d22e37118f8675612abaa /libappstream-glib | |
parent | 18a94a423484340a0e442b54bb89c819a68feb8f (diff) | |
download | appstream-glib-e0748af5348fda0b61d158a7d5a7975296643e8e.tar.gz |
Add as_image_get_basename()
Diffstat (limited to 'libappstream-glib')
-rw-r--r-- | libappstream-glib/as-image.c | 43 | ||||
-rw-r--r-- | libappstream-glib/as-image.h | 3 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 30 |
3 files changed, 76 insertions, 0 deletions
diff --git a/libappstream-glib/as-image.c b/libappstream-glib/as-image.c index 730da46..b2e88d3 100644 --- a/libappstream-glib/as-image.c +++ b/libappstream-glib/as-image.c @@ -44,6 +44,7 @@ struct _AsImagePrivate AsImageKind kind; gchar *url; gchar *md5; + gchar *basename; guint width; guint height; GdkPixbuf *pixbuf; @@ -66,6 +67,7 @@ as_image_finalize (GObject *object) g_object_unref (priv->pixbuf); g_free (priv->url); g_free (priv->md5); + g_free (priv->basename); G_OBJECT_CLASS (as_image_parent_class)->finalize (object); } @@ -147,6 +149,23 @@ as_image_get_url (AsImage *image) } /** + * as_image_get_basename: + * @image: a #AsImage instance. + * + * Gets the suggested basename the image, including file extension. + * + * Returns: filename + * + * Since: 0.1.6 + **/ +const gchar * +as_image_get_basename (AsImage *image) +{ + AsImagePrivate *priv = GET_PRIVATE (image); + return priv->basename; +} + +/** * as_image_get_md5: * @image: a #AsImage instance. * @@ -250,6 +269,23 @@ as_image_set_url (AsImage *image, const gchar *url, gssize url_len) } /** + * as_image_set_basename: + * @image: a #AsImage instance. + * @basename: the new filename basename. + * + * Sets the image basename filename. + * + * Since: 0.1.6 + **/ +void +as_image_set_basename (AsImage *image, const gchar *basename) +{ + AsImagePrivate *priv = GET_PRIVATE (image); + g_free (priv->basename); + priv->basename = g_strdup (basename); +} + +/** * as_image_set_width: * @image: a #AsImage instance. * @width: the width in pixels. @@ -408,6 +444,9 @@ as_image_node_parse (AsImage *image, GNode *node, GError **error) * * Reads a pixbuf from a file. * + * NOTE: This function also sets the suggested filename which can be retrieved + * using as_image_get_basename(). This can be overridden if required. + * * Returns: %TRUE for success * * Since: 0.1.6 @@ -420,6 +459,7 @@ as_image_load_filename (AsImage *image, AsImagePrivate *priv = GET_PRIVATE (image); GdkPixbuf *pixbuf = NULL; gboolean ret = TRUE; + gchar *basename = NULL; gchar *data = NULL; gsize len; @@ -439,10 +479,13 @@ as_image_load_filename (AsImage *image, } /* set */ + basename = g_path_get_basename (filename); + as_image_set_basename (image, basename); as_image_set_pixbuf (image, pixbuf); out: if (pixbuf != NULL) g_object_unref (pixbuf); + g_free (basename); g_free (data); return ret; } diff --git a/libappstream-glib/as-image.h b/libappstream-glib/as-image.h index 1c94639..375a3e6 100644 --- a/libappstream-glib/as-image.h +++ b/libappstream-glib/as-image.h @@ -100,6 +100,7 @@ const gchar *as_image_kind_to_string (AsImageKind kind); /* getters */ const gchar *as_image_get_url (AsImage *image); const gchar *as_image_get_md5 (AsImage *image); +const gchar *as_image_get_basename (AsImage *image); guint as_image_get_width (AsImage *image); guint as_image_get_height (AsImage *image); AsImageKind as_image_get_kind (AsImage *image); @@ -109,6 +110,8 @@ GdkPixbuf *as_image_get_pixbuf (AsImage *image); void as_image_set_url (AsImage *image, const gchar *url, gssize url_len); +void as_image_set_basename (AsImage *image, + const gchar *basename); void as_image_set_width (AsImage *image, guint width); void as_image_set_height (AsImage *image, diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index c6297a3..8c21da8 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -165,6 +165,7 @@ static void ch_test_image_func (void) { AsImage *image; + GdkPixbuf *pixbuf; GError *error = NULL; GNode *n; GNode *root; @@ -173,6 +174,7 @@ ch_test_image_func (void) "<image type=\"thumbnail\" height=\"12\" width=\"34\">" "http://www.hughsie.com/a.jpg</image>"; gboolean ret; + gchar *filename; image = as_image_new (); @@ -201,6 +203,34 @@ ch_test_image_func (void) g_string_free (xml, TRUE); as_node_unref (root); + /* read from image */ + filename = as_test_get_filename ("screenshot.png"); + ret = as_image_load_filename (image, filename, &error); + g_assert_no_error (error); + g_assert (ret); + g_assert_cmpint (as_image_get_width (image), ==, 800); + g_assert_cmpint (as_image_get_height (image), ==, 600); + g_assert_cmpstr (as_image_get_basename (image), ==, "screenshot.png"); + g_assert_cmpstr (as_image_get_md5 (image), ==, "9de72240c27a6f8f2eaab692795cdafc"); + + /* resample */ + pixbuf = as_image_save_pixbuf (image, + 752, 423, + AS_IMAGE_SAVE_FLAG_PAD_16_9); + g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, 752); + g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, 423); + g_object_unref (pixbuf); + + /* save */ + ret = as_image_save_filename (image, + "/tmp/foo.png", + 0, 0, + AS_IMAGE_SAVE_FLAG_NONE, + &error); + g_assert_no_error (error); + g_assert (ret); + + g_free (filename); g_object_unref (image); } |