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