Move logging.[ch] into library
[midnight-commander.git] / src / filegui.c
blobb88af3b168da38d90127d05c5cf483c4bd971ef7
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, 2009 Free Software Foundation, Inc.
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
12 * 2009 Slava Zanko
14 * The copy code was based in GNU's cp, and was written by:
15 * Torbjorn Granlund, David MacKenzie, and Jim Meyering.
17 * The move code was based in GNU's mv, and was written by:
18 * Mike Parker and David MacKenzie.
20 * Janne Kukonlehto added much error recovery to them for being used
21 * in an interactive program.
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
39 * Please note that all dialogs used here must be safe for background
40 * operations.
43 /** \file filegui.c
44 * \brief Source: file management GUI for the text mode edition
47 /* {{{ Include files */
49 #include <config.h>
51 #include <errno.h>
52 #include <ctype.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
58 #if defined(STAT_STATVFS) \
59 && (defined(HAVE_STRUCT_STATVFS_F_BASETYPE) \
60 || defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME))
61 # include <sys/statvfs.h>
62 # define STRUCT_STATFS struct statvfs
63 # define STATFS statvfs
64 #elif defined(HAVE_STATFS) && !defined(STAT_STATFS4)
65 # ifdef HAVE_SYS_VFS_H
66 # include <sys/vfs.h>
67 # elif defined(HAVE_SYS_MOUNT_H) && defined(HAVE_SYS_PARAM_H)
68 # include <sys/param.h>
69 # include <sys/mount.h>
70 # elif defined(HAVE_SYS_STATFS_H)
71 # include <sys/statfs.h>
72 # endif
73 # define STRUCT_STATFS struct statfs
74 # define STATFS statfs
75 #endif
77 #include <unistd.h>
79 #include "lib/global.h"
81 #include "lib/tty/key.h" /* tty_get_event */
82 #include "lib/search.h"
83 #include "lib/vfs/mc-vfs/vfs.h"
84 #include "lib/strescape.h"
85 #include "lib/strutil.h"
87 #include "setup.h" /* verbose */
88 #include "dialog.h" /* do_refresh() */
89 #include "widget.h" /* WLabel */
90 #include "main-widgets.h"
91 #include "main.h" /* the_hint */
92 #include "wtools.h" /* QuickDialog */
93 #include "panel.h" /* current_panel */
94 #include "fileopctx.h" /* FILE_CONT */
95 #include "filegui.h"
97 /* }}} */
98 typedef enum {
99 MSDOS_SUPER_MAGIC = 0x4d44,
100 NTFS_SB_MAGIC = 0x5346544e,
101 NTFS_3G_MAGIC = 0x65735546,
102 PROC_SUPER_MAGIC = 0x9fa0,
103 SMB_SUPER_MAGIC = 0x517B,
104 NCP_SUPER_MAGIC = 0x564c,
105 USBDEVICE_SUPER_MAGIC = 0x9fa2
106 } filegui_nonattrs_fs_t;
108 /* Hack: the vfs code should not rely on this */
109 #define WITH_FULL_PATHS 1
111 /* This structure describes the UI and internal data required by a file
112 * operation context.
114 typedef struct {
115 /* ETA and bps */
117 int showing_eta;
118 int showing_bps;
119 int eta_extra;
121 /* Dialog and widgets for the operation progress window */
123 Dlg_head *op_dlg;
125 WLabel *file_label[2];
126 WLabel *file_string[2];
127 WLabel *progress_label[3];
128 WGauge *progress_gauge[3];
129 WLabel *eta_label;
130 WLabel *bps_label;
131 WLabel *stalled_label;
133 /* Query replace dialog */
135 Dlg_head *replace_dlg;
136 const char *replace_filename;
137 int replace_result;
138 struct stat *s_stat, *d_stat;
139 } FileOpContextUI;
142 /* Used to save the hint line */
143 static int last_hint_line;
145 /* File operate window sizes */
146 #define WX 62
147 #define WY 10
148 #define BY 10
149 #define WX_ETA_EXTRA 12
151 #define FCOPY_GAUGE_X 14
152 #define FCOPY_LABEL_X 5
154 /* Used for button result values */
155 enum {
156 REPLACE_YES = B_USER,
157 REPLACE_NO,
158 REPLACE_APPEND,
159 REPLACE_ALWAYS,
160 REPLACE_UPDATE,
161 REPLACE_NEVER,
162 REPLACE_ABORT,
163 REPLACE_SIZE,
164 REPLACE_REGET
167 static int
168 filegui__check_attrs_on_fs(const char *fs_path)
170 #ifdef STATFS
171 STRUCT_STATFS stfs;
173 if (!setup_copymove_persistent_attr)
174 return 0;
176 if (STATFS(fs_path, &stfs)!=0)
177 return 1;
179 # ifdef __linux__
180 switch ((filegui_nonattrs_fs_t) stfs.f_type)
182 case MSDOS_SUPER_MAGIC:
183 case NTFS_SB_MAGIC:
184 case NTFS_3G_MAGIC:
185 case PROC_SUPER_MAGIC:
186 case SMB_SUPER_MAGIC:
187 case NCP_SUPER_MAGIC:
188 case USBDEVICE_SUPER_MAGIC:
189 return 0;
190 break;
192 # elif defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) \
193 || defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
194 if (!strcmp(stfs.f_fstypename, "msdos")
195 || !strcmp(stfs.f_fstypename, "msdosfs")
196 || !strcmp(stfs.f_fstypename, "ntfs")
197 || !strcmp(stfs.f_fstypename, "procfs")
198 || !strcmp(stfs.f_fstypename, "smbfs")
199 || strstr(stfs.f_fstypename, "fusefs"))
200 return 0;
201 # elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
202 if (!strcmp(stfs.f_basetype, "pcfs")
203 || !strcmp(stfs.f_basetype, "ntfs")
204 || !strcmp(stfs.f_basetype, "proc")
205 || !strcmp(stfs.f_basetype, "smbfs")
206 || !strcmp(stfs.f_basetype, "fuse"))
207 return 0;
208 # endif
209 #endif /* STATFS */
211 return 1;
214 static FileProgressStatus
215 check_progress_buttons (FileOpContext *ctx)
217 int c;
218 Gpm_Event event;
219 FileOpContextUI *ui;
221 if (ctx->ui == NULL)
222 return FILE_CONT;
224 ui = ctx->ui;
226 event.x = -1; /* Don't show the GPM cursor */
227 c = tty_get_event (&event, FALSE, FALSE);
228 if (c == EV_NONE)
229 return FILE_CONT;
231 /* Reinitialize to avoid old values after events other than
232 selecting a button */
233 ui->op_dlg->ret_value = FILE_CONT;
235 dlg_process_event (ui->op_dlg, c, &event);
236 switch (ui->op_dlg->ret_value) {
237 case FILE_SKIP:
238 return FILE_SKIP;
239 break;
240 case B_CANCEL:
241 case FILE_ABORT:
242 return FILE_ABORT;
243 break;
244 default:
245 return FILE_CONT;
249 /* {{{ File progress display routines */
251 void
252 file_op_context_create_ui_without_init (FileOpContext *ctx, int with_eta)
254 FileOpContextUI *ui;
255 int x_size;
256 int minus;
257 int eta_offset;
258 const char *sixty;
259 const char *fifteen;
261 g_return_if_fail (ctx != NULL);
262 g_return_if_fail (ctx->ui == NULL);
264 ui = g_new0 (FileOpContextUI, 1);
265 ctx->ui = ui;
267 minus = verbose ? 0 : 3;
268 eta_offset = with_eta ? (WX_ETA_EXTRA) / 2 : 0;
270 sixty = "";
271 fifteen = "";
273 ctx->recursive_result = 0;
275 ui->replace_result = 0;
276 ui->showing_eta = with_eta;
277 ui->showing_bps = with_eta;
278 ui->eta_extra = with_eta ? WX_ETA_EXTRA : 0;
279 x_size = (WX + 4) + ui->eta_extra;
281 ui->op_dlg =
282 create_dlg (0, 0, WY - minus + 4, x_size, dialog_colors, NULL,
283 NULL, op_names[ctx->operation],
284 DLG_CENTER | DLG_REVERSE);
286 last_hint_line = the_hint->widget.y;
287 if ((ui->op_dlg->y + ui->op_dlg->lines) > last_hint_line)
288 the_hint->widget.y = ui->op_dlg->y + ui->op_dlg->lines + 1;
290 add_widget (ui->op_dlg,
291 button_new (BY - minus, WX - 19 + eta_offset, FILE_ABORT,
292 NORMAL_BUTTON, _("&Abort"), 0));
293 add_widget (ui->op_dlg,
294 button_new (BY - minus, 14 + eta_offset, FILE_SKIP,
295 NORMAL_BUTTON, _("&Skip"), 0));
297 add_widget (ui->op_dlg, ui->progress_gauge[2] =
298 gauge_new (7, FCOPY_GAUGE_X, 0, 100, 0));
299 add_widget (ui->op_dlg, ui->progress_label[2] =
300 label_new (7, FCOPY_LABEL_X, fifteen));
301 add_widget (ui->op_dlg, ui->bps_label = label_new (7, WX, ""));
303 add_widget (ui->op_dlg, ui->progress_gauge[1] =
304 gauge_new (8, FCOPY_GAUGE_X, 0, 100, 0));
305 add_widget (ui->op_dlg, ui->progress_label[1] =
306 label_new (8, FCOPY_LABEL_X, fifteen));
307 add_widget (ui->op_dlg, ui->stalled_label = label_new (8, WX, ""));
309 add_widget (ui->op_dlg, ui->progress_gauge[0] =
310 gauge_new (6, FCOPY_GAUGE_X, 0, 100, 0));
311 add_widget (ui->op_dlg, ui->progress_label[0] =
312 label_new (6, FCOPY_LABEL_X, fifteen));
313 add_widget (ui->op_dlg, ui->eta_label = label_new (6, WX, ""));
315 add_widget (ui->op_dlg, ui->file_string[1] =
316 label_new (4, FCOPY_GAUGE_X, sixty));
317 add_widget (ui->op_dlg, ui->file_label[1] =
318 label_new (4, FCOPY_LABEL_X, fifteen));
319 add_widget (ui->op_dlg, ui->file_string[0] =
320 label_new (3, FCOPY_GAUGE_X, sixty));
321 add_widget (ui->op_dlg, ui->file_label[0] =
322 label_new (3, FCOPY_LABEL_X, fifteen));
325 void
326 file_op_context_create_ui (FileOpContext *ctx, int with_eta)
328 FileOpContextUI *ui;
330 g_return_if_fail (ctx != NULL);
331 g_return_if_fail (ctx->ui == NULL);
333 file_op_context_create_ui_without_init(ctx, with_eta);
334 ui = ctx->ui;
336 /* We will manage the dialog without any help, that's why
337 we have to call init_dlg */
338 init_dlg (ui->op_dlg);
339 ui->op_dlg->running = 1;
342 void
343 file_op_context_destroy_ui (FileOpContext *ctx)
345 FileOpContextUI *ui;
347 g_return_if_fail (ctx != NULL);
349 if (ctx->ui) {
350 ui = ctx->ui;
352 dlg_run_done (ui->op_dlg);
353 destroy_dlg (ui->op_dlg);
354 g_free (ui);
357 the_hint->widget.y = last_hint_line;
359 ctx->ui = NULL;
362 static FileProgressStatus
363 show_no_bar (FileOpContext *ctx, int n)
365 FileOpContextUI *ui;
367 if (ctx->ui == NULL)
368 return FILE_CONT;
370 ui = ctx->ui;
372 if (n >= 0) {
373 label_set_text (ui->progress_label[n], "");
374 gauge_show (ui->progress_gauge[n], 0);
376 return check_progress_buttons (ctx);
379 static FileProgressStatus
380 show_bar (FileOpContext *ctx, int n, double done, double total)
382 FileOpContextUI *ui;
384 if (ctx->ui == NULL)
385 return FILE_CONT;
387 ui = ctx->ui;
390 * Gauge needs integers, so give it with integers between 0 and 1023.
391 * This precision should be quite reasonable.
393 gauge_set_value (ui->progress_gauge[n], 1024,
394 (int) (1024 * done / total));
395 gauge_show (ui->progress_gauge[n], 1);
396 return check_progress_buttons (ctx);
399 static void
400 file_eta_show (FileOpContext *ctx)
402 int eta_hours, eta_mins, eta_s;
403 char eta_buffer[BUF_TINY];
404 FileOpContextUI *ui;
406 if (ctx->ui == NULL)
407 return;
409 ui = ctx->ui;
411 if (!ui->showing_eta)
412 return;
414 if (ctx->eta_secs > 0.5) {
415 eta_hours = ctx->eta_secs / (60 * 60);
416 eta_mins = (ctx->eta_secs - (eta_hours * 60 * 60)) / 60;
417 eta_s = ctx->eta_secs - (eta_hours * 60 * 60 + eta_mins * 60);
418 g_snprintf (eta_buffer, sizeof (eta_buffer), _("ETA %d:%02d.%02d"),
419 eta_hours, eta_mins, eta_s);
420 } else
421 *eta_buffer = 0;
423 label_set_text (ui->eta_label, eta_buffer);
426 static void
427 file_bps_show (FileOpContext *ctx)
429 char bps_buffer[BUF_TINY];
430 FileOpContextUI *ui;
432 if (ctx->ui == NULL)
433 return;
435 ui = ctx->ui;
437 if (!ui->showing_bps)
438 return;
440 if (ctx->bps > 1024 * 1024) {
441 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%.2f MB/s"),
442 ctx->bps / (1024 * 1024.0));
443 } else if (ctx->bps > 1024) {
444 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%.2f KB/s"),
445 ctx->bps / 1024.0);
446 } else if (ctx->bps > 1) {
447 g_snprintf (bps_buffer, sizeof (bps_buffer), _("%ld B/s"),
448 ctx->bps);
449 } else
450 *bps_buffer = 0;
452 label_set_text (ui->bps_label, bps_buffer);
455 FileProgressStatus
456 file_progress_show (FileOpContext *ctx, off_t done, off_t total)
458 FileOpContextUI *ui;
460 g_return_val_if_fail (ctx != NULL, FILE_CONT);
462 if (ctx->ui == NULL)
463 return FILE_CONT;
465 ui = ctx->ui;
467 if (!verbose)
468 return check_progress_buttons (ctx);
469 if (total > 0) {
470 label_set_text (ui->progress_label[0], _("File"));
471 file_eta_show (ctx);
472 file_bps_show (ctx);
473 return show_bar (ctx, 0, done, total);
474 } else
475 return show_no_bar (ctx, 0);
478 FileProgressStatus
479 file_progress_show_count (FileOpContext *ctx, off_t done, off_t total)
481 FileOpContextUI *ui;
483 g_return_val_if_fail (ctx != NULL, FILE_CONT);
485 if (ctx->ui == NULL)
486 return FILE_CONT;
488 ui = ctx->ui;
490 if (!verbose)
491 return check_progress_buttons (ctx);
492 if (total > 0) {
493 label_set_text (ui->progress_label[1], _("Count"));
494 return show_bar (ctx, 1, done, total);
495 } else
496 return show_no_bar (ctx, 1);
499 FileProgressStatus
500 file_progress_show_bytes (FileOpContext *ctx, double done, double total)
502 FileOpContextUI *ui;
504 g_return_val_if_fail (ctx != NULL, FILE_CONT);
506 if (ctx->ui == NULL)
507 return FILE_CONT;
509 ui = ctx->ui;
511 if (!verbose)
512 return check_progress_buttons (ctx);
513 if (total > 0) {
514 label_set_text (ui->progress_label[2], _("Bytes"));
515 return show_bar (ctx, 2, done, total);
516 } else
517 return show_no_bar (ctx, 2);
520 /* }}} */
522 #define truncFileString(ui, s) str_trunc (s, ui->eta_extra + 47)
523 #define truncFileStringSecure(ui, s) path_trunc (s, ui->eta_extra + 47)
525 FileProgressStatus
526 file_progress_show_source (FileOpContext *ctx, const char *s)
528 FileOpContextUI *ui;
530 g_return_val_if_fail (ctx != NULL, FILE_CONT);
532 if (ctx->ui == NULL)
533 return FILE_CONT;
535 ui = ctx->ui;
537 if (s != NULL) {
538 #ifdef WITH_FULL_PATHS
539 int i = strlen (current_panel->cwd);
541 /* We remove the full path we have added before */
542 if (!strncmp (s, current_panel->cwd, i)) {
543 if (s[i] == PATH_SEP)
544 s += i + 1;
546 #endif /* WITH_FULL_PATHS */
548 label_set_text (ui->file_label[0], _("Source"));
549 label_set_text (ui->file_string[0], truncFileString (ui, s));
550 return check_progress_buttons (ctx);
551 } else {
552 label_set_text (ui->file_label[0], "");
553 label_set_text (ui->file_string[0], "");
554 return check_progress_buttons (ctx);
558 FileProgressStatus
559 file_progress_show_target (FileOpContext *ctx, const char *s)
561 FileOpContextUI *ui;
563 g_return_val_if_fail (ctx != NULL, FILE_CONT);
565 if (ctx->ui == NULL)
566 return FILE_CONT;
568 ui = ctx->ui;
570 if (s != NULL) {
571 label_set_text (ui->file_label[1], _("Target"));
572 label_set_text (ui->file_string[1], truncFileStringSecure (ui, s));
573 return check_progress_buttons (ctx);
574 } else {
575 label_set_text (ui->file_label[1], "");
576 label_set_text (ui->file_string[1], "");
577 return check_progress_buttons (ctx);
581 FileProgressStatus
582 file_progress_show_deleting (FileOpContext *ctx, const char *s)
584 FileOpContextUI *ui;
586 g_return_val_if_fail (ctx != NULL, FILE_CONT);
588 if (ctx->ui == NULL)
589 return FILE_CONT;
591 ui = ctx->ui;
593 label_set_text (ui->file_label[0], _("Deleting"));
594 label_set_text (ui->file_label[0], truncFileStringSecure (ui, s));
595 return check_progress_buttons (ctx);
599 * FIXME: probably it is better to replace this with quick dialog machinery,
600 * but actually I'm not familiar with it and have not much time :(
601 * alex
605 static int
606 overwrite_query_dialog (FileOpContext *ctx, enum OperationMode mode)
608 #define ADD_RD_BUTTON(i)\
609 add_widget (ui->replace_dlg,\
610 button_new (rd_widgets [i].ypos, rd_widgets [i].xpos, rd_widgets [i].value,\
611 NORMAL_BUTTON, rd_widgets [i].text, 0))
613 #define ADD_RD_LABEL(i, p1, p2)\
614 g_snprintf (buffer, sizeof (buffer), rd_widgets [i].text, p1, p2);\
615 add_widget (ui->replace_dlg,\
616 label_new (rd_widgets [i].ypos, rd_widgets [i].xpos, buffer))
618 /* dialog sizes */
619 const int rd_ylen = 17;
620 int rd_xlen = 60;
622 struct {
623 const char *text;
624 int ypos, xpos;
625 int value; /* 0 for labels */
626 } rd_widgets[] = {
627 /* 0 */ { N_("Target file already exists!"), 3, 4, 0 },
628 /* 1 */ { "%s", 4, 4, 0 },
629 #if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined _LARGE_FILES && _LARGE_FILES)
630 /* 2 */ { N_("Source date: %s, size %llu"), 6, 4, 0 },
631 /* 3 */ { N_("Target date: %s, size %llu"), 7, 4, 0 },
632 #else
633 /* 2 */ { N_("Source date: %s, size %u"), 6, 4, 0 },
634 /* 3 */ { N_("Target date: %s, size %u"), 7, 4, 0 },
635 #endif
636 /* 4 */ { N_("&Abort"), 14, 25, REPLACE_ABORT },
637 /* 5 */ { N_("If &size differs"), 12, 28, REPLACE_SIZE },
638 /* 6 */ { N_("Non&e"), 11, 47, REPLACE_NEVER },
639 /* 7 */ { N_("&Update"), 11, 36, REPLACE_UPDATE },
640 /* 8 */ { N_("A&ll"), 11, 28, REPLACE_ALWAYS },
641 /* 9 */ { N_("Overwrite all targets?"), 11, 4, 0 },
642 /* 10 */ { N_("&Reget"), 10, 28, REPLACE_REGET },
643 /* 11 */ { N_("A&ppend"), 9, 45, REPLACE_APPEND },
644 /* 12 */ { N_("&No"), 9, 37, REPLACE_NO },
645 /* 13 */ { N_("&Yes"), 9, 28, REPLACE_YES },
646 /* 14 */ { N_("Overwrite this target?"), 9, 4, 0 }
649 const int num = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
650 int *widgets_len;
652 FileOpContextUI *ui = ctx->ui;
654 char buffer[BUF_SMALL];
655 const char *title;
656 const char *stripped_name = strip_home_and_password (ui->replace_filename);
657 int stripped_name_len;
659 int result;
661 widgets_len = g_new0 (int, num);
663 if (mode == Foreground)
664 title = _(" File exists ");
665 else
666 title = _(" Background process: File exists ");
668 stripped_name_len = str_term_width1 (stripped_name);
671 int i, l1, l2, l, row;
673 for (i = 0; i < num; i++) {
674 #ifdef ENABLE_NLS
675 if (i != 1) /* skip filename */
676 rd_widgets[i].text = _(rd_widgets[i].text);
677 #endif /* ENABLE_NLS */
678 widgets_len [i] = str_term_width1 (rd_widgets[i].text);
682 * longest of "Overwrite..." labels
683 * (assume "Target date..." are short enough)
685 l1 = max (widgets_len[9], widgets_len[14]);
687 /* longest of button rows */
688 i = num;
689 for (row = l = l2 = 0; i--;)
690 if (rd_widgets[i].value != 0) {
691 if (row != rd_widgets[i].ypos) {
692 row = rd_widgets[i].ypos;
693 l2 = max (l2, l);
694 l = 0;
696 l += widgets_len[i] + 4;
699 l2 = max (l2, l); /* last row */
700 rd_xlen = max (rd_xlen, l1 + l2 + 8);
701 rd_xlen = max (rd_xlen, str_term_width1 (title) + 2);
702 rd_xlen = max (rd_xlen, min (COLS, stripped_name_len + 8));
704 /* Now place widgets */
705 l1 += 5; /* start of first button in the row */
706 i = num;
707 for (l = l1, row = 0; --i > 1;)
708 if (rd_widgets[i].value != 0) {
709 if (row != rd_widgets[i].ypos) {
710 row = rd_widgets[i].ypos;
711 l = l1;
713 rd_widgets[i].xpos = l;
714 l += widgets_len[i] + 4;
717 /* Abort button is centered */
718 rd_widgets[4].xpos = (rd_xlen - widgets_len[4] - 3) / 2;
721 /* FIXME - missing help node */
722 ui->replace_dlg =
723 create_dlg (0, 0, rd_ylen, rd_xlen, alarm_colors, NULL, "[Replace]",
724 title, DLG_CENTER | DLG_REVERSE);
726 /* prompt -- centered */
727 add_widget (ui->replace_dlg,
728 label_new (rd_widgets [0].ypos,
729 (rd_xlen - widgets_len [0]) / 2,
730 rd_widgets [0].text));
731 /* file name -- centered */
732 stripped_name = str_trunc (stripped_name, rd_xlen - 8);
733 stripped_name_len = str_term_width1 (stripped_name);
734 add_widget (ui->replace_dlg,
735 label_new (rd_widgets [1].ypos,
736 (rd_xlen - stripped_name_len) / 2,
737 stripped_name));
739 /* source date */
740 ADD_RD_LABEL (2, file_date (ui->s_stat->st_mtime),
741 (off_t) ui->s_stat->st_size);
742 /* destination date */
743 ADD_RD_LABEL (3, file_date (ui->d_stat->st_mtime),
744 (off_t) ui->d_stat->st_size);
746 ADD_RD_BUTTON (4); /* Abort */
747 ADD_RD_BUTTON (5); /* If size differs */
748 ADD_RD_BUTTON (6); /* None */
749 ADD_RD_BUTTON (7); /* Update */
750 ADD_RD_BUTTON (8); /* All" */
751 ADD_RD_LABEL (9, 0, 0); /* Overwrite all targets? */
753 /* "this target..." widgets */
754 if (!S_ISDIR (ui->d_stat->st_mode)) {
755 if ((ctx->operation == OP_COPY) && (ui->d_stat->st_size != 0)
756 && (ui->s_stat->st_size > ui->d_stat->st_size))
757 ADD_RD_BUTTON (10); /* Reget */
759 ADD_RD_BUTTON (11); /* Append */
761 ADD_RD_BUTTON (12); /* No */
762 ADD_RD_BUTTON (13); /* Yes */
763 ADD_RD_LABEL (14, 0, 0); /* Overwrite this target? */
765 result = run_dlg (ui->replace_dlg);
766 destroy_dlg (ui->replace_dlg);
768 g_free (widgets_len);
770 return result;
771 #undef ADD_RD_LABEL
772 #undef ADD_RD_BUTTON
775 void
776 file_progress_set_stalled_label (FileOpContext *ctx, const char *stalled_msg)
778 FileOpContextUI *ui;
780 g_return_if_fail (ctx != NULL);
781 g_return_if_fail (ctx->ui != NULL);
783 ui = ctx->ui;
784 label_set_text (ui->stalled_label, stalled_msg);
787 FileProgressStatus
788 file_progress_real_query_replace (FileOpContext *ctx,
789 enum OperationMode mode, const char *destname,
790 struct stat *_s_stat,
791 struct stat *_d_stat)
793 FileOpContextUI *ui;
795 g_return_val_if_fail (ctx != NULL, FILE_CONT);
796 g_return_val_if_fail (ctx->ui != NULL, FILE_CONT);
798 ui = ctx->ui;
800 if (ui->replace_result < REPLACE_ALWAYS) {
801 ui->replace_filename = destname;
802 ui->s_stat = _s_stat;
803 ui->d_stat = _d_stat;
804 ui->replace_result = overwrite_query_dialog (ctx, mode);
805 if (ui->replace_result == B_CANCEL)
806 ui->replace_result = REPLACE_ABORT;
809 switch (ui->replace_result) {
810 case REPLACE_UPDATE:
811 do_refresh ();
812 if (_s_stat->st_mtime > _d_stat->st_mtime)
813 return FILE_CONT;
814 else
815 return FILE_SKIP;
817 case REPLACE_SIZE:
818 do_refresh ();
819 if (_s_stat->st_size == _d_stat->st_size)
820 return FILE_SKIP;
821 else
822 return FILE_CONT;
824 case REPLACE_REGET:
825 /* Careful: we fall through and set do_append */
826 ctx->do_reget = _d_stat->st_size;
828 case REPLACE_APPEND:
829 ctx->do_append = 1;
831 case REPLACE_YES:
832 case REPLACE_ALWAYS:
833 do_refresh ();
834 return FILE_CONT;
835 case REPLACE_NO:
836 case REPLACE_NEVER:
837 do_refresh ();
838 return FILE_SKIP;
839 case REPLACE_ABORT:
840 default:
841 return FILE_ABORT;
845 static gboolean
846 is_wildcarded (char *p)
848 for (; *p; p++) {
849 if (*p == '*')
850 return TRUE;
851 if (*p == '\\' && p[1] >= '1' && p[1] <= '9')
852 return TRUE;
854 return FALSE;
857 char *
858 file_mask_dialog (FileOpContext *ctx, FileOperation operation,
859 gboolean only_one,
860 const char *format, const void *text,
861 const char *def_text, gboolean *do_background)
863 const size_t FMDY = 13;
864 const size_t FMDX = 68;
865 size_t fmd_xlen;
867 /* buttons */
868 const size_t gap = 1;
869 size_t b0_len, b2_len;
870 size_t b1_len = 0;
872 int source_easy_patterns = easy_patterns;
873 size_t i, len;
874 char fmd_buf [BUF_MEDIUM];
875 char *source_mask, *orig_mask, *dest_dir, *tmp;
876 char *def_text_secure;
877 int val;
879 QuickWidget fmd_widgets[] =
881 /* 0 */ QUICK_BUTTON (42, 64, 10, FMDY, N_("&Cancel"), B_CANCEL, NULL),
882 #ifdef WITH_BACKGROUND
883 /* 1 */ QUICK_BUTTON (25, 64, 10, FMDY, N_("&Background"), B_USER, NULL),
884 #define OFFSET 0
885 #else
886 #define OFFSET 1
887 #endif /* WITH_BACKGROUND */
888 /* 2 - OFFSET */
889 QUICK_BUTTON (14, FMDX, 10, FMDY, N_("&OK"), B_ENTER, NULL),
890 /* 3 - OFFSET */
891 QUICK_CHECKBOX (42, FMDX, 8, FMDY, N_("&Stable Symlinks"), &ctx->stable_symlinks),
892 /* 4 - OFFSET */
893 QUICK_CHECKBOX (31, FMDX, 7, FMDY, N_("di&Ve into subdir if exists"), &ctx->dive_into_subdirs),
894 /* 5 - OFFSET */
895 QUICK_CHECKBOX (3, FMDX, 8, FMDY, N_("preserve &Attributes"), &ctx->op_preserve),
896 /* 6 - OFFSET */
897 QUICK_CHECKBOX (3, FMDX, 7, FMDY, N_("follow &Links"), &ctx->follow_links),
898 /* 7 - OFFSET */
899 QUICK_INPUT (3, FMDX, 6, FMDY, "", 58, 0, "input2", &dest_dir),
900 /* 8 - OFFSET */
901 QUICK_LABEL (3, FMDX, 5, FMDY, N_("to:")),
902 /* 9 - OFFSET */
903 QUICK_CHECKBOX (37, FMDX, 4, FMDY, N_("&Using shell patterns"), &source_easy_patterns),
904 /* 10 - OFFSET */
905 QUICK_INPUT (3, FMDX, 3, FMDY, easy_patterns ? "*" : "^\\(.*\\)$", 58, 0, "input-def", &source_mask),
906 /* 11 - OFFSET */
907 QUICK_LABEL (3, FMDX, 2, FMDY, fmd_buf),
908 QUICK_END
911 g_return_val_if_fail (ctx != NULL, NULL);
913 #ifdef ENABLE_NLS
914 /* buttons */
915 for (i = 0; i <= 2 - OFFSET; i++)
916 fmd_widgets[i].u.button.text = _(fmd_widgets[i].u.button.text);
918 /* checkboxes */
919 for (i = 3 - OFFSET; i <= 9 - OFFSET; i++)
920 if (i != 7 - OFFSET)
921 fmd_widgets[i].u.checkbox.text = _(fmd_widgets[i].u.checkbox.text);
922 #endif /* !ENABLE_NLS */
924 fmd_xlen = max (FMDX, (size_t) COLS * 2/3);
926 len = str_term_width1 (fmd_widgets[6 - OFFSET].u.checkbox.text)
927 + str_term_width1 (fmd_widgets[4 - OFFSET].u.checkbox.text) + 15;
928 fmd_xlen = max (fmd_xlen, len);
930 len = str_term_width1 (fmd_widgets[5 - OFFSET].u.checkbox.text)
931 + str_term_width1 (fmd_widgets[3 - OFFSET].u.checkbox.text) + 15;
932 fmd_xlen = max (fmd_xlen, len);
934 /* buttons */
935 b2_len = str_term_width1 (fmd_widgets[2 - OFFSET].u.button.text) + 6 + gap; /* OK */
936 #ifdef WITH_BACKGROUND
937 b1_len = str_term_width1 (fmd_widgets[1].u.button.text) + 4 + gap; /* Background */
938 #endif
939 b0_len = str_term_width1 (fmd_widgets[0].u.button.text) + 4; /* Cancel */
940 len = b0_len + b1_len + b2_len;
941 fmd_xlen = min (max (fmd_xlen, len + 6), (size_t) COLS);
943 if (only_one) {
944 int flen;
946 flen = str_term_width1 (format);
947 i = fmd_xlen - flen - 4; /* FIXME */
948 g_snprintf (fmd_buf, sizeof (fmd_buf),
949 format, str_trunc ((const char *) text, i));
950 } else {
951 g_snprintf (fmd_buf, sizeof (fmd_buf), format, *(const int *) text);
952 fmd_xlen = max (fmd_xlen, (size_t) str_term_width1 (fmd_buf) + 6);
955 for (i = sizeof (fmd_widgets) / sizeof (fmd_widgets[0]); i > 0; )
956 fmd_widgets[--i].x_divisions = fmd_xlen;
958 i = (fmd_xlen - len)/2;
959 /* OK button */
960 fmd_widgets[2 - OFFSET].relative_x = i;
961 i += b2_len;
962 #ifdef WITH_BACKGROUND
963 /* Background button */
964 fmd_widgets[1].relative_x = i;
965 i += b1_len;
966 #endif
967 /* Cancel button */
968 fmd_widgets[0].relative_x = i;
970 #define chkbox_xpos(i) \
971 fmd_widgets [i].relative_x = fmd_xlen - str_term_width1 (fmd_widgets [i].u.checkbox.text) - 6
972 chkbox_xpos (3 - OFFSET);
973 chkbox_xpos (4 - OFFSET);
974 chkbox_xpos (9 - OFFSET);
975 #undef chkbox_xpos
977 /* inputs */
978 fmd_widgets[ 7 - OFFSET].u.input.len =
979 fmd_widgets[10 - OFFSET].u.input.len = fmd_xlen - 6;
981 /* unselect checkbox if target filesystem don't support attributes */
982 ctx->op_preserve = filegui__check_attrs_on_fs (def_text);
984 /* filter out a possible password from def_text */
985 tmp = strip_password (g_strdup (def_text), 1);
986 if (source_easy_patterns)
987 def_text_secure = strutils_glob_escape (tmp);
988 else
989 def_text_secure = strutils_regex_escape (tmp);
990 g_free (tmp);
992 /* destination */
993 fmd_widgets[7 - OFFSET].u.input.text = def_text_secure;
995 ctx->stable_symlinks = 0;
996 *do_background = FALSE;
999 struct stat buf;
1001 QuickDialog Quick_input =
1003 fmd_xlen, FMDY, -1, -1, op_names [operation],
1004 "[Mask Copy/Rename]", fmd_widgets, TRUE
1007 ask_file_mask:
1008 val = quick_dialog_skip (&Quick_input, 4);
1010 if (val == B_CANCEL) {
1011 g_free (def_text_secure);
1012 return NULL;
1015 if (ctx->follow_links)
1016 ctx->stat_func = mc_stat;
1017 else
1018 ctx->stat_func = mc_lstat;
1020 if (ctx->op_preserve) {
1021 ctx->preserve = 1;
1022 ctx->umask_kill = 0777777;
1023 ctx->preserve_uidgid = (geteuid () == 0) ? 1 : 0;
1024 } else {
1025 int i2;
1026 ctx->preserve = ctx->preserve_uidgid = 0;
1027 i2 = umask (0);
1028 umask (i2);
1029 ctx->umask_kill = i2 ^ 0777777;
1032 if (!dest_dir || !*dest_dir) {
1033 g_free (def_text_secure);
1034 g_free (source_mask);
1035 return dest_dir;
1038 ctx->search_handle = mc_search_new(source_mask,-1);
1040 if (ctx->search_handle == NULL) {
1041 message (D_ERROR, MSG_ERROR, _("Invalid source pattern `%s'"),
1042 source_mask);
1043 g_free (dest_dir);
1044 g_free (source_mask);
1045 goto ask_file_mask;
1048 g_free (def_text_secure);
1049 g_free (source_mask);
1051 ctx->search_handle->is_case_sentitive = TRUE;
1052 if (source_easy_patterns)
1053 ctx->search_handle->search_type = MC_SEARCH_T_GLOB;
1054 else
1055 ctx->search_handle->search_type = MC_SEARCH_T_REGEX;
1057 tmp = dest_dir;
1058 dest_dir = tilde_expand (tmp);
1059 g_free (tmp);
1061 ctx->dest_mask = strrchr (dest_dir, PATH_SEP);
1062 if (ctx->dest_mask == NULL)
1063 ctx->dest_mask = dest_dir;
1064 else
1065 ctx->dest_mask++;
1066 orig_mask = ctx->dest_mask;
1067 if (!*ctx->dest_mask
1068 || (!ctx->dive_into_subdirs && !is_wildcarded (ctx->dest_mask)
1069 && (!only_one
1070 || (!mc_stat (dest_dir, &buf) && S_ISDIR (buf.st_mode))))
1071 || (ctx->dive_into_subdirs
1072 && ((!only_one && !is_wildcarded (ctx->dest_mask))
1073 || (only_one && !mc_stat (dest_dir, &buf)
1074 && S_ISDIR (buf.st_mode)))))
1075 ctx->dest_mask = g_strdup ("\\0");
1076 else {
1077 ctx->dest_mask = g_strdup (ctx->dest_mask);
1078 *orig_mask = '\0';
1080 if (!*dest_dir) {
1081 g_free (dest_dir);
1082 dest_dir = g_strdup ("./");
1084 if (val == B_USER)
1085 *do_background = TRUE;
1088 return dest_dir;