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 );
70 if ( p -> unique_id ) {
71 fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
75 if ( p -> desc_en && p -> desc_en [ 0 ] ) {
76 snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
77 fprintf ( f, "%s", buffer );
81 #if 0 // we let pnd_run.sh handle this
82 if ( p -> startdir ) {
83 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
84 fprintf ( f, "%s", buffer );
86 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
93 if ( p -> option_no_x11 ) {
94 nohup = "/usr/bin/nohup ";
100 if ( p -> object_type == pnd_object_type_directory ) {
101 snprintf ( buffer, 1020, "Exec=%s%s -p %s -e %s -b %s",
102 nohup, pndrun, p -> object_path, p -> exec, p -> unique_id );
103 } else if ( p -> object_type == pnd_object_type_pnd ) {
104 snprintf ( buffer, 1020, "Exec=%s%s -p %s/%s -e %s -b %s",
105 nohup, pndrun, p -> object_path, p -> object_filename, p -> exec, p -> unique_id );
109 if ( p -> startdir ) {
110 strncat ( buffer, " -s ", 1020 );
111 strncat ( buffer, p -> startdir, 1020 );
115 if ( p -> option_no_x11 ) {
116 strncat ( buffer, " -x ", 1020 );
120 strncat ( buffer, "\n", 1020 );
123 fprintf ( f, "%s", buffer );
128 char cats [ 512 ] = "";
134 // "Application" used to be in the standard and is commonly supported still
135 // Utility and Network should ensure the app is visible 'somewhere' :/
136 char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
138 // determine searchpath (for conf, not for apps)
139 confpath = pnd_conf_query_searchpath();
141 // inhale the conf file
142 c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
144 // if we can find a default category set, pull it in; otherwise assume
146 if ( pnd_conf_get_as_char ( c, "default" ) ) {
147 defaults = pnd_conf_get_as_char ( c, "default" );
150 // ditch the confpath
156 n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
159 fprintf ( f, "Categories=%s\n", cats );
161 fprintf ( f, "Categories=%s\n", defaults );
165 fprintf ( f, "Categories=%s\n", defaults );
170 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
177 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
178 char buffer [ FILENAME_MAX ]; // target filename
179 char from [ FILENAME_MAX ]; // source filename
180 char bits [ 8 * 1024 ];
184 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
185 if ( ( p -> object_type == pnd_object_type_pnd ) &&
186 ( ! p -> pnd_icon_pos ) )
188 return ( 0 ); // discover code didn't find it, so FAIL
191 // determine filename for target
192 sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
194 /* first.. open the source file, by type of application:
195 * are we looking through a pnd file or a dir?
197 if ( p -> object_type == pnd_object_type_directory ) {
198 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
199 } else if ( p -> object_type == pnd_object_type_pnd ) {
200 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
203 pnd = fopen ( from, "r" );
211 target = fopen ( buffer, "wb" );
218 fseek ( pnd, 0, SEEK_END );
220 //fseek ( pnd, 0, SEEK_SET );
222 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
224 len -= p -> pnd_icon_pos;
228 if ( len > (8*1024) ) {
234 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
241 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
257 #if 1 // we switched direction to freedesktop standard categories
258 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
259 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
263 // clear target so we can easily append
264 memset ( target_buffer, '\0', len );
266 // for each main-cat and sub-cat, including alternates, just append them all together
267 // we'll try mapping them, since the categories file is there, but we'll default to
268 // copying over; this lets the conf file do merging or renaming of cagtegories, which
269 // could still be useful, but we can leave the conf file empty to effect a pure
270 // trusted-PXML-copying
272 // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
273 // there are hundreds), and would also mean new ones and peoples custom ones would
276 /* attempt primary category chain
278 #define MAPCAT(field) \
279 if ( ( t = d -> field ) ) { \
280 match = pnd_map_dotdesktop_category ( c, t ); \
281 strncat ( target_buffer, match ? match : t, len ); \
282 strncat ( target_buffer, ";", len ); \
285 MAPCAT(main_category);
286 MAPCAT(main_category1);
287 MAPCAT(main_category2);
288 MAPCAT(alt_category);
289 MAPCAT(alt_category1);
290 MAPCAT(alt_category2);
292 if ( target_buffer [ 0 ] ) {
293 return ( 1 ); // I guess its 'good'?
297 if ( ( t = d -> main_category ) ) {
298 match = pnd_map_dotdesktop_category ( c, t );
299 strncat ( target_buffer, match ? match : t, len );
300 strncat ( target_buffer, ";", len );
308 #if 0 // we switched direction
309 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
310 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
311 unsigned short int n = 0; // no. matches
315 // clear target so we can easily append
316 memset ( target_buffer, '\0', len );
318 /* attempt primary category chain
322 if ( ( t = d -> main_category ) ) {
323 match = pnd_map_dotdesktop_category ( c, t );
327 ( t = d -> main_category1 ) )
329 match = pnd_map_dotdesktop_category ( c, t );
333 ( t = d -> main_category2 ) )
335 match = pnd_map_dotdesktop_category ( c, t );
339 strncat ( target_buffer, match, len );
340 len -= strlen ( target_buffer );
344 /* attempt secondary category chain
348 if ( ( t = d -> alt_category ) ) {
349 match = pnd_map_dotdesktop_category ( c, t );
353 ( t = d -> alt_category1 ) )
355 match = pnd_map_dotdesktop_category ( c, t );
359 ( t = d -> alt_category2 ) )
361 match = pnd_map_dotdesktop_category ( c, t );
365 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
366 strcat ( target_buffer, ";" );
369 strncat ( target_buffer, match, len );
370 len -= strlen ( target_buffer );
374 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
377 if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
378 match = pnd_map_dotdesktop_category ( c, t );
382 ( t = pnd_pxml_get_subcategory1 ( h ) ) )
384 match = pnd_map_dotdesktop_category ( c, t );
388 ( t = pnd_pxml_get_subcategory2 ( h ) ) )
390 match = pnd_map_dotdesktop_category ( c, t );
394 strncat ( target_buffer, match, len );
395 len -= strlen ( target_buffer );
399 /* attempt secondary category chain
403 if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
404 match = pnd_map_dotdesktop_category ( c, t );
408 ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
410 match = pnd_map_dotdesktop_category ( c, t );
414 ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
416 match = pnd_map_dotdesktop_category ( c, t );
420 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
421 strcat ( target_buffer, ";" );
424 strncat ( target_buffer, match, len );
425 len -= strlen ( target_buffer );
431 strcat ( target_buffer, ";" );
438 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
439 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
443 key = malloc ( strlen ( single_category ) + 4 + 1 );
445 sprintf ( key, "map.%s", single_category );
447 ret = pnd_conf_get_as_char ( c, key );
454 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
455 // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
456 // with a fwrite at the end...
457 char from [ FILENAME_MAX ]; // source filename
458 char bits [ 8 * 1024 ];
461 unsigned char *target = NULL, *targiter = NULL;
463 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
464 if ( ( p -> object_type == pnd_object_type_pnd ) &&
465 ( ! p -> pnd_icon_pos ) )
467 return ( NULL ); // discover code didn't find it, so FAIL
470 /* first.. open the source file, by type of application:
471 * are we looking through a pnd file or a dir?
473 if ( p -> object_type == pnd_object_type_directory ) {
474 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
475 } else if ( p -> object_type == pnd_object_type_pnd ) {
476 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
479 pnd = fopen ( from, "r" );
485 // determine length of file, then adjust by icon position to find begin of icon
488 fseek ( pnd, 0, SEEK_END );
490 //fseek ( pnd, 0, SEEK_SET );
492 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
494 len -= p -> pnd_icon_pos;
496 // create target buffer
497 target = malloc ( len );
510 // copy over icon to target
513 if ( len > (8*1024) ) {
519 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
525 memmove ( targiter, bits, bitlen );