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