Removed -n (no union) from .desktop Exec
[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
6 #include "pnd_apps.h"
7 #include "pnd_container.h"
8 #include "pnd_pxml.h"
9 #include "pnd_discovery.h"
10 #include "pnd_pndfiles.h"
11
12 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
13   char filename [ FILENAME_MAX ];
14   char buffer [ 1024 ];
15   FILE *f;
16
17   // specification
18   // http://standards.freedesktop.org/desktop-entry-spec
19
20   // validation
21
22   if ( ! p -> unique_id ) {
23     return ( 0 );
24   }
25
26   if ( ! p -> exec ) {
27     return ( 0 );
28   }
29
30   if ( ! targetpath ) {
31     return ( 0 );
32   }
33
34   if ( ! pndrun ) {
35     return ( 0 );
36   }
37
38   // set up
39
40   sprintf ( filename, "%s/%s.desktop", targetpath, p -> unique_id );
41
42   // emit
43
44   //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
45
46   f = fopen ( filename, "w" );
47
48   if ( ! f ) {
49     return ( 0 );
50   }
51
52   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
53
54   if ( p -> title_en ) {
55     snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
56     fprintf ( f, "%s", buffer );
57   }
58
59   fprintf ( f, "Type=Application\n" );
60   fprintf ( f, "Version=1.0\n" );
61
62   if ( p -> icon ) {
63     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
64     fprintf ( f, "%s", buffer );
65   }
66
67 #if 0
68   if ( p -> description_en ) {
69     snprintf ( buffer, 1020, "Comment=%s\n", p -> description_en );
70     fprintf ( f, "%s", buffer );
71   }
72 #endif
73
74 #if 0 // we let pnd_run.sh handle this
75   if ( p -> startdir ) {
76     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
77     fprintf ( f, "%s", buffer );
78   } else {
79     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
80   }
81 #endif
82
83   if ( p -> exec ) {
84
85     // basics
86     if ( p -> object_type == pnd_object_type_directory ) {
87       snprintf ( buffer, 1020, "Exec=/usr/bin/nohup %s -p %s -e %s -b %s",
88                  pndrun, p -> object_path, p -> exec, p -> unique_id );
89     } else if ( p -> object_type == pnd_object_type_pnd ) {
90       snprintf ( buffer, 1020, "Exec=/usr/bin/nohup %s -p %s/%s -e %s -b %s",
91                  pndrun, p -> object_path, p -> object_filename, p -> exec, p -> unique_id );
92     }
93
94     // start dir
95     if ( p -> startdir ) {
96       strncat ( buffer, " -s ", 1020 );
97       strncat ( buffer, p -> startdir, 1020 );
98     }
99
100     // newline
101     strncat ( buffer, "\n", 1020 );
102
103     // emit
104     fprintf ( f, "%s", buffer );
105   }
106
107 #if 1 // categories
108   fprintf ( f, "%s\n", "Categories=Application;Network;" );
109 #endif
110
111   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
112
113   fclose ( f );
114
115   return ( 1 );
116 }
117
118 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
119   char buffer [ FILENAME_MAX ]; // target filename
120   char from [ FILENAME_MAX ];   // source filename
121   char bits [ 8 * 1024 ];
122   unsigned int bitlen;
123   FILE *pnd, *target;
124
125   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
126   if ( ( p -> object_type == pnd_object_type_pnd ) &&
127        ( ! p -> pnd_icon_pos ) )
128   {
129     return ( 0 ); // discover code didn't find it, so FAIL
130   }
131
132   // determine filename for target
133   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
134
135   /* first.. open the source file, by type of application:
136    * are we looking through a pnd file or a dir?
137    */
138   if ( p -> object_type == pnd_object_type_directory ) {
139     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
140   } else if ( p -> object_type == pnd_object_type_pnd ) {
141     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
142   }
143
144   pnd = fopen ( from, "r" );
145
146   if ( ! pnd ) {
147     return ( 0 );
148   }
149
150   unsigned int len;
151
152   target = fopen ( buffer, "wb" );
153
154   if ( ! target ) {
155     fclose ( pnd );
156     return ( 0 );
157   }
158
159   fseek ( pnd, 0, SEEK_END );
160   len = ftell ( pnd );
161   //fseek ( pnd, 0, SEEK_SET );
162
163   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
164
165   len -= p -> pnd_icon_pos;
166
167   while ( len ) {
168
169     if ( len > (8*1024) ) {
170       bitlen = (8*1024);
171     } else {
172       bitlen = len;
173     }
174
175     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
176       fclose ( pnd );
177       fclose ( target );
178       unlink ( buffer );
179       return ( 0 );
180     }
181
182     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
183       fclose ( pnd );
184       fclose ( target );
185       unlink ( buffer );
186       return ( 0 );
187     }
188
189     len -= bitlen;
190   } // while
191
192   fclose ( pnd );
193   fclose ( target );
194
195   return ( 1 );
196 }