small typo
[midnight-commander.git] / src / filegui.c
blob4805dedba091be8502258b5008a0110642133ff5
1 /* {{{ Copyright */
3 /* File managing. Important notes on this file:
4 *
5 * About the use of dialogs in this file:
6 * If you want to add a new dialog box (or call a routine that pops
7 * up a dialog box), you have to provide a wrapper for background
8 * operations (ie, background operations have to up-call to the parent
9 * process).
11 * For example, instead of using the message() routine, in this
12 * file, you should use one of the stubs that call message with the
13 * proper number of arguments (ie, message_1s, message_2s and so on).
15 * Actually, that is a rule that should be followed by any routines
16 * that may be called from this module.
20 /* File managing GUI for the text mode edition
22 * Copyright (C) 1994, 1995, 1996 The Free Software Foundation
24 * Written by: 1994, 1995 Janne Kukonlehto
25 * 1994, 1995 Fred Leeflang
26 * 1994, 1995, 1996 Miguel de Icaza
27 * 1995, 1996 Jakub Jelinek
28 * 1997 Norbert Warmuth
29 * 1998 Pavel Machek
31 * The copy code was based in GNU's cp, and was written by:
32 * Torbjorn Granlund, David MacKenzie, and Jim Meyering.
34 * The move code was based in GNU's mv, and was written by:
35 * Mike Parker and David MacKenzie.
37 * Janne Kukonlehto added much error recovery to them for being used
38 * in an interactive program.
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
50 * You should have received a copy of the GNU General Public License
51 * along with this program; if not, write to the Free Software
52 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
55 /* }}} */
57 /* {{{ Include files */
59 #include <config.h>
60 /* Hack: the vfs code should not rely on this */
61 #define WITH_FULL_PATHS 1
63 #include <sys/types.h>
64 #include <stdio.h>
66 #include <errno.h>
67 #include <ctype.h>
68 #include <string.h>
69 #ifdef HAVE_UNISTD_H
70 # include <unistd.h>
71 #endif
72 #include <sys/stat.h>
74 #include "global.h"
75 #include "tty.h"
76 #include "eregex.h"
77 #include "setup.h"
78 #include "dialog.h"
79 /* Needed by query_replace */
80 #include "color.h"
81 #include "win.h"
82 #include "dlg.h"
83 #define WANT_WIDGETS
84 #include "widget.h" /* Note the sequence */
85 #include "main.h" /* WANT_WIDGETS-> we get the the_hint def */
86 #include "layout.h"
87 #include "wtools.h"
89 /* Needed for current_panel, other_panel and WTree */
90 #include "dir.h"
91 #include "panel.h"
92 #include "file.h"
93 #include "filegui.h"
94 #include "fileopctx.h"
95 #include "tree.h"
96 #include "key.h"
97 #include "../vfs/vfs.h"
99 /* }}} */
101 /* This structure describes the UI and internal data required by a file
102 * operation context.
104 typedef struct {
105 /* ETA and bps */
107 int showing_eta;
108 int showing_bps;
109 int eta_extra;
111 /* Dialog and widgets for the operation progress window */
113 Dlg_head *op_dlg;
115 WLabel *file_label[2];
116 WLabel *file_string[2];
117 WLabel *progress_label[3];
118 WGauge *progress_gauge[3];
119 WLabel *eta_label;
120 WLabel *bps_label;
121 WLabel *stalled_label;
123 /* Query replace dialog */
125 Dlg_head *replace_dlg;
126 char *replace_filename;
127 int replace_result;
128 struct stat *s_stat, *d_stat;
129 } FileOpContextUI;
132 /* Used to save the hint line */
133 static int last_hint_line;
135 /* File operate window sizes */
136 #define WX 62
137 #define WY 10
138 #define BY 10
139 #define WX_ETA_EXTRA 12
141 #define FCOPY_GAUGE_X 14
142 #define FCOPY_LABEL_X 5
144 /* Used for button result values */
145 enum {
146 REPLACE_YES = B_USER,
147 REPLACE_NO,
148 REPLACE_APPEND,
149 REPLACE_ALWAYS,
150 REPLACE_UPDATE,
151 REPLACE_NEVER,
152 REPLACE_ABORT,
153 REPLACE_SIZE,
154 REPLACE_REGET
155 } FileReplaceCode;
157 static FileProgressStatus
158 check_progress_buttons (FileOpContext *ctx)
160 int c;
161 Gpm_Event event;
162 FileOpContextUI *ui;
164 if (ctx->ui == NULL)
165 return FILE_CONT;
167 ui = ctx->ui;
169 event.x = -1; /* Don't show the GPM cursor */
170 c = get_event (&event, 0, 0);
171 if (c == EV_NONE)
172 return FILE_CONT;
173 dlg_process_event (ui->op_dlg, c, &event);
174 switch (ui->op_dlg->ret_value) {
175 case FILE_SKIP:
176 return FILE_SKIP;
177 break;
178 case B_CANCEL:
179 case FILE_ABORT:
180 return FILE_ABORT;
181 break;
182 default:
183 return FILE_CONT;
187 /* {{{ File progress display routines */
189 void
190 file_op_context_create_ui (FileOpContext *ctx, FileOperation op, int with_eta)
192 FileOpContextUI *ui;
193 int x_size;
194 int minus;
195 int eta_offset;
196 char *sixty;
197 char *fifteen;
199 g_return_if_fail (ctx != NULL);
200 g_return_if_fail (ctx->ui == NULL);
202 ui = g_new0 (FileOpContextUI, 1);
203 ctx->ui = ui;
205 minus = verbose ? 0 : 3;
206 eta_offset = with_eta ? (WX_ETA_EXTRA) / 2 : 0;
208 sixty = "";
209 fifteen = "";
211 ctx->recursive_result = 0;
213 ui->replace_result = 0;
214 ui->showing_eta = with_eta;
215 ui->showing_bps = with_eta;
216 ui->eta_extra = with_eta ? WX_ETA_EXTRA : 0;
217 x_size = (WX + 4) + ui->eta_extra;
219 ui->op_dlg = create_dlg (0, 0, WY-minus+4, x_size, dialog_colors,
220 NULL, NULL, op_names[op], DLG_CENTER);
222 last_hint_line = the_hint->widget.y;
223 if ((ui->op_dlg->y + ui->op_dlg->lines) > last_hint_line)
224 the_hint->widget.y = ui->op_dlg->y + ui->op_dlg->lines+1;
226 add_widget (ui->op_dlg, button_new (BY-minus, WX - 19 + eta_offset, FILE_ABORT,
227 NORMAL_BUTTON, _("&Abort"), 0, 0, "abort"));
228 add_widget (ui->op_dlg, button_new (BY-minus, 14 + eta_offset, FILE_SKIP,
229 NORMAL_BUTTON, _("&Skip"), 0, 0, "skip"));
231 add_widget (ui->op_dlg, ui->progress_gauge[2] = gauge_new (7, FCOPY_GAUGE_X, 0, 100, 0, "g-1"));
232 add_widget (ui->op_dlg, ui->progress_label[2] = label_new (7, FCOPY_LABEL_X, fifteen, "l-1"));
233 add_widget (ui->op_dlg, ui->bps_label = label_new (7, WX, "", "bps-label"));
235 add_widget (ui->op_dlg, ui->progress_gauge[1] = gauge_new (8, FCOPY_GAUGE_X, 0, 100, 0, "g-2"));
236 add_widget (ui->op_dlg, ui->progress_label[1] = label_new (8, FCOPY_LABEL_X, fifteen, "l-2"));
237 add_widget (ui->op_dlg, ui->stalled_label = label_new (8, WX, "", "stalled"));
239 add_widget (ui->op_dlg, ui->progress_gauge[0] = gauge_new (6, FCOPY_GAUGE_X, 0, 100, 0, "g-3"));
240 add_widget (ui->op_dlg, ui->progress_label[0] = label_new (6, FCOPY_LABEL_X, fifteen, "l-3"));
241 add_widget (ui->op_dlg, ui->eta_label = label_new (6, WX, "", "eta_label"));
243 add_widget (ui->op_dlg, ui->file_string[1] = label_new (4, FCOPY_GAUGE_X, sixty, "fs-l-1"));
244 add_widget (ui->op_dlg, ui->file_label[1] = label_new (4, FCOPY_LABEL_X, fifteen, "fs-l-2"));
245 add_widget (ui->op_dlg, ui->file_string[0] = label_new (3, FCOPY_GAUGE_X, sixty, "fs-x-1"));
246 add_widget (ui->op_dlg, ui->file_label[0] = label_new (3, FCOPY_LABEL_X, fifteen, "fs-x-2"));
248 /* We will manage the dialog without any help, that's why
249 we have to call init_dlg */
250 init_dlg (ui->op_dlg);
251 ui->op_dlg->running = 1;
254 void
255 file_op_context_destroy_ui (FileOpContext *ctx)
257 FileOpContextUI *ui;
259 g_return_if_fail (ctx != NULL);
261 if (ctx->ui){
262 ui = ctx->ui;
264 dlg_run_done (ui->op_dlg);
265 destroy_dlg (ui->op_dlg);
266 g_free (ui);
269 the_hint->widget.y = last_hint_line;
271 ctx->ui = NULL;
274 static FileProgressStatus
275 show_no_bar (FileOpContext *ctx, int n)
277 FileOpContextUI *ui;
279 if (ctx->ui == NULL)
280 return FILE_CONT;
282 ui = ctx->ui;
284 if (n >= 0) {
285 label_set_text (ui->progress_label[n], "");
286 gauge_show (ui->progress_gauge[n], 0);
288 return check_progress_buttons (ctx);
291 static FileProgressStatus
292 show_bar (FileOpContext *ctx, int n, double done, double total)
294 FileOpContextUI *ui;
296 if (ctx->ui == NULL)
297 return FILE_CONT;
299 ui = ctx->ui;
302 * Gauge needs integers, so give it with integers between 0 and 1023.
303 * This precision should be quite reasonable.
305 gauge_set_value (ui->progress_gauge[n], 1024, (int) (1024 * done / total));
306 gauge_show (ui->progress_gauge[n], 1);
307 return check_progress_buttons (ctx);
310 static void
311 file_eta_show (FileOpContext *ctx)
313 int eta_hours, eta_mins, eta_s;
314 char eta_buffer [BUF_TINY];
315 FileOpContextUI *ui;
317 if (ctx->ui == NULL)
318 return;
320 ui = ctx->ui;
322 if (!ui->showing_eta)
323 return;
325 if (ctx->eta_secs > 0.5) {
326 eta_hours = ctx->eta_secs / (60 * 60);
327 eta_mins = (ctx->eta_secs - (eta_hours * 60 * 60)) / 60;
328 eta_s = ctx->eta_secs - (eta_hours * 60 * 60 + eta_mins * 60);
329 g_snprintf (eta_buffer, sizeof (eta_buffer), _("ETA %d:%02d.%02d"), eta_hours, eta_mins, eta_s);
330 } else
331 *eta_buffer = 0;
333 label_set_text (ui->eta_label, eta_buffer);
336 static void
337 file_bps_show (FileOpContext *ctx)
339 char bps_buffer [BUF_TINY];
340 FileOpContextUI *ui;
342 if (ctx->ui == NULL)
343 return;
345 ui = ctx->ui;
347 if (!ui->showing_bps)
348 return;
350 if (ctx->bps > 1024*1024) {
351 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%.2f MB/s"), ctx->bps / (1024*1024.0));
352 } else if (ctx->bps > 1024){
353 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%.2f KB/s"), ctx->bps / 1024.0);
354 } else if (ctx->bps > 1){
355 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%ld B/s"), 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)
431 FileProgressStatus
432 file_progress_show_source (FileOpContext *ctx, char *s)
434 FileOpContextUI *ui;
436 g_return_val_if_fail (ctx != NULL, FILE_CONT);
438 if (ctx->ui == NULL)
439 return FILE_CONT;
441 ui = ctx->ui;
443 if (s != NULL) {
444 #ifdef WITH_FULL_PATHS
445 int i = strlen (cpanel->cwd);
447 /* We remove the full path we have added before */
448 if (!strncmp (s, cpanel->cwd, i)){
449 if (s[i] == PATH_SEP)
450 s += i + 1;
452 #endif /* WITH_FULL_PATHS */
454 label_set_text (ui->file_label[0], _("Source"));
455 label_set_text (ui->file_string[0], truncFileString (ui, s));
456 return check_progress_buttons (ctx);
457 } else {
458 label_set_text (ui->file_label[0], "");
459 label_set_text (ui->file_string[0], "");
460 return check_progress_buttons (ctx);
464 FileProgressStatus
465 file_progress_show_target (FileOpContext *ctx, char *s)
467 FileOpContextUI *ui;
469 g_return_val_if_fail (ctx != NULL, FILE_CONT);
471 if (ctx->ui == NULL)
472 return FILE_CONT;
474 ui = ctx->ui;
476 if (s != NULL) {
477 label_set_text (ui->file_label[1], _("Target"));
478 label_set_text (ui->file_string[1], truncFileString (ui, s));
479 return check_progress_buttons (ctx);
480 } else {
481 label_set_text (ui->file_label[1], "");
482 label_set_text (ui->file_string[1], "");
483 return check_progress_buttons (ctx);
487 FileProgressStatus
488 file_progress_show_deleting (FileOpContext *ctx, char *s)
490 FileOpContextUI *ui;
492 g_return_val_if_fail (ctx != NULL, FILE_CONT);
494 if (ctx->ui == NULL)
495 return FILE_CONT;
497 ui = ctx->ui;
499 label_set_text (ui->file_label[0], _("Deleting"));
500 label_set_text (ui->file_label[0], truncFileString (ui, s));
501 return check_progress_buttons (ctx);
504 #define X_TRUNC 52
507 * FIXME: probably it is better to replace this with quick dialog machinery,
508 * but actually I'm not familiar with it and have not much time :(
509 * alex
511 static struct
513 char* text;
514 int ypos, xpos;
515 int value; /* 0 for labels */
516 char* tkname;
518 rd_widgets [] =
520 {N_("Target file \"%s\" already exists!"),
521 3, 4, 0, "target-e" },
522 {N_("&Abort"), BY + 3, 25, REPLACE_ABORT, "abort" },
523 {N_("if &Size differs"),
524 BY + 1, 28, REPLACE_SIZE, "if-size" },
525 {N_("non&E"), BY, 47, REPLACE_NEVER, "none" },
526 {N_("&Update"), BY, 36, REPLACE_UPDATE, "update" },
527 {N_("al&L"), BY, 28, REPLACE_ALWAYS, "all" },
528 {N_("Overwrite all targets?"),
529 BY, 4, 0, "over-label" },
530 {N_("&Reget"), BY - 1, 28, REPLACE_REGET, "reget" },
531 {N_("ap&Pend"), BY - 2, 45, REPLACE_APPEND, "append" },
532 {N_("&No"), BY - 2, 37, REPLACE_NO, "no" },
533 {N_("&Yes"), BY - 2, 28, REPLACE_YES, "yes" },
534 {N_("Overwrite this target?"),
535 BY - 2, 4, 0, "overlab" },
536 {N_("Target date: %s, size %d"),
537 6, 4, 0, "target-date" },
538 {N_("Source date: %s, size %d"),
539 5, 4, 0, "source-date" }
542 #define ADD_RD_BUTTON(i)\
543 add_widget (ui->replace_dlg,\
544 button_new (rd_widgets [i].ypos, rd_widgets [i].xpos, rd_widgets [i].value,\
545 NORMAL_BUTTON, rd_widgets [i].text, 0, 0, rd_widgets [i].tkname))
547 #define ADD_RD_LABEL(ui,i,p1,p2)\
548 g_snprintf (buffer, sizeof (buffer), rd_widgets [i].text, p1, p2);\
549 add_widget (ui->replace_dlg,\
550 label_new (rd_widgets [i].ypos, rd_widgets [i].xpos, buffer, rd_widgets [i].tkname))
552 static void
553 init_replace (FileOpContext * ctx, enum OperationMode mode)
555 FileOpContextUI *ui;
556 char buffer[BUF_SMALL];
557 char *title;
558 static int rd_xlen = 60, rd_trunc = X_TRUNC;
560 #ifdef ENABLE_NLS
561 static int i18n_flag;
562 if (!i18n_flag) {
563 int l1, l2, l, row;
564 register int i = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
565 while (i--)
566 rd_widgets[i].text = _(rd_widgets[i].text);
569 * longest of "Overwrite..." labels
570 * (assume "Target date..." are short enough)
572 l1 = max (strlen (rd_widgets[6].text),
573 strlen (rd_widgets[11].text));
575 /* longest of button rows */
576 i = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
577 for (row = l = l2 = 0; i--;) {
578 if (rd_widgets[i].value != 0) {
579 if (row != rd_widgets[i].ypos) {
580 row = rd_widgets[i].ypos;
581 l2 = max (l2, l);
582 l = 0;
584 l += strlen (rd_widgets[i].text) + 4;
587 l2 = max (l2, l); /* last row */
588 rd_xlen = max (rd_xlen, l1 + l2 + 8);
589 rd_trunc = rd_xlen - 6;
591 /* Now place buttons */
592 l1 += 5; /* start of first button in the row */
593 i = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
595 for (l = l1, row = 0; --i > 1;) {
596 if (rd_widgets[i].value != 0) {
597 if (row != rd_widgets[i].ypos) {
598 row = rd_widgets[i].ypos;
599 l = l1;
601 rd_widgets[i].xpos = l;
602 l += strlen (rd_widgets[i].text) + 4;
605 /* Abort button is centered */
606 rd_widgets[1].xpos =
607 (rd_xlen - strlen (rd_widgets[1].text) - 3) / 2;
609 #endif /* ENABLE_NLS */
611 ui = ctx->ui;
613 if (mode == Foreground)
614 title = _(" File exists ");
615 else
616 title = _(" Background process: File exists ");
618 /* FIXME - missing help node */
619 ui->replace_dlg =
620 create_dlg (0, 0, 16, rd_xlen, alarm_colors, NULL, "[Replace]",
621 title, DLG_CENTER);
624 ADD_RD_LABEL (ui, 0,
625 name_trunc (ui->replace_filename,
626 rd_trunc - strlen (rd_widgets[0].text)), 0);
627 ADD_RD_BUTTON (1);
629 ADD_RD_BUTTON (2);
630 ADD_RD_BUTTON (3);
631 ADD_RD_BUTTON (4);
632 ADD_RD_BUTTON (5);
633 ADD_RD_LABEL (ui, 6, 0, 0);
635 /* "this target..." widgets */
636 if (!S_ISDIR (ui->d_stat->st_mode)) {
637 if ((ui->d_stat->st_size
638 && ui->s_stat->st_size > ui->d_stat->st_size))
639 ADD_RD_BUTTON (7);
641 ADD_RD_BUTTON (8);
643 ADD_RD_BUTTON (9);
644 ADD_RD_BUTTON (10);
645 ADD_RD_LABEL (ui, 11, 0, 0);
647 ADD_RD_LABEL (ui, 12, file_date (ui->d_stat->st_mtime),
648 (int) ui->d_stat->st_size);
649 ADD_RD_LABEL (ui, 13, file_date (ui->s_stat->st_mtime),
650 (int) ui->s_stat->st_size);
653 void
654 file_progress_set_stalled_label (FileOpContext *ctx, char *stalled_msg)
656 FileOpContextUI *ui;
658 g_return_if_fail (ctx != NULL);
660 if (ctx->ui == NULL)
661 return;
663 ui = ctx->ui;
665 label_set_text (ui->stalled_label, stalled_msg);
668 FileProgressStatus
669 file_progress_real_query_replace (FileOpContext *ctx, enum OperationMode mode,
670 char *destname, struct stat *_s_stat,
671 struct stat *_d_stat)
673 FileOpContextUI *ui;
675 g_return_val_if_fail (ctx != NULL, FILE_CONT);
676 g_return_val_if_fail (ctx->ui != NULL, FILE_CONT);
678 ui = ctx->ui;
680 if (ui->replace_result < REPLACE_ALWAYS){
681 ui->replace_filename = destname;
682 ui->s_stat = _s_stat;
683 ui->d_stat = _d_stat;
684 init_replace (ctx, mode);
685 run_dlg (ui->replace_dlg);
686 ui->replace_result = ui->replace_dlg->ret_value;
687 if (ui->replace_result == B_CANCEL)
688 ui->replace_result = REPLACE_ABORT;
689 destroy_dlg (ui->replace_dlg);
692 switch (ui->replace_result){
693 case REPLACE_UPDATE:
694 do_refresh ();
695 if (_s_stat->st_mtime > _d_stat->st_mtime)
696 return FILE_CONT;
697 else
698 return FILE_SKIP;
700 case REPLACE_SIZE:
701 do_refresh ();
702 if (_s_stat->st_size == _d_stat->st_size)
703 return FILE_SKIP;
704 else
705 return FILE_CONT;
707 case REPLACE_REGET:
708 /* Carefull: we fall through and set do_append */
709 ctx->do_reget = _d_stat->st_size;
711 case REPLACE_APPEND:
712 ctx->do_append = 1;
714 case REPLACE_YES:
715 case REPLACE_ALWAYS:
716 do_refresh ();
717 return FILE_CONT;
718 case REPLACE_NO:
719 case REPLACE_NEVER:
720 do_refresh ();
721 return FILE_SKIP;
722 case REPLACE_ABORT:
723 default:
724 return FILE_ABORT;
728 #define FMDY 13
729 #define FMD_XLEN 64
730 extern int fmd_xlen;
731 static QuickWidget fmd_widgets [] = {
733 #define FMCB0 FMDC
734 #define FMCB12 0
735 #define FMCB11 1
736 /* follow symlinks and preserve Attributes must be the first */
737 { quick_checkbox, 3, 64, 8, FMDY, N_("preserve &Attributes"), 9, 0,
738 0 /* &op_preserve */, 0, "preserve" },
739 { quick_checkbox, 3, 64, 7, FMDY, N_("follow &Links"), 7, 0,
740 0 /* &file_mask_op_follow_links */, 0, "follow" },
741 { quick_label, 3, 64, 5, FMDY, N_("to:"), 0, 0, 0, 0,"to"},
742 { quick_checkbox, 37, 64, 4, FMDY, N_("&Using shell patterns"), 0, 0,
743 0 /* &source_easy_patterns */, 0, "using-shell" },
744 { quick_input, 3, 64, 3, FMDY, "", 58,
745 0, 0, 0, "input-def" },
746 #define FMDI1 4
747 #define FMDI2 5
748 #define FMDC 3
749 { quick_input, 3, 64, 6, FMDY, "", 58, 0,
750 0, 0, "input2" },
751 #define FMDI0 6
752 { quick_label, 3, 64, 2, FMDY, "", 0, 0, 0, 0, "ql" },
753 #define FMBRGT 7
754 { quick_button, 42, 64, 9, FMDY, N_("&Cancel"), 0, B_CANCEL, 0, 0,
755 "cancel" },
756 #undef SKIP
757 #ifdef WITH_BACKGROUND
758 # define SKIP 5
759 # define FMCB21 11
760 # define FMCB22 10
761 # define FMBLFT 9
762 # define FMBMID 8
763 { quick_button, 25, 64, 9, FMDY, N_("&Background"), 0, B_USER, 0, 0, "back" },
764 #else /* WITH_BACKGROUND */
765 # define SKIP 4
766 # define FMCB21 10
767 # define FMCB22 9
768 # define FMBLFT 8
769 # undef FMBMID
770 #endif
771 { quick_button, 14, 64, 9, FMDY, N_("&Ok"), 0, B_ENTER, 0, 0, "ok" },
772 { quick_checkbox, 42, 64, 8, FMDY, N_("&Stable Symlinks"), 0, 0,
773 0 /* &file_mask_stable_symlinks */, 0, "stab-sym" },
774 { quick_checkbox, 31, 64, 7, FMDY, N_("&Dive into subdir if exists"), 0, 0,
775 0 /* &dive_into_subdirs */, 0, "dive" },
776 { 0 } };
778 void
779 fmd_init_i18n (int force)
781 #ifdef ENABLE_NLS
782 static int initialized = FALSE;
783 register int i;
784 int len;
786 if (initialized && !force)
787 return;
789 for (i = sizeof (op_names) / sizeof (op_names[0]); i--;)
790 op_names [i] = _(op_names [i]);
792 i = sizeof (fmd_widgets) / sizeof (fmd_widgets [0]) - 1;
793 while (i--)
794 if (fmd_widgets [i].text[0] != '\0')
795 fmd_widgets [i].text = _(fmd_widgets [i].text);
797 len = strlen (fmd_widgets [FMCB11].text)
798 + strlen (fmd_widgets [FMCB21].text) + 15;
799 fmd_xlen = max (fmd_xlen, len);
801 len = strlen (fmd_widgets [FMCB12].text)
802 + strlen (fmd_widgets [FMCB22].text) + 15;
803 fmd_xlen = max (fmd_xlen, len);
805 len = strlen (fmd_widgets [FMBRGT].text)
806 + strlen (fmd_widgets [FMBLFT].text) + 11;
808 #ifdef FMBMID
809 len += strlen (fmd_widgets [FMBMID].text) + 6;
810 #endif
812 fmd_xlen = max (fmd_xlen, len + 4);
814 len = (fmd_xlen - (len + 6)) / 2;
815 i = fmd_widgets [FMBLFT].relative_x = len + 3;
816 i += strlen (fmd_widgets [FMBLFT].text) + 8;
818 #ifdef FMBMID
819 fmd_widgets [FMBMID].relative_x = i;
820 i += strlen (fmd_widgets [FMBMID].text) + 6;
821 #endif
823 fmd_widgets [FMBRGT].relative_x = i;
825 #define chkbox_xpos(i) \
826 fmd_widgets [i].relative_x = fmd_xlen - strlen (fmd_widgets [i].text) - 6
828 chkbox_xpos(FMCB0);
829 chkbox_xpos(FMCB21);
830 chkbox_xpos(FMCB22);
832 if (fmd_xlen != FMD_XLEN)
834 i = sizeof (fmd_widgets) / sizeof (fmd_widgets [0]) - 1;
835 while (i--)
836 fmd_widgets [i].x_divisions = fmd_xlen;
838 fmd_widgets [FMDI1].hotkey_pos =
839 fmd_widgets [FMDI2].hotkey_pos = fmd_xlen - 6;
841 #undef chkbox_xpos
843 initialized = TRUE;
844 #endif /* !ENABLE_NLS */
847 char *
848 file_mask_dialog (FileOpContext *ctx, FileOperation operation, char *text,
849 char *def_text, int only_one, int *do_background)
851 int source_easy_patterns = easy_patterns;
852 char *source_mask, *orig_mask, *dest_dir;
853 const char *error;
854 struct stat buf;
855 int val;
856 QuickDialog Quick_input;
858 g_return_val_if_fail (ctx != NULL, NULL);
859 #if 0
860 message_3s (1, __FUNCTION__, "text = `%s' \n def_text = `%s'", text, def_text);
861 #endif
862 fmd_init_i18n (FALSE);
864 /* Set up the result pointers */
866 fmd_widgets[FMCB12].result = &ctx->op_preserve;
867 fmd_widgets[FMCB11].result = &ctx->follow_links;
868 fmd_widgets[FMCB22].result = &ctx->stable_symlinks;
869 fmd_widgets[FMCB21].result = &ctx->dive_into_subdirs;
871 /* Create the dialog */
873 ctx->stable_symlinks = 0;
874 fmd_widgets [FMDC].result = &source_easy_patterns;
875 fmd_widgets [FMDI1].text = easy_patterns ? "*" : "^\\(.*\\)$";
876 Quick_input.xlen = fmd_xlen;
877 Quick_input.xpos = -1;
878 Quick_input.title = op_names [operation];
879 Quick_input.help = "[Mask Copy/Rename]";
880 Quick_input.ylen = FMDY;
881 Quick_input.i18n = 1;
882 Quick_input.widgets = fmd_widgets;
883 fmd_widgets [FMDI0].text = text;
884 fmd_widgets [FMDI2].text = def_text;
885 fmd_widgets [FMDI2].str_result = &dest_dir;
886 fmd_widgets [FMDI1].str_result = &source_mask;
888 *do_background = 0;
889 ask_file_mask:
891 if ((val = quick_dialog_skip (&Quick_input, SKIP)) == B_CANCEL)
892 return 0;
894 if (ctx->follow_links)
895 ctx->stat_func = (mc_stat_fn) mc_stat;
896 else
897 ctx->stat_func = (mc_stat_fn) mc_lstat;
899 if (ctx->op_preserve) {
900 ctx->preserve = 1;
901 ctx->umask_kill = 0777777;
902 ctx->preserve_uidgid = (geteuid () == 0) ? 1 : 0;
903 } else {
904 int i;
905 ctx->preserve = ctx->preserve_uidgid = 0;
906 i = umask (0);
907 umask (i);
908 ctx->umask_kill = i ^ 0777777;
911 orig_mask = source_mask;
912 if (!dest_dir || !*dest_dir){
913 if (source_mask)
914 g_free (source_mask);
915 return dest_dir;
917 if (source_easy_patterns){
918 source_easy_patterns = easy_patterns;
919 easy_patterns = 1;
920 source_mask = convert_pattern (source_mask, match_file, 1);
921 easy_patterns = source_easy_patterns;
922 error = re_compile_pattern (source_mask, strlen (source_mask), &ctx->rx);
923 g_free (source_mask);
924 } else
925 error = re_compile_pattern (source_mask, strlen (source_mask), &ctx->rx);
927 if (error){
928 message_3s (1, MSG_ERROR, _("Invalid source pattern `%s' \n %s "),
929 orig_mask, error);
930 if (orig_mask)
931 g_free (orig_mask);
932 goto ask_file_mask;
934 if (orig_mask)
935 g_free (orig_mask);
936 ctx->dest_mask = strrchr (dest_dir, PATH_SEP);
937 if (ctx->dest_mask == NULL)
938 ctx->dest_mask = dest_dir;
939 else
940 ctx->dest_mask++;
941 orig_mask = ctx->dest_mask;
942 if (!*ctx->dest_mask || (!ctx->dive_into_subdirs && !is_wildcarded (ctx->dest_mask) &&
943 (!only_one || (!mc_stat (dest_dir, &buf) && S_ISDIR (buf.st_mode)))) ||
944 (ctx->dive_into_subdirs && ((!only_one && !is_wildcarded (ctx->dest_mask)) ||
945 (only_one
946 && !mc_stat (dest_dir, &buf) && S_ISDIR (buf.st_mode)))))
947 ctx->dest_mask = g_strdup ("*");
948 else {
949 ctx->dest_mask = g_strdup (ctx->dest_mask);
950 *orig_mask = 0;
952 if (!*dest_dir){
953 g_free (dest_dir);
954 dest_dir = g_strdup ("./");
956 if (val == B_USER)
957 *do_background = 1;
958 return dest_dir;