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