Added first stab of pnd exec support
[pandora-libraries.git] / test / discotest.c
1
2 #include <stdio.h> /* for printf, NULL */
3 #include <stdlib.h> /* for free */
4
5 #include "pnd_conf.h"
6 #include "pnd_container.h"
7 #include "pnd_apps.h"
8 #include "pnd_pxml.h"
9 #include "pnd_discovery.h"
10 #include "pnd_locate.h"
11
12 int main ( void) {
13   char *configpath;
14   char *appspath;
15   char *overridespath;
16
17   /* attempt to sort out the config file madness
18    */
19
20   // attempt to fetch a sensible default searchpath for configs
21   configpath = pnd_conf_query_searchpath();
22
23   // attempt to fetch the apps config to pick up a searchpath
24   pnd_conf_handle apph;
25
26   apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
27
28   if ( apph ) {
29     appspath = pnd_conf_get_as_char ( apph, PND_APPS_KEY );
30
31     if ( ! appspath ) {
32       appspath = PND_APPS_SEARCHPATH;
33     }
34
35     overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
36
37     if ( ! overridespath ) {
38       overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
39     }
40
41   } else {
42     // couldn't find a useful app search path so use the default
43     appspath = PND_APPS_SEARCHPATH;
44     overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
45   }
46
47   printf ( "Apps searchpath is '%s'\n", appspath );
48   printf ( "Apps overrides searchpath is '%s'\n", overridespath );
49
50   /* attempt to discover apps in the path
51    */
52   pnd_box_handle applist;
53
54   applist = pnd_disco_search ( appspath, overridespath );
55
56   // list the found apps (if any)
57
58   if ( applist ) {
59     pnd_disco_t *d = pnd_box_get_head ( applist );
60
61     while ( d ) {
62
63       // display the app 'as is'
64
65       printf ( "App: %s\n", pnd_box_get_key ( d ) );
66
67       printf ( "  Base path: %s\n", d -> path_to_object );
68
69       if ( d -> title_en ) {
70         printf ( "  Name: %s\n", d -> title_en );
71       }
72       if ( d -> icon ) {
73         printf ( "  Icon: %s\n", d -> icon );
74       }
75       if ( d -> unique_id ) {
76         printf ( "  Unique ID: %s\n", d -> unique_id );
77       }
78       if ( d -> main_category ) {
79         printf ( "  Category: %s\n", d -> main_category );
80       }
81       if ( d -> exec ) {
82         printf ( "  Executable: %s\n", d -> exec );
83       }
84       if ( d -> startdir ) {
85         printf ( "  Start dir: %s\n", d -> startdir );
86       }
87       if ( d -> clockspeed ) {
88         printf ( "  Clockspeed: %s\n", d -> clockspeed );
89       }
90
91       //pnd_emit_dotdesktop ( "/tmp", d );
92
93       // next!
94       d = pnd_box_get_next ( d );
95
96     } // while applist
97
98   } else {
99     printf ( "No applications found in search path\n" );
100   }
101
102   // lets toy with executing an application
103   char *run_searchpath;
104   char *run_script;
105   char *pndrun;
106
107   if ( apph ) {
108     run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
109     run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
110     pndrun = NULL;
111
112     if ( ! run_searchpath ) {
113       run_searchpath = PND_APPS_SEARCHPATH;
114       run_script = PND_PNDRUN_FILENAME;
115     }
116
117   } else {
118     run_searchpath = NULL;
119     run_script = NULL;
120     pndrun = PND_PNDRUN_DEFAULT;
121   }
122
123   if ( ! pndrun ) {
124     pndrun = pnd_locate_filename ( run_searchpath, run_script );
125   }
126
127   if ( run_searchpath ) printf ( "Locating pnd run in %s\n", run_searchpath );
128   if ( run_script ) printf ( "Locating pnd runscript as %s\n", run_script );
129   if ( pndrun ) printf ( "Default pndrun is %s\n", pndrun );
130
131   if ( ! pndrun ) {
132     printf ( "*** Couldn't locate a pnd runscript.\n" );
133   } else {
134     printf ( "Found a pnd runscript of %s\n", pndrun );
135
136     pnd_disco_t *d = pnd_box_get_head ( applist );
137     if ( d ) {
138       d = pnd_box_get_next ( d );
139
140       if ( d ) {
141         pnd_apps_exec ( pndrun, d -> path_to_object, d -> unique_id, d -> exec, d -> startdir, atoi ( d -> clockspeed ) );
142       }
143     }
144
145   }
146
147   // exeunt with alarums
148   free ( configpath );
149   if ( apph ) {
150     pnd_box_delete ( apph );
151   }
152
153   return ( 0 );
154 }