fb875ff211170409c963e303a96d606675db0b29
[pandora-libraries.git] / lib / pnd_desktop.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <string.h>
4 #include <unistd.h> /* for unlink */
5 #include <stdlib.h> /* for free */
6 #include <sys/stat.h> /* for stat */
7
8 #include "pnd_apps.h"
9 #include "pnd_container.h"
10 #include "pnd_pxml.h"
11 #include "pnd_discovery.h"
12 #include "pnd_pndfiles.h"
13 #include "pnd_conf.h"
14 #include "pnd_desktop.h"
15 #include "pnd_logger.h"
16
17 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
18   char filename [ FILENAME_MAX ];
19   char buffer [ 1024 ];
20   FILE *f;
21
22   // specification
23   // http://standards.freedesktop.org/desktop-entry-spec
24
25   // validation
26
27   if ( ! p -> unique_id ) {
28     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
29     return ( 0 );
30   }
31
32   if ( ! p -> exec ) {
33     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing exec\n", targetpath );
34     return ( 0 );
35   }
36
37   if ( ! targetpath ) {
38     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
39     return ( 0 );
40   }
41
42   if ( ! pndrun ) {
43     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
44     return ( 0 );
45   }
46
47   // set up
48
49   sprintf ( filename, "%s/%s#%u.desktop", targetpath, p -> unique_id, p -> subapp_number );
50
51   // emit
52
53   //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
54
55   f = fopen ( filename, "w" );
56
57   if ( ! f ) {
58     return ( 0 );
59   }
60
61   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
62
63   if ( p -> title_en ) {
64     snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
65     fprintf ( f, "%s", buffer );
66   }
67
68   fprintf ( f, "Type=Application\n" );
69   fprintf ( f, "Version=1.0\n" );
70
71   if ( p -> icon ) {
72     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
73     fprintf ( f, "%s", buffer );
74   }
75
76   if ( p -> unique_id ) {
77     fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
78   }
79
80 #if 1
81   if ( p -> desc_en && p -> desc_en [ 0 ] ) {
82     snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
83     fprintf ( f, "%s", buffer );
84   }
85 #endif
86
87   if ( p -> preview_pic1 ) {
88     fprintf ( f, "X-Pandora-Preview-Pic-1=%s\n", p -> preview_pic1 );
89   }
90
91   if ( p -> clockspeed ) {
92     fprintf ( f, "X-Pandora-Clockspeed=%s\n", p -> clockspeed );
93   }
94
95   if ( p -> startdir ) {
96     fprintf ( f, "X-Pandora-Startdir=%s\n", p -> startdir );
97   }
98
99   if ( p -> main_category ) {
100     fprintf ( f, "X-Pandora-MainCategory=%s\n", p -> main_category );
101   }
102   if ( p -> main_category1 ) {
103     fprintf ( f, "X-Pandora-MainCategory1=%s\n", p -> main_category1 );
104   }
105   if ( p -> main_category2 ) {
106     fprintf ( f, "X-Pandora-MainCategory2=%s\n", p -> main_category2 );
107   }
108
109   if ( p -> alt_category ) {
110     fprintf ( f, "X-Pandora-AltCategory=%s\n", p -> alt_category );
111   }
112   if ( p -> alt_category1 ) {
113     fprintf ( f, "X-Pandora-AltCategory1=%s\n", p -> alt_category1 );
114   }
115   if ( p -> alt_category2 ) {
116     fprintf ( f, "X-Pandora-AltCategory2=%s\n", p -> alt_category2 );
117   }
118   if ( p -> info_filename ) {
119     fprintf ( f, "X-Pandora-Info-Filename=%s\n", p -> info_filename );
120   }
121   if ( p -> info_name ) {
122     fprintf ( f, "X-Pandora-Info-Name=%s\n", p -> info_name );
123   }
124
125   if ( p -> associationitem1_name || p -> associationitem2_name || p -> associationitem3_name ) {
126     fprintf ( f, "MimeType=" );
127     if ( p -> associationitem1_name ) {
128       fprintf ( f, "%s;", p -> associationitem1_filetype );
129       pnd_log ( PND_LOG_DEFAULT, "App %s is handling mimetype %s [1]\n", p -> title_en, p -> associationitem1_filetype );
130     }
131     if ( p -> associationitem2_name ) {
132       fprintf ( f, "%s;", p -> associationitem2_filetype );
133       pnd_log ( PND_LOG_DEFAULT, "App %s is handling mimetype %s [2]\n", p -> title_en, p -> associationitem2_filetype );
134     }
135     if ( p -> associationitem3_name ) {
136       fprintf ( f, "%s;", p -> associationitem3_filetype );
137       pnd_log ( PND_LOG_DEFAULT, "App %s is handling mimetype %s [3]\n", p -> title_en, p -> associationitem3_filetype );
138     }
139     fprintf ( f, "\n" );
140   }
141
142 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
143   if ( p -> startdir ) {
144     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
145     fprintf ( f, "%s", buffer );
146   } else {
147     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
148   }
149 #endif
150
151   if ( p -> exec ) {
152     char *nohup;
153
154     if ( p -> option_no_x11 ) {
155       nohup = "/usr/bin/nohup ";
156     } else {
157       nohup = "";
158     }
159
160     // basics
161     if ( p -> object_type == pnd_object_type_directory ) {
162       snprintf ( buffer, 1020, "Exec=%s%s -p \"%s\" -e \"%s\" -b \"%s\"",
163                  nohup, pndrun, p -> object_path, p -> exec,
164                  p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
165     } else if ( p -> object_type == pnd_object_type_pnd ) {
166       snprintf ( buffer, 1020, "Exec=%s%s -p \"%s/%s\" -e \"%s\" -b \"%s\"",
167                  nohup, pndrun, p -> object_path, p -> object_filename, p -> exec,
168                  p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
169     }
170
171     // start dir
172     if ( p -> startdir ) {
173       strncat ( buffer, " -s ", 1020 );
174       strncat ( buffer, p -> startdir, 1020 );
175     }
176
177     // args
178     if ( p -> execargs ) {
179       char argbuf [ 1024 ];
180       snprintf ( argbuf, 1000, " -a \"%s\"", p -> execargs );
181       strncat ( buffer, argbuf, 1020 );
182     }
183
184     // clockspeed
185     if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
186       strncat ( buffer, " -c ", 1020 );
187       strncat ( buffer, p -> clockspeed, 1020 );
188     }
189
190     // exec options
191     if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
192       strncat ( buffer, " -x ", 1020 );
193     }
194
195     // newline
196     strncat ( buffer, "\n", 1020 );
197
198     // emit
199     fprintf ( f, "%s", buffer );
200
201     // and lets copy in some stuff in case it makes .desktop consumers life easier
202     if ( p -> exec ) { fprintf ( f, "X-Pandora-Exec=%s\n", p -> exec ); }
203     if ( p -> appdata_dirname ) { fprintf ( f, "X-Pandora-Appdata-Dirname=%s\n", p -> appdata_dirname ); }
204     if ( p -> execargs ) { fprintf ( f, "X-Pandora-ExecArgs=%s\n", p -> execargs ); }
205     if ( p -> object_flags & PND_DISCO_FLAG_OVR ) { fprintf ( f, "X-Pandora-Object-Flag-OVR=%s\n", "Yes" ); }
206     if ( p -> object_type == pnd_object_type_pnd ) {
207       fprintf ( f, "X-Pandora-Object-Path=%s\n", p -> object_path );
208       fprintf ( f, "X-Pandora-Object-Filename=%s\n", p -> object_filename );
209     }
210
211   }
212
213   // categories
214   {
215     char cats [ 512 ] = "";
216     int n;
217     pnd_conf_handle c;
218     char *confpath;
219
220     // uuuuh, defaults?
221     // "Application" used to be in the standard and is commonly supported still
222     // Utility and Network should ensure the app is visible 'somewhere' :/
223     char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
224
225     // determine searchpath (for conf, not for apps)
226     confpath = pnd_conf_query_searchpath();
227
228     // inhale the conf file
229     c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
230
231     // if we can find a default category set, pull it in; otherwise assume
232     // the hardcoded one
233     if ( pnd_conf_get_as_char ( c, "default" ) ) {
234       defaults = pnd_conf_get_as_char ( c, "default" );
235     }
236
237     // ditch the confpath
238     free ( confpath );
239
240     // attempt mapping
241     if ( c ) {
242
243       n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
244
245       if ( n ) {
246         fprintf ( f, "Categories=%s\n", cats );
247       } else {
248         fprintf ( f, "Categories=%s\n", defaults );
249       }
250
251     } else {
252       fprintf ( f, "Categories=%s\n", defaults );
253     }
254
255   }
256
257   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
258
259   fclose ( f );
260
261   return ( 1 );
262 }
263
264 unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
265   char filename [ FILENAME_MAX ];
266   char buffer [ 1024 ];
267   FILE *f;
268   char *viewer, *searchpath;
269   pnd_conf_handle desktoph;
270
271   // specification
272   // http://standards.freedesktop.org/desktop-entry-spec
273
274   // validation
275   //
276
277   // viewer
278   searchpath = pnd_conf_query_searchpath();
279
280   desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
281
282   if ( ! desktoph ) {
283     return ( 0 );
284   }
285
286   viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
287
288   if ( ! viewer ) {
289     return ( 0 ); // no way to view the file
290   }
291
292   // etc
293   if ( ! p -> unique_id ) {
294     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
295     return ( 0 );
296   }
297
298   if ( ! p -> info_filename ) {
299     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_filename\n", targetpath );
300     return ( 0 );
301   }
302
303   if ( ! p -> info_name ) {
304     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_name\n", targetpath );
305     return ( 0 );
306   }
307
308   if ( ! targetpath ) {
309     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
310     return ( 0 );
311   }
312
313   if ( ! pndrun ) {
314     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
315     return ( 0 );
316   }
317
318   // set up
319
320   sprintf ( filename, "%s/%s#%uinfo.desktop", targetpath, p -> unique_id, p -> subapp_number );
321
322   // emit
323
324   f = fopen ( filename, "w" );
325
326   if ( ! f ) {
327     return ( 0 );
328   }
329
330   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
331
332   if ( p -> info_name ) {
333     snprintf ( buffer, 1020, "Name=%s\n", p -> info_name );
334     fprintf ( f, "%s", buffer );
335   }
336
337   fprintf ( f, "Type=Application\n" );
338   fprintf ( f, "Version=1.0\n" );
339
340 #if 0
341   if ( p -> icon ) {
342     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
343     fprintf ( f, "%s", buffer );
344   }
345 #endif
346
347   if ( p -> unique_id ) {
348     fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
349   }
350
351   if ( p -> title_en && p -> title_en [ 0 ] ) {
352     snprintf ( buffer, 1020, "Comment=Automatic menu info entry for %s\n", p -> title_en );
353     fprintf ( f, "%s", buffer );
354   }
355
356 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
357   if ( p -> startdir ) {
358     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
359     fprintf ( f, "%s", buffer );
360   } else {
361     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
362   }
363 #endif
364
365   // exec line
366   char args [ 1001 ];
367   char *pargs = args;
368   char *viewerargs = pnd_conf_get_as_char ( desktoph, "info.viewer_args" );
369   if ( viewerargs && viewerargs [ 0 ] ) {
370     snprintf ( pargs, 1001, "%s %s",
371                pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
372   } else {
373     pargs = NULL;
374     // WARNING: This might not be quite right; if no viewer-args, shouldn't we still append the info-filename? likewise,
375     //          even if we do have view-args, shouldn't we check if filename is present?
376   }
377
378   char pndfile [ 1024 ];
379   if ( p -> object_type == pnd_object_type_directory ) {
380     // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
381     strncpy ( pndfile, p -> object_path, 1000 );
382   } else if ( p -> object_type == pnd_object_type_pnd ) {
383     // pnd_run.sh wants the full path and filename for the .pnd file
384     snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
385   }
386
387   pnd_apps_exec_info_t info;
388   info.viewer = viewer;
389   info.args = pargs;
390
391   if ( ! pnd_apps_exec_disco ( pndrun, p, PND_EXEC_OPTION_NORUN | PND_EXEC_OPTION_INFO, &info ) ) {
392     return ( 0 );
393   }
394
395   fprintf ( f, "Exec=%s\n", pnd_apps_exec_runline() );
396
397   if ( pnd_conf_get_as_char ( desktoph, "info.category" ) ) {
398     fprintf ( f, "Categories=%s\n", pnd_conf_get_as_char ( desktoph, "info.category" ) );
399   } else {
400     fprintf ( f, "Categories=Documentation\n" );
401   }
402
403   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
404
405   fclose ( f );
406
407   return ( 1 );
408 }
409
410 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
411   //#define BITLEN (8*1024)
412 #define BITLEN (64*1024)
413   char buffer [ FILENAME_MAX ]; // target filename
414   char from [ FILENAME_MAX ];   // source filename
415   unsigned char bits [ BITLEN ];
416   unsigned int bitlen;
417   FILE *pnd, *target;
418
419   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
420   if ( ( p -> object_type == pnd_object_type_pnd ) &&
421        ( ! p -> pnd_icon_pos ) )
422   {
423     return ( 0 ); // discover code didn't find it, so FAIL
424   }
425
426   // determine filename for target
427   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target
428
429   /* first.. open the source file, by type of application:
430    * are we looking through a pnd file or a dir?
431    */
432   if ( p -> object_type == pnd_object_type_directory ) {
433     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
434   } else if ( p -> object_type == pnd_object_type_pnd ) {
435     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
436   }
437
438   pnd = fopen ( from, "rb" );
439
440   if ( ! pnd ) {
441     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open source\n" );
442     return ( 0 );
443   }
444
445   unsigned int len;
446
447   target = fopen ( buffer, "wb" );
448
449   if ( ! target ) {
450     fclose ( pnd );
451     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open target\n" );
452     return ( 0 );
453   }
454
455   fseek ( pnd, 0, SEEK_END );
456   len = ftell ( pnd );
457   //fseek ( pnd, 0, SEEK_SET );
458
459   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
460
461   len -= p -> pnd_icon_pos;
462
463   pnd_log ( PND_LOG_DEFAULT, "    Emit icon, length: %u\n", len );
464
465   while ( len ) {
466
467     if ( len > (BITLEN) ) {
468       bitlen = (BITLEN);
469     } else {
470       bitlen = len;
471     }
472
473     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
474       fclose ( pnd );
475       fclose ( target );
476       unlink ( buffer );
477       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad read\n" );
478       return ( 0 );
479     }
480
481 #if 0
482     {
483       unsigned int i = 0;
484       char bigbuffer [ 200 * 1024 ] = "\0";
485       char b [ 10 ];
486       pnd_log ( PND_LOG_DEFAULT, "    Read hexdump\n" );
487       while ( i < bitlen ) {
488         sprintf ( b, "%x,", bits [ i ] );
489         strcat ( bigbuffer, b );
490         i++;
491       }
492       pnd_log ( PND_LOG_DEFAULT, bigbuffer );
493     }
494 #endif
495
496     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
497       fclose ( pnd );
498       fclose ( target );
499       unlink ( buffer );
500       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad write\n" );
501       return ( 0 );
502     }
503
504     len -= bitlen;
505     //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, next block, length: %u\n", len );
506   } // while
507
508   fclose ( pnd );
509   fclose ( target );
510
511   //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, done.\n" );
512
513   return ( 1 );
514 }
515
516 #if 1 // we switched direction to freedesktop standard categories
517 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
518 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
519   char *t;
520   char *match;
521
522   // clear target so we can easily append
523   memset ( target_buffer, '\0', len );
524
525   // for each main-cat and sub-cat, including alternates, just append them all together
526   // we'll try mapping them, since the categories file is there, but we'll default to
527   // copying over; this lets the conf file do merging or renaming of cagtegories, which
528   // could still be useful, but we can leave the conf file empty to effect a pure
529   // trusted-PXML-copying
530
531   // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
532   // there are hundreds), and would also mean new ones and peoples custom ones would
533   // flop
534
535   /* attempt primary category chain
536    */
537   #define MAPCAT(field)                                     \
538     if ( ( t = d -> field ) ) {                             \
539       match = pnd_map_dotdesktop_category ( c, t );         \
540       strncat ( target_buffer, match ? match : t, len );    \
541       strncat ( target_buffer, ";", len );                  \
542     }
543
544   MAPCAT(main_category);
545   MAPCAT(main_category1);
546   MAPCAT(main_category2);
547   MAPCAT(alt_category);
548   MAPCAT(alt_category1);
549   MAPCAT(alt_category2);
550
551   if ( target_buffer [ 0 ] ) {
552     return ( 1 ); // I guess its 'good'?
553   }
554
555 #if 0
556   if ( ( t = d -> main_category ) ) {
557     match = pnd_map_dotdesktop_category ( c, t );
558     strncat ( target_buffer, match ? match : t, len );
559     strncat ( target_buffer, ";", len );
560   }
561 #endif
562
563   return ( 0 );
564 }
565 #endif
566
567 #if 0 // we switched direction
568 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
569 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
570   unsigned short int n = 0; // no. matches
571   char *t;
572   char *match;
573
574   // clear target so we can easily append
575   memset ( target_buffer, '\0', len );
576
577   /* attempt primary category chain
578    */
579   match = NULL;
580
581   if ( ( t = d -> main_category ) ) {
582     match = pnd_map_dotdesktop_category ( c, t );
583   }
584
585   if ( ( ! match ) &&
586        ( t = d -> main_category1 ) )
587   {
588     match = pnd_map_dotdesktop_category ( c, t );
589   }
590
591   if ( ( ! match ) &&
592        ( t = d -> main_category2 ) )
593   {
594     match = pnd_map_dotdesktop_category ( c, t );
595   }
596
597   if ( match ) {
598     strncat ( target_buffer, match, len );
599     len -= strlen ( target_buffer );
600     n += 1;
601   }
602
603   /* attempt secondary category chain
604    */
605   match = NULL;
606
607   if ( ( t = d -> alt_category ) ) {
608     match = pnd_map_dotdesktop_category ( c, t );
609   }
610
611   if ( ( ! match ) &&
612        ( t = d -> alt_category1 ) )
613   {
614     match = pnd_map_dotdesktop_category ( c, t );
615   }
616
617   if ( ( ! match ) &&
618        ( t = d -> alt_category2 ) )
619   {
620     match = pnd_map_dotdesktop_category ( c, t );
621   }
622
623   if ( match ) {
624     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
625       strcat ( target_buffer, ";" );
626       len -= 1;
627     }
628     strncat ( target_buffer, match, len );
629     len -= strlen ( target_buffer );
630     n += 1;
631   }
632
633 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
634   match = NULL;
635
636   if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
637     match = pnd_map_dotdesktop_category ( c, t );
638   }
639
640   if ( ( ! match ) &&
641        ( t = pnd_pxml_get_subcategory1 ( h ) ) )
642   {
643     match = pnd_map_dotdesktop_category ( c, t );
644   }
645
646   if ( ( ! match ) &&
647        ( t = pnd_pxml_get_subcategory2 ( h ) ) )
648   {
649     match = pnd_map_dotdesktop_category ( c, t );
650   }
651
652   if ( match ) {
653     strncat ( target_buffer, match, len );
654     len -= strlen ( target_buffer );
655     n += 1;
656   }
657
658   /* attempt secondary category chain
659    */
660   match = NULL;
661
662   if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
663     match = pnd_map_dotdesktop_category ( c, t );
664   }
665
666   if ( ( ! match ) &&
667        ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
668   {
669     match = pnd_map_dotdesktop_category ( c, t );
670   }
671
672   if ( ( ! match ) &&
673        ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
674   {
675     match = pnd_map_dotdesktop_category ( c, t );
676   }
677
678   if ( match ) {
679     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
680       strcat ( target_buffer, ";" );
681       len -= 1;
682     }
683     strncat ( target_buffer, match, len );
684     len -= strlen ( target_buffer );
685     n += 1;
686   }
687 #endif
688
689   if ( n && len ) {
690     strcat ( target_buffer, ";" );
691   }
692
693   return ( n );
694 }
695 #endif
696
697 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
698 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
699   char *key;
700   char *ret;
701
702   key = malloc ( strlen ( single_category ) + 4 + 1 );
703
704   sprintf ( key, "map.%s", single_category );
705
706   ret = pnd_conf_get_as_char ( c, key );
707
708   free ( key );
709
710   return ( ret );
711 }
712
713 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
714   // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
715   // with a fwrite at the end...
716   char from [ FILENAME_MAX ];   // source filename
717   char bits [ 8 * 1024 ];
718   unsigned int bitlen;
719   FILE *pnd = NULL;
720   unsigned char *target = NULL, *targiter = NULL;
721
722   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
723   if ( ( p -> object_type == pnd_object_type_pnd ) &&
724        ( ! p -> pnd_icon_pos ) )
725   {
726     return ( NULL ); // discover code didn't find it, so FAIL
727   }
728
729   /* first.. open the source file, by type of application:
730    * are we looking through a pnd file or a dir?
731    */
732   if ( p -> object_type == pnd_object_type_directory ) {
733     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
734   } else if ( p -> object_type == pnd_object_type_pnd ) {
735     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
736   }
737
738   pnd = fopen ( from, "r" );
739
740   if ( ! pnd ) {
741     return ( NULL );
742   }
743
744   // determine length of file, then adjust by icon position to find begin of icon
745   unsigned int len;
746
747   fseek ( pnd, 0, SEEK_END );
748   len = ftell ( pnd );
749   //fseek ( pnd, 0, SEEK_SET );
750
751   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
752
753   len -= p -> pnd_icon_pos;
754
755   // create target buffer
756   target = malloc ( len );
757
758   if ( ! target ) {
759     fclose ( pnd );
760     return ( 0 );
761   }
762
763   targiter = target;
764
765   if ( r_buflen ) {
766     *r_buflen = len;
767   }
768
769   // copy over icon to target
770   while ( len ) {
771
772     if ( len > (8*1024) ) {
773       bitlen = (8*1024);
774     } else {
775       bitlen = len;
776     }
777
778     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
779       fclose ( pnd );
780       free ( target );
781       return ( NULL );
782     }
783
784     memmove ( targiter, bits, bitlen );
785     targiter += bitlen;
786
787     len -= bitlen;
788   } // while
789
790   fclose ( pnd );
791
792   return ( target );
793 }
794
795 // parse_dotdesktop() can be used to read a libpnd generated .desktop and return a limited
796 // but useful disco-t structure back; possibly useful for scanning .desktops rather than
797 // scanning pnd-files?
798 pnd_disco_t *pnd_parse_dotdesktop ( char *ddpath, unsigned int flags ) {
799
800   // will verify the .desktop has the libpnd-marking on it (X-Pandora-Source): PND_DOTDESKTOP_SOURCE
801
802   // attempt to extract..
803   // - unique-id (from filename or field)
804   // - subapp number (from filename)
805   // - exec required info
806   // - icon path
807   // - name (title-en)
808   // - comment (desc-en)
809   // - option_no_x11
810   // - object path
811   // - appdata name (or unique-id if not present)
812   // - start dir
813   // - args
814   // - clockspeed
815   // - categories
816
817   char pndpath [ 1024 ];
818   bzero ( pndpath, 1024 );
819
820   // filter on filename?
821   if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
822     // too bad we didn't put some libpnd token at the front of the filename or something
823     // hell, we should cleanse unique-id to ensure its not full of special chars like '*' and '..'.. eep!
824     if ( strrchr ( ddpath, '#' ) == NULL ) { // but if requiring libpnd, we can at least check for #subapp-number
825       return ( NULL );
826     }
827   }
828
829   if ( strstr ( ddpath, ".desktop" ) == NULL ) {
830     return ( NULL ); // no .desktop in filename, must be something else... skip!
831   }
832
833   if ( strstr ( ddpath, "info.desktop" ) != NULL ) {
834     // ".....info.desktop" is the 'document help' (README) emitted from a pnd, not an actual app; minimenu rather
835     // expects the doc-info as part of the main app, not a separate app.. so lets drop it here, to avoid doubling up
836     // the number of applications, needlessly..
837     return ( NULL );
838   }
839
840   // determine file length
841   struct stat statbuf;
842
843   if ( stat ( ddpath, &statbuf) < 0 ) {
844     return ( NULL ); // couldn't open
845   }
846
847   // buffers..
848   char dd [ 1024 ];
849   unsigned char libpnd_origin = 0;
850
851   // disco
852   pnd_disco_t *p = malloc ( sizeof(pnd_disco_t) );
853   if ( ! p ) {
854     return ( NULL );
855   }
856   bzero ( p, sizeof(pnd_disco_t) );
857
858   // inhale file
859   FILE *f = fopen ( ddpath, "r" );
860
861   if ( ! f ) {
862     return ( NULL ); // not up or shut up!
863   }
864
865   while ( fgets ( dd, 1024, f ) ) {
866     char *nl = strchr ( dd, '\n' );
867     if ( nl ) {
868       *nl = '\0';
869     }
870
871     // grep
872     //
873
874     if ( strncmp ( dd, "Name=", 5 ) == 0 ) {
875       p -> title_en = strdup ( dd + 5 );
876     } else if ( strncmp ( dd, "Name[en]=", 9 ) == 0 ) {
877       p -> title_en = strdup ( dd + 9 );
878     } else if ( strncmp ( dd, "Icon=", 5 ) == 0 ) {
879       p -> icon = strdup ( dd + 5 );
880     } else if ( strcmp ( dd, PND_DOTDESKTOP_SOURCE ) == 0 ) {
881       libpnd_origin = 1;
882     } else if ( strncmp ( dd, "X-Pandora-UID=", 14 ) == 0 ) {
883       p -> unique_id = strdup ( dd + 14 );
884     } else if ( strncmp ( dd, "X-Pandora-Preview-Pic-1=", 24 ) == 0 ) {
885       p -> preview_pic1 = strdup ( dd + 24 );
886     } else if ( strncmp ( dd, "X-Pandora-Clockspeed=", 21 ) == 0 ) {
887       p -> clockspeed = strdup ( dd + 21 );
888     } else if ( strncmp ( dd, "X-Pandora-Startdir=", 19 ) == 0 ) {
889       p -> startdir = strdup ( dd + 19 );
890     } else if ( strncmp ( dd, "X-Pandora-Appdata-Dirname=", 26 ) == 0 ) {
891       p -> appdata_dirname = strdup ( dd + 26 );
892     } else if ( strncmp ( dd, "X-Pandora-ExecArgs=", 19 ) == 0 ) {
893       p -> execargs = strdup ( dd + 19 );
894     } else if ( strncmp ( dd, "X-Pandora-Exec=", 15 ) == 0 ) {
895       p -> exec = strdup ( dd + 15 );
896     } else if ( strncmp ( dd, "X-Pandora-Object-Path=", 22 ) == 0 ) {
897       p -> object_path = strdup ( dd + 22 );
898     } else if ( strncmp ( dd, "X-Pandora-Object-Filename=", 26 ) == 0 ) {
899       p -> object_filename = strdup ( dd + 26 );
900     } else if ( strncmp ( dd, "X-Pandora-Object-Flag-OVR=", 26 ) == 0 ) {
901       p -> object_flags |= PND_DISCO_FLAG_OVR;
902
903     } else if ( strncmp ( dd, "X-Pandora-MainCategory=", 23 ) == 0 ) {
904       p -> main_category = strdup ( dd + 23 );
905     } else if ( strncmp ( dd, "X-Pandora-MainCategory1=", 24 ) == 0 ) {
906       p -> main_category1 = strdup ( dd + 24 );
907     } else if ( strncmp ( dd, "X-Pandora-MainCategory2=", 24 ) == 0 ) {
908       p -> main_category2 = strdup ( dd + 24 );
909
910     } else if ( strncmp ( dd, "X-Pandora-AltCategory=", 22 ) == 0 ) {
911       p -> alt_category = strdup ( dd + 22 );
912     } else if ( strncmp ( dd, "X-Pandora-AltCategory1=", 23 ) == 0 ) {
913       p -> alt_category1 = strdup ( dd + 23 );
914     } else if ( strncmp ( dd, "X-Pandora-AltCategory2=", 23 ) == 0 ) {
915       p -> alt_category2 = strdup ( dd + 23 );
916
917     } else if ( strncmp ( dd, "X-Pandora-Info-Filename=", 24 ) == 0 ) {
918       p -> info_filename = strdup ( dd + 24 );
919     } else if ( strncmp ( dd, "X-Pandora-Info-Name=", 20 ) == 0 ) {
920       p -> info_name = strdup ( dd + 20 );
921
922     } else if ( strncmp ( dd, "Comment=", 8 ) == 0 ) {
923       p -> desc_en = strdup ( dd + 8 );
924     } else if ( strncmp ( dd, "Comment[en]=", 12 ) == 0 ) {
925       p -> desc_en = strdup ( dd + 12 );
926     } else if ( strncmp ( dd, "Exec=", 5 ) == 0 ) {
927
928       char *e = strstr ( dd, " -e " );
929
930       if ( e ) {
931         // probably libpnd app
932 #if 0 // no needed due to above X-Pandora attributes
933
934         if ( e ) {
935           e += 5;
936
937           char *space = strchr ( e, ' ' );
938           p -> exec = strndup ( e, space - e - 1 );
939         }
940
941         char *b = strstr ( dd, " -b " );
942         if ( b ) {
943           b += 5;
944           char *space = strchr ( b, '\0' );
945           p -> appdata_dirname = strndup ( b, space - b - 1 );
946         }
947
948         char *p = strstr ( dd, " -p " );
949         if ( p ) {
950           p += 5;
951           char *space = strchr ( p, ' ' );
952           strncpy ( pndpath, p, space - p - 1 );
953         }
954 #endif
955
956       } else {
957         // probably not libpnd app
958         p -> exec = strdup ( dd + 5 );
959       }
960
961 #if 0 // ignore; using X- categories now
962     } else if ( strncmp ( dd, "Categories=", 11 ) == 0 ) {
963       // HACK; only honours first category
964       char *semi = strchr ( dd, ';' );
965       if ( semi ) {
966         p -> main_category = strndup ( dd + 11, semi - dd + 11 );
967       } else {
968         p -> main_category = strdup ( dd + 11 );
969       }
970       semi = strchr ( p -> main_category, ';' );
971       if ( semi ) {
972         *semi = '\0';
973       }
974 #endif
975
976     }
977
978     //
979     // /grep
980
981   } // while
982
983   fclose ( f );
984
985   // filter
986   if ( ! libpnd_origin ) {
987
988     // convenience flag
989     if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
990       pnd_disco_destroy ( p );
991       free ( p );
992       return ( NULL );
993     }
994
995   } else {
996     p -> object_flags |= PND_DISCO_LIBPND_DD; // so caller can do something if it wishes
997   }
998
999   // filter on content
1000   if ( ( ! p -> title_en ) ||
1001        ( ! p -> exec )
1002      )
1003   {
1004     pnd_disco_destroy ( p );
1005     free ( p );
1006     return ( NULL );
1007   }
1008
1009   if ( ! p -> unique_id ) {
1010     if ( flags & PND_DOTDESKTOP_LIBPND_ONLY ) {
1011       pnd_disco_destroy ( p );
1012       free ( p );
1013       return ( NULL );
1014     } else {
1015       char hack [ 100 ];
1016       snprintf ( hack, 100, "inode-%lu", statbuf.st_ino );
1017       p -> unique_id = strdup ( hack );
1018     }
1019   }
1020
1021   // additional
1022   p -> object_type = pnd_object_type_pnd;
1023
1024 #if 0 // nolonger needed due to above X-Pandora attributes
1025   char *source;
1026   if ( pndpath [ 0 ] ) {
1027     source = pndpath;
1028   } else {
1029     source = ddpath;
1030   }
1031
1032   char *slash = strrchr ( source, '/' );
1033   if ( slash ) {
1034     p -> object_path = strndup ( source, slash - source );
1035     p -> object_filename = strdup ( slash + 1 );
1036   } else {
1037     p -> object_path = "./";
1038     p -> object_filename = strdup ( source );
1039   }
1040 #endif
1041
1042   // lame guards, in case of lazy consumers and broken .desktop files
1043   if ( p -> object_path == NULL ) {
1044     p -> object_path = strdup ( "/tmp" );
1045   }
1046   if ( p -> object_filename == NULL ) {
1047     p -> object_filename = strdup ( "" ); // force bad filename
1048   }
1049
1050   // return disco-t
1051   return ( p );
1052 }