nautilus_file_get_icon (NautilusFile *file,
			int size,
			NautilusFileIconFlags flags)
{
	NautilusIconInfo *icon;
	GIcon *gicon;
	GdkPixbuf *raw_pixbuf, *scaled_pixbuf;
	int modified_size;

	if (file == NULL) {
		return NULL;
	}
	
	gicon = get_custom_icon (file);
	if (gicon) {
		icon = nautilus_icon_info_lookup (gicon, size);
		g_object_unref (gicon);
		return icon;
	}

	if (flags & NAUTILUS_FILE_ICON_FLAGS_FORCE_THUMBNAIL_SIZE) {
		modified_size = size;
	} else {
		modified_size = size * cached_thumbnail_size / NAUTILUS_ICON_SIZE_STANDARD; 
	}

	if (flags & NAUTILUS_FILE_ICON_FLAGS_USE_THUMBNAILS &&
	    nautilus_file_should_show_thumbnail (file)) {
		if (file->details->thumbnail) {
			int w, h, s;
			double scale;

			raw_pixbuf = g_object_ref (file->details->thumbnail);

			w = gdk_pixbuf_get_width (raw_pixbuf);
			h = gdk_pixbuf_get_height (raw_pixbuf);
			
			s = MAX (w, h);
			scale = (double)modified_size / s;


			if (scale > 0.99) {
				/* never scale any thumbnails up */
				scaled_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
								gdk_pixbuf_get_has_alpha (raw_pixbuf),
								gdk_pixbuf_get_bits_per_sample (raw_pixbuf),
								w * scale, h * scale);
				gdk_pixbuf_fill (scaled_pixbuf, 0xffffff00);
				gdk_pixbuf_copy_area (raw_pixbuf,
						      0, 0, w, h,
						      scaled_pixbuf,
						      (gdk_pixbuf_get_width (scaled_pixbuf) - w) / 2,
						      (gdk_pixbuf_get_height (scaled_pixbuf) - h) / 2);
			} else {
				scaled_pixbuf = gdk_pixbuf_scale_simple (raw_pixbuf,
									 w * scale, h * scale,
									 GDK_INTERP_BILINEAR);
			}
			nautilus_thumbnail_frame_image (&scaled_pixbuf);
			g_object_unref (raw_pixbuf);

			if (modified_size > 128 &&
			    !file->details->thumbnail_wants_original) {
				/* Invalidate if we resize upward */
				file->details->thumbnail_wants_original = TRUE;
				nautilus_file_invalidate_attributes (file, NAUTILUS_FILE_ATTRIBUTE_THUMBNAIL);
			}
			
			icon = nautilus_icon_info_new_for_pixbuf (scaled_pixbuf);
			g_object_unref (scaled_pixbuf);
			return icon;
		} else if (file->details->thumbnail_path == NULL &&
			   file->details->can_read &&				
			   !file->details->is_thumbnailing &&
			   (!file->details->thumbnailing_failed ||
			    !nautilus_has_valid_failed_thumbnail (file))) {
			if (nautilus_can_thumbnail (file)) {
				nautilus_create_thumbnail (file);
			}
		}
	}

	if (file->details->is_thumbnailing &&
	    flags & NAUTILUS_FILE_ICON_FLAGS_USE_THUMBNAILS)
		gicon = g_themed_icon_new (ICON_NAME_THUMBNAIL_LOADING);
	else
		gicon = nautilus_file_get_gicon (file, flags);
	
	if (gicon) {
		icon = nautilus_icon_info_lookup (gicon, size);
		if (nautilus_icon_info_is_fallback (icon)) {
			g_object_unref (icon);
			icon = nautilus_icon_info_lookup (get_default_file_icon (flags), size);
		}
		g_object_unref (gicon);
		return icon;
	} else {
		return nautilus_icon_info_lookup (get_default_file_icon (flags), size);
	}
}
nautilus_file_get_icon (NautilusFile *file,
			int size,
			NautilusFileIconFlags flags)
{
	NautilusIconInfo *icon;
	GIcon *gicon;
	GdkPixbuf *raw_pixbuf, *scaled_pixbuf;
	int modified_size;

	if (file == NULL) {
		return NULL;
	}
	
	gicon = get_custom_icon (file);
	if (gicon) {
		icon = nautilus_icon_info_lookup (gicon, size);
		g_object_unref (gicon);
		return icon;
	}

	if (flags & NAUTILUS_FILE_ICON_FLAGS_FORCE_THUMBNAIL_SIZE) {
		modified_size = size;
	} else {
		modified_size = size * cached_thumbnail_size / NAUTILUS_ICON_SIZE_STANDARD; 
	}

	if (flags & NAUTILUS_FILE_ICON_FLAGS_USE_THUMBNAILS &&
	    nautilus_file_should_show_thumbnail (file)) {
		if (file->details->thumbnail) {
			int w, h, s;
			double scale;

			raw_pixbuf = g_object_ref (file->details->thumbnail);

			w = gdk_pixbuf_get_width (raw_pixbuf);
			h = gdk_pixbuf_get_height (raw_pixbuf);
			
			s = MAX (w, h);
			scale = (double)modified_size / s;


			if (scale > 0.99) {
				 
				scaled_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
								gdk_pixbuf_get_has_alpha (raw_pixbuf),
								gdk_pixbuf_get_bits_per_sample (raw_pixbuf),
								w * scale, h * scale);
				gdk_pixbuf_fill (scaled_pixbuf, 0xffffff00);
				gdk_pixbuf_copy_area (raw_pixbuf,
						      0, 0, w, h,
						      scaled_pixbuf,
						      (gdk_pixbuf_get_width (scaled_pixbuf) - w) / 2,
						      (gdk_pixbuf_get_height (scaled_pixbuf) - h) / 2);
			} else {
				scaled_pixbuf = gdk_pixbuf_scale_simple (raw_pixbuf,
									 w * scale, h * scale,
									 GDK_INTERP_BILINEAR);
			}
			nautilus_thumbnail_frame_image (&scaled_pixbuf);
			g_object_unref (raw_pixbuf);

			if (modified_size > 128 &&
			    !file->details->thumbnail_wants_original) {
				 
				file->details->thumbnail_wants_original = TRUE;
				nautilus_file_invalidate_attributes (file, NAUTILUS_FILE_ATTRIBUTE_THUMBNAIL);
			}
			
			icon = nautilus_icon_info_new_for_pixbuf (scaled_pixbuf);
			g_object_unref (scaled_pixbuf);
			return icon;
		} else if (file->details->thumbnail_path == NULL &&
			   file->details->can_read &&				
			   !file->details->is_thumbnailing &&
			   (!file->details->thumbnailing_failed ||
			    !nautilus_has_valid_failed_thumbnail (file))) {
			if (nautilus_can_thumbnail (file)) {
				nautilus_create_thumbnail (file);
			}
		}
	}

	if (file->details->is_thumbnailing &&
	    flags & NAUTILUS_FILE_ICON_FLAGS_USE_THUMBNAILS)
		gicon = g_themed_icon_new (ICON_NAME_THUMBNAIL_LOADING);
	else
		gicon = nautilus_file_get_gicon (file, flags);
	
	if (gicon) {
		icon = nautilus_icon_info_lookup (gicon, size);
		if (nautilus_icon_info_is_fallback (icon)) {
			g_object_unref (icon);
			icon = nautilus_icon_info_lookup (get_default_file_icon (flags), size);
		}
		g_object_unref (gicon);
		return icon;
	} else {
		return nautilus_icon_info_lookup (get_default_file_icon (flags), size);
	}
}