Skip to content

Commit 0e63732

Browse files
authored
Restore files from trash dialogs - present () not run () (#2546)
1 parent 56fb924 commit 0e63732

3 files changed

Lines changed: 41 additions & 33 deletions

File tree

libcore/FileUtils.vala

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,38 +80,43 @@ namespace Files.FileUtils {
8080
return parent_path;
8181
}
8282

83-
public void restore_files_from_trash (GLib.List<Files.File> files, Gtk.Widget? widget) {
84-
GLib.List<Files.File>? unhandled_files = null;
85-
var original_dirs_hash = get_trashed_files_original_directories (files, out unhandled_files);
86-
83+
public async void restore_files_from_trash (GLib.List<Files.File> files, Gtk.Widget? widget) {
84+
GLib.List<Files.File>? unhandled_files;
85+
var original_dirs_hash = yield get_trashed_files_original_directories (files, out unhandled_files);
8786
foreach (Files.File goffile in unhandled_files) {
8887
var message = _("Could not determine original location of \"%s\"").printf (goffile.get_display_name ());
89-
PF.Dialogs.show_warning_dialog (message, _("The item cannot be restored from trash"),
90-
(widget is Gtk.Window) ? widget as Gtk.Window : null );
88+
PF.Dialogs.show_warning_dialog (
89+
message,
90+
_("The item cannot be restored from trash"),
91+
(widget is Gtk.Window) ? widget as Gtk.Window : null
92+
);
93+
}
94+
95+
foreach (var original_dir in original_dirs_hash.get_keys ()) {
96+
var dir_files = original_dirs_hash.take (original_dir);
97+
Files.FileOperations.copy_move_link.begin (
98+
(owned) dir_files,
99+
original_dir,
100+
Gdk.DragAction.MOVE,
101+
widget,
102+
null
103+
);
91104
}
92-
93-
original_dirs_hash.foreach ((original_dir, dir_files) => {
94-
Files.FileOperations.copy_move_link.begin (dir_files,
95-
original_dir,
96-
Gdk.DragAction.MOVE,
97-
widget,
98-
null);
99-
});
100105
}
101106

102-
private GLib.HashTable<GLib.File, GLib.List<GLib.File>>
103-
get_trashed_files_original_directories (GLib.List<Files.File> files, out GLib.List<Files.File> unhandled_files) {
107+
private async GLib.HashTable<GLib.File, GLib.List<GLib.File>> get_trashed_files_original_directories (
108+
GLib.List<Files.File> files,
109+
out GLib.List<Files.File> unhandled_files) {
104110

105111
var directories = new GLib.HashTable<GLib.File, GLib.List<GLib.File>> (GLib.File.hash, GLib.File.equal);
106112
unhandled_files = null;
107113

108114
var exists_map = new Gee.HashMap<string, int> ();
109-
foreach (unowned Files.File goffile in files) {
115+
foreach (var goffile in files) {
110116
/* Check it is a valid file (e.g. not a dummy row from list view) */
111117
if (goffile == null || goffile.location == null) {
112118
continue;
113119
}
114-
115120
/* Check that file is in root of trash. If not, do not try to restore
116121
* (it will be restored with its parent anyway) */
117122
if (Path.get_dirname (goffile.uri) == "trash:") {
@@ -122,11 +127,13 @@ namespace Files.FileUtils {
122127
if (exists_map.has_key (original_dir.get_path ())) {
123128
exists = exists_map.@get (original_dir.get_path ());
124129
}
125-
if (exists == 1 || (exists < 0 && ensure_exists (original_dir))) {
126-
if (exists < 0) {
130+
131+
if (exists == 1 || (exists < 0 && yield ensure_exists (original_dir))) {
132+
if (exists < 0) { // original existed or was created
127133
exists_map.@set (original_dir.get_path (), 1); // Do not need to check this path again
128134
}
129-
GLib.List<GLib.File>? dir_files = directories.take (original_dir);
135+
136+
List<GLib.File>? dir_files = directories.take (original_dir);
130137
dir_files.prepend (goffile.location);
131138
directories.insert (original_dir, (owned)dir_files);
132139
} else {
@@ -174,12 +181,12 @@ namespace Files.FileUtils {
174181
}
175182
}
176183

177-
private bool ensure_exists (GLib.File file) {
184+
private async bool ensure_exists (GLib.File file) {
178185
if (file.query_exists ()) {
179186
return true;
180187
}
181188

182-
bool success = false;
189+
bool created = false;
183190
var dialog = new Granite.MessageDialog.with_image_from_icon_name (
184191
_("The original folder %s no longer exists").printf (file.get_path ()),
185192
_("The folder can be recreated and selected files that were originally there will be restored to it. Otherwise, files that were in this folder will not be restored."),
@@ -189,20 +196,22 @@ namespace Files.FileUtils {
189196
dialog.add_button (_("Ignore"), Gtk.ResponseType.CANCEL);
190197
dialog.add_button (_("Recreate"), Gtk.ResponseType.ACCEPT);
191198
dialog.set_default_response (Gtk.ResponseType.ACCEPT);
192-
199+
dialog.set_modal (true);
193200
dialog.response.connect ((res) => {
194201
switch (res) {
195202
case Gtk.ResponseType.ACCEPT:
196203
try {
197-
success = file.make_directory_with_parents ();
204+
file.make_directory_with_parents ();
205+
created = true;
198206
} catch (Error e) {
199207
var error_dialog = new Granite.MessageDialog.with_image_from_icon_name (
200208
_("Could not recreate folder %s. Will ignore all files in this folder").printf (file.get_path ()),
201209
e.message,
202210
"dialog-error",
203211
Gtk.ButtonsType.CLOSE
204212
);
205-
error_dialog.response.connect (() => error_dialog.close ());
213+
214+
error_dialog.response.connect (error_dialog.destroy);
206215
error_dialog.present ();
207216
}
208217

@@ -213,12 +222,12 @@ namespace Files.FileUtils {
213222
}
214223

215224
dialog.destroy ();
225+
ensure_exists.callback (); // Continue from after yield statement
216226
});
217227

218-
// Need to continue to use run () in Gtk3 in order to get modal dialog as
219-
// we need to return a result
220-
dialog.run ();
221-
return success;
228+
dialog.present ();
229+
yield;
230+
return created;
222231
}
223232

224233
public string? get_path_for_symlink (GLib.File file) {

plugins/pantheon-files-trash/plugin.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class Files.Plugins.Trash : Files.Plugins.Base {
8383
}
8484

8585
unowned GLib.List<Files.File> selection = view.get_selected_files ();
86-
FileUtils.restore_files_from_trash (selection, window);
86+
FileUtils.restore_files_from_trash.begin (selection, window);
8787
});
8888

8989
delete_button.clicked.connect (() => {

src/View/AbstractDirectoryView.vala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,8 +1119,7 @@ namespace Files {
11191119

11201120
private void on_selection_action_restore (GLib.SimpleAction action, GLib.Variant? param) {
11211121
GLib.List<Files.File> selection = get_selected_files_for_transfer ();
1122-
FileUtils.restore_files_from_trash (selection, window);
1123-
1122+
FileUtils.restore_files_from_trash.begin (selection, window);
11241123
}
11251124

11261125
private void on_selection_action_open_executable (GLib.SimpleAction action, GLib.Variant? param) {

0 commit comments

Comments
 (0)