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