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