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
,
107 static GtkIconSize mount_icon_size
= -1;
109 /* Static prototypes */
111 static void load_default_pixmaps(void);
112 static gint
purge(gpointer data
);
113 static MaskedPixmap
*image_from_file(const char *path
);
114 static MaskedPixmap
*image_from_desktop_file(const char *path
);
115 static MaskedPixmap
*get_bad_image(void);
116 static GdkPixbuf
*scale_pixbuf_up(GdkPixbuf
*src
, int max_w
, int max_h
);
117 static GdkPixbuf
*get_thumbnail_for(const char *path
);
118 static void thumbnail_child_done(ChildThumbnail
*info
);
119 static void child_create_thumbnail(const gchar
*path
);
120 static GList
*thumbs_purge_cache(Option
*option
, xmlNode
*node
, guchar
*label
);
121 static gchar
*thumbnail_path(const gchar
*path
);
122 static gchar
*thumbnail_program(MIME_type
*type
);
124 /****************************************************************
125 * EXTERNAL INTERFACE *
126 ****************************************************************/
128 void pixmaps_init(void)
130 GtkIconFactory
*factory
;
133 gtk_widget_push_colormap(gdk_rgb_get_colormap());
135 pixmap_cache
= g_fscache_new((GFSLoadFunc
) image_from_file
, NULL
, NULL
);
136 desktop_icon_cache
= g_fscache_new((GFSLoadFunc
) image_from_desktop_file
, NULL
, NULL
);
138 g_timeout_add(10000, purge
, NULL
);
140 factory
= gtk_icon_factory_new();
141 for (i
= 0; i
< G_N_ELEMENTS(stocks
); i
++)
144 GError
*error
= NULL
;
147 const gchar
*name
= stocks
[i
];
149 path
= g_strconcat(app_dir
, "/images/", name
, ".png", NULL
);
150 pixbuf
= gdk_pixbuf_new_from_file(path
, &error
);
153 g_warning("%s", error
->message
);
155 pixbuf
= gdk_pixbuf_new_from_xpm_data(bad_xpm
);
159 iset
= gtk_icon_set_new_from_pixbuf(pixbuf
);
160 g_object_unref(G_OBJECT(pixbuf
));
161 gtk_icon_factory_add(factory
, name
, iset
);
162 gtk_icon_set_unref(iset
);
164 gtk_icon_factory_add_default(factory
);
166 mount_icon_size
= gtk_icon_size_register("rox-mount-size", 14, 14);
168 load_default_pixmaps();
170 option_register_widget("thumbs-purge-cache", thumbs_purge_cache
);
173 /* Load image <appdir>/images/name.png.
174 * Always returns with a valid image.
176 MaskedPixmap
*load_pixmap(const char *name
)
179 MaskedPixmap
*retval
;
181 path
= g_strconcat(app_dir
, "/images/", name
, ".png", NULL
);
182 retval
= image_from_file(path
);
186 retval
= get_bad_image();
191 /* Create a MaskedPixmap from a GTK stock ID. Always returns
194 static MaskedPixmap
*mp_from_stock(const char *stock_id
, int size
)
196 GtkIconSet
*icon_set
;
198 MaskedPixmap
*retval
;
200 icon_set
= gtk_icon_factory_lookup_default(stock_id
);
202 return get_bad_image();
204 pixbuf
= gtk_icon_set_render_icon(icon_set
,
205 gtk_widget_get_default_style(), /* Gtk bug */
211 retval
= masked_pixmap_new(pixbuf
);
212 gdk_pixbuf_unref(pixbuf
);
217 void pixmap_make_huge(MaskedPixmap
*mp
)
222 g_return_if_fail(mp
->src_pixbuf
!= NULL
);
224 /* Limit to small size now, otherwise they get scaled up in mixed mode.
227 mp
->huge_pixbuf
= scale_pixbuf_up(mp
->src_pixbuf
,
228 SMALL_WIDTH
, SMALL_HEIGHT
);
230 if (!mp
->huge_pixbuf
)
232 mp
->huge_pixbuf
= mp
->src_pixbuf
;
233 g_object_ref(mp
->huge_pixbuf
);
236 mp
->huge_width
= gdk_pixbuf_get_width(mp
->huge_pixbuf
);
237 mp
->huge_height
= gdk_pixbuf_get_height(mp
->huge_pixbuf
);
240 void pixmap_make_small(MaskedPixmap
*mp
)
245 g_return_if_fail(mp
->src_pixbuf
!= NULL
);
247 mp
->sm_pixbuf
= scale_pixbuf(mp
->src_pixbuf
, SMALL_WIDTH
, SMALL_HEIGHT
);
251 mp
->sm_pixbuf
= mp
->src_pixbuf
;
252 g_object_ref(mp
->sm_pixbuf
);
255 mp
->sm_width
= gdk_pixbuf_get_width(mp
->sm_pixbuf
);
256 mp
->sm_height
= gdk_pixbuf_get_height(mp
->sm_pixbuf
);
259 /* Load image 'path' in the background and insert into pixmap_cache.
260 * Call callback(data, path) when done (path is NULL => error).
261 * If the image is already uptodate, or being created already, calls the
262 * callback right away.
264 void pixmap_background_thumb(const gchar
*path
, GFunc callback
, gpointer data
)
270 ChildThumbnail
*info
;
275 image
= g_fscache_lookup_full(pixmap_cache
, path
,
276 FSCACHE_LOOKUP_ONLY_NEW
, &found
);
280 /* Thumbnail is known, or being created */
282 g_object_unref(image
);
283 callback(data
, NULL
);
287 g_return_if_fail(image
== NULL
);
289 pixbuf
= get_thumbnail_for(path
);
293 struct stat info1
, info2
;
296 // Skip zero-byte files. They're either empty, or special (may cause
297 // us to hang, e.g. /proc/kmsg).
298 if (mc_stat(path
, &info1
) == 0 && info1
.st_size
== 0) {
299 callback(data
, NULL
);
303 dir
= g_path_get_dirname(path
);
305 /* If the image itself is in ~/.thumbnails, load it now
306 * (ie, don't create thumbnails for thumbnails!).
308 if (mc_stat(dir
, &info1
) != 0)
310 callback(data
, NULL
);
316 if (mc_stat(make_path(home_dir
, ".thumbnails/normal"),
318 info1
.st_dev
== info2
.st_dev
&&
319 info1
.st_ino
== info2
.st_ino
)
321 pixbuf
= rox_pixbuf_new_from_file_at_scale(path
,
322 PIXMAP_THUMB_SIZE
, PIXMAP_THUMB_SIZE
, TRUE
, NULL
);
325 g_fscache_insert(pixmap_cache
,
327 callback(data
, NULL
);
337 image
= masked_pixmap_new(pixbuf
);
338 gdk_pixbuf_unref(pixbuf
);
339 g_fscache_insert(pixmap_cache
, path
, image
, TRUE
);
340 callback(data
, (gchar
*) path
);
341 g_object_unref(G_OBJECT(image
));
345 type
= type_from_path(path
);
349 /* Add an entry, set to NULL, so no-one else tries to load this
352 g_fscache_insert(pixmap_cache
, path
, NULL
, TRUE
);
354 thumb_prog
= thumbnail_program(type
);
356 /* Only attempt to load 'images' types ourselves */
357 if (thumb_prog
== NULL
&& strcmp(type
->media_type
, "image") != 0)
359 callback(data
, NULL
);
360 return; /* Don't know how to handle this type */
368 delayed_error("fork(): %s", g_strerror(errno
));
369 callback(data
, NULL
);
375 /* We are the child process. (We are sloppy with freeing
376 memory, but since we go away very quickly, that's ok.) */
381 item
= diritem_new(g_basename(thumb_prog
));
383 diritem_restat(thumb_prog
, item
, NULL
);
384 if (item
->flags
& ITEM_FLAG_APPDIR
)
385 thumb_prog
= g_strconcat(thumb_prog
, "/AppRun",
388 execl(thumb_prog
, thumb_prog
, path
,
389 thumbnail_path(path
),
390 g_strdup_printf("%d", PIXMAP_THUMB_SIZE
),
395 child_create_thumbnail(path
);
401 info
= g_new(ChildThumbnail
, 1);
402 info
->path
= g_strdup(path
);
403 info
->callback
= callback
;
405 on_child_death(child
, (CallbackFn
) thumbnail_child_done
, info
);
408 /****************************************************************
409 * INTERNAL FUNCTIONS *
410 ****************************************************************/
412 /* Create a thumbnail file for this image */
413 static void save_thumbnail(const char *pathname
, GdkPixbuf
*full
)
417 int original_width
, original_height
;
419 char *md5
, *swidth
, *sheight
, *ssize
, *smtime
, *uri
;
424 thumb
= scale_pixbuf(full
, PIXMAP_THUMB_SIZE
, PIXMAP_THUMB_SIZE
);
426 original_width
= gdk_pixbuf_get_width(full
);
427 original_height
= gdk_pixbuf_get_height(full
);
429 if (mc_stat(pathname
, &info
) != 0)
432 swidth
= g_strdup_printf("%d", original_width
);
433 sheight
= g_strdup_printf("%d", original_height
);
434 ssize
= g_strdup_printf("%" SIZE_FMT
, info
.st_size
);
435 smtime
= g_strdup_printf("%ld", (long) info
.st_mtime
);
437 path
= pathdup(pathname
);
438 uri
= g_filename_to_uri(path
, NULL
, NULL
);
440 uri
= g_strconcat("file://", path
, NULL
);
444 to
= g_string_new(home_dir
);
445 g_string_append(to
, "/.thumbnails");
446 mkdir(to
->str
, 0700);
447 g_string_append(to
, "/normal/");
448 mkdir(to
->str
, 0700);
449 g_string_append(to
, md5
);
450 name_len
= to
->len
+ 4; /* Truncate to this length when renaming */
451 g_string_append_printf(to
, ".png.ROX-Filer-%ld", (long) getpid());
455 old_mask
= umask(0077);
456 gdk_pixbuf_save(thumb
, to
->str
, "png", NULL
,
457 "tEXt::Thumb::Image::Width", swidth
,
458 "tEXt::Thumb::Image::Height", sheight
,
459 "tEXt::Thumb::Size", ssize
,
460 "tEXt::Thumb::MTime", smtime
,
461 "tEXt::Thumb::URI", uri
,
462 "tEXt::Software", PROJECT
,
466 /* We create the file ###.png.ROX-Filer-PID and rename it to avoid
467 * a race condition if two programs create the same thumb at
473 final
= g_strndup(to
->str
, name_len
);
474 if (rename(to
->str
, final
))
475 g_warning("Failed to rename '%s' to '%s': %s",
476 to
->str
, final
, g_strerror(errno
));
480 g_string_free(to
, TRUE
);
488 static gchar
*thumbnail_path(const char *path
)
494 uri
= g_filename_to_uri(path
, NULL
, NULL
);
496 uri
= g_strconcat("file://", path
, NULL
);
499 to
= g_string_new(home_dir
);
500 g_string_append(to
, "/.thumbnails");
501 mkdir(to
->str
, 0700);
502 g_string_append(to
, "/normal/");
503 mkdir(to
->str
, 0700);
504 g_string_append(to
, md5
);
505 g_string_append(to
, ".png");
511 g_string_free(to
, FALSE
);
516 /* Return a program to create thumbnails for files of this type.
517 * NULL to try to make it ourself (using gdk).
520 static gchar
*thumbnail_program(MIME_type
*type
)
528 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
529 path
= choices_find_xdg_path_load(leaf
, "MIME-thumb", SITE
);
536 path
= choices_find_xdg_path_load(type
->media_type
, "MIME-thumb",
542 /* Called in a subprocess. Load path and create the thumbnail
543 * file. Parent will notice when we die.
545 static void child_create_thumbnail(const gchar
*path
)
549 image
= rox_pixbuf_new_from_file_at_scale(path
,
550 PIXMAP_THUMB_SIZE
, PIXMAP_THUMB_SIZE
, TRUE
, NULL
);
553 save_thumbnail(path
, image
);
555 /* (no need to unref, as we're about to exit) */
558 /* Called when the child process exits */
559 static void thumbnail_child_done(ChildThumbnail
*info
)
563 thumb
= get_thumbnail_for(info
->path
);
569 image
= masked_pixmap_new(thumb
);
570 g_object_unref(thumb
);
572 g_fscache_insert(pixmap_cache
, info
->path
, image
, FALSE
);
573 g_object_unref(image
);
575 info
->callback(info
->data
, info
->path
);
578 info
->callback(info
->data
, NULL
);
584 /* Check if we have an up-to-date thumbnail for this image.
585 * If so, return it. Otherwise, returns NULL.
587 static GdkPixbuf
*get_thumbnail_for(const char *pathname
)
589 GdkPixbuf
*thumb
= NULL
;
590 char *thumb_path
, *md5
, *uri
, *path
;
591 const char *ssize
, *smtime
;
595 path
= pathdup(pathname
);
596 uri
= g_filename_to_uri(path
, NULL
, NULL
);
598 uri
= g_strconcat("file://", path
, NULL
);
602 thumb_path
= g_strdup_printf("%s/.thumbnails/normal/%s.png",
606 thumb
= gdk_pixbuf_new_from_file(thumb_path
, NULL
);
610 /* Note that these don't need freeing... */
611 ssize
= gdk_pixbuf_get_option(thumb
, "tEXt::Thumb::Size");
615 smtime
= gdk_pixbuf_get_option(thumb
, "tEXt::Thumb::MTime");
619 if (mc_stat(path
, &info
) != 0)
622 ttime
=(time_t) atol(smtime
);
624 if (info
.st_mtime
!= ttime
&& now
>ttime
+PIXMAP_THUMB_TOO_OLD_TIME
)
627 if (info
.st_size
< atol(ssize
))
633 gdk_pixbuf_unref(thumb
);
641 /* Load the image 'path' and return a pointer to the resulting
642 * MaskedPixmap. NULL on failure.
643 * Doesn't check for thumbnails (this is for small icons).
645 static MaskedPixmap
*image_from_file(const char *path
)
649 GError
*error
= NULL
;
651 pixbuf
= gdk_pixbuf_new_from_file(path
, &error
);
654 g_warning("%s\n", error
->message
);
659 image
= masked_pixmap_new(pixbuf
);
661 gdk_pixbuf_unref(pixbuf
);
666 /* Load this icon named by this .desktop file from the current theme.
669 static MaskedPixmap
*image_from_desktop_file(const char *path
)
671 GError
*error
= NULL
;
672 MaskedPixmap
*image
= NULL
;
675 icon
= get_value_from_desktop_file(path
,
676 "Desktop Entry", "Icon", &error
);
679 g_warning("Failed to parse .desktop file '%s':\n%s",
680 path
, error
->message
);
687 image
= image_from_file(icon
);
694 /* For some unknown reason, some icon names have extensions.
697 extension
= strrchr(icon
, '.');
698 if (extension
&& strcmp(extension
, ".png") == 0)
703 /* SVG reader is very noisy, so redirect stderr to stdout */
706 pixbuf
= gtk_icon_theme_load_icon(icon_theme
, icon
, HUGE_WIDTH
,
712 goto err
; /* Might just not be in the theme */
714 image
= masked_pixmap_new(pixbuf
);
715 g_object_unref(pixbuf
);
725 /* Scale src down to fit in max_w, max_h and return the new pixbuf.
726 * If src is small enough, then ref it and return that.
728 GdkPixbuf
*scale_pixbuf(GdkPixbuf
*src
, int max_w
, int max_h
)
732 w
= gdk_pixbuf_get_width(src
);
733 h
= gdk_pixbuf_get_height(src
);
735 if (w
<= max_w
&& h
<= max_h
)
742 float scale_x
= ((float) w
) / max_w
;
743 float scale_y
= ((float) h
) / max_h
;
744 float scale
= MAX(scale_x
, scale_y
);
745 int dest_w
= w
/ scale
;
746 int dest_h
= h
/ scale
;
748 return gdk_pixbuf_scale_simple(src
,
751 GDK_INTERP_BILINEAR
);
755 /* Scale src up to fit in max_w, max_h and return the new pixbuf.
756 * If src is that size or bigger, then ref it and return that.
758 static GdkPixbuf
*scale_pixbuf_up(GdkPixbuf
*src
, int max_w
, int max_h
)
762 w
= gdk_pixbuf_get_width(src
);
763 h
= gdk_pixbuf_get_height(src
);
765 if (w
== 0 || h
== 0 || w
>= max_w
|| h
>= max_h
)
772 float scale_x
= max_w
/ ((float) w
);
773 float scale_y
= max_h
/ ((float) h
);
774 float scale
= MIN(scale_x
, scale_y
);
776 return gdk_pixbuf_scale_simple(src
,
779 GDK_INTERP_BILINEAR
);
783 /* Return a pointer to the (static) bad image. The ref counter will ensure
784 * that the image is never freed.
786 static MaskedPixmap
*get_bad_image(void)
791 bad
= gdk_pixbuf_new_from_xpm_data(bad_xpm
);
792 mp
= masked_pixmap_new(bad
);
793 gdk_pixbuf_unref(bad
);
798 /* Called now and then to clear out old pixmaps */
799 static gint
purge(gpointer data
)
801 g_fscache_purge(pixmap_cache
, PIXMAP_PURGE_TIME
);
806 static gpointer parent_class
;
808 static void masked_pixmap_finialize(GObject
*object
)
810 MaskedPixmap
*mp
= (MaskedPixmap
*) object
;
814 g_object_unref(mp
->src_pixbuf
);
815 mp
->src_pixbuf
= NULL
;
820 g_object_unref(mp
->huge_pixbuf
);
821 mp
->huge_pixbuf
= NULL
;
825 g_object_unref(mp
->pixbuf
);
831 g_object_unref(mp
->sm_pixbuf
);
832 mp
->sm_pixbuf
= NULL
;
835 G_OBJECT_CLASS(parent_class
)->finalize(object
);
838 static void masked_pixmap_class_init(gpointer gclass
, gpointer data
)
840 GObjectClass
*object
= (GObjectClass
*) gclass
;
842 parent_class
= g_type_class_peek_parent(gclass
);
844 object
->finalize
= masked_pixmap_finialize
;
847 static void masked_pixmap_init(GTypeInstance
*object
, gpointer gclass
)
849 MaskedPixmap
*mp
= (MaskedPixmap
*) object
;
851 mp
->src_pixbuf
= NULL
;
853 mp
->huge_pixbuf
= NULL
;
855 mp
->huge_height
= -1;
861 mp
->sm_pixbuf
= NULL
;
866 static GType
masked_pixmap_get_type(void)
868 static GType type
= 0;
872 static const GTypeInfo info
=
874 sizeof (MaskedPixmapClass
),
875 NULL
, /* base_init */
876 NULL
, /* base_finalise */
877 masked_pixmap_class_init
,
878 NULL
, /* class_finalise */
879 NULL
, /* class_data */
880 sizeof(MaskedPixmap
),
885 type
= g_type_register_static(G_TYPE_OBJECT
, "MaskedPixmap",
892 MaskedPixmap
*masked_pixmap_new(GdkPixbuf
*full_size
)
895 GdkPixbuf
*src_pixbuf
, *normal_pixbuf
;
897 g_return_val_if_fail(full_size
!= NULL
, NULL
);
899 src_pixbuf
= scale_pixbuf(full_size
, HUGE_WIDTH
, HUGE_HEIGHT
);
900 g_return_val_if_fail(src_pixbuf
!= NULL
, NULL
);
902 normal_pixbuf
= scale_pixbuf(src_pixbuf
, ICON_WIDTH
, ICON_HEIGHT
);
903 g_return_val_if_fail(normal_pixbuf
!= NULL
, NULL
);
905 mp
= g_object_new(masked_pixmap_get_type(), NULL
);
907 mp
->src_pixbuf
= src_pixbuf
;
909 mp
->pixbuf
= normal_pixbuf
;
910 mp
->width
= gdk_pixbuf_get_width(normal_pixbuf
);
911 mp
->height
= gdk_pixbuf_get_height(normal_pixbuf
);
916 /* Load all the standard pixmaps. Also sets the default window icon. */
917 static void load_default_pixmaps(void)
920 GError
*error
= NULL
;
922 im_error
= mp_from_stock(GTK_STOCK_DIALOG_WARNING
,
923 GTK_ICON_SIZE_DIALOG
);
924 im_unknown
= mp_from_stock(GTK_STOCK_DIALOG_QUESTION
,
925 GTK_ICON_SIZE_DIALOG
);
926 im_symlink
= load_pixmap("symlink");
928 im_unmounted
= mp_from_stock(ROX_STOCK_MOUNT
, mount_icon_size
);
929 im_mounted
= mp_from_stock(ROX_STOCK_MOUNTED
, mount_icon_size
);
930 im_appdir
= load_pixmap("application");
931 im_xattr
= load_pixmap("rox-xattr");
933 im_dirs
= load_pixmap("dirs");
935 pixbuf
= gdk_pixbuf_new_from_file(
936 make_path(app_dir
, ".DirIcon"), &error
);
941 icon_list
= g_list_append(NULL
, pixbuf
);
942 gtk_window_set_default_icon_list(icon_list
);
943 g_list_free(icon_list
);
945 g_object_unref(G_OBJECT(pixbuf
));
949 g_warning("%s\n", error
->message
);
954 /* Also purges memory cache */
955 static void purge_disk_cache(GtkWidget
*button
, gpointer data
)
962 g_fscache_purge(pixmap_cache
, 0);
964 path
= g_strconcat(home_dir
, "/.thumbnails/normal/", NULL
);
969 report_error(_("Can't delete thumbnails in %s:\n%s"),
970 path
, g_strerror(errno
));
974 while ((ent
= readdir(dir
)))
976 if (ent
->d_name
[0] == '.')
978 list
= g_list_prepend(list
,
979 g_strconcat(path
, ent
->d_name
, NULL
));
987 destroy_glist(&list
);
990 info_message(_("There are no thumbnails to delete"));
995 static GList
*thumbs_purge_cache(Option
*option
, xmlNode
*node
, guchar
*label
)
997 GtkWidget
*button
, *align
;
999 g_return_val_if_fail(option
== NULL
, NULL
);
1001 align
= gtk_alignment_new(0, 0.5, 0, 0);
1002 button
= button_new_mixed(GTK_STOCK_CLEAR
,
1003 _("Purge thumbnails disk cache"));
1004 gtk_container_add(GTK_CONTAINER(align
), button
);
1005 g_signal_connect(button
, "clicked", G_CALLBACK(purge_disk_cache
), NULL
);
1007 return g_list_append(NULL
, align
);