Debian Codegraph @

android/app/jni/extern/nfd-modified/src/nfd_zenity.cpp

raw
1 /*
2 Native File Dialog
3
4 http://www.frogtoss.com/labs
5 */
6
7 #include <stdio.h>
8 #include <assert.h>
9 #include <string.h>
10 #include "nfd.h"
11 #include "nfd_common.h"
12
13 #define SIMPLE_EXEC_IMPLEMENTATION
14 #include "simple_exec.h"
15
16
17 const char NO_ZENITY_MSG[] = "zenity not installed";
18
19
20 static void AddTypeToFilterName( const char *typebuf, char *filterName, size_t bufsize )
21 {
22 size_t len = strlen(filterName);
23 if( len > 0 )
24 strncat( filterName, " *.", bufsize - len - 1 );
25 else
26 strncat( filterName, "--file-filter=*.", bufsize - len - 1 );
27
28 len = strlen(filterName);
29 strncat( filterName, typebuf, bufsize - len - 1 );
30 }
31
32 static void AddFiltersToCommandArgs(char** commandArgs, int commandArgsLen, const char *filterList )
33 {
34 char typebuf[NFD_MAX_STRLEN] = {0};
35 const char *p_filterList = filterList;
36 char *p_typebuf = typebuf;
37 char filterName[NFD_MAX_STRLEN] = {0};
38 int i;
39
40 if ( !filterList || strlen(filterList) == 0 )
41 return;
42
43 while ( 1 )
44 {
45
46 if ( NFDi_IsFilterSegmentChar(*p_filterList) )
47 {
48 char typebufWildcard[NFD_MAX_STRLEN];
49 /* add another type to the filter */
50 assert( strlen(typebuf) > 0 );
51 assert( strlen(typebuf) < NFD_MAX_STRLEN-1 );
52
53 snprintf( typebufWildcard, NFD_MAX_STRLEN, "*.%s", typebuf );
54
55 AddTypeToFilterName( typebuf, filterName, NFD_MAX_STRLEN );
56
57 p_typebuf = typebuf;
58 memset( typebuf, 0, sizeof(char) * NFD_MAX_STRLEN );
59 }
60
61 if ( *p_filterList == ';' || *p_filterList == '\0' )
62 {
63 /* end of filter -- add it to the dialog */
64
65 for(i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++);
66
67 commandArgs[i] = strdup(filterName);
68
69 filterName[0] = '\0';
70
71 if ( *p_filterList == '\0' )
72 break;
73 }
74
75 if ( !NFDi_IsFilterSegmentChar( *p_filterList ) )
76 {
77 *p_typebuf = *p_filterList;
78 p_typebuf++;
79 }
80
81 p_filterList++;
82 }
83
84 /* always append a wildcard option to the end*/
85
86 for(i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++);
87
88 commandArgs[i] = strdup("--file-filter=*.*");
89 }
90
91 static nfdresult_t ZenityCommon(char** command, int commandLen, const char* defaultPath, const char* filterList, char** stdOut)
92 {
93 if(defaultPath != NULL)
94 {
95 char* prefix = "--filename=";
96 int len = strlen(prefix) + strlen(defaultPath) + 1;
97
98 char* tmp = (char*) calloc(len, 1);
99 strcat(tmp, prefix);
100 strcat(tmp, defaultPath);
101
102 int i;
103 for(i = 0; command[i] != NULL && i < commandLen; i++);
104
105 command[i] = tmp;
106 }
107
108 AddFiltersToCommandArgs(command, commandLen, filterList);
109
110 int byteCount = 0;
111 int exitCode = 0;
112 int processInvokeError = runCommandArray(stdOut, &byteCount, &exitCode, 0, command);
113
114 for(int i = 0; command[i] != NULL && i < commandLen; i++)
115 free(command[i]);
116
117 nfdresult_t result = NFD_OKAY;
118
119 if(processInvokeError == COMMAND_NOT_FOUND)
120 {
121 NFDi_SetError(NO_ZENITY_MSG);
122 result = NFD_ERROR;
123 }
124 else
125 {
126 if(exitCode == 1)
127 result = NFD_CANCEL;
128 }
129
130 return result;
131 }
132
133
134 static nfdresult_t AllocPathSet(char* zenityList, nfdpathset_t *pathSet )
135 {
136 assert(zenityList);
137 assert(pathSet);
138
139 size_t len = strlen(zenityList) + 1;
140 pathSet->buf = NFDi_Malloc(len);
141
142 int numEntries = 1;
143
144 for(size_t i = 0; i < len; i++)
145 {
146 char ch = zenityList[i];
147
148 if(ch == '|')
149 {
150 numEntries++;
151 ch = '\0';
152 }
153
154 pathSet->buf[i] = ch;
155 }
156
157 pathSet->count = numEntries;
158 assert( pathSet->count > 0 );
159
160 pathSet->indices = NFDi_Malloc( sizeof(size_t)*pathSet->count );
161
162 int entry = 0;
163 pathSet->indices[0] = 0;
164 for(size_t i = 0; i < len; i++)
165 {
166 char ch = zenityList[i];
167
168 if(ch == '|')
169 {
170 entry++;
171 pathSet->indices[entry] = i + 1;
172 }
173 }
174
175 return NFD_OKAY;
176 }
177
178 /* public */
179
180 nfdresult_t NFD_OpenDialog( const char *filterList,
181 const nfdchar_t *defaultPath,
182 nfdchar_t **outPath )
183 {
184 int commandLen = 100;
185 char* command[commandLen];
186 memset(command, 0, commandLen * sizeof(char*));
187
188 command[0] = strdup("zenity");
189 command[1] = strdup("--file-selection");
190 command[2] = strdup("--title=Open File");
191
192 char* stdOut = NULL;
193 nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, filterList, &stdOut);
194
195 if(stdOut != NULL)
196 {
197 size_t len = strlen(stdOut);
198 *outPath = NFDi_Malloc(len);
199 memcpy(*outPath, stdOut, len);
200 (*outPath)[len-1] = '\0'; // trim out the final \n with a null terminator
201 free(stdOut);
202 }
203 else
204 {
205 *outPath = NULL;
206 }
207
208 return result;
209 }
210
211
212 nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
213 const nfdchar_t *defaultPath,
214 nfdpathset_t *outPaths )
215 {
216 int commandLen = 100;
217 char* command[commandLen];
218 memset(command, 0, commandLen * sizeof(char*));
219
220 command[0] = strdup("zenity");
221 command[1] = strdup("--file-selection");
222 command[2] = strdup("--title=Open Files");
223 command[3] = strdup("--multiple");
224
225 char* stdOut = NULL;
226 nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, filterList, &stdOut);
227
228 if(stdOut != NULL)
229 {
230 size_t len = strlen(stdOut);
231 stdOut[len-1] = '\0'; // remove trailing newline
232
233 if ( AllocPathSet( stdOut, outPaths ) == NFD_ERROR )
234 result = NFD_ERROR;
235
236 free(stdOut);
237 }
238 else
239 {
240 result = NFD_ERROR;
241 }
242
243 return result;
244 }
245
246 nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
247 const nfdchar_t *defaultPath,
248 nfdchar_t **outPath )
249 {
250 int commandLen = 100;
251 char* command[commandLen];
252 memset(command, 0, commandLen * sizeof(char*));
253
254 command[0] = strdup("zenity");
255 command[1] = strdup("--file-selection");
256 command[2] = strdup("--title=Save File");
257 command[3] = strdup("--save");
258
259 char* stdOut = NULL;
260 nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, filterList, &stdOut);
261
262 if(stdOut != NULL)
263 {
264 size_t len = strlen(stdOut);
265 *outPath = NFDi_Malloc(len);
266 memcpy(*outPath, stdOut, len);
267 (*outPath)[len-1] = '\0'; // trim out the final \n with a null terminator
268 free(stdOut);
269 }
270 else
271 {
272 *outPath = NULL;
273 }
274
275 return result;
276 }
277
278 nfdresult_t NFD_PickFolder(const nfdchar_t *defaultPath,
279 nfdchar_t **outPath)
280 {
281 int commandLen = 100;
282 char* command[commandLen];
283 memset(command, 0, commandLen * sizeof(char*));
284
285 command[0] = strdup("zenity");
286 command[1] = strdup("--file-selection");
287 command[2] = strdup("--directory");
288 command[3] = strdup("--title=Select folder");
289
290 char* stdOut = NULL;
291 nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, "", &stdOut);
292
293 if(stdOut != NULL)
294 {
295 size_t len = strlen(stdOut);
296 *outPath = NFDi_Malloc(len);
297 memcpy(*outPath, stdOut, len);
298 (*outPath)[len-1] = '\0'; // trim out the final \n with a null terminator
299 free(stdOut);
300 }
301 else
302 {
303 *outPath = NULL;
304 }
305
306 return result;
307 }