Commit f7887adf authored by Eisha Chen-yen-su's avatar Eisha Chen-yen-su

MediaViewer: Add the ability to save the media

See https://gitlab.gnome.org/World/fractal/issues/76
parent f310cb1e
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;
......@@ -283,6 +289,65 @@ 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();
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment