Allow parse_dotdesktop() to mark libpnd origin or not, and convenience filter to...
[pandora-libraries.git] / lib / pnd_desktop.c
index da1b01d..dc7aa63 100644 (file)
@@ -3,6 +3,7 @@
 #include <string.h>
 #include <unistd.h> /* for unlink */
 #include <stdlib.h> /* for free */
+#include <sys/stat.h> /* for stat */
 
 #include "pnd_apps.h"
 #include "pnd_container.h"
@@ -103,11 +104,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
@@ -248,7 +251,7 @@ unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p
 
   // set up
 
-  sprintf ( filename, "%s/%s#info.desktop", targetpath, p -> unique_id );
+  sprintf ( filename, "%s/%s#%uinfo.desktop", targetpath, p -> unique_id, p -> subapp_number );
 
   // emit
 
@@ -296,11 +299,14 @@ unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p
   // exec line
   char args [ 1001 ];
   char *pargs = args;
-  if ( pnd_conf_get_as_char ( desktoph, "info.viewer_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 ];
@@ -312,9 +318,11 @@ unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p
     snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
   }
 
-  if ( ! pnd_apps_exec ( pndrun, pndfile, p -> unique_id, viewer, p -> startdir, pargs,
-                        p -> clockspeed ? atoi ( p -> clockspeed ) : 0, PND_EXEC_OPTION_NORUN ) )
-  {
+  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 );
   }
 
@@ -519,7 +527,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 );
@@ -545,7 +553,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, ";" );
@@ -574,7 +582,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 );
@@ -600,7 +608,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, ";" );
@@ -717,3 +725,135 @@ unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen
 
   return ( target );
 }
+
+// parse_dotdesktop() can be used to read a libpnd generated .desktop and return a limited
+// but useful disco-t structure back; possibly useful for scanning .desktops rather than
+// scanning pnd-files?
+pnd_disco_t *pnd_parse_dotdesktop ( char *ddpath, unsigned int flags ) {
+
+  // will verify the .desktop has the libpnd-marking on it (X-Pandora-Source): PND_DOTDESKTOP_SOURCE
+
+  // attempt to extract..
+  // - unique-id (from filename or field)
+  // - subapp number (from filename)
+  // - exec required info
+  // - icon path
+  // - name (title-en)
+  // - comment (desc-en)
+  // - option_no_x11
+  // - object path
+  // - appdata name (or unique-id if not present)
+  // - start dir
+  // - args
+  // - clockspeed
+  // - categories
+
+  // determine file length
+  struct stat statbuf;
+
+  if ( stat ( ddpath, &statbuf) < 0 ) {
+    return ( NULL ); // couldn't open
+  }
+
+  // buffers..
+  char dd [ 1024 ];
+  unsigned char libpnd_origin = 0;
+
+  // disco
+  pnd_disco_t *p = malloc ( sizeof(pnd_disco_t) );
+  if ( ! p ) {
+    return ( NULL );
+  }
+  bzero ( p, sizeof(pnd_disco_t) );
+
+  // inhale file
+  FILE *f = fopen ( ddpath, "r" );
+
+  if ( ! f ) {
+    return ( NULL ); // not up or shut up!
+  }
+
+  while ( fgets ( dd, 1024, f ) ) {
+    char *nl = strchr ( dd, '\n' );
+    if ( nl ) {
+      *nl = '\0';
+    }
+
+    // grep
+    //
+
+    if ( strncmp ( dd, "Name=", 5 ) == 0 ) {
+      p -> title_en = strdup ( dd + 5 );
+    } else if ( strncmp ( dd, "Icon=", 5 ) == 0 ) {
+      p -> icon = strdup ( dd + 5 );
+    } else if ( strcmp ( dd, PND_DOTDESKTOP_SOURCE ) == 0 ) {
+      libpnd_origin = 1;
+    } else if ( strncmp ( dd, "X-Pandora-UID=", 14 ) == 0 ) {
+      p -> unique_id = strdup ( dd + 14 );
+    } else if ( strncmp ( dd, "Comment=", 8 ) == 0 ) {
+      p -> desc_en = strdup ( dd + 8 );
+    } else if ( strncmp ( dd, "Exec=", 5 ) == 0 ) {
+
+      char *e = strstr ( dd, " -e " );
+      if ( e ) {
+       e += 5;
+
+       char *space = strchr ( e, ' ' );
+       p -> exec = strndup ( e, space - e - 1 );
+      }
+
+      char *b = strstr ( dd, " -b " );
+      if ( b ) {
+       b += 5;
+       char *space = strchr ( b, '\0' );
+       p -> appdata_dirname = strndup ( b, space - b - 1 );
+      }
+
+    } else if ( strncmp ( dd, "Categories=", 11 ) == 0 ) {
+      // HACK; only honours first category
+      char *semi = strchr ( dd, ';' );
+      if ( semi ) {
+       p -> main_category = strndup ( dd + 11, semi - dd + 11 );
+      } else {
+       p -> main_category = strdup ( dd + 11 );
+      }
+      semi = strchr ( p -> main_category, ';' );
+      if ( semi ) {
+       *semi = '\0';
+      }
+    }
+
+    //
+    // /grep
+
+  } // while
+
+  fclose ( f );
+
+  // filter
+  if ( ! libpnd_origin ) {
+    p -> object_flags |= PND_DISCO_CUSTOM1; // so caller can do something if it wishes
+
+    // convenience flag
+    if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
+      pnd_disco_destroy ( p );
+      free ( p );
+      return ( NULL );
+    }
+
+  }
+
+  // additional
+  p -> object_type = pnd_object_type_pnd;
+  char *slash = strrchr ( ddpath, '/' );
+  if ( slash ) {
+    p -> object_path = strndup ( ddpath, slash - ddpath );
+    p -> object_filename = strdup ( slash + 1 );
+  } else {
+    p -> object_path = "./";
+    p -> object_filename = strdup ( ddpath );
+  }
+
+  // return disco-t
+  return ( p );
+}