Merge branch 'master' of git://git.openpandora.org/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 handle this
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       strncat ( buffer, " -a ", 1020 );
122       strncat ( buffer, p -> execargs, 1020 );
123     }
124
125     // clockspeed
126     if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
127       strncat ( buffer, " -c ", 1020 );
128       strncat ( buffer, p -> clockspeed, 1020 );
129     }
130
131     // exec options
132     if ( pnd_pxml_is_affirmative ( p -> option_no_x11 ) ) {
133       strncat ( buffer, " -x ", 1020 );
134     }
135
136     // newline
137     strncat ( buffer, "\n", 1020 );
138
139     // emit
140     fprintf ( f, "%s", buffer );
141   }
142
143   // categories
144   {
145     char cats [ 512 ] = "";
146     int n;
147     pnd_conf_handle c;
148     char *confpath;
149
150     // uuuuh, defaults?
151     // "Application" used to be in the standard and is commonly supported still
152     // Utility and Network should ensure the app is visible 'somewhere' :/
153     char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
154
155     // determine searchpath (for conf, not for apps)
156     confpath = pnd_conf_query_searchpath();
157
158     // inhale the conf file
159     c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
160
161     // if we can find a default category set, pull it in; otherwise assume
162     // the hardcoded one
163     if ( pnd_conf_get_as_char ( c, "default" ) ) {
164       defaults = pnd_conf_get_as_char ( c, "default" );
165     }
166
167     // ditch the confpath
168     free ( confpath );
169
170     // attempt mapping
171     if ( c ) {
172
173       n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
174
175       if ( n ) {
176         fprintf ( f, "Categories=%s\n", cats );
177       } else {
178         fprintf ( f, "Categories=%s\n", defaults );
179       }
180
181     } else {
182       fprintf ( f, "Categories=%s\n", defaults );
183     }
184
185   }
186
187   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
188
189   fclose ( f );
190
191   return ( 1 );
192 }
193
194 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
195   //#define BITLEN (8*1024)
196 #define BITLEN (64*1024)
197   char buffer [ FILENAME_MAX ]; // target filename
198   char from [ FILENAME_MAX ];   // source filename
199   unsigned char bits [ BITLEN ];
200   unsigned int bitlen;
201   FILE *pnd, *target;
202
203   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
204   if ( ( p -> object_type == pnd_object_type_pnd ) &&
205        ( ! p -> pnd_icon_pos ) )
206   {
207     return ( 0 ); // discover code didn't find it, so FAIL
208   }
209
210   // determine filename for target
211   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target
212
213   /* first.. open the source file, by type of application:
214    * are we looking through a pnd file or a dir?
215    */
216   if ( p -> object_type == pnd_object_type_directory ) {
217     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
218   } else if ( p -> object_type == pnd_object_type_pnd ) {
219     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
220   }
221
222   pnd = fopen ( from, "rb" );
223
224   if ( ! pnd ) {
225     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open source\n" );
226     return ( 0 );
227   }
228
229   unsigned int len;
230
231   target = fopen ( buffer, "wb" );
232
233   if ( ! target ) {
234     fclose ( pnd );
235     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open target\n" );
236     return ( 0 );
237   }
238
239   fseek ( pnd, 0, SEEK_END );
240   len = ftell ( pnd );
241   //fseek ( pnd, 0, SEEK_SET );
242
243   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
244
245   len -= p -> pnd_icon_pos;
246
247   pnd_log ( PND_LOG_DEFAULT, "    Emit icon, length: %u\n", len );
248
249   while ( len ) {
250
251     if ( len > (BITLEN) ) {
252       bitlen = (BITLEN);
253     } else {
254       bitlen = len;
255     }
256
257     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
258       fclose ( pnd );
259       fclose ( target );
260       unlink ( buffer );
261       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad read\n" );
262       return ( 0 );
263     }
264
265 #if 0
266     {
267       unsigned int i = 0;
268       char bigbuffer [ 200 * 1024 ] = "\0";
269       char b [ 10 ];
270       pnd_log ( PND_LOG_DEFAULT, "    Read hexdump\n" );
271       while ( i < bitlen ) {
272         sprintf ( b, "%x,", bits [ i ] );
273         strcat ( bigbuffer, b );
274         i++;
275       }
276       pnd_log ( PND_LOG_DEFAULT, bigbuffer );
277     }
278 #endif
279
280     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
281       fclose ( pnd );
282       fclose ( target );
283       unlink ( buffer );
284       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad write\n" );
285       return ( 0 );
286     }
287
288     len -= bitlen;
289     //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, next block, length: %u\n", len );
290   } // while
291
292   fclose ( pnd );
293   fclose ( target );
294
295   //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, done.\n" );
296
297   return ( 1 );
298 }
299
300 #if 1 // we switched direction to freedesktop standard categories
301 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
302 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
303   char *t;
304   char *match;
305
306   // clear target so we can easily append
307   memset ( target_buffer, '\0', len );
308
309   // for each main-cat and sub-cat, including alternates, just append them all together
310   // we'll try mapping them, since the categories file is there, but we'll default to
311   // copying over; this lets the conf file do merging or renaming of cagtegories, which
312   // could still be useful, but we can leave the conf file empty to effect a pure
313   // trusted-PXML-copying
314
315   // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
316   // there are hundreds), and would also mean new ones and peoples custom ones would
317   // flop
318
319   /* attempt primary category chain
320    */
321   #define MAPCAT(field)                                     \
322     if ( ( t = d -> field ) ) {                             \
323       match = pnd_map_dotdesktop_category ( c, t );         \
324       strncat ( target_buffer, match ? match : t, len );    \
325       strncat ( target_buffer, ";", len );                  \
326     }
327
328   MAPCAT(main_category);
329   MAPCAT(main_category1);
330   MAPCAT(main_category2);
331   MAPCAT(alt_category);
332   MAPCAT(alt_category1);
333   MAPCAT(alt_category2);
334
335   if ( target_buffer [ 0 ] ) {
336     return ( 1 ); // I guess its 'good'?
337   }
338
339 #if 0
340   if ( ( t = d -> main_category ) ) {
341     match = pnd_map_dotdesktop_category ( c, t );
342     strncat ( target_buffer, match ? match : t, len );
343     strncat ( target_buffer, ";", len );
344   }
345 #endif
346
347   return ( 0 );
348 }
349 #endif
350
351 #if 0 // we switched direction
352 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
353 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
354   unsigned short int n = 0; // no. matches
355   char *t;
356   char *match;
357
358   // clear target so we can easily append
359   memset ( target_buffer, '\0', len );
360
361   /* attempt primary category chain
362    */
363   match = NULL;
364
365   if ( ( t = d -> main_category ) ) {
366     match = pnd_map_dotdesktop_category ( c, t );
367   }
368
369   if ( ( ! match ) &&
370        ( t = d -> main_category1 ) )
371   {
372     match = pnd_map_dotdesktop_category ( c, t );
373   }
374
375   if ( ( ! match ) &&
376        ( t = d -> main_category2 ) )
377   {
378     match = pnd_map_dotdesktop_category ( c, t );
379   }
380   
381   if ( match ) {
382     strncat ( target_buffer, match, len );
383     len -= strlen ( target_buffer );
384     n += 1;
385   }
386
387   /* attempt secondary category chain
388    */
389   match = NULL;
390
391   if ( ( t = d -> alt_category ) ) {
392     match = pnd_map_dotdesktop_category ( c, t );
393   }
394
395   if ( ( ! match ) &&
396        ( t = d -> alt_category1 ) )
397   {
398     match = pnd_map_dotdesktop_category ( c, t );
399   }
400
401   if ( ( ! match ) &&
402        ( t = d -> alt_category2 ) )
403   {
404     match = pnd_map_dotdesktop_category ( c, t );
405   }
406   
407   if ( match ) {
408     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
409       strcat ( target_buffer, ";" );
410       len -= 1;
411     }
412     strncat ( target_buffer, match, len );
413     len -= strlen ( target_buffer );
414     n += 1;
415   }
416
417 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
418   match = NULL;
419
420   if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
421     match = pnd_map_dotdesktop_category ( c, t );
422   }
423
424   if ( ( ! match ) &&
425        ( t = pnd_pxml_get_subcategory1 ( h ) ) )
426   {
427     match = pnd_map_dotdesktop_category ( c, t );
428   }
429
430   if ( ( ! match ) &&
431        ( t = pnd_pxml_get_subcategory2 ( h ) ) )
432   {
433     match = pnd_map_dotdesktop_category ( c, t );
434   }
435   
436   if ( match ) {
437     strncat ( target_buffer, match, len );
438     len -= strlen ( target_buffer );
439     n += 1;
440   }
441
442   /* attempt secondary category chain
443    */
444   match = NULL;
445
446   if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
447     match = pnd_map_dotdesktop_category ( c, t );
448   }
449
450   if ( ( ! match ) &&
451        ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
452   {
453     match = pnd_map_dotdesktop_category ( c, t );
454   }
455
456   if ( ( ! match ) &&
457        ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
458   {
459     match = pnd_map_dotdesktop_category ( c, t );
460   }
461   
462   if ( match ) {
463     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
464       strcat ( target_buffer, ";" );
465       len -= 1;
466     }
467     strncat ( target_buffer, match, len );
468     len -= strlen ( target_buffer );
469     n += 1;
470   }
471 #endif
472
473   if ( n && len ) {
474     strcat ( target_buffer, ";" );
475   }
476
477   return ( n );
478 }
479 #endif
480
481 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
482 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
483   char *key;
484   char *ret;
485
486   key = malloc ( strlen ( single_category ) + 4 + 1 );
487
488   sprintf ( key, "map.%s", single_category );
489
490   ret = pnd_conf_get_as_char ( c, key );
491
492   free ( key );
493
494   return ( ret );
495 }
496
497 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
498   // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
499   // with a fwrite at the end...
500   char from [ FILENAME_MAX ];   // source filename
501   char bits [ 8 * 1024 ];
502   unsigned int bitlen;
503   FILE *pnd = NULL;
504   unsigned char *target = NULL, *targiter = NULL;
505
506   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
507   if ( ( p -> object_type == pnd_object_type_pnd ) &&
508        ( ! p -> pnd_icon_pos ) )
509   {
510     return ( NULL ); // discover code didn't find it, so FAIL
511   }
512
513   /* first.. open the source file, by type of application:
514    * are we looking through a pnd file or a dir?
515    */
516   if ( p -> object_type == pnd_object_type_directory ) {
517     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
518   } else if ( p -> object_type == pnd_object_type_pnd ) {
519     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
520   }
521
522   pnd = fopen ( from, "r" );
523
524   if ( ! pnd ) {
525     return ( NULL );
526   }
527
528   // determine length of file, then adjust by icon position to find begin of icon
529   unsigned int len;
530
531   fseek ( pnd, 0, SEEK_END );
532   len = ftell ( pnd );
533   //fseek ( pnd, 0, SEEK_SET );
534
535   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
536
537   len -= p -> pnd_icon_pos;
538
539   // create target buffer
540   target = malloc ( len );
541
542   if ( ! target ) {
543     fclose ( pnd );
544     return ( 0 );
545   }
546
547   targiter = target;
548
549   if ( r_buflen ) {
550     *r_buflen = len;
551   }
552
553   // copy over icon to target
554   while ( len ) {
555
556     if ( len > (8*1024) ) {
557       bitlen = (8*1024);
558     } else {
559       bitlen = len;
560     }
561
562     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
563       fclose ( pnd );
564       free ( target );
565       return ( NULL );
566     }
567
568     memmove ( targiter, bits, bitlen );
569     targiter += bitlen;
570
571     len -= bitlen;
572   } // while
573
574   fclose ( pnd );
575
576   return ( target );
577 }