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