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