2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* pixmaps.c - code for handling pixbufs (despite the name!) */
25 /* Remove pixmaps from the cache when they haven't been accessed for
26 * this period of time (seconds).
29 #define PIXMAP_PURGE_TIME 1200
30 #define PIXMAP_THUMB_SIZE 128
31 #define PIXMAP_THUMB_TOO_OLD_TIME 5
37 #include <sys/types.h>
47 #include "gui_support.h"
58 GFSCache
*pixmap_cache
= NULL
;
59 GFSCache
*desktop_icon_cache
= NULL
;
61 static const char * bad_xpm
[] = {
79 MaskedPixmap
*im_error
;
80 MaskedPixmap
*im_unknown
;
81 MaskedPixmap
*im_symlink
;
83 MaskedPixmap
*im_unmounted
;
84 MaskedPixmap
*im_mounted
;
85 MaskedPixmap
*im_appdir
;
86 MaskedPixmap
*im_xattr
;
88 MaskedPixmap
*im_dirs
;
90 typedef struct _ChildThumbnail ChildThumbnail
;
92 /* There is one of these for each active child process */
93 struct _ChildThumbnail
{
99 static const char *stocks
[] = {
100 ROX_STOCK_SHOW_DETAILS
,
101 ROX_STOCK_SHOW_HIDDEN
,
109 static GtkIconSize mount_icon_size
= -1;
111 /* Static prototypes */
113 static void load_default_pixmaps(void);
114 static gint
purge(gpointer data
);
115 static MaskedPixmap
*image_from_file(const char *path
);
116 static MaskedPixmap
*image_from_desktop_file(const char *path
);
117 static MaskedPixmap
*get_bad_image(void);
118 static GdkPixbuf
*scale_pixbuf_up(GdkPixbuf
*src
, int max_w
, int max_h
);
119 static GdkPixbuf
*get_thumbnail_for(const char *path
);
120 static void thumbnail_child_done(ChildThumbnail
*info
);
121 static void child_create_thumbnail(const gchar
*path
);
122 static GList
*thumbs_purge_cache(Option
*option
, xmlNode
*node
, guchar
*label
);
123 static gchar
*thumbnail_path(const gchar
*path
);
124 static gchar
*thumbnail_program(MIME_type
*type
);
126 /****************************************************************
127 * EXTERNAL INTERFACE *
128 ****************************************************************/
130 void pixmaps_init(void)
132 GtkIconFactory
*factory
;
135 gtk_widget_push_colormap(gdk_rgb_get_colormap());
137 pixmap_cache
= g_fscache_new((GFSLoadFunc
) image_from_file
, NULL
, NULL
);
138 desktop_icon_cache
= g_fscache_new((GFSLoadFunc
) image_from_desktop_file
, NULL
, NULL
);
140 g_timeout_add(10000, purge
, NULL
);
142 factory
= gtk_icon_factory_new();
143 for (i
= 0; i
< G_N_ELEMENTS(stocks
); i
++)
146 GError
*error
= NULL
;
149 const gchar
*name
= stocks
[i
];
151 path
= g_strconcat(app_dir
, "/images/", name
, ".png", NULL
);
152 pixbuf
= gdk_pixbuf_new_from_file(path
, &error
);
155 g_warning("%s", error
->message
);
157 pixbuf
= gdk_pixbuf_new_from_xpm_data(bad_xpm
);
161 iset
= gtk_icon_set_new_from_pixbuf(pixbuf
);
162 g_object_unref(G_OBJECT(pixbuf
));
163 gtk_icon_factory_add(factory
, name
, iset
);
164 gtk_icon_set_unref(iset
);
166 gtk_icon_factory_add_default(factory
);
168 mount_icon_size
= gtk_icon_size_register("rox-mount-size", 14, 14);
170 load_default_pixmaps();
172 option_register_widget("thumbs-purge-cache", thumbs_purge_cache
);
175 /* Load image <appdir>/images/name.png.
176 * Always returns with a valid image.
178 MaskedPixmap
*load_pixmap(const char *name
)
181 MaskedPixmap
*retval
;
183 path
= g_strconcat(app_dir
, "/images/", name
, ".png", NULL
);
184 retval
= image_from_file(path
);
188 retval
= get_bad_image();
193 /* Create a MaskedPixmap from a GTK stock ID. Always returns
196 static MaskedPixmap
*mp_from_stock(const char *stock_id
, int size
)
198 GtkIconSet
*icon_set
;
200 MaskedPixmap
*retval
;
202 icon_set
= gtk_icon_factory_lookup_default(stock_id
);
204 return get_bad_image();
206 pixbuf
= gtk_icon_set_render_icon(icon_set
,
207 gtk_widget_get_default_style(), /* Gtk bug */
213 retval
= masked_pixmap_new(pixbuf
);
214 gdk_pixbuf_unref(pixbuf
);
219 void pixmap_make_huge(MaskedPixmap
*mp
)
224 g_return_if_fail(mp
->src_pixbuf
!= NULL
);
226 /* Limit to small size now, otherwise they get scaled up in mixed mode.
229 mp
->huge_pixbuf
= scale_pixbuf_up(mp
->src_pixbuf
,
230 SMALL_WIDTH
, SMALL_HEIGHT
);
232 if (!mp
->huge_pixbuf
)
234 mp
->huge_pixbuf
= mp
->src_pixbuf
;
235 g_object_ref(mp
->huge_pixbuf
);
238 mp
->huge_width
= gdk_pixbuf_get_width(mp
->huge_pixbuf
);
239 mp
->huge_height
= gdk_pixbuf_get_height(mp
->huge_pixbuf
);
242 void pixmap_make_small(MaskedPixmap
*mp
)
247 g_return_if_fail(mp
->src_pixbuf
!= NULL
);
249 mp
->sm_pixbuf
= scale_pixbuf(mp
->src_pixbuf
, SMALL_WIDTH
, SMALL_HEIGHT
);
253 mp
->sm_pixbuf
= mp
->src_pixbuf
;
254 g_object_ref(mp
->sm_pixbuf
);
257 mp
->sm_width
= gdk_pixbuf_get_width(mp
->sm_pixbuf
);
258 mp
->sm_height
= gdk_pixbuf_get_height(mp
->sm_pixbuf
);
261 /* Load image 'path' in the background and insert into pixmap_cache.
262 * Call callback(data, path) when done (path is NULL => error).
263 * If the image is already uptodate, or being created already, calls the
264 * callback right away.
266 void pixmap_background_thumb(const gchar
*path
, GFunc callback
, gpointer data
)
272 ChildThumbnail
*info
;
277 image
= g_fscache_lookup_full(pixmap_cache
, path
,
278 FSCACHE_LOOKUP_ONLY_NEW
, &found
);
282 /* Thumbnail is known, or being created */
284 g_object_unref(image
);
285 callback(data
, NULL
);
289 g_return_if_fail(image
== NULL
);
291 pixbuf
= get_thumbnail_for(path
);
295 struct stat info1
, info2
;
298 // Skip zero-byte files. They're either empty, or special (may cause
299 // us to hang, e.g. /proc/kmsg).
300 if (mc_stat(path
, &info1
) == 0 && info1
.st_size
== 0) {
301 callback(data
, NULL
);
305 dir
= g_path_get_dirname(path
);
307 /* If the image itself is in ~/.thumbnails, load it now
308 * (ie, don't create thumbnails for thumbnails!).
310 if (mc_stat(dir
, &info1
) != 0)
312 callback(data
, NULL
);
318 if (mc_stat(make_path(home_dir
, ".thumbnails/normal"),
320 info1
.st_dev
== info2
.st_dev
&&
321 info1
.st_ino
== info2
.st_ino
)
323 pixbuf
= rox_pixbuf_new_from_file_at_scale(path
,
324 PIXMAP_THUMB_SIZE
, PIXMAP_THUMB_SIZE
, TRUE
, NULL
);
327 g_fscache_insert(pixmap_cache
,
329 callback(data
, NULL
);
339 image
= masked_pixmap_new(pixbuf
);
340 gdk_pixbuf_unref(pixbuf
);
341 g_fscache_insert(pixmap_cache
, path
, image
, TRUE
);
342 callback(data
, (gchar
*) path
);
343 g_object_unref(G_OBJECT(image
));
347 type
= type_from_path(path
);
351 /* Add an entry, set to NULL, so no-one else tries to load this
354 g_fscache_insert(pixmap_cache
, path
, NULL
, TRUE
);
356 thumb_prog
= thumbnail_program(type
);
358 /* Only attempt to load 'images' types ourselves */
359 if (thumb_prog
== NULL
&& strcmp(type
->media_type
, "image") != 0)
361 callback(data
, NULL
);
362 return; /* Don't know how to handle this type */
370 delayed_error("fork(): %s", g_strerror(errno
));
371 callback(data
, NULL
);
377 /* We are the child process. (We are sloppy with freeing
378 memory, but since we go away very quickly, that's ok.) */
383 item
= diritem_new(g_basename(thumb_prog
));
385 diritem_restat(thumb_prog
, item
, NULL
);
386 if (item
->flags
& ITEM_FLAG_APPDIR
)
387 thumb_prog
= g_strconcat(thumb_prog
, "/AppRun",
390 execl(thumb_prog
, thumb_prog
, path
,
391 thumbnail_path(path
),
392 g_strdup_printf("%d", PIXMAP_THUMB_SIZE
),
397 child_create_thumbnail(path
);
403 info
= g_new(ChildThumbnail
, 1);
404 info
->path
= g_strdup(path
);
405 info
->callback
= callback
;
407 on_child_death(child
, (CallbackFn
) thumbnail_child_done
, info
);
410 /****************************************************************
411 * INTERNAL FUNCTIONS *
412 ****************************************************************/
414 /* Create a thumbnail file for this image */
415 static void save_thumbnail(const char *pathname
, GdkPixbuf
*full
)
419 int original_width
, original_height
;
421 char *md5
, *swidth
, *sheight
, *ssize
, *smtime
, *uri
;
426 thumb
= scale_pixbuf(full
, PIXMAP_THUMB_SIZE
, PIXMAP_THUMB_SIZE
);
428 original_width
= gdk_pixbuf_get_width(full
);
429 original_height
= gdk_pixbuf_get_height(full
);
431 if (mc_stat(pathname
, &info
) != 0)
434 swidth
= g_strdup_printf("%d", original_width
);
435 sheight
= g_strdup_printf("%d", original_height
);
436 ssize
= g_strdup_printf("%" SIZE_FMT
, info
.st_size
);
437 smtime
= g_strdup_printf("%ld", (long) info
.st_mtime
);
439 path
= pathdup(pathname
);
440 uri
= g_filename_to_uri(path
, NULL
, NULL
);
442 uri
= g_strconcat("file://", path
, NULL
);
446 to
= g_string_new(home_dir
);
447 g_string_append(to
, "/.thumbnails");
448 mkdir(to
->str
, 0700);
449 g_string_append(to
, "/normal/");
450 mkdir(to
->str
, 0700);
451 g_string_append(to
, md5
);
452 name_len
= to
->len
+ 4; /* Truncate to this length when renaming */
453 g_string_append_printf(to
, ".png.ROX-Filer-%ld", (long) getpid());
457 old_mask
= umask(0077);
458 gdk_pixbuf_save(thumb
, to
->str
, "png", NULL
,
459 "tEXt::Thumb::Image::Width", swidth
,
460 "tEXt::Thumb::Image::Height", sheight
,
461 "tEXt::Thumb::Size", ssize
,
462 "tEXt::Thumb::MTime", smtime
,
463 "tEXt::Thumb::URI", uri
,
464 "tEXt::Software", PROJECT
,
468 /* We create the file ###.png.ROX-Filer-PID and rename it to avoid
469 * a race condition if two programs create the same thumb at
475 final
= g_strndup(to
->str
, name_len
);
476 if (rename(to
->str
, final
))
477 g_warning("Failed to rename '%s' to '%s': %s",
478 to
->str
, final
, g_strerror(errno
));
482 g_string_free(to
, TRUE
);
490 static gchar
*thumbnail_path(const char *path
)
496 uri
= g_filename_to_uri(path
, NULL
, NULL
);
498 uri
= g_strconcat("file://", path
, NULL
);
501 to
= g_string_new(home_dir
);
502 g_string_append(to
, "/.thumbnails");
503 mkdir(to
->str
, 0700);
504 g_string_append(to
, "/normal/");
505 mkdir(to
->str
, 0700);
506 g_string_append(to
, md5
);
507 g_string_append(to
, ".png");
513 g_string_free(to
, FALSE
);
518 /* Return a program to create thumbnails for files of this type.
519 * NULL to try to make it ourself (using gdk).
522 static gchar
*thumbnail_program(MIME_type
*type
)
530 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
531 path
= choices_find_xdg_path_load(leaf
, "MIME-thumb", SITE
);
538 path
= choices_find_xdg_path_load(type
->media_type
, "MIME-thumb",
544 /* Called in a subprocess. Load path and create the thumbnail
545 * file. Parent will notice when we die.
547 static void child_create_thumbnail(const gchar
*path
)
551 image
= rox_pixbuf_new_from_file_at_scale(path
,
552 PIXMAP_THUMB_SIZE
, PIXMAP_THUMB_SIZE
, TRUE
, NULL
);
555 save_thumbnail(path
, image
);
557 /* (no need to unref, as we're about to exit) */
560 /* Called when the child process exits */
561 static void thumbnail_child_done(ChildThumbnail
*info
)
565 thumb
= get_thumbnail_for(info
->path
);
571 image
= masked_pixmap_new(thumb
);
572 g_object_unref(thumb
);
574 g_fscache_insert(pixmap_cache
, info
->path
, image
, FALSE
);
575 g_object_unref(image
);
577 info
->callback(info
->data
, info
->path
);
580 info
->callback(info
->data
, NULL
);
586 /* Check if we have an up-to-date thumbnail for this image.
587 * If so, return it. Otherwise, returns NULL.
589 static GdkPixbuf
*get_thumbnail_for(const char *pathname
)
591 GdkPixbuf
*thumb
= NULL
;
592 char *thumb_path
, *md5
, *uri
, *path
;
593 const char *ssize
, *smtime
;
597 path
= pathdup(pathname
);
598 uri
= g_filename_to_uri(path
, NULL
, NULL
);
600 uri
= g_strconcat("file://", path
, NULL
);
604 thumb_path
= g_strdup_printf("%s/.thumbnails/normal/%s.png",
608 thumb
= gdk_pixbuf_new_from_file(thumb_path
, NULL
);
612 /* Note that these don't need freeing... */
613 ssize
= gdk_pixbuf_get_option(thumb
, "tEXt::Thumb::Size");
614 /* This is optional, so don't flag an error if it is missing */
616 smtime
= gdk_pixbuf_get_option(thumb
, "tEXt::Thumb::MTime");
620 if (mc_stat(path
, &info
) != 0)
623 ttime
=(time_t) atol(smtime
);
625 if (info
.st_mtime
!= ttime
&& now
>ttime
+PIXMAP_THUMB_TOO_OLD_TIME
)
628 if (ssize
&& info
.st_size
< atol(ssize
))
634 gdk_pixbuf_unref(thumb
);
642 /* Load the image 'path' and return a pointer to the resulting
643 * MaskedPixmap. NULL on failure.
644 * Doesn't check for thumbnails (this is for small icons).
646 static MaskedPixmap
*image_from_file(const char *path
)
650 GError
*error
= NULL
;
652 pixbuf
= gdk_pixbuf_new_from_file(path
, &error
);
655 g_warning("%s\n", error
->message
);
660 image
= masked_pixmap_new(pixbuf
);
662 gdk_pixbuf_unref(pixbuf
);
667 /* Load this icon named by this .desktop file from the current theme.
670 static MaskedPixmap
*image_from_desktop_file(const char *path
)
672 GError
*error
= NULL
;
673 MaskedPixmap
*image
= NULL
;
676 icon
= get_value_from_desktop_file(path
,
677 "Desktop Entry", "Icon", &error
);
680 g_warning("Failed to parse .desktop file '%s':\n%s",
681 path
, error
->message
);
688 image
= image_from_file(icon
);
695 /* For some unknown reason, some icon names have extensions.
698 extension
= strrchr(icon
, '.');
699 if (extension
&& strcmp(extension
, ".png") == 0)
704 /* SVG reader is very noisy, so redirect stderr to stdout */
707 pixbuf
= gtk_icon_theme_load_icon(icon_theme
, icon
, HUGE_WIDTH
,
713 goto err
; /* Might just not be in the theme */
715 image
= masked_pixmap_new(pixbuf
);
716 g_object_unref(pixbuf
);
726 /* Scale src down to fit in max_w, max_h and return the new pixbuf.
727 * If src is small enough, then ref it and return that.
729 GdkPixbuf
*scale_pixbuf(GdkPixbuf
*src
, int max_w
, int max_h
)
733 w
= gdk_pixbuf_get_width(src
);
734 h
= gdk_pixbuf_get_height(src
);
736 if (w
<= max_w
&& h
<= max_h
)
743 float scale_x
= ((float) w
) / max_w
;
744 float scale_y
= ((float) h
) / max_h
;
745 float scale
= MAX(scale_x
, scale_y
);
746 int dest_w
= w
/ scale
;
747 int dest_h
= h
/ scale
;
749 return gdk_pixbuf_scale_simple(src
,
752 GDK_INTERP_BILINEAR
);
756 /* Scale src up to fit in max_w, max_h and return the new pixbuf.
757 * If src is that size or bigger, then ref it and return that.
759 static GdkPixbuf
*scale_pixbuf_up(GdkPixbuf
*src
, int max_w
, int max_h
)
763 w
= gdk_pixbuf_get_width(src
);
764 h
= gdk_pixbuf_get_height(src
);
766 if (w
== 0 || h
== 0 || w
>= max_w
|| h
>= max_h
)
773 float scale_x
= max_w
/ ((float) w
);
774 float scale_y
= max_h
/ ((float) h
);
775 float scale
= MIN(scale_x
, scale_y
);
777 return gdk_pixbuf_scale_simple(src
,
780 GDK_INTERP_BILINEAR
);
784 /* Return a pointer to the (static) bad image. The ref counter will ensure
785 * that the image is never freed.
787 static MaskedPixmap
*get_bad_image(void)
792 bad
= gdk_pixbuf_new_from_xpm_data(bad_xpm
);
793 mp
= masked_pixmap_new(bad
);
794 gdk_pixbuf_unref(bad
);
799 /* Called now and then to clear out old pixmaps */
800 static gint
purge(gpointer data
)
802 g_fscache_purge(pixmap_cache
, PIXMAP_PURGE_TIME
);
807 static gpointer parent_class
;
809 static void masked_pixmap_finialize(GObject
*object
)
811 MaskedPixmap
*mp
= (MaskedPixmap
*) object
;
815 g_object_unref(mp
->src_pixbuf
);
816 mp
->src_pixbuf
= NULL
;
821 g_object_unref(mp
->huge_pixbuf
);
822 mp
->huge_pixbuf
= NULL
;
826 g_object_unref(mp
->pixbuf
);
832 g_object_unref(mp
->sm_pixbuf
);
833 mp
->sm_pixbuf
= NULL
;
836 G_OBJECT_CLASS(parent_class
)->finalize(object
);
839 static void masked_pixmap_class_init(gpointer gclass
, gpointer data
)
841 GObjectClass
*object
= (GObjectClass
*) gclass
;
843 parent_class
= g_type_class_peek_parent(gclass
);
845 object
->finalize
= masked_pixmap_finialize
;
848 static void masked_pixmap_init(GTypeInstance
*object
, gpointer gclass
)
850 MaskedPixmap
*mp
= (MaskedPixmap
*) object
;
852 mp
->src_pixbuf
= NULL
;
854 mp
->huge_pixbuf
= NULL
;
856 mp
->huge_height
= -1;
862 mp
->sm_pixbuf
= NULL
;
867 static GType
masked_pixmap_get_type(void)
869 static GType type
= 0;
873 static const GTypeInfo info
=
875 sizeof (MaskedPixmapClass
),
876 NULL
, /* base_init */
877 NULL
, /* base_finalise */
878 masked_pixmap_class_init
,
879 NULL
, /* class_finalise */
880 NULL
, /* class_data */
881 sizeof(MaskedPixmap
),
886 type
= g_type_register_static(G_TYPE_OBJECT
, "MaskedPixmap",
893 MaskedPixmap
*masked_pixmap_new(GdkPixbuf
*full_size
)
896 GdkPixbuf
*src_pixbuf
, *normal_pixbuf
;
898 g_return_val_if_fail(full_size
!= NULL
, NULL
);
900 src_pixbuf
= scale_pixbuf(full_size
, HUGE_WIDTH
, HUGE_HEIGHT
);
901 g_return_val_if_fail(src_pixbuf
!= NULL
, NULL
);
903 normal_pixbuf
= scale_pixbuf(src_pixbuf
, ICON_WIDTH
, ICON_HEIGHT
);
904 g_return_val_if_fail(normal_pixbuf
!= NULL
, NULL
);
906 mp
= g_object_new(masked_pixmap_get_type(), NULL
);
908 mp
->src_pixbuf
= src_pixbuf
;
910 mp
->pixbuf
= normal_pixbuf
;
911 mp
->width
= gdk_pixbuf_get_width(normal_pixbuf
);
912 mp
->height
= gdk_pixbuf_get_height(normal_pixbuf
);
917 /* Load all the standard pixmaps. Also sets the default window icon. */
918 static void load_default_pixmaps(void)
921 GError
*error
= NULL
;
923 im_error
= mp_from_stock(GTK_STOCK_DIALOG_WARNING
,
924 GTK_ICON_SIZE_DIALOG
);
925 im_unknown
= mp_from_stock(GTK_STOCK_DIALOG_QUESTION
,
926 GTK_ICON_SIZE_DIALOG
);
928 im_symlink
= mp_from_stock(ROX_STOCK_SYMLINK
, mount_icon_size
);
929 im_unmounted
= mp_from_stock(ROX_STOCK_MOUNT
, mount_icon_size
);
930 im_mounted
= mp_from_stock(ROX_STOCK_MOUNTED
, mount_icon_size
);
931 im_xattr
= mp_from_stock(ROX_STOCK_XATTR
, mount_icon_size
);
933 im_dirs
= load_pixmap("dirs");
934 im_appdir
= load_pixmap("application");
936 pixbuf
= gdk_pixbuf_new_from_file(
937 make_path(app_dir
, ".DirIcon"), &error
);
942 icon_list
= g_list_append(NULL
, pixbuf
);
943 gtk_window_set_default_icon_list(icon_list
);
944 g_list_free(icon_list
);
946 g_object_unref(G_OBJECT(pixbuf
));
950 g_warning("%s\n", error
->message
);
955 /* Also purges memory cache */
956 static void purge_disk_cache(GtkWidget
*button
, gpointer data
)
963 g_fscache_purge(pixmap_cache
, 0);
965 path
= g_strconcat(home_dir
, "/.thumbnails/normal/", NULL
);
970 report_error(_("Can't delete thumbnails in %s:\n%s"),
971 path
, g_strerror(errno
));
975 while ((ent
= readdir(dir
)))
977 if (ent
->d_name
[0] == '.')
979 list
= g_list_prepend(list
,
980 g_strconcat(path
, ent
->d_name
, NULL
));
988 destroy_glist(&list
);
991 info_message(_("There are no thumbnails to delete"));
996 static GList
*thumbs_purge_cache(Option
*option
, xmlNode
*node
, guchar
*label
)
998 GtkWidget
*button
, *align
;
1000 g_return_val_if_fail(option
== NULL
, NULL
);
1002 align
= gtk_alignment_new(0, 0.5, 0, 0);
1003 button
= button_new_mixed(GTK_STOCK_CLEAR
,
1004 _("Purge thumbnails disk cache"));
1005 gtk_container_add(GTK_CONTAINER(align
), button
);
1006 g_signal_connect(button
, "clicked", G_CALLBACK(purge_disk_cache
), NULL
);
1008 return g_list_append(NULL
, align
);