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