Not only comment it out but removing it
[midnight-commander.git] / src / filegui.c
blob441adb782f2e20efcedc19c74103b6c3fbeaaa2c
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>
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
56 #include "global.h"
57 #include "setup.h" /* verbose */
58 #include "dialog.h" /* do_refresh() */
59 #include "color.h" /* dialog_colors */
60 #include "widget.h" /* WLabel */
61 #define WANT_WIDGETS
62 #include "main.h" /* the_hint */
63 #include "wtools.h" /* QuickDialog */
64 #include "panel.h" /* current_panel */
65 #include "fileopctx.h" /* FILE_CONT */
66 #include "filegui.h"
67 #include "key.h" /* get_event */
68 #include "util.h" /* strip_password() */
70 /* }}} */
72 /* Hack: the vfs code should not rely on this */
73 #define WITH_FULL_PATHS 1
75 /* This structure describes the UI and internal data required by a file
76 * operation context.
78 typedef struct {
79 /* ETA and bps */
81 int showing_eta;
82 int showing_bps;
83 int eta_extra;
85 /* Dialog and widgets for the operation progress window */
87 Dlg_head *op_dlg;
89 WLabel *file_label[2];
90 WLabel *file_string[2];
91 WLabel *progress_label[3];
92 WGauge *progress_gauge[3];
93 WLabel *eta_label;
94 WLabel *bps_label;
95 WLabel *stalled_label;
97 /* Query replace dialog */
99 Dlg_head *replace_dlg;
100 const char *replace_filename;
101 int replace_result;
102 struct stat *s_stat, *d_stat;
103 } FileOpContextUI;
106 /* Used to save the hint line */
107 static int last_hint_line;
109 /* File operate window sizes */
110 #define WX 62
111 #define WY 10
112 #define BY 10
113 #define WX_ETA_EXTRA 12
115 #define FCOPY_GAUGE_X 14
116 #define FCOPY_LABEL_X 5
118 /* Used for button result values */
119 enum {
120 REPLACE_YES = B_USER,
121 REPLACE_NO,
122 REPLACE_APPEND,
123 REPLACE_ALWAYS,
124 REPLACE_UPDATE,
125 REPLACE_NEVER,
126 REPLACE_ABORT,
127 REPLACE_SIZE,
128 REPLACE_REGET
131 static FileProgressStatus
132 check_progress_buttons (FileOpContext *ctx)
134 int c;
135 Gpm_Event event;
136 FileOpContextUI *ui;
138 if (ctx->ui == NULL)
139 return FILE_CONT;
141 ui = ctx->ui;
143 event.x = -1; /* Don't show the GPM cursor */
144 c = get_event (&event, 0, 0);
145 if (c == EV_NONE)
146 return FILE_CONT;
148 /* Reinitialize to avoid old values after events other than
149 selecting a button */
150 ui->op_dlg->ret_value = FILE_CONT;
152 dlg_process_event (ui->op_dlg, c, &event);
153 switch (ui->op_dlg->ret_value) {
154 case FILE_SKIP:
155 return FILE_SKIP;
156 break;
157 case B_CANCEL:
158 case FILE_ABORT:
159 return FILE_ABORT;
160 break;
161 default:
162 return FILE_CONT;
166 /* {{{ File progress display routines */
168 void
169 file_op_context_create_ui (FileOpContext *ctx, int with_eta)
171 FileOpContextUI *ui;
172 int x_size;
173 int minus;
174 int eta_offset;
175 const char *sixty;
176 const char *fifteen;
178 g_return_if_fail (ctx != NULL);
179 g_return_if_fail (ctx->ui == NULL);
181 ui = g_new0 (FileOpContextUI, 1);
182 ctx->ui = ui;
184 minus = verbose ? 0 : 3;
185 eta_offset = with_eta ? (WX_ETA_EXTRA) / 2 : 0;
187 sixty = "";
188 fifteen = "";
190 ctx->recursive_result = 0;
192 ui->replace_result = 0;
193 ui->showing_eta = with_eta;
194 ui->showing_bps = with_eta;
195 ui->eta_extra = with_eta ? WX_ETA_EXTRA : 0;
196 x_size = (WX + 4) + ui->eta_extra;
198 ui->op_dlg =
199 create_dlg (0, 0, WY - minus + 4, x_size, dialog_colors, NULL,
200 NULL, op_names[ctx->operation],
201 DLG_CENTER | DLG_REVERSE);
203 last_hint_line = the_hint->widget.y;
204 if ((ui->op_dlg->y + ui->op_dlg->lines) > last_hint_line)
205 the_hint->widget.y = ui->op_dlg->y + ui->op_dlg->lines + 1;
207 add_widget (ui->op_dlg,
208 button_new (BY - minus, WX - 19 + eta_offset, FILE_ABORT,
209 NORMAL_BUTTON, _("&Abort"), 0));
210 add_widget (ui->op_dlg,
211 button_new (BY - minus, 14 + eta_offset, FILE_SKIP,
212 NORMAL_BUTTON, _("&Skip"), 0));
214 add_widget (ui->op_dlg, ui->progress_gauge[2] =
215 gauge_new (7, FCOPY_GAUGE_X, 0, 100, 0));
216 add_widget (ui->op_dlg, ui->progress_label[2] =
217 label_new (7, FCOPY_LABEL_X, fifteen));
218 add_widget (ui->op_dlg, ui->bps_label = label_new (7, WX, ""));
220 add_widget (ui->op_dlg, ui->progress_gauge[1] =
221 gauge_new (8, FCOPY_GAUGE_X, 0, 100, 0));
222 add_widget (ui->op_dlg, ui->progress_label[1] =
223 label_new (8, FCOPY_LABEL_X, fifteen));
224 add_widget (ui->op_dlg, ui->stalled_label = label_new (8, WX, ""));
226 add_widget (ui->op_dlg, ui->progress_gauge[0] =
227 gauge_new (6, FCOPY_GAUGE_X, 0, 100, 0));
228 add_widget (ui->op_dlg, ui->progress_label[0] =
229 label_new (6, FCOPY_LABEL_X, fifteen));
230 add_widget (ui->op_dlg, ui->eta_label = label_new (6, WX, ""));
232 add_widget (ui->op_dlg, ui->file_string[1] =
233 label_new (4, FCOPY_GAUGE_X, sixty));
234 add_widget (ui->op_dlg, ui->file_label[1] =
235 label_new (4, FCOPY_LABEL_X, fifteen));
236 add_widget (ui->op_dlg, ui->file_string[0] =
237 label_new (3, FCOPY_GAUGE_X, sixty));
238 add_widget (ui->op_dlg, ui->file_label[0] =
239 label_new (3, FCOPY_LABEL_X, fifteen));
241 /* We will manage the dialog without any help, that's why
242 we have to call init_dlg */
243 init_dlg (ui->op_dlg);
244 ui->op_dlg->running = 1;
247 void
248 file_op_context_destroy_ui (FileOpContext *ctx)
250 FileOpContextUI *ui;
252 g_return_if_fail (ctx != NULL);
254 if (ctx->ui) {
255 ui = ctx->ui;
257 dlg_run_done (ui->op_dlg);
258 destroy_dlg (ui->op_dlg);
259 g_free (ui);
262 the_hint->widget.y = last_hint_line;
264 ctx->ui = NULL;
267 static FileProgressStatus
268 show_no_bar (FileOpContext *ctx, int n)
270 FileOpContextUI *ui;
272 if (ctx->ui == NULL)
273 return FILE_CONT;
275 ui = ctx->ui;
277 if (n >= 0) {
278 label_set_text (ui->progress_label[n], "");
279 gauge_show (ui->progress_gauge[n], 0);
281 return check_progress_buttons (ctx);
284 static FileProgressStatus
285 show_bar (FileOpContext *ctx, int n, double done, double total)
287 FileOpContextUI *ui;
289 if (ctx->ui == NULL)
290 return FILE_CONT;
292 ui = ctx->ui;
295 * Gauge needs integers, so give it with integers between 0 and 1023.
296 * This precision should be quite reasonable.
298 gauge_set_value (ui->progress_gauge[n], 1024,
299 (int) (1024 * done / total));
300 gauge_show (ui->progress_gauge[n], 1);
301 return check_progress_buttons (ctx);
304 static void
305 file_eta_show (FileOpContext *ctx)
307 int eta_hours, eta_mins, eta_s;
308 char eta_buffer[BUF_TINY];
309 FileOpContextUI *ui;
311 if (ctx->ui == NULL)
312 return;
314 ui = ctx->ui;
316 if (!ui->showing_eta)
317 return;
319 if (ctx->eta_secs > 0.5) {
320 eta_hours = ctx->eta_secs / (60 * 60);
321 eta_mins = (ctx->eta_secs - (eta_hours * 60 * 60)) / 60;
322 eta_s = ctx->eta_secs - (eta_hours * 60 * 60 + eta_mins * 60);
323 g_snprintf (eta_buffer, sizeof (eta_buffer), _("ETA %d:%02d.%02d"),
324 eta_hours, eta_mins, eta_s);
325 } else
326 *eta_buffer = 0;
328 label_set_text (ui->eta_label, eta_buffer);
331 static void
332 file_bps_show (FileOpContext *ctx)
334 char bps_buffer[BUF_TINY];
335 FileOpContextUI *ui;
337 if (ctx->ui == NULL)
338 return;
340 ui = ctx->ui;
342 if (!ui->showing_bps)
343 return;
345 if (ctx->bps > 1024 * 1024) {
346 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%.2f MB/s"),
347 ctx->bps / (1024 * 1024.0));
348 } else if (ctx->bps > 1024) {
349 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%.2f KB/s"),
350 ctx->bps / 1024.0);
351 } else if (ctx->bps > 1) {
352 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%ld B/s"),
353 ctx->bps);
354 } else
355 *bps_buffer = 0;
357 label_set_text (ui->bps_label, bps_buffer);
360 FileProgressStatus
361 file_progress_show (FileOpContext *ctx, off_t done, off_t total)
363 FileOpContextUI *ui;
365 g_return_val_if_fail (ctx != NULL, FILE_CONT);
367 if (ctx->ui == NULL)
368 return FILE_CONT;
370 ui = ctx->ui;
372 if (!verbose)
373 return check_progress_buttons (ctx);
374 if (total > 0) {
375 label_set_text (ui->progress_label[0], _("File"));
376 file_eta_show (ctx);
377 file_bps_show (ctx);
378 return show_bar (ctx, 0, done, total);
379 } else
380 return show_no_bar (ctx, 0);
383 FileProgressStatus
384 file_progress_show_count (FileOpContext *ctx, off_t done, off_t total)
386 FileOpContextUI *ui;
388 g_return_val_if_fail (ctx != NULL, FILE_CONT);
390 if (ctx->ui == NULL)
391 return FILE_CONT;
393 ui = ctx->ui;
395 if (!verbose)
396 return check_progress_buttons (ctx);
397 if (total > 0) {
398 label_set_text (ui->progress_label[1], _("Count"));
399 return show_bar (ctx, 1, done, total);
400 } else
401 return show_no_bar (ctx, 1);
404 FileProgressStatus
405 file_progress_show_bytes (FileOpContext *ctx, double done, double total)
407 FileOpContextUI *ui;
409 g_return_val_if_fail (ctx != NULL, FILE_CONT);
411 if (ctx->ui == NULL)
412 return FILE_CONT;
414 ui = ctx->ui;
416 if (!verbose)
417 return check_progress_buttons (ctx);
418 if (total > 0) {
419 label_set_text (ui->progress_label[2], _("Bytes"));
420 return show_bar (ctx, 2, done, total);
421 } else
422 return show_no_bar (ctx, 2);
425 /* }}} */
427 #define truncFileString(ui, s) name_trunc (s, ui->eta_extra + 47)
428 #define truncFileStringSecure(ui, s) path_trunc (s, ui->eta_extra + 47)
430 FileProgressStatus
431 file_progress_show_source (FileOpContext *ctx, const char *s)
433 FileOpContextUI *ui;
435 g_return_val_if_fail (ctx != NULL, FILE_CONT);
437 if (ctx->ui == NULL)
438 return FILE_CONT;
440 ui = ctx->ui;
442 if (s != NULL) {
443 #ifdef WITH_FULL_PATHS
444 int i = strlen (current_panel->cwd);
446 /* We remove the full path we have added before */
447 if (!strncmp (s, current_panel->cwd, i)) {
448 if (s[i] == PATH_SEP)
449 s += i + 1;
451 #endif /* WITH_FULL_PATHS */
453 label_set_text (ui->file_label[0], _("Source"));
454 label_set_text (ui->file_string[0], truncFileString (ui, s));
455 return check_progress_buttons (ctx);
456 } else {
457 label_set_text (ui->file_label[0], "");
458 label_set_text (ui->file_string[0], "");
459 return check_progress_buttons (ctx);
463 FileProgressStatus
464 file_progress_show_target (FileOpContext *ctx, const char *s)
466 FileOpContextUI *ui;
468 g_return_val_if_fail (ctx != NULL, FILE_CONT);
470 if (ctx->ui == NULL)
471 return FILE_CONT;
473 ui = ctx->ui;
475 if (s != NULL) {
476 label_set_text (ui->file_label[1], _("Target"));
477 label_set_text (ui->file_string[1], truncFileStringSecure (ui, s));
478 return check_progress_buttons (ctx);
479 } else {
480 label_set_text (ui->file_label[1], "");
481 label_set_text (ui->file_string[1], "");
482 return check_progress_buttons (ctx);
486 FileProgressStatus
487 file_progress_show_deleting (FileOpContext *ctx, const char *s)
489 FileOpContextUI *ui;
491 g_return_val_if_fail (ctx != NULL, FILE_CONT);
493 if (ctx->ui == NULL)
494 return FILE_CONT;
496 ui = ctx->ui;
498 label_set_text (ui->file_label[0], _("Deleting"));
499 label_set_text (ui->file_label[0], truncFileStringSecure (ui, s));
500 return check_progress_buttons (ctx);
503 #define X_TRUNC 52
506 * FIXME: probably it is better to replace this with quick dialog machinery,
507 * but actually I'm not familiar with it and have not much time :(
508 * alex
510 static struct {
511 const char *text;
512 int ypos, xpos;
513 int value; /* 0 for labels */
514 } rd_widgets[] = {
516 N_("Target file \"%s\" already exists!"), 3, 4, 0}, {
517 N_("&Abort"), BY + 3, 25, REPLACE_ABORT}, {
518 N_("If &size differs"), BY + 1, 28, REPLACE_SIZE}, {
519 N_("Non&e"), BY, 47, REPLACE_NEVER}, {
520 N_("&Update"), BY, 36, REPLACE_UPDATE}, {
521 N_("A&ll"), BY, 28, REPLACE_ALWAYS}, {
522 N_("Overwrite all targets?"), BY, 4, 0}, {
523 N_("&Reget"), BY - 1, 28, REPLACE_REGET}, {
524 N_("A&ppend"), BY - 2, 45, REPLACE_APPEND}, {
525 N_("&No"), BY - 2, 37, REPLACE_NO}, {
526 N_("&Yes"), BY - 2, 28, REPLACE_YES}, {
527 N_("Overwrite this target?"), BY - 2, 4, 0}, {
528 #if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined _LARGE_FILES && _LARGE_FILES)
529 N_("Target date: %s, size %llu"), 6, 4, 0}, {
530 N_("Source date: %s, size %llu"), 5, 4, 0}
531 #else
532 N_("Target date: %s, size %u"), 6, 4, 0}, {
533 N_("Source date: %s, size %u"), 5, 4, 0}
534 #endif
537 #define ADD_RD_BUTTON(i)\
538 add_widget (ui->replace_dlg,\
539 button_new (rd_widgets [i].ypos, rd_widgets [i].xpos, rd_widgets [i].value,\
540 NORMAL_BUTTON, rd_widgets [i].text, 0))
542 #define ADD_RD_LABEL(ui,i,p1,p2)\
543 g_snprintf (buffer, sizeof (buffer), rd_widgets [i].text, p1, p2);\
544 add_widget (ui->replace_dlg,\
545 label_new (rd_widgets [i].ypos, rd_widgets [i].xpos, buffer))
547 static void
548 init_replace (FileOpContext *ctx, enum OperationMode mode)
550 FileOpContextUI *ui;
551 char buffer[BUF_SMALL];
552 const char *title;
553 static int rd_xlen = 60, rd_trunc = X_TRUNC;
555 #ifdef ENABLE_NLS
556 static int i18n_flag;
557 if (!i18n_flag) {
558 int l1, l2, l, row;
559 register int i = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
560 while (i--)
561 rd_widgets[i].text = _(rd_widgets[i].text);
564 * longest of "Overwrite..." labels
565 * (assume "Target date..." are short enough)
567 l1 = max (strlen (rd_widgets[6].text),
568 strlen (rd_widgets[11].text));
570 /* longest of button rows */
571 i = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
572 for (row = l = l2 = 0; i--;) {
573 if (rd_widgets[i].value != 0) {
574 if (row != rd_widgets[i].ypos) {
575 row = rd_widgets[i].ypos;
576 l2 = max (l2, l);
577 l = 0;
579 l += strlen (rd_widgets[i].text) + 4;
582 l2 = max (l2, l); /* last row */
583 rd_xlen = max (rd_xlen, l1 + l2 + 8);
584 rd_trunc = rd_xlen - 6;
586 /* Now place buttons */
587 l1 += 5; /* start of first button in the row */
588 i = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
590 for (l = l1, row = 0; --i > 1;) {
591 if (rd_widgets[i].value != 0) {
592 if (row != rd_widgets[i].ypos) {
593 row = rd_widgets[i].ypos;
594 l = l1;
596 rd_widgets[i].xpos = l;
597 l += strlen (rd_widgets[i].text) + 4;
600 /* Abort button is centered */
601 rd_widgets[1].xpos =
602 (rd_xlen - strlen (rd_widgets[1].text) - 3) / 2;
604 #endif /* ENABLE_NLS */
606 ui = ctx->ui;
608 if (mode == Foreground)
609 title = _(" File exists ");
610 else
611 title = _(" Background process: File exists ");
613 /* FIXME - missing help node */
614 ui->replace_dlg =
615 create_dlg (0, 0, 16, rd_xlen, alarm_colors, NULL, "[Replace]",
616 title, DLG_CENTER | DLG_REVERSE);
619 ADD_RD_LABEL (ui, 0,
620 name_trunc (ui->replace_filename,
621 rd_trunc - strlen (rd_widgets[0].text)), 0);
622 ADD_RD_BUTTON (1);
624 ADD_RD_BUTTON (2);
625 ADD_RD_BUTTON (3);
626 ADD_RD_BUTTON (4);
627 ADD_RD_BUTTON (5);
628 ADD_RD_LABEL (ui, 6, 0, 0);
630 /* "this target..." widgets */
631 if (!S_ISDIR (ui->d_stat->st_mode)) {
632 if ((ctx->operation == OP_COPY) && (ui->d_stat->st_size != 0)
633 && (ui->s_stat->st_size > ui->d_stat->st_size))
634 ADD_RD_BUTTON (7); /* reget */
636 ADD_RD_BUTTON (8); /* Overwrite all targets? */
638 ADD_RD_BUTTON (9);
639 ADD_RD_BUTTON (10);
640 ADD_RD_LABEL (ui, 11, 0, 0);
642 ADD_RD_LABEL (ui, 12, file_date (ui->d_stat->st_mtime),
643 (off_t) ui->d_stat->st_size);
644 ADD_RD_LABEL (ui, 13, file_date (ui->s_stat->st_mtime),
645 (off_t) ui->s_stat->st_size);
648 void
649 file_progress_set_stalled_label (FileOpContext *ctx, const char *stalled_msg)
651 FileOpContextUI *ui;
653 g_return_if_fail (ctx != NULL);
655 if (ctx->ui == NULL)
656 return;
658 ui = ctx->ui;
660 label_set_text (ui->stalled_label, stalled_msg);
663 FileProgressStatus
664 file_progress_real_query_replace (FileOpContext *ctx,
665 enum OperationMode mode, const char *destname,
666 struct stat *_s_stat,
667 struct stat *_d_stat)
669 FileOpContextUI *ui;
671 g_return_val_if_fail (ctx != NULL, FILE_CONT);
672 g_return_val_if_fail (ctx->ui != NULL, FILE_CONT);
674 ui = ctx->ui;
676 if (ui->replace_result < REPLACE_ALWAYS) {
677 ui->replace_filename = destname;
678 ui->s_stat = _s_stat;
679 ui->d_stat = _d_stat;
680 init_replace (ctx, mode);
681 run_dlg (ui->replace_dlg);
682 ui->replace_result = ui->replace_dlg->ret_value;
683 if (ui->replace_result == B_CANCEL)
684 ui->replace_result = REPLACE_ABORT;
685 destroy_dlg (ui->replace_dlg);
688 switch (ui->replace_result) {
689 case REPLACE_UPDATE:
690 do_refresh ();
691 if (_s_stat->st_mtime > _d_stat->st_mtime)
692 return FILE_CONT;
693 else
694 return FILE_SKIP;
696 case REPLACE_SIZE:
697 do_refresh ();
698 if (_s_stat->st_size == _d_stat->st_size)
699 return FILE_SKIP;
700 else
701 return FILE_CONT;
703 case REPLACE_REGET:
704 /* Careful: we fall through and set do_append */
705 ctx->do_reget = _d_stat->st_size;
707 case REPLACE_APPEND:
708 ctx->do_append = 1;
710 case REPLACE_YES:
711 case REPLACE_ALWAYS:
712 do_refresh ();
713 return FILE_CONT;
714 case REPLACE_NO:
715 case REPLACE_NEVER:
716 do_refresh ();
717 return FILE_SKIP;
718 case REPLACE_ABORT:
719 default:
720 return FILE_ABORT;
724 #define FMDY 13
725 #define FMD_XLEN 64
726 extern int fmd_xlen;
727 static QuickWidget fmd_widgets[] = {
729 #define FMCB0 FMDC
730 #define FMCB12 0
731 #define FMCB11 1
732 /* follow symlinks and preserve Attributes must be the first */
733 {quick_checkbox, 3, 64, 8, FMDY, N_("preserve &Attributes"), 9, 0,
734 0 /* &op_preserve */ , 0, NULL},
735 {quick_checkbox, 3, 64, 7, FMDY, N_("follow &Links"), 7, 0,
736 0 /* &file_mask_op_follow_links */ , 0, NULL},
737 {quick_label, 3, 64, 5, FMDY, N_("to:"), 0, 0, 0, 0, NULL},
738 {quick_checkbox, 37, 64, 4, FMDY, N_("&Using shell patterns"), 0, 0,
739 0 /* &source_easy_patterns */ , 0, NULL},
740 {quick_input, 3, 64, 3, FMDY, "", 58,
741 0, 0, 0, "input-def"},
742 #define FMDI1 4
743 #define FMDI2 5
744 #define FMDC 3
745 {quick_input, 3, 64, 6, FMDY, "", 58, 0,
746 0, 0, "input2"},
747 #define FMDI0 6
748 {quick_label, 3, 64, 2, FMDY, "", 0, 0, 0, 0, NULL},
749 #define FMBRGT 7
750 {quick_button, 42, 64, 9, FMDY, N_("&Cancel"), 0, B_CANCEL, 0, 0,
751 NULL},
752 #undef SKIP
753 #ifdef WITH_BACKGROUND
754 # define SKIP 5
755 # define FMCB21 11
756 # define FMCB22 10
757 # define FMBLFT 9
758 # define FMBMID 8
759 {quick_button, 25, 64, 9, FMDY, N_("&Background"), 0, B_USER, 0, 0,
760 NULL},
761 #else /* WITH_BACKGROUND */
762 # define SKIP 4
763 # define FMCB21 10
764 # define FMCB22 9
765 # define FMBLFT 8
766 # undef FMBMID
767 #endif
768 {quick_button, 14, 64, 9, FMDY, N_("&OK"), 0, B_ENTER, 0, 0, NULL},
769 {quick_checkbox, 42, 64, 8, FMDY, N_("&Stable Symlinks"), 0, 0,
770 0 /* &file_mask_stable_symlinks */ , 0, NULL},
771 {quick_checkbox, 31, 64, 7, FMDY, N_("&Dive into subdir if exists"), 0,
773 0 /* &dive_into_subdirs */ , 0, NULL},
774 NULL_QuickWidget
777 static int
778 is_wildcarded (char *p)
780 for (; *p; p++) {
781 if (*p == '*')
782 return 1;
783 else if (*p == '\\' && p[1] >= '1' && p[1] <= '9')
784 return 1;
786 return 0;
789 void
790 fmd_init_i18n (int force)
792 #ifdef ENABLE_NLS
793 static int initialized = FALSE;
794 register int i;
795 int len;
797 if (initialized && !force)
798 return;
800 for (i = sizeof (op_names) / sizeof (op_names[0]); i--;)
801 op_names[i] = _(op_names[i]);
803 i = sizeof (fmd_widgets) / sizeof (fmd_widgets[0]) - 1;
804 while (i--)
805 if (fmd_widgets[i].text[0] != '\0')
806 fmd_widgets[i].text = _(fmd_widgets[i].text);
808 len = strlen (fmd_widgets[FMCB11].text)
809 + strlen (fmd_widgets[FMCB21].text) + 15;
810 fmd_xlen = max (fmd_xlen, len);
812 len = strlen (fmd_widgets[FMCB12].text)
813 + strlen (fmd_widgets[FMCB22].text) + 15;
814 fmd_xlen = max (fmd_xlen, len);
816 len = strlen (fmd_widgets[FMBRGT].text)
817 + strlen (fmd_widgets[FMBLFT].text) + 11;
819 #ifdef FMBMID
820 len += strlen (fmd_widgets[FMBMID].text) + 6;
821 #endif
823 fmd_xlen = max (fmd_xlen, len + 4);
825 len = (fmd_xlen - (len + 6)) / 2;
826 i = fmd_widgets[FMBLFT].relative_x = len + 3;
827 i += strlen (fmd_widgets[FMBLFT].text) + 8;
829 #ifdef FMBMID
830 fmd_widgets[FMBMID].relative_x = i;
831 i += strlen (fmd_widgets[FMBMID].text) + 6;
832 #endif
834 fmd_widgets[FMBRGT].relative_x = i;
836 #define chkbox_xpos(i) \
837 fmd_widgets [i].relative_x = fmd_xlen - strlen (fmd_widgets [i].text) - 6
839 chkbox_xpos (FMCB0);
840 chkbox_xpos (FMCB21);
841 chkbox_xpos (FMCB22);
843 if (fmd_xlen != FMD_XLEN) {
844 i = sizeof (fmd_widgets) / sizeof (fmd_widgets[0]) - 1;
845 while (i--)
846 fmd_widgets[i].x_divisions = fmd_xlen;
848 fmd_widgets[FMDI1].hotkey_pos =
849 fmd_widgets[FMDI2].hotkey_pos = fmd_xlen - 6;
851 #undef chkbox_xpos
853 initialized = TRUE;
854 #endif /* !ENABLE_NLS */
857 char *
858 file_mask_dialog (FileOpContext *ctx, FileOperation operation, const char *text,
859 const char *def_text, int only_one, int *do_background)
861 int source_easy_patterns = easy_patterns;
862 char *source_mask, *orig_mask, *dest_dir, *tmpdest;
863 const char *error;
864 char *def_text_secure;
865 struct stat buf;
866 int val;
867 QuickDialog Quick_input;
869 g_return_val_if_fail (ctx != NULL, NULL);
870 #if 0
871 message (1, __FUNCTION__, "text = `%s' \n def_text = `%s'", text,
872 def_text);
873 #endif
874 fmd_init_i18n (FALSE);
876 /* Set up the result pointers */
878 fmd_widgets[FMCB12].result = &ctx->op_preserve;
879 fmd_widgets[FMCB11].result = &ctx->follow_links;
880 fmd_widgets[FMCB22].result = &ctx->stable_symlinks;
881 fmd_widgets[FMCB21].result = &ctx->dive_into_subdirs;
883 /* filter out a possible password from def_text */
884 def_text_secure = strip_password (g_strdup (def_text), 1);
886 /* Create the dialog */
888 ctx->stable_symlinks = 0;
889 fmd_widgets[FMDC].result = &source_easy_patterns;
890 fmd_widgets[FMDI1].text = easy_patterns ? "*" : "^\\(.*\\)$";
891 Quick_input.xlen = fmd_xlen;
892 Quick_input.xpos = -1;
893 Quick_input.title = op_names[operation];
894 Quick_input.help = "[Mask Copy/Rename]";
895 Quick_input.ylen = FMDY;
896 Quick_input.i18n = 1;
897 Quick_input.widgets = fmd_widgets;
898 fmd_widgets[FMDI0].text = text;
899 fmd_widgets[FMDI2].text = def_text_secure;
900 fmd_widgets[FMDI2].str_result = &dest_dir;
901 fmd_widgets[FMDI1].str_result = &source_mask;
903 *do_background = 0;
904 ask_file_mask:
906 if ((val = quick_dialog_skip (&Quick_input, SKIP)) == B_CANCEL) {
907 g_free (def_text_secure);
908 return 0;
910 g_free (def_text_secure);
912 if (ctx->follow_links)
913 ctx->stat_func = mc_stat;
914 else
915 ctx->stat_func = mc_lstat;
917 if (ctx->op_preserve) {
918 ctx->preserve = 1;
919 ctx->umask_kill = 0777777;
920 ctx->preserve_uidgid = (geteuid () == 0) ? 1 : 0;
921 } else {
922 int i;
923 ctx->preserve = ctx->preserve_uidgid = 0;
924 i = umask (0);
925 umask (i);
926 ctx->umask_kill = i ^ 0777777;
929 orig_mask = source_mask;
930 if (!dest_dir || !*dest_dir) {
931 g_free (source_mask);
932 return dest_dir;
934 if (source_easy_patterns) {
935 source_easy_patterns = easy_patterns;
936 easy_patterns = 1;
937 source_mask = convert_pattern (source_mask, match_file, 1);
938 easy_patterns = source_easy_patterns;
939 error =
940 re_compile_pattern (source_mask, strlen (source_mask),
941 &ctx->rx);
942 g_free (source_mask);
943 } else
944 error =
945 re_compile_pattern (source_mask, strlen (source_mask),
946 &ctx->rx);
948 if (error) {
949 message (1, MSG_ERROR, _("Invalid source pattern `%s' \n %s "),
950 orig_mask, error);
951 g_free (orig_mask);
952 goto ask_file_mask;
954 g_free (orig_mask);
956 tmpdest = dest_dir;
957 dest_dir = tilde_expand(tmpdest);
958 g_free(tmpdest);
960 ctx->dest_mask = strrchr (dest_dir, PATH_SEP);
961 if (ctx->dest_mask == NULL)
962 ctx->dest_mask = dest_dir;
963 else
964 ctx->dest_mask++;
965 orig_mask = ctx->dest_mask;
966 if (!*ctx->dest_mask
967 || (!ctx->dive_into_subdirs && !is_wildcarded (ctx->dest_mask)
968 && (!only_one
969 || (!mc_stat (dest_dir, &buf) && S_ISDIR (buf.st_mode))))
970 || (ctx->dive_into_subdirs
971 && ((!only_one && !is_wildcarded (ctx->dest_mask))
972 || (only_one && !mc_stat (dest_dir, &buf)
973 && S_ISDIR (buf.st_mode)))))
974 ctx->dest_mask = g_strdup ("*");
975 else {
976 ctx->dest_mask = g_strdup (ctx->dest_mask);
977 *orig_mask = 0;
979 if (!*dest_dir) {
980 g_free (dest_dir);
981 dest_dir = g_strdup ("./");
983 if (val == B_USER)
984 *do_background = 1;
985 return dest_dir;