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