Added in rudimentary category support for dotdesktop file emitting; ie: PXML's catego...
[pandora-libraries.git] / lib / pnd_desktop.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <string.h>
4 #include <unistd.h> /* for unlink */
5 #include <stdlib.h> /* for free */
6
7 #include "pnd_apps.h"
8 #include "pnd_container.h"
9 #include "pnd_pxml.h"
10 #include "pnd_discovery.h"
11 #include "pnd_pndfiles.h"
12 #include "pnd_conf.h"
13 #include "pnd_desktop.h"
14
15 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
16   char filename [ FILENAME_MAX ];
17   char buffer [ 1024 ];
18   FILE *f;
19
20   // specification
21   // http://standards.freedesktop.org/desktop-entry-spec
22
23   // validation
24
25   if ( ! p -> unique_id ) {
26     return ( 0 );
27   }
28
29   if ( ! p -> exec ) {
30     return ( 0 );
31   }
32
33   if ( ! targetpath ) {
34     return ( 0 );
35   }
36
37   if ( ! pndrun ) {
38     return ( 0 );
39   }
40
41   // set up
42
43   sprintf ( filename, "%s/%s.desktop", targetpath, p -> unique_id );
44
45   // emit
46
47   //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
48
49   f = fopen ( filename, "w" );
50
51   if ( ! f ) {
52     return ( 0 );
53   }
54
55   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
56
57   if ( p -> title_en ) {
58     snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
59     fprintf ( f, "%s", buffer );
60   }
61
62   fprintf ( f, "Type=Application\n" );
63   fprintf ( f, "Version=1.0\n" );
64
65   if ( p -> icon ) {
66     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
67     fprintf ( f, "%s", buffer );
68   }
69
70 #if 0
71   if ( p -> description_en ) {
72     snprintf ( buffer, 1020, "Comment=%s\n", p -> description_en );
73     fprintf ( f, "%s", buffer );
74   }
75 #endif
76
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 );
81   } else {
82     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
83   }
84 #endif
85
86   if ( p -> exec ) {
87
88     // basics
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 );
95     }
96
97     // start dir
98     if ( p -> startdir ) {
99       strncat ( buffer, " -s ", 1020 );
100       strncat ( buffer, p -> startdir, 1020 );
101     }
102
103     // exec options
104     if ( p -> option_no_x11 ) {
105       strncat ( buffer, " -x ", 1020 );
106     }
107
108     // newline
109     strncat ( buffer, "\n", 1020 );
110
111     // emit
112     fprintf ( f, "%s", buffer );
113   }
114
115   // categories
116   {
117     char cats [ 512 ] = "";
118     int n;
119     pnd_conf_handle c;
120     char *confpath;
121
122     // uuuuh, defaults?
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;
126
127     // determine searchpath (for conf, not for apps)
128     confpath = pnd_conf_query_searchpath();
129
130     // inhale the conf file
131     c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
132
133     // if we can find a default category set, pull it in; otherwise assume
134     // the hardcoded one
135     if ( pnd_conf_get_as_char ( c, "default" ) ) {
136       defaults = pnd_conf_get_as_char ( c, "default" );
137     }
138
139     // ditch the confpath
140     free ( confpath );
141
142     // attempt mapping
143     if ( c ) {
144
145       n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
146
147       if ( n ) {
148         fprintf ( f, "Categories=%s\n", cats );
149       } else {
150         fprintf ( f, "Categories=%s\n", defaults );
151       }
152
153     } else {
154       fprintf ( f, "Categories=%s\n", defaults );
155     }
156
157   }
158
159   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
160
161   fclose ( f );
162
163   return ( 1 );
164 }
165
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 ];
170   unsigned int bitlen;
171   FILE *pnd, *target;
172
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 ) )
176   {
177     return ( 0 ); // discover code didn't find it, so FAIL
178   }
179
180   // determine filename for target
181   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
182
183   /* first.. open the source file, by type of application:
184    * are we looking through a pnd file or a dir?
185    */
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 );
190   }
191
192   pnd = fopen ( from, "r" );
193
194   if ( ! pnd ) {
195     return ( 0 );
196   }
197
198   unsigned int len;
199
200   target = fopen ( buffer, "wb" );
201
202   if ( ! target ) {
203     fclose ( pnd );
204     return ( 0 );
205   }
206
207   fseek ( pnd, 0, SEEK_END );
208   len = ftell ( pnd );
209   //fseek ( pnd, 0, SEEK_SET );
210
211   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
212
213   len -= p -> pnd_icon_pos;
214
215   while ( len ) {
216
217     if ( len > (8*1024) ) {
218       bitlen = (8*1024);
219     } else {
220       bitlen = len;
221     }
222
223     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
224       fclose ( pnd );
225       fclose ( target );
226       unlink ( buffer );
227       return ( 0 );
228     }
229
230     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
231       fclose ( pnd );
232       fclose ( target );
233       unlink ( buffer );
234       return ( 0 );
235     }
236
237     len -= bitlen;
238   } // while
239
240   fclose ( pnd );
241   fclose ( target );
242
243   return ( 1 );
244 }
245
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
249   char *t;
250   char *match;
251
252   // clear target so we can easily append
253   memset ( target_buffer, '\0', len );
254
255   /* attempt primary category chain
256    */
257   match = NULL;
258
259   if ( ( t = d -> main_category ) ) {
260     match = pnd_map_dotdesktop_category ( c, t );
261   }
262
263   if ( ( ! match ) &&
264        ( t = d -> main_category1 ) )
265   {
266     match = pnd_map_dotdesktop_category ( c, t );
267   }
268
269   if ( ( ! match ) &&
270        ( t = d -> main_category2 ) )
271   {
272     match = pnd_map_dotdesktop_category ( c, t );
273   }
274   
275   if ( match ) {
276     strncat ( target_buffer, match, len );
277     len -= strlen ( target_buffer );
278     n += 1;
279   }
280
281   /* attempt secondary category chain
282    */
283   match = NULL;
284
285   if ( ( t = d -> alt_category ) ) {
286     match = pnd_map_dotdesktop_category ( c, t );
287   }
288
289   if ( ( ! match ) &&
290        ( t = d -> alt_category1 ) )
291   {
292     match = pnd_map_dotdesktop_category ( c, t );
293   }
294
295   if ( ( ! match ) &&
296        ( t = d -> alt_category2 ) )
297   {
298     match = pnd_map_dotdesktop_category ( c, t );
299   }
300   
301   if ( match ) {
302     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
303       strcat ( target_buffer, ";" );
304       len -= 1;
305     }
306     strncat ( target_buffer, match, len );
307     len -= strlen ( target_buffer );
308     n += 1;
309   }
310
311 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
312   match = NULL;
313
314   if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
315     match = pnd_map_dotdesktop_category ( c, t );
316   }
317
318   if ( ( ! match ) &&
319        ( t = pnd_pxml_get_subcategory1 ( h ) ) )
320   {
321     match = pnd_map_dotdesktop_category ( c, t );
322   }
323
324   if ( ( ! match ) &&
325        ( t = pnd_pxml_get_subcategory2 ( h ) ) )
326   {
327     match = pnd_map_dotdesktop_category ( c, t );
328   }
329   
330   if ( match ) {
331     strncat ( target_buffer, match, len );
332     len -= strlen ( target_buffer );
333     n += 1;
334   }
335
336   /* attempt secondary category chain
337    */
338   match = NULL;
339
340   if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
341     match = pnd_map_dotdesktop_category ( c, t );
342   }
343
344   if ( ( ! match ) &&
345        ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
346   {
347     match = pnd_map_dotdesktop_category ( c, t );
348   }
349
350   if ( ( ! match ) &&
351        ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
352   {
353     match = pnd_map_dotdesktop_category ( c, t );
354   }
355   
356   if ( match ) {
357     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
358       strcat ( target_buffer, ";" );
359       len -= 1;
360     }
361     strncat ( target_buffer, match, len );
362     len -= strlen ( target_buffer );
363     n += 1;
364   }
365 #endif
366
367   if ( n && len ) {
368     strcat ( target_buffer, ";" );
369   }
370
371   return ( n );
372 }
373
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 ) {
376   char *key;
377
378   key = malloc ( strlen ( single_category ) + 4 + 1 );
379
380   sprintf ( key, "map.%s", single_category );
381
382   return ( pnd_conf_get_as_char ( c, key ) );
383 }