Removed type SHELL_ESCAPED_STR in favour of plain char*
[midnight-commander.git] / src / filegui.c
blob87ea36cad34ffcc56901805aa985ba9ef763de81
1 /* File management GUI for the text mode edition
3 * Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
4 * 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 *
6 * Written by: 1994, 1995 Janne Kukonlehto
7 * 1994, 1995 Fred Leeflang
8 * 1994, 1995, 1996 Miguel de Icaza
9 * 1995, 1996 Jakub Jelinek
10 * 1997 Norbert Warmuth
11 * 1998 Pavel Machek
13 * The copy code was based in GNU's cp, and was written by:
14 * Torbjorn Granlund, David MacKenzie, and Jim Meyering.
16 * The move code was based in GNU's mv, and was written by:
17 * Mike Parker and David MacKenzie.
19 * Janne Kukonlehto added much error recovery to them for being used
20 * in an interactive program.
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 * Please note that all dialogs used here must be safe for background
39 * operations.
43 /* {{{ Include files */
45 #include <config.h>
47 #include <errno.h>
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <unistd.h>
55 #include <mhl/memory.h>
56 #include <mhl/string.h>
58 #include "global.h"
59 #include "setup.h" /* verbose */
60 #include "dialog.h" /* do_refresh() */
61 #include "color.h" /* dialog_colors */
62 #include "widget.h" /* WLabel */
63 #include "main-widgets.h"
64 #include "main.h" /* the_hint */
65 #include "wtools.h" /* QuickDialog */
66 #include "panel.h" /* current_panel */
67 #include "fileopctx.h" /* FILE_CONT */
68 #include "filegui.h"
69 #include "key.h" /* get_event */
70 #include "util.h" /* strip_password() */
72 /* }}} */
74 /* Hack: the vfs code should not rely on this */
75 #define WITH_FULL_PATHS 1
77 /* This structure describes the UI and internal data required by a file
78 * operation context.
80 typedef struct {
81 /* ETA and bps */
83 int showing_eta;
84 int showing_bps;
85 int eta_extra;
87 /* Dialog and widgets for the operation progress window */
89 Dlg_head *op_dlg;
91 WLabel *file_label[2];
92 WLabel *file_string[2];
93 WLabel *progress_label[3];
94 WGauge *progress_gauge[3];
95 WLabel *eta_label;
96 WLabel *bps_label;
97 WLabel *stalled_label;
99 /* Query replace dialog */
101 Dlg_head *replace_dlg;
102 const char *replace_filename;
103 int replace_result;
104 struct stat *s_stat, *d_stat;
105 } FileOpContextUI;
108 /* Used to save the hint line */
109 static int last_hint_line;
111 /* File operate window sizes */
112 #define WX 62
113 #define WY 10
114 #define BY 10
115 #define WX_ETA_EXTRA 12
117 #define FCOPY_GAUGE_X 14
118 #define FCOPY_LABEL_X 5
120 /* Used for button result values */
121 enum {
122 REPLACE_YES = B_USER,
123 REPLACE_NO,
124 REPLACE_APPEND,
125 REPLACE_ALWAYS,
126 REPLACE_UPDATE,
127 REPLACE_NEVER,
128 REPLACE_ABORT,
129 REPLACE_SIZE,
130 REPLACE_REGET
133 static FileProgressStatus
134 check_progress_buttons (FileOpContext *ctx)
136 int c;
137 Gpm_Event event;
138 FileOpContextUI *ui;
140 if (ctx->ui == NULL)
141 return FILE_CONT;
143 ui = ctx->ui;
145 event.x = -1; /* Don't show the GPM cursor */
146 c = get_event (&event, 0, 0);
147 if (c == EV_NONE)
148 return FILE_CONT;
150 /* Reinitialize to avoid old values after events other than
151 selecting a button */
152 ui->op_dlg->ret_value = FILE_CONT;
154 dlg_process_event (ui->op_dlg, c, &event);
155 switch (ui->op_dlg->ret_value) {
156 case FILE_SKIP:
157 return FILE_SKIP;
158 break;
159 case B_CANCEL:
160 case FILE_ABORT:
161 return FILE_ABORT;
162 break;
163 default:
164 return FILE_CONT;
168 /* {{{ File progress display routines */
170 void
171 file_op_context_create_ui (FileOpContext *ctx, int with_eta)
173 FileOpContextUI *ui;
174 int x_size;
175 int minus;
176 int eta_offset;
177 const char *sixty;
178 const char *fifteen;
180 g_return_if_fail (ctx != NULL);
181 g_return_if_fail (ctx->ui == NULL);
183 ui = g_new0 (FileOpContextUI, 1);
184 ctx->ui = ui;
186 minus = verbose ? 0 : 3;
187 eta_offset = with_eta ? (WX_ETA_EXTRA) / 2 : 0;
189 sixty = "";
190 fifteen = "";
192 ctx->recursive_result = 0;
194 ui->replace_result = 0;
195 ui->showing_eta = with_eta;
196 ui->showing_bps = with_eta;
197 ui->eta_extra = with_eta ? WX_ETA_EXTRA : 0;
198 x_size = (WX + 4) + ui->eta_extra;
200 ui->op_dlg =
201 create_dlg (0, 0, WY - minus + 4, x_size, dialog_colors, NULL,
202 NULL, op_names[ctx->operation],
203 DLG_CENTER | DLG_REVERSE);
205 last_hint_line = the_hint->widget.y;
206 if ((ui->op_dlg->y + ui->op_dlg->lines) > last_hint_line)
207 the_hint->widget.y = ui->op_dlg->y + ui->op_dlg->lines + 1;
209 add_widget (ui->op_dlg,
210 button_new (BY - minus, WX - 19 + eta_offset, FILE_ABORT,
211 NORMAL_BUTTON, _("&Abort"), 0));
212 add_widget (ui->op_dlg,
213 button_new (BY - minus, 14 + eta_offset, FILE_SKIP,
214 NORMAL_BUTTON, _("&Skip"), 0));
216 add_widget (ui->op_dlg, ui->progress_gauge[2] =
217 gauge_new (7, FCOPY_GAUGE_X, 0, 100, 0));
218 add_widget (ui->op_dlg, ui->progress_label[2] =
219 label_new (7, FCOPY_LABEL_X, fifteen));
220 add_widget (ui->op_dlg, ui->bps_label = label_new (7, WX, ""));
222 add_widget (ui->op_dlg, ui->progress_gauge[1] =
223 gauge_new (8, FCOPY_GAUGE_X, 0, 100, 0));
224 add_widget (ui->op_dlg, ui->progress_label[1] =
225 label_new (8, FCOPY_LABEL_X, fifteen));
226 add_widget (ui->op_dlg, ui->stalled_label = label_new (8, WX, ""));
228 add_widget (ui->op_dlg, ui->progress_gauge[0] =
229 gauge_new (6, FCOPY_GAUGE_X, 0, 100, 0));
230 add_widget (ui->op_dlg, ui->progress_label[0] =
231 label_new (6, FCOPY_LABEL_X, fifteen));
232 add_widget (ui->op_dlg, ui->eta_label = label_new (6, WX, ""));
234 add_widget (ui->op_dlg, ui->file_string[1] =
235 label_new (4, FCOPY_GAUGE_X, sixty));
236 add_widget (ui->op_dlg, ui->file_label[1] =
237 label_new (4, FCOPY_LABEL_X, fifteen));
238 add_widget (ui->op_dlg, ui->file_string[0] =
239 label_new (3, FCOPY_GAUGE_X, sixty));
240 add_widget (ui->op_dlg, ui->file_label[0] =
241 label_new (3, FCOPY_LABEL_X, fifteen));
243 /* We will manage the dialog without any help, that's why
244 we have to call init_dlg */
245 init_dlg (ui->op_dlg);
246 ui->op_dlg->running = 1;
249 void
250 file_op_context_destroy_ui (FileOpContext *ctx)
252 FileOpContextUI *ui;
254 g_return_if_fail (ctx != NULL);
256 if (ctx->ui) {
257 ui = ctx->ui;
259 dlg_run_done (ui->op_dlg);
260 destroy_dlg (ui->op_dlg);
261 g_free (ui);
264 the_hint->widget.y = last_hint_line;
266 ctx->ui = NULL;
269 static FileProgressStatus
270 show_no_bar (FileOpContext *ctx, int n)
272 FileOpContextUI *ui;
274 if (ctx->ui == NULL)
275 return FILE_CONT;
277 ui = ctx->ui;
279 if (n >= 0) {
280 label_set_text (ui->progress_label[n], "");
281 gauge_show (ui->progress_gauge[n], 0);
283 return check_progress_buttons (ctx);
286 static FileProgressStatus
287 show_bar (FileOpContext *ctx, int n, double done, double total)
289 FileOpContextUI *ui;
291 if (ctx->ui == NULL)
292 return FILE_CONT;
294 ui = ctx->ui;
297 * Gauge needs integers, so give it with integers between 0 and 1023.
298 * This precision should be quite reasonable.
300 gauge_set_value (ui->progress_gauge[n], 1024,
301 (int) (1024 * done / total));
302 gauge_show (ui->progress_gauge[n], 1);
303 return check_progress_buttons (ctx);
306 static void
307 file_eta_show (FileOpContext *ctx)
309 int eta_hours, eta_mins, eta_s;
310 char eta_buffer[BUF_TINY];
311 FileOpContextUI *ui;
313 if (ctx->ui == NULL)
314 return;
316 ui = ctx->ui;
318 if (!ui->showing_eta)
319 return;
321 if (ctx->eta_secs > 0.5) {
322 eta_hours = ctx->eta_secs / (60 * 60);
323 eta_mins = (ctx->eta_secs - (eta_hours * 60 * 60)) / 60;
324 eta_s = ctx->eta_secs - (eta_hours * 60 * 60 + eta_mins * 60);
325 g_snprintf (eta_buffer, sizeof (eta_buffer), _("ETA %d:%02d.%02d"),
326 eta_hours, eta_mins, eta_s);
327 } else
328 *eta_buffer = 0;
330 label_set_text (ui->eta_label, eta_buffer);
333 static void
334 file_bps_show (FileOpContext *ctx)
336 char bps_buffer[BUF_TINY];
337 FileOpContextUI *ui;
339 if (ctx->ui == NULL)
340 return;
342 ui = ctx->ui;
344 if (!ui->showing_bps)
345 return;
347 if (ctx->bps > 1024 * 1024) {
348 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%.2f MB/s"),
349 ctx->bps / (1024 * 1024.0));
350 } else if (ctx->bps > 1024) {
351 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%.2f KB/s"),
352 ctx->bps / 1024.0);
353 } else if (ctx->bps > 1) {
354 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%ld B/s"),
355 ctx->bps);
356 } else
357 *bps_buffer = 0;
359 label_set_text (ui->bps_label, bps_buffer);
362 FileProgressStatus
363 file_progress_show (FileOpContext *ctx, off_t done, off_t total)
365 FileOpContextUI *ui;
367 g_return_val_if_fail (ctx != NULL, FILE_CONT);
369 if (ctx->ui == NULL)
370 return FILE_CONT;
372 ui = ctx->ui;
374 if (!verbose)
375 return check_progress_buttons (ctx);
376 if (total > 0) {
377 label_set_text (ui->progress_label[0], _("File"));
378 file_eta_show (ctx);
379 file_bps_show (ctx);
380 return show_bar (ctx, 0, done, total);
381 } else
382 return show_no_bar (ctx, 0);
385 FileProgressStatus
386 file_progress_show_count (FileOpContext *ctx, off_t done, off_t total)
388 FileOpContextUI *ui;
390 g_return_val_if_fail (ctx != NULL, FILE_CONT);
392 if (ctx->ui == NULL)
393 return FILE_CONT;
395 ui = ctx->ui;
397 if (!verbose)
398 return check_progress_buttons (ctx);
399 if (total > 0) {
400 label_set_text (ui->progress_label[1], _("Count"));
401 return show_bar (ctx, 1, done, total);
402 } else
403 return show_no_bar (ctx, 1);
406 FileProgressStatus
407 file_progress_show_bytes (FileOpContext *ctx, double done, double total)
409 FileOpContextUI *ui;
411 g_return_val_if_fail (ctx != NULL, FILE_CONT);
413 if (ctx->ui == NULL)
414 return FILE_CONT;
416 ui = ctx->ui;
418 if (!verbose)
419 return check_progress_buttons (ctx);
420 if (total > 0) {
421 label_set_text (ui->progress_label[2], _("Bytes"));
422 return show_bar (ctx, 2, done, total);
423 } else
424 return show_no_bar (ctx, 2);
427 /* }}} */
429 #define truncFileString(ui, s) name_trunc (s, ui->eta_extra + 47)
430 #define truncFileStringSecure(ui, s) path_trunc (s, ui->eta_extra + 47)
432 FileProgressStatus
433 file_progress_show_source (FileOpContext *ctx, const char *s)
435 FileOpContextUI *ui;
437 g_return_val_if_fail (ctx != NULL, FILE_CONT);
439 if (ctx->ui == NULL)
440 return FILE_CONT;
442 ui = ctx->ui;
444 if (s != NULL) {
445 #ifdef WITH_FULL_PATHS
446 int i = strlen (current_panel->cwd);
448 /* We remove the full path we have added before */
449 if (!strncmp (s, current_panel->cwd, i)) {
450 if (s[i] == PATH_SEP)
451 s += i + 1;
453 #endif /* WITH_FULL_PATHS */
455 label_set_text (ui->file_label[0], _("Source"));
456 label_set_text (ui->file_string[0], truncFileString (ui, s));
457 return check_progress_buttons (ctx);
458 } else {
459 label_set_text (ui->file_label[0], "");
460 label_set_text (ui->file_string[0], "");
461 return check_progress_buttons (ctx);
465 FileProgressStatus
466 file_progress_show_target (FileOpContext *ctx, const char *s)
468 FileOpContextUI *ui;
470 g_return_val_if_fail (ctx != NULL, FILE_CONT);
472 if (ctx->ui == NULL)
473 return FILE_CONT;
475 ui = ctx->ui;
477 if (s != NULL) {
478 label_set_text (ui->file_label[1], _("Target"));
479 label_set_text (ui->file_string[1], truncFileStringSecure (ui, s));
480 return check_progress_buttons (ctx);
481 } else {
482 label_set_text (ui->file_label[1], "");
483 label_set_text (ui->file_string[1], "");
484 return check_progress_buttons (ctx);
488 FileProgressStatus
489 file_progress_show_deleting (FileOpContext *ctx, const char *s)
491 FileOpContextUI *ui;
493 g_return_val_if_fail (ctx != NULL, FILE_CONT);
495 if (ctx->ui == NULL)
496 return FILE_CONT;
498 ui = ctx->ui;
500 label_set_text (ui->file_label[0], _("Deleting"));
501 label_set_text (ui->file_label[0], truncFileStringSecure (ui, s));
502 return check_progress_buttons (ctx);
505 #define X_TRUNC 52
508 * FIXME: probably it is better to replace this with quick dialog machinery,
509 * but actually I'm not familiar with it and have not much time :(
510 * alex
512 static struct {
513 const char *text;
514 int ypos, xpos;
515 int value; /* 0 for labels */
516 } rd_widgets[] = {
518 N_("Target file \"%s\" already exists!"), 3, 4, 0}, {
519 N_("&Abort"), BY + 3, 25, REPLACE_ABORT}, {
520 N_("If &size differs"), BY + 1, 28, REPLACE_SIZE}, {
521 N_("Non&e"), BY, 47, REPLACE_NEVER}, {
522 N_("&Update"), BY, 36, REPLACE_UPDATE}, {
523 N_("A&ll"), BY, 28, REPLACE_ALWAYS}, {
524 N_("Overwrite all targets?"), BY, 4, 0}, {
525 N_("&Reget"), BY - 1, 28, REPLACE_REGET}, {
526 N_("A&ppend"), BY - 2, 45, REPLACE_APPEND}, {
527 N_("&No"), BY - 2, 37, REPLACE_NO}, {
528 N_("&Yes"), BY - 2, 28, REPLACE_YES}, {
529 N_("Overwrite this target?"), BY - 2, 4, 0}, {
530 #if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined _LARGE_FILES && _LARGE_FILES)
531 N_("Target date: %s, size %llu"), 6, 4, 0}, {
532 N_("Source date: %s, size %llu"), 5, 4, 0}
533 #else
534 N_("Target date: %s, size %u"), 6, 4, 0}, {
535 N_("Source date: %s, size %u"), 5, 4, 0}
536 #endif
539 #define ADD_RD_BUTTON(i)\
540 add_widget (ui->replace_dlg,\
541 button_new (rd_widgets [i].ypos, rd_widgets [i].xpos, rd_widgets [i].value,\
542 NORMAL_BUTTON, rd_widgets [i].text, 0))
544 #define ADD_RD_LABEL(ui,i,p1,p2)\
545 g_snprintf (buffer, sizeof (buffer), rd_widgets [i].text, p1, p2);\
546 add_widget (ui->replace_dlg,\
547 label_new (rd_widgets [i].ypos, rd_widgets [i].xpos, buffer))
549 static void
550 init_replace (FileOpContext *ctx, enum OperationMode mode)
552 FileOpContextUI *ui;
553 char buffer[BUF_SMALL];
554 const char *title;
555 static int rd_xlen = 60, rd_trunc = X_TRUNC;
557 #ifdef ENABLE_NLS
558 static int i18n_flag;
559 if (!i18n_flag) {
560 int l1, l2, l, row;
561 register int i = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
562 while (i--)
563 rd_widgets[i].text = _(rd_widgets[i].text);
566 * longest of "Overwrite..." labels
567 * (assume "Target date..." are short enough)
569 l1 = max (strlen (rd_widgets[6].text),
570 strlen (rd_widgets[11].text));
572 /* longest of button rows */
573 i = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
574 for (row = l = l2 = 0; i--;) {
575 if (rd_widgets[i].value != 0) {
576 if (row != rd_widgets[i].ypos) {
577 row = rd_widgets[i].ypos;
578 l2 = max (l2, l);
579 l = 0;
581 l += strlen (rd_widgets[i].text) + 4;
584 l2 = max (l2, l); /* last row */
585 rd_xlen = max (rd_xlen, l1 + l2 + 8);
586 rd_trunc = rd_xlen - 6;
588 /* Now place buttons */
589 l1 += 5; /* start of first button in the row */
590 i = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
592 for (l = l1, row = 0; --i > 1;) {
593 if (rd_widgets[i].value != 0) {
594 if (row != rd_widgets[i].ypos) {
595 row = rd_widgets[i].ypos;
596 l = l1;
598 rd_widgets[i].xpos = l;
599 l += strlen (rd_widgets[i].text) + 4;
602 /* Abort button is centered */
603 rd_widgets[1].xpos =
604 (rd_xlen - strlen (rd_widgets[1].text) - 3) / 2;
606 #endif /* ENABLE_NLS */
608 ui = ctx->ui;
610 if (mode == Foreground)
611 title = _(" File exists ");
612 else
613 title = _(" Background process: File exists ");
615 /* FIXME - missing help node */
616 ui->replace_dlg =
617 create_dlg (0, 0, 16, rd_xlen, alarm_colors, NULL, "[Replace]",
618 title, DLG_CENTER | DLG_REVERSE);
621 ADD_RD_LABEL (ui, 0,
622 name_trunc (ui->replace_filename,
623 rd_trunc - strlen (rd_widgets[0].text)), 0);
624 ADD_RD_BUTTON (1);
626 ADD_RD_BUTTON (2);
627 ADD_RD_BUTTON (3);
628 ADD_RD_BUTTON (4);
629 ADD_RD_BUTTON (5);
630 ADD_RD_LABEL (ui, 6, 0, 0);
632 /* "this target..." widgets */
633 if (!S_ISDIR (ui->d_stat->st_mode)) {
634 if ((ctx->operation == OP_COPY) && (ui->d_stat->st_size != 0)
635 && (ui->s_stat->st_size > ui->d_stat->st_size))
636 ADD_RD_BUTTON (7); /* reget */
638 ADD_RD_BUTTON (8); /* Overwrite all targets? */
640 ADD_RD_BUTTON (9);
641 ADD_RD_BUTTON (10);
642 ADD_RD_LABEL (ui, 11, 0, 0);
644 ADD_RD_LABEL (ui, 12, file_date (ui->d_stat->st_mtime),
645 (off_t) ui->d_stat->st_size);
646 ADD_RD_LABEL (ui, 13, file_date (ui->s_stat->st_mtime),
647 (off_t) ui->s_stat->st_size);
650 void
651 file_progress_set_stalled_label (FileOpContext *ctx, const char *stalled_msg)
653 FileOpContextUI *ui;
655 g_return_if_fail (ctx != NULL);
657 if (ctx->ui == NULL)
658 return;
660 ui = ctx->ui;
662 label_set_text (ui->stalled_label, stalled_msg);
665 FileProgressStatus
666 file_progress_real_query_replace (FileOpContext *ctx,
667 enum OperationMode mode, const char *destname,
668 struct stat *_s_stat,
669 struct stat *_d_stat)
671 FileOpContextUI *ui;
673 g_return_val_if_fail (ctx != NULL, FILE_CONT);
674 g_return_val_if_fail (ctx->ui != NULL, FILE_CONT);
676 ui = ctx->ui;
678 if (ui->replace_result < REPLACE_ALWAYS) {
679 ui->replace_filename = destname;
680 ui->s_stat = _s_stat;
681 ui->d_stat = _d_stat;
682 init_replace (ctx, mode);
683 run_dlg (ui->replace_dlg);
684 ui->replace_result = ui->replace_dlg->ret_value;
685 if (ui->replace_result == B_CANCEL)
686 ui->replace_result = REPLACE_ABORT;
687 destroy_dlg (ui->replace_dlg);
690 switch (ui->replace_result) {
691 case REPLACE_UPDATE:
692 do_refresh ();
693 if (_s_stat->st_mtime > _d_stat->st_mtime)
694 return FILE_CONT;
695 else
696 return FILE_SKIP;
698 case REPLACE_SIZE:
699 do_refresh ();
700 if (_s_stat->st_size == _d_stat->st_size)
701 return FILE_SKIP;
702 else
703 return FILE_CONT;
705 case REPLACE_REGET:
706 /* Careful: we fall through and set do_append */
707 ctx->do_reget = _d_stat->st_size;
709 case REPLACE_APPEND:
710 ctx->do_append = 1;
712 case REPLACE_YES:
713 case REPLACE_ALWAYS:
714 do_refresh ();
715 return FILE_CONT;
716 case REPLACE_NO:
717 case REPLACE_NEVER:
718 do_refresh ();
719 return FILE_SKIP;
720 case REPLACE_ABORT:
721 default:
722 return FILE_ABORT;
726 #define FMDY 13
727 #define FMD_XLEN 64
728 extern int fmd_xlen;
729 static QuickWidget fmd_widgets[] = {
731 #define FMCB0 FMDC
732 #define FMCB12 0
733 #define FMCB11 1
734 /* follow symlinks and preserve Attributes must be the first */
735 {quick_checkbox, 3, 64, 8, FMDY, N_("preserve &Attributes"), 9, 0,
736 0 /* &op_preserve */ , 0, NULL},
737 {quick_checkbox, 3, 64, 7, FMDY, N_("follow &Links"), 7, 0,
738 0 /* &file_mask_op_follow_links */ , 0, NULL},
739 {quick_label, 3, 64, 5, FMDY, N_("to:"), 0, 0, 0, 0, NULL},
740 {quick_checkbox, 37, 64, 4, FMDY, N_("&Using shell patterns"), 0, 0,
741 0 /* &source_easy_patterns */ , 0, NULL},
742 {quick_input, 3, 64, 3, FMDY, "", 58,
743 0, 0, 0, "input-def"},
744 #define FMDI1 4
745 #define FMDI2 5
746 #define FMDC 3
747 {quick_input, 3, 64, 6, FMDY, "", 58, 0,
748 0, 0, "input2"},
749 #define FMDI0 6
750 {quick_label, 3, 64, 2, FMDY, "", 0, 0, 0, 0, NULL},
751 #define FMBRGT 7
752 {quick_button, 42, 64, 9, FMDY, N_("&Cancel"), 0, B_CANCEL, 0, 0,
753 NULL},
754 #undef SKIP
755 #ifdef WITH_BACKGROUND
756 # define SKIP 5
757 # define FMCB21 11
758 # define FMCB22 10
759 # define FMBLFT 9
760 # define FMBMID 8
761 {quick_button, 25, 64, 9, FMDY, N_("&Background"), 0, B_USER, 0, 0,
762 NULL},
763 #else /* WITH_BACKGROUND */
764 # define SKIP 4
765 # define FMCB21 10
766 # define FMCB22 9
767 # define FMBLFT 8
768 # undef FMBMID
769 #endif
770 {quick_button, 14, 64, 9, FMDY, N_("&OK"), 0, B_ENTER, 0, 0, NULL},
771 {quick_checkbox, 42, 64, 8, FMDY, N_("&Stable Symlinks"), 0, 0,
772 0 /* &file_mask_stable_symlinks */ , 0, NULL},
773 {quick_checkbox, 31, 64, 7, FMDY, N_("&Dive into subdir if exists"), 0,
775 0 /* &dive_into_subdirs */ , 0, NULL},
776 NULL_QuickWidget
779 static int
780 is_wildcarded (char *p)
782 for (; *p; p++) {
783 if (*p == '*')
784 return 1;
785 else if (*p == '\\' && p[1] >= '1' && p[1] <= '9')
786 return 1;
788 return 0;
791 void
792 fmd_init_i18n (int force)
794 #ifdef ENABLE_NLS
795 static int initialized = FALSE;
796 register int i;
797 int len;
799 if (initialized && !force)
800 return;
802 for (i = sizeof (op_names) / sizeof (op_names[0]); i--;)
803 op_names[i] = _(op_names[i]);
805 i = sizeof (fmd_widgets) / sizeof (fmd_widgets[0]) - 1;
806 while (i--)
807 if (fmd_widgets[i].text[0] != '\0')
808 fmd_widgets[i].text = _(fmd_widgets[i].text);
810 len = strlen (fmd_widgets[FMCB11].text)
811 + strlen (fmd_widgets[FMCB21].text) + 15;
812 fmd_xlen = max (fmd_xlen, len);
814 len = strlen (fmd_widgets[FMCB12].text)
815 + strlen (fmd_widgets[FMCB22].text) + 15;
816 fmd_xlen = max (fmd_xlen, len);
818 len = strlen (fmd_widgets[FMBRGT].text)
819 + strlen (fmd_widgets[FMBLFT].text) + 11;
821 #ifdef FMBMID
822 len += strlen (fmd_widgets[FMBMID].text) + 6;
823 #endif
825 fmd_xlen = max (fmd_xlen, len + 4);
827 len = (fmd_xlen - (len + 6)) / 2;
828 i = fmd_widgets[FMBLFT].relative_x = len + 3;
829 i += strlen (fmd_widgets[FMBLFT].text) + 8;
831 #ifdef FMBMID
832 fmd_widgets[FMBMID].relative_x = i;
833 i += strlen (fmd_widgets[FMBMID].text) + 6;
834 #endif
836 fmd_widgets[FMBRGT].relative_x = i;
838 #define chkbox_xpos(i) \
839 fmd_widgets [i].relative_x = fmd_xlen - strlen (fmd_widgets [i].text) - 6
841 chkbox_xpos (FMCB0);
842 chkbox_xpos (FMCB21);
843 chkbox_xpos (FMCB22);
845 if (fmd_xlen != FMD_XLEN) {
846 i = sizeof (fmd_widgets) / sizeof (fmd_widgets[0]) - 1;
847 while (i--)
848 fmd_widgets[i].x_divisions = fmd_xlen;
850 fmd_widgets[FMDI1].hotkey_pos =
851 fmd_widgets[FMDI2].hotkey_pos = fmd_xlen - 6;
853 #undef chkbox_xpos
855 initialized = TRUE;
856 #endif /* !ENABLE_NLS */
859 char *
860 file_mask_dialog (FileOpContext *ctx, FileOperation operation, const char *text,
861 const char *def_text, int only_one, int *do_background)
863 int source_easy_patterns = easy_patterns;
864 char *source_mask, *orig_mask, *dest_dir, *tmpdest;
865 const char *error;
866 char *def_text_secure;
867 struct stat buf;
868 int val;
869 QuickDialog Quick_input;
871 g_return_val_if_fail (ctx != NULL, NULL);
872 #if 0
873 message (D_ERROR, __FUNCTION__, "text = `%s' \n def_text = `%s'", text,
874 def_text);
875 #endif
876 fmd_init_i18n (FALSE);
878 /* Set up the result pointers */
880 fmd_widgets[FMCB12].result = &ctx->op_preserve;
881 fmd_widgets[FMCB11].result = &ctx->follow_links;
882 fmd_widgets[FMCB22].result = &ctx->stable_symlinks;
883 fmd_widgets[FMCB21].result = &ctx->dive_into_subdirs;
885 /* filter out a possible password from def_text */
886 def_text_secure = strip_password (g_strdup (def_text), 1);
888 /* Create the dialog */
890 ctx->stable_symlinks = 0;
891 fmd_widgets[FMDC].result = &source_easy_patterns;
892 fmd_widgets[FMDI1].text = easy_patterns ? "*" : "^\\(.*\\)$";
893 Quick_input.xlen = fmd_xlen;
894 Quick_input.xpos = -1;
895 Quick_input.title = op_names[operation];
896 Quick_input.help = "[Mask Copy/Rename]";
897 Quick_input.ylen = FMDY;
898 Quick_input.i18n = 1;
899 Quick_input.widgets = fmd_widgets;
900 fmd_widgets[FMDI0].text = text;
901 fmd_widgets[FMDI2].text = def_text_secure;
902 fmd_widgets[FMDI2].str_result = &dest_dir;
903 fmd_widgets[FMDI1].str_result = &source_mask;
905 *do_background = 0;
906 ask_file_mask:
908 if ((val = quick_dialog_skip (&Quick_input, SKIP)) == B_CANCEL) {
909 g_free (def_text_secure);
910 return 0;
912 g_free (def_text_secure);
914 if (ctx->follow_links)
915 ctx->stat_func = mc_stat;
916 else
917 ctx->stat_func = mc_lstat;
919 if (ctx->op_preserve) {
920 ctx->preserve = 1;
921 ctx->umask_kill = 0777777;
922 ctx->preserve_uidgid = (geteuid () == 0) ? 1 : 0;
923 } else {
924 int i;
925 ctx->preserve = ctx->preserve_uidgid = 0;
926 i = umask (0);
927 umask (i);
928 ctx->umask_kill = i ^ 0777777;
931 orig_mask = source_mask;
932 if (!dest_dir || !*dest_dir) {
933 g_free (source_mask);
934 return dest_dir;
936 if (source_easy_patterns) {
937 source_easy_patterns = easy_patterns;
938 easy_patterns = 1;
939 source_mask = convert_pattern (source_mask, match_file, 1);
940 easy_patterns = source_easy_patterns;
941 error =
942 re_compile_pattern (source_mask, strlen (source_mask),
943 &ctx->rx);
944 g_free (source_mask);
945 } else
946 error =
947 re_compile_pattern (source_mask, strlen (source_mask),
948 &ctx->rx);
950 if (error) {
951 message (D_ERROR, MSG_ERROR, _("Invalid source pattern `%s' \n %s "),
952 orig_mask, error);
953 g_free (orig_mask);
954 goto ask_file_mask;
956 g_free (orig_mask);
958 tmpdest = dest_dir;
959 dest_dir = tilde_expand(tmpdest);
960 g_free(tmpdest);
962 ctx->dest_mask = strrchr (dest_dir, PATH_SEP);
963 if (ctx->dest_mask == NULL)
964 ctx->dest_mask = dest_dir;
965 else
966 ctx->dest_mask++;
967 orig_mask = ctx->dest_mask;
968 if (!*ctx->dest_mask
969 || (!ctx->dive_into_subdirs && !is_wildcarded (ctx->dest_mask)
970 && (!only_one
971 || (!mc_stat (dest_dir, &buf) && S_ISDIR (buf.st_mode))))
972 || (ctx->dive_into_subdirs
973 && ((!only_one && !is_wildcarded (ctx->dest_mask))
974 || (only_one && !mc_stat (dest_dir, &buf)
975 && S_ISDIR (buf.st_mode)))))
976 ctx->dest_mask = g_strdup ("*");
977 else {
978 ctx->dest_mask = g_strdup (ctx->dest_mask);
979 *orig_mask = 0;
981 if (!*dest_dir) {
982 g_free (dest_dir);
983 dest_dir = g_strdup ("./");
985 if (val == B_USER)
986 *do_background = 1;
987 return dest_dir;