8745706aa29e9914b0514f3f4544b59795eb9817
[pandora-libraries.git] / test / discotest.c
1
2 #include <stdio.h> /* for printf, NULL */
3 #include <stdlib.h> /* for free */
4 #include <string.h> /* for strdup */
5
6 #include "pnd_conf.h"
7 #include "pnd_container.h"
8 #include "pnd_apps.h"
9 #include "pnd_pxml.h"
10 #include "pnd_discovery.h"
11 #include "pnd_desktop.h"
12 #include "pnd_locate.h"
13 #include "pnd_utility.h"
14
15 int main ( int argc, char *argv[] ) {
16   char *configpath;
17   char *appspath;
18   char *overridespath;
19   unsigned char i;
20   unsigned char do_exec = 0;
21   unsigned char do_icon = 0;
22   unsigned char do_dotdesktop = 0;
23   char dotdesktoppath [ 1024 ] = "";
24
25   for ( i = 1; i < argc; i++ ) {
26
27     if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'e' ) {
28       printf ( "Will attempt a random exec.\n" );
29       do_exec = 1;
30     } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'i' ) {
31       printf ( "Will attempt to extract icons.\n" );
32       do_icon = 1;
33     } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
34       printf ( "Will attempt to extract dotdesktop.\n" );
35       do_dotdesktop = 1;
36     } else if ( strstr ( argv [ i ], ".desktop" ) ) {
37       strncpy ( dotdesktoppath, argv [ i ], 1023 );
38       printf ( "Will scan '%s' instead of performing discovery\n", dotdesktoppath );
39     } else {
40       printf ( "%s [-e] [-i] [-d] [dotdesktop-file]\n", argv [ 0 ] );
41       printf ( "-e\tOptional. Attempt to exec a random app.\n" );
42       printf ( "-i\tOptional. Attempt to dump icon files from the end of pnd's to ./testdata/dotdesktop.\n" );
43       printf ( "-d\tOptional. Attempt to dump dotdesktop files from the end of pnd's to ./testdata/dotdesktop.\n" );
44       printf ( "dotdesktop-file\tIf path specified and ends with .desktop, try to scan that .desktop instead of pnd-discovery\n" );
45       exit ( 0 );
46     }
47
48   }
49
50   /* attempt to sort out the config file madness
51    */
52
53   // attempt to fetch a sensible default searchpath for configs
54   configpath = pnd_conf_query_searchpath();
55
56   // attempt to fetch the apps config to pick up a searchpath
57   pnd_conf_handle apph;
58
59   apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
60
61   if ( apph ) {
62     appspath = pnd_conf_get_as_char ( apph, PND_APPS_KEY );
63
64     if ( ! appspath ) {
65       appspath = PND_APPS_SEARCHPATH;
66     }
67
68     overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
69
70     if ( ! overridespath ) {
71       overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
72     }
73
74   } else {
75     // couldn't find a useful app search path so use the default
76     appspath = PND_APPS_SEARCHPATH;
77     overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
78   }
79
80   printf ( "Apps searchpath is '%s'\n", appspath );
81   printf ( "Apps overrides searchpath is '%s'\n", overridespath );
82
83   /* find pnd runscript
84    */
85   char *run_searchpath;
86   char *run_script;
87   char *pndrun;
88
89   if ( apph ) {
90     run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
91     run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
92     pndrun = NULL;
93
94     if ( ! run_searchpath ) {
95       run_searchpath = PND_APPS_SEARCHPATH;
96       run_script = PND_PNDRUN_FILENAME;
97     }
98
99   } else {
100     run_searchpath = NULL;
101     run_script = NULL;
102     pndrun = PND_PNDRUN_DEFAULT;
103   }
104
105   if ( ! pndrun ) {
106     pndrun = pnd_locate_filename ( run_searchpath, run_script );
107   }
108
109   if ( run_searchpath ) printf ( "Locating pnd run in %s\n", run_searchpath );
110   if ( run_script ) printf ( "Locating pnd runscript as %s\n", run_script );
111   if ( pndrun ) printf ( "Default pndrun is %s\n", pndrun );
112
113   /* attempt to discover apps in the path
114    */
115   pnd_box_handle applist;
116
117   if ( dotdesktoppath [ 0 ] ) {
118     pnd_disco_t *p = pnd_parse_dotdesktop ( dotdesktoppath );
119     pnd_box_handle disco_box = pnd_box_new ( "discovery" );
120     if ( p ) {
121       pnd_disco_t *ai = pnd_box_allocinsert ( disco_box, dotdesktoppath, sizeof(pnd_disco_t) );
122       memmove ( ai, p, sizeof(pnd_disco_t) );
123     }
124     applist = disco_box;
125   } else {
126     applist = pnd_disco_search ( appspath, overridespath );
127   }
128
129   // list the found apps (if any)
130
131   if ( applist ) {
132     pnd_disco_t *d = pnd_box_get_head ( applist );
133
134     while ( d ) {
135
136       // display the app 'as is'
137
138       printf ( "App: %s (type %u)\n", pnd_box_get_key ( d ), d -> object_type );
139
140       printf ( "  Base path: %s filename: %s\n", d -> object_path, d -> object_filename );
141
142       if ( d -> title_en ) {
143         printf ( "  Name EN: %s\n", d -> title_en );
144       }
145       if ( d -> desc_en ) {
146         printf ( "  Desc EN: %s\n", d -> desc_en );
147       }
148       if ( d -> icon ) {
149         printf ( "  Icon: %s\n", d -> icon );
150       }
151       if ( d -> pnd_icon_pos ) {
152         printf ( "  Icon in pnd might be at: %u\n", d -> pnd_icon_pos );
153       }
154       if ( d -> unique_id ) {
155         printf ( "  Unique ID: %s\n", d -> unique_id );
156       }
157       if ( d -> main_category ) {
158         printf ( "  Category: %s\n", d -> main_category );
159       }
160       if ( d -> exec ) {
161         printf ( "  Executable: %s\n", d -> exec );
162       }
163       if ( d -> startdir ) {
164         printf ( "  Start dir: %s\n", d -> startdir );
165       }
166       if ( d -> clockspeed ) {
167         printf ( "  Clockspeed: %s\n", d -> clockspeed );
168       }
169       if ( d -> preview_pic1 ) {
170         printf ( "  Preview Pic 1: %s\n", d -> preview_pic1 );
171       }
172       if ( d -> preview_pic2 ) {
173         printf ( "  Preview Pic 2: %s\n", d -> preview_pic2 );
174       }
175       if ( d -> mkdir_sp ) {
176         printf ( "  mkdir requests: %s\n", d -> mkdir_sp );
177       }
178
179       if ( do_icon ) {
180         if ( pnd_emit_icon ( "./testdata/dotdesktop", d ) ) {
181           printf ( "  -> icon dump succeeded\n" );
182
183           // fix up icon path to new one..
184           free ( d -> icon );
185           char buffer [ FILENAME_MAX ];
186           sprintf ( buffer, "%s/%s.png", "discotest-temp/", d -> unique_id );
187           d -> icon = strdup ( buffer );
188
189         } else {
190           printf ( "  -> icon dump failed\n" );
191         }
192       }
193
194       if ( do_dotdesktop ) {
195         if ( pnd_emit_dotdesktop ( "./testdata/dotdesktop", pndrun, d ) ) {
196           printf ( "  -> dotdesktop dump succeeded\n" );
197         } else {
198           printf ( "  -> dotdesktop dump failed\n" );
199         }
200       }
201
202       // free current disco <o/
203       pnd_disco_destroy( d );
204
205       // next!
206       d = pnd_box_get_next ( d );
207
208     } // while applist
209
210     // delete our search
211     pnd_box_delete( applist );
212
213   } else {
214     printf ( "No applications found in search path\n" );
215   }
216
217   // lets toy with executing an application
218   if ( do_exec ) {
219
220     if ( ! pndrun ) {
221       printf ( "*** Couldn't locate a pnd runscript.\n" );
222     } else {
223       printf ( "Found a pnd runscript of %s\n", pndrun );
224
225       pnd_disco_t *d = pnd_box_get_head ( applist );
226       if ( d ) {
227         d = pnd_box_get_next ( d );
228
229         if ( d ) {
230           char fullpath [ FILENAME_MAX ];
231           if ( d -> object_type == pnd_object_type_directory ) {
232             sprintf ( fullpath, "%s", d -> object_path );
233           } else if ( d -> object_type == pnd_object_type_pnd ) {
234             sprintf ( fullpath, "%s/%s", d -> object_path, d -> object_filename );
235           }
236
237           printf ( "Guessing appdata path..\n" );
238           char appdata_path [ 1024 ];
239           pnd_get_ro_mountpoint ( fullpath, d -> unique_id, appdata_path, 1024 );
240           printf ( "Guessed readonly app mountpoint '%s'\n", appdata_path );
241           if ( pnd_get_appdata_path ( fullpath, d -> unique_id, appdata_path, 1024 ) ) {
242             printf ( "  Appdata should be: %s\n", appdata_path );
243           } else {
244             printf ( "  Error determining appdata path..\n" );
245           }
246
247           printf ( "Trying to exec '%s'\n", fullpath );
248           pnd_apps_exec ( pndrun, fullpath, d -> unique_id, d -> exec, d -> startdir, NULL, atoi ( d -> clockspeed ), PND_EXEC_OPTION_BLOCK );
249         }
250       }
251
252     }
253
254   } // do_exec?
255
256   // exeunt with alarums
257   free ( configpath );
258   if ( apph ) {
259     pnd_box_delete ( apph );
260   }
261
262   // extra testing - tilde-substitution
263   printf ( "Unrelated test..\n" );
264   char *p = strdup ( "~/.applications" );
265   printf ( "Tilde substitution: in '%s'\n", p );
266   char *expand = pnd_expand_tilde ( p );
267   printf ( "                   out '%s'\n", expand ); free(expand);
268
269   return ( 0 );
270 }