Patch from Cloudef to add very basic support for 'package' PXML tags
[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         continue;
140       }
141
142       app->descriptions_c++;
143       if (app->descriptions_c > app->descriptions_alloc_c) //we don't have enough strings allocated
144       {
145         app->descriptions_alloc_c <<= 1;
146         app->descriptions = (pnd_localized_string_t*)realloc((void*)app->descriptions, app->descriptions_alloc_c * sizeof(pnd_localized_string_t) );
147         if (!app->descriptions) return (0); //errno = ENOMEM
148       }
149
150       pnd_localized_string_t *description = &app->descriptions[app->descriptions_c - 1];
151       description->language = lang;
152       description->string = text;
153
154       // next
155       pElem = pElem -> NextSiblingElement ( PND_PXML_ENAME_DESCRIPTION );
156
157     } // foreach
158
159   } else {
160     // fallback to older approach
161
162     for (pElem = hRoot.FirstChild(PND_PXML_ENAME_DESCRIPTION).Element(); pElem; 
163          pElem = pElem->NextSiblingElement(PND_PXML_ENAME_DESCRIPTION))
164     {
165
166       if ( ! pElem->GetText() ) {
167         continue;
168       }
169
170       char *text = strdup(pElem->GetText());
171       if (!text) continue;
172
173       char *lang = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_DESCRLANG);
174       if (!lang) continue;
175
176       app->descriptions_c++;
177       if (app->descriptions_c > app->descriptions_alloc_c) //we don't have enough strings allocated
178       {
179         app->descriptions_alloc_c <<= 1;
180         app->descriptions = (pnd_localized_string_t*)realloc((void*)app->descriptions, app->descriptions_alloc_c * sizeof(pnd_localized_string_t) );
181         if (!app->descriptions) return (0); //errno = ENOMEM
182       }
183
184       pnd_localized_string_t *description = &app->descriptions[app->descriptions_c - 1];
185       description->language = lang;
186       description->string = text;
187     } // for
188
189   } // new form or old form?
190
191   return ( 1 );
192 }
193
194 unsigned char pnd_pxml_parse ( const char *pFilename, char *buffer, unsigned int length, pnd_pxml_t **apps ) {
195
196   //Load the XML document
197   TiXmlDocument doc;
198   doc.Parse(buffer);
199
200   unsigned char appwrappermode = 0; // >=1 -> using <application>...</application> wrapper
201   unsigned char appcount = 0;
202   pnd_pxml_t *app = NULL;
203
204   TiXmlElement *pElem = NULL;
205   TiXmlElement *appElem = NULL;
206
207   //Find the root element
208   TiXmlHandle hDoc(&doc);
209   TiXmlHandle hRoot(0);
210
211   pElem = hDoc.FirstChild("PXML").Element();
212   if (!pElem) return (0);
213
214   // new Strategy; really, we want multiple app support within a PXML, without the lameness of
215   // multiple <PXML>..</PXML> within a single PXML.xml file. As such, we should have an app within
216   // an <application>..</application> wrapper level, with the ID on that. Further, the icon and
217   // .desktop filenames can have a # appended which is the application-count-# within the PXML,
218   // so that they can have their own .desktop and icon without collisions, but still use the
219   // same unique-id if they want to.
220   //   To avoid breaking existing PXML's (even though we're pre-launch), can detect if ID
221   // is present in PXML line or not; if not, assume application mode?
222   hRoot = TiXmlHandle(pElem);
223
224   // workaround for package ID's used by some package managers
225   // get the package ID and store it for each application
226   char* package_id = NULL;
227   pElem = hRoot.FirstChild ( PND_PXML_ENAME_PACKAGE ).Element();
228   if ( pElem ) {
229         package_id = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_ID );
230   }
231
232   // move to applications element then
233   if ( hRoot.FirstChild(PND_PXML_APP).Element() != NULL ) {
234     appwrappermode = 1;
235     appElem = hRoot.FirstChild(PND_PXML_APP).Element();
236     hRoot = TiXmlHandle ( appElem );
237   }
238
239   // until we run out of applications in the PXML..
240   while ( 1 ) {
241
242     //pnd_log ( PND_LOG_DEFAULT, (char*)"  App #%u inside of PXML %s\n", appcount, pFilename );
243
244     // create the buffer to hold the pxml
245     apps [ appcount ] = (pnd_pxml_t*) malloc ( sizeof(pnd_pxml_t) );
246     memset ( apps [ appcount ], '\0', sizeof(pnd_pxml_t) );
247
248     // due to old code, just make life easier a minute..
249     app = apps [ appcount ];
250     if ( appwrappermode ) {
251       app -> subapp_number = appcount;
252     } else {
253       app -> subapp_number = 0;
254     }
255     
256     // give application the package id, if there is one
257     app -> package_id = package_id;
258
259     //Get unique ID first.
260     if ( appwrappermode ) {
261       app->unique_id = pnd_pxml_get_attribute(appElem, PND_PXML_ATTRNAME_UID);
262       //pnd_log ( PND_LOG_DEFAULT, (char*)"  Subapp #%u has unique_id %s\n", appcount, app -> unique_id );
263       app->appdata_dirname = pnd_pxml_get_attribute(appElem, PND_PXML_ATTRNAME_APPDATANAME);
264     } else {
265       app->unique_id = pnd_pxml_get_attribute(hRoot.Element(), PND_PXML_ATTRNAME_UID);
266       //pnd_log ( PND_LOG_DEFAULT, (char*)"  Only-app #%u has unique_id %s\n", appcount, app -> unique_id );
267     }
268
269     //Everything related to the title:
270     pnd_pxml_parse_titles(hRoot, app);
271
272     //Everything description-related:
273     pnd_pxml_parse_descriptions(hRoot, app);
274
275     //Everything launcher-related in one tag:
276     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_EXEC).Element()) )
277      {
278        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.
279        app->standalone = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECSTAL);
280        app->exec       = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECCMD);
281        app->startdir   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECWD);
282        app->exec_no_x11     = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECNOX11);
283        app->execargs   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECARGS);
284      }
285
286     //The app icon:
287     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_ICON).Element()) ) {
288       app->icon       = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ICONSRC);
289     }
290
291     // <info>
292     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_INFO).Element()) )
293      {
294        app-> info_name = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFONAME );
295        app-> info_filename = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFOSRC );
296        app-> info_type = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFOTYPE );
297      }
298
299     //The preview pics:
300     if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_PREVPICS).Element()) )
301     {
302       //TODO: Change this if pnd_pxml_t gains the feature of more pics than 2.
303       if ( (pElem = pElem->FirstChildElement(PND_PXML_ENAME_PREVPIC)) )
304       {
305         app->previewpic1 = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_PREVPICSRC);
306
307         if ( (pElem = pElem->NextSiblingElement(PND_PXML_ENAME_PREVPIC)) )
308         {
309           app->previewpic2 = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_PREVPICSRC);
310         }
311       } 
312     } //previewpic
313
314     //The author info:
315     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_AUTHOR).Element()) )
316     {
317       app->author_name    = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHORNAME);
318       app->author_website = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHORWWW);
319       //TODO: Uncomment this if the author gets email support.
320       //app->author_email = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHOREMAIL));
321     }
322
323     //The version info:
324     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_VERSION).Element()) )
325     {
326       app->version_major   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMAJOR);
327       app->version_minor   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMINOR);
328       app->version_release = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERREL);
329       app->version_build   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERBUILD);
330     }
331
332     //The OS version info:
333     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_OSVERSION).Element()) )
334     {
335       app->osversion_major   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERMAJOR);
336       app->osversion_minor   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERMINOR);
337       app->osversion_release = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERREL);
338       app->osversion_build   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERBUILD);
339     }
340
341     int i; //For now, we need to keep track of the index of categories.
342     //Categories:
343     if ( (pElem = hRoot.FirstChildElement(PND_PXML_NODENAME_CATS).Element()) ) //First, enter the "categories" node.
344     {
345       i = 0;
346
347       //Goes through all the top-level categories and their sub-categories. i helps limit these to 2.
348       for (pElem = pElem->FirstChildElement(PND_PXML_ENAME_CAT); pElem && i < 2; 
349            pElem = pElem->NextSiblingElement(PND_PXML_ENAME_CAT), i++)
350       {
351         //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.
352         switch (i)
353         {
354         case 0: //first category counts as the main cat for now
355           app->main_category = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CATNAME);
356           break;
357
358         case 1: //...second as the alternative
359           app->altcategory = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CATNAME);
360         }
361
362         TiXmlElement *pSubCatElem; //the sub-elements for a main category.
363         int j = 0; //the subcategory index within this category
364
365         //Goes through all the subcategories within this category. j helps limit these to 2.
366         for (pSubCatElem = pElem->FirstChildElement(PND_PXML_ENAME_SUBCAT); pSubCatElem && j < 2;
367              pSubCatElem = pSubCatElem->NextSiblingElement(PND_PXML_ENAME_SUBCAT), j++)
368         {
369           char *subcat = pnd_pxml_get_attribute(pSubCatElem, PND_PXML_ATTRNAME_SUBCATNAME);
370           if (!(subcat)) continue;
371
372           //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.
373           switch (j | (i << 1))
374           {
375           case 0:
376             app->subcategory1 = subcat;
377             break;
378           case 1:
379             app->subcategory2 = subcat;
380             break;
381           case 2:
382             app->altsubcategory1 = subcat;
383             break;
384           case 3:
385             app->altsubcategory2 = subcat;
386           }
387         }
388       }
389     }
390
391     //All file associations:
392     //Step into the associations node
393     if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_ASSOCS).Element()) )
394     {
395       i = 0;
396       //Go through all associations. i serves as index; since the format only supports 3 associations we need to keep track of the number.
397       for (pElem = pElem->FirstChildElement(PND_PXML_ENAME_ASSOC); pElem && i < 3; 
398            pElem = pElem->NextSiblingElement(PND_PXML_ENAME_ASSOC), i++)
399       {
400         char *name = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCNAME);
401         char *filetype = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCFTYPE);
402         char *paramter = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCARGS);
403
404         if (!(name && filetype && paramter)) continue;
405
406         switch(i) //TODO: same problem here: only 3 associations supported
407         {
408         case 0:
409         {
410           app->associationitem1_name      = name;
411           app->associationitem1_filetype  = filetype;
412           app->associationitem1_parameter = paramter;
413           break;
414         }
415         case 1:
416         {
417           app->associationitem2_name      = name;
418           app->associationitem2_filetype  = filetype;
419           app->associationitem2_parameter = paramter;
420           break;
421         }
422         case 2:
423         {
424           app->associationitem3_name      = name;
425           app->associationitem3_filetype  = filetype;
426           app->associationitem3_parameter = paramter;
427         }
428         }
429       }
430     }
431
432     //Performance related things (aka: Clockspeed XD):
433     pElem = hRoot.FirstChild(PND_PXML_ENAME_CLOCK).Element();
434     if (pElem)
435     {   
436       app->clockspeed = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CLOCKFREQ);
437     }
438
439     // Package
440     pElem = hRoot.FirstChild ( PND_PXML_ENAME_PACKAGE ).Element();
441     if ( pElem ) {      
442       app -> package_name = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_NAME );
443       app -> package_release_date = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_DATE );
444     }
445
446     // mkdir request
447     if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_MKDIR).Element()) ) {
448
449       // seek <dir>
450       if ( (pElem = pElem->FirstChildElement(PND_PXML_ENAME_MKDIR)) ) {
451         char *t;
452
453         if ( ( t = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_MKDIRPATH) ) ) {
454           // first <dir>, so just replace it wholesale; we use strdup so we can free() easily later, consistently. Mmm, leak seems imminent.
455           app -> mkdir_sp = strdup ( t );
456         }
457
458         while ( ( pElem = pElem -> NextSiblingElement ( PND_PXML_ENAME_MKDIR ) ) ) {
459               
460           if ( ( t = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_MKDIRPATH) ) ) {
461             char *foo = (char*) malloc ( strlen ( app -> mkdir_sp ) + strlen ( t ) + 1 /*:*/ + 1 /*\0*/ );
462
463             if ( foo ) {
464               sprintf ( foo, "%s:%s", app -> mkdir_sp, t );
465               free ( app -> mkdir_sp );
466               app -> mkdir_sp = foo;
467             } // assuming we got ram, lets cat it all together
468
469           } // got another elem?
470
471         } // while
472
473       } // found a <dir>
474
475 #if 0
476       if ( app -> mkdir_sp ) {
477         printf ( "mkdir: %s\n", app -> mkdir_sp );
478       }
479 #endif
480
481     } // mkdir
482
483     // if in <application> mode, do we find another app?
484     if ( appwrappermode ) {
485       appElem = appElem -> NextSiblingElement ( PND_PXML_APP );
486       if ( ! appElem ) {
487         //pnd_log ( PND_LOG_DEFAULT, (char*)"  No more applications within PXML\n" );
488         break; // no more applications
489       }
490       // got another application..
491       //pnd_log ( PND_LOG_DEFAULT, "  Found another applications within PXML\n" );
492       appwrappermode++;
493       hRoot = TiXmlHandle ( appElem );
494
495       appcount++;
496
497       if ( appcount == PXML_MAXAPPS ) {
498         return ( 1 ); // thats all we can handle; we're not going to auto-extend this
499       }
500
501     } else {
502       break; // single-app old PXML
503     }
504
505   } // while finding apps
506
507   return (1);
508 }
509
510 } // extern C