Stick <info> element into PXML.xml .. parser, disco-t, pxml-t
[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   for (pElem = hRoot.FirstChild(PND_PXML_ENAME_TITLE).Element(); pElem;
31        pElem = pElem->NextSiblingElement(PND_PXML_ENAME_TITLE))
32   {
33
34     if ( ! pElem->GetText() ) {
35       continue;
36     }
37
38     char *text = strdup(pElem->GetText());
39     if (!text) continue;
40
41     char *lang = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_TITLELANG);
42     if (!lang) continue;
43
44     app->titles_c++;
45     if (app->titles_c > app->titles_alloc_c) //we don't have enough strings allocated
46     {
47       app->titles_alloc_c <<= 1;
48       app->titles = (pnd_localized_string_t *)realloc((void*)app->titles, app->titles_alloc_c);
49       if (!app->titles) return (0); //errno = ENOMEM
50     }
51
52     pnd_localized_string_t *title = &app->titles[app->titles_c - 1];
53     title->language = lang;
54     title->string = text;
55
56     //pnd_log ( PND_LOG_DEFAULT, (char*)"    Title/Lang: %s/%s\n", text, lang );
57
58   }
59
60   return ( 1 );
61 }
62
63 unsigned char pnd_pxml_parse_descriptions(const TiXmlHandle hRoot, pnd_pxml_t *app)
64 {
65         TiXmlElement *pElem;
66         app->descriptions_alloc_c = 4; //TODO: adjust this based on how many descriptions a PXML usually has. Power of 2.
67
68         app->descriptions = (pnd_localized_string_t *)malloc(sizeof(pnd_localized_string_t) * app->descriptions_alloc_c);
69         if (!app->descriptions) 
70         {
71                 app->descriptions_alloc_c = 0;
72                 return (0); //errno = NOMEM
73         }
74
75         for (pElem = hRoot.FirstChild(PND_PXML_ENAME_DESCRIPTION).Element(); pElem; 
76                 pElem = pElem->NextSiblingElement(PND_PXML_ENAME_DESCRIPTION))
77         {
78
79           if ( ! pElem->GetText() ) {
80             continue;
81           }
82
83                 char *text = strdup(pElem->GetText());
84                 if (!text) continue;
85
86                 char *lang = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_DESCRLANG);
87                 if (!lang) continue;
88
89                 app->descriptions_c++;
90                 if (app->descriptions_c > app->descriptions_alloc_c) //we don't have enough strings allocated
91                 {
92                         app->descriptions_alloc_c <<= 1;
93                         app->descriptions = (pnd_localized_string_t*)realloc((void*)app->descriptions, app->descriptions_alloc_c);
94                         if (!app->descriptions) return (0); //errno = ENOMEM
95                 }
96
97                 pnd_localized_string_t *description = &app->descriptions[app->descriptions_c - 1];
98                 description->language = lang;
99                 description->string = text;
100         }
101         return (1);
102 }
103
104 unsigned char pnd_pxml_parse ( const char *pFilename, char *buffer, unsigned int length, pnd_pxml_t **apps ) {
105
106   //Load the XML document
107   TiXmlDocument doc;
108   doc.Parse(buffer);
109
110   unsigned char appwrappermode = 0; // >=1 -> using <application>...</application> wrapper
111   unsigned char appcount = 0;
112   pnd_pxml_t *app = NULL;
113
114   TiXmlElement *pElem = NULL;
115   TiXmlElement *appElem = NULL;
116
117   //Find the root element
118   TiXmlHandle hDoc(&doc);
119   TiXmlHandle hRoot(0);
120
121   pElem = hDoc.FirstChild("PXML").Element();
122   if (!pElem) return (0);
123
124   // new Strategy; really, we want multiple app support within a PXML, without the lameness of
125   // multiple <PXML>..</PXML> within a single PXML.xml file. As such, we should have an app within
126   // an <application>..</application> wrapper level, with the ID on that. Further, the icon and
127   // .desktop filenames can have a # appended which is the application-count-# within the PXML,
128   // so that they can have their own .desktop and icon without collisions, but still use the
129   // same unique-id if they want to.
130   //   To avoid breaking existing PXML's (even though we're pre-launch), can detect if ID
131   // is present in PXML line or not; if not, assume application mode?
132   hRoot = TiXmlHandle(pElem);
133
134   if ( hRoot.FirstChild(PND_PXML_APP).Element() != NULL ) {
135     appwrappermode = 1;
136     appElem = hRoot.FirstChild(PND_PXML_APP).Element();
137     hRoot = TiXmlHandle ( appElem );
138   }
139
140   // until we run out of applications in the PXML..
141   while ( 1 ) {
142
143     //pnd_log ( PND_LOG_DEFAULT, (char*)"  App #%u inside of PXML %s\n", appcount, pFilename );
144
145     // create the buffer to hold the pxml
146     apps [ appcount ] = (pnd_pxml_t*) malloc ( sizeof(pnd_pxml_t) );
147     memset ( apps [ appcount ], '\0', sizeof(pnd_pxml_t) );
148
149     // due to old code, just make life easier a minute..
150     app = apps [ appcount ];
151     if ( appwrappermode ) {
152       app -> subapp_number = appcount;
153     } else {
154       app -> subapp_number = 0;
155     }
156
157     //Get unique ID first.
158     if ( appwrappermode ) {
159       app->unique_id = pnd_pxml_get_attribute(appElem, PND_PXML_ATTRNAME_UID);
160       //pnd_log ( PND_LOG_DEFAULT, (char*)"  Subapp #%u has unique_id %s\n", appcount, app -> unique_id );
161     } else {
162       app->unique_id = pnd_pxml_get_attribute(hRoot.Element(), PND_PXML_ATTRNAME_UID);
163       //pnd_log ( PND_LOG_DEFAULT, (char*)"  Only-app #%u has unique_id %s\n", appcount, app -> unique_id );
164     }
165
166     //Everything related to the title:
167     pnd_pxml_parse_titles(hRoot, app);
168
169     //Everything description-related:
170     pnd_pxml_parse_descriptions(hRoot, app);
171
172     //Everything launcher-related in one tag:
173     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_EXEC).Element()) )
174      {
175        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.
176        app->standalone = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECSTAL);
177        app->exec       = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECCMD);
178        app->startdir   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECWD);
179        app->exec_no_x11     = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECNOX11);
180        app->execargs   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECARGS);
181      }
182
183     //The app icon:
184     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_ICON).Element()) ) {
185       app->icon       = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ICONSRC);
186     }
187
188     // <info>
189     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_INFO).Element()) )
190      {
191        app-> info_name = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFONAME );
192        app-> info_filename = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFOSRC );
193        app-> info_type = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFOTYPE );
194      }
195
196     //The preview pics:
197     if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_PREVPICS).Element()) )
198     {
199       //TODO: Change this if pnd_pxml_t gains the feature of more pics than 2.
200       if ( (pElem = pElem->FirstChildElement(PND_PXML_ENAME_PREVPIC)) )
201       {
202         app->previewpic1 = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_PREVPICSRC);
203
204         if ( (pElem = pElem->NextSiblingElement(PND_PXML_ENAME_PREVPIC)) )
205         {
206           app->previewpic2 = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_PREVPICSRC);
207         }
208       } 
209     } //previewpic
210
211     //The author info:
212     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_AUTHOR).Element()) )
213     {
214       app->author_name    = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHORNAME);
215       app->author_website = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHORWWW);
216       //TODO: Uncomment this if the author gets email support.
217       //app->author_email = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHOREMAIL));
218     }
219
220     //The version info:
221     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_VERSION).Element()) )
222     {
223       app->version_major   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMAJOR);
224       app->version_minor   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMINOR);
225       app->version_release = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERREL);
226       app->version_build   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERBUILD);
227     }
228
229     //The OS version info:
230     if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_OSVERSION).Element()) )
231     {
232       app->osversion_major   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERMAJOR);
233       app->osversion_minor   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERMINOR);
234       app->osversion_release = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERREL);
235       app->osversion_build   = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERBUILD);
236     }
237
238     int i; //For now, we need to keep track of the index of categories.
239     //Categories:
240     if ( (pElem = hRoot.FirstChildElement(PND_PXML_NODENAME_CATS).Element()) ) //First, enter the "categories" node.
241     {
242       i = 0;
243
244       //Goes through all the top-level categories and their sub-categories. i helps limit these to 2.
245       for (pElem = pElem->FirstChildElement(PND_PXML_ENAME_CAT); pElem && i < 2; 
246            pElem = pElem->NextSiblingElement(PND_PXML_ENAME_CAT), i++)
247       {
248         //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.
249         switch (i)
250         {
251         case 0: //first category counts as the main cat for now
252           app->main_category = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CATNAME);
253           break;
254
255         case 1: //...second as the alternative
256           app->altcategory = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CATNAME);
257         }
258
259         TiXmlElement *pSubCatElem; //the sub-elements for a main category.
260         int j = 0; //the subcategory index within this category
261
262         //Goes through all the subcategories within this category. j helps limit these to 2.
263         for (pSubCatElem = pElem->FirstChildElement(PND_PXML_ENAME_SUBCAT); pSubCatElem && j < 2;
264              pSubCatElem = pSubCatElem->NextSiblingElement(PND_PXML_ENAME_SUBCAT), j++)
265         {
266           char *subcat = pnd_pxml_get_attribute(pSubCatElem, PND_PXML_ATTRNAME_SUBCATNAME);
267           if (!(subcat)) continue;
268
269           //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.
270           switch (j | (i << 1))
271           {
272           case 0:
273             app->subcategory1 = subcat;
274             break;
275           case 1:
276             app->subcategory2 = subcat;
277             break;
278           case 2:
279             app->altsubcategory1 = subcat;
280             break;
281           case 3:
282             app->altsubcategory2 = subcat;
283           }
284         }
285       }
286     }
287
288     //All file associations:
289     //Step into the associations node
290     if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_ASSOCS).Element()) )
291     {
292       i = 0;
293       //Go through all associations. i serves as index; since the format only supports 3 associations we need to keep track of the number.
294       for (pElem = pElem->FirstChildElement(PND_PXML_ENAME_ASSOC); pElem && i < 3; 
295            pElem = pElem->NextSiblingElement(PND_PXML_ENAME_ASSOC), i++)
296       {
297         char *name = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCNAME);
298         char *filetype = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCFTYPE);
299         char *paramter = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCARGS);
300
301         if (!(name && filetype && paramter)) continue;
302
303         switch(i) //TODO: same problem here: only 3 associations supported
304         {
305         case 0:
306         {
307           app->associationitem1_name      = name;
308           app->associationitem1_filetype  = filetype;
309           app->associationitem1_parameter = paramter;
310           break;
311         }
312         case 1:
313         {
314           app->associationitem2_name      = name;
315           app->associationitem2_filetype  = filetype;
316           app->associationitem2_parameter = paramter;
317           break;
318         }
319         case 2:
320         {
321           app->associationitem3_name      = name;
322           app->associationitem3_filetype  = filetype;
323           app->associationitem3_parameter = paramter;
324         }
325         }
326       }
327     }
328
329     //Performance related things (aka: Clockspeed XD):
330     pElem = hRoot.FirstChild(PND_PXML_ENAME_CLOCK).Element();
331     if (pElem)
332     {   
333       app->clockspeed = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CLOCKFREQ);
334     }
335
336     // Package
337     pElem = hRoot.FirstChild ( PND_PXML_ENAME_PACKAGE ).Element();
338     if ( pElem ) {      
339       app -> package_name = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_NAME );
340       app -> package_release_date = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_DATE );
341     }
342
343     // mkdir request
344     if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_MKDIR).Element()) ) {
345
346       // seek <dir>
347       if ( (pElem = pElem->FirstChildElement(PND_PXML_ENAME_MKDIR)) ) {
348         char *t;
349
350         if ( ( t = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_MKDIRPATH) ) ) {
351           // first <dir>, so just replace it wholesale; we use strdup so we can free() easily later, consistently. Mmm, leak seems imminent.
352           app -> mkdir_sp = strdup ( t );
353         }
354
355         while ( ( pElem = pElem -> NextSiblingElement ( PND_PXML_ENAME_MKDIR ) ) ) {
356               
357           if ( ( t = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_MKDIRPATH) ) ) {
358             char *foo = (char*) malloc ( strlen ( app -> mkdir_sp ) + strlen ( t ) + 1 /*:*/ + 1 /*\0*/ );
359
360             if ( foo ) {
361               sprintf ( foo, "%s:%s", app -> mkdir_sp, t );
362               free ( app -> mkdir_sp );
363               app -> mkdir_sp = foo;
364             } // assuming we got ram, lets cat it all together
365
366           } // got another elem?
367
368         } // while
369
370       } // found a <dir>
371
372 #if 0
373       if ( app -> mkdir_sp ) {
374         printf ( "mkdir: %s\n", app -> mkdir_sp );
375       }
376 #endif
377
378     } // mkdir
379
380     // if in <application> mode, do we find another app?
381     if ( appwrappermode ) {
382       appElem = appElem -> NextSiblingElement ( PND_PXML_APP );
383       if ( ! appElem ) {
384         //pnd_log ( PND_LOG_DEFAULT, (char*)"  No more applications within PXML\n" );
385         break; // no more applications
386       }
387       // got another application..
388       //pnd_log ( PND_LOG_DEFAULT, "  Found another applications within PXML\n" );
389       appwrappermode++;
390       hRoot = TiXmlHandle ( appElem );
391
392       appcount++;
393
394       if ( appcount == PXML_MAXAPPS ) {
395         return ( 1 ); // thats all we can handle; we're not going to auto-extend this
396       }
397
398     } else {
399       break; // single-app old PXML
400     }
401
402   } // while finding apps
403
404   return (1);
405 }
406
407 } // extern C