+
+#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
+ char *t;
+ char *match;
+
+ // clear target so we can easily append
+ memset ( target_buffer, '\0', len );
+
+ /* attempt primary category chain
+ */
+ match = NULL;
+
+ if ( ( t = d -> main_category ) ) {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( ( ! match ) &&
+ ( t = d -> main_category1 ) )
+ {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( ( ! match ) &&
+ ( t = d -> main_category2 ) )
+ {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( match ) {
+ strncat ( target_buffer, match, len );
+ len -= strlen ( target_buffer );
+ n += 1;
+ }
+
+ /* attempt secondary category chain
+ */
+ match = NULL;
+
+ if ( ( t = d -> alt_category ) ) {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( ( ! match ) &&
+ ( t = d -> alt_category1 ) )
+ {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( ( ! match ) &&
+ ( t = d -> alt_category2 ) )
+ {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( match ) {
+ if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
+ strcat ( target_buffer, ";" );
+ len -= 1;
+ }
+ strncat ( target_buffer, match, len );
+ len -= strlen ( target_buffer );
+ n += 1;
+ }
+
+#if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
+ match = NULL;
+
+ if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( ( ! match ) &&
+ ( t = pnd_pxml_get_subcategory1 ( h ) ) )
+ {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( ( ! match ) &&
+ ( t = pnd_pxml_get_subcategory2 ( h ) ) )
+ {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( match ) {
+ strncat ( target_buffer, match, len );
+ len -= strlen ( target_buffer );
+ n += 1;
+ }
+
+ /* attempt secondary category chain
+ */
+ match = NULL;
+
+ if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( ( ! match ) &&
+ ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
+ {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( ( ! match ) &&
+ ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
+ {
+ match = pnd_map_dotdesktop_category ( c, t );
+ }
+
+ if ( match ) {
+ if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
+ strcat ( target_buffer, ";" );
+ len -= 1;
+ }
+ strncat ( target_buffer, match, len );
+ len -= strlen ( target_buffer );
+ n += 1;
+ }
+#endif
+
+ if ( n && len ) {
+ strcat ( target_buffer, ";" );
+ }
+
+ 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 ) {
+ char *key;
+ char *ret;
+
+ key = malloc ( strlen ( single_category ) + 4 + 1 );
+
+ sprintf ( key, "map.%s", single_category );
+
+ ret = pnd_conf_get_as_char ( c, key );
+
+ free ( key );
+
+ 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 );
+}