1 /* Signal Processing - Get outfile-name
2 
3  * Copyright (C) 1998 J.A. Bezemer
4  *
5  * Licensed under the terms of the GNU General Public License.
6  * ABSOLUTELY NO WARRANTY.
7  * See the file `COPYING' in this directory.
8  */
9 
10 #include "signpr_outfilenm.h"
11 #include "scrollmenu.h"
12 #include "stringinput.h"
13 #include "buttons.h"
14 #include "boxes.h"
15 #include "dirfilemenu.h"
16 #include "errorwindow.h"
17 #include "textwindow.h"
18 #include "checkfile.h"
19 #include "yesnowindow.h"
20 #include "helpline.h"
21 #include "clrscr.h"
22 
23 #include <stdlib.h>
24 #include <sys/stat.h>
25 #include <string.h>
26 #ifndef OLD_CURSES
27 #include <ncurses.h>
28 #else
29 #include <curses.h>
30 #endif
31 
32 
33 int
signproc_select_outfile(char * startdir,char * selectedfile)34 signproc_select_outfile (char *startdir, char *selectedfile)
35 /* Returns 0: canceled, 1: PrevScreen, 2: NextScreen */
36 {
37   scrollmenu_t dirfilelist;
38   stringinput_t string;
39   button_t next_button, cancel_button, prev_button;
40   int dont_stop = TRUE;
41   int returnval = 0;
42   int focus = 1;
43   int in_ch;
44   int i;
45   char helpstring[500];
46   char *charpointer;
47   struct stat filestats;
48   int oldselected;
49 
50   char *helplines[5] =
51   {
52     " Browse files and directories.                                  TAB: Next field",
53     " Enter name of destination sound file.                          TAB: Next field",
54     " To Signal Processing - Filter Selection.                       TAB: Next field",
55     " Back to main menu.                                             TAB: Next field",
56     " Start processing the signal.                                   TAB: Next field"};
57 
58   dirfilelist.y = 3;
59   dirfilelist.x = 5;
60   dirfilelist.h = 12;
61   dirfilelist.w = 32;
62   dirfilelist.firstonscreen = 0;
63   dirfilemenu (startdir, &dirfilelist);
64   dirfilelist.selected = dirfilelist.last_of_1st_part + 1;
65 
66   string.maxlen = 500;
67   string.string = (char *) malloc (string.maxlen * sizeof (char));
68   if (selectedfile[0] == '\0')
69     {
70       strcpy (string.string, startdir);
71       strcat (string.string, "processed.wav");
72     }
73   else
74     strcpy (string.string, selectedfile);
75   string.y = 17;
76   string.x = 5;
77   string.w = 70;
78   string.cursorpos = strlen (string.string);
79   string.firstcharonscreen = strlen (string.string) - string.w + 2;
80   if (string.firstcharonscreen < 0)
81     string.firstcharonscreen = 0;
82 
83   prev_button.text = " < Previous screen ";
84   prev_button.y = 20;
85   prev_button.x = 5;
86   prev_button.selected = FALSE;
87 
88   next_button.text = " Start ";
89   next_button.y = 20;
90   next_button.x = 68;
91   next_button.selected = FALSE;
92 
93   cancel_button.text = " Cancel ";
94   cancel_button.y = 20;
95   cancel_button.x = 36;
96   cancel_button.selected = FALSE;
97 
98   clearscreen (SIGNPR_OUTFILE_HEADERTEXT);
99 
100   do
101     {
102       if (focus == 2)
103 	prev_button.selected = TRUE;
104       else
105 	prev_button.selected = FALSE;
106 
107       if (focus == 3)
108 	cancel_button.selected = TRUE;
109       else
110 	cancel_button.selected = FALSE;
111 
112       if (focus == 4)
113 	next_button.selected = TRUE;
114       else
115 	next_button.selected = FALSE;
116 
117       dirfilelist.hasfocus = (focus == 0);
118 
119       scrollmenu_display (&dirfilelist);
120       mybox (dirfilelist.y - 1, dirfilelist.x - 1,
121 	     dirfilelist.h + 2, dirfilelist.w + 2);
122       mvprintw (dirfilelist.y - 1, dirfilelist.x + 1,
123 		"Files and directories:");
124 
125       stringinput_display (&string);
126       mybox (string.y - 1, string.x - 1, 3, string.w + 2);
127       mvprintw (string.y - 1, string.x + 1, "Destination File Name:");
128 
129       button_display (&prev_button);
130       mybox (prev_button.y - 1, prev_button.x - 1,
131 	     3, strlen (prev_button.text) + 2);
132       button_display (&cancel_button);
133       mybox (cancel_button.y - 1, cancel_button.x - 1,
134 	     3, strlen (cancel_button.text) + 2);
135       button_display (&next_button);
136       mybox (next_button.y - 1, next_button.x - 1,
137 	     3, strlen (next_button.text) + 2);
138 
139       helpline (helplines[focus]);
140 
141       if (focus == 1)
142 	stringinput_display (&string);
143       else
144 	move (0, 79);
145 
146       refresh ();
147 
148       in_ch = getch ();
149 
150       switch (focus)
151 	{
152 	case 0:		/* dirfilelist */
153 	  if (scrollmenu_stdkeys (in_ch, &dirfilelist) >= 0)
154 	    {
155 	      oldselected = dirfilelist.selected;
156 	      i = dirfilemenu_process_select (&dirfilelist,
157 					      helpstring);
158 	      if (i == 0)	/* filename in helpstring */
159 		{
160 		  strcpy (string.string, helpstring);
161 		  focus = 1;
162 		  string.cursorpos = strlen (string.string);
163 		  string.firstcharonscreen = 0;
164 		}
165 	      else
166 		/* dir in helpstring */
167 		{
168 		  scrollmenu_delete_menu (&dirfilelist);
169 		  dirfilemenu (helpstring, &dirfilelist);
170 		  if (dirfilelist.number == 0)
171 		    {
172 		      error_window (
173 				   "No permission to read this directory.");
174 		      header (SIGNPR_OUTFILE_HEADERTEXT);
175 		      scrollmenu_delete_menu (&dirfilelist);
176 		      dirfilemenu (startdir, &dirfilelist);
177 		      dirfilelist.selected = oldselected;
178 		    }
179 		  else
180 		    {
181 		      strcpy (startdir, helpstring);
182 		      dirfilelist.firstonscreen = 0;
183 
184 		      charpointer = strrchr (string.string, '/');
185 		      if (charpointer != NULL)
186 			strcat (helpstring, charpointer + 1);
187 		      else
188 			strcat (helpstring, string.string);
189 		      strcpy (string.string, helpstring);
190 		    }
191 		}
192 	    }
193 	  else
194 	    switch (in_ch)
195 	      {
196 	      case KEY_LEFT:
197 		focus--;
198 		break;
199 	      case KEY_RIGHT:
200 		focus++;
201 		break;
202 	      }
203 	  break;
204 
205 	case 1:		/* string */
206 	  stringinput_stdkeys (in_ch, &string);
207 	  if (in_ch == KEY_ENTER || in_ch == 13)
208 	    {
209 	      strcpy (helpstring, string.string);
210 
211 	      /* cut away last '/'-s */
212 	      while (strlen (helpstring) > 0 &&
213 		     helpstring[strlen (helpstring) - 1] == '/')
214 		helpstring[strlen (helpstring) - 1] = '\0';
215 
216 	      strcat (helpstring, "/");
217 
218 	      if (!stat (helpstring, &filestats) &&
219 		  S_ISDIR (filestats.st_mode))
220 		{
221 		  strcpy (startdir, helpstring);
222 		  scrollmenu_delete_menu (&dirfilelist);
223 		  dirfilemenu (startdir, &dirfilelist);
224 		  dirfilelist.firstonscreen = 0;
225 		  dirfilelist.selected =
226 		    dirfilelist.last_of_1st_part + 1;
227 		  strcpy (string.string, startdir);
228 		  string.cursorpos = strlen (string.string);
229 		  focus = 0;
230 		}
231 	      else		/* it's a file */
232 		focus = 4;
233 	    }
234 	  else
235 	    switch (in_ch)
236 	      {
237 	      case KEY_UP:
238 		focus--;
239 		break;
240 	      case KEY_DOWN:
241 		focus++;
242 		break;
243 	      }
244 	  break;
245 
246 	case 2:		/* < Previous */
247 	  if (in_ch == KEY_ENTER || in_ch == 13)
248 	    {
249 	      strcpy (selectedfile, string.string);
250 	      returnval = 1;
251 	      dont_stop = FALSE;
252 	    }
253 	  else
254 	    switch (in_ch)
255 	      {
256 	      case KEY_LEFT:
257 	      case KEY_UP:
258 		focus--;
259 		break;
260 	      case KEY_RIGHT:
261 	      case KEY_DOWN:
262 		focus++;
263 		break;
264 	      }
265 	  break;
266 
267 	case 3:		/* Cancel */
268 	  if (in_ch == KEY_ENTER || in_ch == 13)
269 	    {
270 	      returnval = 0;
271 	      dont_stop = FALSE;
272 	    }
273 	  else
274 	    switch (in_ch)
275 	      {
276 	      case KEY_LEFT:
277 	      case KEY_UP:
278 		focus--;
279 		break;
280 	      case KEY_RIGHT:
281 	      case KEY_DOWN:
282 		focus++;
283 		break;
284 	      }
285 	  break;
286 
287 	case 4:		/* Next > */
288 	  if (in_ch == KEY_ENTER || in_ch == 13)
289 	    switch (checkfile (string.string))
290 	      {
291 	      case FILE_EXISTS:
292 		if (yesno_window ("The specified file already exists. \
293 Overwrite it?", " Yes ", " No ", 0))
294 		  {
295 		    strcpy (selectedfile, string.string);
296 		    returnval = 2;
297 		    dont_stop = FALSE;
298 		  }
299 		else
300 		  {
301 		    string.cursorpos = strlen (string.string);
302 		    focus = 1;
303 		    header (SIGNPR_OUTFILE_HEADERTEXT);
304 		  }
305 		break;
306 
307 	      case DIR_EXISTS:
308 		error_window ("The specified name is of a directory. A \
309 file name must be specified.");
310 		header (SIGNPR_OUTFILE_HEADERTEXT);
311 		string.cursorpos = strlen (string.string);
312 		focus = 1;
313 		break;
314 
315 	      case DIR_OK_NEW_FILE:
316 		strcpy (selectedfile, string.string);
317 		returnval = 2;
318 		dont_stop = FALSE;
319 		break;
320 
321 	      case DIR_WRONG:
322 		if (*(string.string) == '|')
323 		  {
324 		    /* Piping to a command, so expect `dir' not to exist */
325 		    strcpy (selectedfile, string.string);
326 		    returnval = 2;
327 		    dont_stop = FALSE;
328 		  }
329 		else
330 		  {
331 		    error_window ("The directory of the specified file does \
332 not exist.");
333 		    header (SIGNPR_OUTFILE_HEADERTEXT);
334 		    string.cursorpos = strlen (string.string);
335 		    focus = 1;
336 		  }
337 		break;
338 
339 	      default:
340 		error_window ("Fell out of switch, signpr_outfilenm #1");
341 		header (SIGNPR_OUTFILE_HEADERTEXT);
342 		break;
343 	      }
344 	  else
345 	    switch (in_ch)
346 	      {
347 	      case KEY_LEFT:
348 	      case KEY_UP:
349 		focus--;
350 		break;
351 	      case KEY_RIGHT:
352 	      case KEY_DOWN:
353 		focus++;
354 		break;
355 	      }
356 	  break;
357 	}
358 
359       if (in_ch == 9)		/* TAB */
360 	focus++;
361 
362       if (in_ch == 27)
363 	dont_stop = FALSE;
364 
365       if (focus > 4)
366 	focus = 0;
367       if (focus < 0)
368 	focus = 4;
369     }
370   while (dont_stop);
371 
372   scrollmenu_delete_menu (&dirfilelist);
373   free (string.string);
374 
375   return returnval;
376 }
377