r4990: Bugfix: The Thumb::Size tag is optional in the thumbnail spec (reported
[rox-filer/dt.git] / ROX-Filer / src / pixmaps.c
blobb48694d3b1469d4176cd86bdfd121786170ee58a
1 /*
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)
8 * any later version.
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
13 * more details.
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!) */
22 #include "config.h"
23 #define PIXMAPS_C
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
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <string.h>
41 #include <gtk/gtk.h>
43 #include "global.h"
45 #include "fscache.h"
46 #include "support.h"
47 #include "gui_support.h"
48 #include "pixmaps.h"
49 #include "main.h"
50 #include "filer.h"
51 #include "dir.h"
52 #include "diritem.h"
53 #include "choices.h"
54 #include "options.h"
55 #include "action.h"
56 #include "type.h"
58 GFSCache *pixmap_cache = NULL;
59 GFSCache *desktop_icon_cache = NULL;
61 static const char * bad_xpm[] = {
62 "12 12 3 1",
63 " c #000000000000",
64 ". c #FFFF00000000",
65 "x c #FFFFFFFFFFFF",
66 " ",
67 " ..xxxxxx.. ",
68 " ...xxxx... ",
69 " x...xx...x ",
70 " xx......xx ",
71 " xxx....xxx ",
72 " xxx....xxx ",
73 " xx......xx ",
74 " x...xx...x ",
75 " ...xxxx... ",
76 " ..xxxxxx.. ",
77 " "};
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 {
94 gchar *path;
95 GFunc callback;
96 gpointer data;
99 static const char *stocks[] = {
100 ROX_STOCK_SHOW_DETAILS,
101 ROX_STOCK_SHOW_HIDDEN,
102 ROX_STOCK_SELECT,
103 ROX_STOCK_MOUNT,
104 ROX_STOCK_MOUNTED,
105 ROX_STOCK_SYMLINK,
106 ROX_STOCK_XATTR,
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;
133 int i;
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++)
145 GdkPixbuf *pixbuf;
146 GError *error = NULL;
147 gchar *path;
148 GtkIconSet *iset;
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);
153 if (!pixbuf)
155 g_warning("%s", error->message);
156 g_error_free(error);
157 pixbuf = gdk_pixbuf_new_from_xpm_data(bad_xpm);
159 g_free(path);
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)
180 guchar *path;
181 MaskedPixmap *retval;
183 path = g_strconcat(app_dir, "/images/", name, ".png", NULL);
184 retval = image_from_file(path);
185 g_free(path);
187 if (!retval)
188 retval = get_bad_image();
190 return retval;
193 /* Create a MaskedPixmap from a GTK stock ID. Always returns
194 * a valid image.
196 static MaskedPixmap *mp_from_stock(const char *stock_id, int size)
198 GtkIconSet *icon_set;
199 GdkPixbuf *pixbuf;
200 MaskedPixmap *retval;
202 icon_set = gtk_icon_factory_lookup_default(stock_id);
203 if (!icon_set)
204 return get_bad_image();
206 pixbuf = gtk_icon_set_render_icon(icon_set,
207 gtk_widget_get_default_style(), /* Gtk bug */
208 GTK_TEXT_DIR_LTR,
209 GTK_STATE_NORMAL,
210 size,
211 NULL,
212 NULL);
213 retval = masked_pixmap_new(pixbuf);
214 gdk_pixbuf_unref(pixbuf);
216 return retval;
219 void pixmap_make_huge(MaskedPixmap *mp)
221 if (mp->huge_pixbuf)
222 return;
224 g_return_if_fail(mp->src_pixbuf != NULL);
226 /* Limit to small size now, otherwise they get scaled up in mixed mode.
227 * Also looked ugly.
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)
244 if (mp->sm_pixbuf)
245 return;
247 g_return_if_fail(mp->src_pixbuf != NULL);
249 mp->sm_pixbuf = scale_pixbuf(mp->src_pixbuf, SMALL_WIDTH, SMALL_HEIGHT);
251 if (!mp->sm_pixbuf)
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)
268 gboolean found;
269 MaskedPixmap *image;
270 GdkPixbuf *pixbuf;
271 pid_t child;
272 ChildThumbnail *info;
273 MIME_type *type;
274 gchar *thumb_prog;
277 image = g_fscache_lookup_full(pixmap_cache, path,
278 FSCACHE_LOOKUP_ONLY_NEW, &found);
280 if (found)
282 /* Thumbnail is known, or being created */
283 if (image)
284 g_object_unref(image);
285 callback(data, NULL);
286 return;
289 g_return_if_fail(image == NULL);
291 pixbuf = get_thumbnail_for(path);
293 if (!pixbuf)
295 struct stat info1, info2;
296 char *dir;
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);
302 return;
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);
313 g_free(dir);
314 return;
316 g_free(dir);
318 if (mc_stat(make_path(home_dir, ".thumbnails/normal"),
319 &info2) == 0 &&
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);
325 if (!pixbuf)
327 g_fscache_insert(pixmap_cache,
328 path, NULL, TRUE);
329 callback(data, NULL);
330 return;
335 if (pixbuf)
337 MaskedPixmap *image;
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));
344 return;
347 type = type_from_path(path);
348 if (!type)
349 type = text_plain;
351 /* Add an entry, set to NULL, so no-one else tries to load this
352 * image.
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 */
365 child = fork();
367 if (child == -1)
369 g_free(thumb_prog);
370 delayed_error("fork(): %s", g_strerror(errno));
371 callback(data, NULL);
372 return;
375 if (child == 0)
377 /* We are the child process. (We are sloppy with freeing
378 memory, but since we go away very quickly, that's ok.) */
379 if (thumb_prog)
381 DirItem *item;
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",
388 NULL);
390 execl(thumb_prog, thumb_prog, path,
391 thumbnail_path(path),
392 g_strdup_printf("%d", PIXMAP_THUMB_SIZE),
393 NULL);
394 _exit(1);
397 child_create_thumbnail(path);
398 _exit(0);
401 g_free(thumb_prog);
403 info = g_new(ChildThumbnail, 1);
404 info->path = g_strdup(path);
405 info->callback = callback;
406 info->data = data;
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)
417 struct stat info;
418 gchar *path;
419 int original_width, original_height;
420 GString *to;
421 char *md5, *swidth, *sheight, *ssize, *smtime, *uri;
422 mode_t old_mask;
423 int name_len;
424 GdkPixbuf *thumb;
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)
432 return;
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);
441 if (!uri)
442 uri = g_strconcat("file://", path, NULL);
443 md5 = md5_hash(uri);
444 g_free(path);
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());
455 g_free(md5);
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,
465 NULL);
466 umask(old_mask);
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
470 * once.
473 gchar *final;
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));
479 g_free(final);
482 g_string_free(to, TRUE);
483 g_free(swidth);
484 g_free(sheight);
485 g_free(ssize);
486 g_free(smtime);
487 g_free(uri);
490 static gchar *thumbnail_path(const char *path)
492 gchar *uri, *md5;
493 GString *to;
494 gchar *ans;
496 uri = g_filename_to_uri(path, NULL, NULL);
497 if(!uri)
498 uri = g_strconcat("file://", path, NULL);
499 md5 = md5_hash(uri);
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");
509 g_free(md5);
510 g_free(uri);
512 ans=to->str;
513 g_string_free(to, FALSE);
515 return ans;
518 /* Return a program to create thumbnails for files of this type.
519 * NULL to try to make it ourself (using gdk).
520 * g_free the result.
522 static gchar *thumbnail_program(MIME_type *type)
524 gchar *leaf;
525 gchar *path;
527 if (!type)
528 return NULL;
530 leaf = g_strconcat(type->media_type, "_", type->subtype, NULL);
531 path = choices_find_xdg_path_load(leaf, "MIME-thumb", SITE);
532 g_free(leaf);
533 if (path)
535 return path;
538 path = choices_find_xdg_path_load(type->media_type, "MIME-thumb",
539 SITE);
541 return path;
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)
549 GdkPixbuf *image;
551 image = rox_pixbuf_new_from_file_at_scale(path,
552 PIXMAP_THUMB_SIZE, PIXMAP_THUMB_SIZE, TRUE, NULL);
554 if (image)
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)
563 GdkPixbuf *thumb;
565 thumb = get_thumbnail_for(info->path);
567 if (thumb)
569 MaskedPixmap *image;
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);
579 else
580 info->callback(info->data, NULL);
582 g_free(info->path);
583 g_free(info);
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;
594 struct stat info;
595 time_t ttime, now;
597 path = pathdup(pathname);
598 uri = g_filename_to_uri(path, NULL, NULL);
599 if(!uri)
600 uri = g_strconcat("file://", path, NULL);
601 md5 = md5_hash(uri);
602 g_free(uri);
604 thumb_path = g_strdup_printf("%s/.thumbnails/normal/%s.png",
605 home_dir, md5);
606 g_free(md5);
608 thumb = gdk_pixbuf_new_from_file(thumb_path, NULL);
609 if (!thumb)
610 goto err;
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");
617 if (!smtime)
618 goto err;
620 if (mc_stat(path, &info) != 0)
621 goto err;
623 ttime=(time_t) atol(smtime);
624 time(&now);
625 if (info.st_mtime != ttime && now>ttime+PIXMAP_THUMB_TOO_OLD_TIME)
626 goto err;
628 if (ssize && info.st_size < atol(ssize))
629 goto err;
631 goto out;
632 err:
633 if (thumb)
634 gdk_pixbuf_unref(thumb);
635 thumb = NULL;
636 out:
637 g_free(path);
638 g_free(thumb_path);
639 return 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)
648 GdkPixbuf *pixbuf;
649 MaskedPixmap *image;
650 GError *error = NULL;
652 pixbuf = gdk_pixbuf_new_from_file(path, &error);
653 if (!pixbuf)
655 g_warning("%s\n", error->message);
656 g_error_free(error);
657 return NULL;
660 image = masked_pixmap_new(pixbuf);
662 gdk_pixbuf_unref(pixbuf);
664 return image;
667 /* Load this icon named by this .desktop file from the current theme.
668 * NULL on failure.
670 static MaskedPixmap *image_from_desktop_file(const char *path)
672 GError *error = NULL;
673 MaskedPixmap *image = NULL;
674 char *icon = NULL;
676 icon = get_value_from_desktop_file(path,
677 "Desktop Entry", "Icon", &error);
678 if (error)
680 g_warning("Failed to parse .desktop file '%s':\n%s",
681 path, error->message);
682 goto err;
684 if (!icon)
685 goto err;
687 if (icon[0] == '/')
688 image = image_from_file(icon);
689 else
691 GdkPixbuf *pixbuf;
692 int tmp_fd;
693 char *extension;
695 /* For some unknown reason, some icon names have extensions.
696 * Remove them.
698 extension = strrchr(icon, '.');
699 if (extension && strcmp(extension, ".png") == 0)
701 *extension = '\0';
704 /* SVG reader is very noisy, so redirect stderr to stdout */
705 tmp_fd = dup(2);
706 dup2(1, 2);
707 pixbuf = gtk_icon_theme_load_icon(icon_theme, icon, HUGE_WIDTH,
708 0, NULL);
709 dup2(tmp_fd, 2);
710 close(tmp_fd);
712 if (pixbuf == NULL)
713 goto err; /* Might just not be in the theme */
715 image = masked_pixmap_new(pixbuf);
716 g_object_unref(pixbuf);
718 err:
719 if (error != NULL)
720 g_error_free(error);
721 if (icon != NULL)
722 g_free(icon);
723 return image;
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)
731 int w, h;
733 w = gdk_pixbuf_get_width(src);
734 h = gdk_pixbuf_get_height(src);
736 if (w <= max_w && h <= max_h)
738 gdk_pixbuf_ref(src);
739 return src;
741 else
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,
750 MAX(dest_w, 1),
751 MAX(dest_h, 1),
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)
761 int w, 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)
768 gdk_pixbuf_ref(src);
769 return src;
771 else
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,
778 w * scale,
779 h * scale,
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)
789 GdkPixbuf *bad;
790 MaskedPixmap *mp;
792 bad = gdk_pixbuf_new_from_xpm_data(bad_xpm);
793 mp = masked_pixmap_new(bad);
794 gdk_pixbuf_unref(bad);
796 return mp;
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);
804 return TRUE;
807 static gpointer parent_class;
809 static void masked_pixmap_finialize(GObject *object)
811 MaskedPixmap *mp = (MaskedPixmap *) object;
813 if (mp->src_pixbuf)
815 g_object_unref(mp->src_pixbuf);
816 mp->src_pixbuf = NULL;
819 if (mp->huge_pixbuf)
821 g_object_unref(mp->huge_pixbuf);
822 mp->huge_pixbuf = NULL;
824 if (mp->pixbuf)
826 g_object_unref(mp->pixbuf);
827 mp->pixbuf = NULL;
830 if (mp->sm_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;
855 mp->huge_width = -1;
856 mp->huge_height = -1;
858 mp->pixbuf = NULL;
859 mp->width = -1;
860 mp->height = -1;
862 mp->sm_pixbuf = NULL;
863 mp->sm_width = -1;
864 mp->sm_height = -1;
867 static GType masked_pixmap_get_type(void)
869 static GType type = 0;
871 if (!type)
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),
882 0, /* n_preallocs */
883 masked_pixmap_init
886 type = g_type_register_static(G_TYPE_OBJECT, "MaskedPixmap",
887 &info, 0);
890 return type;
893 MaskedPixmap *masked_pixmap_new(GdkPixbuf *full_size)
895 MaskedPixmap *mp;
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);
914 return mp;
917 /* Load all the standard pixmaps. Also sets the default window icon. */
918 static void load_default_pixmaps(void)
920 GdkPixbuf *pixbuf;
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);
938 if (pixbuf)
940 GList *icon_list;
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));
948 else
950 g_warning("%s\n", error->message);
951 g_error_free(error);
955 /* Also purges memory cache */
956 static void purge_disk_cache(GtkWidget *button, gpointer data)
958 char *path;
959 GList *list = NULL;
960 DIR *dir;
961 struct dirent *ent;
963 g_fscache_purge(pixmap_cache, 0);
965 path = g_strconcat(home_dir, "/.thumbnails/normal/", NULL);
967 dir = opendir(path);
968 if (!dir)
970 report_error(_("Can't delete thumbnails in %s:\n%s"),
971 path, g_strerror(errno));
972 goto out;
975 while ((ent = readdir(dir)))
977 if (ent->d_name[0] == '.')
978 continue;
979 list = g_list_prepend(list,
980 g_strconcat(path, ent->d_name, NULL));
983 closedir(dir);
985 if (list)
987 action_delete(list);
988 destroy_glist(&list);
990 else
991 info_message(_("There are no thumbnails to delete"));
992 out:
993 g_free(path);
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);