x86_64: Cleanup of code for master
[midnight-commander.git] / src / filegui.c
blobd997544e934e2249e03e59a0df9cec64f1c4f49b
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/mcconfig.h"
83 #include "lib/search.h"
84 #include "lib/vfs/mc-vfs/vfs.h"
85 #include "lib/strescape.h"
86 #include "lib/strutil.h"
88 #include "setup.h" /* verbose */
89 #include "dialog.h" /* do_refresh() */
90 #include "widget.h" /* WLabel */
91 #include "main-widgets.h"
92 #include "main.h" /* the_hint */
93 #include "wtools.h" /* QuickDialog */
94 #include "panel.h" /* current_panel */
95 #include "fileopctx.h" /* FILE_CONT */
96 #include "filegui.h"
98 /* }}} */
99 typedef enum {
100 MSDOS_SUPER_MAGIC = 0x4d44,
101 NTFS_SB_MAGIC = 0x5346544e,
102 NTFS_3G_MAGIC = 0x65735546,
103 PROC_SUPER_MAGIC = 0x9fa0,
104 SMB_SUPER_MAGIC = 0x517B,
105 NCP_SUPER_MAGIC = 0x564c,
106 USBDEVICE_SUPER_MAGIC = 0x9fa2
107 } filegui_nonattrs_fs_t;
109 /* Hack: the vfs code should not rely on this */
110 #define WITH_FULL_PATHS 1
112 /* Used for button result values */
113 typedef enum {
114 REPLACE_YES = B_USER,
115 REPLACE_NO,
116 REPLACE_APPEND,
117 REPLACE_ALWAYS,
118 REPLACE_UPDATE,
119 REPLACE_NEVER,
120 REPLACE_ABORT,
121 REPLACE_SIZE,
122 REPLACE_REGET
123 } replace_action_t;
125 /* This structure describes the UI and internal data required by a file
126 * operation context.
128 typedef struct {
129 /* ETA and bps */
130 gboolean showing_eta;
131 gboolean showing_bps;
133 /* Dialog and widgets for the operation progress window */
134 Dlg_head *op_dlg;
135 WLabel *file_string[2];
136 WLabel *file_label[2];
137 WGauge *progress_file_gauge;
138 WLabel *progress_file_label;
140 WGauge *progress_total_gauge;
142 WLabel *total_files_processed_label;
143 WLabel *time_label;
144 WLabel *total_bytes_label;
146 /* Query replace dialog */
147 Dlg_head *replace_dlg;
148 const char *replace_filename;
149 replace_action_t replace_result;
151 struct stat *s_stat, *d_stat;
152 } FileOpContextUI;
155 /* Used to save the hint line */
156 static int last_hint_line;
158 /* File operate window sizes */
159 #define WX 58
160 #define WY 11
161 #define FCOPY_LABEL_X 3
163 static gboolean
164 filegui__check_attrs_on_fs (const char *fs_path)
166 #ifdef STATFS
167 STRUCT_STATFS stfs;
169 if (!setup_copymove_persistent_attr)
170 return FALSE;
172 if (STATFS (fs_path, &stfs) != 0)
173 return TRUE;
175 # ifdef __linux__
176 switch ((filegui_nonattrs_fs_t) stfs.f_type)
178 case MSDOS_SUPER_MAGIC:
179 case NTFS_SB_MAGIC:
180 case NTFS_3G_MAGIC:
181 case PROC_SUPER_MAGIC:
182 case SMB_SUPER_MAGIC:
183 case NCP_SUPER_MAGIC:
184 case USBDEVICE_SUPER_MAGIC:
185 return FALSE;
187 # elif defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) \
188 || defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
189 if (!strcmp(stfs.f_fstypename, "msdos")
190 || !strcmp(stfs.f_fstypename, "msdosfs")
191 || !strcmp(stfs.f_fstypename, "ntfs")
192 || !strcmp(stfs.f_fstypename, "procfs")
193 || !strcmp(stfs.f_fstypename, "smbfs")
194 || strstr(stfs.f_fstypename, "fusefs"))
195 return FALSE;
196 # elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
197 if (!strcmp(stfs.f_basetype, "pcfs")
198 || !strcmp(stfs.f_basetype, "ntfs")
199 || !strcmp(stfs.f_basetype, "proc")
200 || !strcmp(stfs.f_basetype, "smbfs")
201 || !strcmp(stfs.f_basetype, "fuse"))
202 return FALSE;
203 # endif
204 #endif /* STATFS */
206 return TRUE;
209 FileProgressStatus
210 check_progress_buttons (FileOpContext *ctx)
212 int c;
213 Gpm_Event event;
214 FileOpContextUI *ui;
216 if (ctx->ui == NULL)
217 return FILE_CONT;
219 ui = ctx->ui;
221 event.x = -1; /* Don't show the GPM cursor */
222 c = tty_get_event (&event, FALSE, FALSE);
223 if (c == EV_NONE)
224 return FILE_CONT;
226 /* Reinitialize to avoid old values after events other than
227 selecting a button */
228 ui->op_dlg->ret_value = FILE_CONT;
230 dlg_process_event (ui->op_dlg, c, &event);
231 switch (ui->op_dlg->ret_value) {
232 case FILE_SKIP:
233 return FILE_SKIP;
234 case B_CANCEL:
235 case FILE_ABORT:
236 return FILE_ABORT;
237 default:
238 return FILE_CONT;
242 /* {{{ File progress display routines */
244 void
245 file_op_context_create_ui_without_init (FileOpContext *ctx, gboolean with_eta, filegui_dialog_type_t dialog_type)
247 FileOpContextUI *ui;
248 int minus, total_reserve=0;
249 const char *abort_button_label = N_("&Abort");
250 const char *skip_button_label = N_("&Skip");
251 int abort_button_width, skip_button_width, buttons_width;
252 int dlg_width;
254 g_return_if_fail (ctx != NULL);
255 g_return_if_fail (ctx->ui == NULL);
257 #ifdef ENABLE_NLS
258 abort_button_label = _(abort_button_label);
259 skip_button_label = _(skip_button_label);
260 #endif
262 abort_button_width = str_term_width1 (abort_button_label) + 3;
263 skip_button_width = str_term_width1 (skip_button_label) + 3;
264 buttons_width = abort_button_width + skip_button_width + 2;
266 dlg_width = max (WX, buttons_width + 6);
268 ui = g_new0 (FileOpContextUI, 1);
269 ctx->ui = ui;
271 ctx->dialog_type = dialog_type;
272 minus = verbose ? 0 : 3;
274 switch (dialog_type) {
275 case FILEGUI_DIALOG_ONE_ITEM:
276 total_reserve = 0;
277 break;
278 case FILEGUI_DIALOG_MULTI_ITEM:
279 total_reserve = 5;
280 break;
281 case FILEGUI_DIALOG_DELETE_ITEM:
282 total_reserve = -5;
283 break;
286 ctx->recursive_result = RECURSIVE_YES;
288 ui->replace_result = REPLACE_YES;
289 ui->showing_eta = with_eta;
290 ui->showing_bps = with_eta;
292 ui->op_dlg =
293 create_dlg (0, 0, WY - minus + 1 + total_reserve, dlg_width,
294 dialog_colors, NULL, NULL, op_names[ctx->operation],
295 DLG_CENTER | DLG_REVERSE);
297 last_hint_line = the_hint->widget.y;
298 if ((ui->op_dlg->y + ui->op_dlg->lines) > last_hint_line)
299 the_hint->widget.y = ui->op_dlg->y + ui->op_dlg->lines + 1;
301 add_widget (ui->op_dlg,
302 button_new (WY - minus - 2 + total_reserve,
303 dlg_width/2 + 1, FILE_ABORT,
304 NORMAL_BUTTON, abort_button_label, NULL));
305 add_widget (ui->op_dlg,
306 button_new (WY - minus - 2 + total_reserve,
307 dlg_width/2 - 1 - skip_button_width, FILE_SKIP,
308 NORMAL_BUTTON, skip_button_label, NULL));
311 if (dialog_type == FILEGUI_DIALOG_MULTI_ITEM) {
312 add_widget (ui->op_dlg, hline_new (8, 1, dlg_width - 2));
314 add_widget (ui->op_dlg, ui->total_bytes_label =
315 label_new (8, FCOPY_LABEL_X + 15, ""));
317 add_widget (ui->op_dlg, ui->progress_total_gauge =
318 gauge_new (9, FCOPY_LABEL_X + 3, 0, 100, 0));
320 add_widget (ui->op_dlg, ui->total_files_processed_label =
321 label_new (11, FCOPY_LABEL_X, ""));
323 add_widget (ui->op_dlg, ui->time_label =
324 label_new (12, FCOPY_LABEL_X, ""));
327 add_widget (ui->op_dlg, ui->progress_file_label =
328 label_new (7, FCOPY_LABEL_X, ""));
330 add_widget (ui->op_dlg, ui->progress_file_gauge =
331 gauge_new (6, FCOPY_LABEL_X + 3, 0, 100, 0));
333 add_widget (ui->op_dlg, ui->file_string[1] =
334 label_new (5, FCOPY_LABEL_X, ""));
336 add_widget (ui->op_dlg, ui->file_label[1] =
337 label_new (4, FCOPY_LABEL_X, ""));
338 add_widget (ui->op_dlg, ui->file_string[0] =
339 label_new (3, FCOPY_LABEL_X, ""));
340 add_widget (ui->op_dlg, ui->file_label[0] =
341 label_new (2, FCOPY_LABEL_X, ""));
343 if ((right_panel == current_panel)
344 && !mc_config_get_bool (mc_main_config,"Layout",
345 "classic_progressbar", TRUE)) {
346 ui->progress_file_gauge->from_left_to_right = FALSE;
347 if (dialog_type == FILEGUI_DIALOG_MULTI_ITEM)
348 ui->progress_total_gauge->from_left_to_right = FALSE;
352 void
353 file_op_context_create_ui (FileOpContext *ctx, gboolean with_eta, filegui_dialog_type_t dialog_type)
355 FileOpContextUI *ui;
357 g_return_if_fail (ctx != NULL);
358 g_return_if_fail (ctx->ui == NULL);
360 file_op_context_create_ui_without_init (ctx, with_eta, dialog_type);
361 ui = ctx->ui;
363 /* We will manage the dialog without any help, that's why
364 we have to call init_dlg */
365 init_dlg (ui->op_dlg);
366 ui->op_dlg->running = 1;
369 void
370 file_op_context_destroy_ui (FileOpContext *ctx)
372 FileOpContextUI *ui;
374 g_return_if_fail (ctx != NULL);
376 if (ctx->ui) {
377 ui = ctx->ui;
379 dlg_run_done (ui->op_dlg);
380 destroy_dlg (ui->op_dlg);
381 g_free (ui);
384 the_hint->widget.y = last_hint_line;
386 ctx->ui = NULL;
389 static void
390 file_frmt_time (char *buffer, double eta_secs)
392 int eta_hours, eta_mins, eta_s;
393 eta_hours = eta_secs / (60 * 60);
394 eta_mins = (eta_secs - (eta_hours * 60 * 60)) / 60;
395 eta_s = eta_secs - (eta_hours * 60 * 60 + eta_mins * 60);
396 g_snprintf (buffer, BUF_TINY, _("%d:%02d.%02d"), eta_hours, eta_mins, eta_s);
399 static void
400 file_eta_prepare_for_show (char *buffer, double eta_secs, gboolean always_show)
402 char _fmt_buff[BUF_TINY];
403 if (eta_secs <= 0.5 && !always_show) {
404 *buffer = '\0';
405 return;
407 if (eta_secs <= 0.5)
408 eta_secs = 1;
409 file_frmt_time (_fmt_buff, eta_secs);
410 g_snprintf (buffer, BUF_TINY, _("ETA %s"), _fmt_buff);
413 static void
414 file_bps_prepare_for_show (char *buffer, long bps)
416 if (bps > 1024 * 1024) {
417 g_snprintf (buffer, BUF_TINY, _("%.2f MB/s"),
418 bps / (1024 * 1024.0));
419 } else if (bps > 1024) {
420 g_snprintf (buffer, BUF_TINY, _("%.2f KB/s"),
421 bps / 1024.0);
422 } else if (bps > 1) {
423 g_snprintf (buffer, BUF_TINY, _("%ld B/s"),
424 bps);
425 } else
426 *buffer = 0;
430 show progressbar for file
432 void
433 file_progress_show (FileOpContext *ctx, off_t done, off_t total,
434 const char *stalled_msg, gboolean force_update)
436 FileOpContextUI *ui;
437 char buffer[BUF_TINY];
438 char buffer2[BUF_TINY];
439 char buffer3[BUF_TINY];
441 g_return_if_fail (ctx != NULL);
443 if (ctx->ui == NULL)
444 return;
446 ui = ctx->ui;
448 if (!verbose)
449 return;
451 if (total == 0) {
452 gauge_show (ui->progress_file_gauge, 0);
453 return;
456 gauge_set_value (ui->progress_file_gauge, 1024, (int) (1024 * done / total));
457 gauge_show (ui->progress_file_gauge, 1);
459 if (!force_update)
460 return;
462 if (ui->showing_eta && ctx->eta_secs > 0.5) {
463 file_eta_prepare_for_show (buffer2, ctx->eta_secs, FALSE);
464 file_bps_prepare_for_show (buffer3, ctx->bps);
465 g_snprintf (buffer, BUF_TINY, "%s (%s) %s", buffer2, buffer3, stalled_msg);
466 } else {
467 g_snprintf (buffer, BUF_TINY, "%s",stalled_msg);
470 label_set_text (ui->progress_file_label, buffer);
473 void
474 file_progress_show_count (FileOpContext *ctx, off_t done, off_t total)
476 char buffer[BUF_TINY];
477 FileOpContextUI *ui;
479 g_return_if_fail (ctx != NULL);
481 if (ctx->dialog_type != FILEGUI_DIALOG_MULTI_ITEM || ctx->ui == NULL)
482 return;
484 ui = ctx->ui;
486 if (!verbose)
487 return;
489 g_snprintf (buffer, BUF_TINY, _("Files processed: %llu of %llu"),
490 (unsigned long long) done,
491 (unsigned long long) total);
493 label_set_text (ui->total_files_processed_label, buffer);
496 void
497 file_progress_show_total (FileOpTotalContext *tctx, FileOpContext *ctx, double copyed_bytes,
498 gboolean need_show_total_summary)
500 char buffer[BUF_TINY];
501 char buffer2[BUF_TINY];
502 char buffer3[BUF_TINY];
503 char buffer4[BUF_TINY];
504 struct timeval tv_current;
505 FileOpContextUI *ui;
507 if (ctx->dialog_type != FILEGUI_DIALOG_MULTI_ITEM || ctx->ui == NULL)
508 return;
510 ui = ctx->ui;
512 if (ctx->progress_bytes > 0 ){
513 gauge_set_value (ui->progress_total_gauge, 1024, (int) (1024 * copyed_bytes/ ctx->progress_bytes));
514 gauge_show (ui->progress_total_gauge, 1);
515 } else
516 gauge_show (ui->progress_total_gauge, 0);
519 if (!need_show_total_summary && tctx->bps == 0)
520 return;
522 gettimeofday (&tv_current, NULL);
523 file_frmt_time (buffer2, tv_current.tv_sec - tctx->transfer_start.tv_sec);
524 file_eta_prepare_for_show (buffer3, tctx->eta_secs, TRUE);
525 file_bps_prepare_for_show (buffer4, (long) tctx->bps);
529 g_snprintf (buffer, BUF_TINY, _("Time: %s %s (%s)"), buffer2, buffer3, buffer4);
530 label_set_text (ui->time_label, buffer);
532 size_trunc_len (buffer2, 5, tctx->copyed_bytes, 0);
533 size_trunc_len (buffer3, 5, ctx->progress_bytes, 0);
535 g_snprintf (buffer, BUF_TINY, _(" Total: %s of %s "), buffer2, buffer3);
537 label_set_text (ui->total_bytes_label, buffer);
541 /* }}} */
543 #define truncFileString(ui, s) str_trunc (s, 52)
544 #define truncFileStringSecure(ui, s) path_trunc (s, 52)
546 void
547 file_progress_show_source (FileOpContext *ctx, const char *s)
549 FileOpContextUI *ui;
551 g_return_if_fail (ctx != NULL);
553 if (ctx->ui == NULL)
554 return;
556 ui = ctx->ui;
558 if (s != NULL) {
559 #ifdef WITH_FULL_PATHS
560 int i = strlen (current_panel->cwd);
562 /* We remove the full path we have added before */
563 if (!strncmp (s, current_panel->cwd, i)) {
564 if (s[i] == PATH_SEP)
565 s += i + 1;
567 #endif /* WITH_FULL_PATHS */
569 label_set_text (ui->file_label[0], _("Source"));
570 label_set_text (ui->file_string[0], truncFileString (ui, s));
571 } else {
572 label_set_text (ui->file_label[0], "");
573 label_set_text (ui->file_string[0], "");
577 void
578 file_progress_show_target (FileOpContext *ctx, const char *s)
580 FileOpContextUI *ui;
582 g_return_if_fail (ctx != NULL);
584 if (ctx->ui == NULL)
585 return;
587 ui = ctx->ui;
589 if (s != NULL) {
590 label_set_text (ui->file_label[1], _("Target"));
591 label_set_text (ui->file_string[1], truncFileStringSecure (ui, s));
592 } else {
593 label_set_text (ui->file_label[1], "");
594 label_set_text (ui->file_string[1], "");
598 void
599 file_progress_show_deleting (FileOpContext *ctx, const char *s)
601 FileOpContextUI *ui;
603 g_return_if_fail (ctx != NULL);
605 if (ctx->ui == NULL)
606 return;
608 ui = ctx->ui;
609 label_set_text (ui->file_label[0], _("Deleting"));
610 label_set_text (ui->file_label[0], truncFileStringSecure (ui, s));
614 * FIXME: probably it is better to replace this with quick dialog machinery,
615 * but actually I'm not familiar with it and have not much time :(
616 * alex
618 static replace_action_t
619 overwrite_query_dialog (FileOpContext *ctx, enum OperationMode mode)
621 #define ADD_RD_BUTTON(i)\
622 add_widget (ui->replace_dlg,\
623 button_new (rd_widgets [i].ypos, rd_widgets [i].xpos, rd_widgets [i].value,\
624 NORMAL_BUTTON, rd_widgets [i].text, 0))
626 #define ADD_RD_LABEL(i, p1, p2)\
627 g_snprintf (buffer, sizeof (buffer), rd_widgets [i].text, p1, p2);\
628 add_widget (ui->replace_dlg,\
629 label_new (rd_widgets [i].ypos, rd_widgets [i].xpos, buffer))
631 /* dialog sizes */
632 const int rd_ylen = 17;
633 int rd_xlen = 60;
635 struct {
636 const char *text;
637 int ypos, xpos;
638 int value; /* 0 for labels */
639 } rd_widgets[] = {
640 /* 0 */ { N_("Target file already exists!"), 3, 4, 0 },
641 /* 1 */ { "%s", 4, 4, 0 },
642 #if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined _LARGE_FILES && _LARGE_FILES)
643 /* 2 */ { N_("Source date: %s, size %llu"), 6, 4, 0 },
644 /* 3 */ { N_("Target date: %s, size %llu"), 7, 4, 0 },
645 #else
646 /* 2 */ { N_("Source date: %s, size %u"), 6, 4, 0 },
647 /* 3 */ { N_("Target date: %s, size %u"), 7, 4, 0 },
648 #endif
649 /* 4 */ { N_("&Abort"), 14, 25, REPLACE_ABORT },
650 /* 5 */ { N_("If &size differs"), 12, 28, REPLACE_SIZE },
651 /* 6 */ { N_("Non&e"), 11, 47, REPLACE_NEVER },
652 /* 7 */ { N_("&Update"), 11, 36, REPLACE_UPDATE },
653 /* 8 */ { N_("A&ll"), 11, 28, REPLACE_ALWAYS },
654 /* 9 */ { N_("Overwrite all targets?"), 11, 4, 0 },
655 /* 10 */ { N_("&Reget"), 10, 28, REPLACE_REGET },
656 /* 11 */ { N_("A&ppend"), 9, 45, REPLACE_APPEND },
657 /* 12 */ { N_("&No"), 9, 37, REPLACE_NO },
658 /* 13 */ { N_("&Yes"), 9, 28, REPLACE_YES },
659 /* 14 */ { N_("Overwrite this target?"), 9, 4, 0 }
662 const int num = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
663 int *widgets_len;
665 FileOpContextUI *ui = ctx->ui;
667 char buffer[BUF_SMALL];
668 const char *title;
669 const char *stripped_name = strip_home_and_password (ui->replace_filename);
670 int stripped_name_len;
672 int result;
674 widgets_len = g_new0 (int, num);
676 if (mode == Foreground)
677 title = _(" File exists ");
678 else
679 title = _(" Background process: File exists ");
681 stripped_name_len = str_term_width1 (stripped_name);
684 int i, l1, l2, l, row;
686 for (i = 0; i < num; i++) {
687 #ifdef ENABLE_NLS
688 if (i != 1) /* skip filename */
689 rd_widgets[i].text = _(rd_widgets[i].text);
690 #endif /* ENABLE_NLS */
691 widgets_len [i] = str_term_width1 (rd_widgets[i].text);
695 * longest of "Overwrite..." labels
696 * (assume "Target date..." are short enough)
698 l1 = max (widgets_len[9], widgets_len[14]);
700 /* longest of button rows */
701 i = num;
702 for (row = l = l2 = 0; i--;)
703 if (rd_widgets[i].value != 0) {
704 if (row != rd_widgets[i].ypos) {
705 row = rd_widgets[i].ypos;
706 l2 = max (l2, l);
707 l = 0;
709 l += widgets_len[i] + 4;
712 l2 = max (l2, l); /* last row */
713 rd_xlen = max (rd_xlen, l1 + l2 + 8);
714 rd_xlen = max (rd_xlen, str_term_width1 (title) + 2);
715 rd_xlen = max (rd_xlen, min (COLS, stripped_name_len + 8));
717 /* Now place widgets */
718 l1 += 5; /* start of first button in the row */
719 i = num;
720 for (l = l1, row = 0; --i > 1;)
721 if (rd_widgets[i].value != 0) {
722 if (row != rd_widgets[i].ypos) {
723 row = rd_widgets[i].ypos;
724 l = l1;
726 rd_widgets[i].xpos = l;
727 l += widgets_len[i] + 4;
730 /* Abort button is centered */
731 rd_widgets[4].xpos = (rd_xlen - widgets_len[4] - 3) / 2;
734 /* FIXME - missing help node */
735 ui->replace_dlg =
736 create_dlg (0, 0, rd_ylen, rd_xlen, alarm_colors, NULL, "[Replace]",
737 title, DLG_CENTER | DLG_REVERSE);
739 /* prompt -- centered */
740 add_widget (ui->replace_dlg,
741 label_new (rd_widgets [0].ypos,
742 (rd_xlen - widgets_len [0]) / 2,
743 rd_widgets [0].text));
744 /* file name -- centered */
745 stripped_name = str_trunc (stripped_name, rd_xlen - 8);
746 stripped_name_len = str_term_width1 (stripped_name);
747 add_widget (ui->replace_dlg,
748 label_new (rd_widgets [1].ypos,
749 (rd_xlen - stripped_name_len) / 2,
750 stripped_name));
752 /* source date */
753 ADD_RD_LABEL (2, file_date (ui->s_stat->st_mtime),
754 (off_t) ui->s_stat->st_size);
755 /* destination date */
756 ADD_RD_LABEL (3, file_date (ui->d_stat->st_mtime),
757 (off_t) ui->d_stat->st_size);
759 ADD_RD_BUTTON (4); /* Abort */
760 ADD_RD_BUTTON (5); /* If size differs */
761 ADD_RD_BUTTON (6); /* None */
762 ADD_RD_BUTTON (7); /* Update */
763 ADD_RD_BUTTON (8); /* All" */
764 ADD_RD_LABEL (9, 0, 0); /* Overwrite all targets? */
766 /* "this target..." widgets */
767 if (!S_ISDIR (ui->d_stat->st_mode)) {
768 if ((ctx->operation == OP_COPY) && (ui->d_stat->st_size != 0)
769 && (ui->s_stat->st_size > ui->d_stat->st_size))
770 ADD_RD_BUTTON (10); /* Reget */
772 ADD_RD_BUTTON (11); /* Append */
774 ADD_RD_BUTTON (12); /* No */
775 ADD_RD_BUTTON (13); /* Yes */
776 ADD_RD_LABEL (14, 0, 0); /* Overwrite this target? */
778 result = run_dlg (ui->replace_dlg);
779 destroy_dlg (ui->replace_dlg);
781 g_free (widgets_len);
783 return (result == B_CANCEL) ? REPLACE_ABORT : (replace_action_t) result;
784 #undef ADD_RD_LABEL
785 #undef ADD_RD_BUTTON
788 FileProgressStatus
789 file_progress_real_query_replace (FileOpContext *ctx,
790 enum OperationMode mode, const char *destname,
791 struct stat *_s_stat,
792 struct stat *_d_stat)
794 FileOpContextUI *ui;
796 g_return_val_if_fail (ctx != NULL, FILE_CONT);
797 g_return_val_if_fail (ctx->ui != NULL, FILE_CONT);
799 ui = ctx->ui;
801 if (ui->replace_result < REPLACE_ALWAYS) {
802 ui->replace_filename = destname;
803 ui->s_stat = _s_stat;
804 ui->d_stat = _d_stat;
805 ui->replace_result = overwrite_query_dialog (ctx, mode);
808 switch (ui->replace_result) {
809 case REPLACE_UPDATE:
810 do_refresh ();
811 if (_s_stat->st_mtime > _d_stat->st_mtime)
812 return FILE_CONT;
813 else
814 return FILE_SKIP;
816 case REPLACE_SIZE:
817 do_refresh ();
818 if (_s_stat->st_size == _d_stat->st_size)
819 return FILE_SKIP;
820 else
821 return FILE_CONT;
823 case REPLACE_REGET:
824 /* Careful: we fall through and set do_append */
825 ctx->do_reget = _d_stat->st_size;
827 case REPLACE_APPEND:
828 ctx->do_append = TRUE;
830 case REPLACE_YES:
831 case REPLACE_ALWAYS:
832 do_refresh ();
833 return FILE_CONT;
834 case REPLACE_NO:
835 case REPLACE_NEVER:
836 do_refresh ();
837 return FILE_SKIP;
838 case REPLACE_ABORT:
839 default:
840 return FILE_ABORT;
844 static gboolean
845 is_wildcarded (char *p)
847 for (; *p; p++) {
848 if (*p == '*')
849 return TRUE;
850 if (*p == '\\' && p[1] >= '1' && p[1] <= '9')
851 return TRUE;
853 return FALSE;
856 char *
857 file_mask_dialog (FileOpContext *ctx, FileOperation operation,
858 gboolean only_one,
859 const char *format, const void *text,
860 const char *def_text, gboolean *do_background)
862 const size_t FMDY = 13;
863 const size_t FMDX = 68;
864 size_t fmd_xlen;
866 /* buttons */
867 const size_t gap = 1;
868 size_t b0_len, b2_len;
869 size_t b1_len = 0;
871 int source_easy_patterns = easy_patterns;
872 size_t i, len;
873 char fmd_buf [BUF_MEDIUM];
874 char *source_mask, *orig_mask, *dest_dir, *tmp;
875 char *def_text_secure;
876 int val;
878 QuickWidget fmd_widgets[] =
880 /* 0 */ QUICK_BUTTON (42, 64, 10, FMDY, N_("&Cancel"), B_CANCEL, NULL),
881 #ifdef WITH_BACKGROUND
882 /* 1 */ QUICK_BUTTON (25, 64, 10, FMDY, N_("&Background"), B_USER, NULL),
883 #define OFFSET 0
884 #else
885 #define OFFSET 1
886 #endif /* WITH_BACKGROUND */
887 /* 2 - OFFSET */
888 QUICK_BUTTON (14, FMDX, 10, FMDY, N_("&OK"), B_ENTER, NULL),
889 /* 3 - OFFSET */
890 QUICK_CHECKBOX (42, FMDX, 8, FMDY, N_("&Stable Symlinks"), &ctx->stable_symlinks),
891 /* 4 - OFFSET */
892 QUICK_CHECKBOX (31, FMDX, 7, FMDY, N_("di&Ve into subdir if exists"), &ctx->dive_into_subdirs),
893 /* 5 - OFFSET */
894 QUICK_CHECKBOX (3, FMDX, 8, FMDY, N_("preserve &Attributes"), &ctx->op_preserve),
895 /* 6 - OFFSET */
896 QUICK_CHECKBOX (3, FMDX, 7, FMDY, N_("follow &Links"), &ctx->follow_links),
897 /* 7 - OFFSET */
898 QUICK_INPUT (3, FMDX, 6, FMDY, "", 58, 0, "input2", &dest_dir),
899 /* 8 - OFFSET */
900 QUICK_LABEL (3, FMDX, 5, FMDY, N_("to:")),
901 /* 9 - OFFSET */
902 QUICK_CHECKBOX (37, FMDX, 4, FMDY, N_("&Using shell patterns"), &source_easy_patterns),
903 /* 10 - OFFSET */
904 QUICK_INPUT (3, FMDX, 3, FMDY, easy_patterns ? "*" : "^\\(.*\\)$", 58, 0, "input-def", &source_mask),
905 /* 11 - OFFSET */
906 QUICK_LABEL (3, FMDX, 2, FMDY, fmd_buf),
907 QUICK_END
910 g_return_val_if_fail (ctx != NULL, NULL);
912 #ifdef ENABLE_NLS
913 /* buttons */
914 for (i = 0; i <= 2 - OFFSET; i++)
915 fmd_widgets[i].u.button.text = _(fmd_widgets[i].u.button.text);
917 /* checkboxes */
918 for (i = 3 - OFFSET; i <= 9 - OFFSET; i++)
919 if (i != 7 - OFFSET)
920 fmd_widgets[i].u.checkbox.text = _(fmd_widgets[i].u.checkbox.text);
921 #endif /* !ENABLE_NLS */
923 fmd_xlen = max (FMDX, (size_t) COLS * 2/3);
925 len = str_term_width1 (fmd_widgets[6 - OFFSET].u.checkbox.text)
926 + str_term_width1 (fmd_widgets[4 - OFFSET].u.checkbox.text) + 15;
927 fmd_xlen = max (fmd_xlen, len);
929 len = str_term_width1 (fmd_widgets[5 - OFFSET].u.checkbox.text)
930 + str_term_width1 (fmd_widgets[3 - OFFSET].u.checkbox.text) + 15;
931 fmd_xlen = max (fmd_xlen, len);
933 /* buttons */
934 b2_len = str_term_width1 (fmd_widgets[2 - OFFSET].u.button.text) + 6 + gap; /* OK */
935 #ifdef WITH_BACKGROUND
936 b1_len = str_term_width1 (fmd_widgets[1].u.button.text) + 4 + gap; /* Background */
937 #endif
938 b0_len = str_term_width1 (fmd_widgets[0].u.button.text) + 4; /* Cancel */
939 len = b0_len + b1_len + b2_len;
940 fmd_xlen = min (max (fmd_xlen, len + 6), (size_t) COLS);
942 if (only_one) {
943 int flen;
945 flen = str_term_width1 (format);
946 i = fmd_xlen - flen - 4; /* FIXME */
947 g_snprintf (fmd_buf, sizeof (fmd_buf),
948 format, str_trunc ((const char *) text, i));
949 } else {
950 g_snprintf (fmd_buf, sizeof (fmd_buf), format, *(const int *) text);
951 fmd_xlen = max (fmd_xlen, (size_t) str_term_width1 (fmd_buf) + 6);
954 for (i = sizeof (fmd_widgets) / sizeof (fmd_widgets[0]); i > 0; )
955 fmd_widgets[--i].x_divisions = fmd_xlen;
957 i = (fmd_xlen - len)/2;
958 /* OK button */
959 fmd_widgets[2 - OFFSET].relative_x = i;
960 i += b2_len;
961 #ifdef WITH_BACKGROUND
962 /* Background button */
963 fmd_widgets[1].relative_x = i;
964 i += b1_len;
965 #endif
966 /* Cancel button */
967 fmd_widgets[0].relative_x = i;
969 #define chkbox_xpos(i) \
970 fmd_widgets [i].relative_x = fmd_xlen - str_term_width1 (fmd_widgets [i].u.checkbox.text) - 6
971 chkbox_xpos (3 - OFFSET);
972 chkbox_xpos (4 - OFFSET);
973 chkbox_xpos (9 - OFFSET);
974 #undef chkbox_xpos
976 /* inputs */
977 fmd_widgets[ 7 - OFFSET].u.input.len =
978 fmd_widgets[10 - OFFSET].u.input.len = fmd_xlen - 6;
980 /* unselect checkbox if target filesystem don't support attributes */
981 ctx->op_preserve = filegui__check_attrs_on_fs (def_text);
983 /* filter out a possible password from def_text */
984 tmp = strip_password (g_strdup (def_text), 1);
985 if (source_easy_patterns)
986 def_text_secure = strutils_glob_escape (tmp);
987 else
988 def_text_secure = strutils_regex_escape (tmp);
989 g_free (tmp);
991 /* destination */
992 fmd_widgets[7 - OFFSET].u.input.text = def_text_secure;
994 ctx->stable_symlinks = FALSE;
995 *do_background = FALSE;
998 struct stat buf;
1000 QuickDialog Quick_input =
1002 fmd_xlen, FMDY, -1, -1, op_names [operation],
1003 "[Mask Copy/Rename]", fmd_widgets, TRUE
1006 ask_file_mask:
1007 val = quick_dialog_skip (&Quick_input, 4);
1009 if (val == B_CANCEL) {
1010 g_free (def_text_secure);
1011 return NULL;
1014 if (ctx->follow_links)
1015 ctx->stat_func = mc_stat;
1016 else
1017 ctx->stat_func = mc_lstat;
1019 if (ctx->op_preserve) {
1020 ctx->preserve = TRUE;
1021 ctx->umask_kill = 0777777;
1022 ctx->preserve_uidgid = (geteuid () == 0);
1023 } else {
1024 int i2;
1025 ctx->preserve = ctx->preserve_uidgid = FALSE;
1026 i2 = umask (0);
1027 umask (i2);
1028 ctx->umask_kill = i2 ^ 0777777;
1031 if ((dest_dir == NULL) || (*dest_dir == '\0')) {
1032 g_free (def_text_secure);
1033 g_free (source_mask);
1034 return dest_dir;
1037 ctx->search_handle = mc_search_new(source_mask,-1);
1039 if (ctx->search_handle == NULL) {
1040 message (D_ERROR, MSG_ERROR, _("Invalid source pattern `%s'"),
1041 source_mask);
1042 g_free (dest_dir);
1043 g_free (source_mask);
1044 goto ask_file_mask;
1047 g_free (def_text_secure);
1048 g_free (source_mask);
1050 ctx->search_handle->is_case_sentitive = TRUE;
1051 if (source_easy_patterns)
1052 ctx->search_handle->search_type = MC_SEARCH_T_GLOB;
1053 else
1054 ctx->search_handle->search_type = MC_SEARCH_T_REGEX;
1056 tmp = dest_dir;
1057 dest_dir = tilde_expand (tmp);
1058 g_free (tmp);
1060 ctx->dest_mask = strrchr (dest_dir, PATH_SEP);
1061 if (ctx->dest_mask == NULL)
1062 ctx->dest_mask = dest_dir;
1063 else
1064 ctx->dest_mask++;
1065 orig_mask = ctx->dest_mask;
1066 if (!*ctx->dest_mask
1067 || (!ctx->dive_into_subdirs && !is_wildcarded (ctx->dest_mask)
1068 && (!only_one
1069 || (!mc_stat (dest_dir, &buf) && S_ISDIR (buf.st_mode))))
1070 || (ctx->dive_into_subdirs
1071 && ((!only_one && !is_wildcarded (ctx->dest_mask))
1072 || (only_one && !mc_stat (dest_dir, &buf)
1073 && S_ISDIR (buf.st_mode)))))
1074 ctx->dest_mask = g_strdup ("\\0");
1075 else {
1076 ctx->dest_mask = g_strdup (ctx->dest_mask);
1077 *orig_mask = '\0';
1079 if (!*dest_dir) {
1080 g_free (dest_dir);
1081 dest_dir = g_strdup ("./");
1083 if (val == B_USER)
1084 *do_background = TRUE;
1087 return dest_dir;