#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 */
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;
//printf ( "disco root callback encountered '%s'\n", fpath );
// 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;
+ valid = pnd_object_type_directory;
} else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
- valid = 2;
+ valid = pnd_object_type_pnd;
}
// if not a file of interest, just keep looking until we run out
}
// potentially a valid application
- if ( valid == 1 ) {
+ if ( valid == pnd_object_type_directory ) {
// Plaintext PXML file
//printf ( "PXML: disco callback encountered '%s'\n", fpath );
// pick up the PXML if we can
pxmlh = pnd_pxml_fetch ( (char*) fpath );
- } else if ( valid == 2 ) {
+ } 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 ( "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?
p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
- // base path
- p -> path_to_object = strdup ( fpath );
+ // base paths
+ p -> object_path = strdup ( fpath );
- if ( ( fixpxml = strcasestr ( p -> path_to_object, PXML_FILENAME ) ) ) {
+ 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 ) );
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 );
}
#endif
-#if 0
+#if 0 // we let pnd_run.sh handle this
if ( p -> startdir ) {
snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
fprintf ( f, "%s", buffer );
#endif
if ( p -> exec ) {
- snprintf ( buffer, 1020, "Exec=%s -p %s -e %s -u\n", pndrun, p -> path_to_object, p -> exec );
+
+ // basics
+ if ( p -> object_type == pnd_object_type_directory ) {
+ snprintf ( buffer, 1020, "Exec=%s -p %s -e %s -u", 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 -u", 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 );
}
- fprintf ( f, "_Source=libpnd\n" ); // should we need to know 'who' created the file during trimming
+#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 );
+}