diff --git a/gtk/Makefile.am b/gtk/Makefile.am index fac2bce7a..cf95045c8 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -393,6 +393,7 @@ gtk_private_h_sources = \ gtkfilefilterprivate.h \ gtknativedialogprivate.h \ gtkfilechooserutils.h \ + gtkfileoperation.h \ gtkfilesystem.h \ gtkfilesystemmodel.h \ gtkfilethumbnail.h \ @@ -498,6 +499,7 @@ gtk_base_c_sources = \ gtkfilechooserentry.c \ gtkfilechoosersettings.c \ gtkfilechooserutils.c \ + gtkfileoperation.c \ gtkfilechooserwidget.c \ gtkfilefilter.c \ gtkfilesystem.c \ diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index d44fb3418..77e6a9ebd 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -39,6 +39,7 @@ #include "gtkfilechooserentry.h" #include "gtkfilechoosersettings.h" #include "gtkfilechooserutils.h" +#include "gtkfileoperation.h" #include "gtkfilechooser.h" #include "gtkfilesystem.h" #include "gtkfilesystemmodel.h" @@ -3217,31 +3218,93 @@ rename_selected_file (GtkFileChooserDefault *impl) add_idle_to_edit_icon_view (impl, path); } -/* "Copy" (Ctrl+C) puts the selected files on the clipboard as file - * managers do, so that they can be pasted into a folder there; programs - * that take text get their locations, as from "Copy Location". Pasting - * files is left to the file managers, which show the progress of it. */ +/* Cut, Copy and Paste of files, as in file managers. Cut and Copy put + * the selected files on the clipboard in the forms file managers take: + * x-special/gnome-copied-files ("copy" or "cut" and the URIs: Nautilus, + * Caja, Thunar, PCManFM), text/uri-list with the cut mark of KDE + * (Dolphin), and the locations as text for any other program. Paste + * copies or moves what is on the clipboard into the current folder, + * with progress, Cancel and questions (gtkfileoperation.c); after a cut + * the clipboard is emptied, as the files are no longer where it says. + * Cut files are shown dimmed until they are pasted or something else + * goes on the clipboard. The clipboard is only ever asked without + * waiting, so a program that owns it but does not answer blocks + * nothing. */ enum { - COPIED_FILES_GNOME, /* x-special/gnome-copied-files: Nautilus, Thunar, Caja, PCManFM */ - COPIED_FILES_URIS, /* text/uri-list: Dolphin, Konqueror and most others */ + COPIED_FILES_GNOME, + COPIED_FILES_KDE_CUT, + COPIED_FILES_URIS, COPIED_FILES_TEXT }; typedef struct { + GtkFileChooserDefault *impl; /* weak, for a cut */ + gboolean cut; gchar **uris; gchar *locations; /* one per line */ } CopiedFiles; +static void +cut_files_changed (GtkFileChooserDefault *impl) +{ + GtkFileSystemModel *models[3]; + gint i; + + models[0] = impl->browse_files_model; + models[1] = impl->search_model; + models[2] = impl->recent_model; + for (i = 0; i < 3; i++) + if (models[i]) + { + _gtk_file_system_model_clear_cache (models[i], MODEL_COL_LIST_PIXBUF); + _gtk_file_system_model_clear_cache (models[i], MODEL_COL_ICON_PIXBUF); + } +} + static void copied_files_free (CopiedFiles *files) { + if (files->impl) + { + if (files->impl->cut_files == files) + { + files->impl->cut_files = NULL; + cut_files_changed (files->impl); + } + g_object_remove_weak_pointer (G_OBJECT (files->impl), (gpointer *) &files->impl); + } g_strfreev (files->uris); g_free (files->locations); g_slice_free (CopiedFiles, files); } +/* Whether @file is cut and waits on the clipboard to be pasted */ +static gboolean +file_is_cut (GtkFileChooserDefault *impl, + GFile *file) +{ + CopiedFiles *cut_files = impl->cut_files; + gchar *uri; + gboolean cut = FALSE; + gint i; + + if (cut_files == NULL || file == NULL) + return FALSE; + + uri = g_file_get_uri (file); + for (i = 0; cut_files->uris[i]; i++) + if (strcmp (cut_files->uris[i], uri) == 0) + { + cut = TRUE; + break; + } + g_free (uri); + + return cut; +} + static void copied_files_get_cb (GtkClipboard *clipboard, GtkSelectionData *selection_data, @@ -3255,7 +3318,7 @@ copied_files_get_cb (GtkClipboard *clipboard, case COPIED_FILES_GNOME: { gchar *list = g_strjoinv ("\n", files->uris); - gchar *text = g_strconcat ("copy\n", list, NULL); + gchar *text = g_strconcat (files->cut ? "cut\n" : "copy\n", list, NULL); gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data), @@ -3265,6 +3328,12 @@ copied_files_get_cb (GtkClipboard *clipboard, } break; + case COPIED_FILES_KDE_CUT: + gtk_selection_data_set (selection_data, + gtk_selection_data_get_target (selection_data), + 8, (const guchar *) (files->cut ? "1" : "0"), 1); + break; + case COPIED_FILES_URIS: gtk_selection_data_set_uris (selection_data, files->uris); break; @@ -3311,7 +3380,8 @@ copy_files_foreach_cb (GtkTreeModel *model, } static void -copy_files (GtkFileChooserDefault *impl) +put_files_on_clipboard (GtkFileChooserDefault *impl, + gboolean cut) { GPtrArray *uris = g_ptr_array_new (); GString *locations = g_string_new (NULL); @@ -3334,28 +3404,255 @@ copy_files (GtkFileChooserDefault *impl) } g_ptr_array_add (uris, NULL); - files = g_slice_new (CopiedFiles); + files = g_slice_new0 (CopiedFiles); + files->cut = cut; files->uris = (gchar **) g_ptr_array_free (uris, FALSE); files->locations = g_string_free (locations, FALSE); list = gtk_target_list_new (NULL, 0); - gtk_target_list_add (list, gdk_atom_intern_static_string ("x-special/gnome-copied-files"), + gtk_target_list_add (list, gdk_atom_intern_static_string (GTK_FILE_CLIPBOARD_GNOME), 0, COPIED_FILES_GNOME); + if (cut) + gtk_target_list_add (list, gdk_atom_intern_static_string (GTK_FILE_CLIPBOARD_KDE_CUT), + 0, COPIED_FILES_KDE_CUT); gtk_target_list_add_uri_targets (list, COPIED_FILES_URIS); gtk_target_list_add_text_targets (list, COPIED_FILES_TEXT); targets = gtk_target_table_new_from_list (list, &n_targets); gtk_target_list_unref (list); - clipboard = gtk_widget_get_clipboard (impl->browse_files_current_view, GDK_SELECTION_CLIPBOARD); + clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl), GDK_SELECTION_CLIPBOARD); if (gtk_clipboard_set_with_data (clipboard, targets, n_targets, copied_files_get_cb, copied_files_clear_cb, files)) - gtk_clipboard_set_can_store (clipboard, NULL, 0); + { + if (cut) + { + /* what was cut before is no longer (its clear callback ran) */ + files->impl = impl; + g_object_add_weak_pointer (G_OBJECT (impl), (gpointer *) &files->impl); + impl->cut_files = files; + cut_files_changed (impl); + } + else + /* a copy stays there for others when the program ends */ + gtk_clipboard_set_can_store (clipboard, NULL, 0); + } else copied_files_free (files); gtk_target_table_free (targets, n_targets); } +static void +copy_files (GtkFileChooserDefault *impl) +{ + put_files_on_clipboard (impl, FALSE); +} + +static void +cut_files (GtkFileChooserDefault *impl) +{ + put_files_on_clipboard (impl, TRUE); +} + +/* Pasting: what is on the clipboard, asked without waiting */ +typedef struct +{ + GtkFileChooserDefault *impl; /* weak */ + GFile *destination; + GList *files; +} PasteData; + +static void +paste_data_free (PasteData *paste) +{ + if (paste->impl) + g_object_remove_weak_pointer (G_OBJECT (paste->impl), (gpointer *) &paste->impl); + g_object_unref (paste->destination); + g_list_foreach (paste->files, (GFunc) g_object_unref, NULL); + g_list_free (paste->files); + g_slice_free (PasteData, paste); +} + +static void +paste_done (GtkFileOperation *operation, + gboolean cancelled, + gboolean moved, + gpointer data) +{ + GtkFileChooserDefault *impl = data; + + impl->file_operations = g_slist_remove (impl->file_operations, operation); +} + +static void +paste_cut_done (GtkFileOperation *operation, + gboolean cancelled, + gboolean moved, + gpointer data) +{ + GtkFileChooserDefault *impl = data; + + paste_done (operation, cancelled, moved, data); + + /* the files are no longer where the clipboard says: it is emptied, + * if it still holds them */ + if (moved) + { + GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl), + GDK_SELECTION_CLIPBOARD); + + if (impl->cut_files) + gtk_clipboard_clear (clipboard); + else + gtk_clipboard_set_text (clipboard, "", 0); + } +} + +static void +paste_start (PasteData *paste, + gboolean cut) +{ + GtkFileChooserDefault *impl = paste->impl; + GtkFileOperation *operation; + GtkWidget *toplevel; + + if (impl == NULL || paste->files == NULL) + return; + + toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl)); + operation = _gtk_file_operation_start (cut ? GTK_FILE_OPERATION_MOVE : GTK_FILE_OPERATION_COPY, + paste->files, paste->destination, + impl->file_operations_box, + GTK_IS_WINDOW (toplevel) ? GTK_WINDOW (toplevel) : NULL, + cut ? paste_cut_done : paste_done, impl); + if (operation) + impl->file_operations = g_slist_prepend (impl->file_operations, operation); +} + +static void +paste_add_uris (PasteData *paste, + gchar **uris, + gint first) +{ + gint i; + + for (i = first; uris && uris[i]; i++) + if (*uris[i] != '\0') + paste->files = g_list_append (paste->files, g_file_new_for_uri (uris[i])); +} + +static void +paste_kde_cut_received (GtkClipboard *clipboard, + GtkSelectionData *selection_data, + gpointer data) +{ + PasteData *paste = data; + gboolean cut = FALSE; + + if (selection_data && gtk_selection_data_get_length (selection_data) > 0) + cut = gtk_selection_data_get_data (selection_data)[0] == '1'; + + paste_start (paste, cut); + paste_data_free (paste); +} + +static void +paste_uris_received (GtkClipboard *clipboard, + gchar **uris, + gpointer data) +{ + PasteData *paste = data; + + paste_add_uris (paste, uris, 0); + if (paste->impl == NULL || paste->files == NULL) + { + paste_data_free (paste); + return; + } + + /* cut in Dolphin? */ + gtk_clipboard_request_contents (clipboard, + gdk_atom_intern_static_string (GTK_FILE_CLIPBOARD_KDE_CUT), + paste_kde_cut_received, paste); +} + +static void +paste_gnome_received (GtkClipboard *clipboard, + GtkSelectionData *selection_data, + gpointer data) +{ + PasteData *paste = data; + gint length = selection_data ? gtk_selection_data_get_length (selection_data) : -1; + + if (paste->impl == NULL) + { + paste_data_free (paste); + return; + } + + if (length > 0) + { + gchar *text = g_strndup ((const gchar *) gtk_selection_data_get_data (selection_data), length); + gchar **lines = g_strsplit (text, "\n", -1); + gboolean cut = lines[0] && g_str_equal (g_strstrip (lines[0]), "cut"); + gint i; + + for (i = 1; lines[i]; i++) + g_strstrip (lines[i]); + paste_add_uris (paste, lines, 1); + g_strfreev (lines); + g_free (text); + + paste_start (paste, cut); + paste_data_free (paste); + return; + } + + gtk_clipboard_request_uris (clipboard, paste_uris_received, paste); +} + +static void +paste_files (GtkFileChooserDefault *impl) +{ + PasteData *paste; + + if (impl->operation_mode != OPERATION_MODE_BROWSE || impl->current_folder == NULL) + return; + + paste = g_slice_new0 (PasteData); + paste->impl = impl; + g_object_add_weak_pointer (G_OBJECT (impl), (gpointer *) &paste->impl); + paste->destination = g_object_ref (impl->current_folder); + + gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (impl), + GDK_SELECTION_CLIPBOARD), + gdk_atom_intern_static_string (GTK_FILE_CLIPBOARD_GNOME), + paste_gnome_received, paste); +} + +/* Paste in the menu is sensitive when the clipboard has files, which is + * asked when the menu is built, without waiting */ +static void +paste_targets_received (GtkClipboard *clipboard, + GdkAtom *atoms, + gint n_atoms, + gpointer data) +{ + GtkWidget *item = data; + gboolean files = FALSE; + gint i; + + for (i = 0; atoms && i < n_atoms; i++) + if (atoms[i] == gdk_atom_intern_static_string (GTK_FILE_CLIPBOARD_GNOME)) + files = TRUE; + if (!files && atoms) + files = gtk_targets_include_uri (atoms, n_atoms); + + if (gtk_widget_get_parent (item)) + gtk_widget_set_sensitive (item, files); + g_object_unref (item); +} + /* Idle handler for creating a new folder after editing its name cell, or for * canceling the editing. */ @@ -4868,18 +5165,30 @@ browse_files_key_press_event_cb (GtkWidget *widget, return TRUE; } - /* Ctrl+C, on the Mac Command+C */ - if ((event->keyval == GDK_KEY_c || event->keyval == GDK_KEY_C) && + /* Ctrl+X, Ctrl+C and Ctrl+V, on the Mac with Command */ #ifdef GDK_WINDOWING_QUARTZ - (event->state & (gtk_accelerator_get_default_mod_mask () | GDK_MOD2_MASK)) == GDK_MOD2_MASK + if ((event->state & (gtk_accelerator_get_default_mod_mask () | GDK_MOD2_MASK)) == GDK_MOD2_MASK) #else - (event->state & gtk_accelerator_get_default_mod_mask ()) == GDK_CONTROL_MASK + if ((event->state & gtk_accelerator_get_default_mod_mask ()) == GDK_CONTROL_MASK) #endif - ) - { - copy_files (impl); - return TRUE; - } + switch (event->keyval) + { + case GDK_KEY_c: + case GDK_KEY_C: + copy_files (impl); + return TRUE; + case GDK_KEY_x: + case GDK_KEY_X: + if (impl->operation_mode == OPERATION_MODE_BROWSE) + cut_files (impl); + return TRUE; + case GDK_KEY_v: + case GDK_KEY_V: + paste_files (impl); + return TRUE; + default: + break; + } if ((((event->keyval == GDK_KEY_Delete || event->keyval == GDK_KEY_KP_Delete) && !(event->state & gtk_accelerator_get_default_mod_mask ())) @@ -4938,7 +5247,9 @@ popup_menu_detach_cb (GtkWidget *attach_widget, impl->browse_files_popup_menu = NULL; impl->browse_files_popup_menu_add_shortcut_item = NULL; + impl->browse_files_popup_menu_cut_item = NULL; impl->browse_files_popup_menu_copy_item = NULL; + impl->browse_files_popup_menu_paste_item = NULL; impl->browse_files_popup_menu_copy_location_item = NULL; impl->browse_files_popup_menu_rename_item = NULL; impl->browse_files_popup_menu_trash_item = NULL; @@ -5624,6 +5935,17 @@ file_list_build_popup_menu (GtkFileChooserDefault *impl) gtk_widget_show (item); gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item); + item = gtk_separator_menu_item_new (); + gtk_widget_show (item); + gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item); + + item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CUT, NULL); + impl->browse_files_popup_menu_cut_item = item; + g_signal_connect_swapped (item, "activate", + G_CALLBACK (cut_files), impl); + gtk_widget_show (item); + gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item); + item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL); impl->browse_files_popup_menu_copy_item = item; g_signal_connect_swapped (item, "activate", @@ -5631,6 +5953,13 @@ file_list_build_popup_menu (GtkFileChooserDefault *impl) gtk_widget_show (item); gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item); + item = gtk_image_menu_item_new_from_stock (GTK_STOCK_PASTE, NULL); + impl->browse_files_popup_menu_paste_item = item; + g_signal_connect_swapped (item, "activate", + G_CALLBACK (paste_files), impl); + gtk_widget_show (item); + gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item); + item = gtk_image_menu_item_new_with_mnemonic (_("_Copy Location")); impl->browse_files_popup_menu_copy_location_item = item; gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), @@ -5786,9 +6115,20 @@ file_list_update_popup_menu (GtkFileChooserDefault *impl) g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_thumbnails_item, G_CALLBACK (show_thumbnails_toggled_cb), impl); - /* 'Copy' */ + /* 'Cut', 'Copy', 'Paste': cut and paste only in the folders themselves */ gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_item, gtk_widget_get_sensitive (impl->browse_files_popup_menu_copy_location_item)); + gtk_widget_set_sensitive (impl->browse_files_popup_menu_cut_item, + impl->operation_mode == OPERATION_MODE_BROWSE && + gtk_widget_get_sensitive (impl->browse_files_popup_menu_copy_location_item)); + gtk_widget_set_visible (impl->browse_files_popup_menu_paste_item, + impl->operation_mode == OPERATION_MODE_BROWSE); + gtk_widget_set_sensitive (impl->browse_files_popup_menu_paste_item, FALSE); + if (impl->operation_mode == OPERATION_MODE_BROWSE) + gtk_clipboard_request_targets (gtk_widget_get_clipboard (GTK_WIDGET (impl), + GDK_SELECTION_CLIPBOARD), + paste_targets_received, + g_object_ref (impl->browse_files_popup_menu_paste_item)); /* 'Show Preview', not beside the preview of the program */ gtk_widget_set_visible (impl->browse_files_popup_menu_preview_item, @@ -6980,6 +7320,11 @@ file_pane_create (GtkFileChooserDefault *impl, gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0); gtk_size_group_add_widget (size_group, widget); + /* Copying and moving files: a row for each, while it runs */ + impl->file_operations_box = gtk_vbox_new (FALSE, 6); + gtk_box_pack_start (GTK_BOX (vbox), impl->file_operations_box, FALSE, FALSE, 0); + gtk_widget_show (impl->file_operations_box); + /* Preview */ impl->preview_box = gtk_vbox_new (FALSE, 12); @@ -8977,6 +9322,12 @@ gtk_file_chooser_default_dispose (GObject *object) cancel_all_operations (impl); + /* copies and moves stop with the file chooser; their rows go with it, + * and they no longer call back */ + g_slist_foreach (impl->file_operations, (GFunc) _gtk_file_operation_cancel, NULL); + g_slist_free (impl->file_operations); + impl->file_operations = NULL; + /* it belongs to the toplevel, not to us, but speaks to us */ if (impl->preferences_dialog) gtk_widget_destroy (impl->preferences_dialog); @@ -10556,10 +10907,13 @@ file_icon_dim (GtkWidget *widget, static GdkPixbuf * file_icon_dim_if_hidden (GtkFileChooserDefault *impl, + GFile *file, GFileInfo *info, GdkPixbuf *pixbuf) { - if ((g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN) && + /* and files that are cut, waiting to be pasted */ + if (file_is_cut (impl, file) || + (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN) && g_file_info_get_is_hidden (info)) || (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP) && g_file_info_get_is_backup (info))) @@ -10659,7 +11013,7 @@ file_system_model_set (GtkFileSystemModel *model, icon_size = FALLBACK_LIST_VIEW_ICON_SIZE; } - g_value_take_object (value, file_icon_dim_if_hidden (impl, info, + g_value_take_object (value, file_icon_dim_if_hidden (impl, file, info, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), icon_size))); return TRUE; } diff --git a/gtk/gtkfilechooserprivate.h b/gtk/gtkfilechooserprivate.h index afc0f5bc8..e61bb0100 100644 --- a/gtk/gtkfilechooserprivate.h +++ b/gtk/gtkfilechooserprivate.h @@ -179,11 +179,16 @@ struct _GtkFileChooserDefault GtkWidget *browse_shortcuts_popup_menu_rename_item; GtkWidget *browse_files_tree_view; GtkWidget *browse_files_scrolled_window; + GtkWidget *file_operations_box; /* rows of copies and moves */ + GSList *file_operations; /* GtkFileOperation, running */ + gpointer cut_files; /* CopiedFiles on the clipboard, cut here */ GtkWidget *browse_files_current_view; GtkWidget *browse_files_icon_view; GtkWidget *browse_files_popup_menu; GtkWidget *browse_files_popup_menu_add_shortcut_item; + GtkWidget *browse_files_popup_menu_cut_item; GtkWidget *browse_files_popup_menu_copy_item; + GtkWidget *browse_files_popup_menu_paste_item; GtkWidget *browse_files_popup_menu_copy_location_item; GtkWidget *browse_files_popup_menu_rename_item; GtkWidget *browse_files_popup_menu_trash_item; diff --git a/gtk/gtkfileoperation.c b/gtk/gtkfileoperation.c new file mode 100644 index 000000000..6288aede3 --- /dev/null +++ b/gtk/gtkfileoperation.c @@ -0,0 +1,1213 @@ +/* GTK - The GIMP Toolkit + * gtkfileoperation.c: copying and moving files for the file chooser + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* + * The work runs in a thread of its own with the synchronous calls of + * GIO, so that a large copy neither blocks the dialog nor needs a + * callback per block. The thread touches no widget: it counts into + * the operation under a mutex, and a timeout of the main loop shows + * that ten times a second. When it has a question (a file of the same + * name is there, something failed), it hands it to the main loop and + * waits for the answer. Cancel cancels the GCancellable, which ends + * the copy of the file in progress at once; a file that was being + * written anew is removed again. + * + * Moving tries a rename first, which is instant on the same file + * system, and copies and deletes otherwise. Folders are copied with + * everything in them, symbolic links as links. + */ + +#include "config.h" + +#include + +#include "gtkfileoperation.h" +#include "gtkintl.h" +#include "gtkbox.h" +#include "gtkhbox.h" +#include "gtkvbox.h" +#include "gtklabel.h" +#include "gtkprogressbar.h" +#include "gtkbutton.h" +#include "gtkstock.h" +#include "gtkmessagedialog.h" +#include "gtkcheckbutton.h" +#include "gtkentry.h" +#include "gtkmain.h" +#include "gdk/gdk.h" +#include "gtkalias.h" + +/* what a question can be answered with */ +typedef enum +{ + ANSWER_NONE, + ANSWER_CANCEL, + ANSWER_SKIP, + ANSWER_REPLACE, /* a file over a file; folders are merged */ + ANSWER_RENAME +} Answer; + +typedef enum +{ + QUESTION_CONFLICT, + QUESTION_ERROR +} QuestionKind; + +struct _GtkFileOperation +{ + GtkFileOperationKind kind; + GList *sources; + GFile *destination; + GCancellable *cancellable; + + /* main loop side */ + GtkWidget *box; + GtkWidget *row; + GtkWidget *bar; + GtkWidget *label; + GtkWindow *parent; + guint update_id; + guint show_id; + GtkFileOperationDoneFunc done; + gpointer user_data; + + /* shared, under mutex */ + GMutex mutex; + GCond cond; + guint64 total_bytes; + guint64 done_bytes; + guint64 current_bytes; /* of the file being copied */ + guint total_files; + guint done_files; + gchar *current_name; + gboolean counting; + + QuestionKind question; + gchar *question_name; + gchar *question_folder; + gchar *question_text; + gchar *suggested_name; + gboolean question_both_folders; + gboolean question_can_replace; + Answer answer; + gchar *answer_name; + gboolean answered; + + /* thread side */ + Answer conflict_for_all; /* ANSWER_NONE until "for all" */ + Answer error_for_all; + gboolean all_moved; +}; + +/* ---------------------------------------------------------------- */ +/* names */ + +static gchar * +file_display_name (GFile *file) +{ + gchar *basename = g_file_get_basename (file); + gchar *name = g_filename_display_name (basename ? basename : ""); + + g_free (basename); + return name; +} + +/* "name (2).ext": the first of these that is not there yet in @dir. + * Copies into the same folder start at "name (copy).ext". */ +static GFile * +free_name_in (GFile *dir, + const gchar *basename, + gboolean as_copy, + gchar **display) +{ + const gchar *dot = strrchr (basename, '.'); + gchar *stem, *ext; + gint i; + + /* ".hidden" and "folder" have no extension; "a.tar.gz" keeps ".gz" */ + if (dot && dot != basename && dot[1] != '\0') + { + stem = g_strndup (basename, dot - basename); + ext = g_strdup (dot); + } + else + { + stem = g_strdup (basename); + ext = g_strdup (""); + } + + for (i = as_copy ? 1 : 2; i < 10000; i++) + { + gchar *candidate; + GFile *file; + + if (as_copy && i == 1) + /* Translators: the name of a copy of a file in the same folder, + * "%s" is the name without its extension */ + candidate = g_strdup_printf (_("%s (copy)"), stem); + else if (as_copy) + /* Translators: the name of a further copy of a file in the same + * folder, "%s" is the name without its extension, "%d" the number */ + candidate = g_strdup_printf (_("%s (copy %d)"), stem, i); + else + candidate = g_strdup_printf ("%s (%d)", stem, i); + + { + gchar *full = g_strconcat (candidate, ext, NULL); + + g_free (candidate); + file = g_file_get_child (dir, full); + if (!g_file_query_exists (file, NULL)) + { + if (display) + *display = g_filename_display_name (full); + g_free (full); + g_free (stem); + g_free (ext); + return file; + } + g_free (full); + g_object_unref (file); + } + } + + g_free (stem); + g_free (ext); + return NULL; +} + +/* ---------------------------------------------------------------- */ +/* progress, main loop side */ + +static void +update_row (GtkFileOperation *op) +{ + gchar *text, *name, *done_size, *total_size; + guint64 done, total; + guint files, total_files; + gboolean counting; + + g_mutex_lock (&op->mutex); + done = op->done_bytes + op->current_bytes; + total = op->total_bytes; + files = op->done_files; + total_files = op->total_files; + counting = op->counting; + name = g_strdup (op->current_name ? op->current_name : ""); + g_mutex_unlock (&op->mutex); + + if (counting) + { + gtk_progress_bar_pulse (GTK_PROGRESS_BAR (op->bar)); + text = g_strdup (op->kind == GTK_FILE_OPERATION_COPY ? _("Preparing to copy…") + : _("Preparing to move…")); + } + else + { + done_size = g_format_size (done); + total_size = g_format_size (total); + text = g_strdup_printf (op->kind == GTK_FILE_OPERATION_COPY + /* Translators: "Copying 'name': 3 of 10 files, 1.2 MB of 4.5 MB" */ + ? _("Copying “%s”: %u of %u files, %s of %s") + /* Translators: "Moving 'name': 3 of 10 files, 1.2 MB of 4.5 MB" */ + : _("Moving “%s”: %u of %u files, %s of %s"), + name, MIN (files + 1, total_files), total_files, + done_size, total_size); + g_free (done_size); + g_free (total_size); + + if (total > 0) + gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (op->bar), + CLAMP ((gdouble) done / total, 0.0, 1.0)); + else if (total_files > 0) + gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (op->bar), + CLAMP ((gdouble) files / total_files, 0.0, 1.0)); + } + + gtk_label_set_text (GTK_LABEL (op->label), text); + g_free (text); + g_free (name); +} + +static gboolean +update_timeout (gpointer data) +{ + GtkFileOperation *op = data; + + if (op->row && gtk_widget_get_visible (op->row)) + update_row (op); + + return TRUE; +} + +static gboolean +show_timeout (gpointer data) +{ + GtkFileOperation *op = data; + + op->show_id = 0; + if (op->row) + { + update_row (op); + gtk_widget_show (op->row); + } + + return FALSE; +} + +static void +cancel_clicked (GtkButton *button, + GtkFileOperation *op) +{ + _gtk_file_operation_cancel (op); +} + +static void +row_destroyed (GtkWidget *row, + GtkFileOperation *op) +{ + /* the file chooser went away: the operation goes on to its end + * without a row, and stops at the next question */ + op->row = NULL; + op->bar = NULL; + op->label = NULL; + op->done = NULL; + g_cancellable_cancel (op->cancellable); +} + +static void +make_row (GtkFileOperation *op) +{ + GtkWidget *vbox, *button; + + op->row = gtk_hbox_new (FALSE, 6); + vbox = gtk_vbox_new (FALSE, 2); + gtk_box_pack_start (GTK_BOX (op->row), vbox, TRUE, TRUE, 0); + + op->label = gtk_label_new (NULL); + gtk_misc_set_alignment (GTK_MISC (op->label), 0.0, 0.5); + gtk_label_set_ellipsize (GTK_LABEL (op->label), PANGO_ELLIPSIZE_MIDDLE); + gtk_box_pack_start (GTK_BOX (vbox), op->label, FALSE, FALSE, 0); + + op->bar = gtk_progress_bar_new (); + gtk_progress_bar_set_pulse_step (GTK_PROGRESS_BAR (op->bar), 0.1); + gtk_box_pack_start (GTK_BOX (vbox), op->bar, FALSE, FALSE, 0); + + button = gtk_button_new_from_stock (GTK_STOCK_CANCEL); + g_signal_connect (button, "clicked", G_CALLBACK (cancel_clicked), op); + gtk_box_pack_start (GTK_BOX (op->row), button, FALSE, FALSE, 0); + + gtk_widget_show_all (vbox); + gtk_widget_show (button); + /* the row itself only after half a second */ + gtk_box_pack_start (GTK_BOX (op->box), op->row, FALSE, FALSE, 0); + g_signal_connect (op->row, "destroy", G_CALLBACK (row_destroyed), op); +} + +/* ---------------------------------------------------------------- */ +/* questions */ + +enum +{ + RESPONSE_SKIP = 1, + RESPONSE_REPLACE, + RESPONSE_RENAME +}; + +typedef struct +{ + GtkFileOperation *op; + GtkWidget *all_check; + GtkWidget *entry; +} AskData; + +static void +answer (GtkFileOperation *op, + Answer value, + const gchar *name, + gboolean for_all) +{ + g_mutex_lock (&op->mutex); + op->answer = value; + g_free (op->answer_name); + op->answer_name = g_strdup (name); + if (for_all) + { + if (op->question == QUESTION_CONFLICT) + op->conflict_for_all = value; + else + op->error_for_all = value; + } + op->answered = TRUE; + g_cond_signal (&op->cond); + g_mutex_unlock (&op->mutex); +} + +static void +ask_response (GtkDialog *dialog, + gint response, + AskData *ask) +{ + GtkFileOperation *op = ask->op; + gboolean for_all = ask->all_check && + gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ask->all_check)); + const gchar *name = ask->entry ? gtk_entry_get_text (GTK_ENTRY (ask->entry)) : NULL; + Answer value; + + switch (response) + { + case RESPONSE_SKIP: + value = ANSWER_SKIP; + break; + case RESPONSE_REPLACE: + value = ANSWER_REPLACE; + break; + case RESPONSE_RENAME: + if (name == NULL || *name == '\0' || strchr (name, '/') != NULL +#ifdef G_OS_WIN32 + || strchr (name, '\\') != NULL +#endif + ) + return; + value = ANSWER_RENAME; + break; + default: + value = ANSWER_CANCEL; + break; + } + + /* the name of the user is for this file only */ + answer (op, value, name, for_all && value != ANSWER_RENAME ? TRUE : FALSE); + if (for_all && value == ANSWER_RENAME) + { + g_mutex_lock (&op->mutex); + op->conflict_for_all = ANSWER_RENAME; + g_mutex_unlock (&op->mutex); + } + + gtk_widget_destroy (GTK_WIDGET (dialog)); + g_slice_free (AskData, ask); +} + +static void +all_toggled (GtkToggleButton *check, + GtkWidget *entry) +{ + /* "for all" renames to free names, not to one */ + gtk_widget_set_sensitive (entry, !gtk_toggle_button_get_active (check)); +} + +static gboolean +ask_in_main_loop (gpointer data) +{ + GtkFileOperation *op = data; + GtkWidget *dialog, *area; + AskData *ask; + gchar *name, *folder, *text, *suggested; + gboolean both_folders, can_replace; + QuestionKind kind; + + g_mutex_lock (&op->mutex); + kind = op->question; + name = g_strdup (op->question_name); + folder = g_strdup (op->question_folder); + text = g_strdup (op->question_text); + suggested = g_strdup (op->suggested_name); + both_folders = op->question_both_folders; + can_replace = op->question_can_replace; + g_mutex_unlock (&op->mutex); + + if (op->row == NULL) + { + /* nothing to ask in any more */ + answer (op, ANSWER_CANCEL, NULL, FALSE); + goto out; + } + + ask = g_slice_new0 (AskData); + ask->op = op; + + if (kind == QUESTION_CONFLICT) + { + dialog = gtk_message_dialog_new (op->parent, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_NONE, + both_folders + ? _("A folder named “%s” already exists in “%s”.") + : _("A file named “%s” already exists in “%s”."), + name, folder); + gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", + both_folders + ? _("Merging adds the contents to the folder there; files of the same name are asked for.") + : can_replace + ? _("Replacing it overwrites its contents.") + : _("It can be kept under another name.")); + + area = gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (dialog)); + { + GtkWidget *hbox = gtk_hbox_new (FALSE, 6); + GtkWidget *label = gtk_label_new_with_mnemonic (_("_New name:")); + + ask->entry = gtk_entry_new (); + gtk_entry_set_text (GTK_ENTRY (ask->entry), suggested ? suggested : ""); + gtk_entry_set_activates_default (GTK_ENTRY (ask->entry), FALSE); + gtk_label_set_mnemonic_widget (GTK_LABEL (label), ask->entry); + gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (hbox), ask->entry, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (area), hbox, FALSE, FALSE, 0); + gtk_widget_show_all (hbox); + } + + ask->all_check = gtk_check_button_new_with_mnemonic (_("Do this for _all further conflicts")); + gtk_box_pack_start (GTK_BOX (area), ask->all_check, FALSE, FALSE, 0); + gtk_widget_show (ask->all_check); + g_signal_connect (ask->all_check, "toggled", G_CALLBACK (all_toggled), ask->entry); + + gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL); + gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Skip"), RESPONSE_SKIP); + gtk_dialog_add_button (GTK_DIALOG (dialog), _("Re_name"), RESPONSE_RENAME); + if (can_replace) + gtk_dialog_add_button (GTK_DIALOG (dialog), + both_folders ? _("_Merge") : _("_Replace"), + RESPONSE_REPLACE); + gtk_dialog_set_default_response (GTK_DIALOG (dialog), + can_replace ? RESPONSE_REPLACE : RESPONSE_RENAME); + } + else + { + dialog = gtk_message_dialog_new (op->parent, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_NONE, + op->kind == GTK_FILE_OPERATION_COPY + ? _("“%s” could not be copied.") + : _("“%s” could not be moved."), + name); + gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", text); + + area = gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (dialog)); + ask->all_check = gtk_check_button_new_with_mnemonic (_("Skip _all further errors")); + gtk_box_pack_start (GTK_BOX (area), ask->all_check, FALSE, FALSE, 0); + gtk_widget_show (ask->all_check); + + gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL); + gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Skip"), RESPONSE_SKIP); + gtk_dialog_set_default_response (GTK_DIALOG (dialog), RESPONSE_SKIP); + } + + gtk_window_set_title (GTK_WINDOW (dialog), ""); + gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); + g_signal_connect (dialog, "response", G_CALLBACK (ask_response), ask); + gtk_widget_show (dialog); + + out: + g_free (name); + g_free (folder); + g_free (text); + g_free (suggested); + return FALSE; +} + +/* In the thread: asks and waits. Returns the answer; for a rename the + * new basename goes into @new_name. */ +static Answer +ask (GtkFileOperation *op, + QuestionKind kind, + GFile *file, + const gchar *text, + GFile *suggestion, + gboolean both_folders, + gboolean can_replace, + gchar **new_name) +{ + GFile *parent; + Answer value; + + if (g_cancellable_is_cancelled (op->cancellable)) + return ANSWER_CANCEL; + + g_mutex_lock (&op->mutex); + op->question = kind; + g_free (op->question_name); + op->question_name = file_display_name (file); + g_free (op->question_folder); + parent = g_file_get_parent (file); + op->question_folder = parent ? file_display_name (parent) : g_strdup ("/"); + if (parent) + g_object_unref (parent); + g_free (op->question_text); + op->question_text = g_strdup (text); + g_free (op->suggested_name); + op->suggested_name = suggestion ? file_display_name (suggestion) : NULL; + op->question_both_folders = both_folders; + op->question_can_replace = can_replace; + op->answered = FALSE; + g_mutex_unlock (&op->mutex); + + gdk_threads_add_idle (ask_in_main_loop, op); + + g_mutex_lock (&op->mutex); + while (!op->answered) + g_cond_wait (&op->cond, &op->mutex); + value = op->answer; + if (new_name) + *new_name = g_strdup (op->answer_name); + g_mutex_unlock (&op->mutex); + + if (value == ANSWER_CANCEL) + g_cancellable_cancel (op->cancellable); + + return value; +} + +/* ---------------------------------------------------------------- */ +/* the work, in the thread */ + +static void +set_current (GtkFileOperation *op, + GFile *file) +{ + gchar *name = file_display_name (file); + + g_mutex_lock (&op->mutex); + g_free (op->current_name); + op->current_name = name; + op->current_bytes = 0; + g_mutex_unlock (&op->mutex); +} + +static void +add_done (GtkFileOperation *op, + guint64 bytes, + guint files) +{ + g_mutex_lock (&op->mutex); + op->done_bytes += bytes; + op->done_files += files; + op->current_bytes = 0; + g_mutex_unlock (&op->mutex); +} + +static void +copy_progress (goffset current, + goffset total, + gpointer data) +{ + GtkFileOperation *op = data; + + g_mutex_lock (&op->mutex); + op->current_bytes = current; + g_mutex_unlock (&op->mutex); +} + +/* the size and number of files of @file, folders with what is in them */ +static void +count (GtkFileOperation *op, + GFile *file, + GFileInfo *info, + guint64 *bytes, + guint *files) +{ + if (g_cancellable_is_cancelled (op->cancellable)) + return; + + if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) + { + GFileEnumerator *children; + GFileInfo *child_info; + + *files += 1; + children = g_file_enumerate_children (file, + G_FILE_ATTRIBUTE_STANDARD_NAME "," + G_FILE_ATTRIBUTE_STANDARD_TYPE "," + G_FILE_ATTRIBUTE_STANDARD_SIZE, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + op->cancellable, NULL); + if (!children) + return; + + while ((child_info = g_file_enumerator_next_file (children, op->cancellable, NULL))) + { + GFile *child = g_file_get_child (file, g_file_info_get_name (child_info)); + + count (op, child, child_info, bytes, files); + g_object_unref (child); + g_object_unref (child_info); + } + g_object_unref (children); + } + else + { + *files += 1; + if (g_file_info_get_file_type (info) == G_FILE_TYPE_REGULAR) + *bytes += g_file_info_get_size (info); + } +} + +static GFileInfo * +query (GFile *file, + GCancellable *cancellable, + GError **error) +{ + return g_file_query_info (file, + G_FILE_ATTRIBUTE_STANDARD_NAME "," + G_FILE_ATTRIBUTE_STANDARD_TYPE "," + G_FILE_ATTRIBUTE_STANDARD_SIZE, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + cancellable, error); +} + +/* What to do with an error: TRUE to go on (skipped), FALSE to stop */ +static gboolean +handle_error (GtkFileOperation *op, + GFile *file, + GError *error) +{ + Answer value; + + op->all_moved = FALSE; + + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) || + g_cancellable_is_cancelled (op->cancellable)) + return FALSE; + + if (op->error_for_all == ANSWER_SKIP) + return TRUE; + + value = ask (op, QUESTION_ERROR, file, error->message, NULL, FALSE, FALSE, NULL); + + return value == ANSWER_SKIP; +} + +static gboolean transfer (GtkFileOperation *op, GFile *source, GFileInfo *info, + GFile *target); + +/* Counts @source as done (all of it, folders with what is in them): + * for what is skipped or failed, or moved at once */ +static void +add_done_tree (GtkFileOperation *op, + GFile *source, + GFileInfo *info) +{ + guint64 bytes = 0; + guint files = 0; + + count (op, source, info, &bytes, &files); + add_done (op, bytes, files); +} + +/* Copies (or moves, by copying) the contents of the folder @source + * into the folder @target. FALSE when the operation is to stop. */ +static gboolean +transfer_children (GtkFileOperation *op, + GFile *source, + GFile *target) +{ + GFileEnumerator *children; + GFileInfo *child_info; + GError *error = NULL; + gboolean go_on = TRUE; + + children = g_file_enumerate_children (source, + G_FILE_ATTRIBUTE_STANDARD_NAME "," + G_FILE_ATTRIBUTE_STANDARD_TYPE "," + G_FILE_ATTRIBUTE_STANDARD_SIZE, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + op->cancellable, &error); + if (!children) + { + go_on = handle_error (op, source, error); + g_error_free (error); + return go_on; + } + + while (go_on) + { + GFile *child, *child_target; + + child_info = g_file_enumerator_next_file (children, op->cancellable, &error); + if (!child_info) + { + if (error) + { + go_on = handle_error (op, source, error); + g_clear_error (&error); + } + break; + } + + child = g_file_get_child (source, g_file_info_get_name (child_info)); + child_target = g_file_get_child (target, g_file_info_get_name (child_info)); + go_on = transfer (op, child, child_info, child_target); + g_object_unref (child_target); + g_object_unref (child); + g_object_unref (child_info); + } + + g_object_unref (children); + return go_on; +} + +/* What to do where @target is there already: ANSWER_REPLACE (merge + * for folders), ANSWER_RENAME with the new target in @renamed, + * ANSWER_SKIP, ANSWER_CANCEL; ANSWER_NONE when nothing is there. */ +static Answer +resolve_conflict (GtkFileOperation *op, + GFileInfo *info, + GFile *target, + GFile **renamed) +{ + GFileInfo *there; + gboolean is_folder, there_folder, both_folders, can_replace; + GFile *parent, *suggestion; + gchar *name = NULL; + Answer value; + + *renamed = NULL; + + there = query (target, op->cancellable, NULL); + if (!there) + return ANSWER_NONE; + + is_folder = g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY; + there_folder = g_file_info_get_file_type (there) == G_FILE_TYPE_DIRECTORY; + g_object_unref (there); + both_folders = is_folder && there_folder; + /* a folder over a file or the other way round: only another name */ + can_replace = both_folders || (!is_folder && !there_folder); + + parent = g_file_get_parent (target); + suggestion = free_name_in (parent, g_file_info_get_name (info), FALSE, NULL); + + value = op->conflict_for_all; + if (value == ANSWER_REPLACE && !can_replace) + value = ANSWER_NONE; + if (value == ANSWER_NONE) + value = ask (op, QUESTION_CONFLICT, target, NULL, suggestion, + both_folders, can_replace, &name); + + if (value == ANSWER_RENAME) + { + if (name && *name) + { + gchar *filename = g_filename_from_utf8 (name, -1, NULL, NULL, NULL); + + if (filename) + *renamed = g_file_get_child (parent, filename); + g_free (filename); + } + if (*renamed && g_file_query_exists (*renamed, op->cancellable)) + { + /* the name given is taken as well */ + g_object_unref (*renamed); + *renamed = NULL; + } + if (!*renamed && suggestion) + *renamed = g_object_ref (suggestion); + if (!*renamed) + value = ANSWER_SKIP; + } + + g_free (name); + if (suggestion) + g_object_unref (suggestion); + g_object_unref (parent); + + return value; +} + +/* One file or folder: FALSE when the operation is to stop */ +static gboolean +transfer (GtkFileOperation *op, + GFile *source, + GFileInfo *info, + GFile *target) +{ + gboolean is_folder = g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY; + gboolean move = op->kind == GTK_FILE_OPERATION_MOVE; + GFileCopyFlags flags = G_FILE_COPY_NOFOLLOW_SYMLINKS; + GError *error = NULL; + GFile *renamed = NULL; + gboolean replace = FALSE, go_on = TRUE; + + if (g_cancellable_is_cancelled (op->cancellable)) + return FALSE; + + set_current (op, source); + + switch (resolve_conflict (op, info, target, &renamed)) + { + case ANSWER_NONE: + break; + case ANSWER_REPLACE: + replace = TRUE; + break; + case ANSWER_RENAME: + target = renamed; + break; + case ANSWER_SKIP: + op->all_moved = FALSE; + add_done_tree (op, source, info); + return TRUE; + default: + return FALSE; + } + + /* moving: a rename first, instant on the same file system; a folder + * that is merged into another one can not be renamed onto it */ + if (move && !(is_folder && replace)) + { + if (g_file_move (source, target, + flags | G_FILE_COPY_NO_FALLBACK_FOR_MOVE | + (replace ? G_FILE_COPY_OVERWRITE : 0), + op->cancellable, NULL, NULL, &error)) + { + add_done_tree (op, target, info); + goto out; + } + + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED) && + !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE) && + !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE)) + { + go_on = handle_error (op, source, error); + g_clear_error (&error); + add_done_tree (op, source, info); + goto out; + } + g_clear_error (&error); + } + + if (is_folder) + { + gboolean whole; + + if (!replace && !g_file_make_directory (target, op->cancellable, &error)) + { + go_on = handle_error (op, source, error); + g_clear_error (&error); + add_done_tree (op, source, info); + goto out; + } + add_done (op, 0, 1); + + whole = op->all_moved; + op->all_moved = TRUE; + go_on = transfer_children (op, source, target); + + /* moving: the folder goes once everything in it went */ + if (go_on && move && op->all_moved) + g_file_delete (source, op->cancellable, NULL); + op->all_moved = op->all_moved && whole; + goto out; + } + + /* a file or a link */ + if (!g_file_copy (source, target, + flags | (replace ? G_FILE_COPY_OVERWRITE : 0) | + (move ? G_FILE_COPY_ALL_METADATA : 0), + op->cancellable, copy_progress, op, &error)) + { + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + { + /* a file being written anew goes again; one that was being + * replaced is written beside it by GIO and stays as it was */ + if (!replace) + g_file_delete (target, NULL, NULL); + go_on = FALSE; + } + else + go_on = handle_error (op, source, error); + + g_clear_error (&error); + add_done_tree (op, source, info); + goto out; + } + + add_done (op, g_file_info_get_file_type (info) == G_FILE_TYPE_REGULAR + ? g_file_info_get_size (info) : 0, 1); + + if (move && !g_file_delete (source, op->cancellable, &error)) + { + go_on = handle_error (op, source, error); + g_clear_error (&error); + } + + out: + if (renamed) + g_object_unref (renamed); + return go_on; +} + +static gboolean finish_in_main_loop (gpointer data); + +static gpointer +operation_thread (gpointer data) +{ + GtkFileOperation *op = data; + GList *l; + guint64 bytes = 0; + guint files = 0; + + /* Moving within one file system is a rename, at once: those are done + * first, without looking into folders; what is left is counted and + * copied. Names that are there already wait for their question. */ + if (op->kind == GTK_FILE_OPERATION_MOVE) + { + GList *rest = NULL; + + for (l = op->sources; l; l = l->next) + { + GFile *source = l->data, *parent = g_file_get_parent (source), *target; + gchar *basename = g_file_get_basename (source); + gboolean moved = FALSE; + + if (parent && !g_file_equal (parent, op->destination) && + !g_file_equal (op->destination, source) && + !g_file_has_prefix (op->destination, source)) + { + target = g_file_get_child (op->destination, basename); + moved = g_file_move (source, target, + G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_NO_FALLBACK_FOR_MOVE, + op->cancellable, NULL, NULL, NULL); + g_object_unref (target); + } + + if (moved) + g_object_unref (source); + else + rest = g_list_prepend (rest, source); + + g_free (basename); + if (parent) + g_object_unref (parent); + } + + g_list_free (op->sources); + op->sources = g_list_reverse (rest); + } + + /* what there is to do, for the progress */ + for (l = op->sources; l; l = l->next) + { + GFileInfo *info = query (l->data, op->cancellable, NULL); + + if (info) + { + count (op, l->data, info, &bytes, &files); + g_object_unref (info); + } + } + + g_mutex_lock (&op->mutex); + op->total_bytes = bytes; + op->total_files = files; + op->counting = FALSE; + g_mutex_unlock (&op->mutex); + + for (l = op->sources; l; l = l->next) + { + GFile *source = l->data, *target, *parent; + GFileInfo *info; + GError *error = NULL; + gchar *basename; + + if (g_cancellable_is_cancelled (op->cancellable)) + break; + + info = query (source, op->cancellable, &error); + if (!info) + { + if (!handle_error (op, source, error)) + { + g_error_free (error); + break; + } + g_error_free (error); + continue; + } + + parent = g_file_get_parent (source); + basename = g_file_get_basename (source); + + /* into itself or one of its own folders: not possible */ + if (g_file_equal (op->destination, source) || + g_file_has_prefix (op->destination, source)) + { + GError *loop = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE, + _("A folder can not be copied or moved into itself.")); + gboolean go_on = handle_error (op, source, loop); + + g_error_free (loop); + g_free (basename); + if (parent) + g_object_unref (parent); + g_object_unref (info); + if (!go_on) + break; + continue; + } + + if (parent && g_file_equal (parent, op->destination)) + { + /* into the folder it is in: a copy gets a name of its own, + * a move has nothing to do */ + if (op->kind == GTK_FILE_OPERATION_MOVE) + { + guint64 b = 0; + guint f = 0; + + count (op, source, info, &b, &f); + add_done (op, b, f); + g_free (basename); + g_object_unref (parent); + g_object_unref (info); + continue; + } + target = free_name_in (op->destination, basename, TRUE, NULL); + } + else + target = g_file_get_child (op->destination, basename); + + if (target) + { + gboolean go_on = transfer (op, source, info, target); + + g_object_unref (target); + if (!go_on) + { + g_free (basename); + if (parent) + g_object_unref (parent); + g_object_unref (info); + break; + } + } + + g_free (basename); + if (parent) + g_object_unref (parent); + g_object_unref (info); + } + + gdk_threads_add_idle (finish_in_main_loop, op); + return NULL; +} + +/* ---------------------------------------------------------------- */ +/* start and end */ + +static void +operation_free (GtkFileOperation *op) +{ + g_list_foreach (op->sources, (GFunc) g_object_unref, NULL); + g_list_free (op->sources); + g_object_unref (op->destination); + g_object_unref (op->cancellable); + g_mutex_clear (&op->mutex); + g_cond_clear (&op->cond); + g_free (op->current_name); + g_free (op->question_name); + g_free (op->question_folder); + g_free (op->question_text); + g_free (op->suggested_name); + g_free (op->answer_name); + g_slice_free (GtkFileOperation, op); +} + +static gboolean +finish_in_main_loop (gpointer data) +{ + GtkFileOperation *op = data; + gboolean cancelled = g_cancellable_is_cancelled (op->cancellable); + + if (op->update_id) + g_source_remove (op->update_id); + if (op->show_id) + g_source_remove (op->show_id); + if (op->parent) + g_object_remove_weak_pointer (G_OBJECT (op->parent), (gpointer *) &op->parent); + + if (op->row) + { + g_signal_handlers_disconnect_by_func (op->row, row_destroyed, op); + gtk_widget_destroy (op->row); + op->row = NULL; + } + + if (op->done) + op->done (op, cancelled, op->all_moved && !cancelled, op->user_data); + + operation_free (op); + return FALSE; +} + +GtkFileOperation * +_gtk_file_operation_start (GtkFileOperationKind kind, + GList *sources, + GFile *destination, + GtkWidget *box, + GtkWindow *parent, + GtkFileOperationDoneFunc done, + gpointer user_data) +{ + GtkFileOperation *op; + GThread *thread; + GList *l; + + g_return_val_if_fail (sources != NULL, NULL); + g_return_val_if_fail (G_IS_FILE (destination), NULL); + g_return_val_if_fail (GTK_IS_BOX (box), NULL); + + op = g_slice_new0 (GtkFileOperation); + op->kind = kind; + for (l = sources; l; l = l->next) + op->sources = g_list_prepend (op->sources, g_object_ref (l->data)); + op->sources = g_list_reverse (op->sources); + op->destination = g_object_ref (destination); + op->cancellable = g_cancellable_new (); + op->box = box; + op->parent = parent; + if (parent) + g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *) &op->parent); + op->done = done; + op->user_data = user_data; + op->counting = TRUE; + op->all_moved = TRUE; + op->conflict_for_all = ANSWER_NONE; + op->error_for_all = ANSWER_NONE; + g_mutex_init (&op->mutex); + g_cond_init (&op->cond); + + make_row (op); + op->show_id = gdk_threads_add_timeout (500, show_timeout, op); + op->update_id = gdk_threads_add_timeout (100, update_timeout, op); + + thread = g_thread_new ("gtk-file-operation", operation_thread, op); + g_thread_unref (thread); + + return op; +} + +void +_gtk_file_operation_cancel (GtkFileOperation *operation) +{ + g_cancellable_cancel (operation->cancellable); +} diff --git a/gtk/gtkfileoperation.h b/gtk/gtkfileoperation.h new file mode 100644 index 000000000..8261a240c --- /dev/null +++ b/gtk/gtkfileoperation.h @@ -0,0 +1,70 @@ +/* GTK - The GIMP Toolkit + * gtkfileoperation.h: copying and moving files for the file chooser + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* Not installed: for the file chooser of libgtk only */ + +#ifndef __GTK_FILE_OPERATION_H__ +#define __GTK_FILE_OPERATION_H__ + +#include +#include "gtkwidget.h" +#include "gtkwindow.h" + +G_BEGIN_DECLS + +typedef enum +{ + GTK_FILE_OPERATION_COPY, + GTK_FILE_OPERATION_MOVE +} GtkFileOperationKind; + +typedef struct _GtkFileOperation GtkFileOperation; + +/* Called in the main loop when the operation is over: @moved tells + * whether all sources of a move went (none was skipped or failed) */ +typedef void (*GtkFileOperationDoneFunc) (GtkFileOperation *operation, + gboolean cancelled, + gboolean moved, + gpointer user_data); + +/* Copies or moves @sources (a list of GFile) into the folder + * @destination in a thread of its own. Its progress and a Cancel button + * are shown in a row that is packed into @box half a second after the + * start, if it runs that long; questions (a file of the same name, an + * error) are asked in dialogs transient for @parent. The operation + * frees itself after @done. */ +GtkFileOperation *_gtk_file_operation_start (GtkFileOperationKind kind, + GList *sources, + GFile *destination, + GtkWidget *box, + GtkWindow *parent, + GtkFileOperationDoneFunc done, + gpointer user_data); + +void _gtk_file_operation_cancel (GtkFileOperation *operation); + +/* The clipboard of file managers: x-special/gnome-copied-files ("copy" + * or "cut" and URIs, as Nautilus, Caja, Thunar and PCManFM use it) and + * text/uri-list with application/x-kde-cutselection (Dolphin) */ +#define GTK_FILE_CLIPBOARD_GNOME "x-special/gnome-copied-files" +#define GTK_FILE_CLIPBOARD_KDE_CUT "application/x-kde-cutselection" + +G_END_DECLS + +#endif /* __GTK_FILE_OPERATION_H__ */ diff --git a/gtk/makefile.msc.in b/gtk/makefile.msc.in index 67a0ae4d1..7387c4d56 100644 --- a/gtk/makefile.msc.in +++ b/gtk/makefile.msc.in @@ -120,6 +120,7 @@ gtk_OBJECTS_file = \ gtkfilechooserentry.obj \ gtkfilechoosersettings.obj \ gtkfilechooserutils.obj \ + gtkfileoperation.obj \ gtkfilechooserwidget.obj \ gtkfilefilter.obj \ gtkfilesystem.obj \ diff --git a/po-properties/Makevars b/po-properties/Makevars index 379c4eebd..586cf7811 100644 --- a/po-properties/Makevars +++ b/po-properties/Makevars @@ -13,7 +13,7 @@ subdir = po-properties top_builddir = .. # These options get passed to xgettext. -XGETTEXT_OPTIONS = --keyword=_ --keyword=N_ +XGETTEXT_OPTIONS = --from-code=UTF-8 --keyword=_ --keyword=N_ # This is the copyright holder that gets inserted into the header of the # $(DOMAIN).pot file. Set this to the copyright holder of the surrounding diff --git a/po/Makevars b/po/Makevars index 8b08dc880..9ea6a00e0 100644 --- a/po/Makevars +++ b/po/Makevars @@ -13,7 +13,7 @@ subdir = po top_builddir = .. # These options get passed to xgettext. -XGETTEXT_OPTIONS = --keyword=_ --keyword=N_ +XGETTEXT_OPTIONS = --from-code=UTF-8 --keyword=_ --keyword=N_ # This is the copyright holder that gets inserted into the header of the # $(DOMAIN).pot file. Set this to the copyright holder of the surrounding diff --git a/po/POTFILES.in b/po/POTFILES.in index 94143bead..8c52eaa76 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -76,6 +76,7 @@ gtk/gtkfilechooserembed.c gtk/gtkfilechooserentry.c gtk/gtkfilechoosersettings.c gtk/gtkfilechooserutils.c +gtk/gtkfileoperation.c gtk/gtkfilechooserwidget.c gtk/gtkfilefilter.c gtk/gtkfilesel.c diff --git a/po/de.po b/po/de.po index 98e1cdef3..49ab17f6b 100644 --- a/po/de.po +++ b/po/de.po @@ -6130,3 +6130,91 @@ msgstr "" #~ msgid "X" #~ msgstr "X" + +#: gtk/gtkfileoperation.c +#, c-format +msgid "%s (copy)" +msgstr "%s (Kopie)" + +#: gtk/gtkfileoperation.c +#, c-format +msgid "%s (copy %d)" +msgstr "%s (Kopie %d)" + +#: gtk/gtkfileoperation.c +msgid "Preparing to copy…" +msgstr "Kopieren wird vorbereitet …" + +#: gtk/gtkfileoperation.c +msgid "Preparing to move…" +msgstr "Verschieben wird vorbereitet …" + +#: gtk/gtkfileoperation.c +#, c-format +msgid "Copying “%s”: %u of %u files, %s of %s" +msgstr "„%s“ wird kopiert: %u von %u Dateien, %s von %s" + +#: gtk/gtkfileoperation.c +#, c-format +msgid "Moving “%s”: %u of %u files, %s of %s" +msgstr "„%s“ wird verschoben: %u von %u Dateien, %s von %s" + +#: gtk/gtkfileoperation.c +#, c-format +msgid "A folder named “%s” already exists in “%s”." +msgstr "Ein Ordner namens „%s“ existiert bereits in „%s“." + +#: gtk/gtkfileoperation.c +#, c-format +msgid "A file named “%s” already exists in “%s”." +msgstr "Eine Datei namens „%s“ existiert bereits in „%s“." + +#: gtk/gtkfileoperation.c +msgid "Merging adds the contents to the folder there; files of the same name are asked for." +msgstr "Zusammenführen fügt den Inhalt zum vorhandenen Ordner hinzu; bei Dateien gleichen Namens wird nachgefragt." + +#: gtk/gtkfileoperation.c +msgid "Replacing it overwrites its contents." +msgstr "Ersetzen überschreibt ihren Inhalt." + +#: gtk/gtkfileoperation.c +msgid "It can be kept under another name." +msgstr "Sie kann unter einem anderen Namen behalten werden." + +#: gtk/gtkfileoperation.c +msgid "_New name:" +msgstr "_Neuer Name:" + +#: gtk/gtkfileoperation.c +msgid "Do this for _all further conflicts" +msgstr "Für _alle weiteren Konflikte so verfahren" + +#: gtk/gtkfileoperation.c +msgid "_Skip" +msgstr "Ü_berspringen" + +#: gtk/gtkfileoperation.c +msgid "Re_name" +msgstr "_Umbenennen" + +#: gtk/gtkfileoperation.c +msgid "_Merge" +msgstr "_Zusammenführen" + +#: gtk/gtkfileoperation.c +#, c-format +msgid "“%s” could not be copied." +msgstr "„%s“ konnte nicht kopiert werden." + +#: gtk/gtkfileoperation.c +#, c-format +msgid "“%s” could not be moved." +msgstr "„%s“ konnte nicht verschoben werden." + +#: gtk/gtkfileoperation.c +msgid "Skip _all further errors" +msgstr "_Alle weiteren Fehler überspringen" + +#: gtk/gtkfileoperation.c +msgid "A folder can not be copied or moved into itself." +msgstr "Ein Ordner kann nicht in sich selbst kopiert oder verschoben werden."