Added Comment= to .desktop emitted files, if description_en is present
[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
15 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
16   char filename [ FILENAME_MAX ];
17   char buffer [ 1024 ];
18   FILE *f;
19
20   // specification
21   // http://standards.freedesktop.org/desktop-entry-spec
22
23   // validation
24
25   if ( ! p -> unique_id ) {
26     return ( 0 );
27   }
28
29   if ( ! p -> exec ) {
30     return ( 0 );
31   }
32
33   if ( ! targetpath ) {
34     return ( 0 );
35   }
36
37   if ( ! pndrun ) {
38     return ( 0 );
39   }
40
41   // set up
42
43   sprintf ( filename, "%s/%s.desktop", targetpath, p -> unique_id );
44
45   // emit
46
47   //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
48
49   f = fopen ( filename, "w" );
50
51   if ( ! f ) {
52     return ( 0 );
53   }
54
55   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
56
57   if ( p -> title_en ) {
58     snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
59     fprintf ( f, "%s", buffer );
60   }
61
62   fprintf ( f, "Type=Application\n" );
63   fprintf ( f, "Version=1.0\n" );
64
65   if ( p -> icon ) {
66     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
67     fprintf ( f, "%s", buffer );
68   }
69
70 #if 1
71   if ( p -> desc_en && p -> desc_en [ 0 ] ) {
72     snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
73     fprintf ( f, "%s", buffer );
74   }
75 #endif
76
77 #if 0 // we let pnd_run.sh handle this
78   if ( p -> startdir ) {
79     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
80     fprintf ( f, "%s", buffer );
81   } else {
82     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
83   }
84 #endif
85
86   if ( p -> exec ) {
87     char *nohup;
88
89     if ( p -> option_no_x11 ) {
90       nohup = "/usr/bin/nohup ";
91     } else {
92       nohup = "";
93     }
94
95     // basics
96     if ( p -> object_type == pnd_object_type_directory ) {
97       snprintf ( buffer, 1020, "Exec=%s%s -p %s -e %s -b %s",
98                  nohup, pndrun, p -> object_path, p -> exec, p -> unique_id );
99     } else if ( p -> object_type == pnd_object_type_pnd ) {
100       snprintf ( buffer, 1020, "Exec=%s%s -p %s/%s -e %s -b %s",
101                  nohup, pndrun, p -> object_path, p -> object_filename, p -> exec, p -> unique_id );
102     }
103
104     // start dir
105     if ( p -> startdir ) {
106       strncat ( buffer, " -s ", 1020 );
107       strncat ( buffer, p -> startdir, 1020 );
108     }
109
110     // exec options
111     if ( p -> option_no_x11 ) {
112       strncat ( buffer, " -x ", 1020 );
113     }
114
115     // newline
116     strncat ( buffer, "\n", 1020 );
117
118     // emit
119     fprintf ( f, "%s", buffer );
120   }
121
122   // categories
123   {
124     char cats [ 512 ] = "";
125     int n;
126     pnd_conf_handle c;
127     char *confpath;
128
129     // uuuuh, defaults?
130     // "Application" used to be in the standard and is commonly supported still
131     // Utility and Network should ensure the app is visible 'somewhere' :/
132     char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
133
134     // determine searchpath (for conf, not for apps)
135     confpath = pnd_conf_query_searchpath();
136
137     // inhale the conf file
138     c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
139
140     // if we can find a default category set, pull it in; otherwise assume
141     // the hardcoded one
142     if ( pnd_conf_get_as_char ( c, "default" ) ) {
143       defaults = pnd_conf_get_as_char ( c, "default" );
144     }
145
146     // ditch the confpath
147     free ( confpath );
148
149     // attempt mapping
150     if ( c ) {
151
152       n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
153
154       if ( n ) {
155         fprintf ( f, "Categories=%s\n", cats );
156       } else {
157         fprintf ( f, "Categories=%s\n", defaults );
158       }
159
160     } else {
161       fprintf ( f, "Categories=%s\n", defaults );
162     }
163
164   }
165
166   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
167
168   fclose ( f );
169
170   return ( 1 );
171 }
172
173 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
174   char buffer [ FILENAME_MAX ]; // target filename
175   char from [ FILENAME_MAX ];   // source filename
176   char bits [ 8 * 1024 ];
177   unsigned int bitlen;
178   FILE *pnd, *target;
179
180   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
181   if ( ( p -> object_type == pnd_object_type_pnd ) &&
182        ( ! p -> pnd_icon_pos ) )
183   {
184     return ( 0 ); // discover code didn't find it, so FAIL
185   }
186
187   // determine filename for target
188   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
189
190   /* first.. open the source file, by type of application:
191    * are we looking through a pnd file or a dir?
192    */
193   if ( p -> object_type == pnd_object_type_directory ) {
194     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
195   } else if ( p -> object_type == pnd_object_type_pnd ) {
196     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
197   }
198
199   pnd = fopen ( from, "r" );
200
201   if ( ! pnd ) {
202     return ( 0 );
203   }
204
205   unsigned int len;
206
207   target = fopen ( buffer, "wb" );
208
209   if ( ! target ) {
210     fclose ( pnd );
211     return ( 0 );
212   }
213
214   fseek ( pnd, 0, SEEK_END );
215   len = ftell ( pnd );
216   //fseek ( pnd, 0, SEEK_SET );
217
218   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
219
220   len -= p -> pnd_icon_pos;
221
222   while ( len ) {
223
224     if ( len > (8*1024) ) {
225       bitlen = (8*1024);
226     } else {
227       bitlen = len;
228     }
229
230     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
231       fclose ( pnd );
232       fclose ( target );
233       unlink ( buffer );
234       return ( 0 );
235     }
236
237     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
238       fclose ( pnd );
239       fclose ( target );
240       unlink ( buffer );
241       return ( 0 );
242     }
243
244     len -= bitlen;
245   } // while
246
247   fclose ( pnd );
248   fclose ( target );
249
250   return ( 1 );
251 }
252
253 #if 1 // we switched direction to freedesktop standard categories
254 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
255 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
256   char *t;
257   char *match;
258
259   // clear target so we can easily append
260   memset ( target_buffer, '\0', len );
261
262   // for each main-cat and sub-cat, including alternates, just append them all together
263   // we'll try mapping them, since the categories file is there, but we'll default to
264   // copying over; this lets the conf file do merging or renaming of cagtegories, which
265   // could still be useful, but we can leave the conf file empty to effect a pure
266   // trusted-PXML-copying
267
268   // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
269   // there are hundreds), and would also mean new ones and peoples custom ones would
270   // flop
271
272   /* attempt primary category chain
273    */
274   #define MAPCAT(field)                                     \
275     if ( ( t = d -> field ) ) {                             \
276       match = pnd_map_dotdesktop_category ( c, t );         \
277       strncat ( target_buffer, match ? match : t, len );    \
278       strncat ( target_buffer, ";", len );                  \
279     }
280
281   MAPCAT(main_category);
282   MAPCAT(main_category1);
283   MAPCAT(main_category2);
284   MAPCAT(alt_category);
285   MAPCAT(alt_category1);
286   MAPCAT(alt_category2);
287
288   if ( target_buffer [ 0 ] ) {
289     return ( 1 ); // I guess its 'good'?
290   }
291
292 #if 0
293   if ( ( t = d -> main_category ) ) {
294     match = pnd_map_dotdesktop_category ( c, t );
295     strncat ( target_buffer, match ? match : t, len );
296     strncat ( target_buffer, ";", len );
297   }
298 #endif
299
300   return ( 0 );
301 }
302 #endif
303
304 #if 0 // we switched direction
305 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
306 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
307   unsigned short int n = 0; // no. matches
308   char *t;
309   char *match;
310
311   // clear target so we can easily append
312   memset ( target_buffer, '\0', len );
313
314   /* attempt primary category chain
315    */
316   match = NULL;
317
318   if ( ( t = d -> main_category ) ) {
319     match = pnd_map_dotdesktop_category ( c, t );
320   }
321
322   if ( ( ! match ) &&
323        ( t = d -> main_category1 ) )
324   {
325     match = pnd_map_dotdesktop_category ( c, t );
326   }
327
328   if ( ( ! match ) &&
329        ( t = d -> main_category2 ) )
330   {
331     match = pnd_map_dotdesktop_category ( c, t );
332   }
333   
334   if ( match ) {
335     strncat ( target_buffer, match, len );
336     len -= strlen ( target_buffer );
337     n += 1;
338   }
339
340   /* attempt secondary category chain
341    */
342   match = NULL;
343
344   if ( ( t = d -> alt_category ) ) {
345     match = pnd_map_dotdesktop_category ( c, t );
346   }
347
348   if ( ( ! match ) &&
349        ( t = d -> alt_category1 ) )
350   {
351     match = pnd_map_dotdesktop_category ( c, t );
352   }
353
354   if ( ( ! match ) &&
355        ( t = d -> alt_category2 ) )
356   {
357     match = pnd_map_dotdesktop_category ( c, t );
358   }
359   
360   if ( match ) {
361     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
362       strcat ( target_buffer, ";" );
363       len -= 1;
364     }
365     strncat ( target_buffer, match, len );
366     len -= strlen ( target_buffer );
367     n += 1;
368   }
369
370 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
371   match = NULL;
372
373   if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
374     match = pnd_map_dotdesktop_category ( c, t );
375   }
376
377   if ( ( ! match ) &&
378        ( t = pnd_pxml_get_subcategory1 ( h ) ) )
379   {
380     match = pnd_map_dotdesktop_category ( c, t );
381   }
382
383   if ( ( ! match ) &&
384        ( t = pnd_pxml_get_subcategory2 ( h ) ) )
385   {
386     match = pnd_map_dotdesktop_category ( c, t );
387   }
388   
389   if ( match ) {
390     strncat ( target_buffer, match, len );
391     len -= strlen ( target_buffer );
392     n += 1;
393   }
394
395   /* attempt secondary category chain
396    */
397   match = NULL;
398
399   if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
400     match = pnd_map_dotdesktop_category ( c, t );
401   }
402
403   if ( ( ! match ) &&
404        ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
405   {
406     match = pnd_map_dotdesktop_category ( c, t );
407   }
408
409   if ( ( ! match ) &&
410        ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
411   {
412     match = pnd_map_dotdesktop_category ( c, t );
413   }
414   
415   if ( match ) {
416     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
417       strcat ( target_buffer, ";" );
418       len -= 1;
419     }
420     strncat ( target_buffer, match, len );
421     len -= strlen ( target_buffer );
422     n += 1;
423   }
424 #endif
425
426   if ( n && len ) {
427     strcat ( target_buffer, ";" );
428   }
429
430   return ( n );
431 }
432 #endif
433
434 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
435 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
436   char *key;
437   char *ret;
438
439   key = malloc ( strlen ( single_category ) + 4 + 1 );
440
441   sprintf ( key, "map.%s", single_category );
442
443   ret = pnd_conf_get_as_char ( c, key );
444
445   free ( key );
446
447   return ( ret );
448 }
449
450 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
451   // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
452   // with a fwrite at the end...
453   char from [ FILENAME_MAX ];   // source filename
454   char bits [ 8 * 1024 ];
455   unsigned int bitlen;
456   FILE *pnd = NULL;
457   unsigned char *target = NULL, *targiter = NULL;
458
459   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
460   if ( ( p -> object_type == pnd_object_type_pnd ) &&
461        ( ! p -> pnd_icon_pos ) )
462   {
463     return ( NULL ); // discover code didn't find it, so FAIL
464   }
465
466   /* first.. open the source file, by type of application:
467    * are we looking through a pnd file or a dir?
468    */
469   if ( p -> object_type == pnd_object_type_directory ) {
470     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
471   } else if ( p -> object_type == pnd_object_type_pnd ) {
472     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
473   }
474
475   pnd = fopen ( from, "r" );
476
477   if ( ! pnd ) {
478     return ( NULL );
479   }
480
481   // determine length of file, then adjust by icon position to find begin of icon
482   unsigned int len;
483
484   fseek ( pnd, 0, SEEK_END );
485   len = ftell ( pnd );
486   //fseek ( pnd, 0, SEEK_SET );
487
488   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
489
490   len -= p -> pnd_icon_pos;
491
492   // create target buffer
493   target = malloc ( len );
494
495   if ( ! target ) {
496     fclose ( pnd );
497     return ( 0 );
498   }
499
500   targiter = target;
501
502   if ( r_buflen ) {
503     *r_buflen = len;
504   }
505
506   // copy over icon to target
507   while ( len ) {
508
509     if ( len > (8*1024) ) {
510       bitlen = (8*1024);
511     } else {
512       bitlen = len;
513     }
514
515     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
516       fclose ( pnd );
517       free ( target );
518       return ( NULL );
519     }
520
521     memmove ( targiter, bits, bitlen );
522     targiter += bitlen;
523
524     len -= bitlen;
525   } // while
526
527   fclose ( pnd );
528
529   return ( target );
530 }