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