More logging in pnd_notify
[pandora-libraries.git] / lib / pnd_discovery.c
index 01f4c9a..594a03e 100644 (file)
@@ -1,6 +1,9 @@
 
 #include <stdio.h> /* for FILE etc */
 #include <stdlib.h> /* for malloc */
+#include <unistd.h> /* for unlink */
+
+#define __USE_GNU /* for strcasestr */
 #include <string.h> /* for making ftw.h happy */
 
 #define _XOPEN_SOURCE 500
@@ -13,6 +16,7 @@
 #include "pnd_pathiter.h"
 #include "pnd_apps.h"
 #include "pnd_pndfiles.h"
+#include "pnd_logger.h"
 
 // need these 'globals' due to the way nftw and ftw work :/
 static pnd_box_handle disco_box;
@@ -20,29 +24,20 @@ static char *disco_overrides = NULL;
 
 void pnd_disco_destroy ( pnd_disco_t *p ) {
 
-  if ( p -> title_en ) {
-    free ( p -> title_en );
-  }
-
-  if ( p -> icon ) {
-    free ( p -> icon );
-  }
-
-  if ( p -> exec ) {
-    free ( p -> exec );
-  }
-
-  if ( p -> unique_id ) {
-    free ( p -> unique_id );
-  }
-
-  if ( p -> main_category ) {
-    free ( p -> main_category );
-  }
-
-  if ( p -> clockspeed ) {
-    free ( p -> clockspeed );
-  }
+  if ( p -> title_en ) {       free ( p -> title_en );    }
+  if ( p -> unique_id ) {      free ( p -> unique_id );   }
+  if ( p -> icon )     {       free ( p -> icon );        }
+  if ( p -> exec )     {       free ( p -> exec );        }
+  if ( p -> clockspeed ) {     free ( p -> clockspeed );  }
+  if ( p -> startdir ) {       free ( p -> startdir );    }
+  if ( p -> option_no_x11 ) {  free ( p -> option_no_x11 );  }
+  if ( p -> main_category ) {  free ( p -> main_category );  }
+  if ( p -> main_category1 ) { free ( p -> main_category1 ); }
+  if ( p -> main_category2 ) { free ( p -> main_category2 ); }
+  if ( p -> alt_category ) {   free ( p -> alt_category );   }
+  if ( p -> alt_category1 ) {  free ( p -> alt_category1 );  }
+  if ( p -> alt_category2 ) {  free ( p -> alt_category2 );  }
+  if ( p -> mkdir_sp )      {  free ( p -> mkdir_sp );       }
 
   return;
 }
@@ -50,69 +45,196 @@ void pnd_disco_destroy ( pnd_disco_t *p ) {
 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
                                int typeflag, struct FTW *ftwbuf )
 {
-  unsigned char valid = 0; // 1 for plaintext PXML, 2 for PND...
+  unsigned char valid = pnd_object_type_unknown;
+  pnd_pxml_handle pxmlh = 0;
+  unsigned int pxml_close_pos = 0;
+  unsigned char logit = pnd_log_do_buried_logging();
+
+  if ( logit ) {
+    pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
+  }
 
   // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
   if ( typeflag & FTW_D ) {
+    if ( logit ) {
+      pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
+    }
     return ( 0 ); // skip directories and other non-regular files
   }
 
   // PND/PNZ file and others may be valid as well .. but lets leave that for now
+  //   printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
   if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
-    valid = 1;
-  } else if ( strcasecmp ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) == 0 ) {
-    valid = 2;
+    valid = pnd_object_type_directory;
+  } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
+    valid = pnd_object_type_pnd;
   }
 
   // if not a file of interest, just keep looking until we run out
   if ( ! valid ) {
+    if ( logit ) {
+      pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
+    }
     return ( 0 );
   }
 
   // potentially a valid application
