diff --git a/crates/mobile/include/snowbound.h b/crates/mobile/include/snowbound.h index 3e2d290bae945a83ace00dbfbd07f50df20adceb..e5500719711aa682ca2192e952f1085854910364 100644 --- a/crates/mobile/include/snowbound.h +++ b/crates/mobile/include/snowbound.h @@ -73,6 +73,8 @@ char *sb_library_new_section(const Library *library, const char *folder, const c char *sb_library_move_page(const Library *library, const char *from, const char *id, const char *to, const char *before, uint32_t level, const char *discard, const char *author, const char *date, const char *time); char *sb_library_place(const Library *library, const char *path, const char *folder, const char *paths); +char *sb_library_recycle_bin(const Library *library); +bool sb_library_delete(const Library *library, const char *path); char *sb_search(const Library *const *libraries, size_t count, const char *section, const char *query); char *sb_library_tagged(const Library *library); bool sb_library_unlock(const Library *library, const char *path, const char *password); diff --git a/crates/mobile/src/library.rs b/crates/mobile/src/library.rs index 3bd33c8eee027506d6174694ee2a8c3b615635af..cf8425d7743ad31f9f0630342ddf9644a5d0e25d 100644 --- a/crates/mobile/src/library.rs +++ b/crates/mobile/src/library.rs @@ -9,7 +9,7 @@ use notebook::{ discover::{Folder, Reason, SectionState}, live::{self, Hello, Reach, share::Guest}, provider::Documents, - session::{self, Background, Event, Known, Notebook, SyncStatus}, + session::{self, Background, Event, Known, Notebook, RECYCLE_BIN, SyncStatus}, smb::{Client, Credentials}, }; use onestore::{ @@ -384,7 +384,7 @@ fn tabs(folder: &Folder, unlocked: &HashMap, tabs: &mut Vec) { }); } for group in &folder.groups { - if !group.path.ends_with("OneNote_RecycleBin") { + if !group.path.ends_with(RECYCLE_BIN) { self::tabs(group, unlocked, tabs); } } @@ -712,6 +712,36 @@ impl Library { } } + /// The sections of the notebook's recycle bin, Deleted Pages among them, as `tabs` lists + /// sections; none for a lone section. + pub(crate) fn recycle_bin(&self) -> Result> { + if matches!(self.place, Place::File(_) | Place::Document(..)) { + return Ok(Vec::new()); + } + self.with_notebook(false, |notebook| { + notebook.refresh()?; + let mut binned = Vec::new(); + let catalog = notebook.catalog(); + if let Some(bin) = catalog + .groups + .iter() + .find(|group| group.path == RECYCLE_BIN) + { + tabs(bin, &self.unlocked(), &mut binned); + } + Ok(binned) + }) + } + + /// Moves the section or group at catalog `path` into the recycle bin, as OneNote 2010 + /// deletes one. + pub(crate) fn delete(&self, path: &str) -> Result<()> { + self.with_notebook(true, |notebook| { + notebook.refresh()?; + Ok(notebook.delete(path)?) + }) + } + /// The section at catalog `path`, through a replica in the cache, following Work /// Offline. The background may hold the replica for a step, which takes a network round /// trip, so a busy replica is tried again for a while. @@ -1492,7 +1522,8 @@ impl Library { let retired = self.with_section(from, |section| { let mut ops = vec![Op::Section(SectionOp::Delete(vec![space]))]; let added = match section.pages()?.as_slice() { - [(only, ..)] if *only == space => { + // The recycle bin keeps no placeholder page. + [(only, ..)] if *only == space && !from.starts_with(&format!("{RECYCLE_BIN}/")) => { let fresh = dated(author, date, time)?; let added = fresh.space(); ops.push(Op::Section(SectionOp::Create(fresh))); @@ -2052,6 +2083,25 @@ pub unsafe extern "C" fn sb_library_move_page( })()) } +/// The sections of the notebook's recycle bin as JSON, as `sb_library_sections` lists them: +/// Deleted Pages, `OneNote_RecycleBin/OneNote_DeletedPages.one`, and each deleted section. +/// Pages leave it through `sb_library_move_page`, sections through `sb_library_place`. Reads +/// the notebook, so call it off the main thread. +#[unsafe(no_mangle)] +pub extern "C" fn sb_library_recycle_bin(library: &Library) -> *mut c_char { + json(library.recycle_bin()) +} + +/// Moves the section or group at catalog `path` into the notebook's recycle bin; false where +/// it can't. Writes to the notebook, so call it off the main thread. +/// +/// # Safety +/// `path` is NUL-terminated UTF-8. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn sb_library_delete(library: &Library, path: *const c_char) -> bool { + report(library.delete(&string(path))).is_some() +} + /// Puts the section or group at catalog `path` into the group at catalog path `folder` ("" /// for the notebook's top), moving it unless it is there, then orders `folder` as `paths`, a /// JSON array of catalog paths as they are named after the move; entries left out follow @@ -2326,7 +2376,7 @@ pub unsafe extern "C" fn sb_share_list(share: &Share, path: *const c_char) -> *m .is_some_and(|extension| extension.eq_ignore_ascii_case(wanted)) }; // Hidden and system folders, OneNote's recycle bin among them, stay out. - let shown = |name: &str| !name.starts_with('.') && name != "OneNote_RecycleBin"; + let shown = |name: &str| !name.starts_with('.') && name != RECYCLE_BIN; let mut listing = Listing { notebook: entries .iter() diff --git a/crates/mobile/src/tests.rs b/crates/mobile/src/tests.rs index afffa57a2cfe0c427ba69bcb9ac5b45fd28a4505..610ccda4de7dd109a15bd5c81c81bf99b644ebb4 100644 --- a/crates/mobile/src/tests.rs +++ b/crates/mobile/src/tests.rs @@ -525,6 +525,99 @@ fn a_new_page_and_subpage_list_after_their_parent_and_delete_to_the_recycle_bin( ); } +/// A page and a section deleted on iOS list in the recycle bin and leave it for places the +/// reader picks: the page for another section, keeping its identity and content, and the +/// section for a group; the files hold both on a cold reopen. +#[test] +fn deleted_pages_and_sections_restore_from_the_recycle_bin_to_where_they_are_put() { + const DELETED: &str = "OneNote_RecycleBin/OneNote_DeletedPages.one"; + const TITLE: &str = "Image role background=false, printout=false"; + let date = ["Friday, October 16, 2026", "9:00 AM"]; + let (directory, root, library, features) = notebook_open("Features.one", false); + let deleted = space(&features, TITLE); + let page = features.shared.section.page(deleted).unwrap(); + let text = canvas::search::page_text(&page); + assert!(text.contains("Foreground role control")); + features.delete_page(deleted, date[0], date[1]).unwrap(); + drop(features); + library.delete("Empty.one").unwrap(); + let binned: Vec = (library.recycle_bin().unwrap().into_iter()) + .map(|tab| tab.path) + .collect(); + assert_eq!(binned, [DELETED, "OneNote_RecycleBin/Empty.one"]); + assert!( + library + .tabs() + .unwrap() + .iter() + .all(|tab| tab.path != "Empty.one") + ); + + let bin = open(&library, DELETED); + let recycled = space(&bin, TITLE); + drop(bin); + let restored = library + .move_page( + DELETED, + recycled, + "Group A/Duplicate.one", + None, + 1, + None, + "Clover Test", + date, + ) + .unwrap(); + assert_eq!(restored.added, None, "the bin gains no placeholder page"); + assert_eq!(restored.copied, None); + assert!( + open(&library, DELETED) + .shared + .section + .pages() + .unwrap() + .is_empty() + ); + assert_eq!( + library + .place("OneNote_RecycleBin/Empty.one", "Group B", &[]) + .unwrap(), + "Group B/Empty.one" + ); + library.close(Duration::from_secs(20)).unwrap(); + drop(library); + + let cold = Library::open(&root, &directory.path().join("cold"), true).unwrap(); + let paths: Vec = cold + .tabs() + .unwrap() + .into_iter() + .map(|tab| tab.path) + .collect(); + assert!(paths.contains(&"Group B/Empty.one".to_owned())); + let listed = take_json(library::sb_library_recycle_bin(&cold)); + assert_eq!(listed[0]["path"], DELETED); + assert_eq!(listed.as_array().unwrap().len(), 1); + let arrived = restored.page.parse().unwrap(); + let file = root.join("Group A/Duplicate.one"); + assert_eq!(stored_text(&file, arrived), text); + let stored = notebook::session::stored_pages(&std::fs::read(&file).unwrap()).unwrap(); + let back = stored + .iter() + .find(|stored| stored.space == arrived) + .unwrap(); + assert_eq!(back.page.title, TITLE); + assert_eq!( + back.page.identity, page.identity, + "a restored page keeps its identity" + ); + assert!( + notebook::session::stored_pages(&std::fs::read(root.join(DELETED)).unwrap()) + .unwrap() + .is_empty() + ); +} + #[test] fn deleting_a_lone_section_page_still_creates_its_replacement_without_a_bin() { let directory = tempfile::tempdir().unwrap(); diff --git a/crates/notebook/src/session.rs b/crates/notebook/src/session.rs index 6ad99987eee3a124377d81a2a20c69b1735736df..9eb8486b069068cf5d3b9eb6865b3a29d0a2fc0b 100644 --- a/crates/notebook/src/session.rs +++ b/crates/notebook/src/session.rs @@ -475,7 +475,8 @@ pub(crate) const LIMITS: discover::Limits = discover::Limits { }; const TOC: &str = "Open Notebook.onetoc2"; -const RECYCLE_BIN: &str = "OneNote_RecycleBin"; +/// The recycle bin's folder, a section group of the notebook's. +pub const RECYCLE_BIN: &str = "OneNote_RecycleBin"; /// How long OneNote 2010 keeps what its recycle bin holds, its `DaysToKeepRecycledItems`. pub const RECYCLE_DAYS: u32 = 60;