Initial commit of libpnd 0.0.5 so we cna restart with GIT
[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
13 void pnd_pxml_load(const char* pFilename, pnd_pxml_t *app);
14
15 pnd_pxml_handle pnd_pxml_fetch ( char *fullpath ) {
16
17         pnd_pxml_t *p = malloc ( sizeof(pnd_pxml_t) );
18
19         pnd_pxml_load(fullpath, p);
20
21         return (p);
22 }
23
24 void pnd_pxml_delete ( pnd_pxml_handle h ) {
25   pnd_pxml_t *p = (pnd_pxml_t*) h;
26
27   if ( p -> title_en ) {
28     free ( p -> title_en );
29   }
30
31   if ( p -> icon ) {
32     free ( p -> icon );
33   }
34
35   if ( p -> exec ) {
36     free ( p -> exec );
37   }
38   if ( p -> main_category ) {
39     free ( p -> main_category );
40   }
41   if ( p -> unique_id ) {
42     free ( p -> unique_id );
43   }
44   if ( p -> clockspeed ) {
45     free ( p -> clockspeed );
46   }
47
48   return;
49 }
50
51 char *pnd_pxml_get_app_name ( pnd_pxml_handle h ) {
52   pnd_pxml_t *p = (pnd_pxml_t*) h;
53   return ( p -> title_en );
54 }
55
56 char *pnd_pxml_get_icon_path ( pnd_pxml_handle h ) {
57   pnd_pxml_t *p = (pnd_pxml_t*) h;
58   return ( p -> icon );
59 }
60
61 char *pnd_pxml_get_clockspeed ( pnd_pxml_handle h ) {
62   pnd_pxml_t *p = (pnd_pxml_t*) h;
63   return ( p -> clockspeed );
64 }
65
66 void pnd_pxml_set_app_name ( pnd_pxml_handle h, char *v ) {
67   pnd_pxml_t *p = (pnd_pxml_t*) h;
68   if ( p -> title_en ) {
69     free ( p -> title_en );
70     p -> title_en = NULL;
71   }
72
73   if ( v ) {
74     p -> title_en = strdup ( v );
75   }
76
77   return;
78 }
79
80 char *pnd_pxml_get_unique_id ( pnd_pxml_handle h ) {
81   pnd_pxml_t *p = (pnd_pxml_t*) h;
82   return ( p -> unique_id );
83 }
84
85 char *pnd_pxml_get_primary_category ( pnd_pxml_handle h ) {
86   pnd_pxml_t *p = (pnd_pxml_t*) h;
87   return ( p -> main_category );
88 }
89
90 char *pnd_pxml_get_exec_path ( pnd_pxml_handle h ) {
91   pnd_pxml_t *p = (pnd_pxml_t*) h;
92   return ( p -> exec );
93 }
94
95 unsigned char pnd_is_pxml_valid_app ( pnd_pxml_handle h ) {
96   pnd_pxml_t *p = (pnd_pxml_t*) h;
97
98   // for now, lets just verify the exec-path is valid
99   //printf ( "exec is '%s'\n", p -> exec );
100
101   struct stat buf;
102   if ( stat ( p -> exec, &buf ) == 0 ) {
103     return ( 1 ); // path is present
104   }
105
106   return ( 0 );
107 }
108
109 signed char pnd_pxml_merge_override ( pnd_pxml_handle h, char *searchpath ) {
110   // the pxml includes a unique-id; use this value to attempt to find an
111   // override in the given searchpath
112   signed char retval = 0;
113   pnd_pxml_handle mergeh;
114
115   SEARCHPATH_PRE
116   {
117
118     // do it
119     strncat ( buffer, "/", FILENAME_MAX );
120     strncat ( buffer, pnd_pxml_get_unique_id ( h ), FILENAME_MAX );
121     strncat ( buffer, ".xml", FILENAME_MAX );
122     //printf ( "  Path to seek merges: '%s'\n", buffer );
123
124     mergeh = pnd_pxml_fetch ( buffer );
125
126     if ( mergeh ) {
127
128       if ( pnd_pxml_get_app_name ( mergeh ) ) {
129         pnd_pxml_set_app_name ( h, pnd_pxml_get_app_name ( mergeh ) );
130       }
131
132       pnd_pxml_delete ( mergeh );
133     }
134
135   }
136   SEARCHPATH_POST
137
138   return ( retval );
139 }