summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-icon.c
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2014-10-01 14:15:36 +0100
committerRichard Hughes <richard@hughsie.com>2014-10-01 16:20:39 +0100
commitb11a4bdba2edb21f24e1c414ca0636a19e25dbbc (patch)
tree7acb8b26fd9f2f882c4af74d0ac893515588b8cc /libappstream-glib/as-icon.c
parent475e8d18f2154763ccad6bdeb0cef5c33526e9e8 (diff)
downloadappstream-glib-b11a4bdba2edb21f24e1c414ca0636a19e25dbbc.tar.gz
Add as_icon_convert_to_kind()
We might want to convert one icon kind to another when merging AppStream files.
Diffstat (limited to 'libappstream-glib/as-icon.c')
-rw-r--r--libappstream-glib/as-icon.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/libappstream-glib/as-icon.c b/libappstream-glib/as-icon.c
index 00bfbdd..1aea474 100644
--- a/libappstream-glib/as-icon.c
+++ b/libappstream-glib/as-icon.c
@@ -601,6 +601,89 @@ as_icon_load (AsIcon *icon, AsIconLoadFlags flags, GError **error)
}
/**
+ * as_icon_convert_to_kind:
+ * @icon: a #AsIcon instance.
+ * @kind: a %AsIconKind, e.g. #AS_ICON_KIND_EMBEDDED
+ * @error: A #GError or %NULL.
+ *
+ * Converts the icon from one kind to another.
+ *
+ * Returns: %TRUE for success
+ *
+ * Since: 0.3.1
+ **/
+gboolean
+as_icon_convert_to_kind (AsIcon *icon, AsIconKind kind, GError **error)
+{
+ AsIconPrivate *priv = GET_PRIVATE (icon);
+
+ /* these can't be converted */
+ if (priv->kind == AS_ICON_KIND_STOCK ||
+ priv->kind == AS_ICON_KIND_REMOTE)
+ return TRUE;
+
+ /* no change */
+ if (priv->kind == kind)
+ return TRUE;
+
+ /* cached -> embedded */
+ if (priv->kind == AS_ICON_KIND_CACHED && kind == AS_ICON_KIND_EMBEDDED) {
+ gsize data_size;
+ _cleanup_bytes_unref_ GBytes *tmp = NULL;
+ _cleanup_free_ gchar *data = NULL;
+
+ /* load the pixbuf and save it to a PNG buffer */
+ if (priv->pixbuf == NULL) {
+ if (!as_icon_load (icon, AS_ICON_LOAD_FLAG_SEARCH_SIZE, error))
+ return FALSE;
+ }
+ if (!gdk_pixbuf_save_to_buffer (priv->pixbuf, &data, &data_size,
+ "png", error, NULL))
+ return FALSE;
+
+ /* set the PNG buffer to a blob of data */
+ tmp = g_bytes_new (data, data_size);
+ as_icon_set_data (icon, tmp);
+ as_icon_set_kind (icon, kind);
+ return TRUE;
+ }
+
+ /* cached -> embedded */
+ if (priv->kind == AS_ICON_KIND_EMBEDDED && kind == AS_ICON_KIND_CACHED) {
+ _cleanup_free_ gchar *size_str = NULL;
+ _cleanup_free_ gchar *path = NULL;
+ _cleanup_free_ gchar *fn = NULL;
+
+ /* ensure the parent path exists */
+ size_str = g_strdup_printf ("%ix%i", priv->width, priv->height);
+ path = g_build_filename (priv->prefix, size_str, NULL);
+ if (g_mkdir_with_parents (path, 0700) != 0) {
+ g_set_error (error,
+ AS_NODE_ERROR,
+ AS_NODE_ERROR_FAILED,
+ "Failed to create: %s", path);
+ return FALSE;
+ }
+
+ /* save the pixbuf */
+ fn = g_build_filename (path, priv->name, NULL);
+ if (!gdk_pixbuf_save (priv->pixbuf, fn, "png", error, NULL))
+ return FALSE;
+ as_icon_set_kind (icon, kind);
+ return TRUE;
+ }
+
+ /* not supported */
+ g_set_error (error,
+ AS_NODE_ERROR,
+ AS_NODE_ERROR_FAILED,
+ "converting %s to %s is not supported",
+ as_icon_kind_to_string (priv->kind),
+ as_icon_kind_to_string (kind));
+ return FALSE;
+}
+
+/**
* as_icon_new:
*
* Creates a new #AsIcon.