1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008 - DIGITEO - Sylvestre KOUMAR
4 * Copyright (C) 2008 - DIGITEO - Vincent COUVERT
5 * Copyright (C) 2009 - DIGITEO - Allan CORNET
6 *
7 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 *
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
15 *
16 */
17
18 /*------------------------------------------------------------------------*/
19 /* file: sci_uigetfile.cpp */
20 /* desc : interface for uigetfile routine */
21 /*------------------------------------------------------------------------*/
22 #include "CallJuigetfile.hxx"
23 #include "GiwsException.hxx"
24
25 extern "C"
26 {
27 #include <string.h>
28 #include <stdio.h>
29 #include "gw_gui.h"
30 #include "PATH_MAX.h"
31 #include "api_scilab.h"
32 #include "sci_malloc.h"
33 #include "localization.h"
34 #include "Scierror.h"
35 #include "expandPathVariable.h"
36 #include "freeArrayOfString.h"
37 #include "os_string.h"
38 #include "BOOL.h"
39 }
40 /*--------------------------------------------------------------------------*/
freePointersUigetfile(char ** selection,char ** selectionFileNames,char * selectionPathName,char * menuCallback,int selectionSize)41 static void freePointersUigetfile(char **selection, char **selectionFileNames, char *selectionPathName, char *menuCallback,int selectionSize)
42 {
43 if (selection)
44 {
45 for (int i = 0; i < selectionSize; i++)
46 {
47 if (selection[i])
48 {
49 delete selection[i];
50 selection[i] = NULL;
51 }
52 }
53 delete [] selection;
54 selection = NULL;
55 }
56 if (selectionPathName)
57 {
58 delete[] selectionPathName;
59 selectionPathName = NULL;
60 }
61 if (selectionFileNames)
62 {
63 for (int i = 0; i < selectionSize; i++)
64 {
65 if (selectionFileNames[i])
66 {
67 delete selectionFileNames[i];
68 selectionFileNames[i] = NULL;
69 }
70 }
71 delete [] selectionFileNames;
72 selectionFileNames = NULL;
73 }
74 if (menuCallback)
75 {
76 delete[] menuCallback;
77 menuCallback = NULL;
78 }
79 }
80 /*--------------------------------------------------------------------------*/
81 using namespace org_scilab_modules_gui_filechooser;
82
83 /*--------------------------------------------------------------------------*/
84
sci_uigetfile(char * fname,void * pvApiCtx)85 int sci_uigetfile(char *fname, void* pvApiCtx)
86 {
87 SciErr sciErr;
88
89 int nbRow = 0, nbCol = 0;
90 int nbRow2 = 0, nbCol2 = 0;
91 int nbRow3 = 0, nbCol3 = 0;
92 int nbRow4 = 0, nbCol4 = 0;
93
94 int nbRowOutSelection = 1, nbColOutSelection = 0;
95 int nbRowOutFilterIndex = 1, nbColOutFilterIndex = 1;
96 int nbRowOutPath = 1, nbColOutPath = 1;
97
98 char** mask = NULL;
99 char** description = NULL;
100
101 char* titleBox = NULL;
102 char* selectionPathName = NULL;
103 char* initialDirectory = NULL;
104
105 int multipleSelection = 0;
106
107 char **selection = NULL;
108 char **selectionFileNames = NULL;
109 int selectionSize = 0;
110 int filterIndex = 0;
111
112 char *menuCallback = NULL;
113
114 int* piAddr1 = NULL;
115 int* piAddr2 = NULL;
116 int* piAddr3 = NULL;
117 int* piAddr4 = NULL;
118
119 CheckInputArgument(pvApiCtx, 0, 4);
120 CheckOutputArgument(pvApiCtx, 0, 3);
121
122 //inputs checking
123 /* call uigetfile with 1 arg */
124 if (nbInputArgument(pvApiCtx) >= 1)
125 {
126 if (checkInputArgumentType(pvApiCtx, 1, sci_strings) == FALSE)
127 {
128 Scierror(999, _("%s: Wrong type for input argument #%d: A string matrix expected.\n"), fname, 1);
129 return 1;
130 }
131
132 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
133 if (sciErr.iErr)
134 {
135 printError(&sciErr, 0);
136 return 1;
137 }
138
139 if (getAllocatedMatrixOfString(pvApiCtx, piAddr1, &nbRow, &nbCol, &mask))
140 {
141 printError(&sciErr, 0);
142 return 1;
143 }
144
145 if (nbCol == 1)
146 {
147 // only masks of files are provided
148 description = NULL;
149 }
150 else if (nbCol == 2)
151 {
152 // mask contains both the masks for files and the description of each mask
153 // in the sequence [m1, m2,..., mn, d1, d2,...,dn].
154 // So description is at the middle of the array.
155 description = (char **)MALLOC(sizeof(char *) * nbRow);
156 for (int i = 0; i < nbRow; i++)
157 {
158 description[i] = os_strdup(mask[nbRow + i]);
159 }
160 }
161 else
162 {
163 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
164 Scierror(999, _("%s: Wrong size for input argument #%d: A string matrix expected.\n"), fname, 1);
165 return 1;
166 }
167 }
168
169 /* call uigetfile with 2 arg */
170 if (nbInputArgument(pvApiCtx) >= 2)
171 {
172 char *path = NULL;
173
174 if (checkInputArgumentType(pvApiCtx, 2, sci_strings) == FALSE)
175 {
176 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
177 freeArrayOfString(description, nbRow);
178 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2);
179 return 0;
180 }
181
182 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2);
183 if (sciErr.iErr)
184 {
185 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
186 freeArrayOfString(description, nbRow);
187 printError(&sciErr, 0);
188 return 1;
189 }
190
191 if (getAllocatedSingleString(pvApiCtx, piAddr2, &initialDirectory))
192 {
193 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
194 freeArrayOfString(description, nbRow);
195 printError(&sciErr, 0);
196 return 1;
197 }
198
199 path = expandPathVariable(initialDirectory);
200 freeAllocatedSingleString(initialDirectory);
201 initialDirectory = path;
202 }
203
204 /* call uigetfile with 3 arg */
205 if (nbInputArgument(pvApiCtx) >= 3)
206 {
207 if (checkInputArgumentType(pvApiCtx, 3, sci_strings) == FALSE)
208 {
209 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 3);
210 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
211 freeArrayOfString(description, nbRow);
212 return 1;
213 }
214
215 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddr3);
216 if (sciErr.iErr)
217 {
218 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
219 freeArrayOfString(description, nbRow);
220 printError(&sciErr, 0);
221 return 1;
222 }
223
224 if (getAllocatedSingleString(pvApiCtx, piAddr3, &titleBox))
225 {
226 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
227 freeArrayOfString(description, nbRow);
228 printError(&sciErr, 0);
229 return 1;
230 }
231 }
232
233 try
234 {
235 /* Call Java */
236 switch (nbInputArgument(pvApiCtx))
237 {
238 case 0:
239 CallJuigetfileWithoutInput();
240 break;
241
242 case 1:
243 CallJuigetfileOnlyWithMask(mask, description, nbRow);
244 break;
245
246 case 2:
247 CallJuigetfileWithMaskAndInitialdirectory(mask, description, nbRow, initialDirectory);
248 break;
249
250 case 3:
251 CallJuigetfileWithoutMultipleSelection(mask, description, nbRow, initialDirectory, titleBox);
252 break;
253
254 case 4:
255 {
256 if (checkInputArgumentType(pvApiCtx, 4, sci_boolean) == FALSE)
257 {
258 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 4);
259 freeArrayOfString(description, nbRow);
260 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
261 freeAllocatedSingleString(initialDirectory);
262 freeAllocatedSingleString(titleBox);
263
264 return 0;
265 }
266
267 sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddr4);
268 if (sciErr.iErr)
269 {
270 freeArrayOfString(description, nbRow);
271 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
272 freeAllocatedSingleString(initialDirectory);
273 freeAllocatedSingleString(titleBox);
274 printError(&sciErr, 0);
275 return 1;
276 }
277
278 if (getScalarBoolean(pvApiCtx, piAddr4, &multipleSelection))
279 {
280 freeArrayOfString(description, nbRow);
281 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
282 freeAllocatedSingleString(initialDirectory);
283 freeAllocatedSingleString(titleBox);
284 printError(&sciErr, 0);
285 return 1;
286 }
287
288 CallJuigetfile(mask, description, nbRow, initialDirectory, titleBox, BOOLtobool(multipleSelection));
289 }
290 break;
291
292 default:
293 // never here
294 break;
295 }
296
297 // free pointer
298 freeArrayOfString(description, nbRow);
299 freeAllocatedMatrixOfString(nbRow, nbCol, mask);
300 freeAllocatedSingleString(initialDirectory);
301 freeAllocatedSingleString(titleBox);
302
303 // Get return values
304 selection = getJuigetfileSelection();
305 selectionPathName = getJuigetfileSelectionPathName();
306 selectionFileNames = getJuigetfileSelectionFileNames();
307 selectionSize = getJuigetfileSelectionSize();
308 filterIndex = getJuigetfileFilterIndex();
309 menuCallback = getJuigetfileMenuCallback();
310 }
311 catch (const GiwsException::JniCallMethodException & exception)
312 {
313 Scierror(999, "%s: %s\n", fname, exception.getJavaDescription().c_str());
314 return 0;
315 }
316 catch (const GiwsException::JniException & e)
317 {
318 Scierror(999, _("%s: A Java exception arisen:\n%s"), fname, e.whatStr().c_str());
319 return 0;
320 }
321
322 // nbColOutSelection
323 nbColOutSelection = selectionSize;
324
325 //if cancel is selected on the filechooser
326 if (strcmp(selection[0], "") == 0)
327 {
328 // "" is returned as filename
329 sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 1, selection);
330 if (sciErr.iErr)
331 {
332 printError(&sciErr, 0);
333 Scierror(999, _("%s: Memory allocation error.\n"), fname);
334 freePointersUigetfile(selection, selectionFileNames, selectionPathName, menuCallback, selectionSize);
335 return 1;
336 }
337
338 AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
339
340 if (nbOutputArgument(pvApiCtx) > 1)
341 {
342 // "" is returned as pathname
343 sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 2, 1, 1, selection);
344 if (sciErr.iErr)
345 {
346 printError(&sciErr, 0);
347 Scierror(999, _("%s: Memory allocation error.\n"), fname);
348 freePointersUigetfile(selection, selectionFileNames, selectionPathName, menuCallback, selectionSize);
349 return 1;
350 }
351
352 AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
353 }
354
355 if (nbOutputArgument(pvApiCtx) > 2)
356 {
357 // 0 is returned as pathname
358 double tmp = 0;
359 sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 3, 1, 1, &tmp);
360 if (sciErr.iErr)
361 {
362 printError(&sciErr, 0);
363 Scierror(999, _("%s: Memory allocation error.\n"), fname);
364 freePointersUigetfile(selection, selectionFileNames, selectionPathName, menuCallback, selectionSize);
365 return 1;
366 }
367
368 AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx) + 3;
369 }
370
371 freePointersUigetfile(selection, selectionFileNames, selectionPathName, menuCallback, selectionSize);
372 ReturnArguments(pvApiCtx);
373 return 0;
374 }
375
376 // Only one output then it contains path+filenames
377 if (nbOutputArgument(pvApiCtx) <= 1)
378 {
379 sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRowOutSelection, nbColOutSelection, selection);
380 if (sciErr.iErr)
381 {
382 printError(&sciErr, 0);
383 Scierror(999, _("%s: Memory allocation error.\n"), fname);
384 freePointersUigetfile(selection, selectionFileNames, selectionPathName, menuCallback, selectionSize);
385 return 1;
386 }
387
388 AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
389 freePointersUigetfile(selection, selectionFileNames, selectionPathName, menuCallback, selectionSize);
390 ReturnArguments(pvApiCtx);
391 return 0;
392 }
393
394 // More than one output
395 sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRowOutSelection, nbColOutSelection, selectionFileNames);
396 if (sciErr.iErr)
397 {
398 printError(&sciErr, 0);
399 Scierror(999, _("%s: Memory allocation error.\n"), fname);
400 freePointersUigetfile(selection, selectionFileNames, selectionPathName, menuCallback, selectionSize);
401 return 1;
402 }
403
404 if (createSingleString(pvApiCtx, nbInputArgument(pvApiCtx) + 2, selectionPathName))
405 {
406 printError(&sciErr, 0);
407 freePointersUigetfile(selection, selectionFileNames, selectionPathName, menuCallback, selectionSize);
408 return 1;
409 }
410
411 AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
412 AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
413
414 if (nbOutputArgument(pvApiCtx) > 2)
415 {
416 if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 3, filterIndex))
417 {
418 printError(&sciErr, 0);
419 return 1;
420 }
421 AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx) + 3;
422 }
423
424 freePointersUigetfile(selection, selectionFileNames, selectionPathName, menuCallback, selectionSize);
425 ReturnArguments(pvApiCtx);
426 return 0;
427 }
428
429 /*--------------------------------------------------------------------------*/
430