get rid of EOL spaces
[pandora-libraries.git] / lib / pnd_desktop.c
index f3f4b10..84e420a 100644 (file)
@@ -83,7 +83,7 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t
   }
 #endif
 
-#if 0 // we let pnd_run.sh handle this
+#if 0 // we let pnd_run.sh command line handle this instead of in .desktop
   if ( p -> startdir ) {
     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
     fprintf ( f, "%s", buffer );
@@ -103,11 +103,13 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t
 
     // basics
     if ( p -> object_type == pnd_object_type_directory ) {
-      snprintf ( buffer, 1020, "Exec=%s%s -p %s -e %s -b %s",
-                nohup, pndrun, p -> object_path, p -> exec, p -> unique_id );
+      snprintf ( buffer, 1020, "Exec=%s%s -p \"%s\" -e \"%s\" -b \"%s\"",
+                nohup, pndrun, p -> object_path, p -> exec,
+                p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
     } else if ( p -> object_type == pnd_object_type_pnd ) {
-      snprintf ( buffer, 1020, "Exec=%s%s -p %s/%s -e %s -b %s",
-                nohup, pndrun, p -> object_path, p -> object_filename, p -> exec, p -> unique_id );
+      snprintf ( buffer, 1020, "Exec=%s%s -p \"%s/%s\" -e \"%s\" -b \"%s\"",
+                nohup, pndrun, p -> object_path, p -> object_filename, p -> exec,
+                p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
     }
 
     // start dir
@@ -116,6 +118,13 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t
       strncat ( buffer, p -> startdir, 1020 );
     }
 
+    // args
+    if ( p -> execargs ) {
+      char argbuf [ 1024 ];
+      snprintf ( argbuf, 1000, " -a \"%s\"", p -> execargs );
+      strncat ( buffer, argbuf, 1020 );
+    }
+
     // clockspeed
     if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
       strncat ( buffer, " -c ", 1020 );
@@ -123,7 +132,7 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t
     }
 
     // exec options
-    if ( pnd_pxml_is_affirmative ( p -> option_no_x11 ) ) {
+    if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
       strncat ( buffer, " -x ", 1020 );
     }
 
@@ -185,10 +194,158 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t
   return ( 1 );
 }
 
+unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
+  char filename [ FILENAME_MAX ];
+  char buffer [ 1024 ];
+  FILE *f;
+  char *viewer, *searchpath;
+  pnd_conf_handle desktoph;
+
+  // specification
+  // http://standards.freedesktop.org/desktop-entry-spec
+
+  // validation
+  //
+
+  // viewer
+  searchpath = pnd_conf_query_searchpath();
+
+  desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
+
+  if ( ! desktoph ) {
+    return ( 0 );
+  }
+
+  viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
+
+  if ( ! viewer ) {
+    return ( 0 ); // no way to view the file
+  }
+
+  // etc
+  if ( ! p -> unique_id ) {
+    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
+    return ( 0 );
+  }
+
+  if ( ! p -> info_filename ) {
+    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_filename\n", targetpath );
+    return ( 0 );
+  }
+
+  if ( ! p -> info_name ) {
+    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_name\n", targetpath );
+    return ( 0 );
+  }
+
+  if ( ! targetpath ) {
+    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
+    return ( 0 );
+  }
+
+  if ( ! pndrun ) {
+    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
+    return ( 0 );
+  }
+
+  // set up
+
+  sprintf ( filename, "%s/%s#%uinfo.desktop", targetpath, p -> unique_id, p -> subapp_number );
+
+  // emit
+
+  f = fopen ( filename, "w" );
+
+  if ( ! f ) {
+    return ( 0 );
+  }
+
+  fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
+
+  if ( p -> info_name ) {
+    snprintf ( buffer, 1020, "Name=%s\n", p -> info_name );
+    fprintf ( f, "%s", buffer );
+  }
+
+  fprintf ( f, "Type=Application\n" );
+  fprintf ( f, "Version=1.0\n" );
+
+#if 0
+  if ( p -> icon ) {
+    snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
+    fprintf ( f, "%s", buffer );
+  }
+#endif
+
+  if ( p -> unique_id ) {
+    fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
+  }
+
+  if ( p -> title_en && p -> title_en [ 0 ] ) {
+    snprintf ( buffer, 1020, "Comment=Automatic menu info entry for %s\n", p -> title_en );
+    fprintf ( f, "%s", buffer );
+  }
+
+#if 0 // we let pnd_run.sh command line handle this instead of in .desktop
+  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
+
+  // exec line
+  char args [ 1001 ];
+  char *pargs = args;
+  char *viewerargs = pnd_conf_get_as_char ( desktoph, "info.viewer_args" );
+  if ( viewerargs && viewerargs [ 0 ] ) {
+    snprintf ( pargs, 1001, "%s %s",
+              pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
+  } else {
+    pargs = NULL;
+    // WARNING: This might not be quite right; if no viewer-args, shouldn't we still append the info-filename? likewise,
+    //          even if we do have view-args, shouldn't we check if filename is present?
+  }
+
+  char pndfile [ 1024 ];
+  if ( p -> object_type == pnd_object_type_directory ) {
+    // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
+    strncpy ( pndfile, p -> object_path, 1000 );
+  } else if ( p -> object_type == pnd_object_type_pnd ) {
+    // pnd_run.sh wants the full path and filename for the .pnd file
+    snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
+  }
+
+  pnd_apps_exec_info_t info;
+  info.viewer = viewer;
+  info.args = pargs;
+
+  if ( ! pnd_apps_exec_disco ( pndrun, p, PND_EXEC_OPTION_NORUN | PND_EXEC_OPTION_INFO, &info ) ) {
+    return ( 0 );
+  }
+
+  fprintf ( f, "Exec=%s\n", pnd_apps_exec_runline() );
+
+  if ( pnd_conf_get_as_char ( desktoph, "info.category" ) ) {
+    fprintf ( f, "Categories=%s\n", pnd_conf_get_as_char ( desktoph, "info.category" ) );
+  } else {
+    fprintf ( f, "Categories=Documentation\n" );
+  }
+
+  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 ) {
+  //#define BITLEN (8*1024)
+#define BITLEN (64*1024)
   char buffer [ FILENAME_MAX ]; // target filename
   char from [ FILENAME_MAX ];   // source filename
-  char bits [ 8 * 1024 ];
+  unsigned char bits [ BITLEN ];
   unsigned int bitlen;
   FILE *pnd, *target;
 
@@ -211,9 +368,10 @@ unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
   }
 
