]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
22 | /* | |
23 | * Large (and important) sections of this file were adapted from | |
24 | * ROX-Filer source code, Copyright (C) 2003, the ROX-Filer team, | |
25 | * originally licensed under the GPL v2 or greater (as above). | |
26 | * | |
27 | */ | |
28 | ||
29 | #ifdef HAVE_CONFIG_H | |
30 | #include "config.h" | |
31 | #endif | |
32 | ||
33 | #include <stdlib.h> | |
34 | #ifdef HAVE_UNISTD_H | |
35 | #include <unistd.h> | |
36 | #endif | |
37 | #include <errno.h> | |
38 | #include <string.h> | |
39 | #include <glib.h> | |
40 | #include <glib/gstdio.h> | |
41 | #include "viking.h" | |
42 | #include "thumbnails.h" | |
43 | #include "icons/icons.h" | |
44 | #include "md5_hash.h" | |
45 | ||
46 | #ifdef __CYGWIN__ | |
47 | #ifdef __CYGWIN_USE_BIG_TYPES__ | |
48 | #define ST_SIZE_FMT "%lld" | |
49 | #else | |
50 | #define ST_SIZE_FMT "%ld" | |
51 | #endif | |
52 | #else | |
53 | /* FIXME -- on some systems this may need to me "lld", see ROX-Filer code */ | |
54 | #define ST_SIZE_FMT "%ld" | |
55 | #endif | |
56 | ||
57 | static gchar* thumb_dir = NULL; | |
58 | ||
59 | #ifdef WINDOWS | |
60 | static void set_thumb_dir () | |
61 | { | |
62 | thumb_dir = g_strconcat ( g_get_home_dir(), "\\THUMBNAILS\\normal\\", NULL ) ; | |
63 | } | |
64 | #else | |
65 | static void set_thumb_dir () | |
66 | { | |
67 | const gchar *xdg_cache_home = g_getenv("XDG_CACHE_HOME"); | |
68 | if ( xdg_cache_home && g_strcmp0 (xdg_cache_home, "") != 0 ) | |
69 | thumb_dir = g_strconcat ( xdg_cache_home, "/thumbnails/normal/", NULL ); | |
70 | else | |
71 | thumb_dir = g_strconcat ( g_get_home_dir(), "/.cache/thumbnails/normal/", NULL ); | |
72 | } | |
73 | #endif | |
74 | ||
75 | #define PIXMAP_THUMB_SIZE 128 | |
76 | ||
77 | static GdkPixbuf *save_thumbnail(const char *pathname, GdkPixbuf *full); | |
78 | static GdkPixbuf *child_create_thumbnail(const gchar *path); | |
79 | ||
80 | gboolean a_thumbnails_exists ( const gchar *filename ) | |
81 | { | |
82 | GdkPixbuf *pixbuf = a_thumbnails_get(filename); | |
83 | if ( pixbuf ) | |
84 | { | |
85 | g_object_unref ( G_OBJECT ( pixbuf ) ); | |
86 | return TRUE; | |
87 | } | |
88 | return FALSE; | |
89 | } | |
90 | ||
91 | GdkPixbuf *a_thumbnails_get_default () | |
92 | { | |
93 | return gdk_pixbuf_from_pixdata ( &thumbnails_pixbuf, FALSE, NULL ); | |
94 | } | |
95 | ||
96 | /* filename must be absolute. you could have a function to make sure it exists and absolutize it */ | |
97 | ||
98 | void a_thumbnails_create(const gchar *filename) | |
99 | { | |
100 | GdkPixbuf *pixbuf = a_thumbnails_get(filename); | |
101 | ||
102 | if ( ! pixbuf ) | |
103 | pixbuf = child_create_thumbnail(filename); | |
104 | ||
105 | if ( pixbuf ) | |
106 | g_object_unref ( G_OBJECT ( pixbuf ) ); | |
107 | } | |
108 | ||
109 | GdkPixbuf *a_thumbnails_scale_pixbuf(GdkPixbuf *src, int max_w, int max_h) | |
110 | { | |
111 | int w, h; | |
112 | ||
113 | w = gdk_pixbuf_get_width(src); | |
114 | h = gdk_pixbuf_get_height(src); | |
115 | ||
116 | if (w <= max_w && h <= max_h) | |
117 | { | |
118 | g_object_ref ( G_OBJECT ( src ) ); | |
119 | return src; | |
120 | } | |
121 | else | |
122 | { | |
123 | float scale_x = ((float) w) / max_w; | |
124 | float scale_y = ((float) h) / max_h; | |
125 | float scale = MAX(scale_x, scale_y); | |
126 | int dest_w = w / scale; | |
127 | int dest_h = h / scale; | |
128 | ||
129 | return gdk_pixbuf_scale_simple(src, | |
130 | MAX(dest_w, 1), | |
131 | MAX(dest_h, 1), | |
132 | GDK_INTERP_BILINEAR); | |
133 | } | |
134 | } | |
135 | ||
136 | static GdkPixbuf *child_create_thumbnail(const gchar *path) | |
137 | { | |
138 | GdkPixbuf *image, *tmpbuf; | |
139 | ||
140 | image = gdk_pixbuf_new_from_file(path, NULL); | |
141 | if (!image) | |
142 | return NULL; | |
143 | ||
144 | tmpbuf = gdk_pixbuf_apply_embedded_orientation(image); | |
145 | g_object_unref(G_OBJECT(image)); | |
146 | image = tmpbuf; | |
147 | ||
148 | if (image) | |
149 | { | |
150 | GdkPixbuf *thumb = save_thumbnail(path, image); | |
151 | g_object_unref ( G_OBJECT ( image ) ); | |
152 | return thumb; | |
153 | } | |
154 | ||
155 | return NULL; | |
156 | } | |
157 | ||
158 | static GdkPixbuf *save_thumbnail(const char *pathname, GdkPixbuf *full) | |
159 | { | |
160 | struct stat info; | |
161 | gchar *path; | |
162 | int original_width, original_height; | |
163 | const gchar* orientation; | |
164 | GString *to; | |
165 | char *md5, *swidth, *sheight, *ssize, *smtime, *uri; | |
166 | mode_t old_mask; | |
167 | int name_len; | |
168 | GdkPixbuf *thumb; | |
169 | ||
170 | if (stat(pathname, &info) != 0) | |
171 | return NULL; | |
172 | ||
173 | thumb = a_thumbnails_scale_pixbuf(full, PIXMAP_THUMB_SIZE, PIXMAP_THUMB_SIZE); | |
174 | ||
175 | orientation = gdk_pixbuf_get_option (full, "orientation"); | |
176 | ||
177 | original_width = gdk_pixbuf_get_width(full); | |
178 | original_height = gdk_pixbuf_get_height(full); | |
179 | ||
180 | ||
181 | swidth = g_strdup_printf("%d", original_width); | |
182 | sheight = g_strdup_printf("%d", original_height); | |
183 | ssize = g_strdup_printf(ST_SIZE_FMT, info.st_size); | |
184 | smtime = g_strdup_printf("%ld", (long) info.st_mtime); | |
185 | ||
186 | path = file_realpath_dup(pathname); | |
187 | uri = g_strconcat("file://", path, NULL); | |
188 | md5 = md5_hash(uri); | |
189 | g_free(path); | |
190 | ||
191 | to = g_string_new ( thumb_dir ); | |
192 | if ( g_mkdir_with_parents(to->str, 0700) != 0 ) | |
193 | g_warning ("%s: Failed to mkdir %s", __FUNCTION__, to->str ); | |
194 | g_string_append(to, md5); | |
195 | name_len = to->len + 4; /* Truncate to this length when renaming */ | |
196 | #ifdef WINDOWS | |
197 | g_string_append_printf(to, ".png.Viking"); | |
198 | #else | |
199 | g_string_append_printf(to, ".png.Viking-%ld", (long) getpid()); | |
200 | #endif | |
201 | ||
202 | g_free(md5); | |
203 | ||
204 | // Thumb::URI must be in ISO-8859-1 encoding otherwise gdk_pixbuf_save() will fail | |
205 | // - e.g. if characters such as 'ě' are encountered | |
206 | // Also see http://en.wikipedia.org/wiki/ISO/IEC_8859-1 | |
207 | // ATM GLIB Manual doesn't specify in which version this function became available | |
208 | // find out that it's fairly recent so may break builds without this test | |
209 | #if GLIB_CHECK_VERSION(2,40,0) | |
210 | char *thumb_uri = g_str_to_ascii ( uri, NULL ); | |
211 | #else | |
212 | char *thumb_uri = g_strdup ( uri ); | |
213 | #endif | |
214 | old_mask = umask(0077); | |
215 | GError *error = NULL; | |
216 | gdk_pixbuf_save(thumb, to->str, "png", &error, | |
217 | "tEXt::Thumb::Image::Width", swidth, | |
218 | "tEXt::Thumb::Image::Height", sheight, | |
219 | "tEXt::Thumb::Size", ssize, | |
220 | "tEXt::Thumb::MTime", smtime, | |
221 | "tEXt::Thumb::URI", thumb_uri, | |
222 | "tEXt::Software", PROJECT, | |
223 | "tEXt::Software::Orientation", orientation ? orientation : "0", | |
224 | NULL); | |
225 | umask(old_mask); | |
226 | g_free(thumb_uri); | |
227 | ||
228 | if (error) { | |
229 | g_warning ( "%s::%s", __FUNCTION__, error->message ); | |
230 | g_error_free ( error ); | |
231 | g_object_unref ( G_OBJECT(thumb) ); | |
232 | thumb = NULL; /* return NULL */ | |
233 | } | |
234 | else | |
235 | /* We create the file ###.png.Viking-PID and rename it to avoid | |
236 | * a race condition if two programs create the same thumb at | |
237 | * once. | |
238 | */ | |
239 | { | |
240 | gchar *final; | |
241 | ||
242 | final = g_strndup(to->str, name_len); | |
243 | if (rename(to->str, final)) | |
244 | { | |
245 | g_warning("Failed to rename '%s' to '%s': %s", | |
246 | to->str, final, g_strerror(errno)); | |
247 | g_object_unref ( G_OBJECT(thumb) ); | |
248 | thumb = NULL; /* return NULL */ | |
249 | } | |
250 | ||
251 | g_free(final); | |
252 | } | |
253 | ||
254 | g_string_free(to, TRUE); | |
255 | g_free(swidth); | |
256 | g_free(sheight); | |
257 | g_free(ssize); | |
258 | g_free(smtime); | |
259 | g_free(uri); | |
260 | ||
261 | return thumb; | |
262 | } | |
263 | ||
264 | ||
265 | GdkPixbuf *a_thumbnails_get(const gchar *pathname) | |
266 | { | |
267 | GdkPixbuf *thumb = NULL; | |
268 | char *thumb_path, *md5, *uri, *path; | |
269 | const char *ssize, *smtime; | |
270 | struct stat info; | |
271 | ||
272 | path = file_realpath_dup(pathname); | |
273 | uri = g_strconcat("file://", path, NULL); | |
274 | md5 = md5_hash(uri); | |
275 | g_free(uri); | |
276 | ||
277 | thumb_path = g_strdup_printf("%s%s.png", thumb_dir, md5); | |
278 | ||
279 | g_free(md5); | |
280 | ||
281 | thumb = gdk_pixbuf_new_from_file(thumb_path, NULL); | |
282 | if (!thumb) | |
283 | goto err; | |
284 | ||
285 | /* Note that these don't need freeing... */ | |
286 | ssize = gdk_pixbuf_get_option(thumb, "tEXt::Thumb::Size"); | |
287 | if (!ssize) | |
288 | goto err; | |
289 | ||
290 | smtime = gdk_pixbuf_get_option(thumb, "tEXt::Thumb::MTime"); | |
291 | if (!smtime) | |
292 | goto err; | |
293 | ||
294 | if (stat(path, &info) != 0) | |
295 | goto err; | |
296 | ||
297 | if (info.st_mtime != atol(smtime) || info.st_size != atol(ssize)) | |
298 | goto err; | |
299 | ||
300 | goto out; | |
301 | err: | |
302 | if (thumb) | |
303 | g_object_unref ( G_OBJECT ( thumb ) ); | |
304 | thumb = NULL; | |
305 | out: | |
306 | g_free(path); | |
307 | g_free(thumb_path); | |
308 | return thumb; | |
309 | } | |
310 | ||
311 | /* | |
312 | * Startup and finish routines | |
313 | */ | |
314 | ||
315 | void a_thumbnails_init () | |
316 | { | |
317 | set_thumb_dir (); | |
318 | } | |
319 | ||
320 | void a_thumbnails_uninit () | |
321 | { | |
322 | g_free ( thumb_dir ); | |
323 | } |