Merge branch 'master' of ssh://skeezixgit@git.openpandora.org/srv/git/pandora-libraries
[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
7 #include "pnd_apps.h"
8 #include "pnd_container.h"
9 #include "pnd_pxml.h"
10 #include "pnd_discovery.h"
11 #include "pnd_pndfiles.h"
12 #include "pnd_conf.h"
13 #include "pnd_desktop.h"
14 #include "pnd_logger.h"
15
16 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
17   char filename [ FILENAME_MAX ];
18   char buffer [ 1024 ];
19   FILE *f;
20
21   // specification
22   // http://standards.freedesktop.org/desktop-entry-spec
23
24   // validation
25
26   if ( ! p -> unique_id ) {
27     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
28     return ( 0 );
29   }
30
31   if ( ! p -> exec ) {
32     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing exec\n", targetpath );
33     return ( 0 );
34   }
35
36   if ( ! targetpath ) {
37     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
38     return ( 0 );
39   }
40
41   if ( ! pndrun ) {
42     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
43     return ( 0 );
44   }
45
46   // set up
47
48   sprintf ( filename, "%s/%s#%u.desktop", targetpath, p -> unique_id, p -> subapp_number );
49
50   // emit
51
52   //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
53
54   f = fopen ( filename, "w" );
55
56   if ( ! f ) {
57     return ( 0 );
58   }
59
60   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
61
62   if ( p -> title_en ) {
63     snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
64     fprintf ( f, "%s", buffer );
65   }
66
67   fprintf ( f, "Type=Application\n" );
68   fprintf ( f, "Version=1.0\n" );
69
70   if ( p -> icon ) {
71     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
72     fprintf ( f, "%s", buffer );
73   }
74
75   if ( p -> unique_id ) {
76     fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
77   }
78
79 #if 1
80   if ( p -> desc_en && p -> desc_en [ 0 ] ) {
81     snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
82     fprintf ( f, "%s", buffer );
83   }
84 #endif
85
86 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
87   if ( p -> startdir ) {
88     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
89     fprintf ( f, "%s", buffer );
90   } else {
91     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
92   }
93 #endif
94
95   if ( p -> exec ) {
96     char *nohup;
97
98     if ( p -> option_no_x11 ) {
99       nohup = "/usr/bin/nohup ";
100     } else {
101       nohup = "";
102     }
103
104     // basics
105     if ( p -> object_type == pnd_object_type_directory ) {
106       snprintf ( buffer, 1020, "Exec=%s%s -p %s -e %s -b %s",
107                  nohup, pndrun, p -> object_path, p -> exec, p -> unique_id );
108     } else if ( p -> object_type == pnd_object_type_pnd ) {
109       snprintf ( buffer, 1020, "Exec=%s%s -p %s/%s -e %s -b %s",
110                  nohup, pndrun, p -> object_path, p -> object_filename, p -> exec, p -> unique_id );
111     }
112
113     // start dir
114     if ( p -> startdir ) {
115       strncat ( buffer, " -s ", 1020 );
116       strncat ( buffer, p -> startdir, 1020 );
117     }
118
119     // args
120     if ( p -> execargs ) {
121       char argbuf [ 1024 ];
122       snprintf ( argbuf, 1000, " -a \"%s\"", p -> execargs );
123       strncat ( buffer, argbuf, 1020 );
124     }
125
126     // clockspeed
127     if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
128       strncat ( buffer, " -c ", 1020 );
129       strncat ( buffer, p -> clockspeed, 1020 );
130     }
131
132     // exec options
133     if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
134       strncat ( buffer, " -x ", 1020 );
135     }
136
137     // newline
138     strncat ( buffer, "\n", 1020 );
139
140     // emit
141     fprintf ( f, "%s", buffer );
142   }
143
144   // categories
145   {
146     char cats [ 512 ] = "";
147     int n;
148     pnd_conf_handle c;
149     char *confpath;
150
151     // uuuuh, defaults?
152     // "Application" used to be in the standard and is commonly supported still
153     // Utility and Network should ensure the app is visible 'somewhere' :/
154     char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
155
156     // determine searchpath (for conf, not for apps)
157     confpath = pnd_conf_query_searchpath();
158
159     // inhale the conf file
160     c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
161
162     // if we can find a default category set, pull it in; otherwise assume
163     // the hardcoded one
164     if ( pnd_conf_get_as_char ( c, "default" ) ) {
165       defaults = pnd_conf_get_as_char ( c, "default" );
166     }
167
168     // ditch the confpath
169     free ( confpath );
170
171     // attempt mapping
172     if ( c ) {
173
174       n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
175
176       if ( n ) {
177         fprintf ( f, "Categories=%s\n", cats );
178       } else {
179         fprintf ( f, "Categories=%s\n", defaults );
180       }
181
182     } else {
183       fprintf ( f, "Categories=%s\n", defaults );
184     }
185
186   }
187
188   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
189
190   fclose ( f );
191
192   return ( 1 );
193 }
194
195 unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
196   char filename [ FILENAME_MAX ];
197   char buffer [ 1024 ];
198   FILE *f;
199   char *viewer, *searchpath;
200   pnd_conf_handle desktoph;
201
202   // specification
203   // http://standards.freedesktop.org/desktop-entry-spec
204
205   // validation
206   //
207
208   // viewer
209   searchpath = pnd_conf_query_searchpath();
210
211   desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
212
213   if ( ! desktoph ) {
214     return ( 0 );
215   }
216
217   viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
218
219   if ( ! viewer ) {
220     return ( 0 ); // no way to view the file
221   }
222
223   // etc
224   if ( ! p -> unique_id ) {
225     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
226     return ( 0 );
227   }
228
229   if ( ! p -> info_filename ) {
230     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_filename\n", targetpath );
231     return ( 0 );
232   }
233
234   if ( ! p -> info_name ) {
235     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_name\n", targetpath );
236     return ( 0 );
237   }
238
239   if ( ! targetpath ) {
240     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
241     return ( 0 );
242   }
243
244   if ( ! pndrun ) {
245     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
246     return ( 0 );
247   }
248
249   // set up
250
251   sprintf ( filename, "%s/%s#info.desktop", targetpath, p -> unique_id );
252
253   // emit
254
255   f = fopen ( filename, "w" );
256
257   if ( ! f ) {
258     return ( 0 );
259   }
260
261   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
262
263   if ( p -> info_name ) {
264     snprintf ( buffer, 1020, "Name=%s\n", p -> info_name );
265     fprintf ( f, "%s", buffer );
266   }
267
268   fprintf ( f, "Type=Application\n" );
269   fprintf ( f, "Version=1.0\n" );
270
271 #if 0
272   if ( p -> icon ) {
273     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
274     fprintf ( f, "%s", buffer );
275   }
276 #endif
277
278   if ( p -> unique_id ) {
279     fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
280   }
281
282   if ( p -> title_en && p -> title_en [ 0 ] ) {
283     snprintf ( buffer, 1020, "Comment=Automatic menu info entry for %s\n", p -> title_en );
284     fprintf ( f, "%s", buffer );
285   }
286
287 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
288   if ( p -> startdir ) {
289     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
290     fprintf ( f, "%s", buffer );
291   } else {
292     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
293   }
294 #endif
295
296   // exec line
297   char args [ 1001 ];
298   char *pargs = args;
299   if ( pnd_conf_get_as_char ( desktoph, "info.viewer_args" ) ) {
300     snprintf ( pargs, 1001, "%s %s",
301                pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
302   } else {
303     pargs = NULL;
304   }
305
306   char pndfile [ 1024 ];
307   if ( p -> object_type == pnd_object_type_directory ) {
308     // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
309     strncpy ( pndfile, p -> object_path, 1000 );
310   } else if ( p -> object_type == pnd_object_type_pnd ) {
311     // pnd_run.sh wants the full path and filename for the .pnd file
312     snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
313   }
314
315   if ( ! pnd_apps_exec ( pndrun, pndfile, p -> unique_id, viewer, p -> startdir, pargs,
316                          p -> clockspeed ? atoi ( p -> clockspeed ) : 0, PND_EXEC_OPTION_NORUN ) )
317   {
318     return ( 0 );
319   }
320
321   fprintf ( f, "Exec=%s\n", pnd_apps_exec_runline() );
322
323   if ( pnd_conf_get_as_char ( desktoph, "info.category" ) ) {
324     fprintf ( f, "Categories=%s\n", pnd_conf_get_as_char ( desktoph, "info.category" ) );
325   } else {
326     fprintf ( f, "Categories=Documentation\n" );
327   }
328
329   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
330
331   fclose ( f );
332
333   return ( 1 );
334 }
335
336 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
337   //#define BITLEN (8*1024)
338 #define BITLEN (64*1024)
339   char buffer [ FILENAME_MAX ]; // target filename
340   char from [ FILENAME_MAX ];   // source filename
341   unsigned char bits [ BITLEN ];
342   unsigned int bitlen;
343   FILE *pnd, *target;
344
345   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
346   if ( ( p -> object_type == pnd_object_type_pnd ) &&
347        ( ! p -> pnd_icon_pos ) )
348   {
349     return ( 0 ); // discover code didn't find it, so FAIL
350   }
351
352   // determine filename for target
353   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target
354
355   /* first.. open the source file, by type of application:
356    * are we looking through a pnd file or a dir?
357    */
358   if ( p -> object_type == pnd_object_type_directory ) {
359     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
360   } else if ( p -> object_type == pnd_object_type_pnd ) {
361     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
362   }
363
364   pnd = fopen ( from, "rb" );
365
366   if ( ! pnd ) {
367     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open source\n" );
368     return ( 0 );
369   }
370
371   unsigned int len;
372
373   target = fopen ( buffer, "wb" );
374
375   if ( ! target ) {
376     fclose ( pnd );
377     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open target\n" );
378     return ( 0 );
379   }
380
381   fseek ( pnd, 0, SEEK_END );
382   len = ftell ( pnd );
383   //fseek ( pnd, 0, SEEK_SET );
384
385   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
386
387   len -= p -> pnd_icon_pos;
388
389   pnd_log ( PND_LOG_DEFAULT, "    Emit icon, length: %u\n", len );
390
391   while ( len ) {
392
393     if ( len > (BITLEN) ) {
394       bitlen = (BITLEN);
395     } else {
396       bitlen = len;
397     }
398
399     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
400       fclose ( pnd );
401       fclose ( target );
402       unlink ( buffer );
403       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad read\n" );
404       return ( 0 );
405     }
406
407 #if 0
408     {
409       unsigned int i = 0;
410       char bigbuffer [ 200 * 1024 ] = "\0";
411       char b [ 10 ];
412       pnd_log ( PND_LOG_DEFAULT, "    Read hexdump\n" );
413       while ( i < bitlen ) {
414         sprintf ( b, "%x,", bits [ i ] );
415         strcat ( bigbuffer, b );
416         i++;
417       }
418       pnd_log ( PND_LOG_DEFAULT, bigbuffer );
419     }
420 #endif
421
422     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
423       fclose ( pnd );
424       fclose ( target );
425       unlink ( buffer );
426       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad write\n" );
427       return ( 0 );
428     }
429
430     len -= bitlen;
431     //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, next block, length: %u\n", len );
432   } // while
433
434   fclose ( pnd );
435   fclose ( target );
436
437   //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, done.\n" );
438
439   return ( 1 );
440 }
441
442 #if 1 // we switched direction to freedesktop standard categories
443 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
444 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
445   char *t;
446   char *match;
447
448   // clear target so we can easily append
449   memset ( target_buffer, '\0', len );
450
451   // for each main-cat and sub-cat, including alternates, just append them all together
452   // we'll try mapping them, since the categories file is there, but we'll default to
453   // copying over; this lets the conf file do merging or renaming of cagtegories, which
454   // could still be useful, but we can leave the conf file empty to effect a pure
455   // trusted-PXML-copying
456
457   // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
458   // there are hundreds), and would also mean new ones and peoples custom ones would
459   // flop
460
461   /* attempt primary category chain
462    */
463   #define MAPCAT(field)                                     \
464     if ( ( t = d -> field ) ) {                             \
465       match = pnd_map_dotdesktop_category ( c, t );         \
466       strncat ( target_buffer, match ? match : t, len );    \
467       strncat ( target_buffer, ";", len );                  \
468     }
469
470   MAPCAT(main_category);
471   MAPCAT(main_category1);
472   MAPCAT(main_category2);
473   MAPCAT(alt_category);
474   MAPCAT(alt_category1);
475   MAPCAT(alt_category2);
476
477   if ( target_buffer [ 0 ] ) {
478     return ( 1 ); // I guess its 'good'?
479   }
480
481 #if 0
482   if ( ( t = d -> main_category ) ) {
483     match = pnd_map_dotdesktop_category ( c, t );
484     strncat ( target_buffer, match ? match : t, len );
485     strncat ( target_buffer, ";", len );
486   }
487 #endif
488
489   return ( 0 );
490 }
491 #endif
492
493 #if 0 // we switched direction
494 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
495 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
496   unsigned short int n = 0; // no. matches
497   char *t;
498   char *match;
499
500   // clear target so we can easily append
501   memset ( target_buffer, '\0', len );
502
503   /* attempt primary category chain
504    */
505   match = NULL;
506
507   if ( ( t = d -> main_category ) ) {
508     match = pnd_map_dotdesktop_category ( c, t );
509   }
510
511   if ( ( ! match ) &&
512        ( t = d -> main_category1 ) )
513   {
514     match = pnd_map_dotdesktop_category ( c, t );
515   }
516
517   if ( ( ! match ) &&
518        ( t = d -> main_category2 ) )
519   {
520     match = pnd_map_dotdesktop_category ( c, t );
521   }
522   
523   if ( match ) {
524     strncat ( target_buffer, match, len );
525     len -= strlen ( target_buffer );
526     n += 1;
527   }
528
529   /* attempt secondary category chain
530    */
531   match = NULL;
532
533   if ( ( t = d -> alt_category ) ) {
534     match = pnd_map_dotdesktop_category ( c, t );
535   }
536
537   if ( ( ! match ) &&
538        ( t = d -> alt_category1 ) )
539   {
540     match = pnd_map_dotdesktop_category ( c, t );
541   }
542
543   if ( ( ! match ) &&
544        ( t = d -> alt_category2 ) )
545   {
546     match = pnd_map_dotdesktop_category ( c, t );
547   }
548   
549   if ( match ) {
550     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
551       strcat ( target_buffer, ";" );
552       len -= 1;
553     }
554     strncat ( target_buffer, match, len );
555     len -= strlen ( target_buffer );
556     n += 1;
557   }
558
559 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
560   match = NULL;
561
562   if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
563     match = pnd_map_dotdesktop_category ( c, t );
564   }
565
566   if ( ( ! match ) &&
567        ( t = pnd_pxml_get_subcategory1 ( h ) ) )
568   {
569     match = pnd_map_dotdesktop_category ( c, t );
570   }
571
572   if ( ( ! match ) &&
573        ( t = pnd_pxml_get_subcategory2 ( h ) ) )
574   {
575     match = pnd_map_dotdesktop_category ( c, t );
576   }
577   
578   if ( match ) {
579     strncat ( target_buffer, match, len );
580     len -= strlen ( target_buffer );
581     n += 1;
582   }
583
584   /* attempt secondary category chain
585    */
586   match = NULL;
587
588   if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
589     match = pnd_map_dotdesktop_category ( c, t );
590   }
591
592   if ( ( ! match ) &&
593        ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
594   {
595     match = pnd_map_dotdesktop_category ( c, t );
596   }
597
598   if ( ( ! match ) &&
599        ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
600   {
601     match = pnd_map_dotdesktop_category ( c, t );
602   }
603   
604   if ( match ) {
605     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
606       strcat ( target_buffer, ";" );
607       len -= 1;
608     }
609     strncat ( target_buffer, match, len );
610     len -= strlen ( target_buffer );
611     n += 1;
612   }
613 #endif
614
615   if ( n && len ) {
616     strcat ( target_buffer, ";" );
617   }
618
619   return ( n );
620 }
621 #endif
622
623 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
624 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
625   char *key;
626   char *ret;
627
628   key = malloc ( strlen ( single_category ) + 4 + 1 );
629
630   sprintf ( key, "map.%s", single_category );
631
632   ret = pnd_conf_get_as_char ( c, key );
633
634   free ( key );
635
636   return ( ret );
637 }
638
639 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
640   // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
641   // with a fwrite at the end...
642   char from [ FILENAME_MAX ];   // source filename
643   char bits [ 8 * 1024 ];
644   unsigned int bitlen;
645   FILE *pnd = NULL;
646   unsigned char *target = NULL, *targiter = NULL;
647
648   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
649   if ( ( p -> object_type == pnd_object_type_pnd ) &&
650        ( ! p -> pnd_icon_pos ) )
651   {
652     return ( NULL ); // discover code didn't find it, so FAIL
653   }
654
655   /* first.. open the source file, by type of application:
656    * are we looking through a pnd file or a dir?
657    */
658   if ( p -> object_type == pnd_object_type_directory ) {
659     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
660   } else if ( p -> object_type == pnd_object_type_pnd ) {
661     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
662   }
663
664   pnd = fopen ( from, "r" );
665
666   if ( ! pnd ) {
667     return ( NULL );
668   }
669
670   // determine length of file, then adjust by icon position to find begin of icon
671   unsigned int len;
672
673   fseek ( pnd, 0, SEEK_END );
674   len = ftell ( pnd );
675   //fseek ( pnd, 0, SEEK_SET );
676
677   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
678
679   len -= p -> pnd_icon_pos;
680
681   // create target buffer
682   target = malloc ( len );
683
684   if ( ! target ) {
685     fclose ( pnd );
686     return ( 0 );
687   }
688
689   targiter = target;
690
691   if ( r_buflen ) {
692     *r_buflen = len;
693   }
694
695   // copy over icon to target
696   while ( len ) {
697
698     if ( len > (8*1024) ) {
699       bitlen = (8*1024);
700     } else {
701       bitlen = len;
702     }
703
704     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
705       fclose ( pnd );
706       free ( target );
707       return ( NULL );
708     }
709
710     memmove ( targiter, bits, bitlen );
711     targiter += bitlen;
712
713     len -= bitlen;
714   } // while
715
716   fclose ( pnd );
717
718   return ( target );
719 }