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