-  pnd = fopen ( from, "r" );
+  pnd = fopen ( from, "rb" );
 
   if ( ! pnd ) {
+    pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open source\n" );
     return ( 0 );
   }
 
@@ -223,6 +381,7 @@ unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
 
   if ( ! target ) {
     fclose ( pnd );
+    pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open target\n" );
     return ( 0 );
   }
 
@@ -234,10 +393,12 @@ unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
 
   len -= p -> pnd_icon_pos;
 
+  pnd_log ( PND_LOG_DEFAULT, "    Emit icon, length: %u\n", len );
+
   while ( len ) {
 
-    if ( len > (8*1024) ) {
-      bitlen = (8*1024);
+    if ( len > (BITLEN) ) {
+      bitlen = (BITLEN);
     } else {
       bitlen = len;
     }
@@ -246,22 +407,42 @@ unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
       fclose ( pnd );
       fclose ( target );
       unlink ( buffer );
+      pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad read\n" );
       return ( 0 );
     }
 
+#if 0
+    {
+      unsigned int i = 0;
+      char bigbuffer [ 200 * 1024 ] = "\0";
+      char b [ 10 ];
+      pnd_log ( PND_LOG_DEFAULT, "    Read hexdump\n" );
+      while ( i < bitlen ) {
+       sprintf ( b, "%x,", bits [ i ] );
+       strcat ( bigbuffer, b );
+       i++;
+      }
+      pnd_log ( PND_LOG_DEFAULT, bigbuffer );
+    }
+#endif
+
     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
       fclose ( pnd );
       fclose ( target );
       unlink ( buffer );
+      pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad write\n" );
       return ( 0 );
     }
 
     len -= bitlen;
+    //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, next block, length: %u\n", len );
   } // while
 
   fclose ( pnd );
   fclose ( target );
 
+  //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, done.\n" );
+
   return ( 1 );
 }
 
@@ -345,7 +526,7 @@ int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsi
   {
     match = pnd_map_dotdesktop_category ( c, t );
   }
-  
+
   if ( match ) {
     strncat ( target_buffer, match, len );
     len -= strlen ( target_buffer );
@@ -371,7 +552,7 @@ int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsi
   {
     match = pnd_map_dotdesktop_category ( c, t );
   }
-  
+
   if ( match ) {
     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
       strcat ( target_buffer, ";" );
@@ -400,7 +581,7 @@ int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsi
   {
     match = pnd_map_dotdesktop_category ( c, t );
   }
-  
+
   if ( match ) {
     strncat ( target_buffer, match, len );
     len -= strlen ( target_buffer );
@@ -426,7 +607,7 @@ int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsi
   {
     match = pnd_map_dotdesktop_category ( c, t );
   }
-  
+
   if ( match ) {
     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
       strcat ( target_buffer, ";" );