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"
15 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
16 char filename [ FILENAME_MAX ];
21 // http://standards.freedesktop.org/desktop-entry-spec
25 if ( ! p -> unique_id ) {
43 sprintf ( filename, "%s/%s.desktop", targetpath, p -> unique_id );
47 //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
49 f = fopen ( filename, "w" );
55 fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
57 if ( p -> title_en ) {
58 snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
59 fprintf ( f, "%s", buffer );
62 fprintf ( f, "Type=Application\n" );
63 fprintf ( f, "Version=1.0\n" );
66 snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
67 fprintf ( f, "%s", buffer );
71 if ( p -> desc_en && p -> desc_en [ 0 ] ) {
72 snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
73 fprintf ( f, "%s", buffer );
77 #if 0 // we let pnd_run.sh handle this
78 if ( p -> startdir ) {
79 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
80 fprintf ( f, "%s", buffer );
82 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
89 if ( p -> option_no_x11 ) {
90 nohup = "/usr/bin/nohup ";
96 if ( p -> object_type == pnd_object_type_directory ) {
97 snprintf ( buffer, 1020, "Exec=%s%s -p %s -e %s -b %s",
98 nohup, pndrun, p -> object_path, p -> exec, p -> unique_id );
99 } else if ( p -> object_type == pnd_object_type_pnd ) {
100 snprintf ( buffer, 1020, "Exec=%s%s -p %s/%s -e %s -b %s",
101 nohup, pndrun, p -> object_path, p -> object_filename, p -> exec, p -> unique_id );
105 if ( p -> startdir ) {
106 strncat ( buffer, " -s ", 1020 );
107 strncat ( buffer, p -> startdir, 1020 );
111 if ( p -> option_no_x11 ) {
112 strncat ( buffer, " -x ", 1020 );
116 strncat ( buffer, "\n", 1020 );
119 fprintf ( f, "%s", buffer );
124 char cats [ 512 ] = "";
130 // "Application" used to be in the standard and is commonly supported still
131 // Utility and Network should ensure the app is visible 'somewhere' :/
132 char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
134 // determine searchpath (for conf, not for apps)
135 confpath = pnd_conf_query_searchpath();
137 // inhale the conf file
138 c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
140 // if we can find a default category set, pull it in; otherwise assume
142 if ( pnd_conf_get_as_char ( c, "default" ) ) {
143 defaults = pnd_conf_get_as_char ( c, "default" );
146 // ditch the confpath
152 n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
155 fprintf ( f, "Categories=%s\n", cats );
157 fprintf ( f, "Categories=%s\n", defaults );
161 fprintf ( f, "Categories=%s\n", defaults );
166 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
173 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
174 char buffer [ FILENAME_MAX ]; // target filename
175 char from [ FILENAME_MAX ]; // source filename
176 char bits [ 8 * 1024 ];
180 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
181 if ( ( p -> object_type == pnd_object_type_pnd ) &&
182 ( ! p -> pnd_icon_pos ) )
184 return ( 0 ); // discover code didn't find it, so FAIL
187 // determine filename for target
188 sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
190 /* first.. open the source file, by type of application:
191 * are we looking through a pnd file or a dir?
193 if ( p -> object_type == pnd_object_type_directory ) {
194 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
195 } else if ( p -> object_type == pnd_object_type_pnd ) {
196 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
199 pnd = fopen ( from, "r" );
207 target = fopen ( buffer, "wb" );
214 fseek ( pnd, 0, SEEK_END );
216 //fseek ( pnd, 0, SEEK_SET );
218 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
220 len -= p -> pnd_icon_pos;
224 if ( len > (8*1024) ) {
230 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
237 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
253 #if 1 // we switched direction to freedesktop standard categories
254 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
255 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
259 // clear target so we can easily append
260 memset ( target_buffer, '\0', len );
262 // for each main-cat and sub-cat, including alternates, just append them all together
263 // we'll try mapping them, since the categories file is there, but we'll default to
264 // copying over; this lets the conf file do merging or renaming of cagtegories, which
265 // could still be useful, but we can leave the conf file empty to effect a pure
266 // trusted-PXML-copying
268 // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
269 // there are hundreds), and would also mean new ones and peoples custom ones would
272 /* attempt primary category chain
274 #define MAPCAT(field) \
275 if ( ( t = d -> field ) ) { \
276 match = pnd_map_dotdesktop_category ( c, t ); \
277 strncat ( target_buffer, match ? match : t, len ); \
278 strncat ( target_buffer, ";", len ); \
281 MAPCAT(main_category);
282 MAPCAT(main_category1);
283 MAPCAT(main_category2);
284 MAPCAT(alt_category);
285 MAPCAT(alt_category1);
286 MAPCAT(alt_category2);
288 if ( target_buffer [ 0 ] ) {
289 return ( 1 ); // I guess its 'good'?
293 if ( ( t = d -> main_category ) ) {
294 match = pnd_map_dotdesktop_category ( c, t );
295 strncat ( target_buffer, match ? match : t, len );
296 strncat ( target_buffer, ";", len );
304 #if 0 // we switched direction
305 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
306 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
307 unsigned short int n = 0; // no. matches
311 // clear target so we can easily append
312 memset ( target_buffer, '\0', len );
314 /* attempt primary category chain
318 if ( ( t = d -> main_category ) ) {
319 match = pnd_map_dotdesktop_category ( c, t );
323 ( t = d -> main_category1 ) )
325 match = pnd_map_dotdesktop_category ( c, t );
329 ( t = d -> main_category2 ) )
331 match = pnd_map_dotdesktop_category ( c, t );
335 strncat ( target_buffer, match, len );
336 len -= strlen ( target_buffer );
340 /* attempt secondary category chain
344 if ( ( t = d -> alt_category ) ) {
345 match = pnd_map_dotdesktop_category ( c, t );
349 ( t = d -> alt_category1 ) )
351 match = pnd_map_dotdesktop_category ( c, t );
355 ( t = d -> alt_category2 ) )
357 match = pnd_map_dotdesktop_category ( c, t );
361 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
362 strcat ( target_buffer, ";" );
365 strncat ( target_buffer, match, len );
366 len -= strlen ( target_buffer );
370 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
373 if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
374 match = pnd_map_dotdesktop_category ( c, t );
378 ( t = pnd_pxml_get_subcategory1 ( h ) ) )
380 match = pnd_map_dotdesktop_category ( c, t );
384 ( t = pnd_pxml_get_subcategory2 ( h ) ) )
386 match = pnd_map_dotdesktop_category ( c, t );
390 strncat ( target_buffer, match, len );
391 len -= strlen ( target_buffer );
395 /* attempt secondary category chain
399 if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
400 match = pnd_map_dotdesktop_category ( c, t );
404 ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
406 match = pnd_map_dotdesktop_category ( c, t );
410 ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
412 match = pnd_map_dotdesktop_category ( c, t );
416 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
417 strcat ( target_buffer, ";" );
420 strncat ( target_buffer, match, len );
421 len -= strlen ( target_buffer );
427 strcat ( target_buffer, ";" );
434 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
435 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
439 key = malloc ( strlen ( single_category ) + 4 + 1 );
441 sprintf ( key, "map.%s", single_category );
443 ret = pnd_conf_get_as_char ( c, key );
450 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
451 // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
452 // with a fwrite at the end...
453 char from [ FILENAME_MAX ]; // source filename
454 char bits [ 8 * 1024 ];
457 unsigned char *target = NULL, *targiter = NULL;
459 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
460 if ( ( p -> object_type == pnd_object_type_pnd ) &&
461 ( ! p -> pnd_icon_pos ) )
463 return ( NULL ); // discover code didn't find it, so FAIL
466 /* first.. open the source file, by type of application:
467 * are we looking through a pnd file or a dir?
469 if ( p -> object_type == pnd_object_type_directory ) {
470 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
471 } else if ( p -> object_type == pnd_object_type_pnd ) {
472 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
475 pnd = fopen ( from, "r" );
481 // determine length of file, then adjust by icon position to find begin of icon
484 fseek ( pnd, 0, SEEK_END );
486 //fseek ( pnd, 0, SEEK_SET );
488 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
490 len -= p -> pnd_icon_pos;
492 // create target buffer
493 target = malloc ( len );
506 // copy over icon to target
509 if ( len > (8*1024) ) {
515 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
521 memmove ( targiter, bits, bitlen );