-  if ( valid == 1 ) {
+  if ( valid == pnd_object_type_directory ) {
     // Plaintext PXML file
-    //printf ( "disco callback encountered '%s'\n", fpath );
-
-    pnd_pxml_handle pxmlh;
+    //printf ( "PXML: disco callback encountered '%s'\n", fpath );
 
     // pick up the PXML if we can
-
     pxmlh = pnd_pxml_fetch ( (char*) fpath );
 
-    if ( ! pxmlh ) {
-      return ( 0 ); // continue the scan
+  } else if ( valid == pnd_object_type_pnd ) {
+    // PND ... ??
+    FILE *f;
+    char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
+
+    //printf ( "PND: disco callback encountered '%s'\n", fpath );
+
+    // is this a valid .pnd file? The filename is a candidate already, but verify..
+    // .. presence of PXML appended, or at least contained within?
+    // .. presence of an icon appended after PXML?
+
+    // open it up..
+    f = fopen ( fpath, "r" );
+
+    // try to locate the PXML portion
+    if ( ! pnd_pnd_seek_pxml ( f ) ) {
+      fclose ( f );
+      return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
+    }
+
+    // accrue it into a buffer
+    if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
+      fclose ( f );
+      return ( 0 );
     }
 
+    //printf ( "buffer is %s\n", pxmlbuf );
+    //fflush ( stdout );
+
+#if 1 // icon
+    // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
+    {
+      unsigned int pos = ftell ( f );
+      char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
+      pngbuffer [ 0 ] = 137;      pngbuffer [ 1 ] = 80;      pngbuffer [ 2 ] = 78;      pngbuffer [ 3 ] = 71;
+      pngbuffer [ 4 ] = 13;       pngbuffer [ 5 ] = 10;       pngbuffer [ 6 ] = 26;      pngbuffer [ 7 ] = 10;
+      if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
+       if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
+         pxml_close_pos = pos;
+       }
+      }
+    } // icon
+#endif
+
+    // by now, we have <PXML> .. </PXML>, try to parse..
+    pxmlh = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
+
+    // done with file
+    fclose ( f );
+
+  }
+
+  // pxmlh is useful?
+  if ( pxmlh ) {
+
     // look for any overrides, if requested
     pnd_pxml_merge_override ( pxmlh, disco_overrides );
 
     // check for validity and add to resultset if it looks executable
     if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
       pnd_disco_t *p;
+      char *fixpxml;
+      char *z;
 
       p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
-      p -> title_en = strdup ( pnd_pxml_get_app_name ( pxmlh ) );
-      p -> icon = strdup ( pnd_pxml_get_icon_path ( pxmlh ) );
-      p -> exec = strdup ( pnd_pxml_get_exec_path ( pxmlh ) );
-      p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
-      p -> main_category = strdup ( pnd_pxml_get_primary_category ( pxmlh ) );
-      p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) ); 
 
+      // base paths
+      p -> object_path = strdup ( fpath );
+
+      if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
+       *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
+      } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
+       *(fixpxml+1) = '\0'; // for pnd, lop off to last /
+      }
+
+      if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
+       p -> object_filename = strdup ( fixpxml + 1 );
+      }
+
+      // png icon path
+      p -> pnd_icon_pos = pxml_close_pos;
+
+      // type
+      p -> object_type = valid;
+
+      // PXML fields
+      if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
+       p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_description_en ( pxmlh ) ) {
+       p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_icon ( pxmlh ) ) {
+       p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_exec ( pxmlh ) ) {
+       p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
+       p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
+       p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
+       p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) ); 
+      }
+      if ( pnd_pxml_get_startdir ( pxmlh ) ) {
+       p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) ); 
+      }
+      // category kruft
+      if ( pnd_pxml_get_main_category ( pxmlh ) ) {
+       p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
+       p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
+       p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
+       p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
+       p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
+       p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
+      }
+      // preview pics
+      if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
+       p -> preview_pic1 = strdup ( z );
+      }
+      if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
+       p -> preview_pic2 = strdup ( z );
+      }
+      // mkdirs
+      if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
+       p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
+      }
+
+    } else {
+      //printf ( "Invalid PXML; skipping.\n" );
     }
 
     // ditch pxml
     pnd_pxml_delete ( pxmlh );
 
-  } else if ( valid == 2 ) {
-    // PND ... ??
-    printf ( "PND: disco callback encountered '%s'\n", fpath );
-
-    // is this a valid .pnd file? The filename is a candidate already, but verify..
-    // .. presence of PXML appeneded, or at least contained within?
-    // .. presence of an icon appended after PXML?
-
-  }
+  } // got a pxmlh
 
   return ( 0 ); // continue the tree walk
 }
@@ -148,76 +270,3 @@ pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
 
   return ( disco_box );
 }
-
-unsigned char pnd_emit_dotdesktop ( char *targetpath, pnd_disco_t *p ) {
-  char filename [ FILENAME_MAX ];
-  char buffer [ 1024 ];
-  FILE *f;
-
-  // specification
-  // http://standards.freedesktop.org/desktop-entry-spec
-
-  // validation
-
-  if ( ! p -> unique_id ) {
-    return ( 0 );
-  }
-
-  if ( ! p -> exec ) {
-    return ( 0 );
-  }
-
-  // set up
-
-  sprintf ( filename, "%s/%s.desktop", targetpath, p -> unique_id );
-
-  // emit
-
-  //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
-
-  f = fopen ( filename, "w" );
-
-  if ( ! f ) {
-    return ( 0 );
-  }
-
-  if ( p -> title_en ) {
-    snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
-    fprintf ( f, "%s", buffer );
-  }
-
-  fprintf ( f, "Type=Application\n" );
-  fprintf ( f, "Version=1.0\n" );
-
-  if ( p -> icon ) {
-    snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
-    fprintf ( f, "%s", buffer );
-  }
-
-#if 0
-  if ( p -> description_en ) {
-    snprintf ( buffer, 1020, "Comment=%s\n", p -> description_en );
-    fprintf ( f, "%s", buffer );
-  }
-#endif
-
-#if 0
-  if ( p -> startdir ) {
-    snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
-    fprintf ( f, "%s", buffer );
-  } else {
-    fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
-  }
-#endif
-
-  if ( p -> exec ) {
-    snprintf ( buffer, 1020, "Exec=%s\n", p -> exec );
-    fprintf ( f, "%s", buffer );
-  }
-
-  fprintf ( f, "_Source=libpnd\n" ); // should we need to know 'who' created the file during trimming
-
-  fclose ( f );
-
-  return ( 1 );
-}