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