Minor debugging added; turns out a bug I tracked for hours was... a bad SDHC card :(
[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   unsigned 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, "rb" );
217
218   if ( ! pnd ) {
219     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open source\n" );
220     return ( 0 );
221   }
222
223   unsigned int len;
224
225   target = fopen ( buffer, "wb" );
226
227   if ( ! target ) {
228     fclose ( pnd );
229     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open target\n" );
230     return ( 0 );
231   }
232
233   fseek ( pnd, 0, SEEK_END );
234   len = ftell ( pnd );
235   //fseek ( pnd, 0, SEEK_SET );
236
237   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
238
239   len -= p -> pnd_icon_pos;
240
241   pnd_log ( PND_LOG_DEFAULT, "    Emit icon, length: %u\n", len );
242
243   while ( len ) {
244
245     if ( len > (BITLEN) ) {
246       bitlen = (BITLEN);
247     } else {
248       bitlen = len;
249     }
250
251     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
252       fclose ( pnd );
253       fclose ( target );
254       unlink ( buffer );
255       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad read\n" );
256       return ( 0 );
257     }
258
259 #if 0
260     {
261       unsigned int i = 0;
262       char bigbuffer [ 200 * 1024 ] = "\0";
263       char b [ 10 ];
264       pnd_log ( PND_LOG_DEFAULT, "    Read hexdump\n" );
265       while ( i < bitlen ) {
266         sprintf ( b, "%x,", bits [ i ] );
267         strcat ( bigbuffer, b );
268         i++;
269       }
270       pnd_log ( PND_LOG_DEFAULT, bigbuffer );
271     }
272 #endif
273
274     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
275       fclose ( pnd );
276       fclose ( target );
277       unlink ( buffer );
278       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad write\n" );
279       return ( 0 );
280     }
281
282     len -= bitlen;
283     //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, next block, length: %u\n", len );
284   } // while
285
286   fclose ( pnd );
287   fclose ( target );
288
289   //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, done.\n" );
290
291   return ( 1 );
292 }
293
294 #if 1 // we switched direction to freedesktop standard categories
295 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
296 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
297   char *t;
298   char *match;
299
300   // clear target so we can easily append
301   memset ( target_buffer, '\0', len );
302
303   // for each main-cat and sub-cat, including alternates, just append them all together
304   // we'll try mapping them, since the categories file is there, but we'll default to
305   // copying over; this lets the conf file do merging or renaming of cagtegories, which
306   // could still be useful, but we can leave the conf file empty to effect a pure
307   // trusted-PXML-copying
308
309   // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
310   // there are hundreds), and would also mean new ones and peoples custom ones would
311   // flop
312
313   /* attempt primary category chain
314    */
315   #define MAPCAT(field)                                     \
316     if ( ( t = d -> field ) ) {                             \
317       match = pnd_map_dotdesktop_category ( c, t );         \
318       strncat ( target_buffer, match ? match : t, len );    \
319       strncat ( target_buffer, ";", len );                  \
320     }
321
322   MAPCAT(main_category);
323   MAPCAT(main_category1);
324   MAPCAT(main_category2);
325   MAPCAT(alt_category);
326   MAPCAT(alt_category1);
327   MAPCAT(alt_category2);
328
329   if ( target_buffer [ 0 ] ) {
330     return ( 1 ); // I guess its 'good'?
331   }
332
333 #if 0
334   if ( ( t = d -> main_category ) ) {
335     match = pnd_map_dotdesktop_category ( c, t );
336     strncat ( target_buffer, match ? match : t, len );
337     strncat ( target_buffer, ";", len );
338   }
339 #endif
340
341   return ( 0 );
342 }
343 #endif
344
345 #if 0 // we switched direction
346 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
347 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
348   unsigned short int n = 0; // no. matches
349   char *t;
350   char *match;
351
352   // clear target so we can easily append
353   memset ( target_buffer, '\0', len );
354
355   /* attempt primary category chain
356    */
357   match = NULL;
358
359   if ( ( t = d -> main_category ) ) {
360     match = pnd_map_dotdesktop_category ( c, t );
361   }
362
363   if ( ( ! match ) &&
364        ( t = d -> main_category1 ) )
365   {
366     match = pnd_map_dotdesktop_category ( c, t );
367   }
368
369   if ( ( ! match ) &&
370        ( t = d -> main_category2 ) )
371   {
372     match = pnd_map_dotdesktop_category ( c, t );
373   }
374   
375   if ( match ) {
376     strncat ( target_buffer, match, len );
377     len -= strlen ( target_buffer );
378     n += 1;
379   }
380
381   /* attempt secondary category chain
382    */
383   match = NULL;
384
385   if ( ( t = d -> alt_category ) ) {
386     match = pnd_map_dotdesktop_category ( c, t );
387   }
388
389   if ( ( ! match ) &&
390        ( t = d -> alt_category1 ) )
391   {
392     match = pnd_map_dotdesktop_category ( c, t );
393   }
394
395   if ( ( ! match ) &&
396        ( t = d -> alt_category2 ) )
397   {
398     match = pnd_map_dotdesktop_category ( c, t );
399   }
400   
401   if ( match ) {
402     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
403       strcat ( target_buffer, ";" );
404       len -= 1;
405     }
406     strncat ( target_buffer, match, len );
407     len -= strlen ( target_buffer );
408     n += 1;
409   }
410
411 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
412   match = NULL;
413
414   if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
415     match = pnd_map_dotdesktop_category ( c, t );
416   }
417
418   if ( ( ! match ) &&
419        ( t = pnd_pxml_get_subcategory1 ( h ) ) )
420   {
421     match = pnd_map_dotdesktop_category ( c, t );
422   }
423
424   if ( ( ! match ) &&
425        ( t = pnd_pxml_get_subcategory2 ( h ) ) )
426   {
427     match = pnd_map_dotdesktop_category ( c, t );
428   }
429   
430   if ( match ) {
431     strncat ( target_buffer, match, len );
432     len -= strlen ( target_buffer );
433     n += 1;
434   }
435
436   /* attempt secondary category chain
437    */
438   match = NULL;
439
440   if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
441     match = pnd_map_dotdesktop_category ( c, t );
442   }
443
444   if ( ( ! match ) &&
445        ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
446   {
447     match = pnd_map_dotdesktop_category ( c, t );
448   }
449
450   if ( ( ! match ) &&
451        ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
452   {
453     match = pnd_map_dotdesktop_category ( c, t );
454   }
455   
456   if ( match ) {
457     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
458       strcat ( target_buffer, ";" );
459       len -= 1;
460     }
461     strncat ( target_buffer, match, len );
462     len -= strlen ( target_buffer );
463     n += 1;
464   }
465 #endif
466
467   if ( n && len ) {
468     strcat ( target_buffer, ";" );
469   }
470
471   return ( n );
472 }
473 #endif
474
475 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
476 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
477   char *key;
478   char *ret;
479
480   key = malloc ( strlen ( single_category ) + 4 + 1 );
481
482   sprintf ( key, "map.%s", single_category );
483
484   ret = pnd_conf_get_as_char ( c, key );
485
486   free ( key );
487
488   return ( ret );
489 }
490
491 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
492   // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
493   // with a fwrite at the end...
494   char from [ FILENAME_MAX ];   // source filename
495   char bits [ 8 * 1024 ];
496   unsigned int bitlen;
497   FILE *pnd = NULL;
498   unsigned char *target = NULL, *targiter = NULL;
499
500   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
501   if ( ( p -> object_type == pnd_object_type_pnd ) &&
502        ( ! p -> pnd_icon_pos ) )
503   {
504     return ( NULL ); // discover code didn't find it, so FAIL
505   }
506
507   /* first.. open the source file, by type of application:
508    * are we looking through a pnd file or a dir?
509    */
510   if ( p -> object_type == pnd_object_type_directory ) {
511     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
512   } else if ( p -> object_type == pnd_object_type_pnd ) {
513     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
514   }
515
516   pnd = fopen ( from, "r" );
517
518   if ( ! pnd ) {
519     return ( NULL );
520   }
521
522   // determine length of file, then adjust by icon position to find begin of icon
523   unsigned int len;
524
525   fseek ( pnd, 0, SEEK_END );
526   len = ftell ( pnd );
527   //fseek ( pnd, 0, SEEK_SET );
528
529   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
530
531   len -= p -> pnd_icon_pos;
532
533   // create target buffer
534   target = malloc ( len );
535
536   if ( ! target ) {
537     fclose ( pnd );
538     return ( 0 );
539   }
540
541   targiter = target;
542
543   if ( r_buflen ) {
544     *r_buflen = len;
545   }
546
547   // copy over icon to target
548   while ( len ) {
549
550     if ( len > (8*1024) ) {
551       bitlen = (8*1024);
552     } else {
553       bitlen = len;
554     }
555
556     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
557       fclose ( pnd );
558       free ( target );
559       return ( NULL );
560     }
561
562     memmove ( targiter, bits, bitlen );
563     targiter += bitlen;
564
565     len -= bitlen;
566   } // while
567
568   fclose ( pnd );
569
570   return ( target );
571 }