Added X-Pandora-UID to .desktop
[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
161       if ( do_icon ) {
162         if ( pnd_emit_icon ( "./testdata/dotdesktop", d ) ) {
163           printf ( "  -> icon dump succeeded\n" );
164
165           // fix up icon path to new one..
166           free ( d -> icon );
167           char buffer [ FILENAME_MAX ];
168           sprintf ( buffer, "%s/%s.png", "discotest-temp/", d -> unique_id );
169           d -> icon = strdup ( buffer );
170
171         } else {
172           printf ( "  -> icon dump failed\n" );
173         }
174       }
175
176       if ( do_dotdesktop ) {
177         if ( pnd_emit_dotdesktop ( "./testdata/dotdesktop", pndrun, d ) ) {
178           printf ( "  -> dotdesktop dump succeeded\n" );
179         } else {
180           printf ( "  -> dotdesktop dump failed\n" );
181         }
182       }
183
184       // next!
185       d = pnd_box_get_next ( d );
186
187     } // while applist
188
189   } else {
190     printf ( "No applications found in search path\n" );
191   }
192
193   // lets toy with executing an application
194   if ( do_exec ) {
195
196     if ( ! pndrun ) {
197       printf ( "*** Couldn't locate a pnd runscript.\n" );
198     } else {
199       printf ( "Found a pnd runscript of %s\n", pndrun );
200
201       pnd_disco_t *d = pnd_box_get_head ( applist );
202       if ( d ) {
203         d = pnd_box_get_next ( d );
204
205         if ( d ) {
206           char fullpath [ FILENAME_MAX ];
207           if ( d -> object_type == pnd_object_type_directory ) {
208             sprintf ( fullpath, "%s", d -> object_path );
209           } else if ( d -> object_type == pnd_object_type_pnd ) {
210             sprintf ( fullpath, "%s/%s", d -> object_path, d -> object_filename );
211           }
212
213           printf ( "Guessing appdata path..\n" );
214           char appdata_path [ 1024 ];
215           pnd_get_ro_mountpoint ( fullpath, d -> unique_id, appdata_path, 1024 );
216           printf ( "Guessed readonly app mountpoint '%s'\n", appdata_path );
217           if ( pnd_get_appdata_path ( fullpath, d -> unique_id, appdata_path, 1024 ) ) {
218             printf ( "  Appdata should be: %s\n", appdata_path );
219           } else {
220             printf ( "  Error determining appdata path..\n" );
221           }
222
223           printf ( "Trying to exec '%s'\n", fullpath );
224           pnd_apps_exec ( pndrun, fullpath, d -> unique_id, d -> exec, d -> startdir, atoi ( d -> clockspeed ), PND_EXEC_OPTION_BLOCK );
225         }
226       }
227
228     }
229
230   } // do_exec?
231
232   // exeunt with alarums
233   free ( configpath );
234   if ( apph ) {
235     pnd_box_delete ( apph );
236   }
237
238   // extra testing - tilde-substitution
239   printf ( "Unrelated test..\n" );
240   char *p = strdup ( "~/.applications" );
241   printf ( "Tilde substitution: in '%s'\n", p );
242   printf ( "                   out '%s'\n", pnd_expand_tilde ( p ) );
243
244   return ( 0 );
245 }