コード例 #1
0
ファイル: as-icon.c プロジェクト: amigadave/appstream-glib
/**
 * as_icon_node_parse:
 * @icon: a #AsIcon instance.
 * @node: a #GNode.
 * @error: A #GError or %NULL.
 *
 * Populates the object from a DOM node.
 *
 * Returns: %TRUE for success
 *
 * Since: 0.3.1
 **/
gboolean
as_icon_node_parse (AsIcon *icon, GNode *node, GError **error)
{
	AsIconPrivate *priv = GET_PRIVATE (icon);
	const gchar *tmp;
	gint size;
	gboolean prepend_size = TRUE;

	tmp = as_node_get_attribute (node, "type");
	as_icon_set_kind (icon, as_icon_kind_from_string (tmp));
	switch (priv->kind) {
	case AS_ICON_KIND_EMBEDDED:
		if (!as_icon_node_parse_embedded (icon, node, error))
			return FALSE;
		break;
	default:

		/* store the name without any prefix */
		tmp = as_node_get_data (node);
		if (g_strstr_len (tmp, -1, "/") == NULL) {
			as_icon_set_name (icon, tmp, -1);
		} else {
			_cleanup_free_ gchar *basename = NULL;
			basename = g_path_get_basename (tmp);
			as_icon_set_name (icon, basename, -1);
		}

		/* width is optional, assume 64px if missing */
		size = as_node_get_attribute_as_int (node, "width");
		if (size == G_MAXINT) {
			size = 64;
			prepend_size = FALSE;
		}
		priv->width = size;

		/* height is optional, assume 64px if missing */
		size = as_node_get_attribute_as_int (node, "height");
		if (size == G_MAXINT) {
			size = 64;
			prepend_size = FALSE;
		}
		priv->height = size;

		/* only use the size if the metadata has width and height */
		if (prepend_size) {
			g_free (priv->prefix_private);
			priv->prefix_private = g_strdup_printf ("%s/%ix%i",
								priv->prefix,
								priv->width,
								priv->height);
		}
		break;
	}

	return TRUE;
}
コード例 #2
0
ファイル: as-icon.c プロジェクト: fedora-copr/appstream-glib
/**
 * as_icon_node_parse:
 * @icon: a #AsIcon instance.
 * @node: a #GNode.
 * @ctx: a #AsNodeContext.
 * @error: A #GError or %NULL.
 *
 * Populates the object from a DOM node.
 *
 * Returns: %TRUE for success
 *
 * Since: 0.3.1
 **/
gboolean
as_icon_node_parse (AsIcon *icon, GNode *node,
		    AsNodeContext *ctx, GError **error)
{
	AsIconPrivate *priv = GET_PRIVATE (icon);
	const gchar *tmp;
	guint size;
	gboolean prepend_size = TRUE;

	tmp = as_node_get_attribute (node, "type");
	as_icon_set_kind (icon, as_icon_kind_from_string (tmp));
	switch (priv->kind) {
	case AS_ICON_KIND_EMBEDDED:
		if (!as_icon_node_parse_embedded (icon, node, error))
			return FALSE;
		break;
	default:

		/* preserve the URL for remote icons */
		tmp = as_node_get_data (node);
		if (tmp == NULL) {
			g_set_error (error,
				     AS_ICON_ERROR,
				     AS_ICON_ERROR_FAILED,
				     "no data for icon of type %s",
				     as_icon_kind_to_string (priv->kind));
			return FALSE;
		}
		if (priv->kind == AS_ICON_KIND_REMOTE)
			as_icon_set_url (icon, tmp);
		else if (priv->kind == AS_ICON_KIND_LOCAL)
			as_icon_set_filename (icon, tmp);

		/* store the name without any prefix */
		if (g_strstr_len (tmp, -1, "/") == NULL) {
			as_icon_set_name (icon, tmp);
		} else {
			g_autofree gchar *basename = NULL;
			basename = g_path_get_basename (tmp);
			as_icon_set_name (icon, basename);
		}

		/* width is optional, assume 64px if missing */
		size = as_node_get_attribute_as_uint (node, "width");
		if (size == G_MAXUINT) {
			size = 64;
			prepend_size = FALSE;
		}
		priv->width = size;

		/* height is optional, assume 64px if missing */
		size = as_node_get_attribute_as_uint (node, "height");
		if (size == G_MAXUINT) {
			size = 64;
			prepend_size = FALSE;
		}
		priv->height = size;

		/* only use the size if the metadata has width and height */
		if (prepend_size) {
			g_free (priv->prefix_private);
			priv->prefix_private = g_strdup_printf ("%s/%ux%u",
								priv->prefix,
								priv->width,
								priv->height);
		}
		break;
	}

	return TRUE;
}