MediaViewer: Remove code duplication

This removes code duplication between the "Save as" function of the
media viewer and the "Download" function of attached files.

See https://gitlab.gnome.org/World/fractal/issues/76
This commit is contained in:
Eisha CHEN-YEN-SU 2018-06-18 00:21:26 +02:00
parent 3893270366
commit d5a20f41bc

View file

@ -1,13 +1,7 @@
extern crate gdk;
extern crate gtk;
extern crate gettextrs;
use std::env;
use std::fs;
use self::gtk::prelude::*;
use self::gtk::ResponseType;
use self::gettextrs::gettext;
use glib;
@ -290,63 +284,7 @@ impl AppOp {
pub fn save_media(&self) {
if let Some(ref mv) = self.media_viewer {
let main_window = self.ui.builder
.get_object::<gtk::ApplicationWindow>("main_window")
.expect("Cant find main_window in ui file.");
let file_chooser = gtk::FileChooserDialog::new(
Some(&gettext("Save media as")),
Some(&main_window),
gtk::FileChooserAction::Save,
);
file_chooser.set_modal(true);
file_chooser.add_buttons(&[
(&gettext("_Cancel"), ResponseType::Cancel.into()),
(&gettext("_Save"), ResponseType::Accept.into()),
]);
file_chooser.set_current_folder(env::home_dir().unwrap_or_default());
file_chooser.set_current_name(&mv.media_names[mv.current_media_index]);
let local_path = mv.image.local_path.lock().unwrap().clone().unwrap_or_default();
file_chooser.connect_response(move |fcd, res| {
if ResponseType::from(res) == ResponseType::Accept {
if fcd.get_filename().unwrap_or_default().exists() {
let confirm_dialog = gtk::MessageDialog::new(
Some(fcd),
gtk::DialogFlags::MODAL | gtk::DialogFlags::DESTROY_WITH_PARENT,
gtk::MessageType::Question,
gtk::ButtonsType::YesNo,
&gettext("Do you want to overwrite the file?")
);
confirm_dialog.connect_response(clone!(fcd, local_path => move |cd, res| {
if ResponseType::from(res) == ResponseType::Yes {
if let Err(_) = fs::copy(local_path.clone(), fcd.get_filename().unwrap_or_default()) {
let msg = gettext("Could not save the image");
APPOP!(show_error, (msg));
}
cd.destroy();
fcd.destroy();
} else {
cd.destroy();
}
}));
confirm_dialog.show_all();
} else {
if let Err(_) = fs::copy(local_path.clone(), fcd.get_filename().unwrap_or_default()) {
let msg = gettext("Could not save the image");
APPOP!(show_error, (msg));
}
fcd.destroy();
}
} else {
fcd.destroy();
}
});
file_chooser.show_all();
self.save_file_as(mv.image.local_path.lock().unwrap().clone().unwrap_or_default(), mv.media_names[mv.current_media_index].clone());
}
}
}