2 #include <stdio.h> /* for FILE etc */
4 #include <unistd.h> /* for unlink */
5 #include <stdlib.h> /* for free */
8 #include "pnd_container.h"
10 #include "pnd_discovery.h"
11 #include "pnd_pndfiles.h"
13 #include "pnd_desktop.h"
15 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
16 char filename [ FILENAME_MAX ];
21 // http://standards.freedesktop.org/desktop-entry-spec
25 if ( ! p -> unique_id ) {
43 sprintf ( filename, "%s/%s.desktop", targetpath, p -> unique_id );
47 //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
49 f = fopen ( filename, "w" );
55 fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
57 if ( p -> title_en ) {
58 snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
59 fprintf ( f, "%s", buffer );
62 fprintf ( f, "Type=Application\n" );
63 fprintf ( f, "Version=1.0\n" );
66 snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
67 fprintf ( f, "%s", buffer );
71 if ( p -> description_en ) {
72 snprintf ( buffer, 1020, "Comment=%s\n", p -> description_en );
73 fprintf ( f, "%s", buffer );
77 #if 0 // we let pnd_run.sh handle this
78 if ( p -> startdir ) {
79 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
80 fprintf ( f, "%s", buffer );
82 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
89 if ( p -> object_type == pnd_object_type_directory ) {
90 snprintf ( buffer, 1020, "Exec=/usr/bin/nohup %s -p %s -e %s -b %s",
91 pndrun, p -> object_path, p -> exec, p -> unique_id );
92 } else if ( p -> object_type == pnd_object_type_pnd ) {
93 snprintf ( buffer, 1020, "Exec=/usr/bin/nohup %s -p %s/%s -e %s -b %s",
94 pndrun, p -> object_path, p -> object_filename, p -> exec, p -> unique_id );
98 if ( p -> startdir ) {
99 strncat ( buffer, " -s ", 1020 );
100 strncat ( buffer, p -> startdir, 1020 );
104 if ( p -> option_no_x11 ) {
105 strncat ( buffer, " -x ", 1020 );
109 strncat ( buffer, "\n", 1020 );
112 fprintf ( f, "%s", buffer );
117 char cats [ 512 ] = "";
123 // "Application" used to be in the standard and is commonly supported still
124 // Utility and Network should ensure the app is visible 'somewhere' :/
125 char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
127 // determine searchpath (for conf, not for apps)
128 confpath = pnd_conf_query_searchpath();
130 // inhale the conf file
131 c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
133 // if we can find a default category set, pull it in; otherwise assume
135 if ( pnd_conf_get_as_char ( c, "default" ) ) {
136 defaults = pnd_conf_get_as_char ( c, "default" );
139 // ditch the confpath
145 n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
148 fprintf ( f, "Categories=%s\n", cats );
150 fprintf ( f, "Categories=%s\n", defaults );
154 fprintf ( f, "Categories=%s\n", defaults );
159 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
166 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
167 char buffer [ FILENAME_MAX ]; // target filename
168 char from [ FILENAME_MAX ]; // source filename
169 char bits [ 8 * 1024 ];
173 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
174 if ( ( p -> object_type == pnd_object_type_pnd ) &&
175 ( ! p -> pnd_icon_pos ) )
177 return ( 0 ); // discover code didn't find it, so FAIL
180 // determine filename for target
181 sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
183 /* first.. open the source file, by type of application:
184 * are we looking through a pnd file or a dir?
186 if ( p -> object_type == pnd_object_type_directory ) {
187 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
188 } else if ( p -> object_type == pnd_object_type_pnd ) {
189 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
192 pnd = fopen ( from, "r" );
200 target = fopen ( buffer, "wb" );
207 fseek ( pnd, 0, SEEK_END );
209 //fseek ( pnd, 0, SEEK_SET );
211 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
213 len -= p -> pnd_icon_pos;
217 if ( len > (8*1024) ) {
223 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
230 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
246 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
247 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
248 unsigned short int n = 0; // no. matches
252 // clear target so we can easily append
253 memset ( target_buffer, '\0', len );
255 /* attempt primary category chain
259 if ( ( t = d -> main_category ) ) {
260 match = pnd_map_dotdesktop_category ( c, t );
264 ( t = d -> main_category1 ) )
266 match = pnd_map_dotdesktop_category ( c, t );
270 ( t = d -> main_category2 ) )
272 match = pnd_map_dotdesktop_category ( c, t );
276 strncat ( target_buffer, match, len );
277 len -= strlen ( target_buffer );
281 /* attempt secondary category chain
285 if ( ( t = d -> alt_category ) ) {
286 match = pnd_map_dotdesktop_category ( c, t );
290 ( t = d -> alt_category1 ) )
292 match = pnd_map_dotdesktop_category ( c, t );
296 ( t = d -> alt_category2 ) )
298 match = pnd_map_dotdesktop_category ( c, t );
302 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
303 strcat ( target_buffer, ";" );
306 strncat ( target_buffer, match, len );
307 len -= strlen ( target_buffer );
311 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
314 if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
315 match = pnd_map_dotdesktop_category ( c, t );
319 ( t = pnd_pxml_get_subcategory1 ( h ) ) )
321 match = pnd_map_dotdesktop_category ( c, t );
325 ( t = pnd_pxml_get_subcategory2 ( h ) ) )
327 match = pnd_map_dotdesktop_category ( c, t );
331 strncat ( target_buffer, match, len );
332 len -= strlen ( target_buffer );
336 /* attempt secondary category chain
340 if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
341 match = pnd_map_dotdesktop_category ( c, t );
345 ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
347 match = pnd_map_dotdesktop_category ( c, t );
351 ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
353 match = pnd_map_dotdesktop_category ( c, t );
357 if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
358 strcat ( target_buffer, ";" );
361 strncat ( target_buffer, match, len );
362 len -= strlen ( target_buffer );
368 strcat ( target_buffer, ";" );
374 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
375 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
378 key = malloc ( strlen ( single_category ) + 4 + 1 );
380 sprintf ( key, "map.%s", single_category );
382 return ( pnd_conf_get_as_char ( c, key ) );