edit_window: Handle CancellableFuture errors

Ignore the cancellation and instead check for actual errors inside an
uncanceled operation.
This commit is contained in:
Sophie Herold 2025-02-13 14:53:28 +01:00
parent a042bad7e6
commit 828306c206
4 changed files with 48 additions and 25 deletions

View file

@ -111,9 +111,9 @@
], ],
"sources": [ "sources": [
{ {
"type": "archive", "type": "git",
"url": "https://download.gnome.org/sources/glycin/1.2/glycin-1.2.beta.1.tar.xz", "url": "https://gitlab.gnome.org/GNOME/glycin.git",
"sha256": "f6952ed2854641bf521b430c088c953edf53d39fcfb9e0a30eb6872c7482ec26" "commit": "52fe5cdb68abf24361ce31c88c2965bd6c203460"
} }
] ]
}, },

View file

@ -0,0 +1 @@
editing: Don't show error if saving is aborted. Instead handle actual errors correctly.

View file

@ -248,8 +248,8 @@ mod imp {
let obj = self.obj(); let obj = self.obj();
self.save.popdown(); self.save.popdown();
if let Some(current_file) = obj.original_image().file() { if let Some(original_file) = obj.original_image().file() {
if let Some(current_path) = current_file.path() { if let Some(current_path) = original_file.path() {
if let Some(mut file_stem) = current_path.file_stem().map(|x| x.to_os_string()) if let Some(mut file_stem) = current_path.file_stem().map(|x| x.to_os_string())
{ {
let mut tmp_path = current_path.clone(); let mut tmp_path = current_path.clone();
@ -260,7 +260,7 @@ mod imp {
} }
tmp_path.set_file_name(file_stem); tmp_path.set_file_name(file_stem);
let new_file = gio::File::for_path(&tmp_path); let tmp_file = gio::File::for_path(&tmp_path);
let cancellable = gio::Cancellable::new(); let cancellable = gio::Cancellable::new();
if let Some(old_cancellable) = if let Some(old_cancellable) =
@ -270,43 +270,59 @@ mod imp {
} }
let written = self let written = self
.save(current_file.clone(), new_file.clone(), cancellable.clone()) .save(original_file.clone(), tmp_file.clone(), cancellable.clone())
.await; .await;
if written { if written {
if let Err(err) = gio::CancellableFuture::new( log::debug!("Moving image to trash '{}'", original_file.uri());
current_file.trash_future(glib::Priority::DEFAULT), let trash_result = gio::CancellableFuture::new(
original_file.trash_future(glib::Priority::DEFAULT),
cancellable.clone(), cancellable.clone(),
) )
.await .await;
{
if trash_result.is_err() {
// Canceled
log::debug!("Trashing image canceled");
return;
} else if let Ok(Err(err)) = trash_result {
obj.window().show_error( obj.window().show_error(
&gettext("Failed to save image."), &gettext("Failed to save image."),
&format!("Failed to move image {current_path:?} to trash and therefore couldn't save image: {err}"), &format!("Failed to move image {current_path:?} to trash and therefore couldn't save image: {err}"),
ErrorType::General, ErrorType::General,
); );
} else if let Err(err) = gio::CancellableFuture::new( return;
new_file }
log::debug!("Moving '{}' to '{}'", tmp_file.uri(), original_file.uri());
let move_result = gio::CancellableFuture::new(
tmp_file
.move_future( .move_future(
&current_file, &original_file,
gio::FileCopyFlags::NONE, gio::FileCopyFlags::NONE,
glib::Priority::DEFAULT, glib::Priority::DEFAULT,
) )
.0, .0,
cancellable.clone(), cancellable.clone(),
) )
.await .await;
{
if move_result.is_err() {
// Canceled
log::debug!("Moving image canceled");
return;
} else if let Ok(Err(err)) = move_result {
obj.window().show_error( obj.window().show_error(
&gettext("Failed to save image."), &gettext("Failed to save image."),
&format!( &format!(
"Failed to move image image to {current_path:?}: {err} move {:?} to {:?}", new_file.path().unwrap(), current_file.path().unwrap() "Failed to move image image to {current_path:?}: {err} move {:?} to {:?}", tmp_file.path().unwrap(), original_file.path().unwrap()
), ),
ErrorType::General, ErrorType::General,
); );
} else { return;
obj.window().show_specific_image(current_file);
} }
obj.window().show_specific_image(original_file);
} }
} }
} }
@ -356,21 +372,27 @@ mod imp {
Ok(data) => { Ok(data) => {
self.set_saving_status(Some(gettext("Saving Image"))); self.set_saving_status(Some(gettext("Saving Image")));
if let Err(err) = new_file let save_result = gio::CancellableFuture::new(
.replace_contents_future( new_file.replace_contents_future(
data, data,
None, None,
true, true,
gio::FileCreateFlags::NONE, gio::FileCreateFlags::NONE,
),
cancellable,
) )
.await .await;
{
if save_result.is_err() {
log::debug!("Saving image canceled");
} else if let Ok(Err(err)) = save_result {
obj.window().show_error( obj.window().show_error(
&gettext("Failed to Save Image"), &gettext("Failed to Save Image"),
&format!("Failed to write file:\n\n{err:?}"), &format!("Failed to write file:\n\n{err:?}"),
ErrorType::General, ErrorType::General,
); );
} else { } else {
log::debug!("Image saved");
return true; return true;
} }
} }

View file

@ -248,7 +248,7 @@ impl LpWindow {
} }
pub fn show_specific_image(&self, file: gio::File) { pub fn show_specific_image(&self, file: gio::File) {
log::debug!("Showting specific image: {}", file.uri()); log::debug!("Showing specific image: {}", file.uri());
self.image_view().set_images_from_files(vec![file]); self.image_view().set_images_from_files(vec![file]);
self.show_image(); self.show_image();
} }