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