Skip to content

Commit

Permalink
add updateDocument event
Browse files Browse the repository at this point in the history
  • Loading branch information
buckley-w-david committed Sep 24, 2023
1 parent 9f38b1a commit f293a3f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/core/src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ fn main() -> Result<(), Error> {
Event::CheckFetcher(..) |
Event::FetcherAddDocument(..) |
Event::FetcherRemoveDocument(..) |
Event::FetcherUpdateDocument(..) |
Event::FetcherSearch { .. } if !view.is::<Home>() => {
if let Some(home) = history.get_mut(0).filter(|view| view.is::<Home>()) {
let (tx, _rx) = mpsc::channel();
Expand Down
27 changes: 27 additions & 0 deletions crates/core/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,33 @@ impl Library {
Ok(())
}

pub fn update<P: AsRef<Path>>(&mut self, path: P, info: Info) -> Result<(), Error> {
let full_path = self.home.join(path.as_ref());

let md = full_path.metadata()?;
let fp = self.paths.get(path.as_ref()).cloned().or_else(|| {
full_path.metadata().ok()
.and_then(|md| md.fingerprint(self.fat32_epoch).ok())
}).ok_or_else(|| format_err!("can't get fingerprint of {}", path.as_ref().display()))?;
let fp2 = self.paths.get(path.as_ref()).cloned().ok_or_else(|| format_err!("can't get fingerprint of {}", path.as_ref().display()))?;

println!("Update fingerprint for {}: {} → {}.", path.as_ref().display(), fp2, fp);
self.db.remove(&fp).unwrap();
self.db.insert(fp, info);
self.db[&fp].file.size = md.len();
self.paths.insert(path.as_ref().to_path_buf(), fp);
let rp1 = self.reading_state_path(fp2);
let rp2 = self.reading_state_path(fp);
fs::rename(rp1, rp2).ok();
let tpp = self.thumbnail_preview_path(fp2);
if tpp.exists() {
fs::remove_file(tpp).ok();
}
self.has_db_changed = true;
Ok(())
}


pub fn copy_to<P: AsRef<Path>>(&mut self, path: P, other: &mut Library) -> Result<(), Error> {
let src = self.home.join(path.as_ref());

Expand Down
23 changes: 23 additions & 0 deletions crates/core/src/view/home/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,13 @@ impl Home {
self.refresh_visibles(true, false, hub, rq, context);
}

fn update_document(&mut self, path: &Path, info: Info, hub: &Hub, rq: &mut RenderQueue, context: &mut Context) -> Result<(), Error> {
let full_path = context.library.home.join(path);
context.library.update(full_path, info)?;
self.refresh_visibles(true, false, hub, rq, context);
Ok(())
}

fn set_status(&mut self, path: &Path, status: SimpleStatus, hub: &Hub, rq: &mut RenderQueue, context: &mut Context) {
context.library.set_status(path, status);

Expand Down Expand Up @@ -1355,6 +1362,16 @@ impl Home {
hub2.send(Event::FetcherRemoveDocument(id, PathBuf::from(path))).ok();
}
},
Some("updateDocument") => {
if let Some(info) = event.get("info")
.map(ToString::to_string)
.and_then(|v| serde_json::from_str(&v).ok()) {
if let Some(path) = event.get("path")
.and_then(JsonValue::as_str) {
hub2.send(Event::FetcherUpdateDocument(id, PathBuf::from(path), Box::new(info))).ok();
}
}
},
Some("search") => {
let path = event.get("path")
.and_then(JsonValue::as_str)
Expand Down Expand Up @@ -1638,6 +1655,12 @@ impl View for Home {
.ok();
true
},
Event::FetcherUpdateDocument(_, ref path, ref info) => {
self.update_document(path, *info.clone(), hub, rq, context)
.map_err(|e| eprintln!("Can't remove document: {:#}.", e))
.ok();
true
},
Event::Select(EntryId::CopyTo(ref path, index)) => {
self.copy_to(path, index, context)
.map_err(|e| eprintln!("Can't copy document: {:#}.", e))
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ pub enum Event {
SearchResult(usize, Vec<Boundary>),
FetcherAddDocument(u32, Box<Info>),
FetcherRemoveDocument(u32, PathBuf),
FetcherUpdateDocument(u32, PathBuf, Box<Info>),
FetcherSearch {
id: u32,
path: Option<PathBuf>,
Expand Down
1 change: 1 addition & 0 deletions crates/plato/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ pub fn run() -> Result<(), Error> {
Event::CheckFetcher(..) |
Event::FetcherAddDocument(..) |
Event::FetcherRemoveDocument(..) |
Event::FetcherUpdateDocument(..) |
Event::FetcherSearch { .. } if !view.is::<Home>() => {
if let Some(entry) = history.get_mut(0).filter(|entry| entry.view.is::<Home>()) {
let (tx, _rx) = mpsc::channel();
Expand Down

0 comments on commit f293a3f

Please sign in to comment.