Moved time related stuff from lib/util.[ch] into lib/timefmt.[ch].
[midnight-commander/osp/hnatuluk1.git] / src / filegui.c
blobfa93fafe10d644c214057d4cd75ccbecb375938a
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"
87 #include "lib/timefmt.h" /* file_date() */
89 #include "setup.h" /* verbose */
90 #include "dialog.h" /* do_refresh() */
91 #include "widget.h" /* WLabel */
92 #include "main-widgets.h"
93 #include "main.h" /* the_hint */
94 #include "wtools.h" /* QuickDialog */
95 #include "panel.h" /* current_panel */
96 #include "fileopctx.h" /* FILE_CONT */
97 #include "filegui.h"
99 /* }}} */
100 /* *INDENT-OFF* */
101 typedef enum {
102 MSDOS_SUPER_MAGIC = 0x4d44,
103 NTFS_SB_MAGIC = 0x5346544e,
104 NTFS_3G_MAGIC = 0x65735546,
105 PROC_SUPER_MAGIC = 0x9fa0,
106 SMB_SUPER_MAGIC = 0x517B,
107 NCP_SUPER_MAGIC = 0x564c,
108 USBDEVICE_SUPER_MAGIC = 0x9fa2
109 } filegui_nonattrs_fs_t;
110 /* *INDENT-ON* */
112 /* Hack: the vfs code should not rely on this */
113 #define WITH_FULL_PATHS 1
115 /* Used for button result values */
116 typedef enum
118 REPLACE_YES = B_USER,
119 REPLACE_NO,
120 REPLACE_APPEND,
121 REPLACE_ALWAYS,
122 REPLACE_UPDATE,
123 REPLACE_NEVER,
124 REPLACE_ABORT,
125 REPLACE_SIZE,
126 REPLACE_REGET
127 } replace_action_t;
129 /* This structure describes the UI and internal data required by a file
130 * operation context.
132 typedef struct
134 /* ETA and bps */
135 gboolean showing_eta;
136 gboolean showing_bps;
138 /* Dialog and widgets for the operation progress window */
139 Dlg_head *op_dlg;
140 WLabel *file_string[2];
141 WLabel *file_label[2];
142 WGauge *progress_file_gauge;
143 WLabel *progress_file_label;
145 WGauge *progress_total_gauge;
147 WLabel *total_files_processed_label;
148 WLabel *time_label;
149 WLabel *total_bytes_label;
151 /* Query replace dialog */
152 Dlg_head *replace_dlg;
153 const char *replace_filename;
154 replace_action_t replace_result;
156 struct stat *s_stat, *d_stat;
157 } FileOpContextUI;
159 int classic_progressbar = 1;
161 /* Used to save the hint line */
162 static int last_hint_line;
164 /* File operate window sizes */
165 #define WX 58
166 #define WY 11
167 #define FCOPY_LABEL_X 3
169 static gboolean
170 filegui__check_attrs_on_fs (const char *fs_path)
172 #ifdef STATFS
173 STRUCT_STATFS stfs;
175 if (!setup_copymove_persistent_attr)
176 return FALSE;
178 if (STATFS (fs_path, &stfs) != 0)
179 return TRUE;
181 # ifdef __linux__
182 switch ((filegui_nonattrs_fs_t) stfs.f_type)
184 case MSDOS_SUPER_MAGIC:
185 case NTFS_SB_MAGIC:
186 case NTFS_3G_MAGIC:
187 case PROC_SUPER_MAGIC:
188 case SMB_SUPER_MAGIC:
189 case NCP_SUPER_MAGIC:
190 case USBDEVICE_SUPER_MAGIC:
191 return FALSE;
193 # elif defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) \
194 || defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
195 if (!strcmp (stfs.f_fstypename, "msdos")
196 || !strcmp (stfs.f_fstypename, "msdosfs")
197 || !strcmp (stfs.f_fstypename, "ntfs")
198 || !strcmp (stfs.f_fstypename, "procfs")
199 || !strcmp (stfs.f_fstypename, "smbfs") || strstr (stfs.f_fstypename, "fusefs"))
200 return FALSE;
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") || !strcmp (stfs.f_basetype, "fuse"))
206 return FALSE;
207 # endif
208 #endif /* STATFS */
210 return TRUE;
213 FileProgressStatus
214 check_progress_buttons (FileOpContext * ctx)
216 int c;
217 Gpm_Event event;
218 FileOpContextUI *ui;
220 if (ctx->ui == NULL)
221 return FILE_CONT;
223 ui = ctx->ui;
225 event.x = -1; /* Don't show the GPM cursor */
226 c = tty_get_event (&event, FALSE, FALSE);
227 if (c == EV_NONE)
228 return FILE_CONT;
230 /* Reinitialize to avoid old values after events other than
231 selecting a button */
232 ui->op_dlg->ret_value = FILE_CONT;
234 dlg_process_event (ui->op_dlg, c, &event);
235 switch (ui->op_dlg->ret_value)
237 case FILE_SKIP:
238 return FILE_SKIP;
239 case B_CANCEL:
240 case FILE_ABORT:
241 return FILE_ABORT;
242 default:
243 return FILE_CONT;
247 /* {{{ File progress display routines */
249 void
250 file_op_context_create_ui_without_init (FileOpContext * ctx, gboolean with_eta,
251 filegui_dialog_type_t dialog_type)
253 FileOpContextUI *ui;
254 int minus = 0, total_reserve = 0;
255 const char *abort_button_label = N_("&Abort");
256 const char *skip_button_label = N_("&Skip");
257 int abort_button_width, skip_button_width, buttons_width;
258 int dlg_width;
260 g_return_if_fail (ctx != NULL);
261 g_return_if_fail (ctx->ui == NULL);
263 #ifdef ENABLE_NLS
264 abort_button_label = _(abort_button_label);
265 skip_button_label = _(skip_button_label);
266 #endif
268 abort_button_width = str_term_width1 (abort_button_label) + 3;
269 skip_button_width = str_term_width1 (skip_button_label) + 3;
270 buttons_width = abort_button_width + skip_button_width + 2;
272 dlg_width = max (WX, buttons_width + 6);
274 ui = g_new0 (FileOpContextUI, 1);
275 ctx->ui = ui;
277 ctx->dialog_type = dialog_type;
279 switch (dialog_type)
281 case FILEGUI_DIALOG_ONE_ITEM:
282 total_reserve = 0;
283 minus = verbose ? 0 : 2;
284 break;
285 case FILEGUI_DIALOG_MULTI_ITEM:
286 total_reserve = 5;
287 minus = verbose ? 0 : 7;
288 break;
289 case FILEGUI_DIALOG_DELETE_ITEM:
290 total_reserve = -5;
291 minus = 0;
292 break;
295 ctx->recursive_result = RECURSIVE_YES;
297 ui->replace_result = REPLACE_YES;
298 ui->showing_eta = with_eta;
299 ui->showing_bps = with_eta;
301 ui->op_dlg =
302 create_dlg (TRUE, 0, 0, WY - minus + 1 + total_reserve, dlg_width,
303 dialog_colors, NULL, NULL, op_names[ctx->operation], DLG_CENTER | DLG_REVERSE);
305 last_hint_line = the_hint->widget.y;
306 if ((ui->op_dlg->y + ui->op_dlg->lines) > last_hint_line)
307 the_hint->widget.y = ui->op_dlg->y + ui->op_dlg->lines + 1;
309 add_widget (ui->op_dlg,
310 button_new (WY - minus - 2 + total_reserve,
311 dlg_width / 2 + 1, FILE_ABORT,
312 NORMAL_BUTTON, abort_button_label, NULL));
313 add_widget (ui->op_dlg,
314 button_new (WY - minus - 2 + total_reserve,
315 dlg_width / 2 - 1 - skip_button_width, FILE_SKIP,
316 NORMAL_BUTTON, skip_button_label, NULL));
319 if (verbose && dialog_type == FILEGUI_DIALOG_MULTI_ITEM)
321 add_widget (ui->op_dlg, hline_new (8, 1, dlg_width - 2));
323 add_widget (ui->op_dlg, ui->total_bytes_label = label_new (8, FCOPY_LABEL_X + 15, ""));
325 add_widget (ui->op_dlg, ui->progress_total_gauge =
326 gauge_new (9, FCOPY_LABEL_X + 3, 0, 100, 0));
328 add_widget (ui->op_dlg, ui->total_files_processed_label =
329 label_new (11, FCOPY_LABEL_X, ""));
331 add_widget (ui->op_dlg, ui->time_label = label_new (12, FCOPY_LABEL_X, ""));
334 add_widget (ui->op_dlg, ui->progress_file_label = label_new (7, FCOPY_LABEL_X, ""));
336 add_widget (ui->op_dlg, ui->progress_file_gauge = gauge_new (6, FCOPY_LABEL_X + 3, 0, 100, 0));
338 add_widget (ui->op_dlg, ui->file_string[1] = label_new (5, FCOPY_LABEL_X, ""));
340 add_widget (ui->op_dlg, ui->file_label[1] = label_new (4, FCOPY_LABEL_X, ""));
341 add_widget (ui->op_dlg, ui->file_string[0] = label_new (3, FCOPY_LABEL_X, ""));
342 add_widget (ui->op_dlg, ui->file_label[0] = label_new (2, FCOPY_LABEL_X, ""));
344 if ((right_panel == current_panel) && !classic_progressbar)
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,
354 filegui_dialog_type_t dialog_type)
356 FileOpContextUI *ui;
358 g_return_if_fail (ctx != NULL);
359 g_return_if_fail (ctx->ui == NULL);
361 file_op_context_create_ui_without_init (ctx, with_eta, dialog_type);
362 ui = ctx->ui;
364 /* We will manage the dialog without any help, that's why
365 we have to call init_dlg */
366 init_dlg (ui->op_dlg);
369 void
370 file_op_context_destroy_ui (FileOpContext * ctx)
372 FileOpContextUI *ui;
374 g_return_if_fail (ctx != NULL);
376 if (ctx->ui)
378 ui = ctx->ui;
380 dlg_run_done (ui->op_dlg);
381 destroy_dlg (ui->op_dlg);
382 g_free (ui);
385 the_hint->widget.y = last_hint_line;
387 ctx->ui = NULL;
390 static void
391 file_frmt_time (char *buffer, double eta_secs)
393 int eta_hours, eta_mins, eta_s;
394 eta_hours = eta_secs / (60 * 60);
395 eta_mins = (eta_secs - (eta_hours * 60 * 60)) / 60;
396 eta_s = eta_secs - (eta_hours * 60 * 60 + eta_mins * 60);
397 g_snprintf (buffer, BUF_TINY, _("%d:%02d.%02d"), eta_hours, eta_mins, eta_s);
400 static void
401 file_eta_prepare_for_show (char *buffer, double eta_secs, gboolean always_show)
403 char _fmt_buff[BUF_TINY];
404 if (eta_secs <= 0.5 && !always_show)
406 *buffer = '\0';
407 return;
409 if (eta_secs <= 0.5)
410 eta_secs = 1;
411 file_frmt_time (_fmt_buff, eta_secs);
412 g_snprintf (buffer, BUF_TINY, _("ETA %s"), _fmt_buff);
415 static void
416 file_bps_prepare_for_show (char *buffer, long bps)
418 if (bps > 1024 * 1024)
420 g_snprintf (buffer, BUF_TINY, _("%.2f MB/s"), bps / (1024 * 1024.0));
422 else if (bps > 1024)
424 g_snprintf (buffer, BUF_TINY, _("%.2f KB/s"), bps / 1024.0);
426 else if (bps > 1)
428 g_snprintf (buffer, BUF_TINY, _("%ld B/s"), bps);
430 else
431 *buffer = 0;
435 show progressbar for file
437 void
438 file_progress_show (FileOpContext * ctx, off_t done, off_t total,
439 const char *stalled_msg, gboolean force_update)
441 FileOpContextUI *ui;
442 char buffer[BUF_TINY];
443 char buffer2[BUF_TINY];
444 char buffer3[BUF_TINY];
446 g_return_if_fail (ctx != NULL);
448 if (ctx->ui == NULL)
449 return;
451 ui = ctx->ui;
453 if (!verbose)
454 return;
456 if (total == 0)
458 gauge_show (ui->progress_file_gauge, 0);
459 return;
462 gauge_set_value (ui->progress_file_gauge, 1024, (int) (1024 * done / total));
463 gauge_show (ui->progress_file_gauge, 1);
465 if (!force_update)
466 return;
468 if (ui->showing_eta && ctx->eta_secs > 0.5)
470 file_eta_prepare_for_show (buffer2, ctx->eta_secs, FALSE);
471 file_bps_prepare_for_show (buffer3, ctx->bps);
472 g_snprintf (buffer, BUF_TINY, "%s (%s) %s", buffer2, buffer3, stalled_msg);
474 else
476 g_snprintf (buffer, BUF_TINY, "%s", stalled_msg);
479 label_set_text (ui->progress_file_label, buffer);
482 void
483 file_progress_show_count (FileOpContext * ctx, off_t done, off_t total)
485 char buffer[BUF_TINY];
486 FileOpContextUI *ui;
488 g_return_if_fail (ctx != NULL);
490 if (ctx->dialog_type != FILEGUI_DIALOG_MULTI_ITEM || ctx->ui == NULL)
491 return;
493 ui = ctx->ui;
495 if (!verbose)
496 return;
498 g_snprintf (buffer, BUF_TINY, _("Files processed: %llu of %llu"),
499 (unsigned long long) done, (unsigned long long) total);
501 label_set_text (ui->total_files_processed_label, buffer);
504 void
505 file_progress_show_total (FileOpTotalContext * tctx, FileOpContext * ctx, double copyed_bytes,
506 gboolean need_show_total_summary)
508 char buffer[BUF_TINY];
509 char buffer2[BUF_TINY];
510 char buffer3[BUF_TINY];
511 char buffer4[BUF_TINY];
512 struct timeval tv_current;
513 FileOpContextUI *ui;
515 if (!verbose)
516 return;
518 if (ctx->dialog_type != FILEGUI_DIALOG_MULTI_ITEM || ctx->ui == NULL)
519 return;
521 ui = ctx->ui;
523 if (ctx->progress_bytes > 0)
525 gauge_set_value (ui->progress_total_gauge, 1024,
526 (int) (1024 * copyed_bytes / ctx->progress_bytes));
527 gauge_show (ui->progress_total_gauge, 1);
529 else
530 gauge_show (ui->progress_total_gauge, 0);
533 if (!need_show_total_summary && tctx->bps == 0)
534 return;
536 gettimeofday (&tv_current, NULL);
537 file_frmt_time (buffer2, tv_current.tv_sec - tctx->transfer_start.tv_sec);
538 file_eta_prepare_for_show (buffer3, tctx->eta_secs, TRUE);
539 file_bps_prepare_for_show (buffer4, (long) tctx->bps);
541 g_snprintf (buffer, BUF_TINY, _("Time: %s %s (%s)"), buffer2, buffer3, buffer4);
542 label_set_text (ui->time_label, buffer);
544 size_trunc_len (buffer2, 5, tctx->copyed_bytes, 0, panels_options.kilobyte_si);
545 size_trunc_len (buffer3, 5, ctx->progress_bytes, 0, panels_options.kilobyte_si);
547 g_snprintf (buffer, BUF_TINY, _("Total: %s of %s"), buffer2, buffer3);
549 label_set_text (ui->total_bytes_label, buffer);
553 /* }}} */
555 #define truncFileString(ui, s) str_trunc (s, 52)
556 #define truncFileStringSecure(ui, s) path_trunc (s, 52)
558 void
559 file_progress_show_source (FileOpContext * ctx, const char *s)
561 FileOpContextUI *ui;
563 g_return_if_fail (ctx != NULL);
565 if (ctx->ui == NULL)
566 return;
568 ui = ctx->ui;
570 if (s != NULL)
572 #ifdef WITH_FULL_PATHS
573 int i = strlen (current_panel->cwd);
575 /* We remove the full path we have added before */
576 if (!strncmp (s, current_panel->cwd, i))
578 if (s[i] == PATH_SEP)
579 s += i + 1;
581 #endif /* WITH_FULL_PATHS */
583 label_set_text (ui->file_label[0], _("Source"));
584 label_set_text (ui->file_string[0], truncFileString (ui, s));
586 else
588 label_set_text (ui->file_label[0], "");
589 label_set_text (ui->file_string[0], "");
593 void
594 file_progress_show_target (FileOpContext * ctx, const char *s)
596 FileOpContextUI *ui;
598 g_return_if_fail (ctx != NULL);
600 if (ctx->ui == NULL)
601 return;
603 ui = ctx->ui;
605 if (s != NULL)
607 label_set_text (ui->file_label[1], _("Target"));
608 label_set_text (ui->file_string[1], truncFileStringSecure (ui, s));
610 else
612 label_set_text (ui->file_label[1], "");
613 label_set_text (ui->file_string[1], "");
617 void
618 file_progress_show_deleting (FileOpContext * ctx, const char *s)
620 FileOpContextUI *ui;
622 g_return_if_fail (ctx != NULL);
624 if (ctx->ui == NULL)
625 return;
627 ui = ctx->ui;
628 label_set_text (ui->file_label[0], _("Deleting"));
629 label_set_text (ui->file_label[0], truncFileStringSecure (ui, s));
633 * FIXME: probably it is better to replace this with quick dialog machinery,
634 * but actually I'm not familiar with it and have not much time :(
635 * alex
637 static replace_action_t
638 overwrite_query_dialog (FileOpContext * ctx, enum OperationMode mode)
640 #define ADD_RD_BUTTON(i)\
641 add_widget (ui->replace_dlg,\
642 button_new (rd_widgets [i].ypos, rd_widgets [i].xpos, rd_widgets [i].value,\
643 NORMAL_BUTTON, rd_widgets [i].text, 0))
645 #define ADD_RD_LABEL(i, p1, p2)\
646 g_snprintf (buffer, sizeof (buffer), rd_widgets [i].text, p1, p2);\
647 add_widget (ui->replace_dlg,\
648 label_new (rd_widgets [i].ypos, rd_widgets [i].xpos, buffer))
650 /* dialog sizes */
651 const int rd_ylen = 17;
652 int rd_xlen = 60;
654 struct
656 const char *text;
657 int ypos, xpos;
658 int value; /* 0 for labels */
659 } rd_widgets[] =
661 /* *INDENT-OFF* */
662 /* 0 */
663 { N_("Target file already exists!"), 3, 4, 0 },
664 /* 1 */
665 { "%s", 4, 4, 0 },
666 #if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined _LARGE_FILES && _LARGE_FILES)
667 /* 2 */
668 { N_("Source date: %s, size %llu"), 6, 4, 0 },
669 /* 3 */
670 { N_("Target date: %s, size %llu"), 7, 4, 0 },
671 #else
672 /* 2 */
673 { N_("Source date: %s, size %u"), 6, 4, 0 },
674 /* 3 */
675 { N_("Target date: %s, size %u"), 7, 4, 0 },
676 #endif
677 /* 4 */
678 { N_("&Abort"), 14, 25, REPLACE_ABORT },
679 /* 5 */
680 { N_("If &size differs"), 12, 28, REPLACE_SIZE },
681 /* 6 */
682 { N_("Non&e"), 11, 47, REPLACE_NEVER },
683 /* 7 */
684 { N_("&Update"), 11, 36, REPLACE_UPDATE },
685 /* 8 */
686 { N_("A&ll"), 11, 28, REPLACE_ALWAYS },
687 /* 9 */
688 { N_("Overwrite all targets?"), 11, 4, 0 },
689 /* 10 */
690 { N_("&Reget"), 10, 28, REPLACE_REGET },
691 /* 11 */
692 { N_("A&ppend"), 9, 45, REPLACE_APPEND },
693 /* 12 */
694 { N_("&No"), 9, 37, REPLACE_NO },
695 /* 13 */
696 { N_("&Yes"), 9, 28, REPLACE_YES },
697 /* 14 */
698 { N_("Overwrite this target?"), 9, 4, 0 }
699 /* *INDENT-ON* */
702 const int num = sizeof (rd_widgets) / sizeof (rd_widgets[0]);
703 int *widgets_len;
705 FileOpContextUI *ui = ctx->ui;
707 char buffer[BUF_SMALL];
708 const char *title;
709 const char *stripped_name = strip_home_and_password (ui->replace_filename);
710 int stripped_name_len;
712 int result;
714 widgets_len = g_new0 (int, num);
716 if (mode == Foreground)
717 title = _("File exists");
718 else
719 title = _("Background process: File exists");
721 stripped_name_len = str_term_width1 (stripped_name);
724 int i, l1, l2, l, row;
726 for (i = 0; i < num; i++)
728 #ifdef ENABLE_NLS
729 if (i != 1) /* skip filename */
730 rd_widgets[i].text = _(rd_widgets[i].text);
731 #endif /* ENABLE_NLS */
732 widgets_len[i] = str_term_width1 (rd_widgets[i].text);
736 * longest of "Overwrite..." labels
737 * (assume "Target date..." are short enough)
739 l1 = max (widgets_len[9], widgets_len[14]);
741 /* longest of button rows */
742 i = num;
743 for (row = l = l2 = 0; i--;)
744 if (rd_widgets[i].value != 0)
746 if (row != rd_widgets[i].ypos)
748 row = rd_widgets[i].ypos;
749 l2 = max (l2, l);
750 l = 0;
752 l += widgets_len[i] + 4;
755 l2 = max (l2, l); /* last row */
756 rd_xlen = max (rd_xlen, l1 + l2 + 8);
757 rd_xlen = max (rd_xlen, str_term_width1 (title) + 2);
758 rd_xlen = max (rd_xlen, min (COLS, stripped_name_len + 8));
760 /* Now place widgets */
761 l1 += 5; /* start of first button in the row */
762 i = num;
763 for (l = l1, row = 0; --i > 1;)
764 if (rd_widgets[i].value != 0)
766 if (row != rd_widgets[i].ypos)
768 row = rd_widgets[i].ypos;
769 l = l1;
771 rd_widgets[i].xpos = l;
772 l += widgets_len[i] + 4;
775 /* Abort button is centered */
776 rd_widgets[4].xpos = (rd_xlen - widgets_len[4] - 3) / 2;
779 /* FIXME - missing help node */
780 ui->replace_dlg =
781 create_dlg (TRUE, 0, 0, rd_ylen, rd_xlen, alarm_colors, NULL, "[Replace]",
782 title, DLG_CENTER | DLG_REVERSE);
784 /* prompt -- centered */
785 add_widget (ui->replace_dlg,
786 label_new (rd_widgets[0].ypos, (rd_xlen - widgets_len[0]) / 2, rd_widgets[0].text));
787 /* file name -- centered */
788 stripped_name = str_trunc (stripped_name, rd_xlen - 8);
789 stripped_name_len = str_term_width1 (stripped_name);
790 add_widget (ui->replace_dlg,
791 label_new (rd_widgets[1].ypos, (rd_xlen - stripped_name_len) / 2, stripped_name));
793 /* source date */
794 ADD_RD_LABEL (2, file_date (ui->s_stat->st_mtime), (off_t) ui->s_stat->st_size);
795 /* destination date */
796 ADD_RD_LABEL (3, file_date (ui->d_stat->st_mtime), (off_t) ui->d_stat->st_size);
798 ADD_RD_BUTTON (4); /* Abort */
799 ADD_RD_BUTTON (5); /* If size differs */
800 ADD_RD_BUTTON (6); /* None */
801 ADD_RD_BUTTON (7); /* Update */
802 ADD_RD_BUTTON (8); /* All" */
803 ADD_RD_LABEL (9, 0, 0); /* Overwrite all targets? */
805 /* "this target..." widgets */
806 if (!S_ISDIR (ui->d_stat->st_mode))
808 if ((ctx->operation == OP_COPY) && (ui->d_stat->st_size != 0)
809 && (ui->s_stat->st_size > ui->d_stat->st_size))
810 ADD_RD_BUTTON (10); /* Reget */
812 ADD_RD_BUTTON (11); /* Append */
814 ADD_RD_BUTTON (12); /* No */
815 ADD_RD_BUTTON (13); /* Yes */
816 ADD_RD_LABEL (14, 0, 0); /* Overwrite this target? */
818 result = run_dlg (ui->replace_dlg);
819 destroy_dlg (ui->replace_dlg);
821 g_free (widgets_len);
823 return (result == B_CANCEL) ? REPLACE_ABORT : (replace_action_t) result;
824 #undef ADD_RD_LABEL
825 #undef ADD_RD_BUTTON
828 FileProgressStatus
829 file_progress_real_query_replace (FileOpContext * ctx,
830 enum OperationMode mode, const char *destname,
831 struct stat * _s_stat, struct stat * _d_stat)
833 FileOpContextUI *ui;
835 g_return_val_if_fail (ctx != NULL, FILE_CONT);
836 g_return_val_if_fail (ctx->ui != NULL, FILE_CONT);
838 ui = ctx->ui;
840 if (ui->replace_result < REPLACE_ALWAYS)
842 ui->replace_filename = destname;
843 ui->s_stat = _s_stat;
844 ui->d_stat = _d_stat;
845 ui->replace_result = overwrite_query_dialog (ctx, mode);
848 switch (ui->replace_result)
850 case REPLACE_UPDATE:
851 do_refresh ();
852 if (_s_stat->st_mtime > _d_stat->st_mtime)
853 return FILE_CONT;
854 else
855 return FILE_SKIP;
857 case REPLACE_SIZE:
858 do_refresh ();
859 if (_s_stat->st_size == _d_stat->st_size)
860 return FILE_SKIP;
861 else
862 return FILE_CONT;
864 case REPLACE_REGET:
865 /* Careful: we fall through and set do_append */
866 ctx->do_reget = _d_stat->st_size;
868 case REPLACE_APPEND:
869 ctx->do_append = TRUE;
871 case REPLACE_YES:
872 case REPLACE_ALWAYS:
873 do_refresh ();
874 return FILE_CONT;
875 case REPLACE_NO:
876 case REPLACE_NEVER:
877 do_refresh ();
878 return FILE_SKIP;
879 case REPLACE_ABORT:
880 default:
881 return FILE_ABORT;
885 static gboolean
886 is_wildcarded (char *p)
888 for (; *p; p++)
890 if (*p == '*')
891 return TRUE;
892 if (*p == '\\' && p[1] >= '1' && p[1] <= '9')
893 return TRUE;
895 return FALSE;
898 char *
899 file_mask_dialog (FileOpContext * ctx, FileOperation operation,
900 gboolean only_one,
901 const char *format, const void *text,
902 const char *def_text, gboolean * do_background)
904 const size_t FMDY = 13;
905 const size_t FMDX = 68;
906 size_t fmd_xlen;
908 /* buttons */
909 const size_t gap = 1;
910 size_t b0_len, b2_len;
911 size_t b1_len = 0;
913 int source_easy_patterns = easy_patterns;
914 size_t i, len;
915 char fmd_buf[BUF_MEDIUM];
916 char *source_mask, *orig_mask, *dest_dir, *tmp;
917 char *def_text_secure;
918 int val;
920 QuickWidget fmd_widgets[] = {
921 /* 0 */ QUICK_BUTTON (42, 64, 10, FMDY, N_("&Cancel"), B_CANCEL, NULL),
922 #ifdef WITH_BACKGROUND
923 /* 1 */ QUICK_BUTTON (25, 64, 10, FMDY, N_("&Background"), B_USER, NULL),
924 #define OFFSET 0
925 #else
926 #define OFFSET 1
927 #endif /* WITH_BACKGROUND */
928 /* 2 - OFFSET */
929 QUICK_BUTTON (14, FMDX, 10, FMDY, N_("&OK"), B_ENTER, NULL),
930 /* 3 - OFFSET */
931 QUICK_CHECKBOX (42, FMDX, 8, FMDY, N_("&Stable Symlinks"), &ctx->stable_symlinks),
932 /* 4 - OFFSET */
933 QUICK_CHECKBOX (31, FMDX, 7, FMDY, N_("Di&ve into subdir if exists"),
934 &ctx->dive_into_subdirs),
935 /* 5 - OFFSET */
936 QUICK_CHECKBOX (3, FMDX, 8, FMDY, N_("Preserve &attributes"), &ctx->op_preserve),
937 /* 6 - OFFSET */
938 QUICK_CHECKBOX (3, FMDX, 7, FMDY, N_("Follow &links"), &ctx->follow_links),
939 /* 7 - OFFSET */
940 QUICK_INPUT (3, FMDX, 6, FMDY, "", 58, 0, "input2", &dest_dir),
941 /* 8 - OFFSET */
942 QUICK_LABEL (3, FMDX, 5, FMDY, N_("to:")),
943 /* 9 - OFFSET */
944 QUICK_CHECKBOX (37, FMDX, 4, FMDY, N_("&Using shell patterns"), &source_easy_patterns),
945 /* 10 - OFFSET */
946 QUICK_INPUT (3, FMDX, 3, FMDY, easy_patterns ? "*" : "^(.*)$", 58, 0, "input-def",
947 &source_mask),
948 /* 11 - OFFSET */
949 QUICK_LABEL (3, FMDX, 2, FMDY, fmd_buf),
950 QUICK_END
953 g_return_val_if_fail (ctx != NULL, NULL);
955 #ifdef ENABLE_NLS
956 /* buttons */
957 for (i = 0; i <= 2 - OFFSET; i++)
958 fmd_widgets[i].u.button.text = _(fmd_widgets[i].u.button.text);
960 /* checkboxes */
961 for (i = 3 - OFFSET; i <= 9 - OFFSET; i++)
962 if (i != 7 - OFFSET)
963 fmd_widgets[i].u.checkbox.text = _(fmd_widgets[i].u.checkbox.text);
964 #endif /* !ENABLE_NLS */
966 fmd_xlen = max (FMDX, (size_t) COLS * 2 / 3);
968 len = str_term_width1 (fmd_widgets[6 - OFFSET].u.checkbox.text)
969 + str_term_width1 (fmd_widgets[4 - OFFSET].u.checkbox.text) + 15;
970 fmd_xlen = max (fmd_xlen, len);
972 len = str_term_width1 (fmd_widgets[5 - OFFSET].u.checkbox.text)
973 + str_term_width1 (fmd_widgets[3 - OFFSET].u.checkbox.text) + 15;
974 fmd_xlen = max (fmd_xlen, len);
976 /* buttons */
977 b2_len = str_term_width1 (fmd_widgets[2 - OFFSET].u.button.text) + 6 + gap; /* OK */
978 #ifdef WITH_BACKGROUND
979 b1_len = str_term_width1 (fmd_widgets[1].u.button.text) + 4 + gap; /* Background */
980 #endif
981 b0_len = str_term_width1 (fmd_widgets[0].u.button.text) + 4; /* Cancel */
982 len = b0_len + b1_len + b2_len;
983 fmd_xlen = min (max (fmd_xlen, len + 6), (size_t) COLS);
985 if (only_one)
987 int flen;
989 flen = str_term_width1 (format);
990 i = fmd_xlen - flen - 4; /* FIXME */
991 g_snprintf (fmd_buf, sizeof (fmd_buf), format, str_trunc ((const char *) text, i));
993 else
995 g_snprintf (fmd_buf, sizeof (fmd_buf), format, *(const int *) text);
996 fmd_xlen = max (fmd_xlen, (size_t) str_term_width1 (fmd_buf) + 6);
999 for (i = sizeof (fmd_widgets) / sizeof (fmd_widgets[0]); i > 0;)
1000 fmd_widgets[--i].x_divisions = fmd_xlen;
1002 i = (fmd_xlen - len) / 2;
1003 /* OK button */
1004 fmd_widgets[2 - OFFSET].relative_x = i;
1005 i += b2_len;
1006 #ifdef WITH_BACKGROUND
1007 /* Background button */
1008 fmd_widgets[1].relative_x = i;
1009 i += b1_len;
1010 #endif
1011 /* Cancel button */
1012 fmd_widgets[0].relative_x = i;
1014 #define chkbox_xpos(i) \
1015 fmd_widgets [i].relative_x = fmd_xlen - str_term_width1 (fmd_widgets [i].u.checkbox.text) - 6
1016 chkbox_xpos (3 - OFFSET);
1017 chkbox_xpos (4 - OFFSET);
1018 chkbox_xpos (9 - OFFSET);
1019 #undef chkbox_xpos
1021 /* inputs */
1022 fmd_widgets[7 - OFFSET].u.input.len = fmd_widgets[10 - OFFSET].u.input.len = fmd_xlen - 6;
1024 /* unselect checkbox if target filesystem don't support attributes */
1025 ctx->op_preserve = filegui__check_attrs_on_fs (def_text);
1027 /* filter out a possible password from def_text */
1028 tmp = strip_password (g_strdup (def_text), 1);
1029 if (source_easy_patterns)
1030 def_text_secure = strutils_glob_escape (tmp);
1031 else
1032 def_text_secure = strutils_regex_escape (tmp);
1033 g_free (tmp);
1035 /* destination */
1036 fmd_widgets[7 - OFFSET].u.input.text = def_text_secure;
1038 ctx->stable_symlinks = FALSE;
1039 *do_background = FALSE;
1042 struct stat buf;
1044 QuickDialog Quick_input = {
1045 fmd_xlen, FMDY, -1, -1, op_names[operation],
1046 "[Mask Copy/Rename]", fmd_widgets, NULL, TRUE
1049 ask_file_mask:
1050 val = quick_dialog_skip (&Quick_input, 4);
1052 if (val == B_CANCEL)
1054 g_free (def_text_secure);
1055 return NULL;
1058 if (ctx->follow_links)
1059 ctx->stat_func = mc_stat;
1060 else
1061 ctx->stat_func = mc_lstat;
1063 if (ctx->op_preserve)
1065 ctx->preserve = TRUE;
1066 ctx->umask_kill = 0777777;
1067 ctx->preserve_uidgid = (geteuid () == 0);
1069 else
1071 int i2;
1072 ctx->preserve = ctx->preserve_uidgid = FALSE;
1073 i2 = umask (0);
1074 umask (i2);
1075 ctx->umask_kill = i2 ^ 0777777;
1078 if ((dest_dir == NULL) || (*dest_dir == '\0'))
1080 g_free (def_text_secure);
1081 g_free (source_mask);
1082 return dest_dir;
1085 ctx->search_handle = mc_search_new (source_mask, -1);
1087 if (ctx->search_handle == NULL)
1089 message (D_ERROR, MSG_ERROR, _("Invalid source pattern `%s'"), source_mask);
1090 g_free (dest_dir);
1091 g_free (source_mask);
1092 goto ask_file_mask;
1095 g_free (def_text_secure);
1096 g_free (source_mask);
1098 ctx->search_handle->is_case_sensitive = TRUE;
1099 if (source_easy_patterns)
1100 ctx->search_handle->search_type = MC_SEARCH_T_GLOB;
1101 else
1102 ctx->search_handle->search_type = MC_SEARCH_T_REGEX;
1104 tmp = dest_dir;
1105 dest_dir = tilde_expand (tmp);
1106 g_free (tmp);
1108 ctx->dest_mask = strrchr (dest_dir, PATH_SEP);
1109 if (ctx->dest_mask == NULL)
1110 ctx->dest_mask = dest_dir;
1111 else
1112 ctx->dest_mask++;
1113 orig_mask = ctx->dest_mask;
1114 if (!*ctx->dest_mask
1115 || (!ctx->dive_into_subdirs && !is_wildcarded (ctx->dest_mask)
1116 && (!only_one
1117 || (!mc_stat (dest_dir, &buf) && S_ISDIR (buf.st_mode))))
1118 || (ctx->dive_into_subdirs
1119 && ((!only_one && !is_wildcarded (ctx->dest_mask))
1120 || (only_one && !mc_stat (dest_dir, &buf) && S_ISDIR (buf.st_mode)))))
1121 ctx->dest_mask = g_strdup ("\\0");
1122 else
1124 ctx->dest_mask = g_strdup (ctx->dest_mask);
1125 *orig_mask = '\0';
1127 if (!*dest_dir)
1129 g_free (dest_dir);
1130 dest_dir = g_strdup ("./");
1132 if (val == B_USER)
1133 *do_background = TRUE;
1136 return dest_dir;