Added config option so can force 'bad categories' into Other using freedesktop list
[pandora-libraries.git] / minimenu / 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   // unique-id to use for the cache mount
107   char *uid = app -> unique_id;
108
109   if ( app -> appdata_dirname ) {
110     uid = app -> appdata_dirname;
111   }
112
113   // if we don't have a file path sorted out yet, means we need to mount and figure it
114   if ( ! filepath [ 0 ] ) {
115     sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
116
117     if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
118       pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
119       return ( 0 ); // couldn't mount?!
120     }
121
122     sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
123   }
124
125   // load whatever path we've got
126   s = IMG_Load ( filepath );
127
128   if ( ! s ) {
129     // unmount it, if mounted
130     if ( fullpath [ 0 ] ) {
131       pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
132     }
133     pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
134     return ( 0 );
135   }
136
137   // try to copy file to the cache, if we're doing that, and if mounted
138   if ( g_pvwcache && fullpath [ 0 ] ) {
139     char cacheoutpath [ PATH_MAX ] = "";
140
141     // figure out where we want to write the file to
142     if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
143       static char *cache_path = NULL;
144       char buffer [ PATH_MAX ];
145       if ( ! cache_path ) {
146         cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
147       }
148       // make the dir
149       snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
150       struct stat statbuf;
151       if ( stat ( buffer, &statbuf ) != 0 ) {
152         snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
153         system ( buffer );
154       }
155       // set up target filename to copy
156       snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
157       pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );   
158       if ( ! pnd_filecopy ( filepath, buffer ) ) {
159         pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );   
160       }
161     } else {
162       pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
163     }
164
165   } // preview file cache
166
167   // unmount it, if mounted
168   if ( fullpath [ 0 ] ) {
169     pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
170   }
171
172   //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
173
174   // scale
175   if ( s -> w < maxwidth ) {
176     // scale up?
177     if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
178       SDL_Surface *scaled;
179       double scale = (double)maxwidth / (double)s -> w;
180       //pnd_log ( pndn_debug, "  Upscaling; scale factor %f\n", scale );
181       scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
182       SDL_FreeSurface ( s );
183       s = scaled;
184     }
185   } else if ( s -> w > maxwidth ) {
186     SDL_Surface *scaled;
187     double scale = (double)maxwidth / (double)s -> w;
188     //pnd_log ( pndn_debug, "  Downscaling; scale factor %f\n", scale );
189     scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
190     SDL_FreeSurface ( s );
191     s = scaled;
192   }
193
194   // add to cache
195   c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
196   bzero ( c, sizeof(mm_cache_t) );
197
198   if ( ! g_preview_cache ) {
199     g_preview_cache = c;
200   } else {
201     c -> next = g_preview_cache;
202     g_preview_cache = c;
203   }
204
205   strncpy ( c -> uniqueid, app -> unique_id, 1000 );
206   c -> i = s;
207
208   return ( 1 );
209 }
210
211 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
212   SDL_Surface *s;
213   mm_cache_t *c;
214
215   // check if already cached
216   if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
217     return ( 1 ); // already got it
218   }
219
220   // not cached, load it up
221   //
222   unsigned char *iconbuf = NULL;
223   unsigned int buflen = 0;
224
225   // same-path override?
226   char ovrfile [ PATH_MAX ];
227   char *fixpxml;
228   sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
229   fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
230   if ( fixpxml ) {
231     strcpy ( fixpxml, ".png" );
232     struct stat statbuf;
233     if ( stat ( ovrfile, &statbuf ) == 0 ) {
234       buflen = statbuf.st_size;
235       if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
236         int fd = open ( ovrfile, O_RDONLY );
237         if ( fd >= 0 ) {
238           if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
239             free ( iconbuf );
240             close ( fd );
241             return ( 0 );
242           }
243           close ( fd );
244         } // open
245       } // malloc
246     } // stat
247   } // ovr?
248
249   // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
250   if ( ! iconbuf ) {
251
252     if (  app -> object_flags & PND_DISCO_GENERATED ) {
253
254       // maybe we can discover this single-file and find an icon?
255       if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
256
257         // looks like a pnd, now what do we do..
258         pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
259
260         if ( h ) {
261           pnd_disco_t *d = pnd_box_get_head ( h );
262           iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
263         }
264
265       } // filename has .pnd?
266
267     } else {
268
269       // pull icon into buffer from .pnd if not already found an icon
270       iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
271
272     } // generated?
273
274   } // already got icon?
275
276   if ( ! iconbuf ) {
277     return ( 0 );
278   }
279
280   // ready up a RWbuffer for SDL
281   SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
282
283   s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
284
285   if ( ! s ) {
286     return ( 0 );
287   }
288
289   free ( iconbuf ); // ditch the icon from ram
290
291   //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
292
293   // scale the icon?
294   if ( s -> w < maxwidth ) {
295     // scale up?
296     if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
297       SDL_Surface *scaled;
298       double scale = (double)maxwidth / (double)s -> w;
299       //pnd_log ( pndn_debug, "  Upscaling; scale factor %f\n", scale );
300       scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
301       SDL_FreeSurface ( s );
302       s = scaled;
303     }
304   } else if ( s -> w > maxwidth ) {
305     SDL_Surface *scaled;
306     double scale = (double)maxwidth / (double)s -> w;
307     //pnd_log ( pndn_debug, "  Downscaling; scale factor %f\n", scale );
308     scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
309     SDL_FreeSurface ( s );
310     s = scaled;
311   }
312
313   // add to cache
314   c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
315   bzero ( c, sizeof(mm_cache_t) );
316
317   if ( ! g_icon_cache ) {
318     g_icon_cache = c;
319   } else {
320     c -> next = g_icon_cache;
321     g_icon_cache = c;
322   }
323
324   strncpy ( c -> uniqueid, app -> unique_id, 1000 );
325   c -> i = s;
326
327   return ( 1 );
328 }
329
330 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
331   mm_cache_t *iter = head;
332
333   if ( ! id ) {
334     return ( NULL );
335   }
336
337   while ( iter ) {
338     if ( iter -> uniqueid &&
339          strcasecmp ( iter -> uniqueid, id ) == 0 )
340     {
341       return ( iter );
342     }
343     iter = iter -> next;
344   } // while
345
346   return ( NULL );
347 }
348
349 mm_cache_t *cache_query_icon ( char *id ) {
350   return ( cache_query ( id, g_icon_cache ) );
351 }
352
353 mm_cache_t *cache_query_preview ( char *id ) {
354   return ( cache_query ( id, g_preview_cache ) );
355 }
356
357 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
358   static char *searchpaths = NULL;
359   static unsigned int minfree = 0;
360   char searchpath [ PATH_MAX ] = "";
361   char cmdbuf [ PATH_MAX ];
362   FILE *f;
363   unsigned int freespace = 0;
364
365   // figure out the mountpoint for the file, and stick that in at front of searchpath
366   // so that it will get picked first, if possible.
367   char mountpath [ PATH_MAX ];
368   if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
369     sprintf ( searchpath, "%s:", mountpath );
370     //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
371   }
372
373   // try to find a device, in order of searchpath, with enough space for this cache-out
374   //
375
376   // populate the searchpath
377   if ( ! searchpaths ) {
378     searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
379     minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
380   }
381
382   if ( ! searchpaths ) {
383     return ( 0 ); // fail!
384   }
385
386   strncat ( searchpath, searchpaths, PATH_MAX );
387
388   SEARCHPATH_PRE
389   {
390
391     // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
392     sprintf ( cmdbuf, "/bin/df %s", buffer );
393
394     f = popen ( cmdbuf, "r" );
395
396     if ( f ) {
397       while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
398         // just eat it up
399         // /dev/sdc2              7471392    725260   6366600  11% /media/IMAGE
400         if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
401           strncpy ( r_writepath, buffer, len );
402           if ( freespace > minfree ) {
403             pclose ( f );
404             return ( 1 );
405           } // enough free?
406         } // df
407       } // while
408       pclose ( f );
409     }
410
411   }
412   SEARCHPATH_POST
413
414   return ( 0 );
415 }