2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <unistd.h> /* for unlink */
5 #include <limits.h> /* for PATH_MAX */
9 #define __USE_GNU /* for strcasestr */
10 #include <string.h> /* for making ftw.h happy */
16 #include "SDL_image.h"
17 #include "SDL_rotozoom.h"
19 #define __USE_GNU /* for strcasestr */
20 #include <unistd.h> /* for unlink */
21 #include <string.h> /* for making ftw.h happy */
24 #include "pnd_utility.h"
26 #include "pnd_container.h"
27 #include "pnd_discovery.h"
28 #include "pnd_logger.h"
29 #include "pnd_desktop.h"
30 #include "pnd_pndfiles.h"
32 #include "../lib/pnd_pathiter.h"
33 #include "pnd_locate.h"
34 #include "pnd_notify.h"
35 #include "pnd_dbusnotify.h"
43 extern pnd_conf_handle g_conf;
44 extern unsigned char g_pvwcache;
46 mm_cache_t *g_icon_cache = NULL;
47 mm_cache_t *g_preview_cache = NULL;
49 unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
53 // does this sucker even have a preview?
54 if ( ! app -> preview_pic1 ) {
55 return ( 1 ); // nothing here, so thats fine
58 // check if already cached
59 if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
60 return ( 1 ); // already got it
63 // not cached, load it up
67 ui_show_hourglass ( 1 /* updaterect*/ );
69 // see if we can mount the pnd/dir
70 // does preview file exist?
71 // if so, load it up, size it, cache it
72 // if not, warning and bail
75 // can we mount? or can we find it in preview cache? or an override?
76 char fullpath [ PATH_MAX ] = "";
77 char filepath [ PATH_MAX ] = "";
79 // first, check for preview override
81 char ovrfile [ PATH_MAX ];
83 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
84 fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
86 sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
88 if ( stat ( ovrfile, &statbuf ) == 0 ) {
89 strncpy ( filepath, ovrfile, PATH_MAX );
94 // if not yet found, try to find in cache
95 if ( filepath [ 0 ] == '\0' && g_pvwcache ) {
96 static char *cache_findpath = NULL;
97 if ( ! cache_findpath ) {
98 cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
100 char buffer [ FILENAME_MAX ];
101 sprintf ( buffer, "%s.png", app -> unique_id );
102 char *f = pnd_locate_filename ( cache_findpath, buffer );
104 strncpy ( filepath, f, PATH_MAX );
108 // unique-id to use for the cache mount
109 char *uid = app -> unique_id;
111 if ( app -> appdata_dirname ) {
112 uid = app -> appdata_dirname;
115 // if we don't have a file path sorted out yet, means we need to mount and figure it
116 if ( ! filepath [ 0 ] ) {
117 sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
119 if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
120 pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
121 return ( 0 ); // couldn't mount?!
124 sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
127 // load whatever path we've got
128 s = IMG_Load ( filepath );
131 // unmount it, if mounted
132 if ( fullpath [ 0 ] ) {
133 pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
135 pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
139 // try to copy file to the cache, if we're doing that, and if mounted
140 if ( g_pvwcache && fullpath [ 0 ] ) {
141 char cacheoutpath [ PATH_MAX ] = "";
143 // figure out where we want to write the file to
144 if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
145 static char *cache_path = NULL;
146 char buffer [ PATH_MAX ];
147 if ( ! cache_path ) {
148 cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
151 snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
153 if ( stat ( buffer, &statbuf ) != 0 ) {
154 snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
157 // set up target filename to copy
158 snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
159 pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
160 if ( ! pnd_filecopy ( filepath, buffer ) ) {
161 pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
164 pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
167 } // preview file cache
169 // unmount it, if mounted
170 if ( fullpath [ 0 ] ) {
171 pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
174 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
177 if ( s -> w < maxwidth ) {
179 if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
181 double scale = (double)maxwidth / (double)s -> w;
182 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
183 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
184 SDL_FreeSurface ( s );
187 } else if ( s -> w > maxwidth ) {
189 double scale = (double)maxwidth / (double)s -> w;
190 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
191 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
192 SDL_FreeSurface ( s );
197 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
198 bzero ( c, sizeof(mm_cache_t) );
200 if ( ! g_preview_cache ) {
203 c -> next = g_preview_cache;
207 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
213 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
217 // check if already cached
218 if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
219 return ( 1 ); // already got it
222 // not cached, load it up
224 unsigned char *iconbuf = NULL;
225 unsigned int buflen = 0;
227 // same-path override?
228 char ovrfile [ PATH_MAX ];
230 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
231 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
233 strcpy ( fixpxml, ".png" );
235 if ( stat ( ovrfile, &statbuf ) == 0 ) {
236 buflen = statbuf.st_size;
237 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
238 int fd = open ( ovrfile, O_RDONLY );
240 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
251 // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
254 if ( app -> object_flags & PND_DISCO_GENERATED ) {
256 // maybe we can discover this single-file and find an icon?
257 if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
259 // looks like a pnd, now what do we do..
260 pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
263 pnd_disco_t *d = pnd_box_get_head ( h );
264 iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
267 } // filename has .pnd?
271 // pull icon into buffer from .pnd if not already found an icon
272 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
276 } // already got icon?
282 // ready up a RWbuffer for SDL
283 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
285 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
291 free ( iconbuf ); // ditch the icon from ram
293 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
296 if ( s -> w < maxwidth ) {
298 if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
300 double scale = (double)maxwidth / (double)s -> w;
301 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
302 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
303 SDL_FreeSurface ( s );
306 } else if ( s -> w > maxwidth ) {
308 double scale = (double)maxwidth / (double)s -> w;
309 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
310 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
311 SDL_FreeSurface ( s );
316 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
317 bzero ( c, sizeof(mm_cache_t) );
319 if ( ! g_icon_cache ) {
322 c -> next = g_icon_cache;
326 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
332 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
333 mm_cache_t *iter = head;
340 if ( iter -> uniqueid &&
341 strcasecmp ( iter -> uniqueid, id ) == 0 )
351 mm_cache_t *cache_query_icon ( char *id ) {
352 return ( cache_query ( id, g_icon_cache ) );
355 mm_cache_t *cache_query_preview ( char *id ) {
356 return ( cache_query ( id, g_preview_cache ) );
359 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
360 static char *searchpaths = NULL;
361 static unsigned int minfree = 0;
362 char searchpath [ PATH_MAX ] = "";
363 char cmdbuf [ PATH_MAX ];
365 unsigned int freespace = 0;
367 // figure out the mountpoint for the file, and stick that in at front of searchpath
368 // so that it will get picked first, if possible.
369 char mountpath [ PATH_MAX ];
370 if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
371 sprintf ( searchpath, "%s:", mountpath );
372 //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
375 // try to find a device, in order of searchpath, with enough space for this cache-out
378 // populate the searchpath
379 if ( ! searchpaths ) {
380 searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
381 minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
384 if ( ! searchpaths ) {
385 return ( 0 ); // fail!
388 strncat ( searchpath, searchpaths, PATH_MAX );
393 // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
394 sprintf ( cmdbuf, "/bin/df %s", buffer );
396 f = popen ( cmdbuf, "r" );
399 while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
401 // /dev/sdc2 7471392 725260 6366600 11% /media/IMAGE
402 if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
403 strncpy ( r_writepath, buffer, len );
404 if ( freespace > minfree ) {