diff --git a/src/sheet.rs b/src/sheet.rs index c0aeda9..15c420b 100644 --- a/src/sheet.rs +++ b/src/sheet.rs @@ -1,10 +1,12 @@ use std::{ cmp::Ordering, + ffi::OsStr, fs, path::{Path, PathBuf}, }; use chrono::{DateTime, Utc}; +use log::debug; use strum_macros::{EnumDiscriminants, EnumIter}; pub trait PdfSheet { @@ -49,10 +51,19 @@ impl Ord for Sheet { } impl Sheet { - pub fn open_file(&self) { + pub fn open_file_or_annotated_version_if_exists(&self) { let path = &self.pdf.path; - // TODO: open on first_page - opener::open(path).unwrap(); + + let mut annotated_path = path.with_extension("").into_os_string(); + annotated_path.push("_annotated.pdf"); + let annotated_version = Path::new(&annotated_path); + if annotated_version.exists() { + // TODO: open on first_page + opener::open(annotated_version).unwrap(); + } else { + // TODO: open on first_page + opener::open(path).unwrap(); + } } pub fn is_part_of_book(&self) -> bool { diff --git a/src/sheet_validation.rs b/src/sheet_validation.rs index fd47c4d..4ea7efa 100644 --- a/src/sheet_validation.rs +++ b/src/sheet_validation.rs @@ -64,6 +64,16 @@ fn validate_sheet_files(sheets: Vec, dir: impl AsRef) -> FileValida // TODO: improve performance? for pdf_file in find_all_pdfs_in_directory_recursive(dir) { + // Make sure annotated files are not handled (they are then only opened if existent) + if pdf_file + .file_name() + .unwrap() + .to_string_lossy() + .ends_with("_annotated.pdf") + { + continue; + } + if let Some((i, _)) = invalidated_sheets .iter() .enumerate() diff --git a/src/ui/app.rs b/src/ui/app.rs index 2e9d78c..441655e 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -175,7 +175,7 @@ impl AsyncComponent for AppModel { .forward(sender.input_sender(), |_| todo!()), ); } else { - sheet.open_file(); + sheet.open_file_or_annotated_version_if_exists(); let mut sheet = sheet; sheet.last_opened = I64DateTime(Utc::now()); sheet_dao::update_sheet_last_opened(&self.database, &sheet)