Changed the osversion to only accept nonNegativeInteger (in the schema).
[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     // *** commented out this stat() 'race test' since it buggers up icon loading at boot, if using .desktop instead of .pnd scanning..
267     if ( stat ( ovrfile, &statbuf ) == 0 /*&& time ( NULL ) - statbuf.st_mtime > 5*/ ) { // race with pndnotifyd
268       buflen = statbuf.st_size;
269       if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
270         int fd = open ( ovrfile, O_RDONLY );
271         if ( fd >= 0 ) {
272           if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
273             free ( iconbuf );
274             close ( fd );
275             return ( 0 );
276           }
277           close ( fd );
278         } // open
279       } // malloc
280     } // stat
281
282   }
283 #endif
284
285   // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
286   if ( ! iconbuf ) {
287
288     if (  app -> object_flags & PND_DISCO_GENERATED ) {
289
290       // maybe we can discover this single-file and find an icon?
291       if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
292
293         // looks like a pnd, now what do we do..
294         pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
295
296         if ( h ) {
297           pnd_disco_t *d = pnd_box_get_head ( h );
298           iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
299         }
300
301       } // filename has .pnd?
302
303     } else {
304
305       // pull icon into buffer from .pnd if not already found an icon
306       iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
307
308     } // generated?
309
310   } // already got icon?
311
312   if ( ! iconbuf ) {
313     return ( 0 );
314   }
315
316   // ready up a RWbuffer for SDL
317   SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
318
319   s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
320
321   if ( ! s ) {
322     return ( 0 );
323   }
324
325   free ( iconbuf ); // ditch the icon from ram
326
327   //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
328
329   // scale the icon?
330   if ( s -> w < maxwidth ) {
331     // scale up?
332     if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
333       SDL_Surface *scaled;
334       double scale = (double)maxwidth / (double)s -> w;
335       //pnd_log ( pndn_debug, "  Upscaling; scale factor %f\n", scale );
336       scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
337       SDL_FreeSurface ( s );
338       s = scaled;
339     }
340   } else if ( s -> w > maxwidth ) {
341     SDL_Surface *scaled;
342     double scale = (double)maxwidth / (double)s -> w;
343     //pnd_log ( pndn_debug, "  Downscaling; scale factor %f\n", scale );
344     scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
345     SDL_FreeSurface ( s );
346     s = scaled;
347   }
348
349   // add to cache
350   c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
351   bzero ( c, sizeof(mm_cache_t) );
352
353   if ( ! g_icon_cache ) {
354     g_icon_cache = c;
355   } else {
356     c -> next = g_icon_cache;
357     g_icon_cache = c;
358   }
359
360   strncpy ( c -> uniqueid, app -> unique_id, 1000 );
361   c -> i = s;
362
363   return ( 1 );
364 }
365
366 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
367   mm_cache_t *iter = head;
368
369   if ( ! id ) {
370     return ( NULL );
371   }
372
373   while ( iter ) {
374     if ( iter -> uniqueid &&
375          strcasecmp ( iter -> uniqueid, id ) == 0 )
376     {
377       return ( iter );
378     }
379     iter = iter -> next;
380   } // while
381
382   return ( NULL );
383 }
384
385 mm_cache_t *cache_query_icon ( char *id ) {
386   return ( cache_query ( id, g_icon_cache ) );
387 }
388
389 mm_cache_t *cache_query_preview ( char *id ) {
390   return ( cache_query ( id, g_preview_cache ) );
391 }
392
393 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
394   static char *searchpaths = NULL;
395   static unsigned int minfree = 0;
396   char searchpath [ PATH_MAX ] = "";
397   char cmdbuf [ PATH_MAX ];
398   FILE *f;
399   unsigned int freespace = 0;
400
401   // figure out the mountpoint for the file, and stick that in at front of searchpath
402   // so that it will get picked first, if possible.
403   char mountpath [ PATH_MAX ];
404   if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
405     sprintf ( searchpath, "%s:", mountpath );
406     //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
407   }
408
409   // try to find a device, in order of searchpath, with enough space for this cache-out
410   //
411
412   // populate the searchpath
413   if ( ! searchpaths ) {
414     searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
415     minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
416   }
417
418   if ( ! searchpaths ) {
419     return ( 0 ); // fail!
420   }
421
422   strncat ( searchpath, searchpaths, PATH_MAX );
423
424   SEARCHPATH_PRE
425   {
426
427     // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
428     sprintf ( cmdbuf, "/bin/df %s", buffer );
429
430     f = popen ( cmdbuf, "r" );
431
432     if ( f ) {
433       while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
434         // just eat it up
435         // /dev/sdc2              7471392    725260   6366600  11% /media/IMAGE
436         if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
437           strncpy ( r_writepath, buffer, len );
438           if ( freespace > minfree ) {
439             pclose ( f );
440             return ( 1 );
441           } // enough free?
442         } // df
443       } // while
444       pclose ( f );
445     }
446
447   }
448   SEARCHPATH_POST
449
450   return ( 0 );
451 }