2 #include <stdio.h> /* for FILE etc */
4 #include <unistd.h> /* for unlink */
5 #include <stdlib.h> /* for free */
6 #include <sys/stat.h> /* for stat */
9 #include "pnd_container.h"
11 #include "pnd_discovery.h"
12 #include "pnd_pndfiles.h"
14 #include "pnd_desktop.h"
15 #include "pnd_logger.h"
17 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
18 char filename [ FILENAME_MAX ];
23 // http://standards.freedesktop.org/desktop-entry-spec
27 if ( ! p -> unique_id ) {
28 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
33 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing exec\n", targetpath );
38 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
43 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
49 sprintf ( filename, "%s/%s#%u.desktop", targetpath, p -> unique_id, p -> subapp_number );
53 //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
55 f = fopen ( filename, "w" );
61 fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
63 if ( p -> title_en ) {
64 snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
65 fprintf ( f, "%s", buffer );
68 fprintf ( f, "Type=Application\n" );
69 fprintf ( f, "Version=1.0\n" );
72 snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
73 fprintf ( f, "%s", buffer );
76 if ( p -> unique_id ) {
77 fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
81 if ( p -> desc_en && p -> desc_en [ 0 ] ) {
82 snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
83 fprintf ( f, "%s", buffer );
87 if ( p -> preview_pic1 ) {
88 fprintf ( f, "X-Pandora-Preview-Pic-1=%s\n", p -> preview_pic1 );
91 if ( p -> clockspeed ) {
92 fprintf ( f, "X-Pandora-Clockspeed=%s\n", p -> clockspeed );
95 if ( p -> startdir ) {
96 fprintf ( f, "X-Pandora-Startdir=%s\n", p -> startdir );
99 if ( p -> main_category ) {
100 fprintf ( f, "X-Pandora-MainCategory=%s\n", p -> main_category );
102 if ( p -> main_category1 ) {
103 fprintf ( f, "X-Pandora-MainCategory1=%s\n", p -> main_category1 );
105 if ( p -> main_category2 ) {
106 fprintf ( f, "X-Pandora-MainCategory2=%s\n", p -> main_category2 );
109 if ( p -> alt_category ) {
110 fprintf ( f, "X-Pandora-AltCategory=%s\n", p -> alt_category );
112 if ( p -> alt_category1 ) {
113 fprintf ( f, "X-Pandora-AltCategory1=%s\n", p -> alt_category1 );
115 if ( p -> alt_category2 ) {
116 fprintf ( f, "X-Pandora-AltCategory2=%s\n", p -> alt_category2 );
118 if ( p -> info_filename ) {
119 fprintf ( f, "X-Pandora-Info-Filename=%s\n", p -> info_filename );
121 if ( p -> info_name ) {
122 fprintf ( f, "X-Pandora-Info-Name=%s\n", p -> info_name );
125 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
126 if ( p -> startdir ) {
127 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
128 fprintf ( f, "%s", buffer );
130 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
137 if ( p -> option_no_x11 ) {
138 nohup = "/usr/bin/nohup ";
144 if ( p -> object_type == pnd_object_type_directory ) {
145 snprintf ( buffer, 1020, "Exec=%s%s -p \"%s\" -e \"%s\" -b \"%s\"",
146 nohup, pndrun, p -> object_path, p -> exec,
147 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
148 } else if ( p -> object_type == pnd_object_type_pnd ) {
149 snprintf ( buffer, 1020, "Exec=%s%s -p \"%s/%s\" -e \"%s\" -b \"%s\"",
150 nohup, pndrun, p -> object_path, p -> object_filename, p -> exec,
151 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
155 if ( p -> startdir ) {
156 strncat ( buffer, " -s ", 1020 );
157 strncat ( buffer, p -> startdir, 1020 );
161 if ( p -> execargs ) {
162 char argbuf [ 1024 ];
163 snprintf ( argbuf, 1000, " -a \"%s\"", p -> execargs );
164 strncat ( buffer, argbuf, 1020 );
168 if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
169 strncat ( buffer, " -c ", 1020 );
170 strncat ( buffer, p -> clockspeed, 1020 );
174 if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
175 strncat ( buffer, " -x ", 1020 );
179 strncat ( buffer, "\n", 1020 );
182 fprintf ( f, "%s", buffer );
184 // and lets copy in some stuff in case it makes .desktop consumers life easier
185 if ( p -> exec ) { fprintf ( f, "X-Pandora-Exec=%s\n", p -> exec ); }
186 if ( p -> appdata_dirname ) { fprintf ( f, "X-Pandora-Appdata-Dirname=%s\n", p -> appdata_dirname ); }
187 if ( p -> execargs ) { fprintf ( f, "X-Pandora-ExecArgs=%s\n", p -> execargs ); }
188 if ( p -> object_flags & PND_DISCO_FLAG_OVR ) { fprintf ( f, "X-Pandora-Object-Flag-OVR=%s\n", "Yes" ); }
189 if ( p -> object_type == pnd_object_type_pnd ) {
190 fprintf ( f, "X-Pandora-Object-Path=%s\n", p -> object_path );
191 fprintf ( f, "X-Pandora-Object-Filename=%s\n", p -> object_filename );
198 char cats [ 512 ] = "";
204 // "Application" used to be in the standard and is commonly supported still
205 // Utility and Network should ensure the app is visible 'somewhere' :/
206 char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
208 // determine searchpath (for conf, not for apps)
209 confpath = pnd_conf_query_searchpath();
211 // inhale the conf file
212 c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
214 // if we can find a default category set, pull it in; otherwise assume
216 if ( pnd_conf_get_as_char ( c, "default" ) ) {
217 defaults = pnd_conf_get_as_char ( c, "default" );
220 // ditch the confpath
226 n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
229 fprintf ( f, "Categories=%s\n", cats );
231 fprintf ( f, "Categories=%s\n", defaults );
235 fprintf ( f, "Categories=%s\n", defaults );
240 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
247 unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
248 char filename [ FILENAME_MAX ];
249 char buffer [ 1024 ];
251 char *viewer, *searchpath;
252 pnd_conf_handle desktoph;
255 // http://standards.freedesktop.org/desktop-entry-spec
261 searchpath = pnd_conf_query_searchpath();
263 desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
269 viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
272 return ( 0 ); // no way to view the file
276 if ( ! p -> unique_id ) {
277 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
281 if ( ! p -> info_filename ) {
282 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_filename\n", targetpath );
286 if ( ! p -> info_name ) {
287 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_name\n", targetpath );
291 if ( ! targetpath ) {
292 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
297 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
303 sprintf ( filename, "%s/%s#%uinfo.desktop", targetpath, p -> unique_id, p -> subapp_number );
307 f = fopen ( filename, "w" );
313 fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
315 if ( p -> info_name ) {
316 snprintf ( buffer, 1020, "Name=%s\n", p -> info_name );
317 fprintf ( f, "%s", buffer );
320 fprintf ( f, "Type=Application\n" );
321 fprintf ( f, "Version=1.0\n" );
325 snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
326 fprintf ( f, "%s", buffer );
330 if ( p -> unique_id ) {
331 fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
334 if ( p -> title_en && p -> title_en [ 0 ] ) {
335 snprintf ( buffer, 1020, "Comment=Automatic menu info entry for %s\n", p -> title_en );
336 fprintf ( f, "%s", buffer );
339 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
340 if ( p -> startdir ) {
341 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
342 fprintf ( f, "%s", buffer );
344 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
351 char *viewerargs = pnd_conf_get_as_char ( desktoph, "info.viewer_args" );
352 if ( viewerargs && viewerargs [ 0 ] ) {
353 snprintf ( pargs, 1001, "%s %s",
354 pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
357 // WARNING: This might not be quite right; if no viewer-args, shouldn't we still append the info-filename? likewise,
358 // even if we do have view-args, shouldn't we check if filename is present?
361 char pndfile [ 1024 ];
362 if ( p -> object_type == pnd_object_type_directory ) {
363 // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
364 strncpy ( pndfile, p -> object_path, 1000 );
365 } else if ( p -> object_type == pnd_object_type_pnd ) {
366 // pnd_run.sh wants the full path and filename for the .pnd file
367 snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
370 pnd_apps_exec_info_t info;
371 info.viewer = viewer;
374 if ( ! pnd_apps_exec_disco ( pndrun, p, PND_EXEC_OPTION_NORUN | PND_EXEC_OPTION_INFO, &info ) ) {
378 fprintf ( f, "Exec=%s\n", pnd_apps_exec_runline() );
380 if ( pnd_conf_get_as_char ( desktoph, "info.category" ) ) {
381 fprintf ( f, "Categories=%s\n", pnd_conf_get_as_char ( desktoph, "info.category" ) );
383 fprintf ( f, "Categories=Documentation\n" );
386 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
393 unsigned char pnd_emit_dotassoc ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
395 if ( p -> associationitem1_command ) {
396 if ( ! _pnd_emit_dotassoc ( targetpath, pndrun, p, 1 ) ) {
401 if ( p -> associationitem2_command ) {
402 if ( ! _pnd_emit_dotassoc ( targetpath, pndrun, p, 2 ) ) {
407 if ( p -> associationitem3_command ) {
408 if ( ! _pnd_emit_dotassoc ( targetpath, pndrun, p, 3 ) ) {
416 unsigned char _pnd_emit_dotassoc ( char *targetpath, char *pndrun, pnd_disco_t *p, unsigned char i ) {
417 char filename [ FILENAME_MAX ];
418 char buffer [ 1024 ];
427 command = p -> associationitem1_command;
428 args = p -> associationitem1_args;
429 filetype = p -> associationitem1_filetype;
430 mimename = p -> associationitem1_name;
431 } else if ( i == 2 ) {
432 command = p -> associationitem2_command;
433 args = p -> associationitem2_args;
434 filetype = p -> associationitem2_filetype;
435 mimename = p -> associationitem2_name;
436 } else if ( i == 3 ) {
437 command = p -> associationitem3_command;
438 args = p -> associationitem3_args;
439 filetype = p -> associationitem3_filetype;
440 mimename = p -> associationitem3_name;
442 return ( 0 ); // w-t-f
446 // http://standards.freedesktop.org/desktop-entry-spec
450 if ( ! p -> unique_id ) {
451 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
456 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing exec\n", targetpath );
461 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing MIME-type\n", targetpath );
465 if ( ! targetpath ) {
466 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
471 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
477 sprintf ( filename, "%s/%s#%u#%u.desktop", targetpath, p -> unique_id, p -> subapp_number, i );
481 //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
483 f = fopen ( filename, "w" );
489 fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
491 fprintf ( f, "NoDisplay=false\n" ); // file association entries shouldn't live in the menus; but if 'true', xfce won't show this association in the dialog.. stupid!
493 if ( p -> title_en ) {
494 snprintf ( buffer, 1020, "Name=%s\n", mimename );
495 fprintf ( f, "%s", buffer );
498 fprintf ( f, "Type=Application\n" );
499 fprintf ( f, "Version=1.0\n" );
502 snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
503 fprintf ( f, "%s", buffer );
506 if ( p -> unique_id ) {
507 fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
510 if ( p -> clockspeed ) {
511 fprintf ( f, "X-Pandora-Clockspeed=%s\n", p -> clockspeed );
514 if ( p -> startdir ) {
515 fprintf ( f, "X-Pandora-Startdir=%s\n", p -> startdir );
519 pnd_log ( PND_LOG_DEFAULT, " EmitDesktopAssoc: Found file association request in PXML (%s) for mimetype (%s)\n", p -> title_en, filetype );
520 fprintf ( f, "MimeType=%s\n", filetype );
526 if ( p -> option_no_x11 ) {
527 nohup = "/usr/bin/nohup ";
533 if ( p -> object_type == pnd_object_type_directory ) {
534 snprintf ( buffer, 1020, "Exec=%s%s -p \"%s\" -e \"%s\" -b \"%s\"",
535 nohup, pndrun, p -> object_path, command,
536 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
537 } else if ( p -> object_type == pnd_object_type_pnd ) {
538 snprintf ( buffer, 1020, "Exec=%s%s -p \"%s/%s\" -e \"%s\" -b \"%s\"",
539 nohup, pndrun, p -> object_path, p -> object_filename, command,
540 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
544 if ( p -> startdir ) {
545 strncat ( buffer, " -s ", 1020 );
546 strncat ( buffer, p -> startdir, 1020 );
550 if ( p -> execargs ) {
551 char argbuf [ 1024 ];
552 snprintf ( argbuf, 1000, " -a \"%s\"", p -> execargs );
553 strncat ( buffer, argbuf, 1020 );
557 if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
558 strncat ( buffer, " -c ", 1020 );
559 strncat ( buffer, p -> clockspeed, 1020 );
563 if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
564 strncat ( buffer, " -x ", 1020 );
567 // args (mimetype args)
569 char argbuf [ 1024 ];
570 snprintf ( argbuf, 1000, " -- %s", args );
571 strncat ( buffer, argbuf, 1020 );
575 strncat ( buffer, "\n", 1020 );
578 fprintf ( f, "%s", buffer );
580 // and lets copy in some stuff in case it makes .desktop consumers life easier
581 if ( command ) { fprintf ( f, "X-Pandora-Exec=%s\n", command ); }
582 if ( args ) { fprintf ( f, "X-Pandora-ExecArgs=%s\n", args ); }
583 if ( p -> appdata_dirname ) { fprintf ( f, "X-Pandora-Appdata-Dirname=%s\n", p -> appdata_dirname ); }
584 if ( p -> object_flags & PND_DISCO_FLAG_OVR ) { fprintf ( f, "X-Pandora-Object-Flag-OVR=%s\n", "Yes" ); }
585 if ( p -> object_type == pnd_object_type_pnd ) {
586 fprintf ( f, "X-Pandora-Object-Path=%s\n", p -> object_path );
587 fprintf ( f, "X-Pandora-Object-Filename=%s\n", p -> object_filename );
592 fprintf ( f, "Categories=Other\n" ); // NoDisplay means can't confirm choice in xfce, so how do we hide this out of the way?
594 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
601 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
602 //#define BITLEN (8*1024)
603 #define BITLEN (64*1024)
604 char buffer [ FILENAME_MAX ]; // target filename
605 char from [ FILENAME_MAX ]; // source filename
606 unsigned char bits [ BITLEN ];
610 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
611 if ( ( p -> object_type == pnd_object_type_pnd ) &&
612 ( ! p -> pnd_icon_pos ) )
614 return ( 0 ); // discover code didn't find it, so FAIL
617 // determine filename for target
618 sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target
620 /* first.. open the source file, by type of application:
621 * are we looking through a pnd file or a dir?
623 if ( p -> object_type == pnd_object_type_directory ) {
624 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
625 } else if ( p -> object_type == pnd_object_type_pnd ) {
626 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
629 pnd = fopen ( from, "rb" );
632 pnd_log ( PND_LOG_DEFAULT, " Emit icon, couldn't open source\n" );
638 target = fopen ( buffer, "wb" );
642 pnd_log ( PND_LOG_DEFAULT, " Emit icon, couldn't open target\n" );
646 fseek ( pnd, 0, SEEK_END );
648 //fseek ( pnd, 0, SEEK_SET );
650 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
652 len -= p -> pnd_icon_pos;
654 pnd_log ( PND_LOG_DEFAULT, " Emit icon, length: %u\n", len );
658 if ( len > (BITLEN) ) {
664 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
668 pnd_log ( PND_LOG_DEFAULT, " Emit icon, bad read\n" );
675 char bigbuffer [ 200 * 1024 ] = "\0";
677 pnd_log ( PND_LOG_DEFAULT, " Read hexdump\n" );
678 while ( i < bitlen ) {
679 sprintf ( b, "%x,", bits [ i ] );
680 strcat ( bigbuffer, b );
683 pnd_log ( PND_LOG_DEFAULT, bigbuffer );
687 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
691 pnd_log ( PND_LOG_DEFAULT, " Emit icon, bad write\n" );
696 //pnd_log ( PND_LOG_DEFAULT, " Emit icon, next block, length: %u\n", len );
702 //pnd_log ( PND_LOG_DEFAULT, " Emit icon, done.\n" );
707 #if 1 // we switched direction to freedesktop standard categories
708 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
709 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
713 // clear target so we can easily append
714 memset ( target_buffer, '\0', len );
716 // for each main-cat and sub-cat, including alternates, just append them all together
717 // we'll try mapping them, since the categories file is there, but we'll default to
718 // copying over; this lets the conf file do merging or renaming of cagtegories, which
719 // could still be useful, but we can leave the conf file empty to effect a pure
720 // trusted-PXML-copying
722 // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
723 // there are hundreds), and would also mean new ones and peoples custom ones would
726 /* attempt primary category chain
728 #define MAPCAT(field) \
729 if ( ( t = d -> field ) ) { \
730 match = pnd_map_dotdesktop_category ( c, t ); \
731 strncat ( target_buffer, match ? match : t, len ); \
732 strncat ( target_buffer, ";", len ); \
735 MAPCAT(main_category);
736 MAPCAT(main_category1);
737 MAPCAT(main_category2);
738 MAPCAT(alt_category);
739 MAPCAT(alt_category1);
740 MAPCAT(alt_category2);
742 if ( target_buffer [ 0 ] ) {
743 return ( 1 ); // I guess its 'good'?
747 if ( ( t = d -> main_category ) ) {
748 match = pnd_map_dotdesktop_category ( c, t );
749 strncat ( target_buffer, match ? match : t, len );
750 strncat ( target_buffer, ";", len );
758 #if 0 // we switched direction
759 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
760 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
761 unsigned short int n = 0; // no. matches
765 // clear target so we can easily append
766 memset ( target_buffer, '\0', len );
768 /* attempt primary category chain
772 if ( ( t = d -> main_category ) ) {
773 match = pnd_map_dotdesktop_category ( c, t );
777 ( t = d -> main_category1 ) )
779 match = pnd_map_dotdesktop_category ( c, t );
783 ( t = d -> main_category2 ) )
785 match = pnd_map_dotdesktop_category ( c, t );
789 strncat ( target_buffer, match, len );
790 len -= strlen ( target_buffer );
794 /* attempt secondary category chain
798 if ( ( t = d -> alt_category ) ) {
799 match = pnd_map_dotdesktop_category ( c, t );
803 ( t = d -> alt_category1 ) )
805 match = pnd_map_dotdesktop_category ( c, t );
809 ( t = d -> alt_category2 ) )
811 match = pnd_map_dotdesktop_category ( c, t );
815 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
816 strcat ( target_buffer, ";" );
819 strncat ( target_buffer, match, len );
820 len -= strlen ( target_buffer );
824 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
827 if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
828 match = pnd_map_dotdesktop_category ( c, t );
832 ( t = pnd_pxml_get_subcategory1 ( h ) ) )
834 match = pnd_map_dotdesktop_category ( c, t );
838 ( t = pnd_pxml_get_subcategory2 ( h ) ) )
840 match = pnd_map_dotdesktop_category ( c, t );
844 strncat ( target_buffer, match, len );
845 len -= strlen ( target_buffer );
849 /* attempt secondary category chain
853 if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
854 match = pnd_map_dotdesktop_category ( c, t );
858 ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
860 match = pnd_map_dotdesktop_category ( c, t );
864 ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
866 match = pnd_map_dotdesktop_category ( c, t );
870 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
871 strcat ( target_buffer, ";" );
874 strncat ( target_buffer, match, len );
875 len -= strlen ( target_buffer );
881 strcat ( target_buffer, ";" );
888 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
889 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
893 key = malloc ( strlen ( single_category ) + 4 + 1 );
895 sprintf ( key, "map.%s", single_category );
897 ret = pnd_conf_get_as_char ( c, key );
904 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
905 // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
906 // with a fwrite at the end...
907 char from [ FILENAME_MAX ]; // source filename
908 char bits [ 8 * 1024 ];
911 unsigned char *target = NULL, *targiter = NULL;
913 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
914 if ( ( p -> object_type == pnd_object_type_pnd ) &&
915 ( ! p -> pnd_icon_pos ) )
917 return ( NULL ); // discover code didn't find it, so FAIL
920 /* first.. open the source file, by type of application:
921 * are we looking through a pnd file or a dir?
923 if ( p -> object_type == pnd_object_type_directory ) {
924 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
925 } else if ( p -> object_type == pnd_object_type_pnd ) {
926 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
929 pnd = fopen ( from, "r" );
935 // determine length of file, then adjust by icon position to find begin of icon
938 fseek ( pnd, 0, SEEK_END );
940 //fseek ( pnd, 0, SEEK_SET );
942 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
944 len -= p -> pnd_icon_pos;
946 // create target buffer
947 target = malloc ( len );
960 // copy over icon to target
963 if ( len > (8*1024) ) {
969 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
975 memmove ( targiter, bits, bitlen );
986 // parse_dotdesktop() can be used to read a libpnd generated .desktop and return a limited
987 // but useful disco-t structure back; possibly useful for scanning .desktops rather than
988 // scanning pnd-files?
989 pnd_disco_t *pnd_parse_dotdesktop ( char *ddpath, unsigned int flags ) {
991 // will verify the .desktop has the libpnd-marking on it (X-Pandora-Source): PND_DOTDESKTOP_SOURCE
993 // attempt to extract..
994 // - unique-id (from filename or field)
995 // - subapp number (from filename)
996 // - exec required info
999 // - comment (desc-en)
1002 // - appdata name (or unique-id if not present)
1008 char pndpath [ 1024 ];
1009 bzero ( pndpath, 1024 );
1011 // filter on filename?
1012 if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
1013 // too bad we didn't put some libpnd token at the front of the filename or something
1014 // hell, we should cleanse unique-id to ensure its not full of special chars like '*' and '..'.. eep!
1015 if ( strrchr ( ddpath, '#' ) == NULL ) { // but if requiring libpnd, we can at least check for #subapp-number
1020 if ( strstr ( ddpath, ".desktop" ) == NULL ) {
1021 return ( NULL ); // no .desktop in filename, must be something else... skip!
1024 if ( strstr ( ddpath, "info.desktop" ) != NULL ) {
1025 // ".....info.desktop" is the 'document help' (README) emitted from a pnd, not an actual app; minimenu rather
1026 // expects the doc-info as part of the main app, not a separate app.. so lets drop it here, to avoid doubling up
1027 // the number of applications, needlessly..
1031 // determine file length
1032 struct stat statbuf;
1034 if ( stat ( ddpath, &statbuf) < 0 ) {
1035 return ( NULL ); // couldn't open
1040 unsigned char libpnd_origin = 0;
1043 pnd_disco_t *p = malloc ( sizeof(pnd_disco_t) );
1047 bzero ( p, sizeof(pnd_disco_t) );
1050 FILE *f = fopen ( ddpath, "r" );
1053 return ( NULL ); // not up or shut up!
1056 while ( fgets ( dd, 1024, f ) ) {
1057 char *nl = strchr ( dd, '\n' );
1065 if ( strncmp ( dd, "Name=", 5 ) == 0 ) {
1066 p -> title_en = strdup ( dd + 5 );
1067 } else if ( strncmp ( dd, "Name[en]=", 9 ) == 0 ) {
1068 p -> title_en = strdup ( dd + 9 );
1069 } else if ( strncmp ( dd, "Icon=", 5 ) == 0 ) {
1070 p -> icon = strdup ( dd + 5 );
1071 } else if ( strcmp ( dd, PND_DOTDESKTOP_SOURCE ) == 0 ) {
1073 } else if ( strncmp ( dd, "X-Pandora-UID=", 14 ) == 0 ) {
1074 p -> unique_id = strdup ( dd + 14 );
1075 } else if ( strncmp ( dd, "X-Pandora-Preview-Pic-1=", 24 ) == 0 ) {
1076 p -> preview_pic1 = strdup ( dd + 24 );
1077 } else if ( strncmp ( dd, "X-Pandora-Clockspeed=", 21 ) == 0 ) {
1078 p -> clockspeed = strdup ( dd + 21 );
1079 } else if ( strncmp ( dd, "X-Pandora-Startdir=", 19 ) == 0 ) {
1080 p -> startdir = strdup ( dd + 19 );
1081 } else if ( strncmp ( dd, "X-Pandora-Appdata-Dirname=", 26 ) == 0 ) {
1082 p -> appdata_dirname = strdup ( dd + 26 );
1083 } else if ( strncmp ( dd, "X-Pandora-ExecArgs=", 19 ) == 0 ) {
1084 p -> execargs = strdup ( dd + 19 );
1085 } else if ( strncmp ( dd, "X-Pandora-Exec=", 15 ) == 0 ) {
1086 p -> exec = strdup ( dd + 15 );
1087 } else if ( strncmp ( dd, "X-Pandora-Object-Path=", 22 ) == 0 ) {
1088 p -> object_path = strdup ( dd + 22 );
1089 } else if ( strncmp ( dd, "X-Pandora-Object-Filename=", 26 ) == 0 ) {
1090 p -> object_filename = strdup ( dd + 26 );
1091 } else if ( strncmp ( dd, "X-Pandora-Object-Flag-OVR=", 26 ) == 0 ) {
1092 p -> object_flags |= PND_DISCO_FLAG_OVR;
1094 } else if ( strncmp ( dd, "X-Pandora-MainCategory=", 23 ) == 0 ) {
1095 p -> main_category = strdup ( dd + 23 );
1096 } else if ( strncmp ( dd, "X-Pandora-MainCategory1=", 24 ) == 0 ) {
1097 p -> main_category1 = strdup ( dd + 24 );
1098 } else if ( strncmp ( dd, "X-Pandora-MainCategory2=", 24 ) == 0 ) {
1099 p -> main_category2 = strdup ( dd + 24 );
1101 } else if ( strncmp ( dd, "X-Pandora-AltCategory=", 22 ) == 0 ) {
1102 p -> alt_category = strdup ( dd + 22 );
1103 } else if ( strncmp ( dd, "X-Pandora-AltCategory1=", 23 ) == 0 ) {
1104 p -> alt_category1 = strdup ( dd + 23 );
1105 } else if ( strncmp ( dd, "X-Pandora-AltCategory2=", 23 ) == 0 ) {
1106 p -> alt_category2 = strdup ( dd + 23 );
1108 } else if ( strncmp ( dd, "X-Pandora-Info-Filename=", 24 ) == 0 ) {
1109 p -> info_filename = strdup ( dd + 24 );
1110 } else if ( strncmp ( dd, "X-Pandora-Info-Name=", 20 ) == 0 ) {
1111 p -> info_name = strdup ( dd + 20 );
1113 } else if ( strncmp ( dd, "Comment=", 8 ) == 0 ) {
1114 p -> desc_en = strdup ( dd + 8 );
1115 } else if ( strncmp ( dd, "Comment[en]=", 12 ) == 0 ) {
1116 p -> desc_en = strdup ( dd + 12 );
1117 } else if ( strncmp ( dd, "Exec=", 5 ) == 0 ) {
1119 char *e = strstr ( dd, " -e " );
1122 // probably libpnd app
1123 #if 0 // no needed due to above X-Pandora attributes
1128 char *space = strchr ( e, ' ' );
1129 p -> exec = strndup ( e, space - e - 1 );
1132 char *b = strstr ( dd, " -b " );
1135 char *space = strchr ( b, '\0' );
1136 p -> appdata_dirname = strndup ( b, space - b - 1 );
1139 char *p = strstr ( dd, " -p " );
1142 char *space = strchr ( p, ' ' );
1143 strncpy ( pndpath, p, space - p - 1 );
1148 // probably not libpnd app
1149 p -> exec = strdup ( dd + 5 );
1152 #if 0 // ignore; using X- categories now
1153 } else if ( strncmp ( dd, "Categories=", 11 ) == 0 ) {
1154 // HACK; only honours first category
1155 char *semi = strchr ( dd, ';' );
1157 p -> main_category = strndup ( dd + 11, semi - dd + 11 );
1159 p -> main_category = strdup ( dd + 11 );
1161 semi = strchr ( p -> main_category, ';' );
1177 if ( ! libpnd_origin ) {
1180 if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
1181 pnd_disco_destroy ( p );
1187 p -> object_flags |= PND_DISCO_LIBPND_DD; // so caller can do something if it wishes
1190 // filter on content
1191 if ( ( ! p -> title_en ) ||
1195 pnd_disco_destroy ( p );
1200 if ( ! p -> unique_id ) {
1201 if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
1202 pnd_disco_destroy ( p );
1207 snprintf ( hack, 100, "inode-%lu", statbuf.st_ino );
1208 p -> unique_id = strdup ( hack );
1213 p -> object_type = pnd_object_type_pnd;
1215 #if 0 // nolonger needed due to above X-Pandora attributes
1217 if ( pndpath [ 0 ] ) {
1223 char *slash = strrchr ( source, '/' );
1225 p -> object_path = strndup ( source, slash - source );
1226 p -> object_filename = strdup ( slash + 1 );
1228 p -> object_path = "./";
1229 p -> object_filename = strdup ( source );
1233 // lame guards, in case of lazy consumers and broken .desktop files
1234 if ( p -> object_path == NULL ) {
1235 p -> object_path = strdup ( "/tmp" );
1237 if ( p -> object_filename == NULL ) {
1238 p -> object_filename = strdup ( "" ); // force bad filename