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 0 // we let pnd_run.sh command line handle this instead of in .desktop
88 if ( p -> startdir ) {
89 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
90 fprintf ( f, "%s", buffer );
92 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
99 if ( p -> option_no_x11 ) {
100 nohup = "/usr/bin/nohup ";
106 if ( p -> object_type == pnd_object_type_directory ) {
107 snprintf ( buffer, 1020, "Exec=%s%s -p \"%s\" -e \"%s\" -b \"%s\"",
108 nohup, pndrun, p -> object_path, p -> exec,
109 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
110 } else if ( p -> object_type == pnd_object_type_pnd ) {
111 snprintf ( buffer, 1020, "Exec=%s%s -p \"%s/%s\" -e \"%s\" -b \"%s\"",
112 nohup, pndrun, p -> object_path, p -> object_filename, p -> exec,
113 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
117 if ( p -> startdir ) {
118 strncat ( buffer, " -s ", 1020 );
119 strncat ( buffer, p -> startdir, 1020 );
123 if ( p -> execargs ) {
124 char argbuf [ 1024 ];
125 snprintf ( argbuf, 1000, " -a \"%s\"", p -> execargs );
126 strncat ( buffer, argbuf, 1020 );
130 if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
131 strncat ( buffer, " -c ", 1020 );
132 strncat ( buffer, p -> clockspeed, 1020 );
136 if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
137 strncat ( buffer, " -x ", 1020 );
141 strncat ( buffer, "\n", 1020 );
144 fprintf ( f, "%s", buffer );
149 char cats [ 512 ] = "";
155 // "Application" used to be in the standard and is commonly supported still
156 // Utility and Network should ensure the app is visible 'somewhere' :/
157 char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
159 // determine searchpath (for conf, not for apps)
160 confpath = pnd_conf_query_searchpath();
162 // inhale the conf file
163 c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
165 // if we can find a default category set, pull it in; otherwise assume
167 if ( pnd_conf_get_as_char ( c, "default" ) ) {
168 defaults = pnd_conf_get_as_char ( c, "default" );
171 // ditch the confpath
177 n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
180 fprintf ( f, "Categories=%s\n", cats );
182 fprintf ( f, "Categories=%s\n", defaults );
186 fprintf ( f, "Categories=%s\n", defaults );
191 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
198 unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
199 char filename [ FILENAME_MAX ];
200 char buffer [ 1024 ];
202 char *viewer, *searchpath;
203 pnd_conf_handle desktoph;
206 // http://standards.freedesktop.org/desktop-entry-spec
212 searchpath = pnd_conf_query_searchpath();
214 desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
220 viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
223 return ( 0 ); // no way to view the file
227 if ( ! p -> unique_id ) {
228 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
232 if ( ! p -> info_filename ) {
233 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_filename\n", targetpath );
237 if ( ! p -> info_name ) {
238 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_name\n", targetpath );
242 if ( ! targetpath ) {
243 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
248 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
254 sprintf ( filename, "%s/%s#%uinfo.desktop", targetpath, p -> unique_id, p -> subapp_number );
258 f = fopen ( filename, "w" );
264 fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
266 if ( p -> info_name ) {
267 snprintf ( buffer, 1020, "Name=%s\n", p -> info_name );
268 fprintf ( f, "%s", buffer );
271 fprintf ( f, "Type=Application\n" );
272 fprintf ( f, "Version=1.0\n" );
276 snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
277 fprintf ( f, "%s", buffer );
281 if ( p -> unique_id ) {
282 fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
285 if ( p -> title_en && p -> title_en [ 0 ] ) {
286 snprintf ( buffer, 1020, "Comment=Automatic menu info entry for %s\n", p -> title_en );
287 fprintf ( f, "%s", buffer );
290 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
291 if ( p -> startdir ) {
292 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
293 fprintf ( f, "%s", buffer );
295 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
302 char *viewerargs = pnd_conf_get_as_char ( desktoph, "info.viewer_args" );
303 if ( viewerargs && viewerargs [ 0 ] ) {
304 snprintf ( pargs, 1001, "%s %s",
305 pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
308 // WARNING: This might not be quite right; if no viewer-args, shouldn't we still append the info-filename? likewise,
309 // even if we do have view-args, shouldn't we check if filename is present?
312 char pndfile [ 1024 ];
313 if ( p -> object_type == pnd_object_type_directory ) {
314 // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
315 strncpy ( pndfile, p -> object_path, 1000 );
316 } else if ( p -> object_type == pnd_object_type_pnd ) {
317 // pnd_run.sh wants the full path and filename for the .pnd file
318 snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
321 pnd_apps_exec_info_t info;
322 info.viewer = viewer;
325 if ( ! pnd_apps_exec_disco ( pndrun, p, PND_EXEC_OPTION_NORUN | PND_EXEC_OPTION_INFO, &info ) ) {
329 fprintf ( f, "Exec=%s\n", pnd_apps_exec_runline() );
331 if ( pnd_conf_get_as_char ( desktoph, "info.category" ) ) {
332 fprintf ( f, "Categories=%s\n", pnd_conf_get_as_char ( desktoph, "info.category" ) );
334 fprintf ( f, "Categories=Documentation\n" );
337 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
344 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
345 //#define BITLEN (8*1024)
346 #define BITLEN (64*1024)
347 char buffer [ FILENAME_MAX ]; // target filename
348 char from [ FILENAME_MAX ]; // source filename
349 unsigned char bits [ BITLEN ];
353 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
354 if ( ( p -> object_type == pnd_object_type_pnd ) &&
355 ( ! p -> pnd_icon_pos ) )
357 return ( 0 ); // discover code didn't find it, so FAIL
360 // determine filename for target
361 sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target
363 /* first.. open the source file, by type of application:
364 * are we looking through a pnd file or a dir?
366 if ( p -> object_type == pnd_object_type_directory ) {
367 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
368 } else if ( p -> object_type == pnd_object_type_pnd ) {
369 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
372 pnd = fopen ( from, "rb" );
375 pnd_log ( PND_LOG_DEFAULT, " Emit icon, couldn't open source\n" );
381 target = fopen ( buffer, "wb" );
385 pnd_log ( PND_LOG_DEFAULT, " Emit icon, couldn't open target\n" );
389 fseek ( pnd, 0, SEEK_END );
391 //fseek ( pnd, 0, SEEK_SET );
393 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
395 len -= p -> pnd_icon_pos;
397 pnd_log ( PND_LOG_DEFAULT, " Emit icon, length: %u\n", len );
401 if ( len > (BITLEN) ) {
407 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
411 pnd_log ( PND_LOG_DEFAULT, " Emit icon, bad read\n" );
418 char bigbuffer [ 200 * 1024 ] = "\0";
420 pnd_log ( PND_LOG_DEFAULT, " Read hexdump\n" );
421 while ( i < bitlen ) {
422 sprintf ( b, "%x,", bits [ i ] );
423 strcat ( bigbuffer, b );
426 pnd_log ( PND_LOG_DEFAULT, bigbuffer );
430 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
434 pnd_log ( PND_LOG_DEFAULT, " Emit icon, bad write\n" );
439 //pnd_log ( PND_LOG_DEFAULT, " Emit icon, next block, length: %u\n", len );
445 //pnd_log ( PND_LOG_DEFAULT, " Emit icon, done.\n" );
450 #if 1 // we switched direction to freedesktop standard categories
451 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
452 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
456 // clear target so we can easily append
457 memset ( target_buffer, '\0', len );
459 // for each main-cat and sub-cat, including alternates, just append them all together
460 // we'll try mapping them, since the categories file is there, but we'll default to
461 // copying over; this lets the conf file do merging or renaming of cagtegories, which
462 // could still be useful, but we can leave the conf file empty to effect a pure
463 // trusted-PXML-copying
465 // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
466 // there are hundreds), and would also mean new ones and peoples custom ones would
469 /* attempt primary category chain
471 #define MAPCAT(field) \
472 if ( ( t = d -> field ) ) { \
473 match = pnd_map_dotdesktop_category ( c, t ); \
474 strncat ( target_buffer, match ? match : t, len ); \
475 strncat ( target_buffer, ";", len ); \
478 MAPCAT(main_category);
479 MAPCAT(main_category1);
480 MAPCAT(main_category2);
481 MAPCAT(alt_category);
482 MAPCAT(alt_category1);
483 MAPCAT(alt_category2);
485 if ( target_buffer [ 0 ] ) {
486 return ( 1 ); // I guess its 'good'?
490 if ( ( t = d -> main_category ) ) {
491 match = pnd_map_dotdesktop_category ( c, t );
492 strncat ( target_buffer, match ? match : t, len );
493 strncat ( target_buffer, ";", len );
501 #if 0 // we switched direction
502 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
503 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
504 unsigned short int n = 0; // no. matches
508 // clear target so we can easily append
509 memset ( target_buffer, '\0', len );
511 /* attempt primary category chain
515 if ( ( t = d -> main_category ) ) {
516 match = pnd_map_dotdesktop_category ( c, t );
520 ( t = d -> main_category1 ) )
522 match = pnd_map_dotdesktop_category ( c, t );
526 ( t = d -> main_category2 ) )
528 match = pnd_map_dotdesktop_category ( c, t );
532 strncat ( target_buffer, match, len );
533 len -= strlen ( target_buffer );
537 /* attempt secondary category chain
541 if ( ( t = d -> alt_category ) ) {
542 match = pnd_map_dotdesktop_category ( c, t );
546 ( t = d -> alt_category1 ) )
548 match = pnd_map_dotdesktop_category ( c, t );
552 ( t = d -> alt_category2 ) )
554 match = pnd_map_dotdesktop_category ( c, t );
558 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
559 strcat ( target_buffer, ";" );
562 strncat ( target_buffer, match, len );
563 len -= strlen ( target_buffer );
567 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
570 if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
571 match = pnd_map_dotdesktop_category ( c, t );
575 ( t = pnd_pxml_get_subcategory1 ( h ) ) )
577 match = pnd_map_dotdesktop_category ( c, t );
581 ( t = pnd_pxml_get_subcategory2 ( h ) ) )
583 match = pnd_map_dotdesktop_category ( c, t );
587 strncat ( target_buffer, match, len );
588 len -= strlen ( target_buffer );
592 /* attempt secondary category chain
596 if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
597 match = pnd_map_dotdesktop_category ( c, t );
601 ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
603 match = pnd_map_dotdesktop_category ( c, t );
607 ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
609 match = pnd_map_dotdesktop_category ( c, t );
613 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
614 strcat ( target_buffer, ";" );
617 strncat ( target_buffer, match, len );
618 len -= strlen ( target_buffer );
624 strcat ( target_buffer, ";" );
631 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
632 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
636 key = malloc ( strlen ( single_category ) + 4 + 1 );
638 sprintf ( key, "map.%s", single_category );
640 ret = pnd_conf_get_as_char ( c, key );
647 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
648 // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
649 // with a fwrite at the end...
650 char from [ FILENAME_MAX ]; // source filename
651 char bits [ 8 * 1024 ];
654 unsigned char *target = NULL, *targiter = NULL;
656 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
657 if ( ( p -> object_type == pnd_object_type_pnd ) &&
658 ( ! p -> pnd_icon_pos ) )
660 return ( NULL ); // discover code didn't find it, so FAIL
663 /* first.. open the source file, by type of application:
664 * are we looking through a pnd file or a dir?
666 if ( p -> object_type == pnd_object_type_directory ) {
667 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
668 } else if ( p -> object_type == pnd_object_type_pnd ) {
669 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
672 pnd = fopen ( from, "r" );
678 // determine length of file, then adjust by icon position to find begin of icon
681 fseek ( pnd, 0, SEEK_END );
683 //fseek ( pnd, 0, SEEK_SET );
685 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
687 len -= p -> pnd_icon_pos;
689 // create target buffer
690 target = malloc ( len );
703 // copy over icon to target
706 if ( len > (8*1024) ) {
712 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
718 memmove ( targiter, bits, bitlen );
729 // parse_dotdesktop() can be used to read a libpnd generated .desktop and return a limited
730 // but useful disco-t structure back; possibly useful for scanning .desktops rather than
731 // scanning pnd-files?
732 pnd_disco_t *pnd_parse_dotdesktop ( char *ddpath, unsigned int flags ) {
734 // will verify the .desktop has the libpnd-marking on it (X-Pandora-Source): PND_DOTDESKTOP_SOURCE
736 // attempt to extract..
737 // - unique-id (from filename or field)
738 // - subapp number (from filename)
739 // - exec required info
742 // - comment (desc-en)
745 // - appdata name (or unique-id if not present)
751 char pndpath [ 1024 ];
752 bzero ( pndpath, 1024 );
754 // filter on filename?
755 if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
756 // too bad we didn't put some libpnd token at the front of the filename or something
757 // hell, we should cleanse unique-id to ensure its not full of special chars like '*' and '..'.. eep!
758 if ( strrchr ( ddpath, '#' ) == NULL ) { // but if requiring libpnd, we can at least check for #subapp-number
763 if ( strstr ( ddpath, ".desktop" ) == NULL ) {
767 // determine file length
770 if ( stat ( ddpath, &statbuf) < 0 ) {
771 return ( NULL ); // couldn't open
776 unsigned char libpnd_origin = 0;
779 pnd_disco_t *p = malloc ( sizeof(pnd_disco_t) );
783 bzero ( p, sizeof(pnd_disco_t) );
786 FILE *f = fopen ( ddpath, "r" );
789 return ( NULL ); // not up or shut up!
792 while ( fgets ( dd, 1024, f ) ) {
793 char *nl = strchr ( dd, '\n' );
801 if ( strncmp ( dd, "Name=", 5 ) == 0 ) {
802 p -> title_en = strdup ( dd + 5 );
803 } else if ( strncmp ( dd, "Name[en]=", 9 ) == 0 ) {
804 p -> title_en = strdup ( dd + 9 );
805 } else if ( strncmp ( dd, "Icon=", 5 ) == 0 ) {
806 p -> icon = strdup ( dd + 5 );
807 } else if ( strcmp ( dd, PND_DOTDESKTOP_SOURCE ) == 0 ) {
809 } else if ( strncmp ( dd, "X-Pandora-UID=", 14 ) == 0 ) {
810 p -> unique_id = strdup ( dd + 14 );
811 } else if ( strncmp ( dd, "Comment=", 8 ) == 0 ) {
812 p -> desc_en = strdup ( dd + 8 );
813 } else if ( strncmp ( dd, "Comment[en]=", 12 ) == 0 ) {
814 p -> desc_en = strdup ( dd + 12 );
815 } else if ( strncmp ( dd, "Exec=", 5 ) == 0 ) {
817 char *e = strstr ( dd, " -e " );
820 // probably libpnd app
825 char *space = strchr ( e, ' ' );
826 p -> exec = strndup ( e, space - e - 1 );
829 char *b = strstr ( dd, " -b " );
832 char *space = strchr ( b, '\0' );
833 p -> appdata_dirname = strndup ( b, space - b - 1 );
836 char *p = strstr ( dd, " -p " );
839 char *space = strchr ( p, ' ' );
840 strncpy ( pndpath, p, space - p - 1 );
844 // probably not libpnd app
845 p -> exec = strdup ( dd + 5 );
848 } else if ( strncmp ( dd, "Categories=", 11 ) == 0 ) {
849 // HACK; only honours first category
850 char *semi = strchr ( dd, ';' );
852 p -> main_category = strndup ( dd + 11, semi - dd + 11 );
854 p -> main_category = strdup ( dd + 11 );
856 semi = strchr ( p -> main_category, ';' );
870 if ( ! libpnd_origin ) {
873 if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
874 pnd_disco_destroy ( p );
880 p -> object_flags |= PND_DISCO_CUSTOM1; // so caller can do something if it wishes
884 if ( ( ! p -> title_en ) ||
888 pnd_disco_destroy ( p );
893 if ( ! p -> unique_id ) {
894 if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
895 pnd_disco_destroy ( p );
900 snprintf ( hack, 100, "inode-%lu", statbuf.st_ino );
901 p -> unique_id = strdup ( hack );
906 p -> object_type = pnd_object_type_pnd;
909 if ( pndpath [ 0 ] ) {
915 char *slash = strrchr ( source, '/' );
917 p -> object_path = strndup ( source, slash - source );
918 p -> object_filename = strdup ( slash + 1 );
920 p -> object_path = "./";
921 p -> object_filename = strdup ( source );
924 fprintf ( stderr, "object %s /// src %s /// %s /// %s\n", ddpath, source, p -> object_path, p -> object_filename );