Skip to content

Commit 7fd78fc

Browse files
authored
MainWindow: DRY saving recordings process (#263)
1 parent 35ee0ed commit 7fd78fc

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

src/MainWindow.vala

+53-45
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,21 @@ public class MainWindow : Gtk.ApplicationWindow {
128128
//TRANSLATORS: This is the format of filename and %s represents a timestamp here.
129129
//Suffix is automatically appended depending on the recording format.
130130
//e.g. "Recording from 2018-11-10 23.42.36.wav"
131-
string final_file_name = _("Recording from %s").printf (
131+
string default_filename = _("Recording from %s").printf (
132132
new DateTime.now_local ().format ("%Y-%m-%d %H.%M.%S")
133133
) + suffix;
134134

135-
var autosave_dest = Application.settings.get_string ("autosave-destination");
136-
if (autosave_dest != Define.AUTOSAVE_DISABLED) {
137-
var dest = File.new_for_path (autosave_dest).get_child (final_file_name);
135+
ask_save_path.begin (default_filename, (obj, res) => {
136+
File? save_path = ask_save_path.end (res);
137+
138+
if (save_path == null) {
139+
// Log message is already outputted in ask_save_path method
140+
return;
141+
}
142+
143+
bool is_success = false;
138144
try {
139-
if (tmp_file.move (dest, FileCopyFlags.OVERWRITE)) {
140-
welcome_view.show_success_button ();
141-
}
145+
is_success = tmp_file.move (save_path, FileCopyFlags.OVERWRITE);
142146
} catch (Error e) {
143147
show_error_dialog (
144148
_("Failed to save recording"),
@@ -148,50 +152,54 @@ public class MainWindow : Gtk.ApplicationWindow {
148152
recorder.remove_tmp_recording ();
149153
}
150154

155+
if (is_success) {
156+
welcome_view.show_success_button ();
157+
}
158+
151159
if (destroy_on_save) {
152160
destroy ();
153161
}
154-
} else {
155-
var save_dialog = new Gtk.FileDialog () {
156-
title = _("Save your recording"),
157-
accept_label = _("Save"),
158-
modal = true,
159-
initial_name = final_file_name
160-
};
161-
save_dialog.save.begin (this, null, (obj, res) => {
162-
File dest;
163-
try {
164-
dest = save_dialog.save.end (res);
165-
} catch (Error e) {
166-
warning ("Failed to Gtk.FileDialog.save: %s", e.message);
167-
168-
// May be cancelled by user, so delete the tmp recording
169-
recorder.remove_tmp_recording ();
170-
171-
return;
172-
}
173-
174-
try {
175-
if (tmp_file.move (dest, FileCopyFlags.OVERWRITE)) {
176-
welcome_view.show_success_button ();
177-
}
178-
} catch (Error e) {
179-
show_error_dialog (
180-
_("Failed to save recording"),
181-
_("There was an error while moving file to the designated location."),
182-
e.message
183-
);
184-
recorder.remove_tmp_recording ();
185-
}
186-
187-
if (destroy_on_save) {
188-
destroy ();
189-
}
190-
});
191-
}
162+
});
192163
});
193164
}
194165

166+
/**
167+
* Query location where to save recordings.
168+
*
169+
* This method shows Gtk.FileDialog if the autosave is disabled and waits for the user input.
170+
* Otherwise, it returns the location depending on the autosave location.
171+
*
172+
* @param default_filename default filename of recoridngs
173+
*
174+
* @return location where to save recordings
175+
*/
176+
private async File? ask_save_path (string default_filename) {
177+
File? dest = null;
178+
179+
var autosave_dest = Application.settings.get_string ("autosave-destination");
180+
if (autosave_dest == Define.AUTOSAVE_DISABLED) {
181+
var save_dialog = new Gtk.FileDialog () {
182+
title = _("Save your recording"),
183+
accept_label = _("Save"),
184+
modal = true,
185+
initial_name = default_filename
186+
};
187+
188+
try {
189+
dest = yield save_dialog.save (this, null);
190+
} catch (Error e) {
191+
warning ("Failed to Gtk.FileDialog.save: %s", e.message);
192+
193+
// May be cancelled by user, so delete the tmp recording
194+
recorder.remove_tmp_recording ();
195+
}
196+
} else {
197+
dest = File.new_for_path (autosave_dest).get_child (default_filename);
198+
}
199+
200+
return dest;
201+
}
202+
195203
private void show_welcome () {
196204
stack.visible_child = welcome_view;
197205
}

0 commit comments

Comments
 (0)