imageView: Fix two panics when file not exists

This commit is contained in:
Sophie Herold 2022-11-04 19:28:48 +01:00
parent 369b71b15c
commit 165861568c
2 changed files with 16 additions and 16 deletions

View file

@ -18,11 +18,13 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::deps::*;
use crate::i18n::*;
use crate::util;
use gio::prelude::*;
use gio::subclass::prelude::*;
use anyhow::Context;
use once_cell::sync::OnceCell;
use std::cell::RefCell;
@ -68,7 +70,7 @@ glib::wrapper! {
}
impl LpFileModel {
pub fn from_directory(directory: &gio::File) -> Self {
pub fn from_directory(directory: &gio::File) -> anyhow::Result<Self> {
let model = glib::Object::new::<Self>(&[]);
{
@ -85,7 +87,7 @@ impl LpFileModel {
gio::FileQueryInfoFlags::NONE,
gio::Cancellable::NONE,
)
.unwrap();
.context(i18n("Directory does not exist."))?;
// Filter out non-images; For now we support "all" image types.
enumerator.for_each(|info| {
@ -106,7 +108,7 @@ impl LpFileModel {
model.imp().directory.set(directory.clone()).unwrap();
}
model
Ok(model)
}
pub fn directory(&self) -> Option<gio::File> {

View file

@ -154,15 +154,9 @@ impl LpImageView {
}
}
self.build_model_from_file(file);
self.build_model_from_file(file)?;
self.notify("active-file");
// TODO: rework width stuff
// let width = imp.picture.image_width();
// let height = imp.picture.image_height();
// log::debug!("Image dimensions: {} x {}", width, height);
// Ok((width, height))
Ok(())
}
@ -170,7 +164,7 @@ impl LpImageView {
// that holds a `gio::File` for each child within the same directory as the
// file we pass. This model will update with changes to the directory,
// and in turn we'll update our `adw::Carousel`.
fn build_model_from_file(&self, file: &gio::File) {
fn build_model_from_file(&self, file: &gio::File) -> anyhow::Result<()> {
let imp = self.imp();
let carousel = &imp.carousel;
@ -183,27 +177,31 @@ impl LpImageView {
if m.directory().map_or(false, |f| !f.equal(parent)) {
// Clear the carousel before creating the new model
self.clear_carousel(false);
*model = Some(LpFileModel::from_directory(parent));
*model = Some(LpFileModel::from_directory(parent)?);
log::debug!("new model created");
} else {
log::debug!("Re-using old model and navigating to the current file");
self.navigate_to_file(m, file);
return;
return Ok(());
}
} else {
*model = Some(LpFileModel::from_directory(parent));
*model = Some(LpFileModel::from_directory(parent)?);
log::debug!("new model created");
}
}
}
if let Some(model) = imp.model.borrow().as_ref() {
let index = model.index_of(file).unwrap();
let index = model
.index_of(file)
.context(i18n("File not found in model. (TODO: bad message)"))?;
log::debug!("Currently at file {index} in the directory");
carousel.append(&LpImagePage::from_file(file));
self.fill_carousel(model, index);
self.update_action_state(model, index);
}
Ok(())
}
pub fn navigate(&self, direction: adw::NavigationDirection) {
@ -402,7 +400,7 @@ impl LpImageView {
)
.await;
let _ = match wallpaper::WallpaperRequest::default()
match wallpaper::WallpaperRequest::default()
.set_on(wallpaper::SetOn::Background)
.show_preview(true)
.identifier(id)