Stick <info> element into PXML.xml .. parser, disco-t, pxml-t
[pandora-libraries.git] / lib / pnd_discovery.c
index 0013b98..eeee25e 100644 (file)
@@ -16,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;
@@ -23,29 +24,24 @@ 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 -> execargs ) {       free ( p -> execargs );    }
+  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 );       }
+  if ( p -> info_name )     {  free ( p -> info_name );       }
+  if ( p -> info_type )     {  free ( p -> info_type );       }
+  if ( p -> info_filename ) {  free ( p -> info_filename );       }
 
   return;
 }
@@ -55,13 +51,20 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
 {
   unsigned char valid = pnd_object_type_unknown;
   pnd_pxml_handle pxmlh = 0;
+  pnd_pxml_handle *pxmlapps = NULL;
+  pnd_pxml_handle *pxmlappiter;
   unsigned int pxml_close_pos = 0;
+  unsigned char logit = pnd_log_do_buried_logging();
 
-  //printf ( "disco root callback encountered '%s'\n", fpath );
+  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 ) {
-    //printf ( " .. is dir, skipping\n" );
+    if ( logit ) {
+      pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
+    }
     return ( 0 ); // skip directories and other non-regular files
   }
 
@@ -75,7 +78,9 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
 
   // if not a file of interest, just keep looking until we run out
   if ( ! valid ) {
-    //printf ( " .. bad filename, skipping\n" );
+    if ( logit ) {
+      pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
+    }
     return ( 0 );
   }
 
@@ -85,7 +90,7 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
     //printf ( "PXML: disco callback encountered '%s'\n", fpath );
 
     // pick up the PXML if we can
-    pxmlh = pnd_pxml_fetch ( (char*) fpath );
+    pxmlapps = pnd_pxml_fetch ( (char*) fpath );
 
   } else if ( valid == pnd_object_type_pnd ) {
     // PND ... ??
@@ -123,16 +128,32 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
       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;
-       }
+
+      unsigned char padtests = 10;
+      unsigned int padstart = ftell ( f );
+      while ( padtests ) {
+
+       if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
+         if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
+           pxml_close_pos = pos;
+           break;
+         } // if
+         fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
+       } // if read
+
+       padtests --;
+      } // while
+
+      if ( ! padtests ) {
+       // no icon found, so back to where we started looking
+       fseek ( f, padstart, SEEK_SET );
       }
+
     } // icon
 #endif
 
     // by now, we have <PXML> .. </PXML>, try to parse..
-    pxmlh = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
+    pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
 
     // done with file
     fclose ( f );
@@ -140,15 +161,32 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
   }
 
   // pxmlh is useful?
-  if ( pxmlh ) {
+  if ( ! pxmlapps ) {
+    return ( 0 ); // continue tree walk
+  }
+
+  // iterate across apps in the PXML
+  pxmlappiter = pxmlapps;
+  while ( 1 ) {
+    pxmlh = *pxmlappiter;
+    pxmlappiter++;
+
+    if ( ! pxmlh ) {
+      break; // all done
+    }
 
     // look for any overrides, if requested
-    pnd_pxml_merge_override ( pxmlh, disco_overrides );
+    if ( disco_overrides ) {
+      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;
+
+      //pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
 
       p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
 
@@ -165,6 +203,9 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
        p -> object_filename = strdup ( fixpxml + 1 );
       }
 
+      // subapp-number
+      p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
+
       // png icon path
       p -> pnd_icon_pos = pxml_close_pos;
 
@@ -175,24 +216,70 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
       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_execargs ( pxmlh ) ) {
+       p -> execargs = strdup ( pnd_pxml_get_execargs ( 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_main_category ( pxmlh ) ) {
-       p -> main_category = strdup ( pnd_pxml_get_main_category ( 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 ) );
+      }
+      // info
+      if ( pnd_pxml_get_info_src ( pxmlh ) ) {
+       p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_info_name ( pxmlh ) ) {
+       p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_info_type ( pxmlh ) ) {
+       p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
+      }
 
     } else {
       //printf ( "Invalid PXML; skipping.\n" );
@@ -201,7 +288,10 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
     // ditch pxml
     pnd_pxml_delete ( pxmlh );
 
-  } // got a pxmlh
+  } // while pxmlh is good
+
+  // free up the applist
+  free ( pxmlapps );
 
   return ( 0 ); // continue the tree walk
 }
