pndnotifyd: fix some crashes
[pandora-libraries.git] / lib / pnd_tinyxml.cpp
1
2 #include <stdio.h>
3 #include "tinyxml/tinyxml.h"
4 #include "../include/pnd_pxml.h"
5 #include "pnd_tinyxml.h"
6 #include "pnd_logger.h"
7
8 //Easily change the tag names if required (globally in this file):
9 #include "pnd_pxml_names.h"
10
11 extern "C" {
12
13 char *pnd_pxml_get_attribute(TiXmlElement *elem, const char *name)
14 {
15         const char *value = elem->Attribute(name);
16         if (value)
17                 return strdup(value);
18         else
19                 return NULL;
20 }
21
22 unsigned char pnd_pxml_parse_titles(const TiXmlHandle hRoot, pnd_pxml_t *app) {
23   TiXmlElement *pElem;
24   app->titles_alloc_c = 4;  //TODO: adjust this based on how many titles a PXML usually has. Power of 2.
25
26   app->titles = (pnd_localized_string_t *)malloc(sizeof(pnd_localized_string_t) * app->titles_alloc_c);
27   if (!app->titles) return (0); //errno = NOMEM
28
29   // Go through all title tags and load them.
30   // - Check if newer style titles sub-block exists; if so, use that.
31   //   - if not, fall back to old style
32   //     - failing that, crash earth into sun
33   if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_TITLES).Element()) ) {
34     // newer <titles> block
35
36     pElem = pElem -> FirstChildElement ( PND_PXML_ENAME_TITLE );
37
38     while ( pElem ) {
39
40       // handle <title lang="en_US">Program Title</title>
41       //
42
43       // parse out the text and lang
44       char *text, *lang;
45
46       if ( ! ( text = strdup ( pElem -> GetText() ) ) ) {
47         continue;
48       }
49
50       if ( ! ( lang = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_TITLELANG ) ) ) {
51         continue;
52       }
53
54       // increment counter; if we're running out of buffers, grow to handle the new strings
55       app -> titles_c++;
56
57       if ( app -> titles_c > app -> titles_alloc_c ) {
58         // we don't have enough strings allocated
59         app -> titles_alloc_c <<= 1;
60         app -> titles = (pnd_localized_string_t *)realloc((void*)app->titles, app->titles_alloc_c);
61         if (!app->titles) return (0); //errno = ENOMEM
62       }
63
64       // populate the stringbuf
65       pnd_localized_string_t *title = &app->titles[app->titles_c - 1];
66       title->language = lang;
67       title->string = text;
68
69       // next
70       pElem = pElem -> NextSiblingElement ( PND_PXML_ENAME_TITLE );
71
72     } // foreach
73
74   } else {
75     // older style <title> entry series
76
77     for ( pElem = hRoot.FirstChild(PND_PXML_ENAME_TITLE).Element(); pElem;
78           pElem = pElem->NextSiblingElement(PND_PXML_ENAME_TITLE))
79     {
80
81       if ( ! pElem->GetText() ) {
82         continue;
83       }
84
85       char *text = strdup(pElem->GetText());
86       if (!text) continue;
87
88       char *lang = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_TITLELANG);
89       if (!lang) continue;
90
91       app->titles_c++;
92       if (app->titles_c > app->titles_alloc_c) //we don't have enough strings allocated
93       {
94         app->titles_alloc_c <<= 1;
95         app->titles = (pnd_localized_string_t *)realloc((void*)app->titles, app->titles_alloc_c);
96         if (!app->titles) return (0); //errno = ENOMEM
97       }
98
99       pnd_localized_string_t *title = &app->titles[app->titles_c - 1];
100       title->language = lang;
101       title->string = text;
102
103       //pnd_log ( PND_LOG_DEFAULT, (char*)"    Title/Lang: %s/%s\n", text, lang );
104
105     } // for
106
107   } // new or old style <title(s)>
108
109   return ( 1 );
110 }
111
112 unsigned char pnd_pxml_parse_descriptions(const TiXmlHandle hRoot, pnd_pxml_t *app) {
113   TiXmlElement *pElem;
114   app->descriptions_alloc_c = 4; //TODO: adjust this based on how many descriptions a PXML usually has. Power of 2.
115
116   app->descriptions = (pnd_localized_string_t *)malloc(sizeof(pnd_localized_string_t) * app->descriptions_alloc_c);
117   if (!app->descriptions)
118   {
119     app->descriptions_alloc_c = 0;
120     return (0); //errno = NOMEM
121   }
122
123   // similar logic to how <titles> or <title> is parsed
124   // - if <titles> block is found, use that; otherwise fall back to <title> deprecated form
125   if ( (pElem = hRoot.FirstChild ( PND_PXML_NODENAME_DESCRIPTIONS).Element() ) ) {
126     // newer style <descriptions> block
127
128     pElem = pElem -> FirstChildElement ( PND_PXML_ENAME_DESCRIPTION );
129
130     while ( pElem ) {
131
132       char *text, *lang;
133
134       if ( ! ( text = strdup ( pElem -> GetText() ) ) ) {
135         continue;
136       }
137
138       if ( ! ( lang = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_DESCRLANG ) ) ) {
139        if(text) free(text); text = NULL;
140         continue;
141       }
142
143       app->descriptions_c++;
144       if (app->descriptions_c > app->descriptions_alloc_c) //we don't have enough strings allocated
145       {
146         app->descriptions_alloc_c <<= 1;
147         app->descriptions = (pnd_localized_string_t*)realloc((void*)app->descriptions, app->descriptions_alloc_c * sizeof(pnd_localized_string_t) );
148         if (!app->descriptions) { if(text) free(text); if(lang) free(lang); return (0); } //errno = ENOMEM
149       }
150
151       pnd_localized_string_t *description = &app->descriptions[app->descriptions_c - 1];
152       description->language = lang;
153       description->string = text;
154
155       // next
156       pElem = pElem -> NextSiblingElement ( PND_PXML_ENAME_DESCRIPTION );
157
158     } // foreach
159
160   } else {
161     // fallback to older approach
162
163     for (pElem = hRoot.FirstChild(PND_PXML_ENAME_DESCRIPTION).Element(); pElem;
164          pElem = pElem->NextSiblingElement(PND_PXML_ENAME_DESCRIPTION))
165     {
166
167       if ( ! pElem->GetText() ) {
168         continue;
169       }
170
171       char *text = strdup(pElem->GetText());
172       if (!text) continue;
173
174       char *lang = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_DESCRLANG);
175       if (!lang) { if(text) free(text); text = NULL;  continue; }
176
177       app->descriptions_c++;
178       if (app->descriptions_c > app->descriptions_alloc_c) //we don't have enough strings allocated
179       {
180         app->descriptions_alloc_c <<= 1;
181         app->descriptions = (pnd_localized_string_t*)realloc((void*)app->descriptions, app->descriptions_alloc_c * sizeof(pnd_localized_string_t) );
182         if (!app->descriptions) { if(text) free(text); if(lang) free(lang); return (0); } //errno = ENOMEM
183       }
184
185       pnd_localized_string_t *description = &app->descriptions[app->descriptions_c - 1];
186       description->language = lang;
187       description->string = text;
188     } // for
189
190   } // new form or old form?
191
192   return ( 1 );
193 }
194
195 unsigned char pnd_pxml_parse ( const char *pFilename, char *buffer, unsigned int length, pnd_pxml_t **apps ) {
196
197   //Load the XML document
198   TiXmlDocument doc;
199   doc.Parse(buffer);
200
201   unsigned char appwrappermode = 0; // >=1 -> using <application>...</application> wrapper
202   unsigned char appcount = 0;
203   pnd_pxml_t *app = NULL;
204
205   TiXmlElement *pElem = NULL;
206   TiXmlElement *appElem = NULL;
207
208   //Find the root element
209   TiXmlHandle hDoc(&doc);
210   TiXmlHandle hRoot(0);
211
212   pElem = hDoc.FirstChild("PXML").Element();
213   if (!pElem) return (0);
214
215   // new Strategy; really, we want multiple app support within a PXML, without the lameness of
216   // multiple <PXML>..</PXML> within a single PXML.xml file. As such, we should have an app within
217   // an <application>..</application> wrapper level, with the ID on that. Further, the icon and
218   // .desktop filenames can have a # appended which is the application-count-# within the PXML,
219   // so that they can have their own .desktop and icon without collisions, but still use the
220   // same unique-id if they want to.
221   //   To avoid breaking existing PXML's (even though we're pre-launch), can detect if ID
222   // is present in PXML line or not; if not, assume application mode?
223   hRoot = TiXmlHandle(pElem);
224
225   // workaround for package ID's used by some package managers
226   // get the package ID and store it for each application
227   char* package_id = NULL;
228   char* package_version_major = NULL;
229   char* package_version_minor = NULL;
230   char* package_version_release = NULL;
231   char* package_version_build = NULL;
232   pElem = hRoot.FirstChild ( PND_PXML_ENAME_PACKAGE ).Element();
233   if ( pElem ) {
234         package_id = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_ID );
235    TiXmlHandle pRoot = TiXmlHandle( pElem );
236    if ( (pElem = pRoot.FirstChild(PND_PXML_ENAME_VERSION).Element()) )
237    {
238       package_version_major   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMAJOR);
239       package_version_minor   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMINOR);
240       package_version_release = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERREL);
241       package_version_build   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERBUILD);
242    }
243   }
244
245   // move to applications element then
246   if ( hRoot.FirstChild(PND_PXML_APP).Element() != NULL ) {
247     appwrappermode = 1;
248     appElem = hRoot.FirstChild(PND_PXML_APP).Element();
249     hRoot = TiXmlHandle ( appElem );
250   }
251
252   // until we run out of applications in the PXML..
253   while ( 1 ) {
254
255     //pnd_log ( PND_LOG_DEFAULT, (char*)"  App #%u inside of PXML %s\n", appcount, pFilename );
256
257     // create the buffer to hold the pxml
258     apps [ appcount ] = (pnd_pxml_t*) malloc ( sizeof(pnd_pxml_t) );
259     memset ( apps [ appcount ], '\0', sizeof(pnd_pxml_t) );
260
261     // due to old code, just make life easier a minute..
262     app = apps [ appcount ];
263     if ( appwrappermode ) {
264       app -> subapp_number = appcount;
265     } else {
266       app -> subapp_number = 0;
267     }
268
269     // give application the package id, if there is one
270     if( package_id )
271       app -> package_id               = strdup(package_id);
272     if( package_version_major )
273       app -> package_version_major    = strdup(package_version_major);
274     if( package_version_minor )
275       app -> package_version_minor    = strdup(package_version_minor);
276     if( package_version_release )
277       app -> package_version_release  = strdup(package_version_release);
278     if( package_version_build )
279       app -> package_version_build    = strdup(package_version_build);
280
281     //Get unique ID first.
282     if ( appwrappermode ) {
283       app->unique_id = pnd_pxml_get_attribute(appElem, PND_PXML_ATTRNAME_UID);
284       //pnd_log ( PND_LOG_DEFAULT, (char*)"  Subapp #%u has unique_id %s\n", appcount, app -> unique_id );
285       app->appdata_dirname = pnd_pxml_get_attribute(appElem, PND_PXML_ATTRNAME_APPDATANAME);
286     } else {
287       app->unique_id = pnd_pxml_get_attribute(hRoot.Element(), PND_PXML_ATTRNAME_UID);
288       //pnd_log ( PND_LOG_DEFAULT, (char*)"  Only-app #%u has unique_id %s\n", appcount, app -> unique_id );
289     }
290
291     //Everything related to the title:
292     pnd_pxml_parse_titles(hRoot, app);
293
294     //Everything description-related:
295     pnd_pxml_parse_descriptions(hRoot, app);
296
297     //Everything launcher-related in one tag:
298     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_EXEC).Element()) )
299      {
300        app->background = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECBG); //if this returns NULL, the struct is filled with NULL. No need to check.
301        app->standalone = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECSTAL);
302        app->exec       = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECCMD);
303        app->startdir   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECWD);
304        app->exec_no_x11         = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECNOX11);
305        app->execargs   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECARGS);
306        app->exec_dashdash_args  = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_DASHDASH);
307      }
308
309     //The app icon:
310     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_ICON).Element()) ) {
311       app->icon       = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ICONSRC);
312     }
313
314     // <info>
315     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_INFO).Element()) )
316      {
317        app-> info_name = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFONAME );
318        app-> info_filename = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFOSRC );
319        app-> info_type = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFOTYPE );
320      }
321
322     //The preview pics:
323     if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_PREVPICS).Element()) )
324     {
325       //TODO: Change this if pnd_pxml_t gains the feature of more pics than 2.
326       if ( (pElem = pElem->FirstChildElement(PND_PXML_ENAME_PREVPIC)) )
327       {
328         app->previewpic1 = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_PREVPICSRC);
329
330         if ( (pElem = pElem->NextSiblingElement(PND_PXML_ENAME_PREVPIC)) )
331         {
332           app->previewpic2 = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_PREVPICSRC);
333         }
334       }
335     } //previewpic
336
337     //The author info:
338     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_AUTHOR).Element()) )
339     {
340       app->author_name    = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHORNAME);
341       app->author_website = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHORWWW);
342       //TODO: Uncomment this if the author gets email support.
343       //app->author_email = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHOREMAIL));
344     }
345
346     //The version info:
347     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_VERSION).Element()) )
348     {
349       app->version_major   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMAJOR);
350       app->version_minor   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMINOR);
351       app->version_release = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERREL);
352       app->version_build   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERBUILD);
353     }
354
355     //The OS version info:
356     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_OSVERSION).Element()) )
357     {
358       app->osversion_major   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERMAJOR);
359       app->osversion_minor   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERMINOR);
360       app->osversion_release = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERREL);
361       app->osversion_build   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERBUILD);
362     }
363
364     int i; //For now, we need to keep track of the index of categories.
365     //Categories:
366     if ( (pElem = hRoot.FirstChildElement(PND_PXML_NODENAME_CATS).Element()) ) //First, enter the "categories" node.
367     {
368       i = 0;
369
370       //Goes through all the top-level categories and their sub-categories. i helps limit these to 2.
371       for (pElem = pElem->FirstChildElement(PND_PXML_ENAME_CAT); pElem && i < 2;
372            pElem = pElem->NextSiblingElement(PND_PXML_ENAME_CAT), i++)
373       {
374         //TODO: Fix pnd_pxml_t so that there can be more than 2 category 'trees' and more than 2 subcategories. Then this can be removed.
375         switch (i)
376         {
377         case 0: //first category counts as the main cat for now
378           app->main_category = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CATNAME);
379           break;
380
381         case 1: //...second as the alternative
382           app->altcategory = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CATNAME);
383         }
384
385         TiXmlElement *pSubCatElem; //the sub-elements for a main category.
386         int j = 0; //the subcategory index within this category
387
388         //Goes through all the subcategories within this category. j helps limit these to 2.
389         for (pSubCatElem = pElem->FirstChildElement(PND_PXML_ENAME_SUBCAT); pSubCatElem && j < 2;
390              pSubCatElem = pSubCatElem->NextSiblingElement(PND_PXML_ENAME_SUBCAT), j++)
391         {
392           char *subcat = pnd_pxml_get_attribute(pSubCatElem, PND_PXML_ATTRNAME_SUBCATNAME);
393           if (!(subcat)) continue;
394
395           //TODO: This is ugly. Fix pnd_pxml_t so that there can be more than 2 category 'trees' and more than 2 subcategories. Then this can be removed.
396           switch (j | (i << 1))
397           {
398           case 0:
399             app->subcategory1 = subcat;
400             break;
401           case 1:
402             app->subcategory2 = subcat;
403             break;
404           case 2:
405             app->altsubcategory1 = subcat;
406             break;
407           case 3:
408             app->altsubcategory2 = subcat;
409           }
410         }
411       }
412     }
413
414     //All file associations:
415 #if 1
416     //Step into the associations node
417     if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_ASSOCS).Element()) )
418     {
419       i = 0;
420
421       for (pElem = pElem->FirstChildElement(PND_PXML_ENAME_ASSOC); pElem && i < 50;
422            pElem = pElem->NextSiblingElement(PND_PXML_ENAME_ASSOC), i++)
423       {
424         char *name = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCNAME);
425         char *filetype = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCFTYPE);
426         unsigned int maxlen;
427
428         if  ( ! ( name && filetype ) ) {
429           if ( name )     free(name);
430           if ( filetype ) free(filetype);
431           continue;
432         }
433
434         if ( app -> associationitem1_name == NULL ) {
435           // first hit, just dupe
436           app -> associationitem1_name      = strdup ( name );
437           app -> associationitem1_filetype  = strdup ( filetype );
438           pnd_log ( PND_LOG_DEFAULT, (char*)"  File assoc initial: %s -> %s\n", name, filetype );
439         } else {
440           // grow-append buffer
441
442           maxlen = strlen ( app -> associationitem1_name ) + strlen ( name ) + 3;
443           app -> associationitem1_name = (char*) realloc ( app -> associationitem1_name, maxlen );
444           strncat ( app -> associationitem1_name, "; ", maxlen );
445           strncat ( app -> associationitem1_name, name, maxlen );
446
447           maxlen = strlen ( app -> associationitem1_filetype ) + strlen ( filetype ) + 3;
448           app -> associationitem1_filetype = (char*) realloc ( app -> associationitem1_filetype, maxlen );
449           strncat ( app -> associationitem1_filetype, "; ", maxlen );
450           strncat ( app -> associationitem1_filetype, filetype, maxlen );
451
452           pnd_log ( PND_LOG_DEFAULT, (char*)"  File assoc growpend: %s -> %s\n", name, filetype );
453         }
454
455         if ( name )     free(name);
456         if ( filetype ) free(filetype);
457
458       } // for
459     } // assoc
460 #endif
461
462     //Performance related things (aka: Clockspeed XD):
463     pElem = hRoot.FirstChild(PND_PXML_ENAME_CLOCK).Element();
464     if (pElem)
465     {
466       app->clockspeed = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CLOCKFREQ);
467     }
468
469     // Package
470     pElem = hRoot.FirstChild ( PND_PXML_ENAME_PACKAGE ).Element();
471     if ( pElem ) {
472       app -> package_name = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_NAME );
473       app -> package_release_date = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_DATE );
474     }
475
476     // mkdir request
477     if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_MKDIR).Element()) ) {
478
479       // seek <dir>
480       if ( (pElem = pElem->FirstChildElement(PND_PXML_ENAME_MKDIR)) ) {
481         char *t;
482
483         if ( ( t = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_MKDIRPATH) ) ) {
484           // first <dir>, so just replace it wholesale; we use strdup so we can free() easily later, consistently. Mmm, leak seems imminent.
485           app -> mkdir_sp = strdup ( t );
486          free(t); // free this attribute
487         }
488
489         while ( ( pElem = pElem -> NextSiblingElement ( PND_PXML_ENAME_MKDIR ) ) ) {
490
491           if ( ( t = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_MKDIRPATH) ) ) {
492             char *foo = (char*) malloc ( strlen ( app -> mkdir_sp ) + strlen ( t ) + 1 /*:*/ + 1 /*\0*/ );
493
494             if ( foo ) {
495               sprintf ( foo, "%s:%s", app -> mkdir_sp, t );
496               free ( app -> mkdir_sp );
497               app -> mkdir_sp = foo;
498             } // assuming we got ram, lets cat it all together
499
500            free(t); // free this attribute
501           } // got another elem?
502
503         } // while
504
505       } // found a <dir>
506
507 #if 0
508       if ( app -> mkdir_sp ) {
509         printf ( "mkdir: %s\n", app -> mkdir_sp );
510       }
511 #endif
512
513     } // mkdir
514
515     // if in <application> mode, do we find another app?
516     if ( appwrappermode ) {
517       appElem = appElem -> NextSiblingElement ( PND_PXML_APP );
518       if ( ! appElem ) {
519         //pnd_log ( PND_LOG_DEFAULT, (char*)"  No more applications within PXML\n" );
520         break; // no more applications
521       }
522       // got another application..
523       //pnd_log ( PND_LOG_DEFAULT, "  Found another applications within PXML\n" );
524       appwrappermode++;
525       hRoot = TiXmlHandle ( appElem );
526
527       appcount++;
528
529       if ( appcount == PXML_MAXAPPS ) {
530         return ( 1 ); // thats all we can handle; we're not going to auto-extend this
531       }
532
533     } else {
534       break; // single-app old PXML
535     }
536
537   } // while finding apps
538
539   if( package_id )
540      free(package_id);
541   if( package_version_major )
542      free(package_version_major);
543   if( package_version_minor )
544      free(package_version_minor);
545   if( package_version_release )
546      free(package_version_release);
547   if( package_version_build )
548      free(package_version_build);
549
550   return (1);
551 }
552
553 } // extern C