Added basic emit_icon_to_buffer() routine for cpasjuste; you must free its return...
[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 0
71   if ( p -> description_en ) {
72     snprintf ( buffer, 1020, "Comment=%s\n", p -> description_en );
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 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
254 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
255   unsigned short int n = 0; // no. matches
256   char *t;
257   char *match;
258
259   // clear target so we can easily append
260   memset ( target_buffer, '\0', len );
261
262   /* attempt primary category chain
263    */
264   match = NULL;
265
266   if ( ( t = d -> main_category ) ) {
267     match = pnd_map_dotdesktop_category ( c, t );
268   }
269
270   if ( ( ! match ) &&
271        ( t = d -> main_category1 ) )
272   {
273     match = pnd_map_dotdesktop_category ( c, t );
274   }
275
276   if ( ( ! match ) &&
277        ( t = d -> main_category2 ) )
278   {
279     match = pnd_map_dotdesktop_category ( c, t );
280   }
281   
282   if ( match ) {
283     strncat ( target_buffer, match, len );
284     len -= strlen ( target_buffer );
285     n += 1;
286   }
287
288   /* attempt secondary category chain
289    */
290   match = NULL;
291
292   if ( ( t = d -> alt_category ) ) {
293     match = pnd_map_dotdesktop_category ( c, t );
294   }
295
296   if ( ( ! match ) &&
297        ( t = d -> alt_category1 ) )
298   {
299     match = pnd_map_dotdesktop_category ( c, t );
300   }
301
302   if ( ( ! match ) &&
303        ( t = d -> alt_category2 ) )
304   {
305     match = pnd_map_dotdesktop_category ( c, t );
306   }
307   
308   if ( match ) {
309     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
310       strcat ( target_buffer, ";" );
311       len -= 1;
312     }
313     strncat ( target_buffer, match, len );
314     len -= strlen ( target_buffer );
315     n += 1;
316   }
317
318 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
319   match = NULL;
320
321   if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
322     match = pnd_map_dotdesktop_category ( c, t );
323   }
324
325   if ( ( ! match ) &&
326        ( t = pnd_pxml_get_subcategory1 ( h ) ) )
327   {
328     match = pnd_map_dotdesktop_category ( c, t );
329   }
330
331   if ( ( ! match ) &&
332        ( t = pnd_pxml_get_subcategory2 ( h ) ) )
333   {
334     match = pnd_map_dotdesktop_category ( c, t );
335   }
336   
337   if ( match ) {
338     strncat ( target_buffer, match, len );
339     len -= strlen ( target_buffer );
340     n += 1;
341   }
342
343   /* attempt secondary category chain
344    */
345   match = NULL;
346
347   if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
348     match = pnd_map_dotdesktop_category ( c, t );
349   }
350
351   if ( ( ! match ) &&
352        ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
353   {
354     match = pnd_map_dotdesktop_category ( c, t );
355   }
356
357   if ( ( ! match ) &&
358        ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
359   {
360     match = pnd_map_dotdesktop_category ( c, t );
361   }
362   
363   if ( match ) {
364     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
365       strcat ( target_buffer, ";" );
366       len -= 1;
367     }
368     strncat ( target_buffer, match, len );
369     len -= strlen ( target_buffer );
370     n += 1;
371   }
372 #endif
373
374   if ( n && len ) {
375     strcat ( target_buffer, ";" );
376   }
377
378   return ( n );
379 }
380
381 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
382 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
383   char *key;
384   char *ret;
385
386   key = malloc ( strlen ( single_category ) + 4 + 1 );
387
388   sprintf ( key, "map.%s", single_category );
389
390   ret = pnd_conf_get_as_char ( c, key );
391
392   free ( key );
393
394   return ( ret );
395 }
396
397 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
398   // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
399   // with a fwrite at the end...
400   char from [ FILENAME_MAX ];   // source filename
401   char bits [ 8 * 1024 ];
402   unsigned int bitlen;
403   FILE *pnd = NULL;
404   unsigned char *target = NULL, *targiter = NULL;
405
406   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
407   if ( ( p -> object_type == pnd_object_type_pnd ) &&
408        ( ! p -> pnd_icon_pos ) )
409   {
410     return ( NULL ); // discover code didn't find it, so FAIL
411   }
412
413   /* first.. open the source file, by type of application:
414    * are we looking through a pnd file or a dir?
415    */
416   if ( p -> object_type == pnd_object_type_directory ) {
417     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
418   } else if ( p -> object_type == pnd_object_type_pnd ) {
419     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
420   }
421
422   pnd = fopen ( from, "r" );
423
424   if ( ! pnd ) {
425     return ( NULL );
426   }
427
428   // determine length of file, then adjust by icon position to find begin of icon
429   unsigned int len;
430
431   fseek ( pnd, 0, SEEK_END );
432   len = ftell ( pnd );
433   //fseek ( pnd, 0, SEEK_SET );
434
435   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
436
437   len -= p -> pnd_icon_pos;
438
439   // create target buffer
440   target = malloc ( len );
441
442   if ( ! target ) {
443     fclose ( pnd );
444     return ( 0 );
445   }
446
447   targiter = target;
448
449   if ( r_buflen ) {
450     *r_buflen = len;
451   }
452
453   // copy over icon to target
454   while ( len ) {
455
456     if ( len > (8*1024) ) {
457       bitlen = (8*1024);
458     } else {
459       bitlen = len;
460     }
461
462     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
463       fclose ( pnd );
464       free ( target );
465       return ( NULL );
466     }
467
468     memmove ( targiter, bits, bitlen );
469     targiter += bitlen;
470
471     len -= bitlen;
472   } // while
473
474   fclose ( pnd );
475
476   return ( target );
477 }