@@ -237,187 +327,3 @@ pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
 
   return ( disco_box );
 }
-
-unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, 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 );
-  }
-
-  if ( ! targetpath ) {
-    return ( 0 );
-  }
-
-  if ( ! pndrun ) {
-    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 );
-  }
-
-  fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
-
-  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 // we let pnd_run.sh handle this
-  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 ) {
-
-    // basics
-    if ( p -> object_type == pnd_object_type_directory ) {
-      snprintf ( buffer, 1020, "Exec=%s -p %s -e %s -n", pndrun, p -> object_path, p -> exec );
-    } else if ( p -> object_type == pnd_object_type_pnd ) {
-      snprintf ( buffer, 1020, "Exec=%s -p %s/%s -e %s -n", pndrun, p -> object_path, p -> object_filename, p -> exec );
-    }
-
-    // start dir
-    if ( p -> startdir ) {
-      strncat ( buffer, " -s ", 1020 );
-      strncat ( buffer, p -> startdir, 1020 );
-    }
-
-    // newline
-    strncat ( buffer, "\n", 1020 );
-
-    // emit
-    fprintf ( f, "%s", buffer );
-  }
-
-#if 1 // categories
-  fprintf ( f, "%s\n", "Categories=Application;Network;" );
-#endif
-
-  fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
-
-  fclose ( f );
-
-  return ( 1 );
-}
-
-unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
-  char buffer [ FILENAME_MAX ]; // target filename
-  char from [ FILENAME_MAX ];   // source filename
-  char bits [ 8 * 1024 ];
-  unsigned int bitlen;
-  FILE *pnd, *target;
-
-  // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
-  if ( ( p -> object_type == pnd_object_type_pnd ) &&
-       ( ! p -> pnd_icon_pos ) )
-  {
-    return ( 0 ); // discover code didn't find it, so FAIL
-  }
-
-  // determine filename for target
-  sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
-
-  /* first.. open the source file, by type of application:
-   * are we looking through a pnd file or a dir?
-   */
-  if ( p -> object_type == pnd_object_type_directory ) {
-    sprintf ( from, "%s/%s", p -> object_path, p -> icon );
-  } else if ( p -> object_type == pnd_object_type_pnd ) {
-    sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
-  }
-
-  pnd = fopen ( from, "r" );
-
-  if ( ! pnd ) {
-    return ( 0 );
-  }
-
-  unsigned int len;
-
-  target = fopen ( buffer, "wb" );
-
-  if ( ! target ) {
-    fclose ( pnd );
-    return ( 0 );
-  }
-
-  fseek ( pnd, 0, SEEK_END );
-  len = ftell ( pnd );
-  //fseek ( pnd, 0, SEEK_SET );
-
-  fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
-
-  len -= p -> pnd_icon_pos;
-
-  while ( len ) {
-
-    if ( len > (8*1024) ) {
-      bitlen = (8*1024);
-    } else {
-      bitlen = len;
-    }
-
-    if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
-      fclose ( pnd );
-      fclose ( target );
-      unlink ( buffer );
-      return ( 0 );
-    }
-
-    if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
-      fclose ( pnd );
-      fclose ( target );
-      unlink ( buffer );
-      return ( 0 );
-    }
-
-    len -= bitlen;
-  } // while
-
-  fclose ( pnd );
-  fclose ( target );
-
-  return ( 1 );
-}