2 #include <stdio.h> /* for FILE etc */
4 #include <unistd.h> /* for unlink */
5 #include <stdlib.h> /* for free */
8 #include "pnd_container.h"
10 #include "pnd_discovery.h"
11 #include "pnd_pndfiles.h"
13 #include "pnd_desktop.h"
14 #include "pnd_logger.h"
16 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
17 char filename [ FILENAME_MAX ];
22 // http://standards.freedesktop.org/desktop-entry-spec
26 if ( ! p -> unique_id ) {
27 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
32 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing exec\n", targetpath );
37 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
42 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
48 sprintf ( filename, "%s/%s#%u.desktop", targetpath, p -> unique_id, p -> subapp_number );
52 //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
54 f = fopen ( filename, "w" );
60 fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
62 if ( p -> title_en ) {
63 snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
64 fprintf ( f, "%s", buffer );
67 fprintf ( f, "Type=Application\n" );
68 fprintf ( f, "Version=1.0\n" );
71 snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
72 fprintf ( f, "%s", buffer );
75 if ( p -> unique_id ) {
76 fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
80 if ( p -> desc_en && p -> desc_en [ 0 ] ) {
81 snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
82 fprintf ( f, "%s", buffer );
86 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
87 if ( p -> startdir ) {
88 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
89 fprintf ( f, "%s", buffer );
91 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
98 if ( p -> option_no_x11 ) {
99 nohup = "/usr/bin/nohup ";
105 if ( p -> object_type == pnd_object_type_directory ) {
106 snprintf ( buffer, 1020, "Exec=%s%s -p \"%s\" -e \"%s\" -b \"%s\"",
107 nohup, pndrun, p -> object_path, p -> exec,
108 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
109 } else if ( p -> object_type == pnd_object_type_pnd ) {
110 snprintf ( buffer, 1020, "Exec=%s%s -p \"%s/%s\" -e \"%s\" -b \"%s\"",
111 nohup, pndrun, p -> object_path, p -> object_filename, p -> exec,
112 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
116 if ( p -> startdir ) {
117 strncat ( buffer, " -s ", 1020 );
118 strncat ( buffer, p -> startdir, 1020 );
122 if ( p -> execargs ) {
123 char argbuf [ 1024 ];
124 snprintf ( argbuf, 1000, " -a \"%s\"", p -> execargs );
125 strncat ( buffer, argbuf, 1020 );
129 if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
130 strncat ( buffer, " -c ", 1020 );
131 strncat ( buffer, p -> clockspeed, 1020 );
135 if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
136 strncat ( buffer, " -x ", 1020 );
140 strncat ( buffer, "\n", 1020 );
143 fprintf ( f, "%s", buffer );
148 char cats [ 512 ] = "";
154 // "Application" used to be in the standard and is commonly supported still
155 // Utility and Network should ensure the app is visible 'somewhere' :/
156 char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
158 // determine searchpath (for conf, not for apps)
159 confpath = pnd_conf_query_searchpath();
161 // inhale the conf file
162 c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
164 // if we can find a default category set, pull it in; otherwise assume
166 if ( pnd_conf_get_as_char ( c, "default" ) ) {
167 defaults = pnd_conf_get_as_char ( c, "default" );
170 // ditch the confpath
176 n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
179 fprintf ( f, "Categories=%s\n", cats );
181 fprintf ( f, "Categories=%s\n", defaults );
185 fprintf ( f, "Categories=%s\n", defaults );
190 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
197 unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
198 char filename [ FILENAME_MAX ];
199 char buffer [ 1024 ];
201 char *viewer, *searchpath;
202 pnd_conf_handle desktoph;
205 // http://standards.freedesktop.org/desktop-entry-spec
211 searchpath = pnd_conf_query_searchpath();
213 desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
219 viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
222 return ( 0 ); // no way to view the file
226 if ( ! p -> unique_id ) {
227 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
231 if ( ! p -> info_filename ) {
232 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_filename\n", targetpath );
236 if ( ! p -> info_name ) {
237 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_name\n", targetpath );
241 if ( ! targetpath ) {
242 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
247 pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
253 sprintf ( filename, "%s/%s#%uinfo.desktop", targetpath, p -> unique_id, p -> subapp_number );
257 f = fopen ( filename, "w" );
263 fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
265 if ( p -> info_name ) {
266 snprintf ( buffer, 1020, "Name=%s\n", p -> info_name );
267 fprintf ( f, "%s", buffer );
270 fprintf ( f, "Type=Application\n" );
271 fprintf ( f, "Version=1.0\n" );
275 snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
276 fprintf ( f, "%s", buffer );
280 if ( p -> unique_id ) {
281 fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
284 if ( p -> title_en && p -> title_en [ 0 ] ) {
285 snprintf ( buffer, 1020, "Comment=Automatic menu info entry for %s\n", p -> title_en );
286 fprintf ( f, "%s", buffer );
289 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
290 if ( p -> startdir ) {
291 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
292 fprintf ( f, "%s", buffer );
294 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
301 char *viewerargs = pnd_conf_get_as_char ( desktoph, "info.viewer_args" );
302 if ( viewerargs && viewerargs [ 0 ] ) {
303 snprintf ( pargs, 1001, "%s %s",
304 pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
307 // WARNING: This might not be quite right; if no viewer-args, shouldn't we still append the info-filename? likewise,
308 // even if we do have view-args, shouldn't we check if filename is present?
311 char pndfile [ 1024 ];
312 if ( p -> object_type == pnd_object_type_directory ) {
313 // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
314 strncpy ( pndfile, p -> object_path, 1000 );
315 } else if ( p -> object_type == pnd_object_type_pnd ) {
316 // pnd_run.sh wants the full path and filename for the .pnd file
317 snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
320 pnd_apps_exec_info_t info;
321 info.viewer = viewer;
324 if ( ! pnd_apps_exec_disco ( pndrun, p, PND_EXEC_OPTION_NORUN | PND_EXEC_OPTION_INFO, &info ) ) {
328 fprintf ( f, "Exec=%s\n", pnd_apps_exec_runline() );
330 if ( pnd_conf_get_as_char ( desktoph, "info.category" ) ) {
331 fprintf ( f, "Categories=%s\n", pnd_conf_get_as_char ( desktoph, "info.category" ) );
333 fprintf ( f, "Categories=Documentation\n" );
336 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
343 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
344 //#define BITLEN (8*1024)
345 #define BITLEN (64*1024)
346 char buffer [ FILENAME_MAX ]; // target filename
347 char from [ FILENAME_MAX ]; // source filename
348 unsigned char bits [ BITLEN ];
352 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
353 if ( ( p -> object_type == pnd_object_type_pnd ) &&
354 ( ! p -> pnd_icon_pos ) )
356 return ( 0 ); // discover code didn't find it, so FAIL
359 // determine filename for target
360 sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target
362 /* first.. open the source file, by type of application:
363 * are we looking through a pnd file or a dir?
365 if ( p -> object_type == pnd_object_type_directory ) {
366 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
367 } else if ( p -> object_type == pnd_object_type_pnd ) {
368 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
371 pnd = fopen ( from, "rb" );
374 pnd_log ( PND_LOG_DEFAULT, " Emit icon, couldn't open source\n" );
380 target = fopen ( buffer, "wb" );
384 pnd_log ( PND_LOG_DEFAULT, " Emit icon, couldn't open target\n" );
388 fseek ( pnd, 0, SEEK_END );
390 //fseek ( pnd, 0, SEEK_SET );
392 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
394 len -= p -> pnd_icon_pos;
396 pnd_log ( PND_LOG_DEFAULT, " Emit icon, length: %u\n", len );
400 if ( len > (BITLEN) ) {
406 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
410 pnd_log ( PND_LOG_DEFAULT, " Emit icon, bad read\n" );
417 char bigbuffer [ 200 * 1024 ] = "\0";
419 pnd_log ( PND_LOG_DEFAULT, " Read hexdump\n" );
420 while ( i < bitlen ) {
421 sprintf ( b, "%x,", bits [ i ] );
422 strcat ( bigbuffer, b );
425 pnd_log ( PND_LOG_DEFAULT, bigbuffer );
429 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
433 pnd_log ( PND_LOG_DEFAULT, " Emit icon, bad write\n" );
438 //pnd_log ( PND_LOG_DEFAULT, " Emit icon, next block, length: %u\n", len );
444 //pnd_log ( PND_LOG_DEFAULT, " Emit icon, done.\n" );
449 #if 1 // we switched direction to freedesktop standard categories
450 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
451 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
455 // clear target so we can easily append
456 memset ( target_buffer, '\0', len );
458 // for each main-cat and sub-cat, including alternates, just append them all together
459 // we'll try mapping them, since the categories file is there, but we'll default to
460 // copying over; this lets the conf file do merging or renaming of cagtegories, which
461 // could still be useful, but we can leave the conf file empty to effect a pure
462 // trusted-PXML-copying
464 // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
465 // there are hundreds), and would also mean new ones and peoples custom ones would
468 /* attempt primary category chain
470 #define MAPCAT(field) \
471 if ( ( t = d -> field ) ) { \
472 match = pnd_map_dotdesktop_category ( c, t ); \
473 strncat ( target_buffer, match ? match : t, len ); \
474 strncat ( target_buffer, ";", len ); \
477 MAPCAT(main_category);
478 MAPCAT(main_category1);
479 MAPCAT(main_category2);
480 MAPCAT(alt_category);
481 MAPCAT(alt_category1);
482 MAPCAT(alt_category2);
484 if ( target_buffer [ 0 ] ) {
485 return ( 1 ); // I guess its 'good'?
489 if ( ( t = d -> main_category ) ) {
490 match = pnd_map_dotdesktop_category ( c, t );
491 strncat ( target_buffer, match ? match : t, len );
492 strncat ( target_buffer, ";", len );
500 #if 0 // we switched direction
501 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
502 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
503 unsigned short int n = 0; // no. matches
507 // clear target so we can easily append
508 memset ( target_buffer, '\0', len );
510 /* attempt primary category chain
514 if ( ( t = d -> main_category ) ) {
515 match = pnd_map_dotdesktop_category ( c, t );
519 ( t = d -> main_category1 ) )
521 match = pnd_map_dotdesktop_category ( c, t );
525 ( t = d -> main_category2 ) )
527 match = pnd_map_dotdesktop_category ( c, t );
531 strncat ( target_buffer, match, len );
532 len -= strlen ( target_buffer );
536 /* attempt secondary category chain
540 if ( ( t = d -> alt_category ) ) {
541 match = pnd_map_dotdesktop_category ( c, t );
545 ( t = d -> alt_category1 ) )
547 match = pnd_map_dotdesktop_category ( c, t );
551 ( t = d -> alt_category2 ) )
553 match = pnd_map_dotdesktop_category ( c, t );
557 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
558 strcat ( target_buffer, ";" );
561 strncat ( target_buffer, match, len );
562 len -= strlen ( target_buffer );
566 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
569 if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
570 match = pnd_map_dotdesktop_category ( c, t );
574 ( t = pnd_pxml_get_subcategory1 ( h ) ) )
576 match = pnd_map_dotdesktop_category ( c, t );
580 ( t = pnd_pxml_get_subcategory2 ( h ) ) )
582 match = pnd_map_dotdesktop_category ( c, t );
586 strncat ( target_buffer, match, len );
587 len -= strlen ( target_buffer );
591 /* attempt secondary category chain
595 if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
596 match = pnd_map_dotdesktop_category ( c, t );
600 ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
602 match = pnd_map_dotdesktop_category ( c, t );
606 ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
608 match = pnd_map_dotdesktop_category ( c, t );
612 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
613 strcat ( target_buffer, ";" );
616 strncat ( target_buffer, match, len );
617 len -= strlen ( target_buffer );
623 strcat ( target_buffer, ";" );
630 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
631 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
635 key = malloc ( strlen ( single_category ) + 4 + 1 );
637 sprintf ( key, "map.%s", single_category );
639 ret = pnd_conf_get_as_char ( c, key );
646 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
647 // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
648 // with a fwrite at the end...
649 char from [ FILENAME_MAX ]; // source filename
650 char bits [ 8 * 1024 ];
653 unsigned char *target = NULL, *targiter = NULL;
655 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
656 if ( ( p -> object_type == pnd_object_type_pnd ) &&
657 ( ! p -> pnd_icon_pos ) )
659 return ( NULL ); // discover code didn't find it, so FAIL
662 /* first.. open the source file, by type of application:
663 * are we looking through a pnd file or a dir?
665 if ( p -> object_type == pnd_object_type_directory ) {
666 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
667 } else if ( p -> object_type == pnd_object_type_pnd ) {
668 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
671 pnd = fopen ( from, "r" );
677 // determine length of file, then adjust by icon position to find begin of icon
680 fseek ( pnd, 0, SEEK_END );
682 //fseek ( pnd, 0, SEEK_SET );
684 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
686 len -= p -> pnd_icon_pos;
688 // create target buffer
689 target = malloc ( len );
702 // copy over icon to target
705 if ( len > (8*1024) ) {
711 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
717 memmove ( targiter, bits, bitlen );