Reduce over-icon-generation due to multiapps
[pandora-libraries.git] / lib / pnd_desktop.c
index c2cffec..f3f4b10 100644 (file)
@@ -11,6 +11,7 @@
 #include "pnd_pndfiles.h"
 #include "pnd_conf.h"
 #include "pnd_desktop.h"
+#include "pnd_logger.h"
 
 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
   char filename [ FILENAME_MAX ];
@@ -23,24 +24,28 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t
   // validation
 
   if ( ! p -> unique_id ) {
+    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
     return ( 0 );
   }
 
   if ( ! p -> exec ) {
+    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing exec\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.desktop", targetpath, p -> unique_id );
+  sprintf ( filename, "%s/%s#%u.desktop", targetpath, p -> unique_id, p -> subapp_number );
 
   // emit
 
@@ -67,9 +72,13 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t
     fprintf ( f, "%s", buffer );
   }
 
-#if 0
-  if ( p -> description_en ) {
-    snprintf ( buffer, 1020, "Comment=%s\n", p -> description_en );
+  if ( p -> unique_id ) {
+    fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
+  }
+
+#if 1
+  if ( p -> desc_en && p -> desc_en [ 0 ] ) {
+    snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
     fprintf ( f, "%s", buffer );
   }
 #endif
@@ -107,8 +116,14 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t
       strncat ( buffer, p -> startdir, 1020 );
     }
 
+    // clockspeed
+    if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
+      strncat ( buffer, " -c ", 1020 );
+      strncat ( buffer, p -> clockspeed, 1020 );
+    }
+
     // exec options
-    if ( p -> option_no_x11 ) {
+    if ( pnd_pxml_is_affirmative ( p -> option_no_x11 ) ) {
       strncat ( buffer, " -x ", 1020 );
     }
 
@@ -185,7 +200,7 @@ unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
   }
 
   // determine filename for target
-  sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
+  sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target
 
   /* first.. open the source file, by type of application:
    * are we looking through a pnd file or a dir?
@@ -250,6 +265,58 @@ unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
   return ( 1 );
 }
 
+#if 1 // we switched direction to freedesktop standard categories
+// if no categories herein, return 0; otherwise, if some category-like-text, return 1
+int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
+  char *t;
+  char *match;
+
+  // clear target so we can easily append
+  memset ( target_buffer, '\0', len );
+
+  // for each main-cat and sub-cat, including alternates, just append them all together
+  // we'll try mapping them, since the categories file is there, but we'll default to
+  // copying over; this lets the conf file do merging or renaming of cagtegories, which
+  // could still be useful, but we can leave the conf file empty to effect a pure
+  // trusted-PXML-copying
+
+  // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
+  // there are hundreds), and would also mean new ones and peoples custom ones would
+  // flop
+
+  /* attempt primary category chain
+   */
+  #define MAPCAT(field)                                            \
+    if ( ( t = d -> field ) ) {                             \
+      match = pnd_map_dotdesktop_category ( c, t );         \
+      strncat ( target_buffer, match ? match : t, len );    \
+      strncat ( target_buffer, ";", len );                  \
+    }
+
+  MAPCAT(main_category);
+  MAPCAT(main_category1);
+  MAPCAT(main_category2);
+  MAPCAT(alt_category);
+  MAPCAT(alt_category1);
+  MAPCAT(alt_category2);
+
+  if ( target_buffer [ 0 ] ) {
+    return ( 1 ); // I guess its 'good'?
+  }
+
+#if 0
+  if ( ( t = d -> main_category ) ) {
+    match = pnd_map_dotdesktop_category ( c, t );
+    strncat ( target_buffer, match ? match : t, len );
+    strncat ( target_buffer, ";", len );
+  }
+#endif
+
+  return ( 0 );
+}
+#endif
+
+#if 0 // we switched direction
 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
   unsigned short int n = 0; // no. matches
@@ -377,6 +444,7 @@ int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsi
 
   return ( n );
 }
+#endif
 
 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
@@ -393,3 +461,85 @@ char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
 
   return ( ret );
 }
+
+unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
+  // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
+  // with a fwrite at the end...
+  char from [ FILENAME_MAX ];   // source filename
+  char bits [ 8 * 1024 ];
+  unsigned int bitlen;
+  FILE *pnd = NULL;
+  unsigned char *target = NULL, *targiter = NULL;
+
+  // 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 ( NULL ); // discover code didn't find it, so FAIL
+  }
+
+  /* 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 ( NULL );
+  }
+
+  // determine length of file, then adjust by icon position to find begin of icon
+  unsigned int len;
+
+  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;
+
+  // create target buffer
+  target = malloc ( len );
+
+  if ( ! target ) {
+    fclose ( pnd );
+    return ( 0 );
+  }
+
+  targiter = target;
+
+  if ( r_buflen ) {
+    *r_buflen = len;
+  }
+
+  // copy over icon to target
+  while ( len ) {
+
+    if ( len > (8*1024) ) {
+      bitlen = (8*1024);
+    } else {
+      bitlen = len;
+    }
+
+    if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
+      fclose ( pnd );
+      free ( target );
+      return ( NULL );
+    }
+
+    memmove ( targiter, bits, bitlen );
+    targiter += bitlen;
+
+    len -= bitlen;
+  } // while
+
+  fclose ( pnd );
+
+  return ( target );
+}