From f7887adffb77b95cc73a647103d354818ba595a4 Mon Sep 17 00:00:00 2001 From: Eisha CHEN-YEN-SU Date: Sun, 17 Jun 2018 17:24:19 +0200 Subject: [PATCH] MediaViewer: Add the ability to save the media See https://gitlab.gnome.org/World/fractal/issues/76 --- fractal-gtk/src/appop/media_viewer.rs | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/fractal-gtk/src/appop/media_viewer.rs b/fractal-gtk/src/appop/media_viewer.rs index 75cdc9e6..2716146c 100644 --- a/fractal-gtk/src/appop/media_viewer.rs +++ b/fractal-gtk/src/appop/media_viewer.rs @@ -1,7 +1,13 @@ 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::("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(); + } } }