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 -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
121 strncat ( buffer, " -c ", 1020 );
122 strncat ( buffer, p -> clockspeed, 1020 );
126 if ( pnd_pxml_is_affirmative ( p -> option_no_x11 ) ) {
127 strncat ( buffer, " -x ", 1020 );
131 strncat ( buffer, "\n", 1020 );
134 fprintf ( f, "%s", buffer );
139 char cats [ 512 ] = "";
145 // "Application" used to be in the standard and is commonly supported still
146 // Utility and Network should ensure the app is visible 'somewhere' :/
147 char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
149 // determine searchpath (for conf, not for apps)
150 confpath = pnd_conf_query_searchpath();
152 // inhale the conf file
153 c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
155 // if we can find a default category set, pull it in; otherwise assume
157 if ( pnd_conf_get_as_char ( c, "default" ) ) {
158 defaults = pnd_conf_get_as_char ( c, "default" );
161 // ditch the confpath
167 n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
170 fprintf ( f, "Categories=%s\n", cats );
172 fprintf ( f, "Categories=%s\n", defaults );
176 fprintf ( f, "Categories=%s\n", defaults );
181 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
188 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
189 char buffer [ FILENAME_MAX ]; // target filename
190 char from [ FILENAME_MAX ]; // source filename
191 char bits [ 8 * 1024 ];
195 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
196 if ( ( p -> object_type == pnd_object_type_pnd ) &&
197 ( ! p -> pnd_icon_pos ) )
199 return ( 0 ); // discover code didn't find it, so FAIL
202 // determine filename for target
203 sprintf ( buffer, "%s/%s#%u.png", targetpath, p -> unique_id, p -> subapp_number ); // target
205 /* first.. open the source file, by type of application:
206 * are we looking through a pnd file or a dir?
208 if ( p -> object_type == pnd_object_type_directory ) {
209 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
210 } else if ( p -> object_type == pnd_object_type_pnd ) {
211 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
214 pnd = fopen ( from, "r" );
222 target = fopen ( buffer, "wb" );
229 fseek ( pnd, 0, SEEK_END );
231 //fseek ( pnd, 0, SEEK_SET );
233 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
235 len -= p -> pnd_icon_pos;
239 if ( len > (8*1024) ) {
245 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
252 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
268 #if 1 // we switched direction to freedesktop standard categories
269 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
270 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
274 // clear target so we can easily append
275 memset ( target_buffer, '\0', len );
277 // for each main-cat and sub-cat, including alternates, just append them all together
278 // we'll try mapping them, since the categories file is there, but we'll default to
279 // copying over; this lets the conf file do merging or renaming of cagtegories, which
280 // could still be useful, but we can leave the conf file empty to effect a pure
281 // trusted-PXML-copying
283 // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
284 // there are hundreds), and would also mean new ones and peoples custom ones would
287 /* attempt primary category chain
289 #define MAPCAT(field) \
290 if ( ( t = d -> field ) ) { \
291 match = pnd_map_dotdesktop_category ( c, t ); \
292 strncat ( target_buffer, match ? match : t, len ); \
293 strncat ( target_buffer, ";", len ); \
296 MAPCAT(main_category);
297 MAPCAT(main_category1);
298 MAPCAT(main_category2);
299 MAPCAT(alt_category);
300 MAPCAT(alt_category1);
301 MAPCAT(alt_category2);
303 if ( target_buffer [ 0 ] ) {
304 return ( 1 ); // I guess its 'good'?
308 if ( ( t = d -> main_category ) ) {
309 match = pnd_map_dotdesktop_category ( c, t );
310 strncat ( target_buffer, match ? match : t, len );
311 strncat ( target_buffer, ";", len );
319 #if 0 // we switched direction
320 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
321 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
322 unsigned short int n = 0; // no. matches
326 // clear target so we can easily append
327 memset ( target_buffer, '\0', len );
329 /* attempt primary category chain
333 if ( ( t = d -> main_category ) ) {
334 match = pnd_map_dotdesktop_category ( c, t );
338 ( t = d -> main_category1 ) )
340 match = pnd_map_dotdesktop_category ( c, t );
344 ( t = d -> main_category2 ) )
346 match = pnd_map_dotdesktop_category ( c, t );
350 strncat ( target_buffer, match, len );
351 len -= strlen ( target_buffer );
355 /* attempt secondary category chain
359 if ( ( t = d -> alt_category ) ) {
360 match = pnd_map_dotdesktop_category ( c, t );
364 ( t = d -> alt_category1 ) )
366 match = pnd_map_dotdesktop_category ( c, t );
370 ( t = d -> alt_category2 ) )
372 match = pnd_map_dotdesktop_category ( c, t );
376 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
377 strcat ( target_buffer, ";" );
380 strncat ( target_buffer, match, len );
381 len -= strlen ( target_buffer );
385 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
388 if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
389 match = pnd_map_dotdesktop_category ( c, t );
393 ( t = pnd_pxml_get_subcategory1 ( h ) ) )
395 match = pnd_map_dotdesktop_category ( c, t );
399 ( t = pnd_pxml_get_subcategory2 ( h ) ) )
401 match = pnd_map_dotdesktop_category ( c, t );
405 strncat ( target_buffer, match, len );
406 len -= strlen ( target_buffer );
410 /* attempt secondary category chain
414 if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
415 match = pnd_map_dotdesktop_category ( c, t );
419 ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
421 match = pnd_map_dotdesktop_category ( c, t );
425 ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
427 match = pnd_map_dotdesktop_category ( c, t );
431 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
432 strcat ( target_buffer, ";" );
435 strncat ( target_buffer, match, len );
436 len -= strlen ( target_buffer );
442 strcat ( target_buffer, ";" );
449 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
450 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
454 key = malloc ( strlen ( single_category ) + 4 + 1 );
456 sprintf ( key, "map.%s", single_category );
458 ret = pnd_conf_get_as_char ( c, key );
465 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
466 // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
467 // with a fwrite at the end...
468 char from [ FILENAME_MAX ]; // source filename
469 char bits [ 8 * 1024 ];
472 unsigned char *target = NULL, *targiter = NULL;
474 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
475 if ( ( p -> object_type == pnd_object_type_pnd ) &&
476 ( ! p -> pnd_icon_pos ) )
478 return ( NULL ); // discover code didn't find it, so FAIL
481 /* first.. open the source file, by type of application:
482 * are we looking through a pnd file or a dir?
484 if ( p -> object_type == pnd_object_type_directory ) {
485 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
486 } else if ( p -> object_type == pnd_object_type_pnd ) {
487 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
490 pnd = fopen ( from, "r" );
496 // determine length of file, then adjust by icon position to find begin of icon
499 fseek ( pnd, 0, SEEK_END );
501 //fseek ( pnd, 0, SEEK_SET );
503 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
505 len -= p -> pnd_icon_pos;
507 // create target buffer
508 target = malloc ( len );
521 // copy over icon to target
524 if ( len > (8*1024) ) {
530 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
536 memmove ( targiter, bits, bitlen );