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 handle this
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, p -> unique_id );
108 } else if ( p -> object_type == pnd_object_type_pnd ) {
109 snprintf ( buffer, 1020, "Exec=%s%s -p %s/%s -e %s -b %s",
110 nohup, pndrun, p -> object_path, p -> object_filename, p -> exec, p -> unique_id );
114 if ( p -> startdir ) {
115 strncat ( buffer, " -s ", 1020 );
116 strncat ( buffer, p -> startdir, 1020 );
120 if ( p -> execargs ) {
121 strncat ( buffer, " -a ", 1020 );
122 strncat ( buffer, p -> execargs, 1020 );
126 if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
127 strncat ( buffer, " -c ", 1020 );
128 strncat ( buffer, p -> clockspeed, 1020 );
132 if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
133 strncat ( buffer, " -x ", 1020 );
137 strncat ( buffer, "\n", 1020 );
140 fprintf ( f, "%s", buffer );
145 char cats [ 512 ] = "";
151 // "Application" used to be in the standard and is commonly supported still
152 // Utility and Network should ensure the app is visible 'somewhere' :/
153 char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
155 // determine searchpath (for conf, not for apps)
156 confpath = pnd_conf_query_searchpath();
158 // inhale the conf file
159 c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
161 // if we can find a default category set, pull it in; otherwise assume
163 if ( pnd_conf_get_as_char ( c, "default" ) ) {
164 defaults = pnd_conf_get_as_char ( c, "default" );
167 // ditch the confpath
173 n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
176 fprintf ( f, "Categories=%s\n", cats );
178 fprintf ( f, "Categories=%s\n", defaults );
182 fprintf ( f, "Categories=%s\n", defaults );
187 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
194 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
195 //#define BITLEN (8*1024)
196 #define BITLEN (64*1024)
197 char buffer [ FILENAME_MAX ]; // target filename
198 char from [ FILENAME_MAX ]; // source filename
199 unsigned char bits [ BITLEN ];
203 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
204 if ( ( p -> object_type == pnd_object_type_pnd ) &&
205 ( ! p -> pnd_icon_pos ) )
207 return ( 0 ); // discover code didn't find it, so FAIL
210 // determine filename for target
211 sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target
213 /* first.. open the source file, by type of application:
214 * are we looking through a pnd file or a dir?
216 if ( p -> object_type == pnd_object_type_directory ) {
217 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
218 } else if ( p -> object_type == pnd_object_type_pnd ) {
219 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
222 pnd = fopen ( from, "rb" );
225 pnd_log ( PND_LOG_DEFAULT, " Emit icon, couldn't open source\n" );
231 target = fopen ( buffer, "wb" );
235 pnd_log ( PND_LOG_DEFAULT, " Emit icon, couldn't open target\n" );
239 fseek ( pnd, 0, SEEK_END );
241 //fseek ( pnd, 0, SEEK_SET );
243 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
245 len -= p -> pnd_icon_pos;
247 pnd_log ( PND_LOG_DEFAULT, " Emit icon, length: %u\n", len );
251 if ( len > (BITLEN) ) {
257 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
261 pnd_log ( PND_LOG_DEFAULT, " Emit icon, bad read\n" );
268 char bigbuffer [ 200 * 1024 ] = "\0";
270 pnd_log ( PND_LOG_DEFAULT, " Read hexdump\n" );
271 while ( i < bitlen ) {
272 sprintf ( b, "%x,", bits [ i ] );
273 strcat ( bigbuffer, b );
276 pnd_log ( PND_LOG_DEFAULT, bigbuffer );
280 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
284 pnd_log ( PND_LOG_DEFAULT, " Emit icon, bad write\n" );
289 //pnd_log ( PND_LOG_DEFAULT, " Emit icon, next block, length: %u\n", len );
295 //pnd_log ( PND_LOG_DEFAULT, " Emit icon, done.\n" );
300 #if 1 // we switched direction to freedesktop standard categories
301 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
302 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
306 // clear target so we can easily append
307 memset ( target_buffer, '\0', len );
309 // for each main-cat and sub-cat, including alternates, just append them all together
310 // we'll try mapping them, since the categories file is there, but we'll default to
311 // copying over; this lets the conf file do merging or renaming of cagtegories, which
312 // could still be useful, but we can leave the conf file empty to effect a pure
313 // trusted-PXML-copying
315 // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
316 // there are hundreds), and would also mean new ones and peoples custom ones would
319 /* attempt primary category chain
321 #define MAPCAT(field) \
322 if ( ( t = d -> field ) ) { \
323 match = pnd_map_dotdesktop_category ( c, t ); \
324 strncat ( target_buffer, match ? match : t, len ); \
325 strncat ( target_buffer, ";", len ); \
328 MAPCAT(main_category);
329 MAPCAT(main_category1);
330 MAPCAT(main_category2);
331 MAPCAT(alt_category);
332 MAPCAT(alt_category1);
333 MAPCAT(alt_category2);
335 if ( target_buffer [ 0 ] ) {
336 return ( 1 ); // I guess its 'good'?
340 if ( ( t = d -> main_category ) ) {
341 match = pnd_map_dotdesktop_category ( c, t );
342 strncat ( target_buffer, match ? match : t, len );
343 strncat ( target_buffer, ";", len );
351 #if 0 // we switched direction
352 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
353 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
354 unsigned short int n = 0; // no. matches
358 // clear target so we can easily append
359 memset ( target_buffer, '\0', len );
361 /* attempt primary category chain
365 if ( ( t = d -> main_category ) ) {
366 match = pnd_map_dotdesktop_category ( c, t );
370 ( t = d -> main_category1 ) )
372 match = pnd_map_dotdesktop_category ( c, t );
376 ( t = d -> main_category2 ) )
378 match = pnd_map_dotdesktop_category ( c, t );
382 strncat ( target_buffer, match, len );
383 len -= strlen ( target_buffer );
387 /* attempt secondary category chain
391 if ( ( t = d -> alt_category ) ) {
392 match = pnd_map_dotdesktop_category ( c, t );
396 ( t = d -> alt_category1 ) )
398 match = pnd_map_dotdesktop_category ( c, t );
402 ( t = d -> alt_category2 ) )
404 match = pnd_map_dotdesktop_category ( c, t );
408 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
409 strcat ( target_buffer, ";" );
412 strncat ( target_buffer, match, len );
413 len -= strlen ( target_buffer );
417 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
420 if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
421 match = pnd_map_dotdesktop_category ( c, t );
425 ( t = pnd_pxml_get_subcategory1 ( h ) ) )
427 match = pnd_map_dotdesktop_category ( c, t );
431 ( t = pnd_pxml_get_subcategory2 ( h ) ) )
433 match = pnd_map_dotdesktop_category ( c, t );
437 strncat ( target_buffer, match, len );
438 len -= strlen ( target_buffer );
442 /* attempt secondary category chain
446 if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
447 match = pnd_map_dotdesktop_category ( c, t );
451 ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
453 match = pnd_map_dotdesktop_category ( c, t );
457 ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
459 match = pnd_map_dotdesktop_category ( c, t );
463 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
464 strcat ( target_buffer, ";" );
467 strncat ( target_buffer, match, len );
468 len -= strlen ( target_buffer );
474 strcat ( target_buffer, ";" );
481 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
482 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
486 key = malloc ( strlen ( single_category ) + 4 + 1 );
488 sprintf ( key, "map.%s", single_category );
490 ret = pnd_conf_get_as_char ( c, key );
497 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
498 // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
499 // with a fwrite at the end...
500 char from [ FILENAME_MAX ]; // source filename
501 char bits [ 8 * 1024 ];
504 unsigned char *target = NULL, *targiter = NULL;
506 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
507 if ( ( p -> object_type == pnd_object_type_pnd ) &&
508 ( ! p -> pnd_icon_pos ) )
510 return ( NULL ); // discover code didn't find it, so FAIL
513 /* first.. open the source file, by type of application:
514 * are we looking through a pnd file or a dir?
516 if ( p -> object_type == pnd_object_type_directory ) {
517 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
518 } else if ( p -> object_type == pnd_object_type_pnd ) {
519 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
522 pnd = fopen ( from, "r" );
528 // determine length of file, then adjust by icon position to find begin of icon
531 fseek ( pnd, 0, SEEK_END );
533 //fseek ( pnd, 0, SEEK_SET );
535 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
537 len -= p -> pnd_icon_pos;
539 // create target buffer
540 target = malloc ( len );
553 // copy over icon to target
556 if ( len > (8*1024) ) {
562 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
568 memmove ( targiter, bits, bitlen );