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