Created pnd_desktop.[ch] pair, to contain relocated dotdesktop and icon stuff
[pandora-libraries.git] / lib / pnd_discovery.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <unistd.h> /* for unlink */
5
6 #define __USE_GNU /* for strcasestr */
7 #include <string.h> /* for making ftw.h happy */
8
9 #define _XOPEN_SOURCE 500
10 #define __USE_XOPEN_EXTENDED
11 #include <ftw.h> /* for nftw, tree walker */
12
13 #include "pnd_container.h"
14 #include "pnd_pxml.h"
15 #include "pnd_discovery.h"
16 #include "pnd_pathiter.h"
17 #include "pnd_apps.h"
18 #include "pnd_pndfiles.h"
19
20 // need these 'globals' due to the way nftw and ftw work :/
21 static pnd_box_handle disco_box;
22 static char *disco_overrides = NULL;
23
24 void pnd_disco_destroy ( pnd_disco_t *p ) {
25
26   if ( p -> title_en ) {
27     free ( p -> title_en );
28   }
29
30   if ( p -> icon ) {
31     free ( p -> icon );
32   }
33
34   if ( p -> exec ) {
35     free ( p -> exec );
36   }
37
38   if ( p -> unique_id ) {
39     free ( p -> unique_id );
40   }
41
42   if ( p -> main_category ) {
43     free ( p -> main_category );
44   }
45
46   if ( p -> clockspeed ) {
47     free ( p -> clockspeed );
48   }
49
50   return;
51 }
52
53 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
54                                 int typeflag, struct FTW *ftwbuf )
55 {
56   unsigned char valid = pnd_object_type_unknown;
57   pnd_pxml_handle pxmlh = 0;
58   unsigned int pxml_close_pos = 0;
59
60   //printf ( "disco root callback encountered '%s'\n", fpath );
61
62   // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
63   if ( typeflag & FTW_D ) {
64     //printf ( " .. is dir, skipping\n" );
65     return ( 0 ); // skip directories and other non-regular files
66   }
67
68   // PND/PNZ file and others may be valid as well .. but lets leave that for now
69   //   printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
70   if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
71     valid = pnd_object_type_directory;
72   } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
73     valid = pnd_object_type_pnd;
74   }
75
76   // if not a file of interest, just keep looking until we run out
77   if ( ! valid ) {
78     //printf ( " .. bad filename, skipping\n" );
79     return ( 0 );
80   }
81
82   // potentially a valid application
83   if ( valid == pnd_object_type_directory ) {
84     // Plaintext PXML file
85     //printf ( "PXML: disco callback encountered '%s'\n", fpath );
86
87     // pick up the PXML if we can
88     pxmlh = pnd_pxml_fetch ( (char*) fpath );
89
90   } else if ( valid == pnd_object_type_pnd ) {
91     // PND ... ??
92     FILE *f;
93     char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
94
95     //printf ( "PND: disco callback encountered '%s'\n", fpath );
96
97     // is this a valid .pnd file? The filename is a candidate already, but verify..
98     // .. presence of PXML appended, or at least contained within?
99     // .. presence of an icon appended after PXML?
100
101     // open it up..
102     f = fopen ( fpath, "r" );
103
104     // try to locate the PXML portion
105     if ( ! pnd_pnd_seek_pxml ( f ) ) {
106       fclose ( f );
107       return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
108     }
109
110     // accrue it into a buffer
111     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
112       fclose ( f );
113       return ( 0 );
114     }
115
116     //printf ( "buffer is %s\n", pxmlbuf );
117     //fflush ( stdout );
118
119 #if 1 // icon
120     // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
121     {
122       unsigned int pos = ftell ( f );
123       char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
124       pngbuffer [ 0 ] = 137;      pngbuffer [ 1 ] = 80;      pngbuffer [ 2 ] = 78;      pngbuffer [ 3 ] = 71;
125       pngbuffer [ 4 ] = 13;       pngbuffer [ 5 ] = 10;       pngbuffer [ 6 ] = 26;      pngbuffer [ 7 ] = 10;
126       if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
127         if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
128           pxml_close_pos = pos;
129         }
130       }
131     } // icon
132 #endif
133
134     // by now, we have <PXML> .. </PXML>, try to parse..
135     pxmlh = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
136
137     // done with file
138     fclose ( f );
139
140   }
141
142   // pxmlh is useful?
143   if ( pxmlh ) {
144
145     // look for any overrides, if requested
146     pnd_pxml_merge_override ( pxmlh, disco_overrides );
147
148     // check for validity and add to resultset if it looks executable
149     if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
150       pnd_disco_t *p;
151       char *fixpxml;
152
153       p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
154
155       // base paths
156       p -> object_path = strdup ( fpath );
157
158       if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
159         *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
160       } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
161         *(fixpxml+1) = '\0'; // for pnd, lop off to last /
162       }
163
164       if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
165         p -> object_filename = strdup ( fixpxml + 1 );
166       }
167
168       // png icon path
169       p -> pnd_icon_pos = pxml_close_pos;
170
171       // type
172       p -> object_type = valid;
173
174       // PXML fields
175       if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
176         p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
177       }
178       if ( pnd_pxml_get_icon ( pxmlh ) ) {
179         p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
180       }
181       if ( pnd_pxml_get_exec ( pxmlh ) ) {
182         p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
183       }
184       if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
185         p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
186       }
187       if ( pnd_pxml_get_main_category ( pxmlh ) ) {
188         p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
189       }
190       if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
191         p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) ); 
192       }
193       if ( pnd_pxml_get_startdir ( pxmlh ) ) {
194         p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) ); 
195       }
196
197     } else {
198       //printf ( "Invalid PXML; skipping.\n" );
199     }
200
201     // ditch pxml
202     pnd_pxml_delete ( pxmlh );
203
204   } // got a pxmlh
205
206   return ( 0 ); // continue the tree walk
207 }
208
209 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
210
211   //printf ( "Searchpath to discover: '%s'\n", searchpath );
212
213   // alloc a container for the result set
214   disco_box = pnd_box_new ( "discovery" );
215   disco_overrides = overridespath;
216
217   /* iterate across the paths within the searchpath, attempting to locate applications
218    */
219
220   SEARCHPATH_PRE
221   {
222
223     // invoke the dir walking function; thankfully Linux includes a pretty good one
224     nftw ( buffer,               // path to descend
225            pnd_disco_callback,   // callback to do processing
226            10,                   // no more than X open fd's at once
227            FTW_PHYS );           // do not follow symlinks
228
229   }
230   SEARCHPATH_POST
231
232   // return whatever we found, or NULL if nada
233   if ( ! pnd_box_get_head ( disco_box ) ) {
234     pnd_box_delete ( disco_box );
235     disco_box = NULL;
236   }
237
238   return ( disco_box );
239 }