/**
 * gpk_set_animated_icon_from_status:
 **/
gboolean
gpk_set_animated_icon_from_status (GpkAnimatedIcon *icon, PkStatusEnum status, GtkIconSize size)
{
	const gchar *name = NULL;
	guint delay = 0;

	/* see if there is an animation */
	name = gpk_status_enum_to_animation (status);

	/* get the timing */
	if (g_str_has_prefix (name, "pk-action-"))
		delay = 150;
	else if (g_str_has_prefix (name, "process-working"))
		delay = 50;

	/* animate or set static */
	if (delay != 0) {
		gpk_animated_icon_set_frame_delay (icon, delay);
		gpk_animated_icon_set_filename_tile (icon, size, name);
	} else {
		gpk_animated_icon_set_icon_name (icon, size, name);
	}

	/* stop spinning */
	if (status == PK_STATUS_ENUM_FINISHED)
		gpk_animated_icon_enable_animation (icon, FALSE);
	return TRUE;
}
/**
 * gpk_animated_icon_visible_notify_cb:
 **/
static void
gpk_animated_icon_visible_notify_cb (GObject *object, GParamSpec *param, GpkAnimatedIcon *icon)
{
	gboolean ret;
	g_object_get (object, "visible", &ret, NULL);
	if (!ret && icon->animation_id != 0) {
		g_debug ("disabling animation as hidden");
		gpk_animated_icon_enable_animation (icon, FALSE);
	}
}
/**
 * gpk_animated_icon_set_icon_name:
 **/
gboolean
gpk_animated_icon_set_icon_name (GpkAnimatedIcon *icon, GtkIconSize size, const gchar *name)
{
	g_return_val_if_fail (GPK_IS_ANIMATED_ICON (icon), FALSE);
	g_return_val_if_fail (name != NULL, FALSE);

	/* stop existing animation */
	gpk_animated_icon_enable_animation (icon, FALSE);

	/* set static image */
	gtk_image_set_from_icon_name (GTK_IMAGE (icon), name, size);
	return TRUE;
}
/**
 * gpk_animated_icon_set_filename_tile:
 **/
gboolean
gpk_animated_icon_set_filename_tile (GpkAnimatedIcon *icon, GtkIconSize size, const gchar *name)
{
	gboolean ret;
	gint w, h;
	gint rows, cols;
	gint r, c, i;
	GdkPixbuf *pixbuf;

	g_return_val_if_fail (GPK_IS_ANIMATED_ICON (icon), FALSE);
	g_return_val_if_fail (name != NULL, FALSE);

	/* have we already set the same icon */
	if (g_strcmp0 (icon->filename, name) == 0) {
		g_debug ("already set the same icon name %s, ignoring", name);
		return FALSE;
	}

	/* stop existing animation */
	gpk_animated_icon_enable_animation (icon, FALSE);

	/* save new value */
	g_free (icon->filename);
	icon->filename = g_strdup (name);

	/* do we need to unload */
	if (icon->frames != NULL) {
		gpk_animated_icon_free_pixbufs (icon);
	}

	g_debug ("loading from %s", name);
	ret = gtk_icon_size_lookup (size, &w, &h);
	if (!ret) {
		g_warning ("Can't get icon size info");
		return FALSE;
	}

	pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), name, w, 0, NULL);
	/* can't load from gnome-icon-theme */
	if (pixbuf == NULL) {
		g_debug ("can't load animation %s", name);
		return FALSE;
	}

	/* silly hack until we get some metadata */
	if (g_strcmp0 (name, "process-working") == 0) {
		w = gdk_pixbuf_get_width (pixbuf) / 8;
		h = gdk_pixbuf_get_height (pixbuf) / 4;
	}

	/* we failed to get a valid pixbuf */
	if (w == 0 || h == 0) {
		g_object_unref (pixbuf);
		return FALSE;
	}

	cols = gdk_pixbuf_get_width (pixbuf) / w;
	rows = gdk_pixbuf_get_height (pixbuf) / h;

	icon->frame_counter = 0;
	icon->number_frames = rows * cols;
	icon->frames = g_new (GdkPixbuf*, icon->number_frames);

	for (i = 0, r = 0; r < rows; r++)
		for (c = 0; c < cols; c++, i++) {
		icon->frames[i] = gdk_pixbuf_new_subpixbuf (pixbuf, c * w, r * h, w, h);
	}

	g_object_unref (pixbuf);

	/* enable new animation */
	gpk_animated_icon_enable_animation (icon, TRUE);

	return TRUE;
}