2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "filefilter.h"
29 #include "thumb-standard.h"
31 #include "ui-bookmark.h"
32 #include "ui-fileops.h"
38 #define DIALOG_WIDTH 750
40 static GdkPixbuf *file_util_get_error_icon(FileData *fd, GList *list, GtkWidget *widget);
43 *--------------------------------------------------------------------------
44 * Adds 1 or 2 images (if 2, side by side) to a GenericDialog
45 *--------------------------------------------------------------------------
48 #define DIALOG_DEF_IMAGE_DIM_X 150
49 #define DIALOG_DEF_IMAGE_DIM_Y 100
51 static void generic_dialog_add_image(GenericDialog *gd, GtkWidget *box,
52 FileData *fd1, const gchar *header1,
53 gboolean second_image,
54 FileData *fd2, const gchar *header2,
55 gboolean show_filename)
58 GtkWidget *preview_box = NULL;
60 GtkWidget *label = NULL;
62 if (!box) box = gd->vbox;
66 preview_box = pref_box_new(box, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE);
71 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
76 gtk_box_pack_start(GTK_BOX(preview_box), vbox, FALSE, TRUE, 0);
78 sep = gtk_hseparator_new();
79 gtk_box_pack_start(GTK_BOX(preview_box), sep, FALSE, FALSE, 0);
84 gtk_box_pack_start(GTK_BOX(box), vbox, FALSE, TRUE, PREF_PAD_GAP);
86 gtk_widget_show(vbox);
92 head = pref_label_new(vbox, header1);
93 pref_label_bold(head, TRUE, FALSE);
94 gtk_label_set_xalign(GTK_LABEL(head), 0.0);
95 gtk_label_set_yalign(GTK_LABEL(head), 0.5);
98 imd = image_new(FALSE);
99 g_object_set(G_OBJECT(imd->pr), "zoom_expand", FALSE, NULL);
100 gtk_widget_set_size_request(imd->widget, DIALOG_DEF_IMAGE_DIM_X, DIALOG_DEF_IMAGE_DIM_Y);
101 gtk_box_pack_start(GTK_BOX(vbox), imd->widget, TRUE, TRUE, 0);
102 image_change_fd(imd, fd1, 0.0);
103 gtk_widget_show(imd->widget);
107 label = pref_label_new(vbox, (fd1 == NULL) ? "" : fd1->name);
110 /* only the first image is stored (for use in gd_image_set) */
111 g_object_set_data(G_OBJECT(gd->dialog), "img_image", imd);
112 g_object_set_data(G_OBJECT(gd->dialog), "img_label", label);
119 vbox = pref_box_new(preview_box, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
125 head = pref_label_new(vbox, header2);
126 pref_label_bold(head, TRUE, FALSE);
127 gtk_label_set_xalign(GTK_LABEL(head), 0.0);
128 gtk_label_set_yalign(GTK_LABEL(head), 0.5);
131 imd = image_new(FALSE);
132 g_object_set(G_OBJECT(imd->pr), "zoom_expand", FALSE, NULL);
133 gtk_widget_set_size_request(imd->widget, DIALOG_DEF_IMAGE_DIM_X, DIALOG_DEF_IMAGE_DIM_Y);
134 gtk_box_pack_start(GTK_BOX(vbox), imd->widget, TRUE, TRUE, 0);
135 if (fd2) image_change_fd(imd, fd2, 0.0);
136 gtk_widget_show(imd->widget);
140 label = pref_label_new(vbox, (fd2 == NULL) ? "" : fd2->name);
142 g_object_set_data(G_OBJECT(gd->dialog), "img_image2", imd);
143 g_object_set_data(G_OBJECT(gd->dialog), "img_label2", label);
148 *--------------------------------------------------------------------------
149 * Wrappers to aid in setting additional dialog properties (unde mouse, etc.)
150 *--------------------------------------------------------------------------
153 GenericDialog *file_util_gen_dlg(const gchar *title,
155 GtkWidget *parent, gboolean auto_close,
156 void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
160 gd = generic_dialog_new(title, role, parent, auto_close, cancel_cb, data);
161 if (options->place_dialogs_under_mouse)
163 gtk_window_set_position(GTK_WINDOW(gd->dialog), GTK_WIN_POS_MOUSE);
169 FileDialog *file_util_file_dlg(const gchar *title,
172 void (*cancel_cb)(FileDialog *, gpointer), gpointer data)
176 fdlg = file_dialog_new(title, role, parent, cancel_cb, data);
177 if (options->place_dialogs_under_mouse)
179 gtk_window_set_position(GTK_WINDOW(GENERIC_DIALOG(fdlg)->dialog), GTK_WIN_POS_MOUSE);
185 /* this warning dialog is copied from SLIK's ui_utildg.c,
186 * because it does not have a mouse center option,
187 * and we must center it before show, implement it here.
189 static void file_util_warning_dialog_ok_cb(GenericDialog *UNUSED(gd), gpointer UNUSED(data))
194 GenericDialog *file_util_warning_dialog(const gchar *heading, const gchar *message,
195 const gchar *icon_stock_id, GtkWidget *parent)
199 gd = file_util_gen_dlg(heading, "warning", parent, TRUE, NULL, NULL);
200 generic_dialog_add_message(gd, icon_stock_id, heading, message, TRUE);
201 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL, file_util_warning_dialog_ok_cb, TRUE);
202 if (options->place_dialogs_under_mouse)
204 gtk_window_set_position(GTK_WINDOW(gd->dialog), GTK_WIN_POS_MOUSE);
206 gtk_widget_show(gd->dialog);
211 static gint filename_base_length(const gchar *name)
219 if (filter_name_exists(name))
223 ext = registered_extension_from_path(name);
224 if (ext) n -= strlen(ext);
237 UTILITY_TYPE_RENAME_FOLDER,
241 UTILITY_TYPE_DELETE_LINK,
242 UTILITY_TYPE_DELETE_FOLDER,
243 UTILITY_TYPE_CREATE_FOLDER,
244 UTILITY_TYPE_WRITE_METADATA
248 UTILITY_PHASE_START = 0,
249 UTILITY_PHASE_INTERMEDIATE,
250 UTILITY_PHASE_ENTERING,
251 UTILITY_PHASE_CHECKED,
253 UTILITY_PHASE_CANCEL,
254 UTILITY_PHASE_DISCARD
260 UTILITY_RENAME_FORMATTED
263 typedef struct _UtilityDataMessages UtilityDataMessages;
264 struct _UtilityDataMessages {
268 gchar *desc_source_fd;
272 typedef struct _UtilityData UtilityData;
274 struct _UtilityData {
288 guint update_idle_id; /* event source id */
289 guint perform_idle_id; /* event source id */
291 gboolean with_sidecars; /* operate on grouped or single files; TRUE = use file_data_sc_, FALSE = use file_data_ functions */
293 /* alternative dialog parts */
296 UtilityDataMessages messages;
298 /* helper entries for various modes */
299 GtkWidget *rename_entry;
300 GtkWidget *rename_label;
301 GtkWidget *auto_entry_front;
302 GtkWidget *auto_entry_end;
303 GtkWidget *auto_spin_start;
304 GtkWidget *auto_spin_pad;
305 GtkWidget *format_entry;
306 GtkWidget *format_spin;
313 /* data for the operation itself, internal or external */
314 gboolean external; /* TRUE for external command, FALSE for internal */
316 gchar *external_command;
317 gpointer resume_data;
319 FileUtilDoneFunc done_func;
320 void (*details_func)(UtilityData *ud, FileData *fd);
321 gboolean (*finalize_func)(FileData *fd);
322 gboolean (*discard_func)(FileData *fd);
327 UTILITY_COLUMN_FD = 0,
328 UTILITY_COLUMN_PIXBUF,
331 UTILITY_COLUMN_SIDECARS,
332 UTILITY_COLUMN_DEST_PATH,
333 UTILITY_COLUMN_DEST_NAME,
337 typedef struct _UtilityDelayData UtilityDelayData;
339 struct _UtilityDelayData {
346 guint idle_id; /* event source id */
349 static void generic_dialog_image_set(UtilityData *ud, FileData *fd)
353 FileData *fd2 = NULL;
356 imd = g_object_get_data(G_OBJECT(ud->gd->dialog), "img_image");
357 label = g_object_get_data(G_OBJECT(ud->gd->dialog), "img_label");
361 image_change_fd(imd, fd, 0.0);
362 buf = g_strjoin("\n", text_from_time(fd->date), text_from_size(fd->size), NULL);
363 if (label) gtk_label_set_text(GTK_LABEL(label), buf);
366 if (ud->type == UTILITY_TYPE_RENAME || ud->type == UTILITY_TYPE_COPY || ud->type == UTILITY_TYPE_MOVE)
368 imd = g_object_get_data(G_OBJECT(ud->gd->dialog), "img_image2");
369 label = g_object_get_data(G_OBJECT(ud->gd->dialog), "img_label2");
373 if (isfile(fd->change->dest))
375 fd2 = file_data_new_group(fd->change->dest);
376 image_change_fd(imd, fd2, 0.0);
377 buf = g_strjoin("\n", text_from_time(fd2->date), text_from_size(fd2->size), NULL);
378 if (label && fd->change->dest) gtk_label_set_text(GTK_LABEL(label), buf);
379 file_data_unref(fd2);
384 image_change_fd(imd, NULL, 0.0);
385 if (label) gtk_label_set_text(GTK_LABEL(label), "");
391 static gboolean file_util_write_metadata_first(UtilityType type, UtilityPhase phase, GList *flist, const gchar *dest_path, const gchar *editor_key, GtkWidget *parent);
393 #define UTILITY_LIST_MIN_WIDTH 250
394 #define UTILITY_LIST_MIN_HEIGHT 150
396 /* thumbnail spec has a max depth of 4 (.thumb??/fail/appname/??.png) */
397 #define UTILITY_DELETE_MAX_DEPTH 5
399 static UtilityData *file_util_data_new(UtilityType type)
403 ud = g_new0(UtilityData, 1);
406 ud->phase = UTILITY_PHASE_START;
411 static void file_util_data_free(UtilityData *ud)
415 if (ud->update_idle_id) g_source_remove(ud->update_idle_id);
416 if (ud->perform_idle_id) g_source_remove(ud->perform_idle_id);
418 file_data_unref(ud->dir_fd);
419 filelist_free(ud->content_list);
420 filelist_free(ud->flist);
422 if (ud->gd) generic_dialog_close(ud->gd);
424 g_free(ud->dest_path);
425 g_free(ud->external_command);
430 static GtkTreeViewColumn *file_util_dialog_add_list_column(GtkWidget *view, const gchar *text, gboolean image, gint n)
432 GtkTreeViewColumn *column;
433 GtkCellRenderer *renderer;
435 column = gtk_tree_view_column_new();
436 gtk_tree_view_column_set_title(column, text);
437 gtk_tree_view_column_set_min_width(column, 4);
440 gtk_tree_view_column_set_min_width(column, 20);
441 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
442 renderer = gtk_cell_renderer_pixbuf_new();
443 gtk_tree_view_column_pack_start(column, renderer, TRUE);
444 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", n);
448 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
449 renderer = gtk_cell_renderer_text_new();
450 gtk_tree_view_column_pack_start(column, renderer, TRUE);
451 gtk_tree_view_column_add_attribute(column, renderer, "text", n);
453 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
458 static void file_util_dialog_list_select(GtkWidget *view, gint n)
462 GtkTreeSelection *selection;
464 store = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
465 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, n))
467 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
468 gtk_tree_selection_select_iter(selection, &iter);
472 static GtkWidget *file_util_dialog_add_list(GtkWidget *box, GList *list, gboolean full_paths, gboolean with_sidecars)
478 scrolled = gtk_scrolled_window_new(NULL, NULL);
479 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
480 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
481 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
482 gtk_box_pack_start(GTK_BOX(box), scrolled, TRUE, TRUE, 0);
483 gtk_widget_show(scrolled);
485 store = gtk_list_store_new(UTILITY_COLUMN_COUNT, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
486 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
487 g_object_unref(store);
489 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
490 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), FALSE);
492 file_util_dialog_add_list_column(view, "", TRUE, UTILITY_COLUMN_PIXBUF);
496 file_util_dialog_add_list_column(view, _("Path"), FALSE, UTILITY_COLUMN_PATH);
500 file_util_dialog_add_list_column(view, _("Name"), FALSE, UTILITY_COLUMN_NAME);
503 gtk_widget_set_size_request(view, UTILITY_LIST_MIN_WIDTH, UTILITY_LIST_MIN_HEIGHT);
504 gtk_container_add(GTK_CONTAINER(scrolled), view);
505 gtk_widget_show(view);
509 FileData *fd = list->data;
513 sidecars = with_sidecars ? file_data_sc_list_to_string(fd) : NULL;
514 GdkPixbuf *icon = file_util_get_error_icon(fd, list, view);
515 gtk_list_store_append(store, &iter);
516 gtk_list_store_set(store, &iter,
517 UTILITY_COLUMN_FD, fd,
518 UTILITY_COLUMN_PIXBUF, icon,
519 UTILITY_COLUMN_PATH, fd->path,
520 UTILITY_COLUMN_NAME, fd->name,
521 UTILITY_COLUMN_SIDECARS, sidecars,
522 UTILITY_COLUMN_DEST_PATH, fd->change ? fd->change->dest : "error",
523 UTILITY_COLUMN_DEST_NAME, fd->change ? filename_from_path(fd->change->dest) : "error",
534 static gboolean file_util_perform_ci_internal(gpointer data);
535 void file_util_dialog_run(UtilityData *ud);
536 static gint file_util_perform_ci_cb(gpointer resume_data, EditorFlags flags, GList *list, gpointer data);
538 /* call file_util_perform_ci_internal or start_editor_from_filelist_full */
541 static void file_util_resume_cb(GenericDialog *UNUSED(gd), gpointer data)
543 UtilityData *ud = data;
545 editor_resume(ud->resume_data);
547 file_util_perform_ci_internal(ud);
550 static void file_util_abort_cb(GenericDialog *UNUSED(gd), gpointer data)
552 UtilityData *ud = data;
554 editor_skip(ud->resume_data);
556 file_util_perform_ci_cb(NULL, EDITOR_ERROR_SKIPPED, ud->flist, ud);
561 static gint file_util_perform_ci_cb(gpointer resume_data, EditorFlags flags, GList *list, gpointer data)
563 UtilityData *ud = data;
564 gint ret = EDITOR_CB_CONTINUE;
566 ud->resume_data = resume_data;
568 if (EDITOR_ERRORS_BUT_SKIPPED(flags))
570 GString *msg = g_string_new(editor_get_error_str(flags));
572 g_string_append(msg, "\n");
573 g_string_append(msg, ud->messages.fail);
574 g_string_append(msg, "\n");
577 FileData *fd = list->data;
579 g_string_append(msg, fd->path);
580 g_string_append(msg, "\n");
585 g_string_append(msg, _("\n Continue multiple file operation?"));
586 d = file_util_gen_dlg(ud->messages.fail, "dlg_confirm",
588 file_util_abort_cb, ud);
590 generic_dialog_add_message(d, GTK_STOCK_DIALOG_WARNING, NULL, msg->str, TRUE);
592 generic_dialog_add_button(d, GTK_STOCK_GO_FORWARD, _("Co_ntinue"),
593 file_util_resume_cb, TRUE);
594 gtk_widget_show(d->dialog);
595 ret = EDITOR_CB_SUSPEND;
599 file_util_warning_dialog(ud->messages.fail, msg->str, GTK_STOCK_DIALOG_ERROR, NULL);
601 g_string_free(msg, TRUE);
605 while (list) /* be careful, file_util_perform_ci_internal can pass ud->flist as list */
607 FileData *fd = list->data;
610 if (!EDITOR_ERRORS(flags)) /* files were successfully deleted, call the maint functions */
612 if (ud->with_sidecars)
613 file_data_sc_apply_ci(fd);
615 file_data_apply_ci(fd);
618 ud->flist = g_list_remove(ud->flist, fd);
620 if (ud->finalize_func)
622 ud->finalize_func(fd);
625 if (ud->with_sidecars)
626 file_data_sc_free_ci(fd);
628 file_data_free_ci(fd);
632 if (!resume_data) /* end of the list */
634 ud->phase = UTILITY_PHASE_DONE;
635 file_util_dialog_run(ud);
643 * Perform the operation described by FileDataChangeInfo on all files in the list
644 * it is an alternative to start_editor_from_filelist_full, it should use similar interface
648 static gboolean file_util_perform_ci_internal(gpointer data)
650 UtilityData *ud = data;
652 if (!ud->perform_idle_id)
654 /* this function was called directly
655 just setup idle callback and wait until we are called again
658 /* this is removed when ud is destroyed */
659 ud->perform_idle_id = g_idle_add(file_util_perform_ci_internal, ud);
669 /* take a single entry each time, this allows better control over the operation */
670 GList *single_entry = g_list_append(NULL, ud->flist->data);
671 gboolean last = !ud->flist->next;
672 EditorFlags status = EDITOR_ERROR_STATUS;
674 if (ud->with_sidecars ? file_data_sc_perform_ci(single_entry->data)
675 : file_data_perform_ci(single_entry->data))
678 ret = file_util_perform_ci_cb(GINT_TO_POINTER(!last), status, single_entry, ud);
679 g_list_free(single_entry);
681 if (ret == EDITOR_CB_SUSPEND || last) return FALSE;
683 if (ret == EDITOR_CB_SKIP)
685 file_util_perform_ci_cb(NULL, EDITOR_ERROR_SKIPPED, ud->flist, ud);
693 static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolean ext_result)
697 case UTILITY_TYPE_DELETE_LINK:
699 g_assert(ud->dir_fd->sidecar_files == NULL); // directories should not have sidecars
700 if ((internal && file_data_perform_ci(ud->dir_fd)) ||
701 (!internal && ext_result))
703 file_data_apply_ci(ud->dir_fd);
709 text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
710 file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
713 file_data_free_ci(ud->dir_fd);
716 case UTILITY_TYPE_DELETE_FOLDER:
718 FileData *fail = NULL;
720 work = ud->content_list;
730 if ((internal && file_data_sc_perform_ci(fd)) ||
731 (!internal && ext_result))
733 file_data_sc_apply_ci(fd);
737 if (internal) fail = file_data_ref(fd);
740 file_data_sc_free_ci(fd);
745 g_assert(ud->dir_fd->sidecar_files == NULL); // directories should not have sidecars
746 if ((internal && file_data_sc_perform_ci(ud->dir_fd)) ||
747 (!internal && ext_result))
749 file_data_apply_ci(ud->dir_fd);
753 fail = file_data_ref(ud->dir_fd);
762 text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
763 gd = file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
766 if (fail != ud->dir_fd)
768 pref_spacer(gd->vbox, PREF_PAD_GROUP);
769 text = g_strdup_printf(_("Removal of folder contents failed at this file:\n\n%s"),
771 pref_label_new(gd->vbox, text);
775 file_data_unref(fail);
779 case UTILITY_TYPE_RENAME_FOLDER:
781 FileData *fail = NULL;
783 g_assert(ud->dir_fd->sidecar_files == NULL); // directories should not have sidecars
785 if ((internal && file_data_sc_perform_ci(ud->dir_fd)) ||
786 (!internal && ext_result))
788 file_data_sc_apply_ci(ud->dir_fd);
792 fail = file_data_ref(ud->dir_fd);
796 work = ud->content_list;
806 file_data_sc_apply_ci(fd);
808 file_data_sc_free_ci(fd);
815 text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
816 (void) file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
819 file_data_unref(fail);
823 case UTILITY_TYPE_CREATE_FOLDER:
825 if ((internal && mkdir_utf8(ud->dir_fd->path, 0755)) ||
826 (!internal && ext_result))
828 file_data_check_changed_files(ud->dir_fd); /* this will update the FileData and send notification */
834 text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
835 (void) file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
842 g_warning("unhandled operation");
844 ud->phase = UTILITY_PHASE_DONE;
845 file_util_dialog_run(ud);
848 static gint file_util_perform_ci_dir_cb(gpointer UNUSED(resume_data), EditorFlags flags, GList *UNUSED(list), gpointer data)
850 UtilityData *ud = data;
851 file_util_perform_ci_dir(ud, FALSE, !EDITOR_ERRORS_BUT_SKIPPED(flags));
852 return EDITOR_CB_CONTINUE; /* does not matter, there was just single directory */
855 void file_util_perform_ci(UtilityData *ud)
859 case UTILITY_TYPE_COPY:
860 ud->external_command = g_strdup(CMD_COPY);
862 case UTILITY_TYPE_MOVE:
863 ud->external_command = g_strdup(CMD_MOVE);
865 case UTILITY_TYPE_RENAME:
866 case UTILITY_TYPE_RENAME_FOLDER:
867 ud->external_command = g_strdup(CMD_RENAME);
869 case UTILITY_TYPE_DELETE:
870 case UTILITY_TYPE_DELETE_LINK:
871 case UTILITY_TYPE_DELETE_FOLDER:
872 ud->external_command = g_strdup(CMD_DELETE);
874 case UTILITY_TYPE_CREATE_FOLDER:
875 ud->external_command = g_strdup(CMD_FOLDER);
877 case UTILITY_TYPE_FILTER:
878 case UTILITY_TYPE_EDITOR:
879 g_assert(ud->external_command != NULL); /* it should be already set */
881 case UTILITY_TYPE_WRITE_METADATA:
882 ud->external_command = NULL;
885 if (is_valid_editor_command(ud->external_command))
893 flags = start_editor_from_file_full(ud->external_command, ud->dir_fd, file_util_perform_ci_dir_cb, ud);
897 if (editor_blocks_file(ud->external_command))
899 DEBUG_1("Starting %s and waiting for results", ud->external_command);
900 flags = start_editor_from_filelist_full(ud->external_command, ud->flist, NULL, file_util_perform_ci_cb, ud);
904 /* start the editor without callback and finish the operation internally */
905 DEBUG_1("Starting %s and finishing the operation", ud->external_command);
906 flags = start_editor_from_filelist(ud->external_command, ud->flist);
907 file_util_perform_ci_internal(ud);
911 if (EDITOR_ERRORS(flags))
913 gchar *text = g_strdup_printf(_("%s\nUnable to start external command.\n"), editor_get_error_str(flags));
914 file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
918 ud->phase = UTILITY_PHASE_CANCEL;
919 file_util_dialog_run(ud);
924 ud->external = FALSE;
927 file_util_perform_ci_dir(ud, TRUE, FALSE);
931 file_util_perform_ci_internal(ud);
936 static GdkPixbuf *file_util_get_error_icon(FileData *fd, GList *list, GtkWidget *widget)
938 static GdkPixbuf *pb_warning;
939 static GdkPixbuf *pb_error;
940 static GdkPixbuf *pb_apply;
945 pb_warning = gtk_widget_render_icon(widget, GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_MENU, NULL);
950 pb_error = gtk_widget_render_icon(widget, GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_MENU, NULL);
955 pb_apply = gtk_widget_render_icon(widget, GTK_STOCK_APPLY, GTK_ICON_SIZE_MENU, NULL);
958 error = file_data_sc_verify_ci(fd, list);
960 if (!error) return pb_apply;
962 if (error & CHANGE_ERROR_MASK)
972 static void file_util_check_resume_cb(GenericDialog *UNUSED(gd), gpointer data)
974 UtilityData *ud = data;
975 ud->phase = UTILITY_PHASE_CHECKED;
976 file_util_dialog_run(ud);
979 static void file_util_check_abort_cb(GenericDialog *UNUSED(gd), gpointer data)
981 UtilityData *ud = data;
982 ud->phase = UTILITY_PHASE_START;
983 file_util_dialog_run(ud);
986 void file_util_check_ci(UtilityData *ud)
988 gint error = CHANGE_OK;
991 if (ud->type != UTILITY_TYPE_CREATE_FOLDER &&
992 ud->type != UTILITY_TYPE_RENAME_FOLDER)
994 if (ud->dest_path && !isdir(ud->dest_path))
996 error = CHANGE_GENERIC_ERROR;
997 desc = g_strdup_printf(_("%s is not a directory"), ud->dest_path);
1001 g_assert(ud->dir_fd->sidecar_files == NULL); // directories should not have sidecars
1002 error = file_data_verify_ci(ud->dir_fd, ud->flist);
1003 if (error) desc = file_data_get_error_string(error);
1007 error = file_data_verify_ci_list(ud->flist, &desc, ud->with_sidecars);
1012 if (ud->type == UTILITY_TYPE_CREATE_FOLDER || ud->type == UTILITY_TYPE_RENAME_FOLDER)
1014 if (isdir(ud->dest_path) || isfile(ud->dest_path))
1016 error = CHANGE_DEST_EXISTS;
1017 desc = g_strdup_printf(_("%s already exists"), ud->dest_path);
1024 ud->phase = UTILITY_PHASE_CHECKED;
1025 file_util_dialog_run(ud);
1029 if (!(error & CHANGE_ERROR_MASK))
1031 /* just a warning */
1034 d = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1036 file_util_check_abort_cb, ud);
1038 generic_dialog_add_message(d, GTK_STOCK_DIALOG_WARNING, _("Really continue?"), desc, TRUE);
1040 generic_dialog_add_button(d, GTK_STOCK_GO_FORWARD, _("Co_ntinue"),
1041 file_util_check_resume_cb, TRUE);
1042 gtk_widget_show(d->dialog);
1049 d = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1051 file_util_check_abort_cb, ud);
1052 generic_dialog_add_message(d, GTK_STOCK_DIALOG_WARNING, _("This operation can't continue:"), desc, TRUE);
1054 gtk_widget_show(d->dialog);
1063 static void file_util_cancel_cb(GenericDialog *gd, gpointer data)
1065 UtilityData *ud = data;
1067 generic_dialog_close(gd);
1071 ud->phase = UTILITY_PHASE_CANCEL;
1072 file_util_dialog_run(ud);
1075 static void file_util_discard_cb(GenericDialog *gd, gpointer data)
1077 UtilityData *ud = data;
1079 generic_dialog_close(gd);
1083 ud->phase = UTILITY_PHASE_DISCARD;
1084 file_util_dialog_run(ud);
1087 static void file_util_ok_cb(GenericDialog *gd, gpointer data)
1089 UtilityData *ud = data;
1091 generic_dialog_close(gd);
1095 file_util_dialog_run(ud);
1098 static void file_util_fdlg_cancel_cb(FileDialog *fdlg, gpointer data)
1100 UtilityData *ud = data;
1102 file_dialog_close(fdlg);
1106 ud->phase = UTILITY_PHASE_CANCEL;
1107 file_util_dialog_run(ud);
1110 static void file_util_dest_folder_update_path(UtilityData *ud)
1112 g_free(ud->dest_path);
1113 ud->dest_path = g_strdup(gtk_entry_get_text(GTK_ENTRY(ud->fdlg->entry)));
1117 case UTILITY_TYPE_COPY:
1118 file_data_sc_update_ci_copy_list(ud->flist, ud->dest_path);
1120 case UTILITY_TYPE_MOVE:
1121 file_data_sc_update_ci_move_list(ud->flist, ud->dest_path);
1123 case UTILITY_TYPE_FILTER:
1124 case UTILITY_TYPE_EDITOR:
1125 file_data_sc_update_ci_unspecified_list(ud->flist, ud->dest_path);
1127 case UTILITY_TYPE_CREATE_FOLDER:
1128 file_data_unref(ud->dir_fd);
1129 ud->dir_fd = file_data_new_dir(ud->dest_path);
1131 case UTILITY_TYPE_DELETE:
1132 case UTILITY_TYPE_DELETE_LINK:
1133 case UTILITY_TYPE_DELETE_FOLDER:
1134 case UTILITY_TYPE_RENAME:
1135 case UTILITY_TYPE_RENAME_FOLDER:
1136 case UTILITY_TYPE_WRITE_METADATA:
1137 g_warning("unhandled operation");
1141 static void file_util_fdlg_rename_cb(FileDialog *fdlg, gpointer data)
1143 UtilityData *ud = data;
1145 GenericDialog *d = NULL;
1147 file_util_dest_folder_update_path(ud);
1148 if (isdir(ud->dest_path))
1150 file_dialog_sync_history(fdlg, TRUE);
1151 file_dialog_close(fdlg);
1153 file_util_dialog_run(ud);
1157 /* During copy/move operations it is necessary to ensure that the
1158 * target directory exists before continuing with the next step.
1159 * If not revert to the select directory dialog
1161 desc = g_strdup_printf(_("%s is not a directory"), ud->dest_path);
1163 d = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1165 file_util_check_abort_cb, ud);
1166 generic_dialog_add_message(d, GTK_STOCK_DIALOG_WARNING, _("This operation can't continue:"), desc, TRUE);
1168 gtk_widget_show(d->dialog);
1169 ud->phase = UTILITY_PHASE_START;
1171 file_dialog_close(fdlg);
1177 static void file_util_fdlg_ok_cb(FileDialog *fdlg, gpointer data)
1179 UtilityData *ud = data;
1181 file_util_dest_folder_update_path(ud);
1182 if (isdir(ud->dest_path)) file_dialog_sync_history(fdlg, TRUE);
1183 file_dialog_close(fdlg);
1186 ud->phase = UTILITY_PHASE_ENTERING;
1188 file_util_dialog_run(ud);
1193 static void file_util_dest_folder_entry_cb(GtkWidget *UNUSED(entry), gpointer data)
1195 UtilityData *ud = data;
1196 file_util_dest_folder_update_path(ud);
1199 /* format: * = filename without extension, ## = number position, extension is kept */
1200 static gchar *file_util_rename_multiple_auto_format_name(const gchar *format, const gchar *name, gint n)
1211 if (!format || !name) return NULL;
1213 tmp = g_strdup(format);
1214 pad_start = strchr(tmp, '#');
1217 pad_end = pad_start;
1219 while (*pad_end == '#')
1226 parsed = g_strdup_printf("%s%0*d%s", tmp, padding, n, pad_end);
1234 ext = registered_extension_from_path(name);
1236 middle = strchr(parsed, '*');
1244 base = remove_extension_from_path(name);
1245 new_name = g_strconcat(parsed, base, middle, ext, NULL);
1250 new_name = g_strconcat(parsed, ext, NULL);
1259 static void file_util_rename_preview_update(UtilityData *ud)
1261 GtkTreeModel *store;
1262 GtkTreeSelection *selection;
1263 GtkTreeIter iter, iter_selected;
1264 GtkTreePath *path_iter, *path_selected;
1267 const gchar *format;
1276 mode = gtk_notebook_get_current_page(GTK_NOTEBOOK(ud->notebook));
1278 if (mode == UTILITY_RENAME)
1280 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1281 if (gtk_tree_selection_get_selected(selection, &store, &iter))
1284 const gchar *dest = gtk_entry_get_text(GTK_ENTRY(ud->rename_entry));
1286 gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
1287 g_assert(ud->with_sidecars); /* sidecars must be renamed too, it would break the pairing otherwise */
1289 dirname = g_path_get_dirname(fd->change->dest);
1290 destname = g_build_filename(dirname, dest, NULL);
1293 case UTILITY_TYPE_RENAME:
1294 file_data_sc_update_ci_rename(fd, dest);
1296 case UTILITY_TYPE_COPY:
1297 file_data_sc_update_ci_copy(fd, destname);
1299 case UTILITY_TYPE_MOVE:
1300 file_data_sc_update_ci_move(fd, destname);
1304 generic_dialog_image_set(ud, fd);
1306 gtk_list_store_set(GTK_LIST_STORE(store), &iter,
1307 UTILITY_COLUMN_DEST_PATH, fd->change->dest,
1308 UTILITY_COLUMN_DEST_NAME, filename_from_path(fd->change->dest),
1314 front = gtk_entry_get_text(GTK_ENTRY(ud->auto_entry_front));
1315 end = gtk_entry_get_text(GTK_ENTRY(ud->auto_entry_end));
1316 padding = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ud->auto_spin_pad));
1318 format = gtk_entry_get_text(GTK_ENTRY(ud->format_entry));
1320 g_free(options->cp_mv_rn.auto_end);
1321 options->cp_mv_rn.auto_end = g_strdup(end);
1322 options->cp_mv_rn.auto_padding = padding;
1324 if (mode == UTILITY_RENAME_FORMATTED)
1326 start_n = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ud->format_spin));
1327 options->cp_mv_rn.formatted_start = start_n;
1331 start_n = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ud->auto_spin_start));
1332 options->cp_mv_rn.auto_start = start_n;
1335 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ud->listview));
1337 valid = gtk_tree_model_get_iter_first(store, &iter);
1342 gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
1344 if (mode == UTILITY_RENAME_FORMATTED)
1346 dest = file_util_rename_multiple_auto_format_name(format, fd->name, n);
1350 dest = g_strdup_printf("%s%0*d%s", front, padding, n, end);
1353 g_assert(ud->with_sidecars); /* sidecars must be renamed too, it would break the pairing otherwise */
1355 dirname = g_path_get_dirname(fd->change->dest);
1356 destname = g_build_filename(dirname, dest, NULL);
1360 case UTILITY_TYPE_RENAME:
1361 file_data_sc_update_ci_rename(fd, dest);
1363 case UTILITY_TYPE_COPY:
1364 file_data_sc_update_ci_copy(fd, destname);
1366 case UTILITY_TYPE_MOVE:
1367 file_data_sc_update_ci_move(fd, destname);
1376 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1377 gtk_tree_selection_get_selected(selection, &store, &iter_selected);
1378 path_iter=gtk_tree_model_get_path(store,&iter);
1379 path_selected=gtk_tree_model_get_path(store,&iter_selected);
1380 if (!gtk_tree_path_compare(path_iter,path_selected))
1382 generic_dialog_image_set(ud, fd);
1384 gtk_tree_path_free(path_iter);
1385 gtk_tree_path_free(path_selected);
1387 gtk_list_store_set(GTK_LIST_STORE(store), &iter,
1388 UTILITY_COLUMN_DEST_PATH, fd->change->dest,
1389 UTILITY_COLUMN_DEST_NAME, filename_from_path(fd->change->dest),
1392 valid = gtk_tree_model_iter_next(store, &iter);
1396 /* Check the other entries in the list - if there are
1397 * multiple destination filenames with the same name the
1398 * error icons must be updated
1400 valid = gtk_tree_model_get_iter_first(store, &iter);
1404 gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
1406 gtk_list_store_set(GTK_LIST_STORE(store), &iter,
1407 UTILITY_COLUMN_PIXBUF, file_util_get_error_icon(fd, ud->flist, ud->listview),
1409 valid = gtk_tree_model_iter_next(store, &iter);
1414 static void file_util_rename_preview_entry_cb(GtkWidget *UNUSED(entry), gpointer data)
1416 UtilityData *ud = data;
1417 file_util_rename_preview_update(ud);
1420 static void file_util_rename_preview_adj_cb(GtkWidget *UNUSED(spin), gpointer data)
1422 UtilityData *ud = data;
1423 file_util_rename_preview_update(ud);
1426 static gboolean file_util_rename_idle_cb(gpointer data)
1428 UtilityData *ud = data;
1430 file_util_rename_preview_update(ud);
1432 ud->update_idle_id = 0;
1436 static void file_util_rename_preview_order_cb(GtkTreeModel *UNUSED(treemodel), GtkTreePath *UNUSED(tpath),
1437 GtkTreeIter *UNUSED(iter), gpointer data)
1439 UtilityData *ud = data;
1441 if (ud->update_idle_id) return;
1443 ud->update_idle_id = g_idle_add(file_util_rename_idle_cb, ud);
1447 static gboolean file_util_preview_cb(GtkTreeSelection *UNUSED(selection), GtkTreeModel *store,
1448 GtkTreePath *tpath, gboolean path_currently_selected,
1451 UtilityData *ud = data;
1453 FileData *fd = NULL;
1455 if (path_currently_selected ||
1456 !gtk_tree_model_get_iter(store, &iter, tpath)) return TRUE;
1458 gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
1459 generic_dialog_image_set(ud, fd);
1463 if (ud->type == UTILITY_TYPE_RENAME || ud->type == UTILITY_TYPE_COPY || ud->type == UTILITY_TYPE_MOVE)
1465 const gchar *name = filename_from_path(fd->change->dest);
1467 gtk_widget_grab_focus(ud->rename_entry);
1468 gtk_label_set_text(GTK_LABEL(ud->rename_label), fd->name);
1469 g_signal_handlers_block_by_func(ud->rename_entry, (gpointer)(file_util_rename_preview_entry_cb), ud);
1470 gtk_entry_set_text(GTK_ENTRY(ud->rename_entry), name);
1471 gtk_editable_select_region(GTK_EDITABLE(ud->rename_entry), 0, filename_base_length(name));
1472 g_signal_handlers_unblock_by_func(ud->rename_entry, (gpointer)file_util_rename_preview_entry_cb, ud);
1480 static void box_append_safe_delete_status(GenericDialog *gd)
1485 buf = file_util_safe_delete_status();
1486 label = pref_label_new(gd->vbox, buf);
1489 gtk_label_set_xalign(GTK_LABEL(label), 1.0);
1490 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
1491 gtk_widget_set_sensitive(label, FALSE);
1494 static void file_util_details_cb(GenericDialog *UNUSED(gd), gpointer data)
1496 UtilityData *ud = data;
1497 if (ud->details_func && ud->sel_fd)
1499 ud->details_func(ud, ud->sel_fd);
1503 static void file_util_dialog_init_simple_list(UtilityData *ud)
1506 GtkTreeSelection *selection;
1509 const gchar *stock_id;
1511 /** @FIXME use ud->stock_id */
1512 if (ud->type == UTILITY_TYPE_DELETE ||
1513 ud->type == UTILITY_TYPE_DELETE_LINK ||
1514 ud->type == UTILITY_TYPE_DELETE_FOLDER)
1516 stock_id = GTK_STOCK_DELETE;
1520 stock_id = GTK_STOCK_OK;
1523 ud->gd = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1524 ud->parent, FALSE, file_util_cancel_cb, ud);
1525 if (ud->discard_func) generic_dialog_add_button(ud->gd, GTK_STOCK_REVERT_TO_SAVED, _("Discard changes"), file_util_discard_cb, FALSE);
1526 if (ud->details_func) generic_dialog_add_button(ud->gd, GTK_STOCK_INFO, _("File details"), file_util_details_cb, FALSE);
1528 generic_dialog_add_button(ud->gd, stock_id, NULL, file_util_ok_cb, TRUE);
1532 dir_msg = g_strdup_printf("%s\n\n%s\n", ud->messages.desc_source_fd, ud->dir_fd->path);
1536 dir_msg = g_strdup("");
1539 box = generic_dialog_add_message(ud->gd, GTK_STOCK_DIALOG_QUESTION,
1540 ud->messages.question,
1545 box = pref_group_new(box, TRUE, ud->messages.desc_flist, GTK_ORIENTATION_HORIZONTAL);
1547 ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE, ud->with_sidecars);
1548 if (ud->with_sidecars) file_util_dialog_add_list_column(ud->listview, _("Sidecars"), FALSE, UTILITY_COLUMN_SIDECARS);
1550 if (ud->type == UTILITY_TYPE_WRITE_METADATA) file_util_dialog_add_list_column(ud->listview, _("Write to file"), FALSE, UTILITY_COLUMN_DEST_NAME);
1552 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1553 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
1554 gtk_tree_selection_set_select_function(selection, file_util_preview_cb, ud, NULL);
1556 generic_dialog_add_image(ud->gd, box, NULL, NULL, FALSE, NULL, NULL, FALSE);
1558 if (ud->type == UTILITY_TYPE_DELETE ||
1559 ud->type == UTILITY_TYPE_DELETE_LINK ||
1560 ud->type == UTILITY_TYPE_DELETE_FOLDER)
1561 box_append_safe_delete_status(ud->gd);
1563 gtk_widget_show(ud->gd->dialog);
1565 file_util_dialog_list_select(ud->listview, 0);
1568 static void file_util_dialog_init_dest_folder(UtilityData *ud)
1572 const gchar *stock_id;
1574 if (ud->type == UTILITY_TYPE_COPY)
1576 stock_id = GTK_STOCK_COPY;
1580 stock_id = GTK_STOCK_OK;
1583 fdlg = file_util_file_dlg(ud->messages.title, "dlg_dest_folder", ud->parent,
1584 file_util_fdlg_cancel_cb, ud);
1588 generic_dialog_add_message(GENERIC_DIALOG(fdlg), NULL, ud->messages.question, NULL, FALSE);
1590 label = pref_label_new(GENERIC_DIALOG(fdlg)->vbox, _("Choose the destination folder."));
1591 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
1592 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
1594 pref_spacer(GENERIC_DIALOG(fdlg)->vbox, 0);
1596 if (options->with_rename)
1598 file_dialog_add_button(fdlg, stock_id, ud->messages.title, file_util_fdlg_ok_cb, TRUE);
1599 file_dialog_add_button(fdlg, GTK_STOCK_EDIT, "With Rename", file_util_fdlg_rename_cb, TRUE);
1603 file_dialog_add_button(fdlg, GTK_STOCK_EDIT, "With Rename", file_util_fdlg_rename_cb, TRUE);
1604 file_dialog_add_button(fdlg, stock_id, ud->messages.title, file_util_fdlg_ok_cb, TRUE);
1607 file_dialog_add_path_widgets(fdlg, NULL, ud->dest_path, "move_copy", NULL, NULL);
1609 g_signal_connect(G_OBJECT(fdlg->entry), "changed",
1610 G_CALLBACK(file_util_dest_folder_entry_cb), ud);
1612 gtk_widget_show(GENERIC_DIALOG(fdlg)->dialog);
1616 static GtkWidget *furm_simple_vlabel(GtkWidget *box, const gchar *text, gboolean expand)
1621 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1622 gtk_box_pack_start(GTK_BOX(box), vbox, expand, expand, 0);
1623 gtk_widget_show(vbox);
1625 label = gtk_label_new(text);
1626 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1627 gtk_widget_show(label);
1633 static void file_util_dialog_init_source_dest(UtilityData *ud, gboolean second_image)
1635 GtkTreeModel *store;
1636 GtkTreeSelection *selection;
1643 gchar *destination_message;
1645 ud->gd = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1646 ud->parent, FALSE, file_util_cancel_cb, ud);
1648 box = generic_dialog_add_message(ud->gd, NULL, ud->messages.question, NULL, TRUE);
1650 if (ud->discard_func) generic_dialog_add_button(ud->gd, GTK_STOCK_REVERT_TO_SAVED, _("Discard changes"), file_util_discard_cb, FALSE);
1651 if (ud->details_func) generic_dialog_add_button(ud->gd, GTK_STOCK_INFO, _("File details"), file_util_details_cb, FALSE);
1653 generic_dialog_add_button(ud->gd, GTK_STOCK_OK, ud->messages.title, file_util_ok_cb, TRUE);
1655 if (ud->type == UTILITY_TYPE_COPY || ud->type == UTILITY_TYPE_MOVE)
1657 destination_message = g_strconcat(ud->messages.desc_flist," to: ", ud->dest_path, NULL);
1661 destination_message = g_strdup(ud->messages.desc_flist);
1664 box = pref_group_new(box, TRUE, destination_message, GTK_ORIENTATION_HORIZONTAL);
1665 g_free(destination_message);
1667 ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE, ud->with_sidecars);
1668 file_util_dialog_add_list_column(ud->listview, _("Sidecars"), FALSE, UTILITY_COLUMN_SIDECARS);
1670 file_util_dialog_add_list_column(ud->listview, _("New name"), FALSE, UTILITY_COLUMN_DEST_NAME);
1672 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1673 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_SINGLE);
1674 gtk_tree_selection_set_select_function(selection, file_util_preview_cb, ud, NULL);
1677 // column = file_util_rename_multiple_add_column(rd, _("Preview"), RENAME_COLUMN_PREVIEW);
1678 // gtk_tree_view_column_set_visible(column, FALSE);
1680 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ud->listview), TRUE);
1682 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ud->listview));
1683 g_signal_connect(G_OBJECT(store), "row_changed",
1684 G_CALLBACK(file_util_rename_preview_order_cb), ud);
1685 gtk_widget_set_size_request(ud->listview, 300, 150);
1689 generic_dialog_add_image(ud->gd, box, NULL, "Source", TRUE, NULL, "Destination", TRUE);
1693 generic_dialog_add_image(ud->gd, box, NULL, NULL, FALSE, NULL, NULL, FALSE);
1696 // gtk_container_add(GTK_CONTAINER(scrolled), view);
1697 gtk_widget_show(ud->gd->dialog);
1700 ud->notebook = gtk_notebook_new();
1702 gtk_box_pack_start(GTK_BOX(ud->gd->vbox), ud->notebook, FALSE, FALSE, 0);
1703 gtk_widget_show(ud->notebook);
1706 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
1707 gtk_notebook_append_page(GTK_NOTEBOOK(ud->notebook), page, gtk_label_new(_("Manual rename")));
1708 gtk_widget_show(page);
1710 table = pref_table_new(page, 2, 2, FALSE, FALSE);
1712 pref_table_label(table, 0, 0, _("Original name:"), 1.0);
1713 ud->rename_label = pref_table_label(table, 1, 0, "", 0.0);
1715 pref_table_label(table, 0, 1, _("New name:"), 1.0);
1717 ud->rename_entry = gtk_entry_new();
1718 gtk_table_attach(GTK_TABLE(table), ud->rename_entry, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 0, 0);
1719 generic_dialog_attach_default(GENERIC_DIALOG(ud->gd), ud->rename_entry);
1720 gtk_widget_grab_focus(ud->rename_entry);
1722 g_signal_connect(G_OBJECT(ud->rename_entry), "changed",
1723 G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1725 gtk_widget_show(ud->rename_entry);
1727 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
1728 gtk_notebook_append_page(GTK_NOTEBOOK(ud->notebook), page, gtk_label_new(_("Auto rename")));
1729 gtk_widget_show(page);
1732 hbox = pref_box_new(page, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1734 box2 = furm_simple_vlabel(hbox, _("Begin text"), TRUE);
1736 combo = history_combo_new(&ud->auto_entry_front, "", "numerical_rename_prefix", -1);
1737 g_signal_connect(G_OBJECT(ud->auto_entry_front), "changed",
1738 G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1739 gtk_box_pack_start(GTK_BOX(box2), combo, TRUE, TRUE, 0);
1740 gtk_widget_show(combo);
1742 box2 = furm_simple_vlabel(hbox, _("Start #"), FALSE);
1744 ud->auto_spin_start = pref_spin_new(box2, NULL, NULL,
1745 0.0, 1000000.0, 1.0, 0, options->cp_mv_rn.auto_start,
1746 G_CALLBACK(file_util_rename_preview_adj_cb), ud);
1748 box2 = furm_simple_vlabel(hbox, _("End text"), TRUE);
1750 combo = history_combo_new(&ud->auto_entry_end, options->cp_mv_rn.auto_end, "numerical_rename_suffix", -1);
1751 g_signal_connect(G_OBJECT(ud->auto_entry_end), "changed",
1752 G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1753 gtk_box_pack_start(GTK_BOX(box2), combo, TRUE, TRUE, 0);
1754 gtk_widget_show(combo);
1756 ud->auto_spin_pad = pref_spin_new(page, _("Padding:"), NULL,
1757 1.0, 8.0, 1.0, 0, options->cp_mv_rn.auto_padding,
1758 G_CALLBACK(file_util_rename_preview_adj_cb), ud);
1760 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
1761 gtk_notebook_append_page(GTK_NOTEBOOK(ud->notebook), page, gtk_label_new(_("Formatted rename")));
1762 gtk_widget_show(page);
1764 hbox = pref_box_new(page, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1766 box2 = furm_simple_vlabel(hbox, _("Format (* = original name, ## = numbers)"), TRUE);
1768 combo = history_combo_new(&ud->format_entry, "", "auto_rename_format", -1);
1769 g_signal_connect(G_OBJECT(ud->format_entry), "changed",
1770 G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1771 gtk_box_pack_start(GTK_BOX(box2), combo, TRUE, TRUE, 0);
1772 gtk_widget_show(combo);
1774 box2 = furm_simple_vlabel(hbox, _("Start #"), FALSE);
1776 ud->format_spin = pref_spin_new(box2, NULL, NULL,
1777 0.0, 1000000.0, 1.0, 0, options->cp_mv_rn.formatted_start,
1778 G_CALLBACK(file_util_rename_preview_adj_cb), ud);
1780 // gtk_combo_box_set_active(GTK_COMBO_BOX(ud->combo_type), 0); /* callback will take care of the rest */
1782 file_util_dialog_list_select(ud->listview, 0);
1785 static void file_util_finalize_all(UtilityData *ud)
1787 GList *work = ud->flist;
1789 if (ud->phase == UTILITY_PHASE_CANCEL) return;
1790 if (ud->phase == UTILITY_PHASE_DONE && !ud->finalize_func) return;
1791 if (ud->phase == UTILITY_PHASE_DISCARD && !ud->discard_func) return;
1795 FileData *fd = work->data;
1797 if (ud->phase == UTILITY_PHASE_DONE) ud->finalize_func(fd);
1798 else if (ud->phase == UTILITY_PHASE_DISCARD) ud->discard_func(fd);
1802 static gboolean file_util_exclude_fd(UtilityData *ud, FileData *fd)
1804 GtkTreeModel *store;
1808 if (!g_list_find(ud->flist, fd)) return FALSE;
1810 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ud->listview));
1811 valid = gtk_tree_model_get_iter_first(store, &iter);
1815 gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &store_fd, -1);
1819 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1822 valid = gtk_tree_model_iter_next(store, &iter);
1825 ud->flist = g_list_remove(ud->flist, fd);
1827 if (ud->with_sidecars)
1828 file_data_sc_free_ci(fd);
1830 file_data_free_ci(fd);
1832 file_data_unref(fd);
1836 void file_util_dialog_run(UtilityData *ud)
1840 case UTILITY_PHASE_START:
1841 /* create the dialogs */
1844 case UTILITY_TYPE_DELETE:
1845 case UTILITY_TYPE_DELETE_LINK:
1846 case UTILITY_TYPE_DELETE_FOLDER:
1847 case UTILITY_TYPE_EDITOR:
1848 case UTILITY_TYPE_WRITE_METADATA:
1849 file_util_dialog_init_simple_list(ud);
1850 ud->phase = UTILITY_PHASE_ENTERING;
1852 case UTILITY_TYPE_RENAME:
1853 file_util_dialog_init_source_dest(ud, TRUE);
1854 ud->phase = UTILITY_PHASE_ENTERING;
1856 case UTILITY_TYPE_COPY:
1857 case UTILITY_TYPE_MOVE:
1858 file_util_dialog_init_dest_folder(ud);
1859 ud->phase = UTILITY_PHASE_INTERMEDIATE;
1861 case UTILITY_TYPE_FILTER:
1862 case UTILITY_TYPE_CREATE_FOLDER:
1863 file_util_dialog_init_dest_folder(ud);
1864 ud->phase = UTILITY_PHASE_ENTERING;
1866 case UTILITY_TYPE_RENAME_FOLDER:
1867 ud->phase = UTILITY_PHASE_CANCEL; /**< @FIXME not handled for now */
1868 file_util_dialog_run(ud);
1872 case UTILITY_PHASE_INTERMEDIATE:
1875 case UTILITY_TYPE_COPY:
1876 case UTILITY_TYPE_MOVE:
1877 file_util_dialog_init_source_dest(ud, TRUE);
1881 ud->phase = UTILITY_PHASE_ENTERING;
1883 case UTILITY_PHASE_ENTERING:
1884 file_util_check_ci(ud);
1886 case UTILITY_PHASE_CHECKED:
1887 file_util_perform_ci(ud);
1889 case UTILITY_PHASE_CANCEL:
1890 case UTILITY_PHASE_DONE:
1891 case UTILITY_PHASE_DISCARD:
1893 file_util_finalize_all(ud);
1895 /* both DISCARD and DONE finishes the operation for good */
1897 ud->done_func((ud->phase != UTILITY_PHASE_CANCEL), ud->dest_path, ud->done_data);
1899 if (ud->with_sidecars)
1900 file_data_sc_free_ci_list(ud->flist);
1902 file_data_free_ci_list(ud->flist);
1904 /* directory content is always handled including sidecars */
1905 file_data_sc_free_ci_list(ud->content_list);
1907 if (ud->dir_fd) file_data_free_ci(ud->dir_fd);
1908 file_util_data_free(ud);
1916 static void file_util_warn_op_in_progress(const gchar *title)
1918 file_util_warning_dialog(title, _("Another operation in progress.\n"), GTK_STOCK_DIALOG_ERROR, NULL);
1921 static void file_util_details_dialog_close_cb(GtkWidget *UNUSED(widget), gpointer data)
1923 gtk_widget_destroy(data);
1927 static void file_util_details_dialog_destroy_cb(GtkWidget *widget, gpointer data)
1929 UtilityData *ud = data;
1930 g_signal_handlers_disconnect_by_func(ud->gd->dialog, (gpointer)(file_util_details_dialog_close_cb), widget);
1934 static void file_util_details_dialog_ok_cb(GenericDialog *UNUSED(gd), gpointer UNUSED(data))
1939 static void file_util_details_dialog_exclude(GenericDialog *gd, gpointer data, gboolean discard)
1941 UtilityData *ud = data;
1942 FileData *fd = g_object_get_data(G_OBJECT(gd->dialog), "file_data");
1945 file_util_exclude_fd(ud, fd);
1947 if (discard && ud->discard_func) ud->discard_func(fd);
1949 /* all files were excluded, this has the same effect as pressing the cancel button in the confirmation dialog*/
1952 /* both dialogs will be closed anyway, the signals would cause duplicate calls */
1953 g_signal_handlers_disconnect_by_func(ud->gd->dialog, (gpointer)(file_util_details_dialog_close_cb), gd->dialog);
1954 g_signal_handlers_disconnect_by_func(gd->dialog, (gpointer)(file_util_details_dialog_destroy_cb), ud);
1956 file_util_cancel_cb(ud->gd, ud);
1960 static void file_util_details_dialog_exclude_cb(GenericDialog *gd, gpointer data)
1962 file_util_details_dialog_exclude(gd, data, FALSE);
1965 static void file_util_details_dialog_discard_cb(GenericDialog *gd, gpointer data)
1967 file_util_details_dialog_exclude(gd, data, TRUE);
1970 static gchar *file_util_details_get_message(UtilityData *ud, FileData *fd, const gchar **stock_id)
1972 GString *message = g_string_new("");
1974 g_string_append_printf(message, _("File: '%s'\n"), fd->path);
1976 if (ud->with_sidecars && fd->sidecar_files)
1978 GList *work = fd->sidecar_files;
1979 g_string_append(message, _("with sidecar files:\n"));
1983 FileData *sfd = work->data;
1985 g_string_append_printf(message, _(" '%s'\n"), sfd->path);
1989 g_string_append(message, _("\nStatus: "));
1991 error = ud->with_sidecars ? file_data_sc_verify_ci(fd, ud->flist) : file_data_verify_ci(fd, ud->flist);
1995 gchar *err_msg = file_data_get_error_string(error);
1996 g_string_append(message, err_msg);
1997 if (stock_id) *stock_id = (error & CHANGE_ERROR_MASK) ? GTK_STOCK_DIALOG_ERROR : GTK_STOCK_DIALOG_WARNING;
2001 g_string_append(message, _("no problem detected"));
2002 if (stock_id) *stock_id = GTK_STOCK_DIALOG_INFO;
2005 return g_string_free(message, FALSE);;
2008 static void file_util_details_dialog(UtilityData *ud, FileData *fd)
2013 const gchar *stock_id;
2015 gd = file_util_gen_dlg(_("File details"), "details", ud->gd->dialog, TRUE, NULL, ud);
2016 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, file_util_details_dialog_ok_cb, TRUE);
2017 generic_dialog_add_button(gd, GTK_STOCK_REMOVE, _("Exclude file"), file_util_details_dialog_exclude_cb, FALSE);
2019 g_object_set_data(G_OBJECT(gd->dialog), "file_data", fd);
2021 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
2022 G_CALLBACK(file_util_details_dialog_destroy_cb), ud);
2024 /* in case the ud->gd->dialog is closed during editing */
2025 g_signal_connect(G_OBJECT(ud->gd->dialog), "destroy",
2026 G_CALLBACK(file_util_details_dialog_close_cb), gd->dialog);
2029 message = file_util_details_get_message(ud, fd, &stock_id);
2031 box = generic_dialog_add_message(gd, stock_id, _("File details"), message, TRUE);
2033 generic_dialog_add_image(gd, box, fd, NULL, FALSE, NULL, NULL, FALSE);
2035 gtk_widget_show(gd->dialog);
2040 static void file_util_write_metadata_details_dialog(UtilityData *ud, FileData *fd)
2052 const gchar *stock_id;
2054 if (fd && fd->modified_xmp)
2056 keys = g_hash_table_get_keys(fd->modified_xmp);
2062 gd = file_util_gen_dlg(_("Overview of changed metadata"), "details", ud->gd->dialog, TRUE, NULL, ud);
2063 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, file_util_details_dialog_ok_cb, TRUE);
2064 generic_dialog_add_button(gd, GTK_STOCK_REMOVE, _("Exclude file"), file_util_details_dialog_exclude_cb, FALSE);
2065 generic_dialog_add_button(gd, GTK_STOCK_REVERT_TO_SAVED, _("Discard changes"), file_util_details_dialog_discard_cb, FALSE);
2067 g_object_set_data(G_OBJECT(gd->dialog), "file_data", fd);
2069 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
2070 G_CALLBACK(file_util_details_dialog_destroy_cb), ud);
2072 /* in case the ud->gd->dialog is closed during editing */
2073 g_signal_connect(G_OBJECT(ud->gd->dialog), "destroy",
2074 G_CALLBACK(file_util_details_dialog_close_cb), gd->dialog);
2076 message1 = file_util_details_get_message(ud, fd, &stock_id);
2078 if (fd->change && fd->change->dest)
2080 message2 = g_strdup_printf(_("The following metadata tags will be written to\n'%s'."), fd->change->dest);
2084 message2 = g_strdup_printf(_("The following metadata tags will be written to the image file itself."));
2087 box = generic_dialog_add_message(gd, stock_id, _("Overview of changed metadata"), message1, TRUE);
2089 box = pref_group_new(box, TRUE, message2, GTK_ORIENTATION_HORIZONTAL);
2091 frame = pref_frame_new(box, TRUE, NULL, GTK_ORIENTATION_HORIZONTAL, 2);
2092 table = pref_table_new(frame, 2, g_list_length(keys), FALSE, TRUE);
2098 const gchar *key = work->data;
2099 gchar *title = exif_get_description_by_key(key);
2100 gchar *title_f = g_strdup_printf("%s:", title);
2101 gchar *value = metadata_read_string(fd, key, METADATA_FORMATTED);
2105 label = gtk_label_new(title_f);
2106 gtk_label_set_xalign(GTK_LABEL(label), 1.0);
2107 gtk_label_set_yalign(GTK_LABEL(label), 0.0);
2109 pref_label_bold(label, TRUE, FALSE);
2110 gtk_table_attach(GTK_TABLE(table), label,
2114 gtk_widget_show(label);
2116 label = gtk_label_new(value);
2118 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
2119 gtk_label_set_yalign(GTK_LABEL(label), 0.0);
2121 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
2122 gtk_table_attach(GTK_TABLE(table), label,
2126 gtk_widget_show(label);
2134 generic_dialog_add_image(gd, box, fd, NULL, FALSE, NULL, NULL, FALSE);
2136 gtk_widget_set_size_request(gd->dialog, DIALOG_WIDTH, -1);
2137 gtk_widget_show(gd->dialog);
2145 static void file_util_mark_ungrouped_files(GList *work)
2149 FileData *fd = work->data;
2150 file_data_set_regroup_when_finished(fd, TRUE);
2155 static void file_util_delete_full(FileData *source_fd, GList *flist, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
2158 GList *ungrouped = NULL;
2162 flist = g_list_append(flist, file_data_ref(source_fd));
2166 flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2168 if (!file_data_sc_add_ci_delete_list(flist))
2170 file_util_warn_op_in_progress(_("File deletion failed"));
2171 file_data_disable_grouping_list(ungrouped, FALSE);
2172 filelist_free(flist);
2173 filelist_free(ungrouped);
2177 file_util_mark_ungrouped_files(ungrouped);
2178 filelist_free(ungrouped);
2180 ud = file_util_data_new(UTILITY_TYPE_DELETE);
2184 ud->with_sidecars = TRUE;
2188 ud->content_list = NULL;
2189 ud->parent = parent;
2190 ud->done_data = done_data;
2191 ud->done_func = done_func;
2193 ud->details_func = file_util_details_dialog;
2194 if(options->file_ops.safe_delete_enable)
2196 message = _("This will move the following files to the Trash bin");
2200 message = _("This will permanently delete the following files");
2202 ud->messages.title = _("Delete");
2203 ud->messages.question = _("Delete files?");
2204 ud->messages.desc_flist = message;
2205 ud->messages.desc_source_fd = "";
2206 ud->messages.fail = _("File deletion failed");
2208 file_util_dialog_run(ud);
2212 static void file_util_write_metadata_full(FileData *source_fd, GList *flist, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
2217 flist = g_list_append(flist, file_data_ref(source_fd));
2221 if (!file_data_add_ci_write_metadata_list(flist))
2223 file_util_warn_op_in_progress(_("Can't write metadata"));
2224 filelist_free(flist);
2228 ud = file_util_data_new(UTILITY_TYPE_WRITE_METADATA);
2232 ud->with_sidecars = FALSE; /* operate on individual files, not groups */
2236 ud->content_list = NULL;
2237 ud->parent = parent;
2239 ud->done_func = done_func;
2240 ud->done_data = done_data;
2242 ud->details_func = file_util_write_metadata_details_dialog;
2243 ud->finalize_func = metadata_write_queue_remove;
2244 ud->discard_func = metadata_write_queue_remove;
2246 ud->messages.title = _("Write metadata");
2247 ud->messages.question = _("Write metadata?");
2248 ud->messages.desc_flist = _("This will write the changed metadata into the following files");
2249 ud->messages.desc_source_fd = "";
2250 ud->messages.fail = _("Metadata writing failed");
2252 file_util_dialog_run(ud);
2255 static void file_util_move_full(FileData *source_fd, GList *flist, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
2258 GList *ungrouped = NULL;
2261 flist = g_list_append(flist, file_data_ref(source_fd));
2265 flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2267 if (!file_data_sc_add_ci_move_list(flist, dest_path))
2269 file_util_warn_op_in_progress(_("Move failed"));
2270 file_data_disable_grouping_list(ungrouped, FALSE);
2271 filelist_free(flist);
2272 filelist_free(ungrouped);
2276 file_util_mark_ungrouped_files(ungrouped);
2277 filelist_free(ungrouped);
2279 ud = file_util_data_new(UTILITY_TYPE_MOVE);
2283 ud->with_sidecars = TRUE;
2287 ud->content_list = NULL;
2288 ud->parent = parent;
2289 ud->details_func = file_util_details_dialog;
2291 if (dest_path) ud->dest_path = g_strdup(dest_path);
2293 ud->messages.title = _("Move");
2294 ud->messages.question = _("Move files?");
2295 ud->messages.desc_flist = _("This will move the following files");
2296 ud->messages.desc_source_fd = "";
2297 ud->messages.fail = _("Move failed");
2299 file_util_dialog_run(ud);
2302 static void file_util_copy_full(FileData *source_fd, GList *flist, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
2305 GList *ungrouped = NULL;
2308 flist = g_list_append(flist, file_data_ref(source_fd));
2312 if (file_util_write_metadata_first(UTILITY_TYPE_COPY, phase, flist, dest_path, NULL, parent))
2315 flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2317 if (!file_data_sc_add_ci_copy_list(flist, dest_path))
2319 file_util_warn_op_in_progress(_("Copy failed"));
2320 file_data_disable_grouping_list(ungrouped, FALSE);
2321 filelist_free(flist);
2322 filelist_free(ungrouped);
2326 file_util_mark_ungrouped_files(ungrouped);
2327 filelist_free(ungrouped);
2329 ud = file_util_data_new(UTILITY_TYPE_COPY);
2333 ud->with_sidecars = TRUE;
2337 ud->content_list = NULL;
2338 ud->parent = parent;
2339 ud->details_func = file_util_details_dialog;
2341 if (dest_path) ud->dest_path = g_strdup(dest_path);
2343 ud->messages.title = _("Copy");
2344 ud->messages.question = _("Copy files?");
2345 ud->messages.desc_flist = _("This will copy the following files");
2346 ud->messages.desc_source_fd = "";
2347 ud->messages.fail = _("Copy failed");
2349 file_util_dialog_run(ud);
2352 static void file_util_rename_full(FileData *source_fd, GList *flist, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
2355 GList *ungrouped = NULL;
2358 flist = g_list_append(flist, file_data_ref(source_fd));
2362 flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2364 if (!file_data_sc_add_ci_rename_list(flist, dest_path))
2366 file_util_warn_op_in_progress(_("Rename failed"));
2367 file_data_disable_grouping_list(ungrouped, FALSE);
2368 filelist_free(flist);
2369 filelist_free(ungrouped);
2373 file_util_mark_ungrouped_files(ungrouped);
2374 filelist_free(ungrouped);
2376 ud = file_util_data_new(UTILITY_TYPE_RENAME);
2380 ud->with_sidecars = TRUE;
2384 ud->content_list = NULL;
2385 ud->parent = parent;
2387 ud->details_func = file_util_details_dialog;
2389 ud->messages.title = _("Rename");
2390 ud->messages.question = _("Rename files?");
2391 ud->messages.desc_flist = _("This will rename the following files");
2392 ud->messages.desc_source_fd = "";
2393 ud->messages.fail = _("Rename failed");
2395 file_util_dialog_run(ud);
2398 static void file_util_start_editor_full(const gchar *key, FileData *source_fd, GList *flist, const gchar *dest_path, const gchar *working_directory, GtkWidget *parent, UtilityPhase phase)
2401 GList *ungrouped = NULL;
2403 if (editor_no_param(key))
2405 gchar *file_directory = NULL;
2406 if (!working_directory)
2408 /* working directory was not specified, try to extract it from the files */
2410 file_directory = remove_level_from_path(source_fd->path);
2412 if (!file_directory && flist)
2413 file_directory = remove_level_from_path(((FileData *)flist->data)->path);
2414 working_directory = file_directory;
2417 /* just start the editor, don't care about files */
2418 start_editor(key, working_directory);
2419 g_free(file_directory);
2420 filelist_free(flist);
2427 /* flist is most probably NULL
2428 operate on source_fd and it's sidecars
2430 flist = g_list_concat(filelist_copy(source_fd->sidecar_files), flist);
2431 flist = g_list_append(flist, file_data_ref(source_fd));
2436 if (file_util_write_metadata_first(UTILITY_TYPE_FILTER, phase, flist, dest_path, key, parent))
2439 flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2441 if (!file_data_sc_add_ci_unspecified_list(flist, dest_path))
2443 file_util_warn_op_in_progress(_("Can't run external editor"));
2444 file_data_disable_grouping_list(ungrouped, FALSE);
2445 filelist_free(flist);
2446 filelist_free(ungrouped);
2450 file_util_mark_ungrouped_files(ungrouped);
2451 filelist_free(ungrouped);
2453 if (editor_is_filter(key))
2454 ud = file_util_data_new(UTILITY_TYPE_FILTER);
2456 ud = file_util_data_new(UTILITY_TYPE_EDITOR);
2459 /* ask for destination if we don't have it */
2460 if (ud->type == UTILITY_TYPE_FILTER && dest_path == NULL) phase = UTILITY_PHASE_START;
2464 ud->with_sidecars = TRUE;
2466 ud->external_command = g_strdup(key);
2470 ud->content_list = NULL;
2471 ud->parent = parent;
2473 ud->details_func = file_util_details_dialog;
2475 if (dest_path) ud->dest_path = g_strdup(dest_path);
2477 ud->messages.title = _("Editor");
2478 ud->messages.question = _("Run editor?");
2479 ud->messages.desc_flist = _("This will copy the following files");
2480 ud->messages.desc_source_fd = "";
2481 ud->messages.fail = _("External command failed");
2483 file_util_dialog_run(ud);
2486 static GList *file_util_delete_dir_remaining_folders(GList *dlist)
2488 GList *rlist = NULL;
2495 dlist = dlist->next;
2498 (strcmp(fd->name, THUMB_FOLDER_GLOBAL) != 0 &&
2499 strcmp(fd->name, THUMB_FOLDER_LOCAL) != 0 &&
2500 strcmp(fd->name, GQ_CACHE_LOCAL_METADATA) != 0) )
2502 rlist = g_list_prepend(rlist, fd);
2506 return g_list_reverse(rlist);
2509 static gboolean file_util_delete_dir_empty_path(UtilityData *ud, FileData *fd, gint level)
2517 DEBUG_1("deltree into: %s", fd->path);
2520 if (level > UTILITY_DELETE_MAX_DEPTH)
2522 log_printf("folder recursion depth past %d, giving up\n", UTILITY_DELETE_MAX_DEPTH);
2527 if (!filelist_read_lstat(fd, &flist, &dlist))
2535 ok = file_data_sc_add_ci_delete(fd);
2538 ud->content_list = g_list_prepend(ud->content_list, fd);
2551 ok = file_util_delete_dir_empty_path(ud, lfd, level);
2562 DEBUG_1("deltree child: %s", lfd->path);
2564 ok = file_data_sc_add_ci_delete(lfd);
2567 ud->content_list = g_list_prepend(ud->content_list, lfd);
2572 filelist_free(dlist);
2573 filelist_free(flist);
2576 DEBUG_1("deltree done: %s", fd->path);
2581 static gboolean file_util_delete_dir_prepare(UtilityData *ud, GList *flist, GList *dlist)
2595 ok = file_util_delete_dir_empty_path(ud, fd, 0);
2599 if (ok && file_data_sc_add_ci_delete_list(flist))
2601 ud->content_list = g_list_concat(filelist_copy(flist), ud->content_list);
2610 ok = file_data_sc_add_ci_delete(ud->dir_fd);
2615 work = ud->content_list;
2622 file_data_sc_free_ci(fd);
2629 static void file_util_delete_dir_full(FileData *fd, GtkWidget *parent, UtilityPhase phase)
2635 if (!isdir(fd->path)) return;
2637 if (islink(fd->path))
2640 ud = file_util_data_new(UTILITY_TYPE_DELETE_LINK);
2643 ud->with_sidecars = TRUE;
2644 ud->dir_fd = file_data_ref(fd);
2645 ud->content_list = NULL;
2648 ud->parent = parent;
2650 ud->messages.title = _("Delete folder");
2651 ud->messages.question = _("Delete symbolic link?");
2652 ud->messages.desc_flist = "";
2653 ud->messages.desc_source_fd = _("This will delete the symbolic link.\n"
2654 "The folder this link points to will not be deleted.");
2655 ud->messages.fail = _("Link deletion failed");
2657 file_util_dialog_run(ud);
2661 if (!access_file(fd->path, W_OK | X_OK))
2665 text = g_strdup_printf(_("Unable to remove folder %s\n"
2666 "Permissions do not allow writing to the folder."), fd->path);
2667 file_util_warning_dialog(_("Delete failed"), text, GTK_STOCK_DIALOG_ERROR, parent);
2673 if (!filelist_read_lstat(fd, &flist, &dlist))
2677 text = g_strdup_printf(_("Unable to list contents of folder %s"), fd->path);
2678 file_util_warning_dialog(_("Delete failed"), text, GTK_STOCK_DIALOG_ERROR, parent);
2684 rlist = file_util_delete_dir_remaining_folders(dlist);
2691 gd = file_util_gen_dlg(_("Folder contains subfolders"), "dlg_warning",
2692 parent, TRUE, NULL, NULL);
2693 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
2695 text = g_strdup_printf(_("Unable to delete the folder:\n\n%s\n\n"
2696 "This folder contains subfolders which must be moved before it can be deleted."),
2698 box = generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
2699 _("Folder contains subfolders"),
2703 box = pref_group_new(box, TRUE, _("Subfolders:"), GTK_ORIENTATION_VERTICAL);
2705 rlist = filelist_sort_path(rlist);
2706 file_util_dialog_add_list(box, rlist, FALSE, FALSE);
2708 gtk_widget_show(gd->dialog);
2713 ud = file_util_data_new(UTILITY_TYPE_DELETE_FOLDER);
2716 ud->with_sidecars = TRUE;
2717 ud->dir_fd = file_data_ref(fd);
2718 ud->content_list = NULL; /* will be filled by file_util_delete_dir_prepare */
2719 ud->flist = flist = filelist_sort_path(flist);
2721 ud->parent = parent;
2723 ud->messages.title = _("Delete folder");
2724 ud->messages.question = _("Delete folder?");
2725 ud->messages.desc_flist = _("The folder contains these files:");
2726 ud->messages.desc_source_fd = _("This will delete the folder.\n"
2727 "The contents of this folder will also be deleted.");
2728 ud->messages.fail = _("File deletion failed");
2730 if (!file_util_delete_dir_prepare(ud, flist, dlist))
2734 text = g_strdup_printf(_("Unable to list contents of folder %s"), fd->path);
2735 file_util_warning_dialog(_("Delete failed"), text, GTK_STOCK_DIALOG_ERROR, parent);
2737 file_data_unref(ud->dir_fd);
2738 file_util_data_free(ud);
2742 filelist_free(dlist);
2743 file_util_dialog_run(ud);
2749 filelist_free(dlist);
2750 filelist_free(flist);
2753 static gboolean file_util_rename_dir_scan(UtilityData *ud, FileData *fd)
2761 if (!filelist_read_lstat(fd, &flist, &dlist))
2767 ud->content_list = g_list_concat(flist, ud->content_list);
2777 ud->content_list = g_list_prepend(ud->content_list, file_data_ref(lfd));
2778 ok = file_util_rename_dir_scan(ud, lfd);
2781 filelist_free(dlist);
2786 static gboolean file_util_rename_dir_prepare(UtilityData *ud, const gchar *new_path)
2790 gint orig_len = strlen(ud->dir_fd->path);
2792 ok = file_util_rename_dir_scan(ud, ud->dir_fd);
2794 work = ud->content_list;
2804 g_assert(strncmp(fd->path, ud->dir_fd->path, orig_len) == 0);
2806 np = g_strconcat(new_path, fd->path + orig_len, NULL);
2808 ok = file_data_sc_add_ci_rename(fd, np);
2810 DEBUG_1("Dir rename: %s -> %s", fd->path, np);
2816 ok = file_data_sc_add_ci_rename(ud->dir_fd, new_path);
2821 work = ud->content_list;
2828 file_data_sc_free_ci(fd);
2836 static void file_util_rename_dir_full(FileData *fd, const gchar *new_path, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
2840 ud = file_util_data_new(UTILITY_TYPE_RENAME_FOLDER);
2843 ud->with_sidecars = TRUE; /* does not matter, the directory should not have sidecars
2844 and the content must be handled including sidecars */
2846 ud->dir_fd = file_data_ref(fd);
2848 ud->content_list = NULL;
2849 ud->parent = parent;
2851 ud->done_func = done_func;
2852 ud->done_data = done_data;
2853 ud->dest_path = g_strdup(new_path);
2855 ud->messages.title = _("Rename");
2856 ud->messages.question = _("Rename folder?");
2857 ud->messages.desc_flist = _("The folder contains the following files");
2858 ud->messages.desc_source_fd = "";
2859 ud->messages.fail = _("Rename failed");
2861 if (!file_util_rename_dir_prepare(ud, new_path))
2863 file_util_warn_op_in_progress(ud->messages.fail);
2864 file_util_data_free(ud);
2868 // ud->flist = filelist_recursive(fd);
2870 file_util_dialog_run(ud);
2873 static void file_util_create_dir_full(FileData *fd, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
2877 ud = file_util_data_new(UTILITY_TYPE_CREATE_FOLDER);
2880 ud->with_sidecars = TRUE;
2884 ud->content_list = NULL;
2885 ud->parent = parent;
2889 g_assert_not_reached(); // not used in current design
2890 ud->dest_path = g_strdup(dest_path);
2894 gchar *buf = new_folder(GTK_WINDOW(parent), fd->path);
2897 ud->phase = UTILITY_PHASE_CANCEL;
2902 ud->dest_path = buf;
2903 ud->dir_fd = file_data_new_dir(ud->dest_path);
2907 ud->done_func = done_func;
2908 ud->done_data = done_data;
2910 ud->messages.title = _("Create Folder");
2911 ud->messages.question = _("Create folder?");
2912 ud->messages.desc_flist = "";
2913 ud->messages.desc_source_fd = "";
2914 ud->messages.fail = _("Can't create folder");
2916 file_util_dialog_run(ud);
2920 static gboolean file_util_write_metadata_first_after_done(gpointer data)
2922 UtilityDelayData *dd = data;
2924 /* start the delayed operation with original arguments */
2927 case UTILITY_TYPE_FILTER:
2928 case UTILITY_TYPE_EDITOR:
2929 file_util_start_editor_full(dd->editor_key, NULL, dd->flist, dd->dest_path, NULL, dd->parent, dd->phase);
2931 case UTILITY_TYPE_COPY:
2932 file_util_copy_full(NULL, dd->flist, dd->dest_path, dd->parent, dd->phase);
2935 g_warning("unsupported type");
2937 g_free(dd->dest_path);
2938 g_free(dd->editor_key);
2943 static void file_util_write_metadata_first_done(gboolean success, const gchar *UNUSED(done_path), gpointer data)
2945 UtilityDelayData *dd = data;
2949 dd->idle_id = g_idle_add(file_util_write_metadata_first_after_done, dd);
2953 /* the operation was cancelled */
2954 filelist_free(dd->flist);
2955 g_free(dd->dest_path);
2956 g_free(dd->editor_key);
2960 static gboolean file_util_write_metadata_first(UtilityType type, UtilityPhase phase, GList *flist, const gchar *dest_path, const gchar *editor_key, GtkWidget *parent)
2962 GList *unsaved = NULL;
2963 UtilityDelayData *dd;
2970 FileData *fd = work->data;
2975 filelist_free(unsaved);
2976 return FALSE; /* another op. in progress, let the caller handle it */
2979 if (fd->modified_xmp) /* has unsaved metadata */
2981 unsaved = g_list_prepend(unsaved, file_data_ref(fd));
2985 if (!unsaved) return FALSE;
2987 /* save arguments of the original operation */
2989 dd = g_new0(UtilityDelayData, 1);
2994 dd->dest_path = g_strdup(dest_path);
2995 dd->editor_key = g_strdup(editor_key);
2996 dd->parent = parent;
2998 file_util_write_metadata(NULL, unsaved, parent, FALSE, file_util_write_metadata_first_done, dd);
3003 /* full-featured entry points
3006 void file_util_delete(FileData *source_fd, GList *source_list, GtkWidget *parent)
3008 file_util_delete_full(source_fd, source_list, parent, options->file_ops.confirm_delete ? UTILITY_PHASE_START : UTILITY_PHASE_ENTERING, NULL, NULL);
3011 void file_util_delete_notify_done(FileData *source_fd, GList *source_list, GtkWidget *parent, FileUtilDoneFunc done_func, gpointer done_data)
3013 file_util_delete_full(source_fd, source_list, parent, options->file_ops.confirm_delete ? UTILITY_PHASE_START : UTILITY_PHASE_ENTERING, done_func, done_data);
3016 void file_util_write_metadata(FileData *source_fd, GList *source_list, GtkWidget *parent, gboolean force_dialog, FileUtilDoneFunc done_func, gpointer done_data)
3018 file_util_write_metadata_full(source_fd, source_list, parent,
3019 ((options->metadata.save_in_image_file && options->metadata.confirm_write) || force_dialog) ? UTILITY_PHASE_START : UTILITY_PHASE_ENTERING,
3020 done_func, done_data);
3023 void file_util_copy(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent)
3025 file_util_copy_full(source_fd, source_list, dest_path, parent, UTILITY_PHASE_START);
3028 void file_util_move(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent)
3030 file_util_move_full(source_fd, source_list, dest_path, parent, UTILITY_PHASE_START);
3033 void file_util_rename(FileData *source_fd, GList *source_list, GtkWidget *parent)
3035 file_util_rename_full(source_fd, source_list, NULL, parent, UTILITY_PHASE_START);
3038 /* these avoid the location entry dialog unless there is an error, list must be files only and
3039 * dest_path must be a valid directory path
3041 void file_util_move_simple(GList *list, const gchar *dest_path, GtkWidget *parent)
3043 file_util_move_full(NULL, list, dest_path, parent, UTILITY_PHASE_ENTERING);
3046 void file_util_copy_simple(GList *list, const gchar *dest_path, GtkWidget *parent)
3048 file_util_copy_full(NULL, list, dest_path, parent, UTILITY_PHASE_ENTERING);
3051 void file_util_rename_simple(FileData *fd, const gchar *dest_path, GtkWidget *parent)
3053 file_util_rename_full(fd, NULL, dest_path, parent, UTILITY_PHASE_ENTERING);
3057 void file_util_start_editor_from_file(const gchar *key, FileData *fd, GtkWidget *parent)
3059 file_util_start_editor_full(key, fd, NULL, NULL, NULL, parent, UTILITY_PHASE_ENTERING);
3062 void file_util_start_editor_from_filelist(const gchar *key, GList *list, const gchar *working_directory, GtkWidget *parent)
3064 file_util_start_editor_full(key, NULL, list, NULL, working_directory, parent, UTILITY_PHASE_ENTERING);
3067 //void file_util_start_filter_from_file(const gchar *key, FileData *fd, const gchar *dest_path, GtkWidget *parent)
3069 //file_util_start_editor_full(key, fd, NULL, dest_path, NULL, parent, UTILITY_PHASE_ENTERING);
3072 void file_util_start_filter_from_filelist(const gchar *key, GList *list, const gchar *dest_path, GtkWidget *parent)
3074 file_util_start_editor_full(key, NULL, list, dest_path, NULL, parent, UTILITY_PHASE_ENTERING);
3077 void file_util_delete_dir(FileData *fd, GtkWidget *parent)
3079 file_util_delete_dir_full(fd, parent, UTILITY_PHASE_START);
3082 void file_util_create_dir(FileData *dir_fd, GtkWidget *parent, FileUtilDoneFunc done_func, gpointer done_data)
3084 file_util_create_dir_full(dir_fd, NULL, parent, UTILITY_PHASE_ENTERING, done_func, done_data);
3087 void file_util_rename_dir(FileData *source_fd, const gchar *new_path, GtkWidget *parent, FileUtilDoneFunc done_func, gpointer done_data)
3089 file_util_rename_dir_full(source_fd, new_path, parent, UTILITY_PHASE_ENTERING, done_func, done_data);
3093 void file_util_copy_path_to_clipboard(FileData *fd, gboolean quoted)
3095 if (!fd || !*fd->path) return;
3097 if (options->clipboard_selection == CLIPBOARD_PRIMARY)
3099 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), quoted ? g_shell_quote(fd->path) : fd->path , -1);
3101 else if (options->clipboard_selection == CLIPBOARD_CLIPBOARD)
3103 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), quoted ? g_shell_quote(fd->path) : fd->path , -1);
3107 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), quoted ? g_shell_quote(fd->path) : fd->path , -1);
3108 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), quoted ? g_shell_quote(fd->path) : fd->path , -1);
3112 void file_util_copy_path_list_to_clipboard(GList *list, gboolean quoted)
3115 GString *path_list_str;
3117 path_list_str = g_string_new("");
3120 FileData *fd = work->data;
3123 if (!fd || !*fd->path) continue;
3127 g_string_append(path_list_str, g_shell_quote(fd->path));
3131 g_string_append(path_list_str, fd->path);
3133 if (work) g_string_append_c(path_list_str, ' ');
3136 if (options->clipboard_selection == CLIPBOARD_PRIMARY)
3138 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), path_list_str->str, path_list_str->len);
3140 else if (options->clipboard_selection == CLIPBOARD_CLIPBOARD)
3142 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), path_list_str->str, path_list_str->len);
3146 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), path_list_str->str, path_list_str->len);
3147 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), path_list_str->str, path_list_str->len);
3150 g_string_free(path_list_str, TRUE);
3151 filelist_free(list);
3154 static void new_folder_entry_activate_cb(GtkWidget *UNUSED(widget), gpointer data)
3156 GtkDialog *dialog = data;
3158 gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
3161 gchar *new_folder(GtkWindow *window , gchar *path)
3170 GtkWidget *folder_name_entry;
3172 GtkWidget *dialog_warning;
3173 GtkWidget *content_area;
3174 gboolean ok_or_cancel = FALSE;
3176 gchar *new_folder_name;
3177 gchar *new_folder_path_full = NULL;
3178 gchar *window_title;
3179 PangoLayout *layout;
3182 buf = g_build_filename(path, _("New folder"), NULL);
3183 folder_path = unique_filename(buf, NULL, " ", FALSE);
3184 folder_name = g_path_get_basename(folder_path);
3185 window_title = g_strconcat(_("Create Folder - "), GQ_APPNAME, NULL);
3187 dialog = gtk_dialog_new_with_buttons(window_title,
3191 GTK_RESPONSE_REJECT,
3193 GTK_RESPONSE_ACCEPT,
3196 layout = gtk_widget_create_pango_layout(GTK_WIDGET(dialog), window_title);
3197 pango_layout_get_pixel_size(layout, &width, &height);
3198 gtk_window_set_default_size(GTK_WINDOW(dialog), width * 2, -1);
3200 content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
3201 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
3202 gtk_box_pack_start(GTK_BOX(content_area), vbox, FALSE, FALSE, 0);
3204 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
3205 gtk_container_set_border_width(GTK_CONTAINER(hbox), PREF_PAD_GAP);
3206 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
3207 image = gtk_image_new_from_icon_name("dialog-question", GTK_ICON_SIZE_DIALOG);
3208 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
3209 label = gtk_label_new(_("Create new folder"));
3210 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
3212 folder_name_entry = gtk_entry_new();
3213 gtk_entry_set_text(GTK_ENTRY(folder_name_entry), folder_name);
3214 gtk_box_pack_start(GTK_BOX(vbox), folder_name_entry, FALSE, FALSE, PREF_PAD_SPACE);
3215 g_signal_connect(G_OBJECT(folder_name_entry), "activate", G_CALLBACK(new_folder_entry_activate_cb), dialog);
3217 gtk_widget_show_all(dialog);
3219 while (ok_or_cancel == FALSE)
3221 result = gtk_dialog_run(GTK_DIALOG(dialog));
3225 case GTK_RESPONSE_ACCEPT:
3226 new_folder_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(folder_name_entry)));
3227 new_folder_path_full = g_build_filename(path, new_folder_name, NULL);
3228 if (isname(new_folder_path_full))
3230 dialog_warning = gtk_message_dialog_new(GTK_WINDOW(window),
3232 GTK_MESSAGE_WARNING,
3234 _("Cannot create folder:"));
3235 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", new_folder_name);
3236 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
3238 gtk_dialog_run(GTK_DIALOG(dialog_warning));
3240 gtk_widget_destroy(dialog_warning);
3241 g_free(new_folder_path_full);
3242 new_folder_path_full = NULL;
3243 g_free(new_folder_name);
3244 ok_or_cancel = FALSE;
3248 ok_or_cancel = TRUE;
3251 case GTK_RESPONSE_REJECT:
3252 ok_or_cancel = TRUE;
3255 ok_or_cancel = TRUE;
3261 g_free(folder_path);
3262 g_free(folder_name);
3263 g_object_unref(layout);
3264 g_free(window_title);
3265 gtk_widget_destroy(dialog);
3267 return new_folder_path_full;
3269 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */