4480a12d35ef7046c9456b86e0f0c6a44efc4cce
[pandora-libraries.git] / mmcache.c
1
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 */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8
9 #define __USE_GNU /* for strcasestr */
10 #include <string.h> /* for making ftw.h happy */
11
12 #include <fcntl.h>
13 #include <limits.h>
14
15 #include "SDL.h"
16 #include "SDL_image.h"
17 #include "SDL_rotozoom.h"
18
19 #define __USE_GNU /* for strcasestr */
20 #include <unistd.h> /* for unlink */
21 #include <string.h> /* for making ftw.h happy */
22
23 #include "pnd_pxml.h"
24 #include "pnd_utility.h"
25 #include "pnd_conf.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"
31 #include "pnd_apps.h"
32 #include "../lib/pnd_pathiter.h"
33 #include "pnd_locate.h"
34
35 #include "mmenu.h"
36 #include "mmapps.h"
37 #include "mmcache.h"
38 #include "mmcat.h"
39 #include "mmui.h"
40
41 extern pnd_conf_handle g_conf;
42 extern unsigned char g_pvwcache;
43
44 mm_cache_t *g_icon_cache = NULL;
45 mm_cache_t *g_preview_cache = NULL;
46
47 unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
48   SDL_Surface *s;
49   mm_cache_t *c;
50
51   // does this sucker even have a preview?
52   if ( ! app -> preview_pic1 ) {
53     return ( 1 ); // nothing here, so thats fine
54   }
55
56   // check if already cached
57   if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
58     return ( 1 ); // already got it
59   }
60
61   // not cached, load it up
62   //
63
64   // show hourglass
65   ui_show_hourglass ( 1 /* updaterect*/ );
66
67   // see if we can mount the pnd/dir
68   // does preview file exist?
69   //   if so, load it up, size it, cache it
70   //   if not, warning and bail
71   // unmount it
72
73   // can we mount? or can we find it in preview cache? or an override?
74   char fullpath [ PATH_MAX ] = "";
75   char filepath [ PATH_MAX ] = "";
76
77   // first, check for preview override
78   {
79     char ovrfile [ PATH_MAX ];
80     char *fooby;
81     sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
82     fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
83     if ( fooby ) {
84       sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
85       struct stat statbuf;
86       if ( stat ( ovrfile, &statbuf ) == 0 ) {
87         strncpy ( filepath, ovrfile, PATH_MAX );
88       } // stat
89     } // ovr?
90   }
91
92   // if not yet found, try to find in cache
93   if ( filepath [ 0 ] == '\0' && g_pvwcache ) {
94     static char *cache_findpath = NULL;
95     if ( ! cache_findpath ) {
96       cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
97     }
98     char buffer [ FILENAME_MAX ];
99     sprintf ( buffer, "%s.png", app -> unique_id );
100     char *f = pnd_locate_filename ( cache_findpath, buffer );
101     if ( f ) {
102       strncpy ( filepath, f, PATH_MAX );
103     }
104   }
105
106   // if we don't have a file path sorted out yet, means we need to mount and figure it
107   if ( ! filepath [ 0 ] ) {
108     sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
109
110     if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, app -> unique_id ) ) {
111       pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
112       return ( 0 ); // couldn't mount?!
113     }
114
115     sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, app -> unique_id, app -> preview_pic1 );
116   }
117
118   // load whatever path we've got
119   s = IMG_Load ( filepath );
120
121   if ( ! s ) {
122     // unmount it, if mounted
123     if ( fullpath [ 0 ] ) {
124       pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
125     }
126     pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
127     return ( 0 );
128   }
129
130   // try to copy file to the cache, if we're doing that, and if mounted
131   if ( g_pvwcache && fullpath [ 0 ] ) {
132     char cacheoutpath [ PATH_MAX ] = "";
133
134     // figure out where we want to write the file to
135     if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
136       static char *cache_path = NULL;
137       char buffer [ PATH_MAX ];
138       if ( ! cache_path ) {
139         cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
140       }
141       // make the dir
142       snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
143       struct stat statbuf;
144       if ( stat ( buffer, &statbuf ) != 0 ) {
145         snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
146         system ( buffer );
147       }
148       // set up target filename to copy
149       snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
150       pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );   
151       if ( ! pnd_filecopy ( filepath, buffer ) ) {
152         pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );   
153       }
154     } else {
155       pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
156     }
157
158   } // preview file cache
159
160   // unmount it, if mounted
161   if ( fullpath [ 0 ] ) {
162     pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
163   }
164
165   //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
166
167   // scale
168   if ( s -> w < maxwidth ) {
169     // scale up?
170     if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
171       SDL_Surface *scaled;
172       double scale = (double)maxwidth / (double)s -> w;
173       //pnd_log ( pndn_debug, "  Upscaling; scale factor %f\n", scale );
174       scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
175       SDL_FreeSurface ( s );
176       s = scaled;
177     }
178   } else if ( s -> w > maxwidth ) {
179     SDL_Surface *scaled;
180     double scale = (double)maxwidth / (double)s -> w;
181     //pnd_log ( pndn_debug, "  Downscaling; scale factor %f\n", scale );
182     scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
183     SDL_FreeSurface ( s );
184     s = scaled;
185   }
186
187   // add to cache
188   c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
189   bzero ( c, sizeof(mm_cache_t) );
190
191   if ( ! g_preview_cache ) {
192     g_preview_cache = c;
193   } else {
194     c -> next = g_preview_cache;
195     g_preview_cache = c;
196   }
197
198   strncpy ( c -> uniqueid, app -> unique_id, 1000 );
199   c -> i = s;
200
201   return ( 1 );
202 }
203
204 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
205   SDL_Surface *s;
206   mm_cache_t *c;
207
208   // check if already cached
209   if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
210     return ( 1 ); // already got it
211   }
212
213   // not cached, load it up
214   //
215   unsigned char *iconbuf = NULL;
216   unsigned int buflen = 0;
217
218   // same-path override?
219   char ovrfile [ PATH_MAX ];
220   char *fixpxml;
221   sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
222   fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
223   if ( fixpxml ) {
224     strcpy ( fixpxml, ".png" );
225     struct stat statbuf;
226     if ( stat ( ovrfile, &statbuf ) == 0 ) {
227       buflen = statbuf.st_size;
228       if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
229         int fd = open ( ovrfile, O_RDONLY );
230         if ( fd >= 0 ) {
231           if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
232             free ( iconbuf );
233             close ( fd );
234             return ( 0 );
235           }
236           close ( fd );
237         } // open
238       } // malloc
239     } // stat
240   } // ovr?
241
242   // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
243   if ( ! iconbuf ) {
244
245     if (  app -> object_flags & PND_DISCO_GENERATED ) {
246
247       // maybe we can discover this single-file and find an icon?
248       if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
249
250         // looks like a pnd, now what do we do..
251         pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
252
253         if ( h ) {
254           pnd_disco_t *d = pnd_box_get_head ( h );
255           iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
256         }
257
258       } // filename has .pnd?
259
260     } else {
261
262       // pull icon into buffer from .pnd if not already found an icon
263       iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
264
265     } // generated?
266
267   } // already got icon?
268
269   if ( ! iconbuf ) {
270     return ( 0 );
271   }
272
273   // ready up a RWbuffer for SDL
274   SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
275
276   s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
277
278   if ( ! s ) {
279     return ( 0 );
280   }
281
282   free ( iconbuf ); // ditch the icon from ram
283
284   //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
285
286   // scale the icon?
287   if ( s -> w < maxwidth ) {
288     // scale up?
289     if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
290       SDL_Surface *scaled;
291       double scale = (double)maxwidth / (double)s -> w;
292       //pnd_log ( pndn_debug, "  Upscaling; scale factor %f\n", scale );
293       scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
294       SDL_FreeSurface ( s );
295       s = scaled;
296     }
297   } else if ( s -> w > maxwidth ) {
298     SDL_Surface *scaled;
299     double scale = (double)maxwidth / (double)s -> w;
300     //pnd_log ( pndn_debug, "  Downscaling; scale factor %f\n", scale );
301     scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
302     SDL_FreeSurface ( s );
303     s = scaled;
304   }
305
306   // add to cache
307   c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
308   bzero ( c, sizeof(mm_cache_t) );
309
310   if ( ! g_icon_cache ) {
311     g_icon_cache = c;
312   } else {
313     c -> next = g_icon_cache;
314     g_icon_cache = c;
315   }
316
317   strncpy ( c -> uniqueid, app -> unique_id, 1000 );
318   c -> i = s;
319
320   return ( 1 );
321 }
322
323 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
324   mm_cache_t *iter = head;
325
326   if ( ! id ) {
327     return ( NULL );
328   }
329
330   while ( iter ) {
331     if ( iter -> uniqueid &&
332          strcasecmp ( iter -> uniqueid, id ) == 0 )
333     {
334       return ( iter );
335     }
336     iter = iter -> next;
337   } // while
338
339   return ( NULL );
340 }
341
342 mm_cache_t *cache_query_icon ( char *id ) {
343   return ( cache_query ( id, g_icon_cache ) );
344 }
345
346 mm_cache_t *cache_query_preview ( char *id ) {
347   return ( cache_query ( id, g_preview_cache ) );
348 }
349
350 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
351   static char *searchpaths = NULL;
352   static unsigned int minfree = 0;
353   char searchpath [ PATH_MAX ] = "";
354   char cmdbuf [ PATH_MAX ];
355   FILE *f;
356   unsigned int freespace = 0;
357
358   // figure out the mountpoint for the file, and stick that in at front of searchpath
359   // so that it will get picked first, if possible.
360   char mountpath [ PATH_MAX ];
361   if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
362     sprintf ( searchpath, "%s:", mountpath );
363     //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
364   }
365
366   // try to find a device, in order of searchpath, with enough space for this cache-out
367   //
368
369   // populate the searchpath
370   if ( ! searchpaths ) {
371     searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
372     minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
373   }
374
375   if ( ! searchpaths ) {
376     return ( 0 ); // fail!
377   }
378
379   strncat ( searchpath, searchpaths, PATH_MAX );
380
381   SEARCHPATH_PRE
382   {
383
384     // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
385     sprintf ( cmdbuf, "/bin/df %s", buffer );
386
387     f = popen ( cmdbuf, "r" );
388
389     if ( f ) {
390       while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
391         // just eat it up
392         // /dev/sdc2              7471392    725260   6366600  11% /media/IMAGE
393         if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
394           strncpy ( r_writepath, buffer, len );
395           if ( freespace > minfree ) {
396             pclose ( f );
397             return ( 1 );
398           } // enough free?
399         } // df
400       } // while
401       pclose ( f );
402     }
403
404   }
405   SEARCHPATH_POST
406
407   return ( 0 );
408 }