3 #include "tinyxml/tinyxml.h"
4 #include "../include/pnd_pxml.h"
5 #include "pnd_tinyxml.h"
6 #include "pnd_logger.h"
8 //Easily change the tag names if required (globally in this file):
9 #include "pnd_pxml_names.h"
13 char *pnd_pxml_get_attribute(TiXmlElement *elem, const char *name)
15 const char *value = elem->Attribute(name);
22 unsigned char pnd_pxml_parse_titles(const TiXmlHandle hRoot, pnd_pxml_t *app) {
24 app->titles_alloc_c = 4; //TODO: adjust this based on how many titles a PXML usually has. Power of 2.
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
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
36 pElem = pElem -> FirstChildElement ( PND_PXML_ENAME_TITLE );
40 // handle <title lang="en_US">Program Title</title>
43 // parse out the text and lang
46 if ( ! ( text = strdup ( pElem -> GetText() ) ) ) {
50 if ( ! ( lang = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_TITLELANG ) ) ) {
54 // increment counter; if we're running out of buffers, grow to handle the new strings
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
64 // populate the stringbuf
65 pnd_localized_string_t *title = &app->titles[app->titles_c - 1];
66 title->language = lang;
70 pElem = pElem -> NextSiblingElement ( PND_PXML_ENAME_TITLE );
75 // older style <title> entry series
77 for ( pElem = hRoot.FirstChild(PND_PXML_ENAME_TITLE).Element(); pElem;
78 pElem = pElem->NextSiblingElement(PND_PXML_ENAME_TITLE))
81 if ( ! pElem->GetText() ) {
85 char *text = strdup(pElem->GetText());
88 char *lang = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_TITLELANG);
92 if (app->titles_c > app->titles_alloc_c) //we don't have enough strings allocated
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
99 pnd_localized_string_t *title = &app->titles[app->titles_c - 1];
100 title->language = lang;
101 title->string = text;
103 //pnd_log ( PND_LOG_DEFAULT, (char*)" Title/Lang: %s/%s\n", text, lang );
107 } // new or old style <title(s)>
112 unsigned char pnd_pxml_parse_descriptions(const TiXmlHandle hRoot, pnd_pxml_t *app) {
114 app->descriptions_alloc_c = 4; //TODO: adjust this based on how many descriptions a PXML usually has. Power of 2.
116 app->descriptions = (pnd_localized_string_t *)malloc(sizeof(pnd_localized_string_t) * app->descriptions_alloc_c);
117 if (!app->descriptions)
119 app->descriptions_alloc_c = 0;
120 return (0); //errno = NOMEM
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
128 pElem = pElem -> FirstChildElement ( PND_PXML_ENAME_DESCRIPTION );
134 if ( ! ( text = strdup ( pElem -> GetText() ) ) ) {
138 if ( ! ( lang = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_DESCRLANG ) ) ) {
139 if(text) free(text); text = NULL;
143 app->descriptions_c++;
144 if (app->descriptions_c > app->descriptions_alloc_c) //we don't have enough strings allocated
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
151 pnd_localized_string_t *description = &app->descriptions[app->descriptions_c - 1];
152 description->language = lang;
153 description->string = text;
156 pElem = pElem -> NextSiblingElement ( PND_PXML_ENAME_DESCRIPTION );
161 // fallback to older approach
163 for (pElem = hRoot.FirstChild(PND_PXML_ENAME_DESCRIPTION).Element(); pElem;
164 pElem = pElem->NextSiblingElement(PND_PXML_ENAME_DESCRIPTION))
167 if ( ! pElem->GetText() ) {
171 char *text = strdup(pElem->GetText());
174 char *lang = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_DESCRLANG);
175 if (!lang) { if(text) free(text); text = NULL; continue; }
177 app->descriptions_c++;
178 if (app->descriptions_c > app->descriptions_alloc_c) //we don't have enough strings allocated
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
185 pnd_localized_string_t *description = &app->descriptions[app->descriptions_c - 1];
186 description->language = lang;
187 description->string = text;
190 } // new form or old form?
195 unsigned char pnd_pxml_parse ( const char *pFilename, char *buffer, unsigned int length, pnd_pxml_t **apps ) {
197 //Load the XML document
201 unsigned char appwrappermode = 0; // >=1 -> using <application>...</application> wrapper
202 unsigned char appcount = 0;
203 pnd_pxml_t *app = NULL;
205 TiXmlElement *pElem = NULL;
206 TiXmlElement *appElem = NULL;
208 //Find the root element
209 TiXmlHandle hDoc(&doc);
210 TiXmlHandle hRoot(0);
212 pElem = hDoc.FirstChild("PXML").Element();
213 if (!pElem) return (0);
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);
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 pElem = hRoot.FirstChild ( PND_PXML_ENAME_PACKAGE ).Element();
230 package_id = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_ID );
233 // move to applications element then
234 if ( hRoot.FirstChild(PND_PXML_APP).Element() != NULL ) {
236 appElem = hRoot.FirstChild(PND_PXML_APP).Element();
237 hRoot = TiXmlHandle ( appElem );
240 // until we run out of applications in the PXML..
243 //pnd_log ( PND_LOG_DEFAULT, (char*)" App #%u inside of PXML %s\n", appcount, pFilename );
245 // create the buffer to hold the pxml
246 apps [ appcount ] = (pnd_pxml_t*) malloc ( sizeof(pnd_pxml_t) );
247 memset ( apps [ appcount ], '\0', sizeof(pnd_pxml_t) );
249 // due to old code, just make life easier a minute..
250 app = apps [ appcount ];
251 if ( appwrappermode ) {
252 app -> subapp_number = appcount;
254 app -> subapp_number = 0;
257 // give application the package id, if there is one
259 app -> package_id = strdup(package_id);
261 //Get unique ID first.
262 if ( appwrappermode ) {
263 app->unique_id = pnd_pxml_get_attribute(appElem, PND_PXML_ATTRNAME_UID);
264 //pnd_log ( PND_LOG_DEFAULT, (char*)" Subapp #%u has unique_id %s\n", appcount, app -> unique_id );
265 app->appdata_dirname = pnd_pxml_get_attribute(appElem, PND_PXML_ATTRNAME_APPDATANAME);
267 app->unique_id = pnd_pxml_get_attribute(hRoot.Element(), PND_PXML_ATTRNAME_UID);
268 //pnd_log ( PND_LOG_DEFAULT, (char*)" Only-app #%u has unique_id %s\n", appcount, app -> unique_id );
271 //Everything related to the title:
272 pnd_pxml_parse_titles(hRoot, app);
274 //Everything description-related:
275 pnd_pxml_parse_descriptions(hRoot, app);
277 //Everything launcher-related in one tag:
278 if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_EXEC).Element()) )
280 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.
281 app->standalone = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECSTAL);
282 app->exec = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECCMD);
283 app->startdir = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECWD);
284 app->exec_no_x11 = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECNOX11);
285 app->execargs = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_EXECARGS);
289 if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_ICON).Element()) ) {
290 app->icon = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ICONSRC);
294 if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_INFO).Element()) )
296 app-> info_name = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFONAME );
297 app-> info_filename = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFOSRC );
298 app-> info_type = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_INFOTYPE );
302 if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_PREVPICS).Element()) )
304 //TODO: Change this if pnd_pxml_t gains the feature of more pics than 2.
305 if ( (pElem = pElem->FirstChildElement(PND_PXML_ENAME_PREVPIC)) )
307 app->previewpic1 = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_PREVPICSRC);
309 if ( (pElem = pElem->NextSiblingElement(PND_PXML_ENAME_PREVPIC)) )
311 app->previewpic2 = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_PREVPICSRC);
317 if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_AUTHOR).Element()) )
319 app->author_name = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHORNAME);
320 app->author_website = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHORWWW);
321 //TODO: Uncomment this if the author gets email support.
322 //app->author_email = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_AUTHOREMAIL));
326 if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_VERSION).Element()) )
328 app->version_major = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMAJOR);
329 app->version_minor = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERMINOR);
330 app->version_release = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERREL);
331 app->version_build = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_VERBUILD);
334 //The OS version info:
335 if ( (pElem = hRoot.FirstChild(PND_PXML_ENAME_OSVERSION).Element()) )
337 app->osversion_major = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERMAJOR);
338 app->osversion_minor = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERMINOR);
339 app->osversion_release = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERREL);
340 app->osversion_build = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_OSVERBUILD);
343 int i; //For now, we need to keep track of the index of categories.
345 if ( (pElem = hRoot.FirstChildElement(PND_PXML_NODENAME_CATS).Element()) ) //First, enter the "categories" node.
349 //Goes through all the top-level categories and their sub-categories. i helps limit these to 2.
350 for (pElem = pElem->FirstChildElement(PND_PXML_ENAME_CAT); pElem && i < 2;
351 pElem = pElem->NextSiblingElement(PND_PXML_ENAME_CAT), i++)
353 //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.
356 case 0: //first category counts as the main cat for now
357 app->main_category = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CATNAME);
360 case 1: //...second as the alternative
361 app->altcategory = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CATNAME);
364 TiXmlElement *pSubCatElem; //the sub-elements for a main category.
365 int j = 0; //the subcategory index within this category
367 //Goes through all the subcategories within this category. j helps limit these to 2.
368 for (pSubCatElem = pElem->FirstChildElement(PND_PXML_ENAME_SUBCAT); pSubCatElem && j < 2;
369 pSubCatElem = pSubCatElem->NextSiblingElement(PND_PXML_ENAME_SUBCAT), j++)
371 char *subcat = pnd_pxml_get_attribute(pSubCatElem, PND_PXML_ATTRNAME_SUBCATNAME);
372 if (!(subcat)) continue;
374 //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.
375 switch (j | (i << 1))
378 app->subcategory1 = subcat;
381 app->subcategory2 = subcat;
384 app->altsubcategory1 = subcat;
387 app->altsubcategory2 = subcat;
393 //All file associations:
394 //Step into the associations node
395 if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_ASSOCS).Element()) )
398 //Go through all associations. i serves as index; since the format only supports 3 associations we need to keep track of the number.
399 for (pElem = pElem->FirstChildElement(PND_PXML_ENAME_ASSOC); pElem && i < 3;
400 pElem = pElem->NextSiblingElement(PND_PXML_ENAME_ASSOC), i++)
402 char *name = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCNAME);
403 char *filetype = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCFTYPE);
404 char *paramter = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_ASSOCARGS);
406 if (!(name && filetype && paramter)) continue;
408 switch(i) //TODO: same problem here: only 3 associations supported
412 app->associationitem1_name = name;
413 app->associationitem1_filetype = filetype;
414 app->associationitem1_parameter = paramter;
419 app->associationitem2_name = name;
420 app->associationitem2_filetype = filetype;
421 app->associationitem2_parameter = paramter;
426 app->associationitem3_name = name;
427 app->associationitem3_filetype = filetype;
428 app->associationitem3_parameter = paramter;
434 //Performance related things (aka: Clockspeed XD):
435 pElem = hRoot.FirstChild(PND_PXML_ENAME_CLOCK).Element();
438 app->clockspeed = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_CLOCKFREQ);
442 pElem = hRoot.FirstChild ( PND_PXML_ENAME_PACKAGE ).Element();
444 app -> package_name = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_NAME );
445 app -> package_release_date = pnd_pxml_get_attribute ( pElem, PND_PXML_ATTRNAME_PACKAGE_DATE );
449 if ( (pElem = hRoot.FirstChild(PND_PXML_NODENAME_MKDIR).Element()) ) {
452 if ( (pElem = pElem->FirstChildElement(PND_PXML_ENAME_MKDIR)) ) {
455 if ( ( t = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_MKDIRPATH) ) ) {
456 // first <dir>, so just replace it wholesale; we use strdup so we can free() easily later, consistently. Mmm, leak seems imminent.
457 app -> mkdir_sp = strdup ( t );
460 while ( ( pElem = pElem -> NextSiblingElement ( PND_PXML_ENAME_MKDIR ) ) ) {
462 if ( ( t = pnd_pxml_get_attribute(pElem, PND_PXML_ATTRNAME_MKDIRPATH) ) ) {
463 char *foo = (char*) malloc ( strlen ( app -> mkdir_sp ) + strlen ( t ) + 1 /*:*/ + 1 /*\0*/ );
466 sprintf ( foo, "%s:%s", app -> mkdir_sp, t );
467 free ( app -> mkdir_sp );
468 app -> mkdir_sp = foo;
469 } // assuming we got ram, lets cat it all together
471 } // got another elem?
478 if ( app -> mkdir_sp ) {
479 printf ( "mkdir: %s\n", app -> mkdir_sp );
485 // if in <application> mode, do we find another app?
486 if ( appwrappermode ) {
487 appElem = appElem -> NextSiblingElement ( PND_PXML_APP );
489 //pnd_log ( PND_LOG_DEFAULT, (char*)" No more applications within PXML\n" );
490 break; // no more applications
492 // got another application..
493 //pnd_log ( PND_LOG_DEFAULT, " Found another applications within PXML\n" );
495 hRoot = TiXmlHandle ( appElem );
499 if ( appcount == PXML_MAXAPPS ) {
500 return ( 1 ); // thats all we can handle; we're not going to auto-extend this
504 break; // single-app old PXML
507 } // while finding apps