mmenu: Mostly untested; pushing in list-view changes so porg/pmprog can fiddle easier :)
[pandora-libraries.git] / minimenu / mmcache.c
index c5682f2..8ecd92a 100644 (file)
@@ -5,6 +5,7 @@
 #include <limits.h> /* for PATH_MAX */
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <time.h> /* for time() */
 
 #define __USE_GNU /* for strcasestr */
 #include <string.h> /* for making ftw.h happy */
 #include "pnd_apps.h"
 #include "../lib/pnd_pathiter.h"
 #include "pnd_locate.h"
+#include "pnd_notify.h"
+#include "pnd_dbusnotify.h"
 
 #include "mmenu.h"
 #include "mmapps.h"
 #include "mmcache.h"
 #include "mmcat.h"
 #include "mmui.h"
+#include "mmui_context.h"
 
 extern pnd_conf_handle g_conf;
 extern unsigned char g_pvwcache;
+extern pnd_conf_handle g_desktopconf;
 
 mm_cache_t *g_icon_cache = NULL;
 mm_cache_t *g_preview_cache = NULL;
@@ -103,16 +108,23 @@ unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned
     }
   }
 
+  // unique-id to use for the cache mount
+  char *uid = app -> unique_id;
+
+  if ( app -> appdata_dirname ) {
+    uid = app -> appdata_dirname;
+  }
+
   // if we don't have a file path sorted out yet, means we need to mount and figure it
   if ( ! filepath [ 0 ] ) {
     sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
 
-    if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, app -> unique_id ) ) {
+    if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
       pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
       return ( 0 ); // couldn't mount?!
     }
 
-    sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, app -> unique_id, app -> preview_pic1 );
+    sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
   }
 
   // load whatever path we've got
@@ -121,7 +133,7 @@ unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned
   if ( ! s ) {
     // unmount it, if mounted
     if ( fullpath [ 0 ] ) {
-      pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
+      pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
     }
     pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
     return ( 0 );
@@ -147,9 +159,9 @@ unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned
       }
       // set up target filename to copy
       snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
-      pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );   
+      pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
       if ( ! pnd_filecopy ( filepath, buffer ) ) {
-       pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );   
+       pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
       }
     } else {
       pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
@@ -215,7 +227,7 @@ unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned ch
   unsigned char *iconbuf = NULL;
   unsigned int buflen = 0;
 
-  // same-path override?
+  // same-path icon override?
   char ovrfile [ PATH_MAX ];
   char *fixpxml;
   sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
@@ -239,15 +251,64 @@ unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned ch
     } // stat
   } // ovr?
 
+  // perhaps pndnotifyd has dropped a copy of the icon into /tmp?
+#if 1
+  {
+    static char *iconpath = NULL;
+
+    if ( ! iconpath ) {
+      iconpath = pnd_conf_get_as_char ( g_desktopconf, "desktop.iconpath" );
+    }
+
+    sprintf ( ovrfile, "%s/%s.png", iconpath, app -> unique_id );
+
+    // making sure the file is at least a few seconds old, to help avoid race condition
+    struct stat statbuf;
+    // *** commented out this stat() 'race test' since it buggers up icon loading at boot, if using .desktop instead of .pnd scanning..
+    if ( stat ( ovrfile, &statbuf ) == 0 /*&& time ( NULL ) - statbuf.st_mtime > 5*/ ) { // race with pndnotifyd
+      buflen = statbuf.st_size;
+      if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
+       int fd = open ( ovrfile, O_RDONLY );
+       if ( fd >= 0 ) {
+         if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
+           free ( iconbuf );
+           close ( fd );
+           return ( 0 );
+         }
+         close ( fd );
+       } // open
+      } // malloc
+    } // stat
+
+  }
+#endif
+
   // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
-  if ( ! ( app -> object_flags & PND_DISCO_GENERATED ) ) {
+  if ( ! iconbuf ) {
+
+    if (  app -> object_flags & PND_DISCO_GENERATED ) {
 
-    // pull icon into buffer from .pnd if not already found an icon
-    if ( ! iconbuf ) {
+      // maybe we can discover this single-file and find an icon?
+      if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
+
+       // looks like a pnd, now what do we do..
+       pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
+
+       if ( h ) {
+         pnd_disco_t *d = pnd_box_get_head ( h );
+         iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
+       }
+
+      } // filename has .pnd?
+
+    } else {
+
+      // pull icon into buffer from .pnd if not already found an icon
       iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
-    }
 
-  } // generated?
+    } // generated?
+
+  } // already got icon?
 
   if ( ! iconbuf ) {
     return ( 0 );
@@ -286,6 +347,11 @@ unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned ch
     s = scaled;
   }
 
+  // scale for tiny?
+  SDL_Surface *scaled_tiny = SDL_ConvertSurface ( s, s -> format, s -> flags );
+  extern ui_context_t ui_display_context;
+  scaled_tiny = ui_scale_image ( scaled_tiny, -1 , ui_display_context.text_height );
+
   // add to cache
   c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
   bzero ( c, sizeof(mm_cache_t) );
@@ -299,6 +365,7 @@ unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned ch
 
   strncpy ( c -> uniqueid, app -> unique_id, 1000 );
   c -> i = s;
+  c -> itiny = scaled_tiny;
 
   return ( 1 );
 }