Refreshed pnd_run and sudoers scripts from wiki
[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     // exec options
101     if ( p -> option_no_x11 ) {
102       strncat ( buffer, " -x ", 1020 );
103     }
104
105     // newline
106     strncat ( buffer, "\n", 1020 );
107
108     // emit
109     fprintf ( f, "%s", buffer );
110   }
111
112 #if 1 // categories
113   fprintf ( f, "%s\n", "Categories=Application;Network;" );
114 #endif
115
116   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
117
118   fclose ( f );
119
120   return ( 1 );
121 }
122
123 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
124   char buffer [ FILENAME_MAX ]; // target filename
125   char from [ FILENAME_MAX ];   // source filename
126   char bits [ 8 * 1024 ];
127   unsigned int bitlen;
128   FILE *pnd, *target;
129
130   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
131   if ( ( p -> object_type == pnd_object_type_pnd ) &&
132        ( ! p -> pnd_icon_pos ) )
133   {
134     return ( 0 ); // discover code didn't find it, so FAIL
135   }
136
137   // determine filename for target
138   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
139
140   /* first.. open the source file, by type of application:
141    * are we looking through a pnd file or a dir?
142    */
143   if ( p -> object_type == pnd_object_type_directory ) {
144     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
145   } else if ( p -> object_type == pnd_object_type_pnd ) {
146     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
147   }
148
149   pnd = fopen ( from, "r" );
150
151   if ( ! pnd ) {
152     return ( 0 );
153   }
154
155   unsigned int len;
156
157   target = fopen ( buffer, "wb" );
158
159   if ( ! target ) {
160     fclose ( pnd );
161     return ( 0 );
162   }
163
164   fseek ( pnd, 0, SEEK_END );
165   len = ftell ( pnd );
166   //fseek ( pnd, 0, SEEK_SET );
167
168   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
169
170   len -= p -> pnd_icon_pos;
171
172   while ( len ) {
173
174     if ( len > (8*1024) ) {
175       bitlen = (8*1024);
176     } else {
177       bitlen = len;
178     }
179
180     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
181       fclose ( pnd );
182       fclose ( target );
183       unlink ( buffer );
184       return ( 0 );
185     }
186
187     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
188       fclose ( pnd );
189       fclose ( target );
190       unlink ( buffer );
191       return ( 0 );
192     }
193
194     len -= bitlen;
195   } // while
196
197   fclose ( pnd );
198   fclose ( target );
199
200   return ( 1 );
201 }