4b743d437ec669c27a27e1e3cbdd1532e402a7a2
[pandora-libraries.git] / lib / pnd_pxml.c
1
2 #include <stdio.h> /* for FILE */
3 #include <stdlib.h> /* for malloc */
4 #include <string.h> /* for string ops */
5
6 #include <sys/types.h> /* for stat */
7 #include <sys/stat.h> /* for stat */
8 #include <unistd.h> /* for stat */
9
10 #include "pnd_pxml.h"
11 #include "pnd_pathiter.h"
12 #include "pnd_tinyxml.h"
13 #include "pnd_logger.h"
14
15 pnd_pxml_handle *pnd_pxml_fetch ( char *fullpath ) {
16   FILE *f;
17   char *b;
18   unsigned int len;
19
20   f = fopen ( fullpath, "r" );
21
22   if ( ! f ) {
23     return ( 0 );
24   }
25
26   fseek ( f, 0, SEEK_END );
27
28   len = ftell ( f );
29
30   fseek ( f, 0, SEEK_SET );
31
32   b = (char*) malloc ( len );
33
34   if ( ! b ) {
35     fclose ( f );
36     return ( 0 );
37   }
38
39   fread ( b, 1, len, f );
40
41   fclose ( f );
42
43   return ( pnd_pxml_fetch_buffer ( fullpath, b ) );
44 }
45
46 pnd_pxml_handle *pnd_pxml_fetch_buffer ( char *filename, char *buffer ) {
47
48   pnd_pxml_t **p = malloc ( sizeof(pnd_pxml_t*) * PXML_MAXAPPS );
49   memset ( p, '\0', sizeof(pnd_pxml_t*) * PXML_MAXAPPS );
50
51   if ( ! pnd_pxml_parse ( filename, buffer, strlen ( buffer ), p ) ) {
52     return ( 0 );
53   }
54
55   return ( (pnd_pxml_handle*) p );
56 }
57
58 void pnd_pxml_delete ( pnd_pxml_handle h ) {
59   pnd_pxml_t *p = (pnd_pxml_t*) h;
60
61   int i;
62   if (p->titles) {
63     for (i = 0; i < p->titles_c; i++)
64     {
65       free(p->titles[i].language);
66       free(p->titles[i].string);
67     }
68     free(p->titles);
69   }
70
71   if (p->descriptions) {
72     for (i = 0; i < p->descriptions_c; i++)
73     {
74       free(p->descriptions[i].language);
75       free(p->descriptions[i].string);
76     }
77     free(p->descriptions);
78   }
79
80   if ( p -> standalone ) {
81     free ( p -> standalone );
82   }
83   if ( p -> icon ) {
84     free ( p -> icon );
85   }
86   if ( p -> previewpic1 ) {
87     free ( p -> previewpic1 );
88   }
89   if ( p -> previewpic2 ) {
90     free ( p -> previewpic2 );
91   }
92   if ( p -> author_name ) {
93     free ( p -> author_name );
94   }
95   if ( p -> author_website ) {
96     free ( p -> author_website );
97   }
98   if ( p -> version_major ) {
99     free ( p -> version_major );
100   }
101   if ( p -> version_minor ) {
102     free ( p -> version_minor );
103   }
104   if ( p -> version_release ) {
105     free ( p -> version_release );
106   }
107   if ( p -> version_build ) {
108     free ( p -> version_build );
109   }
110   if ( p -> exec ) {
111     free ( p -> exec );
112   }
113   if ( p -> main_category ) {
114     free ( p -> main_category );
115   }
116   if ( p -> subcategory1 ) {
117     free ( p -> subcategory1 );
118   }
119   if ( p -> subcategory2 ) {
120     free ( p -> subcategory2 );
121   }
122   if ( p -> altcategory ) {
123     free ( p -> altcategory );
124   }
125   if ( p -> altsubcategory1 ) {
126     free ( p -> altsubcategory1 );
127   }
128   if ( p -> altsubcategory2 ) {
129     free ( p -> altsubcategory2 );
130   }
131   if ( p -> osversion_major ) {
132     free ( p -> osversion_major );
133   }
134   if ( p -> osversion_minor ) {
135     free ( p -> osversion_minor );
136   }
137   if ( p -> osversion_release ) {
138     free ( p -> osversion_release );
139   }
140   if ( p -> osversion_build ) {
141     free ( p -> osversion_build );
142   }
143   if ( p -> associationitem1_name ) {
144     free ( p -> associationitem1_name );
145   }
146   if ( p -> associationitem1_filetype ) {
147     free ( p -> associationitem1_filetype );
148   }
149   if ( p -> associationitem1_parameter ) {
150     free ( p -> associationitem1_parameter );
151   }
152   if ( p -> associationitem2_name ) {
153     free ( p -> associationitem2_name );
154   }
155   if ( p -> associationitem2_filetype ) {
156     free ( p -> associationitem2_filetype );
157   }
158   if ( p -> associationitem2_parameter ) {
159     free ( p -> associationitem2_parameter );
160   }
161   if ( p -> associationitem3_name ) {
162     free ( p -> associationitem3_name );
163   }
164   if ( p -> associationitem1_filetype ) {
165     free ( p -> associationitem3_filetype );
166   }
167   if ( p -> associationitem1_parameter ) {
168     free ( p -> associationitem3_parameter );
169   }
170   if ( p -> clockspeed ) {
171     free ( p -> clockspeed );
172   }
173   if ( p -> background ) {
174     free ( p -> background );
175   }
176   if ( p -> startdir ) {
177     free ( p -> startdir );
178   }
179   if ( p -> appdata_dirname ) {
180     free ( p -> appdata_dirname );
181   }
182
183   free(p); /*very important!*/
184
185   return;
186 }
187
188 void pnd_pxml_set_app_name ( pnd_pxml_handle h, char *v ) {
189   /* 
190    * Please do not use this function if it can be avoided; it is only here for compatibility.
191    * The function might fail on low memory, and there's no way for the user to know when this happens.
192    */
193   pnd_pxml_t *p = (pnd_pxml_t*) h;
194   char has_en_field = 0;
195   int i;
196
197   if (!v) return; /*TODO: add error information? Make it possible to set the string to NULL?*/
198
199   for (i = 0; i < p->titles_c; i++)
200   {
201     if (strncmp("en", p->titles[i].language, 2) == 0) /*strict comparison; match "en_US", "en_GB" etc... All these are set.*/
202     {
203       free(p->titles[i].string);
204       p->titles[i].string = strdup(v);
205       has_en_field = 1;
206     }
207   }
208
209   if (!has_en_field)
210   {
211     p->titles_c++;
212     if (p->titles_c > p->titles_alloc_c) //we don't have enough strings allocated
213     {
214       p->titles_alloc_c <<= 1;
215       p->titles = (pnd_localized_string_t *)realloc((void*)p->titles, p->titles_alloc_c);
216       if (!p->titles) return; //errno = ENOMEM
217     }
218     p->titles[p->titles_c - 1].language = "en_US";
219     p->titles[p->titles_c - 1].string = strdup(v);
220   }
221
222   return;
223 }
224
225 unsigned char pnd_is_pxml_valid_app ( pnd_pxml_handle h ) {
226   //pnd_pxml_t *p = (pnd_pxml_t*) h; //unused atm
227
228   // for now, lets just verify the exec-path is valid
229
230   //printf ( "exec is '%s'\n", p -> exec );
231
232   return ( 1 );
233
234   // even this is complicated by pnd_run.sh semantics .. can't check if it exists
235   // during discovery, since it is not mounted yet..
236 #if 0
237   struct stat buf;
238   if ( stat ( p -> exec, &buf ) == 0 ) {
239     return ( 1 ); // path is present
240   }
241 #endif
242
243   return ( 0 );
244 }
245
246 signed char pnd_pxml_merge_override ( pnd_pxml_handle h, char *searchpath ) {
247   // the pxml includes a unique-id; use this value to attempt to find an
248   // override in the given searchpath
249   signed char retval = 0;
250
251 #if 0 // TODO: Unfinished entirely now
252   pnd_pxml_handle mergeh;
253
254   if ( ! pnd_pxml_get_unique_id ( h ) ) {
255     return ( -1 ); // no unique-id present, so can't use it to name potential override files
256   }
257
258   SEARCHPATH_PRE
259   {
260
261     // do it
262     strncat ( buffer, "/", FILENAME_MAX );
263     strncat ( buffer, pnd_pxml_get_unique_id ( h ), FILENAME_MAX );
264     strncat ( buffer, ".xml", FILENAME_MAX );
265     //printf ( "  Path to seek merges: '%s'\n", buffer );
266
267     // TODO: handle multiple subapps!
268     mergeh = pnd_pxml_fetch ( buffer );
269
270     if ( mergeh ) {
271
272       // TODO: handle all the various data bits
273       if ( pnd_pxml_get_app_name_en ( mergeh ) ) {
274         pnd_pxml_set_app_name ( h, pnd_pxml_get_app_name_en ( mergeh ) );
275       }
276
277       pnd_pxml_delete ( mergeh );
278     }
279
280   }
281   SEARCHPATH_POST
282 #endif
283
284   return ( retval );
285 }
286
287 char *pnd_pxml_get_best_localized_string(pnd_localized_string_t strings[], int strings_c, char *iso_lang)
288 {
289   int i;
290   int similarity_weight = 0xffff; /*Set to something Really Bad in the beginning*/
291   char *best_match = NULL;
292
293   for(i = 0; i < strings_c; i++)
294   {
295     // factor in the length -- if we're given 'en' and have a string 'en_US', thats better than 'de_something'; if we don't
296     // use length, then en_US and de_FO are same to 'en'.
297     int maxcount = strlen ( strings[i].language ) < strlen ( iso_lang ) ? strlen ( strings[i].language ) : strlen ( iso_lang );
298     int new_weight = abs(strncmp(strings[i].language, iso_lang, maxcount));
299     //pnd_log ( PND_LOG_DEFAULT, "looking for lang %s, looking at lang %s (weight %d, old %d): %s\n",
300     //          iso_lang, strings [ i ].language, new_weight, similarity_weight, strings [ i ].string );
301     if (new_weight < similarity_weight)
302     {
303       similarity_weight = new_weight;
304       best_match = strings[i].string;
305     }
306   }
307
308   if ( best_match ) {
309     //pnd_log ( PND_LOG_DEFAULT, "best match: %s\n", best_match );
310     return strdup(best_match);
311   }
312
313   //pnd_log ( PND_LOG_DEFAULT, "best match: FAIL\n" );
314
315   return ( NULL );
316 }
317
318 char *pnd_pxml_get_package_id ( pnd_pxml_handle h ) {
319   pnd_pxml_t *p = (pnd_pxml_t*) h;
320   return ( p -> package_id );
321 }
322
323 char *pnd_pxml_get_app_name ( pnd_pxml_handle h, char *iso_lang ) {
324   pnd_pxml_t *p = (pnd_pxml_t *) h;
325   return pnd_pxml_get_best_localized_string(p->titles, p->titles_c, iso_lang);
326 }
327
328 char *pnd_pxml_get_app_name_en ( pnd_pxml_handle h ) {
329   return pnd_pxml_get_app_name(h, "en");
330 }
331
332 char *pnd_pxml_get_app_name_de ( pnd_pxml_handle h ) {
333   return pnd_pxml_get_app_name(h, "de");
334 }
335
336 char *pnd_pxml_get_app_name_it ( pnd_pxml_handle h ) {
337   return pnd_pxml_get_app_name(h, "it");
338 }
339
340 char *pnd_pxml_get_app_name_fr ( pnd_pxml_handle h ) {
341   return pnd_pxml_get_app_name(h, "fr");
342 }
343 char *pnd_pxml_get_unique_id ( pnd_pxml_handle h ) {
344   pnd_pxml_t *p = (pnd_pxml_t*) h;
345   return ( p -> unique_id );
346 }
347
348 char *pnd_pxml_get_appdata_dirname ( pnd_pxml_handle h ) {
349   pnd_pxml_t *p = (pnd_pxml_t*) h;
350   return ( p -> appdata_dirname );
351 }
352
353 char *pnd_pxml_get_standalone ( pnd_pxml_handle h ) {
354   pnd_pxml_t *p = (pnd_pxml_t*) h;
355   return ( p -> standalone );
356 }
357
358 char *pnd_pxml_get_icon ( pnd_pxml_handle h ) {
359   pnd_pxml_t *p = (pnd_pxml_t*) h;
360   return ( p -> icon );
361 }
362
363 // this guy's func name is 'out of sync' with the family of functions below; but since it
364 // exists, rather than just remove it and break someones code, will add in the appropriate
365 // function wrapper; the header only specifies the other guy (always did), so the header
366 // was already on the right path.
367 char *pnd_pxml_get_app_description ( pnd_pxml_handle h, char *iso_lang ) {
368   pnd_pxml_t *p = (pnd_pxml_t *) h;
369   return pnd_pxml_get_best_localized_string(p->descriptions, p->descriptions_c, iso_lang);
370 }
371
372 char *pnd_pxml_get_description_en ( pnd_pxml_handle h ) {
373   return pnd_pxml_get_app_description(h, "en");
374 }
375
376 char *pnd_pxml_get_description_de ( pnd_pxml_handle h ) {
377   return pnd_pxml_get_app_description(h, "de");
378 }
379
380 char *pnd_pxml_get_description_it ( pnd_pxml_handle h ) {
381   return pnd_pxml_get_app_description(h, "it");
382 }
383
384 char *pnd_pxml_get_description_fr ( pnd_pxml_handle h ) {
385   return pnd_pxml_get_app_description(h, "fr");
386 }
387
388 // wrapper added so family of function names is consistent; see comment for pnd_pxml_get_app_description() above
389 char *pnd_pxml_get_description ( pnd_pxml_handle h, char *iso_lang) {
390   return ( pnd_pxml_get_app_description ( h, iso_lang ) );
391 }
392
393 char *pnd_pxml_get_previewpic1 ( pnd_pxml_handle h ) {
394   pnd_pxml_t *p = (pnd_pxml_t*) h;
395   return ( p -> previewpic1 );
396 }
397
398 char *pnd_pxml_get_previewpic2 ( pnd_pxml_handle h ) {
399   pnd_pxml_t *p = (pnd_pxml_t*) h;
400   return ( p -> previewpic2 );
401 }
402
403 char *pnd_pxml_get_author_name ( pnd_pxml_handle h ) {
404   pnd_pxml_t *p = (pnd_pxml_t*) h;
405   return ( p -> author_name );
406 }
407
408 char *pnd_pxml_get_author_website ( pnd_pxml_handle h ) {
409   pnd_pxml_t *p = (pnd_pxml_t*) h;
410   return ( p -> author_website );
411 }
412
413 char *pnd_pxml_get_version_major ( pnd_pxml_handle h ) {
414   pnd_pxml_t *p = (pnd_pxml_t*) h;
415   return ( p -> version_major );
416 }
417
418 char *pnd_pxml_get_version_minor ( pnd_pxml_handle h ) {
419   pnd_pxml_t *p = (pnd_pxml_t*) h;
420   return ( p -> version_minor );
421 }
422
423 char *pnd_pxml_get_version_release ( pnd_pxml_handle h ) {
424   pnd_pxml_t *p = (pnd_pxml_t*) h;
425   return ( p -> version_release );
426 }
427
428 char *pnd_pxml_get_version_build ( pnd_pxml_handle h ) {
429   pnd_pxml_t *p = (pnd_pxml_t*) h;
430   return ( p -> version_build );
431 }
432
433 char *pnd_pxml_get_exec ( pnd_pxml_handle h ) {
434   pnd_pxml_t *p = (pnd_pxml_t*) h;
435   return ( p -> exec );
436 }
437
438 char *pnd_pxml_get_execargs ( pnd_pxml_handle h ) {
439   pnd_pxml_t *p = (pnd_pxml_t*) h;
440   return ( p -> execargs );
441 }
442
443 char *pnd_pxml_get_exec_option_no_x11 ( pnd_pxml_handle h ) {
444   pnd_pxml_t *p = (pnd_pxml_t*) h;
445   return ( p -> exec_no_x11 );
446 }
447
448 char *pnd_pxml_get_main_category ( pnd_pxml_handle h ) {
449   pnd_pxml_t *p = (pnd_pxml_t*) h;
450   return ( p -> main_category );
451 }
452
453 char *pnd_pxml_get_subcategory1 ( pnd_pxml_handle h ) {
454   pnd_pxml_t *p = (pnd_pxml_t*) h;
455   return ( p -> subcategory1 );
456 }
457
458 char *pnd_pxml_get_subcategory2 ( pnd_pxml_handle h ) {
459   pnd_pxml_t *p = (pnd_pxml_t*) h;
460   return ( p -> subcategory2 );
461 }
462
463 char *pnd_pxml_get_altcategory ( pnd_pxml_handle h ) {
464   pnd_pxml_t *p = (pnd_pxml_t*) h;
465   return ( p -> altcategory );
466 }
467
468 char *pnd_pxml_get_altsubcategory1 ( pnd_pxml_handle h ) {
469   pnd_pxml_t *p = (pnd_pxml_t*) h;
470   return ( p -> altsubcategory1 );
471 }
472
473 char *pnd_pxml_get_altsubcategory2 ( pnd_pxml_handle h ) {
474   pnd_pxml_t *p = (pnd_pxml_t*) h;
475   return ( p -> altsubcategory2 );
476 }
477
478 char *pnd_pxml_get_osversion_major ( pnd_pxml_handle h ) {
479   pnd_pxml_t *p = (pnd_pxml_t*) h;
480   return ( p -> osversion_major );
481 }
482
483 char *pnd_pxml_get_osversion_minor ( pnd_pxml_handle h ) {
484   pnd_pxml_t *p = (pnd_pxml_t*) h;
485   return ( p -> osversion_minor );
486 }
487
488 char *pnd_pxml_get_osversion_release ( pnd_pxml_handle h ) {
489   pnd_pxml_t *p = (pnd_pxml_t*) h;
490   return ( p -> osversion_release );
491 }
492
493 char *pnd_pxml_get_osversion_build ( pnd_pxml_handle h ) {
494   pnd_pxml_t *p = (pnd_pxml_t*) h;
495   return ( p -> osversion_build );
496 }
497
498 char *pnd_pxml_get_associationitem1_name ( pnd_pxml_handle h ) {
499   pnd_pxml_t *p = (pnd_pxml_t*) h;
500   return ( p -> associationitem1_name );
501 }
502
503 char *pnd_pxml_get_associationitem1_filetype ( pnd_pxml_handle h ) {
504   pnd_pxml_t *p = (pnd_pxml_t*) h;
505   return ( p -> associationitem1_filetype );
506 }
507
508 char *pnd_pxml_get_associationitem1_parameter ( pnd_pxml_handle h ) {
509   pnd_pxml_t *p = (pnd_pxml_t*) h;
510   return ( p -> associationitem1_parameter );
511 }
512
513 char *pnd_pxml_get_associationitem2_name ( pnd_pxml_handle h ) {
514   pnd_pxml_t *p = (pnd_pxml_t*) h;
515   return ( p -> associationitem2_name );
516 }
517
518 char *pnd_pxml_get_associationitem2_filetype ( pnd_pxml_handle h ) {
519   pnd_pxml_t *p = (pnd_pxml_t*) h;
520   return ( p -> associationitem2_filetype );
521 }
522
523 char *pnd_pxml_get_associationitem2_parameter ( pnd_pxml_handle h ) {
524   pnd_pxml_t *p = (pnd_pxml_t*) h;
525   return ( p -> associationitem2_parameter );
526 }
527
528 char *pnd_pxml_get_associationitem3_name ( pnd_pxml_handle h ) {
529   pnd_pxml_t *p = (pnd_pxml_t*) h;
530   return ( p -> associationitem3_name );
531 }
532
533 char *pnd_pxml_get_associationitem3_filetype ( pnd_pxml_handle h ) {
534   pnd_pxml_t *p = (pnd_pxml_t*) h;
535   return ( p -> associationitem3_filetype );
536 }
537
538 char *pnd_pxml_get_associationitem3_parameter ( pnd_pxml_handle h ) {
539   pnd_pxml_t *p = (pnd_pxml_t*) h;
540   return ( p -> associationitem3_parameter );
541 }
542
543 char *pnd_pxml_get_clockspeed ( pnd_pxml_handle h ) {
544   pnd_pxml_t *p = (pnd_pxml_t*) h;
545   return ( p -> clockspeed );
546 }
547
548 char *pnd_pxml_get_background ( pnd_pxml_handle h ) {
549   pnd_pxml_t *p = (pnd_pxml_t*) h;
550   return ( p -> background );
551 }
552
553 char *pnd_pxml_get_startdir ( pnd_pxml_handle h ) {
554   pnd_pxml_t *p = (pnd_pxml_t*) h;
555   return ( p -> startdir );
556 }
557
558 char *pnd_pxml_get_mkdir ( pnd_pxml_handle h ) {
559   pnd_pxml_t *p = (pnd_pxml_t*) h;
560   return ( p -> mkdir_sp );
561 }
562
563 unsigned char pnd_pxml_is_affirmative ( char *v ) {
564
565   if ( ! v ) {
566     return ( 0 );
567   }
568
569   if ( ( v [ 0 ] == 'Y' ) ||
570        ( v [ 0 ] == 'y' ) ||
571        ( v [ 0 ] == '1' ) )
572   {
573     return ( 0 );
574   }
575
576   return ( 0 );
577 }
578
579 pnd_pxml_x11_req_e pnd_pxml_get_x11 ( char *pxmlvalue ) {
580
581   if ( ! pxmlvalue ) {
582     return ( pnd_pxml_x11_ignored );
583   } else if ( strcasecmp ( pxmlvalue, "req" ) == 0 ) {
584     return ( pnd_pxml_x11_required );
585   } else if ( strcasecmp ( pxmlvalue, "stop" ) == 0 ) {
586     return ( pnd_pxml_x11_stop );
587   } else if ( strcasecmp ( pxmlvalue, "ignore" ) == 0 ) {
588     return ( pnd_pxml_x11_ignored );
589   }
590
591   return ( pnd_pxml_x11_ignored ); // default
592 }
593
594 char *pnd_pxml_get_info_name ( pnd_pxml_handle h ) {
595   pnd_pxml_t *p = (pnd_pxml_t*) h;
596   return ( p -> info_name );
597 }
598
599 char *pnd_pxml_get_info_type ( pnd_pxml_handle h ) {
600   pnd_pxml_t *p = (pnd_pxml_t*) h;
601   return ( p -> info_type );
602 }
603
604 char *pnd_pxml_get_info_src ( pnd_pxml_handle h ) {
605   pnd_pxml_t *p = (pnd_pxml_t*) h;
606   return ( p -> info_filename );
607 }