diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be30d78..67d836c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,6 +44,9 @@ jobs: - name: Formatting run: cargo fmt --all -- --check + - name: Shell scripts of the release tarball + run: shellcheck packaging/tarball/*.sh + - name: Lints run: cargo clippy --workspace --all-targets --locked -- -D warnings diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1e12928..d96d82a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -119,17 +119,12 @@ jobs: echo "VERSION=$VERSION" >> $GITHUB_ENV echo "Building version: $VERSION" - - name: Package raw binaries (.tar.gz) + # Binaries plus everything around them, with install.sh for a home + # directory install (immutable distributions, anything without a package). + - name: Package the installable tarball (.tar.gz) run: | - mkdir -p dist/binaries - cp target/release/pdfs dist/binaries/ - cp target/release/pdfs-tray dist/binaries/ - cp target/release/pdfs-app dist/binaries/ - cp target/release/pdfs-prompt dist/binaries/ - po/build.sh dist/binaries/locale - - # Create tarball - tar -czf proton-drive-linux-${{ env.VERSION }}-x86_64.tar.gz -C dist/binaries . + tarball=$(packaging/tarball/build.sh target/release "${{ env.VERSION }}" .) + packaging/tarball/test.sh "$tarball" # Every crate the build needs, so COPR can build the spec without network. - name: Package vendored crates (.tar.xz) diff --git a/Cargo.lock b/Cargo.lock index a8dc170..1ea51b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1114,6 +1114,12 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "data-encoding" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" + [[package]] name = "dbl" version = "0.3.2" @@ -2902,6 +2908,32 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "lofty" +version = "0.25.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35d45c446382e04663705aa8e786157f64ef1fa85755d410121ffbf08f54b0db" +dependencies = [ + "byteorder", + "data-encoding", + "flate2", + "lofty_attr", + "log", + "ogg_pager", + "paste", +] + +[[package]] +name = "lofty_attr" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdbca1b60ba7cea959ffc84ac27f3dbdbacd6dfda168c7eabbc00e48654bb9fd" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + [[package]] name = "log" version = "0.4.35" @@ -3323,6 +3355,15 @@ dependencies = [ "subtle", ] +[[package]] +name = "ogg_pager" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "166611b491f939446e64c6be930843a31eb8e6798204ec3e9f57e084c18e7446" +dependencies = [ + "byteorder", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -3541,7 +3582,7 @@ checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" [[package]] name = "pdfs-cli" -version = "3.7.0" +version = "3.8.0" dependencies = [ "anyhow", "clap", @@ -3558,11 +3599,12 @@ dependencies = [ [[package]] name = "pdfs-core" -version = "3.7.0" +version = "3.8.0" dependencies = [ "async-trait", "base64 0.23.1", "chrono", + "dbus-secret-service", "dbus-secret-service-keyring-store", "directories", "ignore", @@ -3577,6 +3619,7 @@ dependencies = [ "serde_json", "sha1 0.11.0", "sha2 0.11.1", + "shell-words", "thiserror 2.0.21", "tokio", "tracing", @@ -3586,7 +3629,7 @@ dependencies = [ [[package]] name = "pdfs-fuse" -version = "3.7.0" +version = "3.8.0" dependencies = [ "async-trait", "base64 0.23.1", @@ -3594,10 +3637,12 @@ dependencies = [ "futures", "image", "libc", + "lofty", "notify", "parking_lot 0.13.0", "pdfs-core", "proton-drive-rs", + "serde", "serde_json", "sha1 0.11.0", "tokio", @@ -3608,7 +3653,7 @@ dependencies = [ [[package]] name = "pdfs-gui" -version = "3.7.0" +version = "3.8.0" dependencies = [ "anyhow", "async-channel", @@ -3848,9 +3893,9 @@ dependencies = [ [[package]] name = "proton-drive-rs" -version = "0.11.0" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14a22e621abbc1c61b4640728a7de969c16b66d5d983b59c638d501fda25a7b9" +checksum = "4265c2cf3bdc59ee273d7363c8b6a7cb4e98be6102ffa18144c4d8ebda00737b" dependencies = [ "async-trait", "base64 0.23.1", @@ -3876,9 +3921,9 @@ dependencies = [ [[package]] name = "proton-sdk" -version = "0.11.0" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd55a5e8b6600f3aa282ebbdf13ff366431ff9aae84d4eb20b0ef06d598919fe" +checksum = "fb98ab3e336ed95f82368a50348dad00e5fc919e982d748e83e844afcadbd5d8" dependencies = [ "aes-gcm 0.11.1", "async-trait", @@ -4761,6 +4806,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shell-words" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6fe69c597f9c37bfeeeeeb33da3530379845f10be461a66d16d03eca2ded77" + [[package]] name = "shlex" version = "2.0.1" diff --git a/Cargo.toml b/Cargo.toml index bc5cbe1..9384d01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "2" members = ["crates/pdfs-core", "crates/pdfs-fuse", "crates/pdfs-cli", "crates/pdfs-gui"] [workspace.package] -version = "3.7.0" +version = "3.8.0" edition = "2024" license = "MIT" authors = ["Nils Pukropp "] @@ -15,8 +15,8 @@ keywords = ["proton", "drive", "fuse", "sync", "linux"] categories = ["filesystem", "command-line-utilities"] [workspace.dependencies] -proton-sdk = { version = "0.11.0" } -proton-drive-rs = { version = "0.11.0" } +proton-sdk = { version = "0.12.0" } +proton-drive-rs = { version = "0.12.0" } chrono = { version = "0.4", default-features = false, features = ["std"] } # Internal crates. @@ -39,6 +39,8 @@ tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } directories = "6" ignore = "0.4" +# Command lines typed into Preferences, split and shown the way a shell would. +shell-words = "1" sha2 = "0.11" # Google Takeout exports are zip sets; the importer reads their central # directories and streams entries out without extracting to disk first. @@ -50,12 +52,19 @@ sha1 = "0.11" # formats a camera or phone actually produces — the rest of the crate's codecs # would be dead weight in the daemon. image = { version = "0.25", default-features = false, features = ["jpeg", "png", "webp", "gif", "tiff", "bmp", "rayon"] } +# Cover pictures of songs, read from their tags (ID3, FLAC, Ogg, MP4, APE) for +# the song's thumbnail. Its default feature is required: 0.25 does not build +# without it. +lofty = "0.25" rusqlite = { version = "0.41", features = ["bundled"] } # keyring 4 split into `keyring-core` plus per-platform stores. The libdbus # Secret Service store is the one keyring 3's `sync-secret-service` wrapped, and # with no `target` it matches items v3 wrote, so existing logins survive. keyring-core = "1" dbus-secret-service-keyring-store = { version = "1", features = ["crypto-rust"] } +# The same Secret Service client the store wraps, used directly to replace an +# item instead of editing it, which oo7 before 0.7.0 kept only in memory (B291). +dbus-secret-service = { version = "4.1", features = ["crypto-rust"] } clap = { version = "4", features = ["derive"] } # `unstable-dynamic`: Tab calls `pdfs` back, so arguments complete from the # running service. Only `pdfs-cli/src/complete.rs` uses it. diff --git a/README.md b/README.md index 4b0d64d..b4a4009 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,8 @@ scriptable CLI. | Arch Linux | [`proton-drive-for-linux`](https://aur.archlinux.org/packages/proton-drive-for-linux) (also `-bin` and `-git`) in the AUR | | Debian, Ubuntu 24.04 or newer | `.deb` from the [latest release](https://github.com/narrrl/proton-drive-linux/releases/latest) | | NixOS | The flake in this repository: see [Installation → NixOS](docs/INSTALL.md#nixos-flake) | -| Anything else | [Build from source](docs/INSTALL.md#build-from-source) | +| Fedora Atomic (Silverblue, Kinoite, Bazzite, Bluefin) | The COPR repository, layered with `rpm-ostree`: see [Installation → Fedora Atomic](docs/INSTALL.md#fedora-atomic-silverblue-kinoite-bazzite-bluefin) | +| Anything else, immutable ones included | `.tar.gz` from the [latest release](https://github.com/narrrl/proton-drive-linux/releases/latest), installed into your home folder by its `install.sh`: see [Installation → Other distributions](docs/INSTALL.md#other-distributions), or [build from source](docs/INSTALL.md#build-from-source) | **Fedora** diff --git a/crates/pdfs-cli/src/main.rs b/crates/pdfs-cli/src/main.rs index 87fb67a..2fc2e17 100644 --- a/crates/pdfs-cli/src/main.rs +++ b/crates/pdfs-cli/src/main.rs @@ -15,8 +15,8 @@ use pdfs_core::cache::ContentCache; use pdfs_core::config::AppDirs; use pdfs_core::control::{ ConflictKeep, ErrorKind, LinkChange, MountKind, RefreshScope, Request as CtlRequest, - Response as CtlResponse, RestoreItem as CtlRestoreItem, ShareEntryKind, SyncIssue, SyncPhase, - daemon_outdated, pending_summary, + Response as CtlResponse, RestoreItem as CtlRestoreItem, ShareEntryKind, SyncFolderInfo, + SyncIssue, SyncPhase, daemon_outdated, pending_summary, }; use pdfs_core::db::Db; use pdfs_core::service; @@ -228,14 +228,19 @@ enum Command { /// numbered parts, and a photo's metadata often sits in a different part /// than the photo. Albums in the export are recreated as Proton albums, and /// photos already on the account (same name and same content) are skipped, - /// so an interrupted import can simply be run again. + /// so an interrupted import can simply be run again, or resumed with + /// `--resume` without naming the archives again. ImportGooglePhotos { /// The export's `.zip` files (e.g. `takeout-*.zip`). - #[arg(required = true, value_hint = ValueHint::FilePath)] + #[arg(required_unless_present = "resume", value_hint = ValueHint::FilePath)] archives: Vec, /// Report what would be imported without uploading anything. - #[arg(long)] + #[arg(long, conflicts_with = "resume")] dry_run: bool, + /// Start the last import again with the same archives, after it was + /// interrupted, stopped or failed. + #[arg(long, conflicts_with = "archives")] + resume: bool, /// Stay attached and print progress until the import finishes. #[arg(long)] wait: bool, @@ -746,6 +751,42 @@ enum ConflictCmd { }, } +/// `pdfs sync ignore`: the patterns in `ignore_patterns`, for every synced +/// folder, or with `--folder` the ones in that folder's own `.pdfsignore`. +#[derive(Subcommand, Debug)] +enum IgnoreCmd { + /// List the patterns every synced folder leaves out, or one folder's own. + List { + /// List the own patterns of the synced folder holding this path. + #[arg(long, value_hint = ValueHint::DirPath)] + folder: Option, + }, + /// Leave out files matching a gitignore-style pattern, e.g. `*.db` or `build/`. + Add { + /// The pattern; end it with `/` to match folders only. + pattern: String, + /// Add it only for the synced folder holding this path. + #[arg(long, value_hint = ValueHint::DirPath)] + folder: Option, + }, + /// Stop leaving out files matching a pattern. + Remove { + /// The pattern, as `sync ignore list` shows it. + pattern: String, + /// Remove it from the own patterns of the synced folder holding this path. + #[arg(long, value_hint = ValueHint::DirPath)] + folder: Option, + }, + /// Go back to the built-in patterns. + Reset, + /// Say whether a file in a synced folder is synced, and which pattern decides. + Check { + /// A file or folder inside a synced folder. + #[arg(value_hint = ValueHint::AnyPath)] + path: PathBuf, + }, +} + #[derive(Subcommand)] enum SyncCmd { /// Add a local folder to this machine's device and upload its contents. @@ -844,6 +885,11 @@ enum SyncCmd { #[arg(add = ArgValueCandidates::new(complete::queue_id))] id: Option, }, + /// Show or change the patterns every synced folder leaves out, or check a path. + Ignore { + #[command(subcommand)] + action: IgnoreCmd, + }, /// Switch a folder between full-copy sync and on-demand (FUSE, no local storage). Mode { /// Synced-folder id (from `sync list`). @@ -1030,8 +1076,9 @@ fn main() -> Result<()> { Command::ImportGooglePhotos { archives, dry_run, + resume, wait, - } => cmd_import_google_photos(archives, dry_run, wait), + } => cmd_import_google_photos(archives, dry_run, resume, wait), Command::RedatePhotos { dry_run, limit, @@ -1318,6 +1365,7 @@ fn cmd_sync(action: SyncCmd) -> Result<()> { SyncCmd::Issues => return cmd_sync_issues(), SyncCmd::Export { id, dest } => return cmd_sync_export(id, &dest), SyncCmd::Limit { up, down } => return cmd_sync_limit(up, down), + SyncCmd::Ignore { action } => return cmd_sync_ignore(action), SyncCmd::Discard { id, yes } => { if !yes { confirm(&format!( @@ -2916,24 +2964,34 @@ fn photo_count(n: usize) -> String { } } -fn cmd_import_google_photos(archives: Vec, dry_run: bool, wait: bool) -> Result<()> { - // The daemon opens the archives itself off the shared filesystem, so it gets - // absolute paths — the same arrangement `pdfs upload` uses. - let mut absolute = Vec::with_capacity(archives.len()); - for archive in &archives { - let path = std::fs::canonicalize(archive) - .with_context(|| format!("resolve {}", archive.display()))?; - absolute.push( - path.to_str() - .ok_or_else(|| anyhow!("path is not valid UTF-8: {}", path.display()))? - .to_owned(), - ); - } +fn cmd_import_google_photos( + archives: Vec, + dry_run: bool, + resume: bool, + wait: bool, +) -> Result<()> { + let request = if resume { + CtlRequest::ResumeImport + } else { + // The daemon opens the archives itself off the shared filesystem, so it + // gets absolute paths — the same arrangement `pdfs upload` uses. + let mut absolute = Vec::with_capacity(archives.len()); + for archive in &archives { + let path = std::fs::canonicalize(archive) + .with_context(|| format!("resolve {}", archive.display()))?; + absolute.push( + path.to_str() + .ok_or_else(|| anyhow!("path is not valid UTF-8: {}", path.display()))? + .to_owned(), + ); + } + CtlRequest::ImportTakeout { + archives: absolute, + dry_run, + } + }; - match control_request(CtlRequest::ImportTakeout { - archives: absolute, - dry_run, - })? { + match control_request(request)? { CtlResponse::Ok { message } => println!("{message}"), CtlResponse::Error { message, kind } => bail!("{}", cli_error(kind, &message)), other => bail!("unexpected response: {other:?}"), @@ -2945,20 +3003,34 @@ fn cmd_import_google_photos(archives: Vec, dry_run: bool, wait: bool) - // Poll rather than stream: the control protocol is request/response, and an // import runs for hours — a held-open socket would be the only long-lived - // connection the daemon has. + // connection the daemon has. A line is printed when the step changes and + // every few percent of it, not on every poll. + let mut last_line = String::new(); + let mut last_printed = std::time::Instant::now(); loop { std::thread::sleep(std::time::Duration::from_secs(2)); match control_request(CtlRequest::ImportStatus)? { CtlResponse::ImportStatus { running: true, - summary: _, - } => continue, - CtlResponse::ImportStatus { summary, .. } => { - print_import_summary(summary.as_ref()); - return finished_run( - "the import", - summary.map(|s| (s.failed, s.cancelled, s.error)), - ); + progress, + .. + } => { + let line = progress.as_ref().map(progress_line).unwrap_or_default(); + let step_changed = line.split(':').next() != last_line.split(':').next(); + if !line.is_empty() + && line != last_line + && (step_changed || last_printed.elapsed().as_secs() >= 30) + { + println!("{line}"); + last_line = line; + last_printed = std::time::Instant::now(); + } + } + CtlResponse::ImportStatus { + summary, resumable, .. + } => { + print_import_summary(summary.as_ref(), &resumable); + return import_outcome(summary.as_ref()); } CtlResponse::Error { message, kind } => bail!("{}", cli_error(kind, &message)), other => bail!("unexpected response: {other:?}"), @@ -2966,6 +3038,47 @@ fn cmd_import_google_photos(archives: Vec, dry_run: bool, wait: bool) - } } +/// One line saying where a running import is. +fn progress_line(progress: &pdfs_core::control::ImportProgress) -> String { + use pdfs_core::control::ImportPhase; + let step = match progress.phase { + ImportPhase::Scanning => "Reading archives", + ImportPhase::Checking => "Checking for photos already on Proton", + ImportPhase::Uploading => "Uploading", + ImportPhase::Albums => "Filing photos into albums", + }; + match (progress.total, &progress.current) { + (0, _) => format!("{step}…"), + (total, Some(name)) => format!("{step}: {} of {total} ({name})", progress.done), + (total, None) => format!("{step}: {} of {total}", progress.done), + } +} + +/// How an import `--wait` followed ended, as the exit status: an error unless +/// it did everything it set out to do, so a script can tell. +fn import_outcome(summary: Option<&pdfs_core::control::ImportSummary>) -> Result<()> { + let Some(summary) = summary else { + bail!("the import ended without a result; the service may have restarted"); + }; + if let Some(error) = &summary.error { + bail!("the import failed: {error}"); + } + if summary.interrupted { + bail!("the import was interrupted; run `pdfs import-google-photos --resume` to continue"); + } + if summary.cancelled { + bail!("the import was cancelled"); + } + if summary.failed > 0 || !summary.unreadable_archives.is_empty() { + bail!( + "the import finished, but {} photo(s) and {} archive(s) failed", + summary.failed, + summary.unreadable_archives.len() + ); + } + Ok(()) +} + fn cmd_redate_photos( dry_run: bool, limit: Option, @@ -3100,11 +3213,19 @@ fn print_redate_summary(summary: Option<&pdfs_core::control::RedateSummary>) { fn cmd_import_status() -> Result<()> { match control_request(CtlRequest::ImportStatus)? { - CtlResponse::ImportStatus { running, summary } => { + CtlResponse::ImportStatus { + running, + summary, + progress, + resumable, + } => { if running { - println!("Import running. `pdfs transfers` shows its progress."); + match progress.as_ref() { + Some(progress) => println!("Import running. {}", progress_line(progress)), + None => println!("Import running."), + } } - print_import_summary(summary.as_ref()); + print_import_summary(summary.as_ref(), &resumable); } CtlResponse::Error { message, kind } => bail!("{}", cli_error(kind, &message)), other => bail!("unexpected response: {other:?}"), @@ -3121,18 +3242,28 @@ fn cmd_cancel_import() -> Result<()> { Ok(()) } -fn print_import_summary(summary: Option<&pdfs_core::control::ImportSummary>) { +fn print_import_summary(summary: Option<&pdfs_core::control::ImportSummary>, resumable: &[String]) { let Some(summary) = summary else { println!("No import has run yet."); return; }; + if summary.dry_run { + println!("Scan only; nothing was uploaded."); + } if let Some(error) = &summary.error { - println!("Failed : {error}"); - return; + println!("Stopped by : {error}"); } println!("Photos in export : {}", summary.found); - println!("Uploaded : {}", summary.uploaded); + if summary.dry_run { + println!("Would upload : {}", summary.to_upload); + println!("Bytes to upload : {}", summary.to_upload_bytes); + } else { + println!("Uploaded : {}", summary.uploaded); + } println!("Already on Proton: {}", summary.duplicates); + if summary.live_photos > 0 { + println!("Live photos : {}", summary.live_photos); + } if summary.skipped_trashed > 0 { println!("Skipped (trash) : {}", summary.skipped_trashed); } @@ -3145,12 +3276,20 @@ fn print_import_summary(summary: Option<&pdfs_core::control::ImportSummary>) { if summary.failed > 0 { println!("Failed : {}", summary.failed); } + for archive in &summary.unreadable_archives { + println!("Unreadable : {archive}"); + } if summary.bytes > 0 { println!("Uploaded bytes : {}", summary.bytes); } - if summary.cancelled { + if summary.interrupted { + println!("The service stopped before the import finished."); + } else if summary.cancelled { println!("Import was cancelled before it finished."); } + if !resumable.is_empty() { + println!("Run `pdfs import-google-photos --resume` to continue it."); + } } fn cmd_album(uid: String, limit: usize, offset: usize) -> Result<()> { @@ -3602,6 +3741,173 @@ fn cmd_sync_limit(up: Option, down: Option) -> Result<()> { })?) } +fn cmd_sync_ignore(action: IgnoreCmd) -> Result<()> { + use pdfs_core::syncignore::{ + DEFAULT_IGNORE_PATTERNS, FolderRules, IgnoreRules, RuleSource, Verdict, add_folder_pattern, + check_pattern, remove_folder_pattern, + }; + let dirs = AppDirs::new()?; + let change = |edit: &dyn Fn(&mut Vec)| -> Result<()> { + dirs.change_ignore_patterns(edit)?; + // The next pass picks the change up anyway; this only hurries it. + let _ = control_request(CtlRequest::SyncNow { id: None }); + Ok(()) + }; + let valid = |pattern: &str| -> Result { + let pattern = pattern.trim(); + if pattern.is_empty() || pattern.starts_with('#') { + bail!("not a pattern: `{pattern}`"); + } + check_pattern(pattern).map_err(|e| anyhow!("not a valid pattern: {e}"))?; + Ok(pattern.to_string()) + }; + match action { + IgnoreCmd::List { folder: None } => { + let config = dirs.load_config(); + let list = config.resolved_ignore_patterns(); + if list.is_empty() { + println!("No patterns: every file is synced."); + } + for pattern in list { + if DEFAULT_IGNORE_PATTERNS.contains(&pattern.as_str()) { + println!("{pattern} (built in)"); + } else { + println!("{pattern}"); + } + } + } + IgnoreCmd::List { + folder: Some(folder), + } => { + let (_, root) = mirror_folder_of(&folder)?; + let rules = FolderRules::read(&root)?; + let patterns = rules.patterns(); + if patterns.is_empty() { + println!( + "{} has no own patterns; the ones for all synced folders apply.", + root.display() + ); + } + for pattern in patterns { + println!("{pattern}"); + } + } + IgnoreCmd::Add { + pattern, + folder: None, + } => { + let pattern = valid(&pattern)?; + change(&|list| { + if !list.contains(&pattern) { + list.push(pattern.clone()); + } + })?; + } + IgnoreCmd::Add { + pattern, + folder: Some(folder), + } => { + let pattern = valid(&pattern)?; + let (id, root) = mirror_folder_of(&folder)?; + if add_folder_pattern(&root, &pattern)? { + let _ = control_request(CtlRequest::SyncNow { id: Some(id) }); + } else { + println!("`{pattern}` is already in {}/.pdfsignore.", root.display()); + } + } + IgnoreCmd::Remove { + pattern, + folder: None, + } => { + let pattern = pattern.trim().to_string(); + let list = dirs.load_config().resolved_ignore_patterns(); + if !list.contains(&pattern) { + bail!("`{pattern}` is not in the list; see `pdfs sync ignore list`"); + } + change(&|list| list.retain(|p| *p != pattern))?; + } + IgnoreCmd::Remove { + pattern, + folder: Some(folder), + } => { + let (id, root) = mirror_folder_of(&folder)?; + if !remove_folder_pattern(&root, &pattern)? { + bail!( + "`{}` is not among the own patterns of {}; see `pdfs sync ignore list --folder {}`", + pattern.trim(), + root.display(), + root.display() + ); + } + let _ = control_request(CtlRequest::SyncNow { id: Some(id) }); + } + IgnoreCmd::Reset => change(&|list| { + *list = DEFAULT_IGNORE_PATTERNS + .iter() + .map(|p| (*p).to_string()) + .collect() + })?, + IgnoreCmd::Check { path } => { + let path = std::path::absolute(&path)?; + let folder = synced_folder_of(&path)?; + if folder.mode != "mirror" { + println!( + "{} is online only; patterns apply only to folders kept on this computer.", + folder.local_path + ); + return Ok(()); + } + let root = PathBuf::from(&folder.local_path); + let rel = path.strip_prefix(&root).unwrap_or(&path).to_string_lossy(); + let globals = dirs.load_config().resolved_ignore_patterns(); + let rules = IgnoreRules::load(&root, &globals); + let source = |source: &RuleSource| match source { + RuleSource::AllFolders => "the patterns for all synced folders".to_string(), + RuleSource::ThisFolder => format!("{}/.pdfsignore", root.display()), + }; + match rules.explain(&rel, path.is_dir()) { + Verdict::Synced => println!("Synced: no pattern matches it."), + Verdict::Excluded { pattern, source: s } => { + println!("Not synced: excluded by `{pattern}` in {}.", source(&s)) + } + Verdict::Included { pattern, source: s } => { + println!("Synced: `{pattern}` in {} includes it again.", source(&s)) + } + } + } + } + Ok(()) +} + +/// The synced folder holding `path`: the innermost one, as the daemon lists +/// them. +fn synced_folder_of(path: &Path) -> Result { + let path = std::path::absolute(path)?; + let folders = match control_request(CtlRequest::ListSyncFolders)? { + CtlResponse::SyncFolders { items } => items, + CtlResponse::Error { message, kind } => bail!("{}", cli_error(kind, &message)), + other => bail!("unexpected reply: {other:?}"), + }; + folders + .into_iter() + .filter(|f| path.starts_with(&f.local_path)) + .max_by_key(|f| f.local_path.len()) + .ok_or_else(|| anyhow!("{} is not in a synced folder", path.display())) +} + +/// The id and root of the synced folder kept on this computer that holds +/// `path`. Own patterns mean nothing to an online-only folder. +fn mirror_folder_of(path: &Path) -> Result<(i64, PathBuf)> { + let folder = synced_folder_of(path)?; + if folder.mode != "mirror" { + bail!( + "{} is online only; patterns apply only to folders kept on this computer", + folder.local_path + ); + } + Ok((folder.id, PathBuf::from(folder.local_path))) +} + /// How long the oldest write may wait to upload before `diagnose` calls it /// out. Under a day it is a slow link or a big file; past it, something is /// holding the queue. @@ -4509,6 +4815,86 @@ mod login_wait_tests { assert!(finished_run("the import", None).is_err()); } + #[test] + fn a_waited_for_import_that_did_not_finish_exits_with_an_error() { + use pdfs_core::control::ImportSummary; + let done = ImportSummary { + uploaded: 4, + ..Default::default() + }; + assert!(import_outcome(Some(&done)).is_ok()); + assert!(import_outcome(None).is_err()); + for broken in [ + ImportSummary { + interrupted: true, + ..Default::default() + }, + ImportSummary { + unreadable_archives: vec!["/exports/takeout-002.zip".into()], + ..Default::default() + }, + ImportSummary { + error: Some("check for existing photos: offline".into()), + ..Default::default() + }, + ] { + assert!(import_outcome(Some(&broken)).is_err(), "{broken:?}"); + } + let interrupted = import_outcome(Some(&ImportSummary { + interrupted: true, + ..Default::default() + })); + assert!(interrupted.unwrap_err().to_string().contains("--resume")); + } + + #[test] + fn resume_needs_no_archives_and_excludes_them() { + let cli = Cli::try_parse_from(["pdfs", "import-google-photos", "--resume"]).unwrap(); + assert!(matches!( + cli.command, + Command::ImportGooglePhotos { resume: true, .. } + )); + assert!(Cli::try_parse_from(["pdfs", "import-google-photos"]).is_err()); + } + + #[test] + fn sync_ignore_takes_a_pattern_that_looks_like_an_option_after_dashes() { + let cli = Cli::try_parse_from(["pdfs", "sync", "ignore", "add", "--", "-temp*"]).unwrap(); + assert!(matches!( + cli.command, + Command::Sync { + action: SyncCmd::Ignore { + action: IgnoreCmd::Add { + ref pattern, + folder: None + } + } + } if pattern == "-temp*" + )); + assert!(Cli::try_parse_from(["pdfs", "sync", "ignore", "add"]).is_err()); + assert!(Cli::try_parse_from(["pdfs", "sync", "ignore", "reset"]).is_ok()); + let cli = + Cli::try_parse_from(["pdfs", "sync", "ignore", "add", "*.db", "--folder", "Work"]) + .unwrap(); + assert!(matches!( + cli.command, + Command::Sync { + action: SyncCmd::Ignore { + action: IgnoreCmd::Add { + ref pattern, + folder: Some(ref folder) + } + } + } if pattern == "*.db" && folder == Path::new("Work") + )); + assert!( + Cli::try_parse_from(["pdfs", "sync", "ignore", "list", "--folder", "Work"]).is_ok() + ); + assert!( + Cli::try_parse_from(["pdfs", "import-google-photos", "--resume", "a.zip"]).is_err() + ); + } + #[test] fn waiting_for_a_session_stops_hammering_the_keyring() { let mut checks_left = 20; diff --git a/crates/pdfs-core/Cargo.toml b/crates/pdfs-core/Cargo.toml index cbc54c0..9deceea 100644 --- a/crates/pdfs-core/Cargo.toml +++ b/crates/pdfs-core/Cargo.toml @@ -20,12 +20,14 @@ thiserror.workspace = true tracing.workspace = true directories.workspace = true ignore.workspace = true +shell-words.workspace = true sha2.workspace = true sha1.workspace = true zip.workspace = true rusqlite.workspace = true keyring-core.workspace = true dbus-secret-service-keyring-store.workspace = true +dbus-secret-service.workspace = true parking_lot.workspace = true async-trait.workspace = true libc.workspace = true diff --git a/crates/pdfs-core/examples/demo_daemon.rs b/crates/pdfs-core/examples/demo_daemon.rs new file mode 100644 index 0000000..c3bf34f --- /dev/null +++ b/crates/pdfs-core/examples/demo_daemon.rs @@ -0,0 +1,925 @@ +//! A stand-in daemon that serves made-up data, for the README screenshots. +//! +//! `scripts/screenshots.sh` runs it; see `docs/DEVELOPMENT.md`. It answers the +//! control socket the way the real daemon does, for the requests the captured +//! pages send, with an invented account: no Proton login, no FUSE mount and +//! nothing private. Every name, place and number below is made up. +//! +//! ```text +//! demo_daemon serve answer the control socket until killed +//! demo_daemon seed-keyring store a fake session, so the app signs in +//! ``` +//! +//! Both refuse to run outside a throwaway environment: `PDFS_DEMO_ROOT` must be +//! set and every XDG directory must lie inside it, so the real daemon's socket +//! is never touched (`HOME` may instead name a folder that does not exist, the +//! demo user's, so paths read as theirs), and `seed-keyring` refuses the session bus the script was +//! started on (`PDFS_DEMO_OUTER_BUS`), so the fake session only ever lands in a +//! private keyring. + +use std::collections::BTreeMap; +use std::io::{BufRead, BufReader, Write}; +use std::os::unix::fs::PermissionsExt; +use std::os::unix::net::{UnixListener, UnixStream}; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::time::Duration; + +use chrono::{Datelike, Local, TimeZone}; +use pdfs_core::cache::Pin; +use pdfs_core::config::{AppDirs, KEYRING_SERVICE}; +use pdfs_core::control::{ + ActivityEntry, ActivityKind, AlbumInfo, DeviceInfo, DirEntry, Event, LocalHit, PhotoItem, + PhotoKind, PhotoLocation, PhotoMonth, PhotoThumb, PlaceInfo, Request, Response, SearchHit, + SyncFolderInfo, Topic, +}; +use pdfs_core::mounts::{MountAccess, MountKind, MountMode, MountSpec}; + +const USERNAME: &str = "alex.example"; +const MOUNTPOINT: &str = "/home/alex/ProtonDrive"; +const GIB: u64 = 1 << 30; +const DAY: i64 = 86_400; + +/// How many photos the timeline holds. The demo pictures repeat, but not +/// within a screen. +const PHOTOS: usize = 1_840; + +fn main() { + if let Err(e) = run() { + eprintln!("demo_daemon: {e}"); + std::process::exit(1); + } +} + +fn run() -> Result<(), String> { + check_sandbox()?; + let args: Vec = std::env::args().skip(1).collect(); + match args.iter().map(String::as_str).collect::>()[..] { + ["serve", photos] => serve(Path::new(photos)), + ["seed-keyring"] => seed_keyring(), + _ => Err("usage: demo_daemon serve | seed-keyring".into()), + } +} + +/// Refuse to run anywhere but inside the throwaway root the script made. +fn check_sandbox() -> Result<(), String> { + let root = std::env::var_os("PDFS_DEMO_ROOT") + .map(PathBuf::from) + .ok_or("PDFS_DEMO_ROOT is not set; run scripts/screenshots.sh")?; + let home = std::env::var_os("HOME").map(PathBuf::from); + if home.is_none_or(|home| home.exists() && !home.starts_with(&root)) { + return Err("HOME must lie inside PDFS_DEMO_ROOT or not exist".into()); + } + for var in [ + "XDG_CONFIG_HOME", + "XDG_STATE_HOME", + "XDG_CACHE_HOME", + "XDG_DATA_HOME", + ] { + let inside = std::env::var_os(var) + .map(PathBuf::from) + .is_some_and(|dir| dir.starts_with(&root)); + if !inside { + return Err(format!("{var} must lie inside PDFS_DEMO_ROOT")); + } + } + Ok(()) +} + +/// Store a made-up session where the app looks for one. Its keys "unlock" +/// without a prompt; nothing ever uses them against Proton. +fn seed_keyring() -> Result<(), String> { + let bus = std::env::var("DBUS_SESSION_BUS_ADDRESS").unwrap_or_default(); + let outer = + std::env::var("PDFS_DEMO_OUTER_BUS").map_err(|_| "PDFS_DEMO_OUTER_BUS is not set")?; + if bus.is_empty() || bus == outer { + return Err("refusing to write a fake session to your own keyring".into()); + } + let session = serde_json::json!({ + "session_id": "demo-session", + "username": USERNAME, + "user_id": "demo-user", + "access_token": "demo", + "refresh_token": "demo", + "scopes": ["full", "self", "drive"], + "password_mode": 1, + "key_passphrases": { "demo-key": "ZGVtbw==" }, + }); + let store: Arc = + dbus_secret_service_keyring_store::Store::new().map_err(|e| e.to_string())?; + keyring_core::set_default_store(store); + keyring_core::Entry::new(KEYRING_SERVICE, "session") + .and_then(|entry| entry.set_password(&session.to_string())) + .map_err(|e| e.to_string()) +} + +fn serve(photos: &Path) -> Result<(), String> { + let demo = Arc::new(Demo::new(photos)?); + let dirs = AppDirs::new().map_err(|e| e.to_string())?; + dirs.ensure().map_err(|e| e.to_string())?; + let socket = dirs.control_socket(); + let _ = std::fs::remove_file(&socket); + let listener = UnixListener::bind(&socket).map_err(|e| format!("{}: {e}", socket.display()))?; + std::fs::set_permissions(&socket, std::fs::Permissions::from_mode(0o600)) + .map_err(|e| e.to_string())?; + eprintln!("demo_daemon: serving {}", socket.display()); + for stream in listener.incoming().flatten() { + let demo = Arc::clone(&demo); + std::thread::spawn(move || { + if let Err(e) = connection(&demo, stream) { + eprintln!("demo_daemon: {e}"); + } + }); + } + Ok(()) +} + +fn connection(demo: &Demo, stream: UnixStream) -> std::io::Result<()> { + let mut reader = BufReader::new(stream.try_clone()?); + let mut line = String::new(); + reader.read_line(&mut line)?; + let request: Request = match serde_json::from_str(line.trim()) { + Ok(request) => request, + Err(e) => { + eprintln!("demo_daemon: unreadable request: {e}"); + return Ok(()); + } + }; + let mut out = stream; + if matches!(request, Request::Subscribe) { + return subscription(demo, &mut out); + } + send(&mut out, &demo.answer(&request)) +} + +/// What the real daemon sends a new subscriber, then a heartbeat now and then +/// so the window never counts the daemon as gone. +fn subscription(demo: &Demo, out: &mut UnixStream) -> std::io::Result<()> { + send(out, &Event::Status(Box::new(demo.status())))?; + send( + out, + &Event::Transfers { + items: Vec::new(), + jobs: Vec::new(), + }, + )?; + send( + out, + &Event::Changed { + topics: Topic::ALL.to_vec(), + }, + )?; + loop { + std::thread::sleep(Duration::from_secs(10)); + send(out, &Event::Heartbeat)?; + } +} + +fn send(out: &mut UnixStream, value: &impl serde::Serialize) -> std::io::Result<()> { + let mut line = serde_json::to_vec(value)?; + line.push(b'\n'); + out.write_all(&line)?; + out.flush() +} + +/// One invented account. +struct Demo { + now: i64, + pictures: Vec, + photos: Vec, + /// Each photo's place, by index into [`PLACES`]. + place_of: Vec>, +} + +/// Towns the photos were "taken" in: name, ISO country, latitude, longitude. +const PLACES: &[(&str, &str, f64, f64)] = &[ + ("Lisbon", "PT", 38.7223, -9.1393), + ("Porto", "PT", 41.1579, -8.6291), + ("Barcelona", "ES", 41.3874, 2.1686), + ("Paris", "FR", 48.8566, 2.3522), + ("Amsterdam", "NL", 52.3676, 4.9041), + ("Copenhagen", "DK", 55.6761, 12.5683), + ("Berlin", "DE", 52.5200, 13.4050), + ("Prague", "CZ", 50.0755, 14.4378), + ("Vienna", "AT", 48.2082, 16.3738), + ("Florence", "IT", 43.7696, 11.2558), + ("Rome", "IT", 41.9028, 12.4964), + ("Thira", "GR", 36.4166, 25.4318), +]; + +const ALBUMS: &[&str] = &[ + "Summer in Portugal", + "Family", + "Hiking", + "Food", + "City trips", + "Best of 2025", +]; + +impl Demo { + fn new(dir: &Path) -> Result { + let mut pictures: Vec = std::fs::read_dir(dir) + .map_err(|e| format!("{}: {e}", dir.display()))? + .flatten() + .map(|e| e.path()) + .filter(|p| p.extension().is_some_and(|ext| ext == "jpg")) + .collect(); + pictures.sort(); + if pictures.is_empty() { + return Err(format!("no .jpg pictures in {}", dir.display())); + } + let now = Local::now().timestamp(); + let mut demo = Demo { + now, + pictures, + photos: Vec::new(), + place_of: Vec::new(), + }; + demo.make_photos(); + Ok(demo) + } + + /// The timeline: newest first, a few a week back three years, plus a + /// photo on this day in each of the last three years. + fn make_photos(&mut self) { + let mut seed = 0x5eed_u64; + let mut next = move || { + seed = seed + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + (seed >> 33) as i64 + }; + let today = Local::now().date_naive(); + let noon = |years_back: i32| { + let day = today + .with_year(today.year() - years_back) + .unwrap_or(today - chrono::Days::new(365 * years_back as u64)); + Local + .from_local_datetime(&day.and_hms_opt(11, 30, 0).unwrap_or_default()) + .single() + .map_or(0, |t| t.timestamp()) + }; + // Three years, so a few a day. + let step = 3 * 365 * DAY / PHOTOS as i64; + let mut times: Vec = (0..PHOTOS as i64) + .map(|i| self.now - 3 * 3600 - i * step - next() % step) + .collect(); + times.extend([noon(1), noon(2), noon(3)]); + times.sort_unstable_by(|a, b| b.cmp(a)); + for (i, time) in times.into_iter().enumerate() { + let picture = &self.pictures[i % self.pictures.len()]; + let kind = match i % 23 { + 7 | 15 => PhotoKind::Video, + 19 => PhotoKind::Raw, + _ => PhotoKind::Photo, + }; + let place = (i % 5 != 4).then_some((i / 7) % PLACES.len()); + self.photos.push(PhotoItem { + uid: format!("demo-photos~{i}"), + capture_time: time, + thumb_path: Some(picture.display().to_string()), + name: Some(format!( + "IMG_{}.{}", + 20_000 + i, + if kind == PhotoKind::Video { + "MP4" + } else { + "JPG" + } + )), + ratio: Some(if picture.to_string_lossy().ends_with("-p.jpg") { + 2.0 / 3.0 + } else { + 1.5 + }), + no_thumb: false, + kind, + favorite: i % 11 == 3, + group_size: 0, + has_raw: kind == PhotoKind::Raw, + place_id: place.map(|p| p as u32 + 1), + }); + self.place_of.push(place); + } + } + + fn status(&self) -> Response { + Response::Status { + username: USERNAME.into(), + mountpoint: MOUNTPOINT.into(), + pinned: 3, + used: 6 * GIB + 300 * (1 << 20), + budget: 20 * GIB, + pins: self.pins(), + online: true, + pending_uploads: 0, + pending_changes: 0, + parked_uploads: 0, + failing_ops: 0, + failing_error: None, + staged_bytes: 0, + staged_oldest_secs: 0, + paused: false, + paused_until: None, + session_expired: false, + tokens_unsaved: false, + version: env!("CARGO_PKG_VERSION").into(), + } + } + + fn answer(&self, request: &Request) -> Response { + match request { + Request::Status => self.status(), + Request::AccountQuota => Response::AccountQuota { + max_space: 500 * GIB as i64, + used_space: 137 * GIB as i64, + }, + Request::ListDir { path } => Response::Entries { + entries: self.dir(path), + }, + Request::ReserveFileThumbGeneration => Response::FileThumbGeneration { generation: 1 }, + Request::FileThumbs { items, .. } => Response::Thumbs { + items: items + .iter() + .map(|item| PhotoThumb { + uid: item.uid.clone(), + path: self.file_picture(&item.uid), + pending: false, + }) + .collect(), + }, + Request::PhotosTimeline { + offset, + limit, + kind, + favorites, + .. + } => { + let items: Vec = self + .photos + .iter() + .filter(|p| kind.is_none_or(|k| p.kind == k)) + .filter(|p| !favorites || p.favorite) + .skip(*offset) + .take(*limit) + .cloned() + .collect(); + Response::Photos { + available: true, + items, + counts: Some(self.counts()), + } + } + Request::PhotoMonths { kind } => Response::PhotoMonths { + months: self.months(*kind), + }, + Request::PhotosOnThisDay { .. } => { + let today = Local::now(); + let items = self + .photos + .iter() + .filter(|p| { + let at = Local.timestamp_opt(p.capture_time, 0).single(); + at.is_some_and(|at| { + at.year() < today.year() + && at.month() == today.month() + && at.day() == today.day() + }) + }) + .cloned() + .collect(); + Response::Photos { + available: true, + items, + counts: None, + } + } + Request::PhotoThumbs { uids } => Response::Thumbs { + items: uids + .iter() + .map(|uid| PhotoThumb { + uid: uid.clone(), + path: self + .photos + .iter() + .find(|p| &p.uid == uid) + .and_then(|p| p.thumb_path.clone()), + pending: false, + }) + .collect(), + }, + Request::PhotoAlbums => Response::Albums { + available: true, + items: self.albums(), + }, + Request::AlbumPhotos { uid, offset, limit } => { + let index: usize = uid + .rsplit('~') + .next() + .and_then(|n| n.parse().ok()) + .unwrap_or(0); + Response::Photos { + available: true, + items: self + .album_photos(index) + .skip(*offset) + .take(*limit) + .cloned() + .collect(), + counts: None, + } + } + Request::PhotoPlaces => Response::Places { + items: self.places(), + mapping: None, + }, + Request::PlaceLocations { id } => Response::PhotoLocations { + items: self.place_locations(*id), + }, + Request::PlacePhotos { id, offset, limit } => Response::Photos { + available: true, + items: self + .in_place(*id) + .map(|(_, p)| p) + .skip(*offset) + .take(*limit) + .cloned() + .collect(), + counts: None, + }, + Request::ListDevices => Response::Devices { + items: self.devices(), + }, + Request::ListSyncFolders => Response::SyncFolders { + items: self.sync_folders(), + }, + Request::ListLocations => Response::Locations { + items: self.locations(), + }, + // Every conflict in the history is resolved: an open one would + // badge every page. + Request::ListConflicts => Response::Conflicts { items: Vec::new() }, + Request::ListPendingOps => Response::PendingOps { items: Vec::new() }, + Request::ListActivity { limit } => Response::Activity { + items: self.activity().into_iter().take(*limit).collect(), + }, + Request::ListInvitations => Response::Invitations { items: Vec::new() }, + Request::ListBookmarks => Response::Bookmarks { items: Vec::new() }, + Request::GetQueueStatus => Response::Transfers { + items: Vec::new(), + jobs: Vec::new(), + }, + Request::ListPins => Response::Pins { pins: self.pins() }, + Request::SearchV2 { query, limit, .. } => Response::SearchResultsV2 { + drive_hits: self.search(query, *limit), + local_hits: self.local_search(query), + local_indexing: false, + }, + Request::Search { query, limit, .. } => Response::SearchResults { + hits: self.search(query, *limit), + }, + Request::ImportStatus => Response::ImportStatus { + running: false, + summary: None, + progress: None, + resumable: Vec::new(), + }, + Request::PhotosRefreshStatus => Response::PhotosRefresh { running: false }, + other => { + eprintln!("demo_daemon: no demo answer for {}", request_name(other)); + Response::Ok { + message: String::new(), + } + } + } + } + + fn counts(&self) -> (usize, usize, usize) { + let of = |kind| self.photos.iter().filter(|p| p.kind == kind).count(); + ( + of(PhotoKind::Photo), + of(PhotoKind::Video), + of(PhotoKind::Raw), + ) + } + + fn months(&self, kind: Option) -> Vec { + let mut months: BTreeMap<(i32, i32), usize> = BTreeMap::new(); + for photo in self + .photos + .iter() + .filter(|p| kind.is_none_or(|k| p.kind == k)) + { + if let Some(at) = Local.timestamp_opt(photo.capture_time, 0).single() { + *months.entry((at.year(), at.month() as i32)).or_default() += 1; + } + } + months + .into_iter() + .rev() + .map(|((year, month), count)| PhotoMonth { year, month, count }) + .collect() + } + + fn albums(&self) -> Vec { + ALBUMS + .iter() + .enumerate() + .map(|(i, name)| AlbumInfo { + uid: format!("demo-album~{i}"), + name: (*name).into(), + photo_count: self.album_photos(i).count(), + cover_uid: self.album_photos(i).next().map(|p| p.uid.clone()), + last_activity: Some(self.now - (i as i64 * 9 + 2) * DAY), + shared: i == 1, + }) + .collect() + } + + fn album_photos(&self, album: usize) -> impl Iterator { + let stride = 3 + album; + self.photos + .iter() + .skip(album * 2) + .step_by(stride) + .take(40 + album * 17) + } + + fn in_place(&self, id: u32) -> impl Iterator { + self.photos + .iter() + .enumerate() + .filter(move |(i, _)| self.place_of[*i].is_some_and(|p| p as u32 + 1 == id)) + } + + fn places(&self) -> Vec { + PLACES + .iter() + .enumerate() + .filter_map(|(i, (name, country, latitude, longitude))| { + let id = i as u32 + 1; + let count = self.in_place(id).count(); + let cover = self.in_place(id).next()?.1.clone(); + Some(PlaceInfo { + id, + name: (*name).into(), + country: (*country).into(), + latitude: *latitude, + longitude: *longitude, + photo_count: count, + cover, + }) + }) + .collect() + } + + fn place_locations(&self, id: u32) -> Vec { + let Some((_, _, latitude, longitude)) = PLACES.get(id as usize - 1) else { + return Vec::new(); + }; + self.in_place(id) + .map(|(i, photo)| { + // Spread around the centre, the same way on every run. + let angle = i as f64 * 2.399; + let reach = 0.004 + (i % 9) as f64 * 0.003; + PhotoLocation { + photo: photo.clone(), + latitude: latitude + reach * angle.sin(), + longitude: longitude + reach * angle.cos(), + } + }) + .collect() + } + + /// My files: folders first, then a few files, some of them pictures with + /// thumbnails. Any folder below the root holds the same few documents. + fn dir(&self, path: &str) -> Vec { + let folder = |name: &str, days: i64| DirEntry { + name: name.into(), + is_dir: true, + modified: self.now - days * DAY, + uid: format!("demo-files~{path}/{name}"), + path: join(path, name), + ..Default::default() + }; + let file = |name: &str, size: u64, days: i64| DirEntry { + name: name.into(), + is_dir: false, + size, + modified: self.now - days * DAY - 4000, + uid: format!("demo-files~{path}/{name}"), + path: join(path, name), + ..Default::default() + }; + if !path.trim_matches('/').is_empty() { + return vec![ + file("Notes.md", 4_812, 1), + file("Overview.pdf", 912_384, 6), + file("Budget.ods", 38_112, 12), + ]; + } + let mut entries = vec![ + folder("Documents", 1), + folder("Music", 30), + folder("Pictures", 2), + folder("Projects", 4), + folder("Recipes", 60), + folder("Taxes", 120), + folder("Videos", 14), + folder("Work", 3), + ]; + let mut pinned = file("Lisbon sunset.jpg", 4_218_112, 20); + pinned.pinned = true; + pinned.cached = true; + entries.extend([ + file("Apartment lease.pdf", 1_482_011, 40), + file("Budget 2026.ods", 52_340, 2), + file("Cliffs.jpg", 3_844_201, 33), + file("Garden.jpg", 2_904_877, 9), + pinned, + file("Meeting notes.md", 6_120, 0), + file("Mountain lake.jpg", 5_120_330, 61), + file("Packing list.txt", 1_204, 5), + file("Road trip.jpg", 3_211_008, 75), + file("Thesis draft.odt", 812_448, 18), + ]); + entries + } + + /// The picture behind a demo file, for its thumbnail. + fn file_picture(&self, uid: &str) -> Option { + let name = uid.rsplit('/').next()?; + if !name.ends_with(".jpg") { + return None; + } + let index = name.bytes().map(usize::from).sum::() % self.pictures.len(); + Some(self.pictures[index].display().to_string()) + } + + fn pins(&self) -> Vec { + [ + ("Documents", true), + ("Work/Quarterly report.odt", false), + ("Lisbon sunset.jpg", false), + ] + .into_iter() + .map(|(path, dir)| Pin { + uid: format!("demo-files~/{path}"), + path: (*path).into(), + recursive: dir, + is_dir: Some(dir), + cached: true, + }) + .collect() + } + + fn devices(&self) -> Vec { + vec![ + DeviceInfo { + uid: "demo-devices~1".into(), + name: "alex-laptop".into(), + device_type: "Linux".into(), + last_sync: Some(self.now - 240), + this_device: true, + adopted: true, + }, + DeviceInfo { + uid: "demo-devices~2".into(), + name: "studio-desktop".into(), + device_type: "Windows".into(), + last_sync: Some(self.now - 2 * DAY - 3 * 3600), + this_device: false, + adopted: false, + }, + DeviceInfo { + uid: "demo-devices~3".into(), + name: "old-macbook".into(), + device_type: "MacOs".into(), + last_sync: Some(self.now - 210 * DAY), + this_device: false, + adopted: false, + }, + ] + } + + /// This computer's synced folders: mirrors, and one kept online only. + const FOLDERS: &[(&str, &str)] = &[ + ("/home/alex/Documents", "mirror"), + ("/home/alex/Pictures/Camera", "mirror"), + ("/home/alex/Music", "ondemand"), + ("/home/alex/Projects", "mirror"), + ]; + + fn sync_folders(&self) -> Vec { + Self::FOLDERS + .iter() + .enumerate() + .map(|(i, (path, mode))| SyncFolderInfo { + id: i as i64 + 1, + kind: "device".into(), + local_path: (*path).into(), + remote_uid: format!("demo-sync~{i}"), + mode: (*mode).into(), + pending_mode: None, + state: "idle".into(), + last_sync: self.now - 240 - i as i64 * 60, + progress: None, + paused: false, + }) + .collect() + } + + fn locations(&self) -> Vec { + let mut items = vec![MountSpec { + id: 1, + kind: MountKind::MyFiles, + local_path: MOUNTPOINT.into(), + root_uid: "demo-files~".into(), + root_share_id: String::new(), + mode: MountMode::OnDemand, + access: MountAccess::Rw, + state: "idle".into(), + last_sync: self.now - 60, + pending_mode: None, + mounted: true, + progress: None, + paused: false, + }]; + items.extend( + Self::FOLDERS + .iter() + .enumerate() + .map(|(i, (path, mode))| MountSpec { + id: i as i64 + 2, + kind: MountKind::Device { + sync_folder_id: i as i64 + 1, + }, + local_path: (*path).into(), + root_uid: format!("demo-sync~{i}"), + root_share_id: String::new(), + mode: MountMode::from(*mode), + access: MountAccess::Rw, + state: "idle".into(), + last_sync: self.now - 240 - i as i64 * 60, + pending_mode: None, + mounted: *mode == "ondemand", + progress: None, + paused: false, + }), + ); + items + } + + fn activity(&self) -> Vec { + let at = |minutes: i64| self.now - minutes * 60; + let entry = |time, kind, target: &str, detail: &str, ok| ActivityEntry { + time, + kind, + target: target.into(), + detail: detail.into(), + ok, + }; + let conflict = |time, name: &str, ext: &str| { + entry( + time, + ActivityKind::Conflict, + &format!("{name} (sync-conflict {time}).{ext}"), + &format!("differs from {name}.{ext}"), + false, + ) + }; + vec![ + entry(at(2), ActivityKind::Upload, "Meeting notes.md", "", true), + entry( + at(14), + ActivityKind::Share, + "Budget 2026.ods", + "1 recipient(s) as editor", + true, + ), + entry( + at(38), + ActivityKind::Upload, + "Pictures/Camera/IMG_20481.JPG", + "", + true, + ), + entry( + at(39), + ActivityKind::Upload, + "Pictures/Camera/IMG_20480.JPG", + "", + true, + ), + entry( + at(52), + ActivityKind::Rename, + "Thesis draft.odt", + "was thesis-v3.odt", + true, + ), + conflict(at(180), "Quarterly report", "odt"), + entry( + at(240), + ActivityKind::Move, + "Taxes/2025", + "from Documents", + true, + ), + entry( + at(26 * 60), + ActivityKind::PublicLink, + "Road trip.jpg", + "", + true, + ), + conflict(at(26 * 60 + 5), "Packing list", "txt"), + entry(at(27 * 60), ActivityKind::CreateFolder, "Recipes", "", true), + conflict(at(28 * 60), "Garden plan", "svg"), + entry(at(29 * 60), ActivityKind::Trash, "Old invoices", "", true), + entry( + at(30 * 60), + ActivityKind::Restore, + "Apartment lease.pdf", + "", + true, + ), + entry( + at(50 * 60), + ActivityKind::Upload, + "Videos/Harbour at dusk.mp4", + "", + true, + ), + ] + } + + /// Hits for the search launcher: every demo file whose name holds the + /// query, or a few to show when nothing does. + fn search(&self, query: &str, limit: usize) -> Vec { + let names = [ + ("Work/Quarterly report.odt", false, 180_912), + ("Work/Quarterly report 2025.pdf", false, 1_204_334), + ("Work/Reports", true, 0), + ("Documents/Expense report March.ods", false, 44_201), + ("Documents/Lab report.pdf", false, 2_301_442), + ("Projects/Annual report draft.md", false, 18_400), + ("Taxes/2025/Tax report.pdf", false, 640_008), + ("Pictures/Report cover.jpg", false, 2_801_220), + ]; + let needle = query.to_lowercase(); + names + .iter() + .filter(|(path, _, _)| needle.is_empty() || path.to_lowercase().contains(&needle)) + .take(limit) + .enumerate() + .map(|(i, (path, is_dir, size))| SearchHit { + name: path.rsplit('/').next().unwrap_or(path).into(), + path: (*path).into(), + is_dir: *is_dir, + size: *size, + modified: self.now - (i as i64 * 5 + 1) * DAY, + pinned: i == 0, + cached: i == 0, + uid: format!("demo-files~/{path}"), + mounted_path: Some(format!("{MOUNTPOINT}/{path}")), + score: 100 - i as i64, + }) + .collect() + } + + fn local_search(&self, query: &str) -> Vec { + let needle = query.to_lowercase(); + [("/home/alex/Downloads/report-template.ott", 22_018)] + .iter() + .filter(|(path, _)| path.to_lowercase().contains(&needle)) + .map(|(path, size)| LocalHit { + name: path.rsplit('/').next().unwrap_or(path).into(), + path: (*path).into(), + is_dir: false, + size: *size, + modified: self.now - 4 * DAY, + score: 10, + }) + .collect() + } +} + +fn join(dir: &str, name: &str) -> String { + let dir = dir.trim_matches('/'); + if dir.is_empty() { + name.into() + } else { + format!("{dir}/{name}") + } +} + +/// The variant name of `request`, for the log of requests the demo ignores. +fn request_name(request: &Request) -> String { + let json = serde_json::to_string(request).unwrap_or_default(); + json.trim_start_matches(['{', '"']) + .split('"') + .next() + .unwrap_or_default() + .to_string() +} diff --git a/crates/pdfs-core/src/auth.rs b/crates/pdfs-core/src/auth.rs index 5acb3a0..fe57852 100644 --- a/crates/pdfs-core/src/auth.rs +++ b/crates/pdfs-core/src/auth.rs @@ -123,13 +123,84 @@ fn client_config() -> ProtonClientConfiguration { ProtonClientConfiguration::new(app_version).with_user_agent(user_agent) } -fn keyring_entry() -> Result { - // Installed lazily rather than once at startup so a session bus that was not - // up yet on the first attempt is retried on the next one. - if keyring_core::get_default_store().is_none() { - keyring_core::set_default_store(dbus_secret_service_keyring_store::Store::new()?); +/// Run `op` on the session's keyring entry. +fn with_keyring(op: impl Fn(&Entry) -> keyring_core::Result) -> Result { + with_keyring_entry(KEYRING_SERVICE, KEYRING_USER, op) +} + +/// Run `op` on the entry `service`/`user`, reconnecting once if the Secret +/// Service fails the call. +/// +/// The store holds one D-Bus connection and one Secret Service session for the +/// whole process. The session lives in the provider's memory: once the provider +/// restarts (oo7's unit restarts it on failure, and both oo7 and gnome-keyring +/// can be `--replace`d), every read and write on the old session fails with +/// `NoSession`, for as long as this process runs. A daemon then keeps refreshed +/// tokens only in memory, and a cold boot loses them (`docs/BUGS.md` B285). A +/// platform failure therefore drops the store, and the call is made once more +/// on a new one. A locked keyring, a dismissed prompt or a missing entry is +/// not the connection's fault and comes back as it is. +fn with_keyring_entry( + service: &str, + user: &str, + op: impl Fn(&Entry) -> keyring_core::Result, +) -> Result { + let store = keyring_store()?; + match op(&Entry::new(service, user)?) { + Err(keyring_core::Error::PlatformFailure(e)) => { + tracing::warn!( + error = %e, + "the Secret Service failed this process's connection; reconnecting" + ); + drop_keyring_store(&store); + keyring_store()?; + Ok(op(&Entry::new(service, user)?)?) + } + result => Ok(result?), + } +} + +/// Held while the default store is looked at and replaced, so that threads +/// failing at the same time drop the broken store once and share its +/// replacement. +static KEYRING_STORE: parking_lot::Mutex<()> = parking_lot::Mutex::new(()); + +/// The process's Secret Service store, connected now if there is none. +/// +/// Connected lazily rather than once at startup so a session bus that was not +/// up yet on the first attempt is tried again on the next one. +fn keyring_store() -> Result> { + let _swap = KEYRING_STORE.lock(); + if let Some(store) = keyring_core::get_default_store() { + return Ok(store); + } + let store = connect_keyring_store()?; + keyring_core::set_default_store(Arc::clone(&store)); + Ok(store) +} + +/// Forget `failed`, unless another thread has already replaced it. +fn drop_keyring_store(failed: &Arc) { + let _swap = KEYRING_STORE.lock(); + if keyring_core::get_default_store().is_some_and(|store| Arc::ptr_eq(&store, failed)) { + keyring_core::unset_default_store(); + } +} + +#[cfg(not(test))] +fn connect_keyring_store() -> Result> { + Ok(dbus_secret_service_keyring_store::Store::new()?) +} + +/// The tests' in-memory store when they set one, so a reconnect finds the +/// same entries; the real Secret Service otherwise. +#[cfg(test)] +fn connect_keyring_store() -> Result> { + tests::KEYRING_CONNECTS.fetch_add(1, Ordering::SeqCst); + match tests::MOCK_STORE.lock().clone() { + Some(store) => Ok(store), + None => Ok(dbus_secret_service_keyring_store::Store::new()?), } - Ok(Entry::new(KEYRING_SERVICE, KEYRING_USER)?) } /// What the user answered when asked for the mailbox password. @@ -419,19 +490,54 @@ fn classify_verification_gate(e: ProtonError) -> Error { } } +/// Save `stored` to the keyring. +/// +/// An existing entry is replaced by a new item rather than edited, because +/// oo7 before 0.7.0 keeps an edit only in memory and a reboot brings the +/// spent refresh token back (`docs/BUGS.md` B291). The usual write is the +/// fallback: for the first save, and whenever replacing is not possible. fn write_stored(stored: &StoredSession) -> Result<()> { let json = Zeroizing::new(serde_json::to_string(stored)?); - keyring_entry()?.set_password(&json)?; - Ok(()) + if replace_items() { + match crate::secretservice::replace(KEYRING_SERVICE, KEYRING_USER, json.as_bytes()) { + crate::secretservice::Replaced::Saved => return Ok(()), + crate::secretservice::Replaced::Skipped(why) => { + tracing::debug!(%why, "not replacing the keyring item; writing it in place"); + } + } + } + with_keyring(|entry| entry.set_password(&json)) +} + +/// Whether [`write_stored`] talks to the Secret Service directly: always, +/// except in tests that use the in-memory store. +#[cfg(not(test))] +fn replace_items() -> bool { + true +} + +#[cfg(test)] +fn replace_items() -> bool { + tests::MOCK_STORE.lock().is_none() +} + +/// A short, one-way tag for a refresh token, for the log: enough to tell +/// whether the token a start finds is the one the last save wrote, and +/// nothing that helps anyone use it. +fn fingerprint(token: &str) -> String { + use sha2::{Digest, Sha256}; + Sha256::digest(token.as_bytes())[..4] + .iter() + .map(|b| format!("{b:02x}")) + .collect() } /// Load the persisted session blob, or `Error::NotLoggedIn` if absent. pub fn load() -> Result { - let entry = keyring_entry()?; - match entry.get_password() { + match with_keyring(|entry| entry.get_password()) { Ok(json) => Ok(serde_json::from_str(&json)?), - Err(keyring_core::Error::NoEntry) => Err(Error::NotLoggedIn), - Err(e) => Err(e.into()), + Err(Error::Keyring(keyring_core::Error::NoEntry)) => Err(Error::NotLoggedIn), + Err(e) => Err(e), } } @@ -589,9 +695,9 @@ pub fn logout() -> Result { tracing::warn!(error = %e, "clearing the kernel-keyring unlock on logout failed"); } let _ = kernelkey::UNSAVED_TOKENS.clear(); - match keyring_entry()?.delete_credential() { - Ok(()) | Err(keyring_core::Error::NoEntry) => Ok(ended), - Err(e) => Err(e.into()), + match with_keyring(|entry| entry.delete_credential()) { + Ok(()) | Err(Error::Keyring(keyring_core::Error::NoEntry)) => Ok(ended), + Err(e) => Err(e), } } @@ -634,6 +740,10 @@ fn resume_stored(stored: &StoredSession) -> Result { resumed.access_token = tokens.access_token.clone(); resumed.refresh_token = tokens.refresh_token.clone(); } + tracing::info!( + refresh = %fingerprint(&resumed.refresh_token), + "resuming the stored session" + ); let session = ProtonApiSession::resume(client_config(), resumed.to_params())?; let sink = register_refresh_handler(&session, stored.refresh_token.clone()); if let Some(tokens) = unsaved { @@ -996,21 +1106,15 @@ impl TokenSink { } } - /// Write the unsaved rotation, if there is one. A blob that moved on, or - /// is gone with a logout, drops it: there is nothing it may still go into. + /// Write the unsaved rotation, if there is one. A blob that moved on drops + /// it. A missing blob is retried: some Secret Service providers also report + /// locked or temporarily hidden items as absent. Never recreate it here. fn flush(&self) -> Result<()> { let mut state = self.state.lock(); let Some(tokens) = state.unsaved.clone() else { return Ok(()); }; - let mut stored = match load() { - Ok(stored) => stored, - Err(Error::NotLoggedIn) => { - state.settle(); - return Ok(()); - } - Err(e) => return Err(e), - }; + let mut stored = load()?; match take_rotation( &mut stored, &self.session_id, @@ -1019,8 +1123,11 @@ impl TokenSink { ) { Rotation::Taken => { write_stored(&stored)?; + tracing::info!( + refresh = %fingerprint(&tokens.refresh_token), + "successfully auto-persisted refreshed tokens in keyring" + ); state.stored_refresh = tokens.refresh_token; - tracing::info!("successfully auto-persisted refreshed tokens in keyring"); } Rotation::MovedOn => { tracing::info!( @@ -1213,12 +1320,180 @@ mod tests { /// The keyring is one store per process, so the tests that use it, /// through `keyring-core`'s in-memory store, take turns. fn mock_keyring() -> parking_lot::MutexGuard<'static, ()> { - static KEYRING: parking_lot::Mutex<()> = parking_lot::Mutex::new(()); let turn = KEYRING.lock(); - keyring_core::set_default_store(keyring_core::mock::Store::new().unwrap()); + let store: Arc = keyring_core::mock::Store::new().unwrap(); + *MOCK_STORE.lock() = Some(Arc::clone(&store)); + keyring_core::set_default_store(store); turn } + static KEYRING: parking_lot::Mutex<()> = parking_lot::Mutex::new(()); + + /// The in-memory store a reconnect hands out, so it finds the same + /// entries; `None` connects to the real Secret Service. + pub(super) static MOCK_STORE: parking_lot::Mutex>> = + parking_lot::Mutex::new(None); + + /// How many times a store was connected. + pub(super) static KEYRING_CONNECTS: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); + + /// The session's entry, to make its next call fail. + fn keyring_entry() -> Entry { + Entry::new(KEYRING_SERVICE, KEYRING_USER).unwrap() + } + + /// Make the session entry's next call fail with `error`. + fn fail_next(error: keyring_core::Error) { + keyring_entry() + .as_any() + .downcast_ref::() + .unwrap() + .set_error(error); + } + + fn no_session() -> keyring_core::Error { + keyring_core::Error::PlatformFailure( + std::io::Error::other("The session does not exist (NoSession)").into(), + ) + } + + /// B285: a restarted Secret Service no longer knows the process's + /// session, and every call on it failed until the process restarted. + #[test] + fn a_dropped_secret_service_session_is_reconnected_and_retried() { + let _turn = mock_keyring(); + write_stored(&stored_with("r")).unwrap(); + let connects = KEYRING_CONNECTS.load(Ordering::SeqCst); + + fail_next(no_session()); + assert_eq!(load().unwrap().refresh_token, "r"); + assert_eq!(KEYRING_CONNECTS.load(Ordering::SeqCst), connects + 1); + + fail_next(no_session()); + write_stored(&stored_with("r2")).unwrap(); + assert_eq!(load().unwrap().refresh_token, "r2"); + assert_eq!(KEYRING_CONNECTS.load(Ordering::SeqCst), connects + 2); + } + + #[test] + fn a_rotation_lands_at_once_after_the_keyring_restarts() { + let _turn = mock_keyring(); + write_stored(&stored_with("r")).unwrap(); + let sink = Arc::new(TokenSink::new("s", "r".into())); + + fail_next(no_session()); + sink.rotated(tokens("a2", "r2")).unwrap(); + assert_eq!(load().unwrap().refresh_token, "r2"); + assert!(sink.state.lock().unsaved.is_none()); + } + + /// A locked keyring or a dismissed prompt is no fault of the connection. + #[test] + fn a_locked_keyring_is_not_reconnected() { + let _turn = mock_keyring(); + write_stored(&stored_with("r")).unwrap(); + let connects = KEYRING_CONNECTS.load(Ordering::SeqCst); + + fail_next(keyring_core::Error::NoStorageAccess( + std::io::Error::other("locked").into(), + )); + assert!(matches!( + load(), + Err(Error::Keyring(keyring_core::Error::NoStorageAccess(_))) + )); + assert_eq!(KEYRING_CONNECTS.load(Ordering::SeqCst), connects); + assert_eq!(load().unwrap().refresh_token, "r"); + } + + /// The real Secret Service, restarted between two calls on one store. + /// + /// Run inside a throwaway session bus, never against the desktop's + /// keyring. With gnome-keyring: + /// + /// ```sh + /// echo -n pw | gnome-keyring-daemon --unlock --components=secrets + /// PDFS_SECRET_SERVICE_RESTART_TEST=1 \ + /// PDFS_RESTART_PROVIDER='echo -n pw | gnome-keyring-daemon --replace --unlock --components=secrets' \ + /// cargo test -p pdfs-core real_secret_service -- --ignored + /// ``` + #[test] + #[ignore = "needs a throwaway Secret Service; see the doc comment"] + fn real_secret_service_survives_a_provider_restart() { + if std::env::var_os("PDFS_SECRET_SERVICE_RESTART_TEST").is_none() { + return; + } + let restart = std::env::var("PDFS_RESTART_PROVIDER").expect("PDFS_RESTART_PROVIDER"); + let _turn = KEYRING.lock(); + *MOCK_STORE.lock() = None; + keyring_core::unset_default_store(); + let (service, user) = ("pdfs-test", "restart"); + + with_keyring_entry(service, user, |e| e.set_password("before")).unwrap(); + let status = std::process::Command::new("sh") + .args(["-c", &restart]) + .status() + .unwrap(); + assert!(status.success(), "restarting the provider failed"); + std::thread::sleep(Duration::from_secs(1)); + + let connects = KEYRING_CONNECTS.load(Ordering::SeqCst); + let read = with_keyring_entry(service, user, |e| e.get_password()).unwrap(); + assert_eq!(read, "before"); + assert_eq!( + KEYRING_CONNECTS.load(Ordering::SeqCst), + connects + 1, + "the provider kept the old session, so nothing was tested" + ); + with_keyring_entry(service, user, |e| e.set_password("after")).unwrap(); + let read = with_keyring_entry(service, user, |e| e.get_password()).unwrap(); + assert_eq!(read, "after"); + with_keyring_entry(service, user, |e| e.delete_credential()).unwrap(); + keyring_core::unset_default_store(); + } + + /// B291: every save after the first replaces the keyring item, so a + /// provider that keeps an edited secret only in memory (oo7 before 0.7.0) + /// still has the last one after a restart, and only one item is left. + /// + /// Run like [`real_secret_service_survives_a_provider_restart`], in a + /// throwaway session bus only. + #[test] + #[ignore = "needs a throwaway Secret Service; see real_secret_service_survives_a_provider_restart"] + fn real_secret_service_keeps_the_last_save_across_a_provider_restart() { + if std::env::var_os("PDFS_SECRET_SERVICE_RESTART_TEST").is_none() { + return; + } + let restart = std::env::var("PDFS_RESTART_PROVIDER").expect("PDFS_RESTART_PROVIDER"); + let _turn = KEYRING.lock(); + *MOCK_STORE.lock() = None; + keyring_core::unset_default_store(); + + for refresh in ["r1", "r2", "r3"] { + write_stored(&stored_with(refresh)).unwrap(); + } + let status = std::process::Command::new("sh") + .args(["-c", &restart]) + .status() + .unwrap(); + assert!(status.success(), "restarting the provider failed"); + std::thread::sleep(Duration::from_secs(1)); + + assert_eq!(load().unwrap().refresh_token, "r3"); + let ss = + dbus_secret_service::SecretService::connect(dbus_secret_service::EncryptionType::Plain) + .unwrap(); + let found = ss + .search_items(std::collections::HashMap::from([ + ("service", KEYRING_SERVICE), + ("username", KEYRING_USER), + ])) + .unwrap(); + assert_eq!(found.unlocked.len() + found.locked.len(), 1); + with_keyring(|e| e.delete_credential()).unwrap(); + keyring_core::unset_default_store(); + } + fn stored_with(refresh: &str) -> StoredSession { serde_json::from_str(&blob("").replace(r#""r""#, &format!("{refresh:?}"))).unwrap() } @@ -1273,12 +1548,7 @@ mod tests { write_stored(&stored_with("r")).unwrap(); let sink = Arc::new(TokenSink::new("s", "r".into())); - let entry = keyring_entry().unwrap(); - let cred = entry - .as_any() - .downcast_ref::() - .unwrap(); - cred.set_error(keyring_core::Error::NoStorageAccess( + fail_next(keyring_core::Error::NoStorageAccess( std::io::Error::other("locked").into(), )); assert!(sink.rotated(tokens("a2", "r2")).is_err()); @@ -1306,6 +1576,42 @@ mod tests { } } + /// oo7 0.7.0 reports a locked collection's items as absent (upstream #614). + /// Treating that as logout loses the only live tokens (B284, issue #78). + #[test] + fn a_temporarily_missing_session_does_not_discard_a_rotation() { + let _turn = mock_keyring(); + write_stored(&stored_with("r")).unwrap(); + let sink = Arc::new(TokenSink::new("s", "r".into())); + fail_next(keyring_core::Error::NoEntry); + + assert!(sink.rotated(tokens("a2", "r2")).is_err()); + assert!(sink.state.lock().unsaved.is_some()); + assert_eq!(load().unwrap().refresh_token, "r"); + + sink.flush().unwrap(); + assert_eq!(load().unwrap().refresh_token, "r2"); + assert!(sink.state.lock().unsaved.is_none()); + } + + #[test] + fn retrying_a_missing_session_never_recreates_it_or_overwrites_a_new_login() { + let _turn = mock_keyring(); + let sink = TokenSink::new("s", "r".into()); + sink.state.lock().unsaved = Some(tokens("a2", "r2")); + + assert!(matches!(sink.flush(), Err(Error::NotLoggedIn))); + assert!(matches!(load(), Err(Error::NotLoggedIn))); + assert!(sink.state.lock().unsaved.is_some()); + + let mut login = stored_with("fresh"); + login.session_id = "new".into(); + write_stored(&login).unwrap(); + sink.flush().unwrap(); + assert!(sink.state.lock().unsaved.is_none()); + assert_eq!(load().unwrap().refresh_token, "fresh"); + } + #[test] fn an_unsaved_rotation_goes_only_into_the_blob_it_was_refused_for() { let unsaved = UnsavedRotation { diff --git a/crates/pdfs-core/src/config.rs b/crates/pdfs-core/src/config.rs index df8ad87..2b93b00 100644 --- a/crates/pdfs-core/src/config.rs +++ b/crates/pdfs-core/src/config.rs @@ -391,6 +391,27 @@ impl AppConfig { } } +/// Apply `edit` to the ignore patterns in effect in the config at `path`, +/// under the config lock, and return the saved config. A list equal to the +/// built-in one is stored as unset, so it follows the built-in list in later +/// releases. +pub fn change_ignore_patterns_at( + path: &Path, + edit: impl FnOnce(&mut Vec), +) -> Result { + update_config_at(path, |config| { + let mut list = config.resolved_ignore_patterns(); + edit(&mut list); + let builtin = list.len() == DEFAULT_IGNORE_PATTERNS.len() + && list + .iter() + .zip(DEFAULT_IGNORE_PATTERNS) + .all(|(a, b)| a == b); + config.ignore_patterns = (!builtin).then_some(list); + config.clone() + }) +} + /// Fail when the config file at `path` exists but does not parse, so that /// [`AppDirs::save_config`] never writes over settings it could not read. fn refuse_to_replace_unreadable(path: &Path) -> Result<()> { @@ -407,6 +428,116 @@ fn refuse_to_replace_unreadable(path: &Path) -> Result<()> { } } +/// [`AppDirs::load_config`] for the file at `path`. +fn load_config_at(path: &Path) -> AppConfig { + if path.exists() { + match std::fs::read_to_string(path) + .map_err(Error::from) + .and_then(|content| serde_json::from_str::(&content).map_err(Error::from)) + { + Ok(config) => return config, + Err(e) => { + // Preserve the original bytes for diagnosis/recovery. In + // particular, never replace a truncated config with defaults + // and silently lose mount, device, or ignore settings. + // `save_config` refuses to write over it for the same reason. + tracing::error!(path = %path.display(), error = %e, "config is unreadable; using in-memory defaults without overwriting it"); + return AppConfig::default(); + } + } + } + + let default_config = AppConfig::default(); + if let Err(e) = save_config_at(path, &default_config) { + tracing::warn!(path = %path.display(), error = %e, "could not write the default config"); + } + default_config +} + +/// [`AppDirs::save_config`] for the file at `path`. +fn save_config_at(path: &Path, config: &AppConfig) -> Result<()> { + refuse_to_replace_unreadable(path)?; + let parent = path + .parent() + .ok_or_else(|| Error::Other("configuration path has no parent".into()))?; + std::fs::create_dir_all(parent)?; + let nonce = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|duration| duration.as_nanos()) + .unwrap_or(0); + let tmp = path.with_extension(format!("json.tmp-{}-{nonce}", std::process::id())); + use std::os::unix::fs::OpenOptionsExt as _; + let mut file = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .mode(0o600) + .open(&tmp)?; + use std::io::Write as _; + file.write_all(&serde_json::to_vec_pretty(config)?)?; + file.sync_all()?; + std::fs::rename(&tmp, path)?; + std::fs::File::open(parent)?.sync_all()?; + Ok(()) +} + +/// [`AppDirs::update_config`] for the file at `path`. +fn update_config_at(path: &Path, change: impl FnOnce(&mut AppConfig) -> R) -> Result { + let _lock = lock_config(path)?; + // Refuse before reading: an unreadable file loads as defaults, and the + // change must not be applied to those. + refuse_to_replace_unreadable(path)?; + let mut config = load_config_at(path); + let out = change(&mut config); + save_config_at(path, &config)?; + Ok(out) +} + +/// How long [`lock_config`] waits for another writer. A change holds the lock +/// for one small read and write; anything longer is a stuck process, and the +/// setting is better reported as not saved than waited on forever. +const CONFIG_LOCK_WAIT: std::time::Duration = std::time::Duration::from_secs(5); + +/// Take the lock that orders changes to the config file at `path`, held until +/// the returned file is dropped. +/// +/// A separate `config.json.lock`, never the config file itself: that file is +/// replaced by a rename on every save, so a lock on it would guard a file +/// nobody reads any more. +fn lock_config(path: &Path) -> Result { + use std::os::fd::AsRawFd as _; + use std::os::unix::fs::OpenOptionsExt as _; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + let mut name = path.as_os_str().to_owned(); + name.push(".lock"); + let file = std::fs::OpenOptions::new() + .create(true) + .read(true) + .write(true) + .truncate(false) + .mode(0o600) + .open(PathBuf::from(name))?; + let deadline = std::time::Instant::now() + CONFIG_LOCK_WAIT; + loop { + // SAFETY: `file` owns the descriptor and outlives the call. + if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0 { + return Ok(file); + } + let e = std::io::Error::last_os_error(); + if e.raw_os_error() != Some(libc::EWOULDBLOCK) { + return Err(Error::Io(e)); + } + if std::time::Instant::now() >= deadline { + return Err(Error::Other(format!( + "{} is being changed by another process; try again", + path.display() + ))); + } + std::thread::sleep(std::time::Duration::from_millis(10)); + } +} + /// Resolved XDG locations for state, cache, and the default mountpoint. pub struct AppDirs { dirs: ProjectDirs, @@ -426,65 +557,40 @@ impl AppDirs { /// Load config from disk, creating default if missing. pub fn load_config(&self) -> AppConfig { - let path = self.config_path(); - if path.exists() { - match std::fs::read_to_string(&path) - .map_err(Error::from) - .and_then(|content| { - serde_json::from_str::(&content).map_err(Error::from) - }) { - Ok(config) => return config, - Err(e) => { - // Preserve the original bytes for diagnosis/recovery. In - // particular, never replace a truncated config with defaults - // and silently lose mount, device, or ignore settings. - // `save_config` refuses to write over it for the same reason. - tracing::error!(path = %path.display(), error = %e, "config is unreadable; using in-memory defaults without overwriting it"); - return AppConfig::default(); - } - } - } - - let default_config = AppConfig::default(); - if let Err(e) = self.save_config(&default_config) { - tracing::warn!(path = %path.display(), error = %e, "could not write the default config"); - } - default_config + load_config_at(&self.config_path()) } /// Persist `config` to [`config_path`](Self::config_path), creating the - /// config dir if missing. The Settings page calls this after editing the - /// cache budget or mountpoint; the daemon re-reads the file on its next mount. + /// config dir if missing. /// - /// Refuses while the file on disk does not parse. Every caller loads, - /// changes one field and saves, and [`load_config`](Self::load_config) - /// gives defaults for a file it cannot read, so the first change after - /// the file was damaged wrote defaults over the mountpoint, the device and - /// the ignore rules the user had (`docs/BUGS.md` B223). + /// Refuses while the file on disk does not parse: [`load_config`](Self::load_config) + /// gives defaults for a file it cannot read, so the first change after the + /// file was damaged would write defaults over the mountpoint, the device + /// and the ignore rules the user had (`docs/BUGS.md` B223). + /// + /// Writes the whole file. To change a setting, use + /// [`update_config`](Self::update_config), which does not lose another + /// process's change. pub fn save_config(&self, config: &AppConfig) -> Result<()> { - let path = self.config_path(); - refuse_to_replace_unreadable(&path)?; - let parent = path - .parent() - .ok_or_else(|| Error::Other("configuration path has no parent".into()))?; - std::fs::create_dir_all(parent)?; - let nonce = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|duration| duration.as_nanos()) - .unwrap_or(0); - let tmp = path.with_extension(format!("json.tmp-{}-{nonce}", std::process::id())); - use std::os::unix::fs::OpenOptionsExt as _; - let mut file = std::fs::OpenOptions::new() - .write(true) - .create_new(true) - .mode(0o600) - .open(&tmp)?; - use std::io::Write as _; - file.write_all(&serde_json::to_vec_pretty(config)?)?; - file.sync_all()?; - std::fs::rename(&tmp, &path)?; - std::fs::File::open(parent)?.sync_all()?; - Ok(()) + save_config_at(&self.config_path(), config) + } + + /// Change settings in place: read the file, apply `change`, write it back, + /// all under a lock, and return what `change` returned. + /// + /// The app, the tray and the daemon each write the file. Without the lock, + /// two of them changing different settings at once both read the old file, + /// and the one that wrote last undid the other's change (`docs/BUGS.md` + /// B292). + pub fn update_config(&self, change: impl FnOnce(&mut AppConfig) -> R) -> Result { + update_config_at(&self.config_path(), change) + } + + /// Apply `edit` to the ignore patterns in effect for every synced folder + /// and save them, returning the saved config. See + /// [`change_ignore_patterns_at`]. + pub fn change_ignore_patterns(&self, edit: impl FnOnce(&mut Vec)) -> Result { + change_ignore_patterns_at(&self.config_path(), edit) } /// This machine's id (e.g. ~/.config/proton-drive-linux/machine-id). See @@ -562,6 +668,12 @@ impl AppDirs { self.state_dir().join("control.sock") } + /// The Google Photos import's last run: its archives and how it ended, so + /// an import cut short by a restart can be resumed. + pub fn takeout_import_path(&self) -> PathBuf { + self.state_dir().join("takeout-import.json") + } + /// Unix domain socket the tray uses to ensure single instance. pub fn tray_socket(&self) -> PathBuf { self.state_dir().join("tray.sock") @@ -676,6 +788,89 @@ mod tests { std::fs::metadata(p).unwrap().permissions().mode() & 0o777 } + /// Two writers changing different settings at once both keep their change + /// (`docs/BUGS.md` B292). + #[test] + fn concurrent_changes_to_different_settings_are_all_kept() { + let dir = std::env::temp_dir().join(format!( + "pdfs-cfg-race-{}-{:?}", + std::process::id(), + std::thread::current().id() + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("config.json"); + + let a = { + let path = path.clone(); + std::thread::spawn(move || { + for i in 1..=100u64 { + update_config_at(&path, |c| c.upload_limit = Some(i)).unwrap(); + } + }) + }; + let b = { + let path = path.clone(); + std::thread::spawn(move || { + for i in 1..=100u64 { + update_config_at(&path, |c| c.download_limit = Some(i)).unwrap(); + } + }) + }; + a.join().unwrap(); + b.join().unwrap(); + + let config = load_config_at(&path); + assert_eq!(config.upload_limit, Some(100)); + assert_eq!(config.download_limit, Some(100)); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn ignore_patterns_back_at_the_built_in_list_are_stored_as_unset() { + let dir = std::env::temp_dir().join(format!( + "pdfs-cfg-ignore-{}-{:?}", + std::process::id(), + std::thread::current().id() + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("config.json"); + + let config = change_ignore_patterns_at(&path, |list| list.push("*.db".into())).unwrap(); + assert_eq!( + config.ignore_patterns.as_ref().unwrap().last().unwrap(), + "*.db" + ); + let config = change_ignore_patterns_at(&path, |list| list.retain(|p| p != "*.db")).unwrap(); + assert_eq!(config.ignore_patterns, None); + assert_eq!(load_config_at(&path).ignore_patterns, None); + // An empty list is a choice, not the built-in one. + let config = change_ignore_patterns_at(&path, Vec::clear).unwrap(); + assert_eq!(config.ignore_patterns, Some(Vec::new())); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn a_change_is_never_applied_over_an_unreadable_config() { + let dir = std::env::temp_dir().join(format!( + "pdfs-cfg-update-guard-{}-{:?}", + std::process::id(), + std::thread::current().id() + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("config.json"); + std::fs::write(&path, br#"{"mountpoint": "/home/me/Pro"#).unwrap(); + + assert!(update_config_at(&path, |c| c.tray_hidden = true).is_err()); + assert_eq!( + std::fs::read(&path).unwrap(), + br#"{"mountpoint": "/home/me/Pro"# + ); + let _ = std::fs::remove_dir_all(&dir); + } + /// A damaged config must not be replaced by defaults the next time a /// setting changes (`docs/BUGS.md` B223). #[test] diff --git a/crates/pdfs-core/src/control.rs b/crates/pdfs-core/src/control.rs index 4a757d5..7ef2881 100644 --- a/crates/pdfs-core/src/control.rs +++ b/crates/pdfs-core/src/control.rs @@ -279,6 +279,11 @@ pub enum Request { /// and files what it has uploaded into its albums, then reports /// `cancelled`. Replies with [`Response::Ok`]. CancelImport, + /// Start the last import again with the same archives, after it was + /// interrupted, cancelled or failed. Photos it already uploaded are found + /// on the account and skipped. Replies like [`Request::ImportTakeout`]; + /// [`ErrorKind::NotFound`] when there is nothing to resume. + ResumeImport, /// Upload the photo at `source_path` under the given name and media type. /// /// A path, not the bytes: this protocol is line-delimited JSON, and @@ -768,6 +773,9 @@ pub enum Topic { Conflicts, /// The activity log gained entries. Activity, + /// File thumbnails were made or fetched. A front-end asks again for the + /// tiles it still shows blank; the ones it already painted stay as they are. + Thumbnails, /// A topic from a newer daemon, which this front-end cannot act on. #[serde(other)] Unknown, @@ -777,7 +785,7 @@ impl Topic { /// Every topic a front-end can act on. A new subscription is told all of /// them have changed, so it catches up on whatever happened while it was /// not connected. - pub const ALL: [Topic; 9] = [ + pub const ALL: [Topic; 10] = [ Topic::Files, Topic::Photos, Topic::Trash, @@ -787,6 +795,7 @@ impl Topic { Topic::Queue, Topic::Conflicts, Topic::Activity, + Topic::Thumbnails, ]; } @@ -1685,10 +1694,75 @@ pub struct ImportSummary { pub bytes: u64, /// True when the run stopped early because it was cancelled. pub cancelled: bool, - /// Why the run as a whole failed, when it did, such as an archive that - /// is not a zip. The counts are then empty. + /// Why the run as a whole stopped early, when it did, such as the + /// account being unreachable. The counts say how far it got. #[serde(default)] pub error: Option, + /// What kind of failure [`error`](Self::error) is, so a front-end can word + /// it in the user's language. The text itself is the daemon's English. + #[serde(default)] + pub error_kind: Option, + /// True for a scan that uploads nothing ([`Request::ImportTakeout`] with + /// `dry_run`). [`to_upload`](Self::to_upload) then says what an import + /// would upload. + #[serde(default)] + pub dry_run: bool, + /// Photos not yet on the account: what a dry run would upload, and what a + /// real run set out to upload once the duplicates were known. + #[serde(default)] + pub to_upload: usize, + /// Bytes of [`to_upload`](Self::to_upload). + #[serde(default)] + pub to_upload_bytes: u64, + /// Live and motion photos whose video was filed with its still. + #[serde(default)] + pub live_photos: usize, + /// True when the service stopped while the import ran (a shutdown or a + /// restart). [`Request::ResumeImport`] picks it up again. + #[serde(default)] + pub interrupted: bool, + /// Archives that could not be read and were left out, by path. + #[serde(default)] + pub unreadable_archives: Vec, +} + +impl ImportSummary { + /// Whether the run did all it set out to do. + pub fn complete(&self) -> bool { + self.error.is_none() + && !self.cancelled + && !self.interrupted + && self.failed == 0 + && self.unreadable_archives.is_empty() + } +} + +/// Which step a running Takeout import is on. +#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum ImportPhase { + /// Reading the archives' directories and metadata. + #[default] + Scanning, + /// Asking the account which photos it already has. + Checking, + /// Uploading the photos that are new. + Uploading, + /// Creating albums and filing photos into them. + Albums, +} + +/// Where a running import is ([`Response::ImportStatus`]). +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] +pub struct ImportProgress { + pub phase: ImportPhase, + /// Steps done in this phase. + pub done: u64, + /// Steps in this phase; `0` while not yet known. + pub total: u64, + /// The photo being uploaded, during [`ImportPhase::Uploading`]. + #[serde(default)] + pub current: Option, } /// One album in a [`Response::Albums`] listing. @@ -1792,6 +1866,11 @@ pub struct PhotoItem { /// the JPEG it shows is not the whole of what is stored. #[serde(default)] pub has_raw: bool, + /// GeoNames id of the town the photo was taken in, when its location is + /// known; [`crate::places::city`] names it. Older daemons omit it, and + /// album entries do not carry it. + #[serde(default)] + pub place_id: Option, } /// A daemon too old to classify a timeline entry is assumed to have served a @@ -1872,11 +1951,26 @@ pub fn is_video_file_name(name: &str, size: Option) -> bool { !transport_stream || size.is_none_or(|size| size >= TS_VIDEO_MIN_BYTES) } +/// Whether `name` is a song or other recording in a format that can carry +/// its own cover picture: ID3 (MP3, AAC, WAV, AIFF), FLAC and Ogg pictures, +/// MP4 `covr` (M4A, M4B), or APE tags (WavPack, Monkey's Audio). +pub fn is_audio_file_name(name: &str) -> bool { + let Some((_, extension)) = name.rsplit_once('.') else { + return false; + }; + AUDIO_COVER_EXTENSIONS.contains(&extension.to_ascii_lowercase().as_str()) +} + +const AUDIO_COVER_EXTENSIONS: &[&str] = &[ + "aac", "aif", "aiff", "ape", "flac", "m4a", "m4b", "mp3", "oga", "ogg", "opus", "wav", "wv", +]; + /// Whether file listings show a thumbnail for a file named `name`, of `size` -/// bytes when known: an image [`is_thumbnail_image_name`] accepts, or a video, -/// whose thumbnail is a frame the daemon takes with ffmpeg. +/// bytes when known: an image [`is_thumbnail_image_name`] accepts, a video, +/// whose thumbnail is a frame the daemon takes with ffmpeg, or a song, whose +/// thumbnail is its cover: the picture in its tags, or a cover image beside it. pub fn is_thumbnail_media_name(name: &str, size: Option) -> bool { - is_thumbnail_image_name(name) || is_video_file_name(name, size) + is_thumbnail_image_name(name) || is_video_file_name(name, size) || is_audio_file_name(name) } /// Whether `name` is one of the concrete RAW formats handled by the exiftool @@ -2184,8 +2278,17 @@ pub enum Response { /// report; `summary` is `None` when no import has run this session. ImportStatus { running: bool, + /// The counts so far while running, the final report after. #[serde(default)] summary: Option, + /// The current step, while running. + #[serde(default)] + progress: Option, + /// The archives of the last run, when it can be resumed with + /// [`Request::ResumeImport`]: it was a real import that did not + /// finish. Survives a restart of the service. + #[serde(default)] + resumable: Vec, }, /// How the photo re-date is doing (reply to [`Request::RedateStatus`]). /// `running` is false once it has finished, when `summary` is the final @@ -2658,6 +2761,16 @@ mod tests { assert!(end.is_none(), "{end:?}"); } + #[test] + fn a_thumbnail_change_travels_as_its_own_topic() { + let line = serde_json::to_string(&Event::Changed { + topics: vec![Topic::Thumbnails], + }) + .unwrap(); + assert_eq!(line, r#"{"Changed":{"topics":["thumbnails"]}}"#); + assert!(Topic::ALL.contains(&Topic::Thumbnails)); + } + #[test] fn a_daemon_without_subscriptions_is_told_apart_from_a_dead_one() { let socket = fake_daemon("legacy", "{\"Error\":{\"message\":\"unknown request\"}}\n"); @@ -2722,7 +2835,7 @@ mod tests { } #[test] - fn listings_thumbnail_images_and_videos() { + fn listings_thumbnail_images_videos_and_songs() { assert!(is_thumbnail_media_name("photo.JPG", Some(10))); assert!(is_thumbnail_media_name("camera.nef", None)); assert!(is_thumbnail_media_name("clip.mp4", Some(10))); @@ -2731,13 +2844,24 @@ mod tests { Some(64 * 1024 * 1024) )); assert!(!is_thumbnail_media_name("app.ts", Some(2048))); - assert!(!is_thumbnail_media_name( - "song.flac", - Some(64 * 1024 * 1024) - )); + assert!(is_thumbnail_media_name("song.flac", Some(64 * 1024 * 1024))); + assert!(is_thumbnail_media_name("Track 01.MP3", None)); assert!(!is_thumbnail_media_name("notes.txt", None)); } + #[test] + fn songs_are_told_by_their_extension() { + for name in [ + "a.mp3", "b.FLAC", "c.ogg", "d.oga", "e.opus", "f.m4a", "g.m4b", "h.aac", "i.wav", + "j.aiff", "k.aif", "l.wv", "m.ape", + ] { + assert!(is_audio_file_name(name), "{name}"); + } + for name in ["mp3", "song.mp3.zip", "clip.mp4", "notes.txt", "cover.jpg"] { + assert!(!is_audio_file_name(name), "{name}"); + } + } + /// Keep every extension accepted by the shared classifier covered. Drive /// activation uses this classification before it knows whether opening a /// file should stream through FUSE or materialise a complete cache entry; diff --git a/crates/pdfs-core/src/db/albums.rs b/crates/pdfs-core/src/db/albums.rs index 1ff0469..f41c01c 100644 --- a/crates/pdfs-core/src/db/albums.rs +++ b/crates/pdfs-core/src/db/albums.rs @@ -186,6 +186,8 @@ impl Db { // made, and it shows exactly what they put in it. group_size: 1, has_raw: false, + // Album rows carry no location. + place_id: None, }) })? .collect::>()?; @@ -247,6 +249,8 @@ impl Db { // made, and it shows exactly what they put in it. group_size: 1, has_raw: false, + // Album rows carry no location. + place_id: None, }) })? .collect::>()?; diff --git a/crates/pdfs-core/src/db/mod.rs b/crates/pdfs-core/src/db/mod.rs index c397024..8d0da7f 100644 --- a/crates/pdfs-core/src/db/mod.rs +++ b/crates/pdfs-core/src/db/mod.rs @@ -445,6 +445,13 @@ fn is_in_memory(path: &Path) -> bool { /// SQLite uses POSIX record locks, so the two never interact, and a lock file /// left behind carries no state — the lock lives in the kernel and dies with /// the process, however it exits. +/// +/// A refusal is retried for [`LOCK_RETRY`] before it counts as another +/// daemon. A child this process spawns holds a copy of every descriptor from +/// its fork until its exec closes them, and `flock` belongs to the open file, +/// not the descriptor. So a lock the process has just let go of can stay held +/// for that moment, and a reopen right after a drop is refused +/// (`docs/BUGS.md` B290). fn acquire_single_writer_lock(path: &Path) -> Result> { if is_in_memory(path) { return Ok(None); @@ -456,22 +463,32 @@ fn acquire_single_writer_lock(path: &Path) -> Result> { .truncate(false) .write(true) .open(&lock_path)?; - // SAFETY: `file` owns the descriptor and outlives the call. - let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }; - if rc != 0 { + let deadline = std::time::Instant::now() + LOCK_RETRY; + loop { + // SAFETY: `file` owns the descriptor and outlives the call. + let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }; + if rc == 0 { + return Ok(Some(file)); + } let e = std::io::Error::last_os_error(); - if e.raw_os_error() == Some(libc::EWOULDBLOCK) { + if e.raw_os_error() != Some(libc::EWOULDBLOCK) { + return Err(Error::Io(e)); + } + if std::time::Instant::now() >= deadline { return Err(Error::Other(format!( "another Proton Drive daemon is already using {} — \ stop it first (`systemctl --user stop proton-drive.service`)", path.display() ))); } - return Err(Error::Io(e)); + std::thread::sleep(std::time::Duration::from_millis(20)); } - Ok(Some(file)) } +/// How long a refused single-writer lock is retried; far longer than a fork +/// takes to reach its exec, far shorter than anyone notices. +const LOCK_RETRY: std::time::Duration = std::time::Duration::from_millis(500); + fn lock_path_for(path: &Path) -> PathBuf { let mut name = path.as_os_str().to_owned(); name.push(".lock"); diff --git a/crates/pdfs-core/src/db/photos.rs b/crates/pdfs-core/src/db/photos.rs index 003561c..d2562c7 100644 --- a/crates/pdfs-core/src/db/photos.rs +++ b/crates/pdfs-core/src/db/photos.rs @@ -40,6 +40,8 @@ pub struct StoredPhoto { /// Whether any member of the group is a camera raw file. The grid badges /// that, because the JPEG it shows is not the whole of what is stored. pub has_raw: bool, + /// The town the photo was taken in, once its location has been read. + pub place_id: Option, } /// A town photos were taken in: its GeoNames id (see [`crate::places`]), how @@ -256,7 +258,7 @@ fn group_photos(entries: &[Grouped]) -> Vec { /// own group: the column is `NULL` until the next refresh computes it, and a /// `NULL` would otherwise match nothing and drop the photo off the page. const PHOTO_SELECT: &str = "SELECT p.uid, p.capture_time, p.name, p.ratio, p.thumb_state, \ - p.kind, p.favorite, g.n, g.raw \ + p.kind, p.favorite, g.n, g.raw, p.place_id \ FROM photos p JOIN (SELECT COALESCE(group_key, uid) AS k, COUNT(*) AS n, \ MAX(kind = 2) AS raw FROM photos GROUP BY k) g ON g.k = COALESCE(p.group_key, p.uid)"; @@ -272,6 +274,7 @@ fn stored_photo(r: &rusqlite::Row<'_>) -> rusqlite::Result { favorite: r.get::<_, i64>(6)? != 0, group_size: r.get::<_, i64>(7)?.max(1) as usize, has_raw: r.get::<_, i64>(8)? != 0, + place_id: r.get(9)?, }) } diff --git a/crates/pdfs-core/src/db/tests.rs b/crates/pdfs-core/src/db/tests.rs index baa5a22..b80e3f9 100644 --- a/crates/pdfs-core/src/db/tests.rs +++ b/crates/pdfs-core/src/db/tests.rs @@ -363,6 +363,23 @@ fn place_photos_carry_their_own_location() { ); } +/// A timeline photo names the town it was taken in, so the viewer can show it; +/// one without a location names none. +#[test] +fn timeline_photos_name_their_town() { + let db = Db::open_in_memory().unwrap(); + db.photos_replace(&[ + located("b", 500, "IMG_5.JPG", BERLIN), + TimelineRow::new("n", 400), + ]) + .unwrap(); + let berlin = db.photo_places().unwrap()[0].id; + let mut photos = db.photos_by_uid(&["b".into(), "n".into()]).unwrap(); + photos.sort_by(|a, b| a.uid.cmp(&b.uid)); + let towns: Vec> = photos.iter().map(|photo| photo.place_id).collect(); + assert_eq!(towns, [Some(berlin), None]); +} + /// A refresh that skips an already-resolved photo keeps where it was taken; one /// that reads the node again takes what the node now says. #[test] diff --git a/crates/pdfs-core/src/lib.rs b/crates/pdfs-core/src/lib.rs index 723dfa2..516898c 100644 --- a/crates/pdfs-core/src/lib.rs +++ b/crates/pdfs-core/src/lib.rs @@ -23,6 +23,7 @@ pub mod places; pub mod profile; pub mod sdkcache; pub mod search; +mod secretservice; pub mod service; pub mod shell; pub mod similar; diff --git a/crates/pdfs-core/src/opener.rs b/crates/pdfs-core/src/opener.rs index c21d477..0617dad 100644 --- a/crates/pdfs-core/src/opener.rs +++ b/crates/pdfs-core/src/opener.rs @@ -206,26 +206,23 @@ pub fn reap(mut child: std::process::Child) { }); } -/// [`open`] with the policy from the user's `config.json`, read once per -/// process. For callers that open the occasional path and have no reason to -/// carry a policy around; long-lived windows should hold their own copy. +/// [`open`] with the policy from the user's `config.json`, read again on +/// every call: the file is small, a path is opened by hand, and a rule changed +/// in Preferences then applies to the next file opened, without a restart. pub fn open_default(path: &Path, is_dir: bool) { - open(default_policy(), path, is_dir); + open(&default_policy(), path, is_dir); } /// [`open_default`] matching the rules against `name` — see /// [`OpenWith::command_for_named`]. pub fn open_default_named(path: &Path, name: &str, is_dir: bool) { - open_named(default_policy(), path, name, is_dir); + open_named(&default_policy(), path, name, is_dir); } -fn default_policy() -> &'static OpenWith { - static POLICY: std::sync::OnceLock = std::sync::OnceLock::new(); - POLICY.get_or_init(|| { - crate::config::AppDirs::new() - .map(|dirs| dirs.load_config().resolved_open_with()) - .unwrap_or_default() - }) +fn default_policy() -> OpenWith { + crate::config::AppDirs::new() + .map(|dirs| dirs.load_config().resolved_open_with()) + .unwrap_or_default() } /// `$PDFS_TERMINAL`, then `$TERMINAL`, then the first [`KNOWN_TERMINALS`] entry @@ -266,6 +263,33 @@ fn with_exec_flag(program: &str) -> Vec { argv } +/// Split a command line typed by the user into argv, the way a shell would: +/// quotes keep spaces, nothing is expanded. `None` for an unclosed quote. +pub fn parse_command(line: &str) -> Option> { + shell_words::split(line).ok() +} + +/// Show `argv` as one line that [`parse_command`] turns back into it. +pub fn format_command(argv: &[String]) -> String { + shell_words::join(argv) +} + +/// Whether the program `argv` starts with can be found: an absolute or +/// relative path to a file, or a name on `PATH`. A `$VAR` program counts as +/// found, since it is looked up only when a file is opened. +pub fn command_available(argv: &[String]) -> bool { + let Some(program) = argv.first() else { + return false; + }; + if program.starts_with('$') { + return true; + } + if program.contains('/') { + return Path::new(program).is_file(); + } + on_path(program) +} + fn on_path(program: &str) -> bool { let Some(path) = std::env::var_os("PATH") else { return false; @@ -703,4 +727,24 @@ mod tests { // Unknown terminals get the near-universal `-e`. assert_eq!(with_exec_flag("mystery-term"), vec!["mystery-term", "-e"]); } + + #[test] + fn a_typed_command_line_round_trips() { + let argv = parse_command("alacritty -e 'my editor' --flag").unwrap(); + assert_eq!(argv, ["alacritty", "-e", "my editor", "--flag"]); + assert_eq!(parse_command(&format_command(&argv)).unwrap(), argv); + assert_eq!(parse_command("nvim 'unclosed"), None); + assert_eq!(parse_command(" ").unwrap(), Vec::::new()); + } + + #[test] + fn a_command_is_available_when_its_program_is() { + assert!(command_available(&["sh".to_string()])); + assert!(command_available(&["$EDITOR".to_string()])); + assert!(!command_available(&[])); + assert!(!command_available( + &["pdfs-no-such-program-xyz".to_string()] + )); + assert!(!command_available(&["/nonexistent/program".to_string()])); + } } diff --git a/crates/pdfs-core/src/secretservice.rs b/crates/pdfs-core/src/secretservice.rs new file mode 100644 index 0000000..5543ba3 --- /dev/null +++ b/crates/pdfs-core/src/secretservice.rs @@ -0,0 +1,135 @@ +//! Saving a new secret into an existing Secret Service item by replacing the +//! item, not by editing its secret (`docs/BUGS.md` B291). +//! +//! oo7-daemon before 0.7.0 applies `SetSecret` only in memory: the keyring +//! file keeps the old secret, and it comes back once the daemon restarts. +//! Fedora 45 ships such a build (`0.7.0~beta^1.e91b98a`). Every refreshed +//! sign-in pdfs saved there was gone after a reboot, and the next start found +//! a refresh token Proton had already spent. +//! +//! `CreateItem` with `replace` is written to the file there, and every +//! provider implements it. So a secret that changes is written as a new item +//! that replaces the old one: in the old one's collection, with its label and +//! all of its attributes, so the provider recognises it as the same entry. + +use dbus_secret_service::{Collection, EncryptionType, Path, SecretService}; +use std::collections::HashMap; + +/// The content type the keyring store writes its secrets with. +const CONTENT_TYPE: &str = "application/octet-stream"; + +/// What [`replace`] did. +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum Replaced { + /// The secret is saved. + Saved, + /// Nothing was changed; save it the usual way. Says why, for the log. + Skipped(String), +} + +/// Save `secret` into the one item for `service`/`user` by replacing it. +/// +/// Skips, changing nothing, when there is no such item yet (the store creates +/// it), when there is more than one, when it is locked, or when the Secret +/// Service fails before the new item exists. Never shows a prompt: one would +/// block the caller, so it is dismissed and the write is skipped. +pub(crate) fn replace(service: &str, user: &str, secret: &[u8]) -> Replaced { + match try_replace(service, user, secret) { + Ok(replaced) => replaced, + Err(e) => Replaced::Skipped(e.to_string()), + } +} + +fn try_replace( + service: &str, + user: &str, + secret: &[u8], +) -> Result { + let ss = SecretService::connect_with_max_prompt_timeout(EncryptionType::Dh, 0)?; + let lookup = HashMap::from([("service", service), ("username", user)]); + let found = ss.search_items(lookup.clone())?; + if !found.locked.is_empty() { + return Ok(Replaced::Skipped("the item is locked".into())); + } + let [old] = found.unlocked.as_slice() else { + return Ok(Replaced::Skipped(format!( + "{} items match, not one", + found.unlocked.len() + ))); + }; + let Some(collection) = collection_of(&old.path) else { + return Ok(Replaced::Skipped(format!( + "no collection in the item path {}", + old.path + ))); + }; + let label = old.get_label()?; + let attributes = old.get_attributes()?; + let attributes: HashMap<&str, &str> = attributes + .iter() + .map(|(k, v)| (k.as_str(), v.as_str())) + .collect(); + let collection = Collection::new(&ss, collection); + let new = collection.create_item(&label, attributes, secret, true, CONTENT_TYPE)?; + + // A provider that did not match the old item leaves it next to the new + // one, and two items make every read ambiguous. + let after = ss.search_items(lookup)?; + for item in after.unlocked.iter().chain(&after.locked) { + if item.path == new.path { + continue; + } + if let Err(e) = item.delete() { + // The old item still holds the previous secret, so taking the new + // one out leaves things as they were, for the usual write. + if let Err(undo) = new.delete() { + tracing::error!( + error = %undo, + "could not remove a duplicate keyring item; remove one of the two \ + `proton-drive-linux` entries in your keyring and sign in again" + ); + } + return Ok(Replaced::Skipped(format!( + "could not remove the replaced item: {e}" + ))); + } + } + Ok(Replaced::Saved) +} + +/// The collection an item path belongs to: the path without its last part, +/// as long as it is a Secret Service object path. +fn collection_of(item: &Path<'_>) -> Option> { + let (collection, _) = item.rsplit_once('/')?; + if !collection.starts_with("/org/freedesktop/secrets/") || collection.ends_with('/') { + return None; + } + Path::new(collection.to_owned()).ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn collection(item: &str) -> Option { + collection_of(&Path::new(item).unwrap()).map(|p| p.to_string()) + } + + #[test] + fn an_item_belongs_to_the_collection_its_path_names() { + assert_eq!( + collection("/org/freedesktop/secrets/collection/login/14").as_deref(), + Some("/org/freedesktop/secrets/collection/login") + ); + assert_eq!( + collection("/org/freedesktop/secrets/aliases/default/3").as_deref(), + Some("/org/freedesktop/secrets/aliases/default") + ); + } + + #[test] + fn a_path_outside_the_secret_service_names_no_collection() { + assert_eq!(collection("/org/example/item/1"), None); + assert_eq!(collection("/item"), None); + } +} diff --git a/crates/pdfs-core/src/syncignore.rs b/crates/pdfs-core/src/syncignore.rs index 35c8d5e..f4c9717 100644 --- a/crates/pdfs-core/src/syncignore.rs +++ b/crates/pdfs-core/src/syncignore.rs @@ -30,6 +30,11 @@ use ignore::gitignore::{Gitignore, GitignoreBuilder}; /// them. Version-control metadata, dependency and build trees, editor /// leftovers, and OS turds — none of which belong on Drive, all of which are /// large or rewritten constantly. +/// +/// The second half is what Nextcloud, Dropbox and Syncthing leave out as well: +/// files a desktop, a file system or a program in the middle of a write leaves +/// behind, never anything a person made. Every one is in a test, next to a real +/// file of a similar name that must stay synced. pub const DEFAULT_IGNORE_PATTERNS: &[&str] = &[ ".git/", ".hg/", @@ -43,6 +48,23 @@ pub const DEFAULT_IGNORE_PATTERNS: &[&str] = &[ "*.tmp", ".DS_Store", "Thumbs.db", + // Folder settings of Windows and KDE's Dolphin. + "desktop.ini", + ".directory", + // Trash folders and file-system leftovers: a file deleted while open on a + // FUSE or NFS mount lives on under one of these names until it is closed. + ".Trash-*/", + ".Trashes/", + ".Spotlight-V100/", + ".fuse_hidden*", + ".nfs*", + // Downloads and saves still being written. + "*.part", + "*.crdownload", + "*.kate-swp", + // Office lock files: LibreOffice's `.~lock.#`, Microsoft Office's `~$`. + ".~lock.*#", + "~$*", ]; /// Suffixes that mean a file is incomplete *by construction*: only the program @@ -119,6 +141,225 @@ pub fn is_scratch_name(name: &str) -> bool { /// Filenames accepted as a folder's ignore file, in precedence order. const IGNORE_FILE_NAMES: &[&str] = &[".pdfsignore", ".protonignore"]; +/// Whether `line` is a rule [`IgnoreRules::load`] accepts: a pattern, a +/// comment or a blank line. The error says what is wrong with it. +pub fn check_pattern(line: &str) -> Result<(), String> { + GitignoreBuilder::new("/") + .add_line(None, line) + .map(|_| ()) + .map_err(|e| e.to_string()) +} + +/// The lines of an ignore file that [`IgnoreRules::load`] would skip, by line +/// number (from 1), with what is wrong with each. +pub fn check_rules(text: &str) -> Vec<(usize, String)> { + text.lines() + .enumerate() + .filter_map(|(i, line)| check_pattern(line).err().map(|e| (i + 1, e))) + .collect() +} + +/// A synced folder's own ignore file, read so it can be edited and written +/// back. See [`FolderRules::read`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FolderRules { + /// The file the rules are in: the one in use, or `.pdfsignore` when there + /// is none yet. + pub path: std::path::PathBuf, + /// The rules as read; empty when there is no file. `None` means there was + /// no file, which [`write`](Self::write) checks is still so. + pub text: Option, +} + +/// Why [`FolderRules::write`] did not write. +#[derive(Debug)] +pub enum WriteRulesError { + /// The file changed after it was read, for example by an edit on another + /// computer that sync brought in. The rules on disk are the new ones. + Changed, + /// Reading or writing failed. + Io(std::io::Error), +} + +impl std::fmt::Display for WriteRulesError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Changed => f.write_str("the file changed since it was opened"), + Self::Io(e) => e.fmt(f), + } + } +} + +impl std::error::Error for WriteRulesError {} + +impl From for WriteRulesError { + fn from(e: std::io::Error) -> Self { + Self::Io(e) + } +} + +impl FolderRules { + /// Read the ignore file of the folder at `root`, the same one + /// [`IgnoreRules::load`] uses. + pub fn read(root: &Path) -> std::io::Result { + for name in IGNORE_FILE_NAMES { + let path = root.join(name); + match std::fs::read_to_string(&path) { + Ok(text) => { + return Ok(Self { + path, + text: Some(text), + }); + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue, + Err(e) => return Err(e), + } + } + // Like `load`, a missing folder reads as one without rules; the write + // is what fails then. + Ok(Self { + path: root.join(IGNORE_FILE_NAMES[0]), + text: None, + }) + } + + /// Replace the rules with `text`, unless the file changed since it was + /// read. Rules that are all blank remove the file, so a folder never keeps + /// an empty one around. + /// + /// Written to a temporary file and renamed over the old one, so the sync + /// pass reading the rules at the same moment sees the old ones or the new + /// ones, never half of them. + pub fn write(&self, text: &str) -> Result<(), WriteRulesError> { + let now = match std::fs::read_to_string(&self.path) { + Ok(now) => Some(now), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => None, + Err(e) => return Err(e.into()), + }; + if now != self.text { + return Err(WriteRulesError::Changed); + } + if text.trim().is_empty() { + return match std::fs::remove_file(&self.path) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e.into()), + _ => Ok(()), + }; + } + let Some(name) = self.path.file_name() else { + return Err(std::io::Error::other("the rules path has no file name").into()); + }; + let mut tmp_name = std::ffi::OsString::from("."); + tmp_name.push(name); + tmp_name.push(format!(".tmp-{}", std::process::id())); + let tmp = self.path.with_file_name(tmp_name); + let mut body = text.to_string(); + if !body.ends_with('\n') { + body.push('\n'); + } + let written = std::fs::write(&tmp, body).and_then(|()| std::fs::rename(&tmp, &self.path)); + if written.is_err() { + let _ = std::fs::remove_file(&tmp); + } + Ok(written?) + } + + /// The patterns in the rules: every line that is not blank or a comment, + /// trimmed of surrounding white space. + pub fn patterns(&self) -> Vec<&str> { + self.text + .as_deref() + .unwrap_or_default() + .lines() + .map(str::trim) + .filter(|line| !line.is_empty() && !line.starts_with('#')) + .collect() + } +} + +/// How often [`add_folder_pattern`] and [`remove_folder_pattern`] read the +/// rules again when they changed between the read and the write. +const RULES_RETRIES: usize = 3; + +/// Add `pattern` at the end of the own rules of the folder at `root`, keeping +/// every other line and comment. Says whether it added it: a pattern already +/// there is left alone. +pub fn add_folder_pattern(root: &Path, pattern: &str) -> std::io::Result { + let pattern = pattern.trim(); + edit_folder_rules(root, |rules| { + if rules.patterns().contains(&pattern) { + return None; + } + let mut text = rules.text.clone().unwrap_or_default(); + if !text.is_empty() && !text.ends_with('\n') { + text.push('\n'); + } + text.push_str(pattern); + text.push('\n'); + Some(text) + }) +} + +/// Remove every line that is `pattern` from the own rules of the folder at +/// `root`. Says whether there was one. +pub fn remove_folder_pattern(root: &Path, pattern: &str) -> std::io::Result { + let pattern = pattern.trim(); + edit_folder_rules(root, |rules| { + if !rules.patterns().contains(&pattern) { + return None; + } + let text = rules.text.as_deref().unwrap_or_default(); + Some( + text.lines() + .filter(|line| line.trim() != pattern) + .map(|line| format!("{line}\n")) + .collect(), + ) + }) +} + +/// Read the folder's rules, let `edit` return their new text (or `None` to +/// leave them), and write it. Rules that changed in between, such as by sync +/// bringing in an edit from another computer, are read again and edited anew. +fn edit_folder_rules( + root: &Path, + edit: impl Fn(&FolderRules) -> Option, +) -> std::io::Result { + for _ in 0..RULES_RETRIES { + let rules = FolderRules::read(root)?; + let Some(text) = edit(&rules) else { + return Ok(false); + }; + match rules.write(&text) { + Ok(()) => return Ok(true), + Err(WriteRulesError::Changed) => continue, + Err(WriteRulesError::Io(e)) => return Err(e), + } + } + Err(std::io::Error::other( + "the rules kept changing while they were being saved", + )) +} + +/// Where a rule that decided about a path came from. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum RuleSource { + /// The rules for every synced folder (`ignore_patterns`). + AllFolders, + /// The folder's own ignore file. + ThisFolder, +} + +/// What the rules say about one path, and which rule says it. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Verdict { + /// No rule matches: the path is synced. + Synced, + /// Left out by `pattern`. `pattern` may name a folder the path is in. + Excluded { pattern: String, source: RuleSource }, + /// A rule excluded it, and a later `!pattern` brought it back. + Included { pattern: String, source: RuleSource }, +} + /// Compiled ignore rules for one synced folder. pub struct IgnoreRules { matcher: Option, @@ -161,6 +402,27 @@ impl IgnoreRules { } } + /// [`load`](Self::load) with `text` standing in for the folder's ignore + /// file, for showing what rules being edited would do before they are + /// saved. + pub fn with_folder_text(root: &Path, globals: &[String], text: &str) -> Self { + let mut builder = GitignoreBuilder::new(root); + let mut any = false; + for pattern in globals { + any |= builder.add_line(None, pattern).is_ok(); + } + let file = root.join(IGNORE_FILE_NAMES[0]); + for line in text.lines() { + any |= builder.add_line(Some(file.clone()), line).is_ok(); + } + if !any { + return Self { matcher: None }; + } + Self { + matcher: builder.build().ok(), + } + } + /// Rules matching nothing, for callers with no folder context. pub fn empty() -> Self { Self { matcher: None } @@ -207,6 +469,50 @@ impl IgnoreRules { } } +impl IgnoreRules { + /// What the rules say about `rel`, and which rule decides, the way + /// [`is_ignored`](Self::is_ignored) decides it. + pub fn explain(&self, rel: &str, is_dir: bool) -> Verdict { + let Some(matcher) = &self.matcher else { + return Verdict::Synced; + }; + let rel = rel.trim_matches('/'); + if rel.is_empty() { + return Verdict::Synced; + } + let source = |glob: &ignore::gitignore::Glob| match glob.from() { + Some(_) => RuleSource::ThisFolder, + None => RuleSource::AllFolders, + }; + let mut prefix = String::new(); + let mut parts = rel.split('/').peekable(); + while let Some(part) = parts.next() { + if !prefix.is_empty() { + prefix.push('/'); + } + prefix.push_str(part); + let last = parts.peek().is_none(); + let as_dir = if last { is_dir } else { true }; + match matcher.matched(&prefix, as_dir) { + ignore::Match::Ignore(glob) => { + return Verdict::Excluded { + pattern: glob.original().to_string(), + source: source(glob), + }; + } + ignore::Match::Whitelist(glob) if last => { + return Verdict::Included { + pattern: glob.original().to_string(), + source: source(glob), + }; + } + _ => {} + } + } + Verdict::Synced + } +} + impl std::fmt::Debug for IgnoreRules { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("IgnoreRules") @@ -255,6 +561,198 @@ mod tests { std::fs::write(root.join(name), body).unwrap(); } + #[test] + fn the_added_defaults_leave_out_clutter_but_not_look_alikes() { + let dir = TempDir::new(); + let rules = IgnoreRules::load(dir.path(), &globals()); + for (clutter, is_dir) in [ + ("desktop.ini", false), + ("Photos/.directory", false), + (".Trash-1000", true), + (".Trashes", true), + (".Spotlight-V100", true), + (".fuse_hidden0000001a00000002", false), + (".nfs000000000123abcd", false), + ("movie.mkv.part", false), + ("setup.exe.crdownload", false), + ("notes.txt.kate-swp", false), + (".~lock.report.odt#", false), + ("~$budget.xlsx", false), + ] { + assert!(rules.is_ignored(clutter, is_dir), "{clutter}"); + } + for kept in [ + "Desktop/notes.txt", + "directory.txt", + "Trash-talk.md", + "nfs-setup.md", + "parts.csv", + "part.txt", + "report.odt", + "budget.xlsx", + ] { + assert!(!rules.is_ignored(kept, false), "{kept}"); + } + } + + #[test] + fn a_bad_pattern_is_reported_with_its_line() { + assert!(check_pattern("*.db").is_ok()); + assert!(check_pattern("# a comment").is_ok()); + assert!(check_pattern("").is_ok()); + assert!(check_pattern("*.{jpg,png").is_err()); + assert!(check_pattern("[z-a]").is_err()); + let bad = check_rules("*.log\n{oops\nbuild/\n"); + assert_eq!(bad.len(), 1); + assert_eq!(bad[0].0, 2); + } + + #[test] + fn explain_names_the_rule_and_where_it_came_from() { + let dir = TempDir::new(); + write(dir.path(), ".pdfsignore", "*.db\n!keep.tmp\n"); + let rules = IgnoreRules::load(dir.path(), &globals()); + assert_eq!( + rules.explain("data/cache.db", false), + Verdict::Excluded { + pattern: "*.db".into(), + source: RuleSource::ThisFolder + } + ); + assert_eq!( + rules.explain("app/node_modules/x/index.js", false), + Verdict::Excluded { + pattern: "node_modules/".into(), + source: RuleSource::AllFolders + } + ); + assert_eq!( + rules.explain("keep.tmp", false), + Verdict::Included { + pattern: "!keep.tmp".into(), + source: RuleSource::ThisFolder + } + ); + assert_eq!(rules.explain("src/main.rs", false), Verdict::Synced); + // `explain` and `is_ignored` never disagree. + for (path, is_dir) in [ + ("data/cache.db", false), + ("keep.tmp", false), + ("x.tmp", false), + ] { + assert_eq!( + rules.is_ignored(path, is_dir), + matches!(rules.explain(path, is_dir), Verdict::Excluded { .. }), + "{path}" + ); + } + } + + #[test] + fn rules_being_edited_explain_like_the_saved_ones() { + let dir = TempDir::new(); + let text = "*.db\n!keep.db\n"; + write(dir.path(), ".pdfsignore", text); + let saved = IgnoreRules::load(dir.path(), &globals()); + let edited = IgnoreRules::with_folder_text(dir.path(), &globals(), text); + for path in ["a.db", "keep.db", "node_modules/x", "src/x.rs"] { + assert_eq!( + saved.explain(path, false), + edited.explain(path, false), + "{path}" + ); + } + } + + #[test] + fn folder_rules_are_written_back_and_removed_when_blank() { + let dir = TempDir::new(); + let rules = FolderRules::read(dir.path()).unwrap(); + assert_eq!(rules.text, None); + assert_eq!(rules.path, dir.path().join(".pdfsignore")); + rules.write("*.db").unwrap(); + assert_eq!( + std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(), + "*.db\n" + ); + assert!(IgnoreRules::load(dir.path(), &[]).is_ignored("x.db", false)); + + let rules = FolderRules::read(dir.path()).unwrap(); + rules.write(" \n").unwrap(); + assert!(!dir.path().join(".pdfsignore").exists()); + } + + #[test] + fn folder_rules_keep_the_file_name_in_use() { + let dir = TempDir::new(); + write(dir.path(), ".protonignore", "old/\n"); + let rules = FolderRules::read(dir.path()).unwrap(); + assert_eq!(rules.path, dir.path().join(".protonignore")); + rules.write("new/\n").unwrap(); + assert!(!dir.path().join(".pdfsignore").exists()); + assert_eq!( + std::fs::read_to_string(dir.path().join(".protonignore")).unwrap(), + "new/\n" + ); + } + + #[test] + fn folder_rules_changed_elsewhere_are_not_written_over() { + let dir = TempDir::new(); + write(dir.path(), ".pdfsignore", "a/\n"); + let rules = FolderRules::read(dir.path()).unwrap(); + write(dir.path(), ".pdfsignore", "b/\n"); + assert!(matches!(rules.write("c/"), Err(WriteRulesError::Changed))); + assert_eq!( + std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(), + "b/\n" + ); + + // A file that appeared after an empty read counts as a change too. + let dir = TempDir::new(); + let rules = FolderRules::read(dir.path()).unwrap(); + write(dir.path(), ".pdfsignore", "b/\n"); + assert!(matches!(rules.write("c/"), Err(WriteRulesError::Changed))); + } + + #[test] + fn a_pattern_added_to_a_folder_keeps_its_other_lines() { + let dir = TempDir::new(); + assert!(add_folder_pattern(dir.path(), " *.db ").unwrap()); + assert_eq!( + std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(), + "*.db\n" + ); + + write(dir.path(), ".pdfsignore", "# my notes\nbuild/"); + assert!(add_folder_pattern(dir.path(), "*.db").unwrap()); + assert!(!add_folder_pattern(dir.path(), "build/").unwrap()); + assert_eq!( + std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(), + "# my notes\nbuild/\n*.db\n" + ); + assert_eq!( + FolderRules::read(dir.path()).unwrap().patterns(), + ["build/", "*.db"] + ); + } + + #[test] + fn removing_a_folder_pattern_keeps_comments_and_drops_an_empty_file() { + let dir = TempDir::new(); + write(dir.path(), ".pdfsignore", "# keep\nbuild/\n*.db\n"); + assert!(remove_folder_pattern(dir.path(), "build/").unwrap()); + assert!(!remove_folder_pattern(dir.path(), "build/").unwrap()); + assert_eq!( + std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(), + "# keep\n*.db\n" + ); + + write(dir.path(), ".pdfsignore", "*.db\n"); + assert!(remove_folder_pattern(dir.path(), "*.db").unwrap()); + assert!(!dir.path().join(".pdfsignore").exists()); + } + #[test] fn no_rules_at_all_ignores_nothing() { let dir = TempDir::new(); diff --git a/crates/pdfs-core/src/takeout.rs b/crates/pdfs-core/src/takeout.rs index 16d0542..fba5934 100644 --- a/crates/pdfs-core/src/takeout.rs +++ b/crates/pdfs-core/src/takeout.rs @@ -25,7 +25,9 @@ //! The service folder (`Google Photos`) is localized, as are the year folders //! (`Photos from 2019`) and the trash folder, so nothing here matches on the //! service name: the first component is dropped when it is the export root, the -//! second is the service, and the rest is `/`. A folder is an +//! second is the service, and the rest is `/`. The Photos service +//! is the one whose sidecars carry a `photoTakenTime`; other services in the same +//! export (`Google Drive/`, `YouTube/`) are left out. A folder is an //! **album** when it carries an album metadata sidecar naming it; anything else //! is a plain bucket that imports into the timeline only. Trash is matched by //! name against [`TRASH_FOLDERS`] — the one place a localized name is @@ -121,8 +123,14 @@ const MEDIA_EXTENSIONS: &[(&str, &str)] = &[ ("m2ts", "video/mp2t"), ("mpg", "video/mpeg"), ("mpeg", "video/mpeg"), + // The video half of a Pixel motion photo, `PXL_….MP` beside `PXL_….MP.jpg`. + ("mp", "video/mp4"), ]; +/// Sidecars bigger than this are not read. A real one is a few kilobytes; a +/// `.json` this large is some other service's data and not worth the memory. +const SIDECAR_MAX_BYTES: u64 = 1024 * 1024; + /// The media type for `name`'s extension, or `None` when it is not media this /// import handles. pub fn media_type_of(name: &str) -> Option<&'static str> { @@ -147,6 +155,13 @@ pub enum Bucket { Archived, } +/// A position in WGS 84 decimal degrees, from a sidecar's `geoData`. +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +pub struct GeoPoint { + pub latitude: f64, + pub longitude: f64, +} + /// One media file found in the export. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct TakeoutPhoto { @@ -162,14 +177,49 @@ pub struct TakeoutPhoto { pub size: u64, /// Media type from the extension. pub media_type: String, - /// Capture time in epoch seconds, from `photoTakenTime` (falling back to - /// `creationTime`). `None` when the photo has no sidecar — the upload then - /// defaults it to now, as the SDK does. + /// Capture time in epoch seconds, from the sidecar's `photoTakenTime`, + /// else from a date in the file name. `None` when neither has one; the + /// importer then tries the file's EXIF, then [`creation_time`](Self::creation_time). pub capture_time: Option, + /// The sidecar's `creationTime`: when the photo reached Google Photos, not + /// when it was taken. Only a last resort for the capture time. + #[serde(default)] + pub creation_time: Option, + /// Where the photo was taken, from the sidecar's `geoData` (or + /// `geoDataExif`). Google writes `0, 0` for "unknown", which is dropped. + #[serde(default)] + pub location: Option, /// The sidecar's `favorited` flag. pub favorite: bool, /// Which bucket the file was found in. pub bucket: Bucket, + /// Pairs the halves of a live or motion photo: a still and a video in the + /// same folder carry the same key (`IMG_1.HEIC` + `IMG_1.MOV`, + /// `PXL_1.MP.jpg` + `PXL_1.MP`). `None` for media that cannot be either half. + #[serde(default)] + pub live_key: Option, +} + +impl TakeoutPhoto { + /// Whether this is the video half of a pair, as opposed to the still. + pub fn is_video(&self) -> bool { + self.media_type.starts_with("video/") + } + + /// Whether the pair this belongs to is a Pixel motion photo (`.MP`) rather + /// than an Apple live photo. + pub fn is_motion_photo(&self) -> bool { + self.live_key + .as_deref() + .is_some_and(|key| key.ends_with(".mp")) + } +} + +/// An archive the scan had to leave out, and why. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct UnreadableArchive { + pub path: PathBuf, + pub reason: String, } /// Everything one scan found, ready to be turned into an import plan. @@ -183,8 +233,16 @@ pub struct TakeoutScan { /// Media files skipped because they sit in the trash bucket. pub skipped_trashed: usize, /// Entries that were neither media nor a sidecar (`archive_browser.html`, - /// activity `.csv`s, `.mp` motion-photo parts). + /// activity `.csv`s), and media from other Google services in the same + /// export (`Google Drive/`, `YouTube/`). pub skipped_other: usize, + /// Archives that could not be opened or are not a zip. The rest of the + /// export is still imported. + #[serde(default)] + pub unreadable_archives: Vec, + /// Entries inside an otherwise readable archive that could not be read. + #[serde(default)] + pub unreadable_entries: usize, } impl TakeoutScan { @@ -195,7 +253,7 @@ impl TakeoutScan { } /// Google's per-photo sidecar. Only the fields the import uses are read; the -/// file also carries geo data, people tags and view counts. +/// file also carries people tags, a description and view counts. #[derive(Debug, Default, Deserialize)] struct PhotoSidecar { #[serde(default)] @@ -205,19 +263,69 @@ struct PhotoSidecar { #[serde(rename = "creationTime", default)] creation_time: Option, #[serde(default)] - favorited: bool, + favorited: Option, + /// The position as edited in Google Photos, else as the camera wrote it. + #[serde(rename = "geoData", default)] + geo_data: Option, + #[serde(rename = "geoDataExif", default)] + geo_data_exif: Option, } -/// A sidecar timestamp: epoch seconds, as a *string*. +impl PhotoSidecar { + fn location(&self) -> Option { + self.geo_data + .as_ref() + .and_then(SidecarGeo::point) + .or_else(|| self.geo_data_exif.as_ref().and_then(SidecarGeo::point)) + } +} + +/// A sidecar timestamp: epoch seconds, written as a string, sometimes a number. #[derive(Debug, Deserialize)] struct SidecarTime { #[serde(default)] - timestamp: Option, + timestamp: Option, +} + +#[derive(Debug, Deserialize)] +#[serde(untagged)] +enum SidecarNumber { + Number(i64), + Text(String), } impl SidecarTime { + /// The timestamp, when it is a real moment. `0` and negative values are + /// Google's "unknown" and would file the photo under 1970. fn epoch_seconds(&self) -> Option { - self.timestamp.as_ref()?.parse().ok() + let seconds = match self.timestamp.as_ref()? { + SidecarNumber::Number(seconds) => *seconds, + SidecarNumber::Text(text) => text.trim().parse().ok()?, + }; + (seconds > 0).then_some(seconds) + } +} + +/// `geoData` in a sidecar. All-zero means Google has no position. +#[derive(Debug, Deserialize)] +struct SidecarGeo { + #[serde(default)] + latitude: Option, + #[serde(default)] + longitude: Option, +} + +impl SidecarGeo { + fn point(&self) -> Option { + let (latitude, longitude) = (self.latitude?, self.longitude?); + let on_globe = latitude.is_finite() + && longitude.is_finite() + && (-90.0..=90.0).contains(&latitude) + && (-180.0..=180.0).contains(&longitude); + (on_globe && (latitude != 0.0 || longitude != 0.0)).then_some(GeoPoint { + latitude, + longitude, + }) } } @@ -262,21 +370,58 @@ impl RawEntry { /// is one album, and a sidecar in part 3 is matched to its photo in part 1. /// Reads only the central directory and the JSON sidecars — no media bytes, so /// this stays fast on a 200 GB export. +/// +/// An archive that cannot be opened is left out and listed in +/// [`TakeoutScan::unreadable_archives`]; the rest still imports. pub fn scan_archives(archives: &[PathBuf]) -> CoreResult { - let mut entries: Vec = Vec::new(); - let mut sidecars: HashMap> = HashMap::new(); + scan_archives_until(archives, || false) +} + +/// [`scan_archives`] that gives up when `stop` returns true, checked between +/// archives and every few hundred entries. +pub fn scan_archives_until( + archives: &[PathBuf], + stop: impl Fn() -> bool, +) -> CoreResult { + let mut entries: Vec<(String, RawEntry)> = Vec::new(); + let mut sidecars: Vec<(String, String, Vec)> = Vec::new(); + let mut unreadable_archives = Vec::new(); + let mut unreadable_entries = 0; for (index, path) in archives.iter().enumerate() { - let file = std::fs::File::open(path) - .map_err(|e| CoreError::invalid(format!("cannot open {}: {e}", path.display())))?; - let mut zip = zip::ZipArchive::new(std::io::BufReader::new(file)).map_err(|e| { - CoreError::invalid(format!("{} is not a readable zip: {e}", path.display())) - })?; + if stop() { + return Err(CoreError::conflict("the import was cancelled")); + } + let opened = std::fs::File::open(path) + .map_err(|e| e.to_string()) + .and_then(|file| { + zip::ZipArchive::new(std::io::BufReader::new(file)) + .map_err(|e| format!("not a readable zip: {e}")) + }); + let mut zip = match opened { + Ok(zip) => zip, + Err(reason) => { + tracing::warn!(archive = %path.display(), %reason, "skipping an unreadable takeout archive"); + unreadable_archives.push(UnreadableArchive { + path: path.clone(), + reason, + }); + continue; + } + }; for position in 0..zip.len() { - let mut entry = zip.by_index(position).map_err(|e| { - CoreError::invalid(format!("{}: unreadable entry: {e}", path.display())) - })?; + if position % 512 == 0 && stop() { + return Err(CoreError::conflict("the import was cancelled")); + } + let mut entry = match zip.by_index(position) { + Ok(entry) => entry, + Err(e) => { + tracing::debug!(archive = %path.display(), position, error = %e, "unreadable takeout entry"); + unreadable_entries += 1; + continue; + } + }; if entry.is_dir() { continue; } @@ -286,32 +431,92 @@ pub fn scan_archives(archives: &[PathBuf]) -> CoreResult { continue; }; let name = name.to_string_lossy().replace('\\', "/"); - let Some(relative) = strip_export_prefix(&name) else { + let Some((service, relative)) = strip_export_prefix(&name) else { continue; }; + let (service, relative) = (service.to_string(), relative.to_string()); let size = entry.size(); // Sidecars are read now, while the entry is open: they are a few // hundred bytes each and reopening the archive per photo later would // be a second full pass. if relative.to_ascii_lowercase().ends_with(".json") { - let mut body = Vec::with_capacity(size.min(64 * 1024) as usize); - if entry.read_to_end(&mut body).is_ok() { - sidecars.insert(relative.to_string(), body); + if size <= SIDECAR_MAX_BYTES { + let mut body = Vec::with_capacity(size as usize); + if entry.read_to_end(&mut body).is_ok() { + sidecars.push((service, relative, body)); + } else { + unreadable_entries += 1; + } } continue; } - - entries.push(RawEntry { - archive: index, - entry: name.clone(), - relative: relative.to_string(), - size, - }); + entries.push(( + service, + RawEntry { + archive: index, + entry: name, + relative, + size, + }, + )); } } - Ok(assemble(entries, &sidecars)) + let (entries, sidecars, other_services) = keep_photos_services(entries, sidecars); + let mut scan = assemble(entries, &sidecars); + scan.skipped_other += other_services; + scan.unreadable_archives = unreadable_archives; + scan.unreadable_entries = unreadable_entries; + Ok(scan) +} + +/// Drop what other Google services put into the same export. +/// +/// A Takeout can hold `Google Drive/`, `YouTube/` and others next to +/// `Google Photos/`, and their media must not land in the photo timeline. The +/// service folder's name is localized (`Google Fotos`), so the Photos service +/// is recognized by its content instead: it is the folder whose sidecars carry +/// a `photoTakenTime`. When no folder does — an export without sidecars, or +/// one re-zipped without the service level — everything is kept, as before. +/// +/// Returns the entries and sidecars to import, and how many media files were +/// left out. +fn keep_photos_services( + entries: Vec<(String, RawEntry)>, + sidecars: Vec<(String, String, Vec)>, +) -> (Vec, HashMap>, usize) { + #[derive(Deserialize)] + struct Marker { + #[serde(rename = "photoTakenTime")] + photo_taken_time: Option, + } + let mut photos_services: std::collections::HashSet<&str> = std::collections::HashSet::new(); + for (service, _, body) in &sidecars { + if photos_services.contains(service.as_str()) { + continue; + } + if serde_json::from_slice::(body).is_ok_and(|m| m.photo_taken_time.is_some()) { + photos_services.insert(service.as_str()); + } + } + let keep = |service: &str| photos_services.is_empty() || photos_services.contains(service); + + let mut skipped = 0; + let mut kept_entries = Vec::with_capacity(entries.len()); + for (service, raw) in entries { + if keep(&service) { + kept_entries.push(raw); + } else if media_type_of(raw.file_name()).is_some() { + skipped += 1; + } + } + let kept_sidecars = sidecars + .iter() + .filter(|(service, _, _)| keep(service)) + .map(|(_, relative, body)| (relative.clone(), body.clone())) + .collect(); + (kept_entries, kept_sidecars, skipped) } /// Build the scan from raw entries plus the sidecar bodies keyed by their path @@ -403,19 +608,19 @@ fn assemble(entries: Vec, sidecars: &HashMap>) -> Take .unwrap_or(&file_name) .to_string(); - // Sidecar first, then the date stamped into the name. + // Sidecar first, then the date stamped into the name. `creationTime` + // is when the photo reached Google, which for a scan or an old photo + // uploaded late is years off; the importer uses it only after EXIF. let capture_time = sidecar .photo_taken_time .as_ref() .and_then(SidecarTime::epoch_seconds) - .or_else(|| { - sidecar - .creation_time - .as_ref() - .and_then(SidecarTime::epoch_seconds) - }) .or_else(|| capture_time_from_name(&name)) .or_else(|| capture_time_from_name(&file_name)); + let creation_time = sidecar + .creation_time + .as_ref() + .and_then(SidecarTime::epoch_seconds); scan.photos.push(TakeoutPhoto { archive: raw.archive, @@ -424,31 +629,57 @@ fn assemble(entries: Vec, sidecars: &HashMap>) -> Take size: raw.size, media_type: media_type.to_string(), capture_time, - favorite: sidecar.favorited, + creation_time, + location: sidecar.location(), + favorite: sidecar.favorited.unwrap_or(false), bucket, + live_key: live_key(&folder, &file_name), }); } scan } -/// Drop the export root (`Takeout/`) and the localized service folder from a -/// path inside the archive, leaving `/` or ``. +/// Split a path inside the archive into the localized service folder and the +/// rest, dropping the export root (`Takeout/`): `("Google Fotos", +/// "/")`. /// /// `None` for a path with no service folder at all (the export's own /// `archive_browser.html`), which carries nothing to import. -fn strip_export_prefix(path: &str) -> Option<&str> { +fn strip_export_prefix(path: &str) -> Option<(&str, &str)> { let mut rest = path; - // The root is usually `Takeout/`, but an export unzipped and re-zipped by - // the user may not have it — so the root is dropped only when a service - // folder follows it, which the component count decides. + // The root is `Takeout/`, but an export unzipped and re-zipped by the user + // may not have it. `Google Photos//` then has three parts like + // `Takeout/Google Photos/`, so the count alone cannot tell them + // apart: a root of another name is only taken for one with four parts. + let (first, after) = rest.split_once('/')?; let components = rest.split('/').count(); - if components >= 3 { - rest = rest.split_once('/')?.1; + if (first.eq_ignore_ascii_case("takeout") && components >= 3) || components >= 4 { + rest = after; } - // Drop the service folder (`Google Photos`, `Google Fotos`, …). - let (_service, tail) = rest.split_once('/')?; - Some(tail) + // The service folder (`Google Photos`, `Google Fotos`, …). + rest.split_once('/') +} + +/// The key that pairs the still and the video of one live or motion photo, or +/// `None` when `file_name` cannot be either half. +/// +/// Apple writes `IMG_1.HEIC` (or `.JPG`) and `IMG_1.MOV`, which share the stem. +/// A Pixel writes `PXL_1.MP.jpg` and `PXL_1.MP`; there the still's stem is the +/// video's whole name. The folder is part of the key so two cameras' `IMG_1` +/// in different albums never pair. +fn live_key(folder: &str, file_name: &str) -> Option { + let lower = file_name.to_lowercase(); + let (stem, ext) = lower.rsplit_once('.')?; + let key = match ext { + "heic" | "heif" | "jpg" | "jpeg" => stem, + // Apple's video half. A plain `.mp4` beside a photo of the same name is + // far more often an unrelated clip than half of a live photo. + "mov" => stem, + "mp" => lower.as_str(), + _ => return None, + }; + (!key.is_empty()).then(|| format!("{folder}/{key}")) } /// Split `/` into its parts; the folder is `""` at the root. @@ -858,13 +1089,13 @@ mod tests { RawEntry { archive, entry: path.to_string(), - relative: strip_export_prefix(path).unwrap().to_string(), + relative: strip_export_prefix(path).unwrap().1.to_string(), size, } } fn sidecar(map: &mut HashMap>, path: &str, body: &str) { - let relative = strip_export_prefix(path).unwrap().to_string(); + let relative = strip_export_prefix(path).unwrap().1.to_string(); map.insert(relative, body.as_bytes().to_vec()); } @@ -872,12 +1103,17 @@ mod tests { fn strips_export_root_and_localized_service_folder() { assert_eq!( strip_export_prefix("Takeout/Google Fotos/Island 2019/IMG_1.jpg"), - Some("Island 2019/IMG_1.jpg") + Some(("Google Fotos", "Island 2019/IMG_1.jpg")) ); // Already unwrapped by the user: no `Takeout/` root left. assert_eq!( strip_export_prefix("Google Photos/IMG_1.jpg"), - Some("IMG_1.jpg") + Some(("Google Photos", "IMG_1.jpg")) + ); + // ... and an album keeps its folder. + assert_eq!( + strip_export_prefix("Google Photos/Iceland/IMG_1.jpg"), + Some(("Google Photos", "Iceland/IMG_1.jpg")) ); // Nothing below a service folder is nothing to import. assert_eq!(strip_export_prefix("archive_browser.html"), None); @@ -957,23 +1193,144 @@ mod tests { } #[test] - fn creation_time_is_the_fallback_capture_time() { + fn creation_time_is_kept_apart_as_the_last_resort() { + // `creationTime` is when the photo reached Google. It must not beat the + // date in the name, and the importer tries EXIF before it. let mut sidecars = HashMap::new(); sidecar( &mut sidecars, "Takeout/Google Photos/Photos from 2019/IMG_1.jpg.json", r#"{"creationTime":{"timestamp":"1500000000"}}"#, ); + sidecar( + &mut sidecars, + "Takeout/Google Photos/Photos from 2019/IMG_20190612_101500.jpg.json", + r#"{"creationTime":{"timestamp":"1700000000"}}"#, + ); let scan = assemble( - vec![entry( - 0, - "Takeout/Google Photos/Photos from 2019/IMG_1.jpg", - 10, - )], + vec![ + entry(0, "Takeout/Google Photos/Photos from 2019/IMG_1.jpg", 10), + entry( + 0, + "Takeout/Google Photos/Photos from 2019/IMG_20190612_101500.jpg", + 10, + ), + ], &sidecars, ); - assert_eq!(scan.photos[0].capture_time, Some(1_500_000_000)); + assert_eq!(scan.photos[0].capture_time, None); + assert_eq!(scan.photos[0].creation_time, Some(1_500_000_000)); + assert_eq!( + scan.photos[1].capture_time, + capture_time_from_name("IMG_20190612_101500.jpg") + ); + assert_eq!(scan.photos[1].creation_time, Some(1_700_000_000)); + } + + #[test] + fn sidecar_timestamps_tolerate_numbers_zero_and_null() { + let mut sidecars = HashMap::new(); + for (name, body) in [ + ("A.jpg", r#"{"photoTakenTime":{"timestamp":1560000000}}"#), + ( + "B.jpg", + r#"{"photoTakenTime":{"timestamp":"0"},"favorited":null}"#, + ), + ( + "C.jpg", + r#"{"photoTakenTime":{"timestamp":null},"geoData":null}"#, + ), + ] { + sidecar( + &mut sidecars, + &format!("Takeout/Google Photos/Photos from 2019/{name}.json"), + body, + ); + } + let scan = assemble( + ["A.jpg", "B.jpg", "C.jpg"] + .iter() + .map(|name| { + entry( + 0, + &format!("Takeout/Google Photos/Photos from 2019/{name}"), + 1, + ) + }) + .collect(), + &sidecars, + ); + + assert_eq!(scan.photos[0].capture_time, Some(1_560_000_000)); + assert_eq!(scan.photos[1].capture_time, None, "0 is not 1970"); + assert!(!scan.photos[1].favorite); + assert_eq!(scan.photos[2].capture_time, None); + } + + #[test] + fn location_comes_from_geo_data_and_zero_means_none() { + let mut sidecars = HashMap::new(); + sidecar( + &mut sidecars, + "Takeout/Google Photos/Iceland/IMG_1.jpg.json", + r#"{"geoData":{"latitude":64.1466,"longitude":-21.9426,"altitude":0.0}}"#, + ); + sidecar( + &mut sidecars, + "Takeout/Google Photos/Iceland/IMG_2.jpg.json", + r#"{"geoData":{"latitude":0.0,"longitude":0.0}, + "geoDataExif":{"latitude":63.4186,"longitude":-19.0060}}"#, + ); + sidecar( + &mut sidecars, + "Takeout/Google Photos/Iceland/IMG_3.jpg.json", + r#"{"geoData":{"latitude":0.0,"longitude":0.0}}"#, + ); + let scan = assemble( + (1..=3) + .map(|n| entry(0, &format!("Takeout/Google Photos/Iceland/IMG_{n}.jpg"), 1)) + .collect(), + &sidecars, + ); + + assert_eq!( + scan.photos[0].location, + Some(GeoPoint { + latitude: 64.1466, + longitude: -21.9426 + }) + ); + assert_eq!( + scan.photos[1].location.map(|p| p.latitude), + Some(63.4186), + "geoDataExif fills in for an empty geoData" + ); + assert_eq!(scan.photos[2].location, None); + } + + #[test] + fn live_and_motion_photo_halves_share_a_key() { + let scan = assemble( + vec![ + entry(0, "Takeout/Google Photos/Trip/IMG_1.HEIC", 1), + entry(0, "Takeout/Google Photos/Trip/IMG_1.MOV", 1), + entry(0, "Takeout/Google Photos/Trip/PXL_2.MP.jpg", 1), + entry(0, "Takeout/Google Photos/Trip/PXL_2.MP", 1), + entry(0, "Takeout/Google Photos/Other/IMG_1.MOV", 1), + entry(0, "Takeout/Google Photos/Trip/scan.png", 1), + ], + &HashMap::new(), + ); + let key = |i: usize| scan.photos[i].live_key.clone(); + + assert_eq!(key(0), key(1)); + assert!(!scan.photos[0].is_motion_photo()); + assert_eq!(key(2), key(3)); + assert!(scan.photos[3].is_motion_photo()); + assert!(scan.photos[3].is_video(), "the .MP half is a video"); + assert_ne!(key(1), key(4), "another folder's IMG_1 does not pair"); + assert_eq!(key(5), None); } #[test] @@ -1387,16 +1744,77 @@ mod tests { } #[test] - fn a_file_that_is_not_a_zip_is_a_readable_error() { - let path = + fn a_file_that_is_not_a_zip_is_left_out_and_the_rest_imports() { + let broken = std::env::temp_dir().join(format!("pdfs-takeout-not-a-zip-{}", std::process::id())); - std::fs::write(&path, b"not a zip at all").unwrap(); - let error = scan_archives(std::slice::from_ref(&path)).unwrap_err(); - let _ = std::fs::remove_file(&path); + std::fs::write(&broken, b"not a zip at all").unwrap(); + let good = write_zip( + "beside-broken", + &[("Takeout/Google Photos/Photos from 2019/IMG_1.jpg", b"x")], + ); + + let scan = scan_archives(&[broken.clone(), good.clone()]).unwrap(); + let _ = std::fs::remove_file(&broken); + let _ = std::fs::remove_file(&good); + + assert_eq!(scan.photos.len(), 1); + assert_eq!(scan.photos[0].archive, 1); + assert_eq!(scan.unreadable_archives.len(), 1); + assert_eq!(scan.unreadable_archives[0].path, broken); assert!( - error.message.contains("not a readable zip"), - "unexpected message: {}", - error.message + scan.unreadable_archives[0] + .reason + .contains("not a readable zip"), + "unexpected reason: {}", + scan.unreadable_archives[0].reason ); } + + #[test] + fn other_services_in_the_same_export_are_not_imported() { + let zip = write_zip( + "services", + &[ + ( + "Takeout/Google Fotos/Fotos von 2019/IMG_1.jpg", + b"x".as_slice(), + ), + ( + "Takeout/Google Fotos/Fotos von 2019/IMG_1.jpg.json", + br#"{"photoTakenTime":{"timestamp":"1560000000"}}"#.as_slice(), + ), + ("Takeout/Drive/My Drive/holiday.jpg", b"y".as_slice()), + ("Takeout/YouTube/videos/clip.mp4", b"z".as_slice()), + ], + ); + + let scan = scan_archives(std::slice::from_ref(&zip)).unwrap(); + let _ = std::fs::remove_file(&zip); + + assert_eq!(scan.photos.len(), 1); + assert_eq!(scan.photos[0].name, "IMG_1.jpg"); + assert_eq!(scan.skipped_other, 2); + } + + #[test] + fn an_export_without_sidecars_keeps_every_service() { + let zip = write_zip( + "no-sidecars", + &[("Takeout/Google Photos/Photos from 2019/IMG_1.jpg", b"x")], + ); + let scan = scan_archives(std::slice::from_ref(&zip)).unwrap(); + let _ = std::fs::remove_file(&zip); + assert_eq!(scan.photos.len(), 1); + } + + #[test] + fn a_stop_request_ends_the_scan() { + let zip = write_zip( + "stop", + &[("Takeout/Google Photos/Photos from 2019/IMG_1.jpg", b"x")], + ); + let result = scan_archives_until(std::slice::from_ref(&zip), || true); + let _ = std::fs::remove_file(&zip); + assert!(result.is_err()); + } } diff --git a/crates/pdfs-fuse/Cargo.toml b/crates/pdfs-fuse/Cargo.toml index 7a44d36..6a36a0e 100644 --- a/crates/pdfs-fuse/Cargo.toml +++ b/crates/pdfs-fuse/Cargo.toml @@ -20,9 +20,11 @@ tokio.workspace = true tracing.workspace = true fuser.workspace = true libc.workspace = true +serde.workspace = true serde_json.workspace = true base64.workspace = true image.workspace = true +lofty.workspace = true notify = "8" parking_lot.workspace = true diff --git a/crates/pdfs-fuse/src/background.rs b/crates/pdfs-fuse/src/background.rs index 9026a4c..9364472 100644 --- a/crates/pdfs-fuse/src/background.rs +++ b/crates/pdfs-fuse/src/background.rs @@ -157,6 +157,17 @@ fn apply_event(core: &Core, event: &DriveEvent, dirty: &mut DirtyParents) -> pdf if ours { debug!(uid = %node_uid, "ignoring remote event for a node with a queued write"); } + // A file kept offline keeps its copy through a change, and the + // keeper replaces it once this batch's listings are read again. + // The copy is checked against the revision's `(mtime, size)`, so + // no read takes it for a newer revision. Evicted, it was gone until + // something read the file, which then downloaded it (B282). A + // node new in, or moved into, a kept folder is fetched by checking + // that folder. + let kept = match !*is_trashed && !ours { + true => kept_by_pin(core, node_uid, parent_node_uid.as_ref()), + false => None, + }; let mut had_node = false; core.for_each_mount(|st, notify| { if *is_trashed { @@ -172,9 +183,12 @@ fn apply_event(core: &Core, event: &DriveEvent, dirty: &mut DirtyParents) -> pdf } }); // Once, not once per mount: the content cache is per-daemon. - if had_node { + if had_node && kept.is_none() { core.cache.evict(node_uid); } + if let Some(kept) = kept { + dirty.kept.insert(kept); + } // A create, rename or move-in shows up as a change to the parent // listing too, which is recorded for the end of the batch rather // than acted on here — see [`DirtyParents`]. @@ -205,6 +219,8 @@ fn apply_event(core: &Core, event: &DriveEvent, dirty: &mut DirtyParents) -> pdf | DriveEvent::ScopeAccessLost { .. } | DriveEvent::SharedWithMeUpdated { .. } => { warn!("event access continuity lost; downgrading shared trees and resyncing lazily"); + // Changes went by unseen, kept files among them (B282). + core.recheck_all_pins(); apply_result = apply_result.and(core.db.clear_state(SHARED_WITH_ME_SYNCED_MS)); core.for_each_mount(|st, notify| { let dirs: Vec = st.children.keys().copied().collect(); @@ -296,6 +312,21 @@ struct DirtyParents { /// Checked per mount — a mount that already lists every one of those /// children under that parent keeps its listing. ours: HashMap>, + /// Pinned files and folders the batch changed, for the pin keeper to + /// check once the listings above are dropped (`keeper`). + kept: HashSet, +} + +/// What the pin keeper has to check after a remote change to `node_uid`, +/// whose parent is now `parent_uid`: the node itself when a pin keeps it, or +/// else the parent, when a pinned folder holds it and the node is new there. +fn kept_by_pin(core: &Core, node_uid: &NodeUid, parent_uid: Option<&NodeUid>) -> Option { + if core.cache.is_pinned(node_uid) { + return Some(node_uid.clone()); + } + parent_uid + .filter(|parent| core.cache.is_pinned(parent)) + .cloned() } /// Whether this mount's cached listing of `parent_uid` already accounts for @@ -476,6 +507,9 @@ pub(super) async fn run_event_sync( cursor = Some(applied); } flush_dirty_parents(&core, &dirty); + for uid in &dirty.kept { + core.recheck_pinned(uid); + } // Another device changed the tree: whatever a front-end lists may have // moved, including what is shared and what is in the trash. core.events diff --git a/crates/pdfs-fuse/src/control.rs b/crates/pdfs-fuse/src/control.rs index b0ae3d7..0db3cf0 100644 --- a/crates/pdfs-fuse/src/control.rs +++ b/crates/pdfs-fuse/src/control.rs @@ -204,18 +204,42 @@ fn route_to_mount(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Cor /// B99), so resolving one needs this too. A mirror folder has no mount to run /// against: a file there is an ordinary local file. fn route_change(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Core, PathBuf)> { + route_within(core, mountpoint, path, |path| { + format!("{path} is in a mirrored folder; change the local file there instead") + }) +} + +/// [`route_to_mount`] for keeping a node offline or releasing it. A file in an +/// on-demand synced folder is a different Drive node from any file of the +/// same name under the primary mount, and keeping one offline did nothing for +/// the other: a program reading the synced folder's copy downloaded it +/// (`docs/BUGS.md` B281). A mirror folder already holds every file on disk. +fn route_keep(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Core, PathBuf)> { + route_within(core, mountpoint, path, |path| { + format!( + "{path} is in a mirrored folder, which keeps all its files on this computer already" + ) + }) +} + +/// [`route_to_mount`], refusing a path that leaves the mount it routed to and +/// answering a path in a mirror folder, which has no mount, with `mirrored`. +fn route_within( + core: &Core, + mountpoint: &Path, + path: &str, + mirrored: impl FnOnce(&str) -> String, +) -> CoreResult<(Core, PathBuf)> { let routed = route_to_mount(core, mountpoint, path).map_err(|e| { let p = Path::new(path); - let mirrored = core + let in_mirror = core .db .sync_folder_list() .unwrap_or_default() .iter() .any(|folder| folder.mode == "mirror" && p.starts_with(&folder.local_path)); - if mirrored { - CoreError::invalid(format!( - "{path} is in a mirrored folder; change the local file there instead" - )) + if in_mirror { + CoreError::invalid(mirrored(path)) } else { e } @@ -231,6 +255,18 @@ fn route_change(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Core, } } +/// The path the pin registry keeps for a pin made through `routed`: the +/// mount-relative path on the primary mount, as it always was, and the +/// absolute path the request named on an on-demand folder's mount, since +/// that node is not under the primary mountpoint (`docs/BUGS.md` B281). +fn pin_record(routed: &Core, rel: &Path, path: &str) -> PathBuf { + if routed.primary { + rel.to_path_buf() + } else { + Path::new(path).components().collect() + } +} + /// [`route_change`] for a request that trashes or renames the node it names. /// A mount's root is My files or a whole synced folder. An on-demand folder's /// root has a parent on Drive, so nothing further down would stop `pdfs rm @@ -521,8 +557,8 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U Ok(CtlRequest::Subscribe) => { CtlResponse::error(CoreError::invalid("a subscription is served on its own")) } - Ok(CtlRequest::Pin { path }) => match rel_to_mount(mountpoint, &path) { - Ok(rel) => match core.pin(&rel) { + Ok(CtlRequest::Pin { path }) => match route_keep(core, mountpoint, &path) { + Ok((routed, rel)) => match routed.pin(&rel, &pin_record(&routed, &rel, &path)) { Ok(name) => { core.touch_profile(); CtlResponse::Ok { @@ -533,8 +569,8 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U }, Err(e) => CtlResponse::error(e), }, - Ok(CtlRequest::Unpin { path }) => match rel_to_mount(mountpoint, &path) { - Ok(rel) => match core.unpin(&rel) { + Ok(CtlRequest::Unpin { path }) => match route_keep(core, mountpoint, &path) { + Ok((routed, rel)) => match routed.unpin(&rel) { Ok(name) => { core.touch_profile(); CtlResponse::Ok { @@ -841,35 +877,23 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U if archives.is_empty() { CtlResponse::error(CoreError::invalid("no Takeout archives given")) } else { - let paths: Vec = archives.into_iter().map(PathBuf::from).collect(); - let missing: Vec = paths - .iter() - .filter(|path| !path.is_file()) - .map(|path| path.display().to_string()) - .collect(); - if !missing.is_empty() { - CtlResponse::error(CoreError::not_found(format!( - "cannot read: {}", - missing.join(", ") - ))) - } else { - let count = paths.len(); - match core.spawn_takeout_import(paths, dry_run) { - Ok(()) => CtlResponse::Ok { - message: format!( - "{} {count} archive(s)", - if dry_run { "scanning" } else { "importing" } - ), - }, - Err(e) => CtlResponse::error(e), - } - } + let paths = archives.into_iter().map(PathBuf::from).collect(); + start_takeout_import(core, paths, dry_run) } } Ok(CtlRequest::ImportStatus) => { - let (running, summary) = crate::takeout::import_status(); - CtlResponse::ImportStatus { running, summary } + let report = crate::takeout::import_status(); + CtlResponse::ImportStatus { + running: report.running, + summary: report.summary, + progress: report.progress, + resumable: report.resumable, + } } + Ok(CtlRequest::ResumeImport) => match crate::takeout::resumable_archives() { + None => CtlResponse::error(CoreError::not_found("there is no import to resume")), + Some(paths) => start_takeout_import(core, paths, false), + }, Ok(CtlRequest::CancelImport) => { if crate::takeout::cancel_import() { CtlResponse::Ok { @@ -1296,11 +1320,9 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U core.cache.set_budget(bytes); // Persist so the next mount keeps the new cap. Best-effort: a config // write failure is reported but the live cap is already applied. - match AppDirs::new().map(|dirs| { - let mut cfg = dirs.load_config(); - cfg.cache_budget = Some(bytes); - dirs.save_config(&cfg) - }) { + match AppDirs::new() + .map(|dirs| dirs.update_config(|cfg| cfg.cache_budget = Some(bytes))) + { Ok(Ok(())) => { core.touch_profile(); CtlResponse::Ok { @@ -1320,10 +1342,10 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U // Persisted like the cache budget: best-effort, the live caps are // already applied. match AppDirs::new().map(|dirs| { - let mut cfg = dirs.load_config(); - cfg.upload_limit = Some(upload); - cfg.download_limit = Some(download); - dirs.save_config(&cfg) + dirs.update_config(|cfg| { + cfg.upload_limit = Some(upload); + cfg.download_limit = Some(download); + }) }) { Ok(Ok(())) => CtlResponse::Ok { message: format!( @@ -1973,6 +1995,31 @@ pub(crate) fn run_control_socket( } } +/// Check that every archive is there, then start the import in the background. +fn start_takeout_import(core: &Core, paths: Vec, dry_run: bool) -> CtlResponse { + let missing: Vec = paths + .iter() + .filter(|path| !path.is_file()) + .map(|path| path.display().to_string()) + .collect(); + if !missing.is_empty() { + return CtlResponse::error(CoreError::not_found(format!( + "cannot read: {}", + missing.join(", ") + ))); + } + let count = paths.len(); + match core.spawn_takeout_import(paths, dry_run) { + Ok(()) => CtlResponse::Ok { + message: format!( + "{} {count} archive(s)", + if dry_run { "scanning" } else { "importing" } + ), + }, + Err(e) => CtlResponse::error(e), + } +} + #[cfg(test)] mod request_limit_tests { use super::*; diff --git a/crates/pdfs-fuse/src/devices.rs b/crates/pdfs-fuse/src/devices.rs index 20b602e..1e3ef7e 100644 --- a/crates/pdfs-fuse/src/devices.rs +++ b/crates/pdfs-fuse/src/devices.rs @@ -123,9 +123,7 @@ pub(crate) fn sync_root_reserved() -> pdfs_core::syncroot::Reserved { pub(crate) fn set_adopted_device_uid(uid: Option<&str>) -> CoreResult<()> { let dirs = pdfs_core::config::AppDirs::new() .map_err(|e| CoreError::internal(format!("config dirs: {e}")))?; - let mut cfg = dirs.load_config(); - cfg.device_uid = uid.map(|u| u.to_string()); - dirs.save_config(&cfg) + dirs.update_config(|cfg| cfg.device_uid = uid.map(|u| u.to_string())) .map_err(|e| CoreError::internal(format!("write config: {e}"))) } diff --git a/crates/pdfs-fuse/src/keeper.rs b/crates/pdfs-fuse/src/keeper.rs new file mode 100644 index 0000000..5fa019f --- /dev/null +++ b/crates/pdfs-fuse/src/keeper.rs @@ -0,0 +1,271 @@ +//! Keeping pinned files on this computer after they were pinned. +//! +//! A pin downloads a file once. Nothing downloaded it again afterwards: a +//! change from another device deleted the cached copy, a file added to a +//! pinned folder elsewhere was never fetched, and a download that failed while +//! the link was down stayed failed. Each of those left a file listed as kept +//! offline that the next read downloaded, and that could not be read at all +//! without a network (`docs/BUGS.md` B282). +//! +//! The keeper is one thread that checks pins again and downloads what is +//! missing or stale: every pin when the daemon starts and when the link comes +//! back, and the pins a batch of remote events touched once the batch is +//! applied. A remote change no longer deletes a pinned file's copy. The copy +//! is stored under the revision's `(mtime, size)`, so a read of a newer +//! revision does not take it, and the keeper replaces it once the new one is +//! downloaded. + +use std::collections::HashSet; +use std::time::{Duration, Instant}; + +use proton_drive_rs::NodeKind; +use proton_drive_rs::proton_sdk::ids::NodeUid; +use tracing::{debug, info, warn}; + +use super::{Core, is_local_uid, node_size, parse_uid}; + +/// How often the keeper looks for work. +const KEEPER_TICK: Duration = Duration::from_secs(1); +/// The first wait before a check that could not download everything is tried +/// again. It doubles on every failure, up to [`RETRY_MAX`]. +const RETRY_FIRST: Duration = Duration::from_secs(30); +const RETRY_MAX: Duration = Duration::from_secs(30 * 60); + +/// What the keeper owes a check. +#[derive(Debug, Default)] +pub(crate) struct PinWork { + /// Every pin. + all: bool, + /// The pinned files and folders named here. A folder is checked whole. + nodes: HashSet, +} + +impl PinWork { + fn is_empty(&self) -> bool { + !self.all && self.nodes.is_empty() + } + + fn merge(&mut self, other: PinWork) { + self.all |= other.all; + self.nodes.extend(other.nodes); + } +} + +/// A file a pin keeps, as the tree knows it now. +pub(crate) struct KeptFile { + pub(crate) uid: NodeUid, + pub(crate) name: String, + pub(crate) mtime: i64, + pub(crate) size: u64, +} + +impl Core { + /// Have the keeper check every pin. + pub(crate) fn recheck_all_pins(&self) { + self.pin_work.lock().all = true; + } + + /// Have the keeper check the pinned file or folder `uid`. + pub(crate) fn recheck_pinned(&self, uid: &NodeUid) { + self.pin_work.lock().nodes.insert(uid.clone()); + } + + /// The keeper's loop, until the daemon stops. Starts with a check of every + /// pin, which catches up on whatever changed while the daemon was down. + /// + /// Work that failed waits out a backoff of its own. New work does not wait + /// for it, so a file that keeps failing does not hold up the next change. + pub(crate) fn run_pin_keeper(&self) { + self.recheck_all_pins(); + let mut backlog = PinWork::default(); + let mut backlog_due = Instant::now(); + let mut retry = RETRY_FIRST; + while self.shutdown.sleep(KEEPER_TICK) { + if !self.is_online() { + continue; + } + let mut work = std::mem::take(&mut *self.pin_work.lock()); + let retrying = !backlog.is_empty() && Instant::now() >= backlog_due; + if retrying { + work.merge(std::mem::take(&mut backlog)); + } + if work.is_empty() { + continue; + } + let failed = self.keep_pins(&work); + if failed.is_empty() { + if retrying { + retry = RETRY_FIRST; + } + continue; + } + backlog.merge(failed); + backlog_due = Instant::now() + retry; + retry = (retry * 2).min(RETRY_MAX); + } + } + + /// Download every file `work` covers whose cached copy is missing or + /// stale. Returns what could not be checked or downloaded. + fn keep_pins(&self, work: &PinWork) -> PinWork { + let mut failed = PinWork::default(); + let roots: Vec = if work.all { + self.cache + .list_pins() + .iter() + .filter_map(|pin| parse_uid(&pin.uid)) + .chain(work.nodes.iter().cloned()) + .collect() + } else { + work.nodes.iter().cloned().collect() + }; + let mut seen: HashSet = HashSet::new(); + let mut files: Vec = Vec::new(); + for uid in roots { + if self.shutdown.is_stopping() { + return failed; + } + if !self.cache.is_pinned(&uid) { + continue; + } + let found = { + let st = self.state(); + st.by_uid.get(&uid).and_then(|&ino| { + let e = st.entries.get(&ino)?; + Some((ino, e.parent, e.node.is_folder())) + }) + }; + let Some((ino, parent, is_folder)) = found else { + debug!(%uid, "pin keeper: pinned node is not in the tree"); + continue; + }; + if is_folder { + match self.pin_subtree_files(ino) { + Ok(inos) => files.extend(inos.into_iter().filter(|i| seen.insert(*i))), + Err(e) => { + warn!(%uid, error = %e, "pin keeper: listing a kept folder failed"); + failed.nodes.insert(uid); + } + } + continue; + } + // An event about this file dropped its parent's listing. Listing + // it again brings the file's entry up to the revision Drive has + // now, which is what the cached copy is checked against. + if !work.all + && parent != ino + && let Err(e) = self.ensure_children(parent) + { + warn!(%uid, error = ?e, "pin keeper: listing a kept file's folder failed"); + failed.nodes.insert(uid); + continue; + } + if seen.insert(ino) { + files.push(ino); + } + } + + let stale: Vec = files + .into_iter() + .filter_map(|ino| self.kept_file(ino)) + .filter(|f| !self.cache.is_cached(&f.uid, f.mtime, f.size)) + .collect(); + if stale.is_empty() { + return failed; + } + let job = self.transfers.begin_job("Updating files kept offline"); + job.set_total(stale.len() as u64); + let (mut fetched, mut failures) = (0usize, 0usize); + for file in stale { + if self.shutdown.is_stopping() { + break; + } + // Unpinned while the check ran. + if !self.cache.is_pinned(&file.uid) { + job.step(); + continue; + } + job.detail(file.name.clone()); + match self.fetch_pinned(&file.uid, &file.name, file.mtime, file.size) { + Ok(()) => fetched += 1, + Err(e) => { + warn!(uid = %file.uid, error = %e, "pin keeper: download failed"); + failed.nodes.insert(file.uid); + failures += 1; + } + } + job.step(); + } + info!( + fetched, + failed = failures, + "pin keeper: brought files kept offline up to date" + ); + failed + } + + /// The file at `ino`, as a pin keeps it. `None` for a folder, a file this + /// daemon still owes an upload for, or a file whose real size cannot be + /// read. + /// + /// A queued write is ahead of Drive. Downloading Drive's revision would + /// cache what the write replaces, and the drain keeps the uploaded bytes + /// of a pinned file once they land. + /// + /// A listing carries the ciphertext size until its size upgrade lands + /// (B12). Stored under that size, the copy never matched the plaintext it + /// holds, so every read downloaded the file again and every check + /// downloaded it once more. + pub(crate) fn kept_file(&self, ino: u64) -> Option { + let (uid, name, mtime, size, provisional) = { + let st = self.state(); + let e = st.entries.get(&ino)?; + let NodeKind::File { claimed_size, .. } = &e.node.kind else { + return None; + }; + ( + e.uid.clone(), + e.node.name.clone(), + e.node.modification_time, + node_size(&e.node), + claimed_size.is_none(), + ) + }; + if is_local_uid(&uid) || self.pending.lock().contains_key(&uid) { + return None; + } + let size = match provisional { + true => self.resolve_provisional_size(ino, &uid)?, + false => size, + }; + Some(KeptFile { + uid, + name, + mtime, + size, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn work_that_failed_keeps_every_node_and_the_full_check() { + let mut backlog = PinWork::default(); + assert!(backlog.is_empty()); + let uid = parse_uid("vol~a").unwrap(); + backlog.merge(PinWork { + all: false, + nodes: HashSet::from([uid.clone()]), + }); + backlog.merge(PinWork { + all: true, + nodes: HashSet::from([uid.clone()]), + }); + assert!(backlog.all); + assert_eq!(backlog.nodes.len(), 1); + assert!(!backlog.is_empty()); + } +} diff --git a/crates/pdfs-fuse/src/lib.rs b/crates/pdfs-fuse/src/lib.rs index c10e354..73c0f10 100644 --- a/crates/pdfs-fuse/src/lib.rs +++ b/crates/pdfs-fuse/src/lib.rs @@ -91,6 +91,7 @@ mod drive; use drive::{DriveApi, RevisionRead}; mod events; mod filesystem; +mod keeper; mod link; pub use filesystem::ProtonFs; mod mount; @@ -589,6 +590,8 @@ struct Core { /// Nudges the drain workers: set true and notify to have them re-examine /// the queue instead of waiting out their backoff. drain_wake: Arc<(Mutex, Condvar)>, + /// The pins the keeper owes a check (`keeper`). + pin_work: Arc>, /// The daemon's stop signal, shared with every background loop this `Core` /// was cloned into so teardown can end and join them (`docs/BUGS.md` B44). shutdown: Arc, @@ -3165,6 +3168,28 @@ impl Core { ) } + /// [`Core::resolve_anywhere`], widened to the Photos timeline for sharing. + /// + /// A photo lives on the photos volume, which no location shows, so the + /// residency proof above never finds it. The timeline in the `photos` table + /// is this daemon's proof instead: it lists only photos on the user's own + /// volume, which are theirs to share. A photo in an album someone else + /// shared is not in it and stays refused. + pub(crate) fn resolve_shareable(&self, uid: &str) -> CoreResult { + resolve_anywhere_with( + uid, + |uid| self.owns_visible_uid(uid), + |uid| { + let uid = uid.to_string(); + Ok(self.db.mirror_contains_uid(&uid)? + || !self + .db + .photos_by_uid(std::slice::from_ref(&uid))? + .is_empty()) + }, + ) + } + fn source_parent_uid(&self, ino: u64, rel: &Path) -> CoreResult { let state = self.state(); let entry = state @@ -4270,7 +4295,12 @@ impl Core { /// recursive pin and downloads every descendant file on a background thread, /// reported as a job: a big folder far outlasts the socket read timeout. /// Returns a human message. - fn pin(&self, rel: &Path) -> CoreResult { + /// + /// `recorded` is the path the pin registry keeps for the front ends to + /// open the pin by: `rel` itself on the primary mount, which they join to + /// the mountpoint, and the absolute local path on an on-demand folder's + /// mount, whose node is not reachable under the primary mountpoint at all. + fn pin(&self, rel: &Path, recorded: &Path) -> CoreResult { let (ino, uid) = self.resolve(rel)?; let (name, is_folder, mtime, size) = { let st = self.state(); @@ -4294,7 +4324,7 @@ impl Core { // Record the recursive pin first so every descendant is eviction- // exempt before we start filling the cache with the subtree. self.cache - .add_pin(&uid, rel, true) + .add_pin(&uid, recorded, true) .map_err(|e| CoreError::from_api(&e, "pin"))?; let core = self.clone(); let folder = name.clone(); @@ -4304,9 +4334,15 @@ impl Core { .map_err(|e| CoreError::internal(format!("spawn pin: {e}")))?; return Ok(format!("{name}: downloading its files in the background")); } + // Stored under a provisional size, the copy would never match the + // plaintext it holds (`docs/BUGS.md` B282). + let size = match self.kept_file(ino) { + Some(file) if file.uid == uid => file.size, + _ => size, + }; self.fetch_content(&uid, &name, mtime, size)?; self.cache - .add_pin(&uid, rel, false) + .add_pin(&uid, recorded, false) .map_err(|e| CoreError::from_api(&e, "pin"))?; Ok(name) } @@ -4324,14 +4360,20 @@ impl Core { Ok(files) => { job.set_total(files.len() as u64); let mut failed = 0usize; - for (uid, file, mtime, size) in files { + for ino in files { if self.shutdown.is_stopping() || !self.cache.is_pinned(folder) { return; } - job.detail(file.clone()); - if let Err(e) = self.fetch_pinned(&uid, &file, mtime, size) { - warn!(%uid, error = %e, "pin folder: download failed"); - failed += 1; + // A file with a queued write, or no readable size, is + // left to the drain and the keeper (`keeper::kept_file`). + if let Some(file) = self.kept_file(ino) { + job.detail(file.name.clone()); + if let Err(e) = + self.fetch_pinned(&file.uid, &file.name, file.mtime, file.size) + { + warn!(uid = %file.uid, error = %e, "pin folder: download failed"); + failed += 1; + } } job.step(); } @@ -4365,9 +4407,9 @@ impl Core { Ok(()) } - /// Every file in the subtree rooted at folder `ino`. - fn pin_subtree_files(&self, ino: u64) -> CoreResult> { - let mut files: Vec<(NodeUid, String, i64, u64)> = Vec::new(); + /// The inode of every file in the subtree rooted at folder `ino`. + fn pin_subtree_files(&self, ino: u64) -> CoreResult> { + let mut files: Vec = Vec::new(); let mut stack = vec![ino]; while let Some(dir) = stack.pop() { if self.shutdown.is_stopping() { @@ -4382,12 +4424,7 @@ impl Core { if e.node.is_folder() { stack.push(k); } else { - files.push(( - e.uid.clone(), - e.node.name.clone(), - e.node.modification_time, - node_size(&e.node), - )); + files.push(k); } } } diff --git a/crates/pdfs-fuse/src/link.rs b/crates/pdfs-fuse/src/link.rs index 3430235..517d175 100644 --- a/crates/pdfs-fuse/src/link.rs +++ b/crates/pdfs-fuse/src/link.rs @@ -286,6 +286,9 @@ impl Core { warn!(error = %e, "moving backed-off ops up after a reconnect failed"); } self.wake_drain(); + // Downloads for pins failed while the link was down, and the events + // that would have named the files are behind us (`docs/BUGS.md` B282). + self.recheck_all_pins(); } /// Wait up to `budget` for the link to come back. Returns whether it is up. diff --git a/crates/pdfs-fuse/src/mount.rs b/crates/pdfs-fuse/src/mount.rs index 001f0df..1051512 100644 --- a/crates/pdfs-fuse/src/mount.rs +++ b/crates/pdfs-fuse/src/mount.rs @@ -511,6 +511,7 @@ pub(crate) fn mount_with( landed: Arc::new(Mutex::new(HashMap::new())), unadopted: Arc::new(Mutex::new(HashMap::new())), drain_wake: Arc::new((Mutex::new(false), Condvar::new())), + pin_work: Arc::new(Mutex::new(Default::default())), shutdown: Arc::new(crate::shutdown::Shutdown::default()), upload_times: Arc::new(Mutex::new(HashMap::new())), upload_cancel: Arc::new(Mutex::new(HashMap::new())), @@ -621,6 +622,17 @@ pub(crate) fn mount_with( keep_or_abandon(&core, control_socket, &mut workers, spawned)?; } + // Keep pinned files on this computer through changes made elsewhere, and + // fetch the ones a failed or missed download left out (`docs/BUGS.md` + // B282). Its own thread: a check lists folders and downloads files. + { + let keeper_core = core.clone(); + let spawned = std::thread::Builder::new() + .name("pdfs-pin-keeper".into()) + .spawn(move || keeper_core.run_pin_keeper()); + keep_or_abandon(&core, control_socket, &mut workers, spawned)?; + } + // Keep the launcher prompt's "This computer" index fresh. Its own thread: // the walk is I/O-heavy and must never sit in front of a FUSE callback. if matches!(host, Host::Daemon) { diff --git a/crates/pdfs-fuse/src/photos.rs b/crates/pdfs-fuse/src/photos.rs index 4e7b36b..9d055d3 100644 --- a/crates/pdfs-fuse/src/photos.rs +++ b/crates/pdfs-fuse/src/photos.rs @@ -19,7 +19,7 @@ use std::time::{Duration, Instant}; use base64::Engine as _; use pdfs_core::control::{ FileThumbRequest, MappingProgress, PhotoItem, PhotoKind, PhotoLocation, PhotoThumb, PlaceInfo, - ThumbnailBuildStatus, Topic, is_raw_image_name, is_thumbnail_image_name, + ThumbnailBuildStatus, Topic, is_audio_file_name, is_raw_image_name, is_thumbnail_image_name, is_thumbnail_media_name, }; use pdfs_core::db::{self, StoredPhoto}; @@ -76,6 +76,36 @@ const VIDEO_BOX_WALK_MAX: usize = 32; /// A single frame is seconds of work even for 4K; this only stops a wedged /// ffmpeg from holding a thumbnail permit. const FFMPEG_TIMEOUT: Duration = Duration::from_secs(30); +/// A song no larger than this is downloaded whole for its cover. +const AUDIO_WHOLE_MAX_BYTES: u64 = 4 * 1024 * 1024; +/// How much of a song's start is read first, to learn where its tags end. +const AUDIO_PROBE_BYTES: u64 = 64 * 1024; +/// How much of a song's start is read when its first bytes don't say where its +/// tags end: an Ogg comment packet, which holds the cover, has no size up front. +const AUDIO_HEAD_BYTES: u64 = 4 * 1024 * 1024; +/// Read past the tags at a song's start, so the first audio frames are there +/// for a reader that looks for them. +const AUDIO_HEAD_SLACK: u64 = 64 * 1024; +/// Largest start of a song read for its tags. A cover bigger than this is not +/// worth the download for a 512-pixel thumbnail. +const AUDIO_TAG_MAX_BYTES: u64 = 16 * 1024 * 1024; +/// How much of a song's end is read in formats that keep their tags there +/// (APE tags in WavPack and Monkey's Audio, `id3 ` chunks in WAV and AIFF). +const AUDIO_TAIL_BYTES: u64 = 4 * 1024 * 1024; +/// Names of a folder's cover image, best first, matched without case and +/// extension. A song without a picture in its tags gets this image's. +const FOLDER_COVER_STEMS: [&str; 6] = [ + "cover", + "folder", + "front", + "album", + "albumart", + "albumartsmall", +]; +/// Image types a folder cover is taken in. +const FOLDER_COVER_EXTENSIONS: [&str; 4] = ["jpg", "jpeg", "png", "webp"]; +/// Largest folder cover image downloaded whole for a song's thumbnail. +const FOLDER_COVER_MAX_BYTES: u64 = 32 * 1024 * 1024; /// Largest thumbnail attached to an upload. Proton's own clients hold theirs to /// this, stepping the JPEG quality down until it fits. const UPLOAD_THUMB_MAX_BYTES: usize = 60 * 1024; @@ -630,6 +660,7 @@ enum Fetched { /// A whole image. Whole(Vec), Video(VideoSample), + Audio(AudioSample), } /// The parts of a video its thumbnail needs, downloaded: the start of the @@ -766,13 +797,181 @@ fn video_sample_thumbnail( video_file_thumbnail(&file.0, ffmpeg, timeout) } +/// The parts of a song its cover needs, downloaded: the start, where most +/// formats keep their tags, an M4A's index, and for formats that keep their +/// tags at the end, the end. +struct AudioSample { + /// The whole file's length. + size: u64, + /// Each part and the offset it came from. + parts: Vec<(u64, Vec)>, +} + +/// Where the tags at the start of a song end, read from its first `probe` +/// bytes: past an ID3v2 tag, and past FLAC's metadata blocks, which hold its +/// picture. `None` when the start says nothing, as for Ogg. A FLAC block +/// header past `probe` stops the walk with a guess of [`AUDIO_HEAD_BYTES`] +/// more. +fn audio_tags_end(probe: &[u8]) -> Option { + let mut offset = 0u64; + let mut known = false; + if probe.get(0..3) == Some(b"ID3") { + let size = probe.get(6..10)?; + if size.iter().any(|byte| byte & 0x80 != 0) { + return None; + } + let size = size + .iter() + .fold(0u64, |acc, byte| (acc << 7) | u64::from(*byte)); + let footer = probe.get(5).is_some_and(|flags| flags & 0x10 != 0); + offset = 10 + size + if footer { 10 } else { 0 }; + known = true; + } + let start = usize::try_from(offset).ok()?; + if probe.get(start..start + 4) == Some(b"fLaC") { + offset += 4; + loop { + let at = usize::try_from(offset).ok()?; + let Some(header) = probe.get(at..at + 4) else { + return Some(offset + AUDIO_HEAD_BYTES); + }; + let len = u64::from_be_bytes([0, 0, 0, 0, 0, header[1], header[2], header[3]]); + offset += 4 + len; + if header[0] & 0x80 != 0 { + return Some(offset); + } + } + } + known.then_some(offset) +} + +/// Whether a song in format `name` may keep its tags at its end. +fn audio_tags_at_end(name: &str) -> bool { + let extension = name + .rsplit_once('.') + .map(|(_, extension)| extension.to_ascii_lowercase()) + .unwrap_or_default(); + matches!(extension.as_str(), "wv" | "ape" | "wav" | "aif" | "aiff") +} + +/// Download what the cover of the song `reader` reads, named `name`, needs. A +/// song of up to [`AUDIO_WHOLE_MAX_BYTES`] comes whole; a larger one brings its +/// tags, never its audio. +async fn sample_audio(reader: &dyn RevisionRead, name: &str) -> DriveResult { + let size = reader.size(); + if size <= AUDIO_WHOLE_MAX_BYTES { + let whole = reader.read_at(0, size).await?; + return Ok(AudioSample { + size, + parts: vec![(0, whole)], + }); + } + let probe = reader.read_at(0, AUDIO_PROBE_BYTES.min(size)).await?; + // An M4A keeps its cover in its index, fetched below wherever it sits. + let iso_media = probe.get(4..8) == Some(b"ftyp"); + let want = match audio_tags_end(&probe) { + _ if iso_media => 0, + Some(end) => end.saturating_add(AUDIO_HEAD_SLACK), + None => AUDIO_HEAD_BYTES, + } + .min(AUDIO_TAG_MAX_BYTES) + .min(size); + let head = if want > probe.len() as u64 { + reader.read_at(0, want).await? + } else { + probe + }; + let mut parts = Vec::new(); + match iso_media_index(reader, &head, size).await? { + Some((offset, len)) if offset.saturating_add(len) > head.len() as u64 => { + parts.push((offset, reader.read_at(offset, len).await?)); + } + _ => {} + } + let head_len = head.len() as u64; + parts.push((0, head)); + if audio_tags_at_end(name) { + let start = size.saturating_sub(AUDIO_TAIL_BYTES).max(head_len); + if start < size { + parts.push((start, reader.read_at(start, size - start).await?)); + } + } + Ok(AudioSample { size, parts }) +} + +/// The picture a song's tags carry: the front cover, or else the first one. +/// `None` for a song without one, or one lofty can't read. +fn embedded_cover(path: &Path) -> Option> { + use lofty::config::{ParseOptions, ParsingMode}; + use lofty::file::TaggedFileExt as _; + use lofty::picture::PictureType; + + let tagged = lofty::probe::Probe::open(path) + .ok()? + .options( + ParseOptions::new() + .read_properties(false) + .parsing_mode(ParsingMode::Relaxed), + ) + .guess_file_type() + .ok()? + .read() + .ok()?; + let pictures = || tagged.tags().iter().flat_map(|tag| tag.pictures()); + pictures() + .find(|picture| picture.pic_type() == PictureType::CoverFront) + .or_else(|| pictures().next()) + .map(|picture| picture.data().to_vec()) + .filter(|data| !data.is_empty()) +} + +/// A thumbnail from the cover in a downloaded [`AudioSample`]'s tags. The +/// sample is laid out in a staging file at the offsets it came from, so the +/// tags are where the file's own headers say they are; the audio between +/// holds no data and costs no disk. A song without a cover, or with one the +/// image decoder can't read, is [`ScaleAttempt::Undecodable`]. +fn audio_cover_thumbnail(sample: &AudioSample, name: &str, staging_dir: &Path) -> ScaleAttempt { + let Some(file) = RawTempFile::create_in(staging_dir, &[], name) else { + return ScaleAttempt::Unavailable; + }; + let laid_out = OpenOptions::new() + .write(true) + .open(&file.0) + .and_then(|staged| { + for (offset, bytes) in &sample.parts { + staged.write_all_at(bytes, *offset)?; + } + staged.set_len(sample.size) + }); + if laid_out.is_err() { + return ScaleAttempt::Unavailable; + } + match embedded_cover(&file.0) { + Some(picture) => scale_thumbnail(&picture, "cover", staging_dir), + None => ScaleAttempt::Undecodable, + } +} + +/// How good a folder cover `name` is: its place in [`FOLDER_COVER_STEMS`], +/// lowest best, or `None` for a file that is no cover image. +fn cover_rank(name: &str) -> Option { + let (stem, extension) = name.rsplit_once('.')?; + let extension = extension.to_ascii_lowercase(); + if !FOLDER_COVER_EXTENSIONS.contains(&extension.as_str()) { + return None; + } + let stem = stem.to_ascii_lowercase(); + FOLDER_COVER_STEMS.iter().position(|known| *known == stem) +} + /// The thumbnail to attach to an upload of the local file at `path`, which is /// named `name` in Drive. Without one, Drive's own apps show a generic icon and /// the gallery has to download the whole file to make its own. /// -/// Best effort: a file that isn't an image or a video, is too large to read, or -/// can't be decoded uploads without a thumbnail, as it always did. Blocking and -/// CPU-bound, so async callers run it on the blocking pool. +/// Best effort: a file that isn't an image, a video or a song with a cover in +/// its tags, is too large to read, or can't be decoded uploads without a +/// thumbnail, as it always did. Blocking and CPU-bound, so async callers run it +/// on the blocking pool. pub(crate) fn upload_thumbnails(path: &Path, name: &str, staging_dir: &Path) -> Vec { if is_video_name(name) && !ffmpeg_available() { return Vec::new(); @@ -806,6 +1005,14 @@ fn upload_thumbnails_with( return Vec::new(); }; scale_thumbnail_unbudgeted(&bytes, name, staging_dir) + } else if is_audio_file_name(name) { + // Only the song's own picture: a cover image beside it is not known + // here, where `path` may be a staging copy rather than the song's + // folder. The daemon finds that one when a listing asks. + match embedded_cover(path) { + Some(picture) => scale_thumbnail(&picture, "cover", staging_dir), + None => return Vec::new(), + } } else { return Vec::new(); }; @@ -986,7 +1193,7 @@ impl Core { if known_empty && !self.listing_stale(PHOTOS_SYNCED_MS, TIMELINE_TTL) { return Ok(None); } - if !self.rt.block_on(self.refresh_timeline())? { + if !self.refresh_timeline_joined()? { return Ok(None); } } else if self.listing_stale(PHOTOS_SYNCED_MS, TIMELINE_TTL) { @@ -1027,6 +1234,7 @@ impl Core { favorite: photo.favorite, group_size: photo.group_size as u32, has_raw: photo.has_raw, + place_id: photo.place_id, } } @@ -1702,6 +1910,7 @@ impl Core { return (uid, ThumbAttempt::Cancelled); } let video = is_video_name(&name); + let audio = is_audio_file_name(&name); // Without ffmpeg a video can still have its uploader's // thumbnail, but Photos has asked the server already. let can_sample = !video || ffmpeg_available(); @@ -1756,6 +1965,7 @@ impl Core { let download_uid = uid.clone(); let drive = core.drive.clone(); + let audio_name = name.clone(); let download = async move { // Photos asked the server in one batch already. A // file view's or a build's file may carry a @@ -1775,6 +1985,11 @@ impl Core { if video { let reader = drive.open_revision(&download_uid).await?; sample_video(reader.as_ref()).await.map(Fetched::Video) + } else if audio { + let reader = drive.open_revision(&download_uid).await?; + sample_audio(reader.as_ref(), &audio_name) + .await + .map(Fetched::Audio) } else { client .download_file(&download_uid) @@ -1832,7 +2047,11 @@ impl Core { // Decoding + scaling a 20 MP JPEG is CPU-bound and would // stall the runtime's worker; hand it to the blocking pool. let staging_dir = core.cache.raw_thumbnail_staging_dir().to_path_buf(); + let staging = staging_dir.clone(); let made = tokio::task::spawn_blocking(move || match fetched { + Fetched::Audio(sample) => { + audio_cover_thumbnail(&sample, &name, &staging_dir) + } Fetched::Video(sample) => video_sample_thumbnail( &sample, &name, @@ -1849,6 +2068,14 @@ impl Core { }) .await .unwrap_or(ScaleAttempt::Unavailable); + // A song without a picture of its own shows its + // folder's cover. + let made = match made { + ScaleAttempt::Undecodable if audio && core.thumb_job_current(job) => { + core.folder_cover_thumbnail(&uid, staging).await + } + made => made, + }; if !core.thumb_job_current(job) { return (uid, ThumbAttempt::Cancelled); } @@ -1909,9 +2136,85 @@ impl Core { ThumbAttempt::Cancelled => summary.cancelled += 1, } } + // A folder on screen whose tiles were given up on, as a recursive + // build's start does to them, fills in without being opened again. + if summary.made > 0 { + self.events.publish(&[Topic::Thumbnails]); + } summary } + /// The cover image in the folder of the song `uid`: the best-named one of + /// [`FOLDER_COVER_STEMS`] among its siblings the database knows. + fn folder_cover(&self, uid: &NodeUid) -> Option { + let node = self.db.node_by_uid(&uid.to_string()).ok()??; + let parent = node.parent_uid?; + let siblings = match self.db.children_if_listed(&parent) { + Ok(Some(children)) => children, + _ => self.db.known_children(&parent).ok()?, + }; + siblings + .into_iter() + .filter(|sibling| !sibling.is_folder() && !sibling.trashed) + .filter_map(|sibling| cover_rank(&sibling.name).map(|rank| (rank, sibling))) + .min_by(|(a, x), (b, y)| a.cmp(b).then_with(|| x.name.cmp(&y.name))) + .map(|(_, cover)| cover) + } + + /// A thumbnail of the song `uid` made from its folder's cover image: the + /// cover's own cached thumbnail when it has one, else the thumbnail its + /// uploader attached, else the image itself, up to + /// [`FOLDER_COVER_MAX_BYTES`]. + /// + /// The result is stored under the song, so a cover added to the folder or + /// replaced later shows only once the song changes or the daemon restarts + /// and forgets the songs it found no cover for. + async fn folder_cover_thumbnail(&self, uid: &NodeUid, staging_dir: PathBuf) -> ScaleAttempt { + let Some(cover) = self.folder_cover(uid) else { + return ScaleAttempt::Undecodable; + }; + let ttype = ThumbnailType::Thumbnail; + let cached = self + .cache + .cached_thumbnail_path(&cover.uid, ttype.as_i32(), cover.modification_time) + .and_then(|path| std::fs::read(path).ok()); + let ready = match cached { + Some(bytes) => Some(bytes), + None => match self.drive.download_thumbnail(&cover.uid, ttype).await { + Ok(bytes) => bytes.filter(|bytes| !bytes.is_empty()), + Err(e) => { + warn!(cover = %cover.uid, error = %e, "folder cover thumbnail download failed"); + return ScaleAttempt::Unavailable; + } + }, + }; + if let Some(bytes) = ready { + return match ratio_of(&bytes) { + Some(ratio) => ScaleAttempt::Made(GeneratedThumb { bytes, ratio }), + None => ScaleAttempt::Undecodable, + }; + } + let read = async { + let reader = self.drive.open_revision(&cover.uid).await?; + if reader.size() > FOLDER_COVER_MAX_BYTES { + return Ok(None); + } + reader.read_at(0, reader.size()).await.map(Some) + }; + let bytes = match read.await { + Ok(Some(bytes)) => bytes, + Ok(None) => return ScaleAttempt::Undecodable, + Err(e) => { + warn!(cover = %cover.uid, error = %e, "folder cover download failed"); + return ScaleAttempt::Unavailable; + } + }; + let name = cover.name; + tokio::task::spawn_blocking(move || scale_thumbnail(&bytes, &name, &staging_dir)) + .await + .unwrap_or(ScaleAttempt::Unavailable) + } + pub(crate) fn thumbnail_build_status(&self) -> ThumbnailBuildStatus { self.thumbnail_build.lock().clone() } @@ -2337,6 +2640,36 @@ impl Core { /// Refresh the timeline off the request path, so a stale page is still served /// at DB speed. At most one refresh runs at a time. + /// Refresh the timeline and wait for it, sharing a refresh already under + /// way instead of starting a second one beside it. + /// + /// The first request after an empty cache has nothing to serve until the + /// fetch lands, so it waits. Two such requests (the gallery and its retry, + /// or a background refresh started by an import) used to each fetch the + /// whole library at once, sharing the session's rate limit and taking + /// minutes. Returns whether the account has a photos library. + pub(crate) fn refresh_timeline_joined(&self) -> CoreResult { + if self.timeline_refreshing.swap(true, Ordering::SeqCst) { + while self.timeline_refreshing.load(Ordering::SeqCst) && !self.shutdown.is_stopping() { + std::thread::sleep(std::time::Duration::from_millis(200)); + } + // A refresh that landed stamped the timeline fresh. One that failed + // did not, and the caller gets this refresh's own error instead of + // an empty timeline. + if !self.listing_stale(PHOTOS_SYNCED_MS, TIMELINE_TTL) + || self.timeline_refreshing.swap(true, Ordering::SeqCst) + { + return Ok(self.db.state_i64(PHOTOS_AVAILABLE).ok().flatten() != Some(0)); + } + } + let refreshed = self.rt.block_on(self.refresh_timeline()); + self.timeline_refreshing.store(false, Ordering::SeqCst); + if refreshed.is_ok() { + self.events.publish(&[Topic::Photos]); + } + refreshed + } + pub(crate) fn spawn_timeline_refresh(&self) { if self.timeline_refreshing.swap(true, Ordering::SeqCst) { return; @@ -2453,6 +2786,10 @@ mod decode_budget_tests { #[cfg(test)] mod thumb_tests { + use super::{ + AUDIO_HEAD_BYTES, AUDIO_TAIL_BYTES, AudioSample, audio_cover_thumbnail, audio_tags_end, + cover_rank, sample_audio, + }; use super::{ RawTempFile, ScaleAttempt, THUMB_EDGE, UPLOAD_IMAGE_MAX_BYTES, UPLOAD_THUMB_MAX_BYTES, VIDEO_HEAD_BYTES, VIDEO_WHOLE_MAX_BYTES, VideoSample, can_make_thumbnail, exiftool_binary, @@ -2964,6 +3301,28 @@ mod thumb_tests { assert!(thumb.ratio > 0.0); } + /// Optional check against real songs, through the same partial download a + /// Drive song gets. `PDFS_AUDIO_FIXTURE` lists files separated by `:`. + #[test] + #[ignore = "set PDFS_AUDIO_FIXTURE to songs with a cover in their tags"] + fn real_audio_fixture_yields_its_cover() { + let paths = std::env::var_os("PDFS_AUDIO_FIXTURE").expect("PDFS_AUDIO_FIXTURE"); + for path in std::env::split_paths(&paths) { + let name = path.file_name().unwrap().to_string_lossy().into_owned(); + let bytes = std::fs::read(&path).unwrap(); + let revision = FakeRevision::new(bytes.len() as u64).with(0, bytes); + let sample = block_on(sample_audio(&revision, &name)).unwrap(); + let read = revision.read.load(Ordering::Relaxed); + let attempt = audio_cover_thumbnail(&sample, &name, &std::env::temp_dir()); + assert!( + matches!(attempt, ScaleAttempt::Made(_)), + "{name}: no cover, read {read} of {} bytes", + revision.size + ); + eprintln!("{name}: read {read} of {} bytes", revision.size); + } + } + #[test] fn videos_are_told_apart_by_name() { for name in [ @@ -3120,4 +3479,201 @@ mod thumb_tests { "a small one is untouched" ); } + + /// A `width`×`height` PNG, the cover a tag carries. + fn png(width: u32, height: u32) -> Vec { + let mut bytes = Vec::new(); + image::DynamicImage::ImageRgb8(image::RgbImage::new(width, height)) + .write_to( + &mut std::io::Cursor::new(&mut bytes), + image::ImageFormat::Png, + ) + .unwrap(); + bytes + } + + /// An ID3v2.3 tag holding `picture` as its front cover. + fn id3_with_cover(picture: &[u8]) -> Vec { + let mut body = vec![0u8]; + body.extend_from_slice(b"image/png\0"); + body.push(3); + body.push(0); + body.extend_from_slice(picture); + let mut frame = b"APIC".to_vec(); + frame.extend_from_slice(&(body.len() as u32).to_be_bytes()); + frame.extend_from_slice(&[0, 0]); + frame.extend_from_slice(&body); + let len = frame.len() as u32; + let mut tag = b"ID3\x03\x00\x00".to_vec(); + tag.extend((0..4).rev().map(|i| ((len >> (7 * i)) & 0x7f) as u8)); + tag.extend_from_slice(&frame); + tag + } + + /// Ten silent MPEG-1 Layer III frames at 128 kbit/s and 44.1 kHz. + fn mpeg_frames() -> Vec { + let mut frame = vec![0u8; 417]; + frame[..4].copy_from_slice(&[0xFF, 0xFB, 0x90, 0x64]); + frame.repeat(10) + } + + /// A FLAC stream's metadata: STREAMINFO, then `picture` as its front cover. + fn flac_with_cover(picture: &[u8]) -> Vec { + let mut info = vec![0x10, 0x00, 0x10, 0x00, 0, 0, 0, 0, 0, 0]; + let packed: u64 = (44_100 << 44) | (1 << 41) | (15 << 36); + info.extend_from_slice(&packed.to_be_bytes()); + info.extend_from_slice(&[0; 16]); + let mut block = Vec::new(); + block.extend_from_slice(&3u32.to_be_bytes()); + block.extend_from_slice(&9u32.to_be_bytes()); + block.extend_from_slice(b"image/png"); + block.extend_from_slice(&0u32.to_be_bytes()); + for value in [16u32, 8, 24, 0, picture.len() as u32] { + block.extend_from_slice(&value.to_be_bytes()); + } + block.extend_from_slice(picture); + let mut out = b"fLaC".to_vec(); + out.push(0); + out.extend_from_slice(&(info.len() as u32).to_be_bytes()[1..]); + out.extend_from_slice(&info); + out.push(0x80 | 6); + out.extend_from_slice(&(block.len() as u32).to_be_bytes()[1..]); + out.extend_from_slice(&block); + out + } + + fn whole_song(bytes: Vec) -> AudioSample { + AudioSample { + size: bytes.len() as u64, + parts: vec![(0, bytes)], + } + } + + #[test] + fn a_song_shows_the_cover_in_its_tags() { + let tmp = scratch("song-cover"); + std::fs::create_dir_all(&tmp).unwrap(); + let mut mp3 = id3_with_cover(&png(16, 8)); + mp3.extend(mpeg_frames()); + let ScaleAttempt::Made(thumb) = audio_cover_thumbnail(&whole_song(mp3), "song.mp3", &tmp) + else { + panic!("an ID3 cover scales"); + }; + assert!((thumb.ratio - 2.0).abs() < 1e-6); + + let ScaleAttempt::Made(thumb) = + audio_cover_thumbnail(&whole_song(flac_with_cover(&png(8, 16))), "song.flac", &tmp) + else { + panic!("a FLAC picture scales"); + }; + assert!((thumb.ratio - 0.5).abs() < 1e-6); + std::fs::remove_dir_all(&tmp).unwrap(); + } + + #[test] + fn a_song_without_a_cover_is_no_thumbnail_of_its_own() { + let tmp = scratch("song-bare"); + std::fs::create_dir_all(&tmp).unwrap(); + let attempt = audio_cover_thumbnail(&whole_song(mpeg_frames()), "song.mp3", &tmp); + assert!(matches!(attempt, ScaleAttempt::Undecodable)); + std::fs::remove_dir_all(&tmp).unwrap(); + } + + #[test] + fn the_start_of_a_song_says_where_its_tags_end() { + let tag = id3_with_cover(&png(4, 4)); + assert_eq!(audio_tags_end(&tag), Some(tag.len() as u64)); + let flac = flac_with_cover(&png(4, 4)); + assert_eq!(audio_tags_end(&flac), Some(flac.len() as u64)); + // FLAC after an ID3 tag, as some taggers write it. + let both = [tag.clone(), flac.clone()].concat(); + assert_eq!(audio_tags_end(&both), Some(both.len() as u64)); + // The picture's block header cut off by the end of the probe, just + // past STREAMINFO: a guess past it. + let cut = &flac[..4 + 4 + 34 + 2]; + assert!(audio_tags_end(cut).is_some_and(|end| end > cut.len() as u64)); + assert_eq!(audio_tags_end(b"OggS\0\0\0\0"), None); + assert_eq!( + audio_tags_end(b"ID3\x03\x00\x00\x80\0\0\0"), + None, + "not synchsafe" + ); + } + + #[test] + fn a_large_song_downloads_its_tags_not_its_audio() { + const SIZE: u64 = 300 << 20; + let mut start = id3_with_cover(&png(16, 8)); + start.extend(mpeg_frames()); + let revision = FakeRevision::new(SIZE).with(0, start); + let sample = block_on(sample_audio(&revision, "long mix.mp3")).unwrap(); + let read = revision.read.load(Ordering::Relaxed); + assert!(read < 1 << 20, "read {read} bytes"); + + let tmp = scratch("song-sparse"); + std::fs::create_dir_all(&tmp).unwrap(); + let ScaleAttempt::Made(thumb) = audio_cover_thumbnail(&sample, "long mix.mp3", &tmp) else { + panic!("the cover comes from the start alone"); + }; + assert!((thumb.ratio - 2.0).abs() < 1e-6); + std::fs::remove_dir_all(&tmp).unwrap(); + + // Ogg says nothing up front: a bounded head. WavPack keeps its tags at + // the end: the tail too. + let revision = FakeRevision::new(SIZE); + block_on(sample_audio(&revision, "live.ogg")).unwrap(); + assert!(revision.read.load(Ordering::Relaxed) <= AUDIO_HEAD_BYTES + (64 << 10)); + let revision = FakeRevision::new(SIZE); + let sample = block_on(sample_audio(&revision, "live.wv")).unwrap(); + assert!( + sample + .parts + .iter() + .any(|(offset, bytes)| *offset == SIZE - AUDIO_TAIL_BYTES + && bytes.len() as u64 == AUDIO_TAIL_BYTES) + ); + } + + #[test] + fn an_uploaded_song_carries_its_cover() { + let tmp = scratch("upload-song"); + std::fs::create_dir_all(&tmp).unwrap(); + // A staging blob has no extension: the format comes from the bytes. + let blob = tmp.join("blob"); + let mut mp3 = id3_with_cover(&png(64, 64)); + mp3.extend(mpeg_frames()); + std::fs::write(&blob, mp3).unwrap(); + let ffmpeg = fake_ffmpeg(&tmp, "exit 1"); + let thumbs = upload_thumbnails_with(&blob, "song.mp3", &tmp, ffmpeg.as_os_str()); + let [thumbnail] = thumbs.as_slice() else { + panic!("one thumbnail, got {}", thumbs.len()); + }; + assert!(matches!(thumbnail.thumbnail_type, ThumbnailType::Thumbnail)); + assert_eq!(ratio_of(&thumbnail.content), Some(1.0)); + + std::fs::write(&blob, mpeg_frames()).unwrap(); + assert!( + upload_thumbnails_with(&blob, "song.mp3", &tmp, ffmpeg.as_os_str()).is_empty(), + "a song without a cover uploads bare" + ); + let _ = std::fs::remove_dir_all(&tmp); + } + + #[test] + fn a_folder_cover_is_found_by_its_usual_names() { + assert_eq!(cover_rank("cover.jpg"), Some(0)); + assert_eq!(cover_rank("Cover.JPG"), Some(0)); + assert_eq!(cover_rank("folder.png"), Some(1)); + assert_eq!(cover_rank("AlbumArtSmall.jpg"), Some(5)); + assert!(cover_rank("cover.jpg") < cover_rank("front.webp")); + for name in [ + "cover.txt", + "my cover.jpg", + "cover", + "back.jpg", + "cover.jpg.part", + ] { + assert_eq!(cover_rank(name), None, "{name}"); + } + } } diff --git a/crates/pdfs-fuse/src/revisions.rs b/crates/pdfs-fuse/src/revisions.rs index 2aa1d83..34a698c 100644 --- a/crates/pdfs-fuse/src/revisions.rs +++ b/crates/pdfs-fuse/src/revisions.rs @@ -111,6 +111,11 @@ impl Core { // stale from here on, and the server may not have applied the swap yet. self.cache.evict(uid); self.evict_reader(uid); + // A file kept offline gets the restored content back on disk, and + // again when the event of the swap arrives (`docs/BUGS.md` B282). + if self.cache.is_pinned(uid) { + self.recheck_pinned(uid); + } self.log_activity( ActivityKind::Restore, name, diff --git a/crates/pdfs-fuse/src/sharing.rs b/crates/pdfs-fuse/src/sharing.rs index a4d8fe1..c5ab8c3 100644 --- a/crates/pdfs-fuse/src/sharing.rs +++ b/crates/pdfs-fuse/src/sharing.rs @@ -105,7 +105,7 @@ impl Core { role: &str, message: Option<&str>, ) -> CoreResult<(usize, usize)> { - let uid = self.resolve_anywhere(uid)?; + let uid = self.resolve_shareable(uid)?; self.share_node_for_uid(&uid, emails, role, message) } @@ -138,7 +138,7 @@ impl Core { &self, uid: &str, ) -> CoreResult<(Vec, Option)> { - let uid = self.resolve_anywhere(uid)?; + let uid = self.resolve_shareable(uid)?; self.list_share_for_uid(&uid) } @@ -215,7 +215,7 @@ impl Core { kind: ShareEntryKind, role: &str, ) -> CoreResult<()> { - let uid = self.resolve_anywhere(uid)?; + let uid = self.resolve_shareable(uid)?; self.update_share_role_for_uid(&uid, id, kind, role) } @@ -278,7 +278,7 @@ impl Core { id: &str, kind: ShareEntryKind, ) -> CoreResult<()> { - let uid = self.resolve_anywhere(uid)?; + let uid = self.resolve_shareable(uid)?; self.remove_share_entry_for_uid(&uid, id, kind) } @@ -349,7 +349,7 @@ impl Core { password: Option<&str>, expires: Option, ) -> CoreResult { - let uid = self.resolve_anywhere(uid)?; + let uid = self.resolve_shareable(uid)?; self.create_public_link_for_uid(&uid, role, password, expires) } @@ -391,7 +391,7 @@ impl Core { password: LinkChange, expires: LinkChange, ) -> CoreResult { - let uid = self.resolve_anywhere(uid)?; + let uid = self.resolve_shareable(uid)?; self.update_public_link_for_uid(&uid, id, role, password, expires) } @@ -460,7 +460,7 @@ impl Core { /// [`Core::remove_public_link`] for a node addressed through any daemon /// location. pub(crate) fn remove_public_link_by_uid(&self, uid: &str, id: &str) -> CoreResult<()> { - let uid = self.resolve_anywhere(uid)?; + let uid = self.resolve_shareable(uid)?; self.remove_public_link_for_uid(&uid, id) } @@ -486,7 +486,7 @@ impl Core { /// [`Core::stop_sharing`] for a node addressed through any daemon location. pub(crate) fn stop_sharing_by_uid(&self, uid: &str) -> CoreResult { - let uid = self.resolve_anywhere(uid)?; + let uid = self.resolve_shareable(uid)?; self.stop_sharing_for_uid(&uid) } diff --git a/crates/pdfs-fuse/src/sim/daemon.rs b/crates/pdfs-fuse/src/sim/daemon.rs index 4e33268..b897306 100644 --- a/crates/pdfs-fuse/src/sim/daemon.rs +++ b/crates/pdfs-fuse/src/sim/daemon.rs @@ -3447,6 +3447,118 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + #[test] + #[ignore = "mounts FUSE: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"] + fn pinning_a_file_in_an_on_demand_folder_keeps_that_folders_copy() { + // Keeping a file offline took only paths under the primary mount, so + // the copy in an on-demand synced folder could not be kept, and a + // program reading it there downloaded it every time (B281). + let drive = FakeDrive::new(); + let dir = scratch("pin-in-ondemand"); + let daemon = Daemon::start(&dir, drive.client(1, Faults::lan())).unwrap(); + let ondemand = dir.join("ondemand"); + sync_folder(&daemon, &ondemand, "ondemand"); + + let content = b"not a real png, but kept all the same".repeat(64); + let file = ondemand.join("photo.png"); + std::fs::write(&file, &content).unwrap(); + assert!(wait_until(Duration::from_secs(30), || { + daemon.pending().is_ok_and(|items| items.is_empty()) + })); + daemon.request(&Request::PurgeCache).unwrap(); + + let path = file.canonicalize().unwrap().to_string_lossy().into_owned(); + let pinned = daemon + .request(&Request::Pin { path: path.clone() }) + .unwrap(); + assert!(matches!(pinned, Response::Ok { .. }), "{pinned:?}"); + let pins = || match daemon.request(&Request::ListPins) { + Ok(Response::Pins { pins }) => pins, + other => panic!("pins: {other:?}"), + }; + let listed = pins(); + assert!( + listed.iter().any(|pin| pin.path == path && pin.cached), + "the pin opens by the path it was made through: {listed:?}" + ); + + // With the link gone, only a kept copy can answer the read. + daemon.client.set_online(false); + assert_eq!(std::fs::read(&file).unwrap(), content); + daemon.client.set_online(true); + + let unpinned = daemon + .request(&Request::Unpin { path: path.clone() }) + .unwrap(); + assert!(matches!(unpinned, Response::Ok { .. }), "{unpinned:?}"); + assert!(pins().is_empty()); + + assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown))); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + #[ignore = "mounts FUSE: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"] + fn a_file_kept_offline_stays_on_disk_through_changes_made_elsewhere() { + // A change from another device deleted a pinned file's copy and + // nothing downloaded it again, and a file added to a pinned folder + // elsewhere was never downloaded: both were listed as kept offline + // and read only with a network (B282). + let drive = FakeDrive::new(); + let dir = scratch("pin-keeper"); + let daemon = Daemon::start(&dir, drive.client(1, Faults::lan())).unwrap(); + std::fs::create_dir(daemon.mountpoint.join("Docs")).unwrap(); + std::fs::write(daemon.mountpoint.join("Docs/a.txt"), b"first").unwrap(); + std::fs::write(daemon.mountpoint.join("solo.txt"), b"alone").unwrap(); + assert!(wait_until(Duration::from_secs(30), || { + daemon.pending().is_ok_and(|items| items.is_empty()) + && drive.lookup("Docs/a.txt").is_some() + && drive.lookup("solo.txt").is_some() + })); + daemon.request(&Request::PurgeCache).unwrap(); + let entry = |folder: &str, name: &str| match daemon.request(&Request::ListDir { + path: folder.into(), + }) { + Ok(Response::Entries { entries }) => entries.into_iter().find(|e| e.name == name), + _ => None, + }; + for path in ["Docs", "solo.txt"] { + let pinned = daemon.request(&Request::Pin { path: path.into() }).unwrap(); + assert!(matches!(pinned, Response::Ok { .. }), "{pinned:?}"); + } + assert!(wait_until(Duration::from_secs(30), || { + entry("Docs", "a.txt").is_some_and(|e| e.cached) + && entry("", "solo.txt").is_some_and(|e| e.cached) + })); + + // Another device edits both files and adds one to the kept folder. + drive.device().write("Docs/a.txt", b"second, and longer"); + drive.device().write("solo.txt", b"alone, edited"); + drive.device().write("Docs/new.txt", b"made elsewhere"); + assert!( + wait_until(Duration::from_secs(60), || { + entry("Docs", "a.txt").is_some_and(|e| e.cached && e.size == 18) + && entry("", "solo.txt").is_some_and(|e| e.cached && e.size == 13) + && entry("Docs", "new.txt").is_some_and(|e| e.cached) + }), + "the keeper downloaded the new content: {:?} {:?} {:?}", + entry("Docs", "a.txt"), + entry("", "solo.txt"), + entry("Docs", "new.txt"), + ); + + // With the link gone, only the kept copies can answer. + daemon.client.set_online(false); + let read = |rel: &str| std::fs::read(daemon.mountpoint.join(rel)).unwrap(); + assert_eq!(read("Docs/a.txt"), b"second, and longer"); + assert_eq!(read("solo.txt"), b"alone, edited"); + assert_eq!(read("Docs/new.txt"), b"made elsewhere"); + daemon.client.set_online(true); + + assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown))); + let _ = std::fs::remove_dir_all(&dir); + } + /// The synced folder `id` as the daemon lists it. fn listed_folder(daemon: &Daemon, id: i64) -> Option { match daemon.request(&Request::ListSyncFolders) { @@ -3620,6 +3732,88 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + /// A song with no picture in its tags shows the cover image in its folder, + /// and the daemon tells a following front-end that a thumbnail landed, so + /// a folder on screen fills in without being opened again. + #[test] + #[ignore = "mounts FUSE: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"] + fn a_song_without_a_cover_shows_its_folders_and_says_so() { + let dir = scratch("song-cover"); + std::fs::create_dir_all(&dir).unwrap(); + // Silent MPEG-1 Layer III frames, without any tag. + let mut frame = vec![0u8; 417]; + frame[..4].copy_from_slice(&[0xFF, 0xFB, 0x90, 0x64]); + let song = frame.repeat(64); + let mut cover = Vec::new(); + image::codecs::jpeg::JpegEncoder::new(&mut cover) + .encode_image(&image::RgbImage::new(300, 300)) + .unwrap(); + + let drive = FakeDrive::new(); + drive.device().write("track one.mp3", &song); + drive.device().write("Cover.jpg", &cover); + let client = drive.client(1, Faults::lan()); + let daemon = Daemon::start(&dir.join("daemon"), client).unwrap(); + + let landed = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let mut subscription = control::subscribe(&daemon.socket).unwrap(); + let flag = landed.clone(); + std::thread::spawn(move || { + // The first change names every topic: a new subscription catches up. + let mut caught_up = false; + while let Ok(Some(event)) = subscription.next_event() { + if let control::Event::Changed { topics } = event { + if caught_up && topics.contains(&control::Topic::Thumbnails) { + flag.store(true, std::sync::atomic::Ordering::SeqCst); + return; + } + caught_up = true; + } + } + }); + + let Ok(Response::Entries { entries }) = daemon.request(&Request::ListDir { + path: String::new(), + }) else { + panic!("listing"); + }; + let song = entries + .iter() + .find(|entry| entry.name == "track one.mp3") + .expect("the song is listed"); + let items = vec![control::FileThumbRequest { + uid: song.uid.clone(), + modified: song.modified, + name: song.name.clone(), + }]; + let Ok(Response::FileThumbGeneration { generation }) = + daemon.request(&Request::ReserveFileThumbGeneration) + else { + panic!("generation"); + }; + let mut path = None; + let settled = wait_until(Duration::from_secs(60), || { + let Ok(Response::Thumbs { items: replies }) = daemon.request(&Request::FileThumbs { + items: items.clone(), + generation, + }) else { + return false; + }; + path = replies.first().and_then(|reply| reply.path.clone()); + path.is_some() + }); + assert!(settled, "the song got no thumbnail"); + let thumb = image::load_from_memory(&std::fs::read(path.unwrap()).unwrap()).unwrap(); + assert_eq!(thumb.width(), thumb.height(), "the square cover"); + assert!( + wait_until(Duration::from_secs(15), || landed + .load(std::sync::atomic::Ordering::SeqCst)), + "no thumbnails change was published" + ); + assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown))); + let _ = std::fs::remove_dir_all(&dir); + } + /// A video in My files gets a thumbnail from a frame of its own, but a /// recording can be gigabytes: only its start, and an MP4's index, are /// downloaded for it. A video with a thumbnail from its uploader is not diff --git a/crates/pdfs-fuse/src/takeout.rs b/crates/pdfs-fuse/src/takeout.rs index 4a23d86..3b45f32 100644 --- a/crates/pdfs-fuse/src/takeout.rs +++ b/crates/pdfs-fuse/src/takeout.rs @@ -2,36 +2,47 @@ //! //! [`pdfs_core::takeout`] says what is in the archives; this is the part that //! talks to Proton. One import runs on its own thread and moves through four -//! phases, reporting into the transfer registry as a job so the GUI's transfer -//! list shows it without a bespoke widget: +//! phases ([`ImportPhase`]), which [`Request::ImportStatus`] reports together +//! with the running counts: //! -//! 1. **Scan** — read the archives' central directories and sidecars. -//! 2. **Deduplicate against the account** — ask the photos volume which of the -//! names already exist ([`ProtonPhotosClient::name_collisions`]), hash only -//! those files, then confirm with the content digest. A library that is -//! already half-synced by Proton Photos on a phone therefore costs one cheap -//! request per 150 names and reads no media at all for the misses. -//! 3. **Upload** the survivors, streaming each entry straight out of its zip — -//! no extraction to disk, so the import needs no scratch space. +//! 1. **Scan** — read the archives' central directories and sidecars. An +//! archive that cannot be read is left out and reported; the rest imports. +//! 2. **Check** — ask the photos volume which of the names already exist +//! ([`ProtonPhotosClient::name_collisions`]), hash only those files, then +//! confirm with the content digest. A library that is already half-synced +//! by Proton Photos on a phone therefore costs one cheap request per 150 +//! names and reads no media at all for the misses. +//! 3. **Upload** the survivors, streaming each entry out of its zip. A photo +//! that can have a thumbnail is copied to a staging file first, one at a +//! time. The video half of a live or motion photo goes after its still and +//! is filed with it. //! 4. **Albums** — create the albums the export names that the account lacks, //! then add every photo of each album, the ones that were already on the //! account included. //! -//! Re-running an interrupted import is the resume story: phase 2 finds -//! everything the previous run uploaded and skips it. Nothing is persisted -//! between runs. +//! Resuming is re-running: phase 2 finds everything an earlier run uploaded +//! and skips it. The archives and the outcome of the last run are kept in the +//! state directory, so an import cut short by a reboot can be picked up again +//! with [`Request::ResumeImport`] without choosing the archives again. +//! +//! [`Request::ImportStatus`]: pdfs_core::control::Request::ImportStatus +//! [`Request::ResumeImport`]: pdfs_core::control::Request::ResumeImport +//! [`ProtonPhotosClient::name_collisions`]: proton_drive_rs::ProtonPhotosClient::name_collisions use std::collections::{BTreeMap, HashMap}; use std::io::Read; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::Arc; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; -use pdfs_core::control::{ActivityKind, ImportSummary, TransferDirection}; -use pdfs_core::takeout::{Bucket, TakeoutPhoto, TakeoutScan, scan_archives}; +use pdfs_core::control::{ + ActivityKind, ImportPhase, ImportProgress, ImportSummary, TransferDirection, +}; +use pdfs_core::takeout::{Bucket, TakeoutPhoto, TakeoutScan, scan_archives_until}; use pdfs_core::{CoreError, CoreResult}; use proton_drive_rs::proton_sdk::ids::NodeUid; -use proton_drive_rs::{PhotoTag, PhotoTagsUpdate, PhotoUploadMetadata}; +use proton_drive_rs::{PhotoLocation, PhotoTag, PhotoTagsUpdate, PhotoUploadMetadata}; +use serde::{Deserialize, Serialize}; use sha1::{Digest, Sha1}; use tracing::{info, warn}; @@ -42,23 +53,161 @@ use crate::transfers::CountingReader; /// The one import this daemon may be running, and how the last one ended. /// /// Process-global rather than a [`Core`] field because it must be *one* import -/// per daemon however many `Core` clones exist (the forked on-demand mounts each -/// hold one), and because a front-end asking for the result after the import -/// finished must still find it. +/// per daemon however many `Core` clones exist, and because a front-end asking +/// for the result after the import finished must still find it. Loaded from +/// the state directory on first use, so the last run survives a restart. static IMPORT: std::sync::LazyLock> = - std::sync::LazyLock::new(|| parking_lot::Mutex::new(ImportState::default())); + std::sync::LazyLock::new(|| parking_lot::Mutex::new(ImportState::load(saved_path()))); + +/// Photos uploaded between two timeline refreshes, so a long import shows up +/// in the gallery as it goes rather than only at the end. +const TIMELINE_REFRESH_EVERY: usize = 250; + +/// Photos uploaded between two saves of the running counts, so a run cut short +/// by a power loss still reports roughly how far it got. +const CHECKPOINT_EVERY: usize = 25; + +/// How far apart, in seconds, the still and the video of one live photo may +/// claim to have been taken before they are treated as two unrelated files +/// that happen to share a name. +const LIVE_PAIR_MAX_SKEW: i64 = 120; #[derive(Default)] struct ImportState { running: bool, cancel: Option>, - last: Option, + archives: Vec, + dry_run: bool, + /// Live counts while running, the final report after. + summary: Option, + progress: Option, + /// Where the state is saved; `None` keeps it in memory only (tests). + path: Option, } -/// Whether an import is running, and the last one's report. -pub(crate) fn import_status() -> (bool, Option) { +/// What [`ImportState`] keeps on disk. +#[derive(Serialize, Deserialize, Default)] +struct SavedImport { + archives: Vec, + dry_run: bool, + running: bool, + summary: Option, +} + +fn saved_path() -> Option { + if cfg!(test) { + return None; + } + pdfs_core::config::AppDirs::new() + .ok() + .map(|dirs| dirs.takeout_import_path()) +} + +impl ImportState { + /// The state as the last daemon left it. A run that was still marked as + /// running did not get to finish: the daemon stopped under it. + fn load(path: Option) -> Self { + let saved = path + .as_deref() + .and_then(|path| std::fs::read(path).ok()) + .and_then(|bytes| { + serde_json::from_slice::(&bytes) + .inspect_err(|error| warn!(%error, "ignoring an unreadable import state")) + .ok() + }) + .unwrap_or_default(); + let mut summary = saved.summary; + if saved.running { + summary.get_or_insert_with(Default::default).interrupted = true; + } + Self { + archives: saved.archives, + dry_run: saved.dry_run, + summary, + path, + ..Default::default() + } + } + + /// Write the state out. Atomic, so a crash mid-write keeps the old one. + fn save(&self) { + let Some(path) = &self.path else { + return; + }; + let saved = SavedImport { + archives: self.archives.clone(), + dry_run: self.dry_run, + running: self.running, + summary: self.summary.clone(), + }; + let written = serde_json::to_vec_pretty(&saved) + .map_err(std::io::Error::other) + .and_then(|bytes| write_atomically(path, &bytes)); + if let Err(error) = written { + warn!(path = %path.display(), %error, "cannot save the import state"); + } + } + + /// The archives of the last run when it can be resumed: a real import that + /// did not finish everything it set out to do. + fn resumable(&self) -> Vec { + let unfinished = self + .summary + .as_ref() + .is_some_and(|summary| !summary.complete()); + if self.running || self.dry_run || !unfinished { + return Vec::new(); + } + self.archives + .iter() + .map(|path| path.display().to_string()) + .collect() + } +} + +fn write_atomically(path: &Path, bytes: &[u8]) -> std::io::Result<()> { + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + let tmp = path.with_extension(format!("tmp-{}", std::process::id())); + { + use std::io::Write; + let mut file = std::fs::File::create(&tmp)?; + file.write_all(bytes)?; + file.sync_all()?; + } + std::fs::rename(&tmp, path)?; + if let Some(parent) = path.parent() { + std::fs::File::open(parent)?.sync_all()?; + } + Ok(()) +} + +/// What [`Request::ImportStatus`](pdfs_core::control::Request::ImportStatus) +/// answers. +pub(crate) struct ImportStatusReport { + pub running: bool, + pub summary: Option, + pub progress: Option, + pub resumable: Vec, +} + +/// Whether an import is running, its counts and step, and what can be resumed. +pub(crate) fn import_status() -> ImportStatusReport { let state = IMPORT.lock(); - (state.running, state.last.clone()) + ImportStatusReport { + running: state.running, + summary: state.summary.clone(), + progress: state.running.then(|| state.progress.clone()).flatten(), + resumable: state.resumable(), + } +} + +/// The archives [`Request::ResumeImport`](pdfs_core::control::Request::ResumeImport) +/// would import again, or `None` when there is nothing to resume. +pub(crate) fn resumable_archives() -> Option> { + let state = IMPORT.lock(); + (!state.resumable().is_empty()).then(|| state.archives.clone()) } /// Ask the running import to stop. False when none is running. @@ -72,17 +221,65 @@ pub(crate) fn cancel_import() -> bool { } } +/// Publishes a running import's step and counts into [`IMPORT`]. +struct Reporter; + +impl Reporter { + fn phase(&self, phase: ImportPhase, total: u64) { + IMPORT.lock().progress = Some(ImportProgress { + phase, + done: 0, + total, + current: None, + }); + } + + fn step(&self) { + if let Some(progress) = IMPORT.lock().progress.as_mut() { + progress.done += 1; + } + } + + fn current(&self, name: &str) { + if let Some(progress) = IMPORT.lock().progress.as_mut() { + progress.current = Some(name.to_string()); + } + } + + fn counts(&self, summary: &ImportSummary) { + IMPORT.lock().summary = Some(summary.clone()); + } + + /// Save the running state, still marked as running. + fn checkpoint(&self) { + IMPORT.lock().save(); + } +} + +/// How a planned photo relates to a live or motion photo pair. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum LiveRole { + /// The still of a pair: uploaded first, tagged as a live photo. + Still(PhotoTag), + /// The video of a pair, filed with the still at this index of the plan. + Video(usize), +} + /// One photo to import, after the copies Google keeps per album are folded into /// a single upload that joins several albums. struct Planned { photo: TakeoutPhoto, /// Every album this photo belongs to, by export title. albums: Vec, + /// The live-photo keys of every folded copy (each copy sits in its own + /// folder, so each has its own key). + live_keys: Vec, + live: Option, /// Filled in phase 2/3: where the photo ended up on the account. uid: Option, } -/// Fold the scan into one entry per distinct photo. +/// Fold the scan into one entry per distinct photo, then pair live photos. /// /// Google stores a full copy of a photo in every album folder it appears in, so /// the same file is in the export two or three times over. They are folded on @@ -102,6 +299,8 @@ fn plan(scan: &TakeoutScan) -> Vec { Planned { photo: photo.clone(), albums: Vec::new(), + live_keys: Vec::new(), + live: None, uid: None, } }); @@ -110,18 +309,94 @@ fn plan(scan: &TakeoutScan) -> Vec { { entry.albums.push(title.clone()); } - // A copy that carries a capture time wins over one that does not: the - // album copy and the year-folder copy do not always both have a sidecar. + if let Some(live_key) = &photo.live_key + && !entry.live_keys.contains(live_key) + { + entry.live_keys.push(live_key.clone()); + } + // A copy that carries metadata wins over one that does not: the album + // copy and the year-folder copy do not always both have a sidecar. if entry.photo.capture_time.is_none() && photo.capture_time.is_some() { entry.photo.capture_time = photo.capture_time; } + if entry.photo.creation_time.is_none() && photo.creation_time.is_some() { + entry.photo.creation_time = photo.creation_time; + } + if entry.photo.location.is_none() && photo.location.is_some() { + entry.photo.location = photo.location; + } entry.photo.favorite |= photo.favorite; } - order + let mut planned: Vec = order .into_iter() .filter_map(|key| by_key.remove(&key)) - .collect() + .collect(); + pair_live_photos(&mut planned); + planned +} + +/// Pair the still and the video of each live or motion photo. +/// +/// The halves share a live key (same folder, same stem). The video takes the +/// still's date and place when it has none of its own, because a `.MOV` half +/// usually has no sidecar. Two files whose capture times disagree by minutes +/// are not a pair, whatever their names. +fn pair_live_photos(planned: &mut [Planned]) { + let mut stills: HashMap<&str, usize> = HashMap::new(); + for (index, item) in planned.iter().enumerate() { + if !item.photo.is_video() { + for key in &item.live_keys { + stills.entry(key.as_str()).or_insert(index); + } + } + } + + let mut pairs: Vec<(usize, usize)> = Vec::new(); + let mut taken = vec![false; planned.len()]; + for (video, item) in planned.iter().enumerate() { + if !item.photo.is_video() { + continue; + } + let Some(&still) = item + .live_keys + .iter() + .find_map(|key| stills.get(key.as_str())) + else { + continue; + }; + if taken[still] { + continue; + } + if let (Some(a), Some(b)) = (item.photo.capture_time, planned[still].photo.capture_time) + && (a - b).abs() > LIVE_PAIR_MAX_SKEW + { + continue; + } + taken[still] = true; + pairs.push((still, video)); + } + + for (still, video) in pairs { + let tag = if planned[video].photo.is_motion_photo() { + PhotoTag::MotionPhoto + } else { + PhotoTag::LivePhoto + }; + let (capture_time, location) = ( + planned[still].photo.capture_time, + planned[still].photo.location, + ); + planned[still].live = Some(LiveRole::Still(tag)); + let video = &mut planned[video]; + video.live = Some(LiveRole::Video(still)); + if video.photo.capture_time.is_none() { + video.photo.capture_time = capture_time; + } + if video.photo.location.is_none() { + video.photo.location = location; + } + } } /// Open one archive and stream `entry` through `sink`. @@ -161,35 +436,123 @@ impl Core { Ok(path) } + /// Whether the import should stop taking on new work. + fn import_should_stop(&self, cancel: &AtomicBool) -> bool { + cancel.load(Ordering::Relaxed) || self.shutdown.is_stopping() + } + /// Import Google Photos Takeout archives. Blocking; run on its own thread. /// + /// Never fails as a whole: whatever stops the run early is recorded in the + /// summary's `error` next to the counts of what it did get done, and the + /// photos it uploaded are still filed into their albums. + /// /// `cancel` stops the run at the next photo boundary — an upload already on /// the wire is allowed to finish, so a cancelled import never leaves a /// half-written revision behind. - pub(crate) fn import_takeout( + fn import_takeout( &self, - archives: Vec, + archives: &[PathBuf], dry_run: bool, - cancel: Arc, - ) -> CoreResult { - let job = self.transfers.begin_job("Importing Google Photos"); - job.detail("reading archives"); - - let scan = scan_archives(&archives)?; - let mut planned = plan(&scan); + cancel: &AtomicBool, + job: &crate::transfers::JobGuard, + ) -> ImportSummary { + let report = Reporter; let mut summary = ImportSummary { - found: planned.len(), - skipped_trashed: scan.skipped_trashed, + dry_run, ..Default::default() }; - job.set_total(planned.len() as u64); + let mut planned = Vec::new(); + let mut albums = Vec::new(); + + let outcome = self.run_import( + archives, + cancel, + &mut summary, + &mut planned, + &mut albums, + &report, + job, + ); + // Whether the run left work undone because it was asked to stop. A + // cancel that arrives after the last photo is not a cancelled run. + let stopped_early = match &outcome { + Ok(stopped) => *stopped, + Err(e) => { + warn!(error = %e, "takeout import stopped"); + summary.error = Some(e.message.clone()); + summary.error_kind = Some(e.kind); + false + } + }; + summary.interrupted = stopped_early && self.shutdown.is_stopping(); + summary.cancelled = stopped_early && !summary.interrupted; + + // Albums for whatever made it up, after a cancel or an error too — but + // not while the daemon is shutting down, which must not wait on it. A + // resumed run files them. + if !dry_run && !summary.interrupted && planned.iter().any(|p| p.uid.is_some()) { + if !albums.is_empty() { + report.phase(ImportPhase::Albums, albums.len() as u64); + job.detail("sorting photos into albums"); + if let Err(e) = self.link_albums(&planned, &albums, &mut summary, &report) { + warn!(error = %e, "takeout album linking failed"); + } + // Album linking stops for a shutdown too; a resumed run files + // the rest. + summary.interrupted |= self.shutdown.is_stopping(); + } + report.counts(&summary); + } + if summary.uploaded > 0 { + self.spawn_timeline_refresh(); + } + summary + } + + #[allow(clippy::too_many_arguments)] + fn run_import( + &self, + archives: &[PathBuf], + cancel: &AtomicBool, + summary: &mut ImportSummary, + planned: &mut Vec, + albums: &mut Vec, + report: &Reporter, + job: &crate::transfers::JobGuard, + ) -> CoreResult { + // Phase 1: scan. + report.phase(ImportPhase::Scanning, 0); + job.detail("reading archives"); + let scan = match scan_archives_until(archives, || self.import_should_stop(cancel)) { + Ok(scan) => scan, + Err(_) if self.import_should_stop(cancel) => return Ok(true), + Err(e) => return Err(e), + }; + *planned = plan(&scan); + *albums = scan.albums.clone(); + summary.found = planned.len(); + summary.skipped_trashed = scan.skipped_trashed; + summary.unreadable_archives = scan + .unreadable_archives + .iter() + .map(|archive| archive.path.display().to_string()) + .collect(); + report.counts(summary); info!( archives = archives.len(), + unreadable = scan.unreadable_archives.len(), photos = summary.found, albums = scan.albums.len(), trashed = summary.skipped_trashed, + other = scan.skipped_other, "takeout scan complete" ); + if scan.photos.is_empty() && scan.unreadable_archives.len() == archives.len() { + return Err(CoreError::invalid( + "none of the archives could be read; are they Google Takeout .zip files?", + )); + } let photos = self.photos(); if self @@ -205,29 +568,33 @@ impl Core { // Phase 2: which of these are already on the account. job.detail("checking for photos you already have"); - self.mark_existing(&mut planned, &archives, &mut summary, &cancel)?; - - if dry_run { - summary.duplicates = planned.iter().filter(|p| p.uid.is_some()).count(); - return Ok(summary); + self.mark_existing(planned, archives, summary, cancel, report)?; + if self.import_should_stop(cancel) { + return Ok(true); } - // Phase 3: upload what is left, one archive at a time so each zip's - // central directory is parsed once. - self.upload_planned(&mut planned, &archives, &mut summary, &cancel, &job)?; - - // Phase 4: albums. Attempted even after a cancel — the photos that did - // upload should still land in their albums. - if !scan.albums.is_empty() { - job.detail("sorting photos into albums"); - if let Err(e) = self.link_albums(&planned, &scan.albums, &mut summary) { - warn!(error = %e, "takeout album linking failed"); - } + let remaining = planned.iter().filter(|p| p.uid.is_none()); + summary.to_upload = remaining.clone().count(); + summary.to_upload_bytes = remaining.map(|p| p.photo.size).sum(); + report.counts(summary); + if summary.dry_run { + return Ok(false); } - self.spawn_timeline_refresh(); - summary.cancelled = cancel.load(Ordering::Relaxed); - Ok(summary) + // Favorites the export knows about for photos the account already had: + // a first import may have missed them, and setting one is idempotent. + let existing_favorites: Vec = planned + .iter() + .filter(|p| p.photo.favorite) + .filter_map(|p| p.uid.clone()) + .collect(); + self.mark_favorites(existing_favorites); + + // Phase 3: upload what is left. + report.checkpoint(); + report.phase(ImportPhase::Uploading, summary.to_upload as u64); + job.set_total(summary.to_upload as u64); + Ok(self.upload_planned(planned, archives, summary, cancel, job, report)) } /// Fill in `uid` for every planned photo the account already holds. @@ -241,7 +608,9 @@ impl Core { archives: &[PathBuf], summary: &mut ImportSummary, cancel: &AtomicBool, + report: &Reporter, ) -> CoreResult<()> { + report.phase(ImportPhase::Checking, 0); let photos = self.photos(); let names: Vec = planned.iter().map(|p| p.photo.name.clone()).collect(); let collisions = self @@ -259,17 +628,29 @@ impl Core { .push(index); } } + report.phase( + ImportPhase::Checking, + by_archive.values().map(Vec::len).sum::() as u64, + ); // Kept as a pair list, not a map: the duplicate lookup answers in input // order, so the order these were built in is what maps answers back to // planned photos. let mut digests: Vec<(usize, String)> = Vec::new(); - for (archive_index, indices) in by_archive { - if cancel.load(Ordering::Relaxed) { - break; - } - let mut archive = open_archive(archives, archive_index)?; + 'archives: for (archive_index, indices) in by_archive { + let mut archive = match open_archive(archives, archive_index) { + Ok(archive) => archive, + Err(e) => { + // Not hashed means not recognised as a duplicate; the upload + // phase then fails these photos on the same archive. + warn!(error = %e, "cannot reopen a takeout archive to check for duplicates"); + continue; + } + }; for index in indices { + if self.import_should_stop(cancel) { + break 'archives; + } let entry = planned[index].photo.entry.clone(); match read_entry(&mut archive, &entry, sha1_of) { Ok(Ok(digest)) => digests.push((index, digest)), @@ -277,9 +658,10 @@ impl Core { warn!(entry, error = %e, "cannot hash takeout entry"); } } + report.step(); } } - if digests.is_empty() { + if digests.is_empty() || self.import_should_stop(cancel) { return Ok(()); } @@ -298,10 +680,16 @@ impl Core { summary.duplicates += 1; } } + report.counts(summary); Ok(()) } /// Upload every planned photo that is not already on the account. + /// + /// Two rounds: everything but the videos of live photos first, then those + /// videos, so each one's still has a uid to be filed under. Within a round + /// the photos go archive by archive, so each zip's directory is read once. + /// Returns whether it stopped with photos left, on a cancel or a shutdown. fn upload_planned( &self, planned: &mut [Planned], @@ -309,149 +697,209 @@ impl Core { summary: &mut ImportSummary, cancel: &AtomicBool, job: &crate::transfers::JobGuard, - ) -> CoreResult<()> { - let photos = self.photos(); + report: &Reporter, + ) -> bool { let mut favorites: Vec = Vec::new(); + let mut stopped = false; - let mut by_archive: BTreeMap> = BTreeMap::new(); - for (index, item) in planned.iter().enumerate() { - if item.uid.is_none() { - by_archive - .entry(item.photo.archive) - .or_default() - .push(index); + for videos_round in [false, true] { + let mut by_archive: BTreeMap> = BTreeMap::new(); + for (index, item) in planned.iter().enumerate() { + let is_paired_video = matches!(item.live, Some(LiveRole::Video(_))); + if item.uid.is_none() && is_paired_video == videos_round { + by_archive + .entry(item.photo.archive) + .or_default() + .push(index); + } } - } - for (archive_index, indices) in by_archive { - if cancel.load(Ordering::Relaxed) || self.shutdown.is_stopping() { - break; - } - let mut archive = open_archive(archives, archive_index)?; - - for index in indices { - if cancel.load(Ordering::Relaxed) || self.shutdown.is_stopping() { + for (archive_index, indices) in by_archive { + if self.import_should_stop(cancel) { + stopped = true; break; } - let photo = planned[index].photo.clone(); - job.detail(photo.name.clone()); - - let guard = self.transfers.begin( - photo.name.clone(), - "", - TransferDirection::Upload, - photo.size, - ); - let metadata = PhotoUploadMetadata { - capture_time: photo.capture_time, - last_modification_time: photo - .capture_time - .and_then(pdfs_core::timestamp::modification_time), - ..Default::default() - }; - // A thumbnail is made from a file, so a photo that can have one - // is copied out of the archive first and uploaded from the copy. - // Without one, Proton's apps show a blank tile until they make - // their own. - let staged = can_make_thumbnail(&photo.name, photo.size) - .then(|| self.stage_takeout_entry(&mut archive, &photo.entry)) - .and_then(|staged| { - staged - .inspect_err(|e| { - warn!(name = photo.name, error = %e, - "copying a takeout photo out failed; uploading it without a thumbnail") - }) - .ok() - }); - let uploaded = match &staged { - Some(path) => { - let thumbnails = self.upload_thumbnails(path, &photo.name); - // No sidecar and no date in the name: the photo's own - // EXIF, before Drive files it under today. - let capture_time = metadata - .capture_time - .or_else(|| pdfs_core::exiftime::capture_time_of(path)); - let metadata = PhotoUploadMetadata { - capture_time, - last_modification_time: capture_time - .and_then(pdfs_core::timestamp::modification_time), - ..metadata - }; - std::fs::File::open(path) - .map_err(|e| { - CoreError::internal(format!("open {}: {e}", path.display())) - }) - .map(|file| { - let counting = - CountingReader::new(std::io::BufReader::new(file), &guard); - self.rt.block_on(photos.upload_photo_from( - &photo.name, - &photo.media_type, - counting, - photo.size as i64, - thumbnails, - metadata, - false, - )) - }) - } - None => read_entry(&mut archive, &photo.entry, |reader| { - let counting = CountingReader::new(reader, &guard); - self.rt.block_on(photos.upload_photo_from( - &photo.name, - &photo.media_type, - counting, - photo.size as i64, - Vec::new(), - metadata, - false, - )) - }), - }; - if let Some(path) = staged { - let _ = std::fs::remove_file(path); - } - drop(guard); - job.step(); - - match uploaded { - Ok(Ok(uid)) => { - summary.uploaded += 1; - summary.bytes += photo.size; - if photo.favorite { - favorites.push(uid.clone()); - } - planned[index].uid = Some(uid); - } - Ok(Err(e)) => { - summary.failed += 1; - warn!(name = photo.name, error = %e, "takeout photo upload failed"); - } + let mut archive = match open_archive(archives, archive_index) { + Ok(archive) => archive, Err(e) => { - summary.failed += 1; - warn!(name = photo.name, error = %e, "cannot read takeout entry"); + // The archive was readable at scan time; whatever broke + // it since costs its own photos, not the whole run. + warn!(error = %e, photos = indices.len(), "cannot reopen a takeout archive"); + summary.failed += indices.len(); + for _ in &indices { + job.step(); + report.step(); + } + report.counts(summary); + continue; } + }; + + for index in indices { + if self.import_should_stop(cancel) { + stopped = true; + break; + } + let main_uid = match planned[index].live { + Some(LiveRole::Video(still)) => planned[still].uid.clone(), + _ => None, + }; + let photo = planned[index].photo.clone(); + job.detail(photo.name.clone()); + report.current(&photo.name); + + let uploaded = + self.upload_takeout_photo(&mut archive, &planned[index], main_uid.clone()); + job.step(); + report.step(); + + match uploaded { + Ok(uid) => { + self.note_self_change(&uid); + summary.uploaded += 1; + summary.bytes += photo.size; + if main_uid.is_some() { + summary.live_photos += 1; + } + if photo.favorite { + favorites.push(uid.clone()); + } + planned[index].uid = Some(uid); + if summary.uploaded.is_multiple_of(TIMELINE_REFRESH_EVERY) { + self.spawn_timeline_refresh(); + } + if summary.uploaded.is_multiple_of(CHECKPOINT_EVERY) { + report.counts(summary); + report.checkpoint(); + } + } + Err(e) => { + summary.failed += 1; + warn!(name = photo.name, error = %e, "takeout photo upload failed"); + } + } + report.counts(summary); } } } - // Favorites are a separate call: the upload seal takes classification - // tags, but `Favorite` is not one of them — it goes through the photos - // volume's dedicated favorite endpoint. - if !favorites.is_empty() { - let updates: Vec = favorites - .into_iter() - .map(|node_uid| PhotoTagsUpdate { - node_uid, - tags_to_add: vec![PhotoTag::Favorite], - tags_to_remove: Vec::new(), - }) - .collect(); - if let Err(e) = self.rt.block_on(photos.update_photos(&updates)) { - warn!(error = %e, "marking imported favorites failed"); + self.mark_favorites(favorites); + stopped + } + + /// Upload one planned photo out of its open archive. + fn upload_takeout_photo( + &self, + archive: &mut zip::ZipArchive>, + item: &Planned, + main_uid: Option, + ) -> CoreResult { + let photo = &item.photo; + let photos = self.photos(); + let guard = self.transfers.begin( + photo.name.clone(), + "", + TransferDirection::Upload, + photo.size, + ); + let tags = match item.live { + Some(LiveRole::Still(tag)) => vec![tag], + _ => Vec::new(), + }; + let metadata = |capture_time: Option| PhotoUploadMetadata { + capture_time, + last_modification_time: capture_time.and_then(pdfs_core::timestamp::modification_time), + main_photo_uid: main_uid.clone(), + tags: tags.clone(), + location: photo + .location + .and_then(|p| PhotoLocation::from_claimed(Some(p.latitude), Some(p.longitude))), + // A draft under this name on the photos volume is this import's own, + // left by a run that was cut short — possibly under an earlier + // login, whose session id no longer matches. + replace_existing_draft: true, + }; + + // A thumbnail is made from a file, so a photo that can have one is + // copied out of the archive first and uploaded from the copy. Without + // one, Proton's apps show a blank tile until they make their own. + let staged = can_make_thumbnail(&photo.name, photo.size) + .then(|| self.stage_takeout_entry(archive, &photo.entry)) + .and_then(|staged| { + staged + .inspect_err(|e| { + warn!(name = photo.name, error = %e, + "copying a takeout photo out failed; uploading it without a thumbnail") + }) + .ok() + }); + let uploaded = match &staged { + Some(path) => { + let thumbnails = self.upload_thumbnails(path, &photo.name); + // No sidecar date and none in the name: the photo's own EXIF, + // then when Google received it, before Drive files it under + // today. + let capture_time = photo + .capture_time + .or_else(|| pdfs_core::exiftime::capture_time_of(path)) + .or(photo.creation_time); + std::fs::File::open(path) + .map_err(|e| CoreError::internal(format!("open {}: {e}", path.display()))) + .and_then(|file| { + let counting = CountingReader::new(std::io::BufReader::new(file), &guard); + self.rt + .block_on(photos.upload_photo_from( + &photo.name, + &photo.media_type, + counting, + photo.size as i64, + thumbnails, + metadata(capture_time), + false, + )) + .map_err(|e| CoreError::from_api(&e, "upload")) + }) } + None => read_entry(archive, &photo.entry, |reader| { + let counting = CountingReader::new(reader, &guard); + self.rt + .block_on(photos.upload_photo_from( + &photo.name, + &photo.media_type, + counting, + photo.size as i64, + Vec::new(), + metadata(photo.capture_time.or(photo.creation_time)), + false, + )) + .map_err(|e| CoreError::from_api(&e, "upload")) + }) + .and_then(|result| result), + }; + if let Some(path) = staged { + let _ = std::fs::remove_file(path); + } + uploaded + } + + /// Mark photos as favorites. A separate call: the upload seal takes + /// classification tags, but `Favorite` is not one of them — it goes through + /// the photos volume's dedicated favorite endpoint. + fn mark_favorites(&self, uids: Vec) { + if uids.is_empty() { + return; + } + let updates: Vec = uids + .into_iter() + .map(|node_uid| PhotoTagsUpdate { + node_uid, + tags_to_add: vec![PhotoTag::Favorite], + tags_to_remove: Vec::new(), + }) + .collect(); + if let Err(e) = self.rt.block_on(self.photos().update_photos(&updates)) { + warn!(error = %e, "marking imported favorites failed"); } - Ok(()) } /// Create the albums the export names that the account lacks, then add each @@ -461,6 +909,7 @@ impl Core { planned: &[Planned], albums: &[String], summary: &mut ImportSummary, + report: &Reporter, ) -> CoreResult<()> { let photos = self.photos(); @@ -482,6 +931,10 @@ impl Core { } for title in albums { + if self.shutdown.is_stopping() { + break; + } + report.step(); let members: Vec = planned .iter() .filter(|item| item.albums.iter().any(|album| album == title)) @@ -517,13 +970,14 @@ impl Core { // A photo already in the album is the expected // outcome of a re-run, not a failure worth counting. Err(e) => { - warn!(album = title, %uid, error = %e, "cannot add photo to album") + tracing::debug!(album = title, %uid, error = %e, "cannot add photo to album") } } } } Err(e) => warn!(album = title, error = %e, "album addition failed"), } + report.counts(summary); } Ok(()) } @@ -548,58 +1002,87 @@ impl Core { } state.running = true; state.cancel = Some(cancel.clone()); - state.last = None; + state.archives = archives.clone(); + state.dry_run = dry_run; + // A placeholder until the scan has counts, so a front-end can + // tell a scan from an import from the start. + state.summary = Some(ImportSummary { + dry_run, + ..Default::default() + }); + state.progress = Some(ImportProgress::default()); + state.save(); } let core = self.clone(); std::thread::spawn(move || { - let outcome = core.import_takeout(archives, dry_run, cancel); + // The job is dropped only after the final state is stored: the + // job leaving the transfer snapshot is the front-ends' cue to ask + // for the result, which must then be there. + let job = core.transfers.begin_job("Importing Google Photos"); + let run = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + core.import_takeout(&archives, dry_run, &cancel, &job) + })); + // A panic must not leave the import marked as running for the + // life of the daemon, refusing every later one. + let summary = run.unwrap_or_else(|_| ImportSummary { + dry_run, + error: Some("the import stopped on an internal error".to_string()), + error_kind: Some(pdfs_core::control::ErrorKind::Internal), + ..IMPORT.lock().summary.clone().unwrap_or_default() + }); { let mut state = IMPORT.lock(); state.running = false; state.cancel = None; - // A failed run is reported too: without it, `--wait` read - // "no import has run" and exited as if all was well. - state.last = Some(match &outcome { - Ok(summary) => summary.clone(), - Err(e) => ImportSummary { - error: Some(e.to_string()), - ..Default::default() - }, - }); + state.progress = None; + state.summary = Some(summary.clone()); + state.save(); } - match outcome { - Ok(summary) => { - let mut parts = vec![format!("{} uploaded", summary.uploaded)]; - if summary.duplicates > 0 { - parts.push(format!("{} already there", summary.duplicates)); - } - if summary.albums_created > 0 { - parts.push(format!("{} albums", summary.albums_created)); - } - if summary.failed > 0 { - parts.push(format!("{} failed", summary.failed)); - } - if summary.cancelled { - parts.push("cancelled".to_string()); - } - core.log_activity( - ActivityKind::Upload, - "Google Photos import", - parts.join(" · "), - summary.failed == 0 && !summary.cancelled, - ); - } - Err(e) => { - warn!(error = %e, "takeout import failed"); - core.log_activity(ActivityKind::Upload, "Google Photos import", &e, false); - } + drop(job); + if !dry_run { + core.log_activity( + ActivityKind::Upload, + "Google Photos import", + activity_line(&summary), + summary.complete(), + ); } }); Ok(()) } } +/// The activity-feed line for a finished import. The feed is the daemon's own +/// log, in English like the rest of it. +fn activity_line(summary: &ImportSummary) -> String { + let mut parts = vec![format!("{} uploaded", summary.uploaded)]; + if summary.duplicates > 0 { + parts.push(format!("{} already there", summary.duplicates)); + } + if summary.albums_created > 0 { + parts.push(format!("{} albums", summary.albums_created)); + } + if summary.failed > 0 { + parts.push(format!("{} failed", summary.failed)); + } + if !summary.unreadable_archives.is_empty() { + parts.push(format!( + "{} archives unreadable", + summary.unreadable_archives.len() + )); + } + if summary.interrupted { + parts.push("interrupted".to_string()); + } else if summary.cancelled { + parts.push("cancelled".to_string()); + } + if let Some(error) = &summary.error { + parts.push(error.clone()); + } + parts.join(" · ") +} + /// Open one of the import's archives by index. fn open_archive( archives: &[PathBuf], @@ -642,18 +1125,37 @@ pub(crate) fn hex_lower(bytes: &[u8]) -> String { #[cfg(test)] mod tests { use super::*; - use pdfs_core::takeout::TakeoutPhoto; + use pdfs_core::takeout::{GeoPoint, TakeoutPhoto}; fn photo(name: &str, size: u64, bucket: Bucket) -> TakeoutPhoto { + let folder = match &bucket { + Bucket::Album(title) => title.clone(), + _ => "Photos from 2019".to_string(), + }; + let (stem, ext) = name.rsplit_once('.').unwrap(); + let media_type = match ext.to_ascii_lowercase().as_str() { + "mov" | "mp4" | "mp" => "video/mp4", + _ => "image/jpeg", + }; TakeoutPhoto { archive: 0, - entry: format!("Takeout/Google Photos/x/{name}"), + entry: format!("Takeout/Google Photos/{folder}/{name}"), name: name.to_string(), size, - media_type: "image/jpeg".to_string(), + media_type: media_type.to_string(), capture_time: None, + creation_time: None, + location: None, favorite: false, bucket, + live_key: Some(format!( + "{folder}/{}", + if ext.eq_ignore_ascii_case("mp") { + name.to_lowercase() + } else { + stem.to_lowercase() + } + )), } } @@ -692,10 +1194,14 @@ mod tests { #[test] fn metadata_from_any_copy_survives_the_fold() { // Only the album copy carries a sidecar; the fold must not lose it. - let mut bare = photo("IMG_1.jpg", 100, Bucket::Timeline); - bare.capture_time = None; + let bare = photo("IMG_1.jpg", 100, Bucket::Timeline); let mut rich = photo("IMG_1.jpg", 100, Bucket::Album("Iceland".into())); rich.capture_time = Some(1_560_000_000); + rich.creation_time = Some(1_600_000_000); + rich.location = Some(GeoPoint { + latitude: 64.1, + longitude: -21.9, + }); rich.favorite = true; let planned = plan(&TakeoutScan { @@ -704,9 +1210,158 @@ mod tests { }); assert_eq!(planned[0].photo.capture_time, Some(1_560_000_000)); + assert_eq!(planned[0].photo.creation_time, Some(1_600_000_000)); + assert!(planned[0].photo.location.is_some()); assert!(planned[0].photo.favorite); } + #[test] + fn a_live_photo_video_is_filed_with_its_still_and_takes_its_date() { + let mut still = photo("IMG_1.HEIC", 100, Bucket::Timeline); + still.capture_time = Some(1_560_000_000); + still.location = Some(GeoPoint { + latitude: 64.1, + longitude: -21.9, + }); + let video = photo("IMG_1.MOV", 300, Bucket::Timeline); + let plain = photo("IMG_2.jpg", 100, Bucket::Timeline); + + let planned = plan(&TakeoutScan { + photos: vec![video, still, plain], + ..Default::default() + }); + + assert_eq!(planned[0].live, Some(LiveRole::Video(1))); + assert_eq!(planned[1].live, Some(LiveRole::Still(PhotoTag::LivePhoto))); + assert_eq!(planned[2].live, None); + assert_eq!(planned[0].photo.capture_time, Some(1_560_000_000)); + assert!(planned[0].photo.location.is_some()); + } + + #[test] + fn a_pixel_motion_photo_pairs_on_the_mp_name() { + let planned = plan(&TakeoutScan { + photos: vec![ + photo("PXL_1.MP.jpg", 100, Bucket::Timeline), + photo("PXL_1.MP", 300, Bucket::Timeline), + ], + ..Default::default() + }); + assert_eq!( + planned[0].live, + Some(LiveRole::Still(PhotoTag::MotionPhoto)) + ); + assert_eq!(planned[1].live, Some(LiveRole::Video(0))); + } + + #[test] + fn halves_taken_minutes_apart_are_not_a_pair() { + let mut still = photo("IMG_1.jpg", 100, Bucket::Timeline); + still.capture_time = Some(1_560_000_000); + let mut video = photo("IMG_1.MOV", 300, Bucket::Timeline); + video.capture_time = Some(1_560_000_000 + 3_600); + + let planned = plan(&TakeoutScan { + photos: vec![still, video], + ..Default::default() + }); + assert!(planned.iter().all(|p| p.live.is_none())); + } + + #[test] + fn an_album_copy_pairs_through_any_of_its_folders() { + // The still was first seen in an album, the video only in the year + // folder; the album copy of the still also sits in the year folder. + let planned = plan(&TakeoutScan { + photos: vec![ + photo("IMG_1.HEIC", 100, Bucket::Album("Trip".into())), + photo("IMG_1.HEIC", 100, Bucket::Timeline), + photo("IMG_1.MOV", 300, Bucket::Timeline), + ], + ..Default::default() + }); + assert_eq!(planned.len(), 2); + assert_eq!(planned[1].live, Some(LiveRole::Video(0))); + } + + #[test] + fn a_run_the_daemon_never_finished_loads_as_interrupted_and_resumable() { + let path = + std::env::temp_dir().join(format!("pdfs-takeout-state-{}.json", std::process::id())); + let saved = SavedImport { + archives: vec![PathBuf::from("/exports/takeout-001.zip")], + dry_run: false, + running: true, + summary: None, + }; + std::fs::write(&path, serde_json::to_vec(&saved).unwrap()).unwrap(); + + let state = ImportState::load(Some(path.clone())); + let _ = std::fs::remove_file(&path); + + assert!(!state.running); + assert!(state.summary.as_ref().unwrap().interrupted); + assert_eq!(state.resumable(), vec!["/exports/takeout-001.zip"]); + } + + #[test] + fn a_finished_import_or_a_scan_is_not_resumable() { + let done = ImportState { + archives: vec![PathBuf::from("/a.zip")], + summary: Some(ImportSummary { + uploaded: 3, + ..Default::default() + }), + ..Default::default() + }; + assert!(done.resumable().is_empty()); + + let scan = ImportState { + archives: vec![PathBuf::from("/a.zip")], + dry_run: true, + summary: Some(ImportSummary { + cancelled: true, + ..Default::default() + }), + ..Default::default() + }; + assert!(scan.resumable().is_empty()); + + let failed = ImportState { + archives: vec![PathBuf::from("/a.zip")], + summary: Some(ImportSummary { + failed: 1, + ..Default::default() + }), + ..Default::default() + }; + assert_eq!(failed.resumable(), vec!["/a.zip"]); + } + + #[test] + fn state_survives_a_save_and_load() { + let path = std::env::temp_dir().join(format!( + "pdfs-takeout-roundtrip-{}.json", + std::process::id() + )); + let state = ImportState { + archives: vec![PathBuf::from("/a.zip")], + summary: Some(ImportSummary { + uploaded: 2, + cancelled: true, + ..Default::default() + }), + path: Some(path.clone()), + ..Default::default() + }; + state.save(); + let loaded = ImportState::load(Some(path.clone())); + let _ = std::fs::remove_file(&path); + + assert_eq!(loaded.summary, state.summary); + assert_eq!(loaded.archives, state.archives); + } + #[test] fn sha1_matches_the_known_digest_of_abc() { let mut reader: &[u8] = b"abc"; diff --git a/crates/pdfs-gui/resources/style.css b/crates/pdfs-gui/resources/style.css index 16ade45..0a242f9 100644 --- a/crates/pdfs-gui/resources/style.css +++ b/crates/pdfs-gui/resources/style.css @@ -61,14 +61,28 @@ scale.browser-status-meter trough { min-width: 140px; min-height: 6px; } /* Photo viewer: always light-on-dark, whatever the system style */ .photo-viewer-window { background-color: #111014; } -.viewer-top-bar { background: linear-gradient(to bottom, rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0)); padding: 10px 16px 28px 20px; color: white; } +.media-player-window video { background-color: black; } +.media-player-note { color: rgba(255, 255, 255, 0.5); } +.media-player-note .file-thumbnail { border-radius: 12px; } +.viewer-top-bar { background: linear-gradient(to bottom, rgba(0, 0, 0, 0.85), rgba(0, 0, 0, 0.45) 60%, rgba(0, 0, 0, 0)); padding: 10px 16px 40px 20px; color: white; } .viewer-title { font-weight: 700; font-size: 1.05rem; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9); color: white; } .viewer-counter { font-size: 0.8rem; color: rgba(255, 255, 255, 0.72); text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9); } -.viewer-action-btn { color: white; background-color: rgba(255, 255, 255, 0.12); border-radius: 50%; min-width: 34px; min-height: 34px; margin-left: 4px; } -.viewer-action-btn:hover { background-color: rgba(255, 255, 255, 0.28); color: white; } +/* Dark discs with a white glyph read on a white photo as well as on a black one. */ +.viewer-action-btn { color: white; background-color: rgba(0, 0, 0, 0.5); border-radius: 50%; min-width: 36px; min-height: 36px; margin-left: 4px; -gtk-icon-shadow: 0 1px 2px rgba(0, 0, 0, 0.6); } +.viewer-action-btn:hover { background-color: rgba(0, 0, 0, 0.75); color: white; } .viewer-action-btn:checked { background-color: var(--accent-bg-color); color: var(--accent-fg-color); } -.viewer-close-btn { background-color: rgba(255, 255, 255, 0.2); margin-left: 10px; } +.viewer-close-btn { margin-left: 12px; } .viewer-close-btn:hover { background-color: var(--destructive-bg-color); color: var(--destructive-fg-color); } +.viewer-group-pill { color: white; background-color: rgba(0, 0, 0, 0.5); border-radius: 999px; min-height: 36px; padding: 0 14px; font-weight: 700; font-size: 0.85rem; } +.viewer-group-pill:hover { background-color: rgba(0, 0, 0, 0.75); color: white; } +.viewer-group-pill.lit { background-color: var(--accent-bg-color); color: var(--accent-fg-color); } +/* The thumbnail strip along the lightbox's bottom edge, the photo on screen ringed in the accent. */ +.viewer-strip-bar { background: linear-gradient(to top, rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0)); padding: 28px 12px 10px 12px; } +.viewer-strip { background: none; } +.viewer-strip > row { padding: 2px; margin: 0 2px; border-radius: 8px; background: none; } +.viewer-strip > row:hover { background-color: rgba(255, 255, 255, 0.15); } +.viewer-strip > row:selected { background-color: var(--accent-bg-color); } +.viewer-strip-thumb { border-radius: 6px; background-color: rgba(255, 255, 255, 0.08); } .viewer-nav-btn { background-color: rgba(0, 0, 0, 0.45); color: white; margin: 20px; min-width: 44px; min-height: 44px; border-radius: 50%; opacity: 0.7; transition: opacity 150ms ease, background-color 150ms ease; } .viewer-nav-btn:hover { background-color: rgba(0, 0, 0, 0.85); color: white; opacity: 1; } .viewer-nav-btn:disabled { opacity: 0; } diff --git a/crates/pdfs-gui/src/app/events.rs b/crates/pdfs-gui/src/app/events.rs index e951b1d..65e33f1 100644 --- a/crates/pdfs-gui/src/app/events.rs +++ b/crates/pdfs-gui/src/app/events.rs @@ -128,8 +128,22 @@ fn follow_changes(ui: &Rc, state: &FeedState, topics: &[Topic]) { if changed(Topic::Shares) { refresh_invitations_badge(ui, Duration::ZERO); } - match ui.stack.visible_child_name().as_deref() { - Some("browser") if changed(Topic::Files) => load_browser(ui), + // On any page: a grid, a list or a player may show a tile still blank. + if changed(Topic::Thumbnails) { + refresh_file_thumbnails(ui); + } + let page = ui.stack.visible_child_name(); + // A timeline out of sight is read again when the user comes back to it; + // one on screen is left alone (below). + if changed(Topic::Photos) && (page.as_deref() != Some("gallery") || !photo_grid_shown(ui)) { + ui.gallery.timeline_stale.set(true); + } + match page.as_deref() { + // A search on screen is left alone: reloading would swap its hits for + // the folder's listing while the query stays in the box. + Some("browser") if changed(Topic::Files) && ui.browser.search.text().trim().is_empty() => { + load_browser(ui) + } // The timeline is left alone: reloading it would throw away the place // the user scrolled to. F5 reloads it on request. Some("gallery") diff --git a/crates/pdfs-gui/src/app/main.rs b/crates/pdfs-gui/src/app/main.rs index 1678368..d3233a3 100644 --- a/crates/pdfs-gui/src/app/main.rs +++ b/crates/pdfs-gui/src/app/main.rs @@ -26,9 +26,12 @@ use pages::photo_viewer::*; use pages::photos::*; use pages::places::*; use pages::places_map::*; +use pages::preferences_apps::*; +use pages::preferences_sync::*; use pages::shared::*; use pages::shared_by_me::*; use pages::status::*; +use pages::sync_exclusions::*; use pages::takeout::*; use pages::trash::*; use pages::verify::*; @@ -39,7 +42,10 @@ use widgets::compat::*; use widgets::crumb::*; use widgets::details::*; use widgets::file_list::*; +use widgets::ignore_dialog::*; use widgets::menu::*; +use widgets::photo_drag::*; +use widgets::player::*; use widgets::scrubber::*; use widgets::share_dialog::*; use widgets::thumbnails::*; @@ -75,11 +81,11 @@ use pdfs_core::config::{AppDirs, FileSort, FilesView, PhotosView, WindowState}; use pdfs_core::control::{ ActivityEntry, ActivityKind, AlbumInfo, BookmarkInfo, ConflictInfo, ConflictKeep, DeviceInfo, - DirEntry, ErrorKind, ImportSummary, InvitationInfo, JobItem, LinkChange, MappingProgress, - PendingOpInfo, PhotoItem, PhotoKind, PhotoMonth, PlaceInfo, PublicLinkInfo, RefreshScope, - Request, Response, RestorableFolder, RestoreItem, RevisionInfo, SearchHit, ShareEntry, - ShareEntryKind, SharedItem, SyncFolderInfo, SyncIssue, SyncPhase, SyncProgress, - ThumbnailBuildStatus, Topic, TransferDirection, TransferItem, send, + DirEntry, ErrorKind, ImportPhase, ImportProgress, ImportSummary, InvitationInfo, JobItem, + LinkChange, MappingProgress, PendingOpInfo, PhotoItem, PhotoKind, PhotoMonth, PlaceInfo, + PublicLinkInfo, RefreshScope, Request, Response, RestorableFolder, RestoreItem, RevisionInfo, + SearchHit, ShareEntry, ShareEntryKind, SharedItem, SyncFolderInfo, SyncIssue, SyncPhase, + SyncProgress, ThumbnailBuildStatus, Topic, TransferDirection, TransferItem, send, }; use pdfs_core::mounts::{MountAccess, MountKind, MountMode, MountSpec}; @@ -295,6 +301,39 @@ fn parse_launch(args: &[String]) -> Launch { Launch::Window } +/// The Sync view a `--page` name opens: "activity" lives on as its History +/// view, and "sync-folders" is its Folders view. +fn sync_view(page: &str) -> Option<&'static str> { + match page { + "activity" => Some("history"), + "sync-folders" => Some("folders"), + _ => None, + } +} + +/// The Preferences page a `--page` name opens: "preferences" and +/// "preferences-" followed by the page's name. +fn preferences_page(page: &str) -> Option<&'static str> { + match page { + "preferences" | "preferences-general" => Some("general"), + "preferences-sync" => Some("sync"), + "preferences-apps" => Some("apps"), + "preferences-storage" => Some("storage"), + _ => None, + } +} + +/// The Photos view a `--page` name opens, and whether Places shows its map: +/// "albums", "places" and "places-map". Other names are stack pages. +fn photos_view(page: &str) -> Option<(u32, bool)> { + match page { + "albums" => Some((VIEW_ALBUMS, false)), + "places" => Some((VIEW_PLACES, false)), + "places-map" => Some((VIEW_PLACES, true)), + _ => None, + } +} + /// Raise the window if there is one, else build it. A second launch reaches /// the running instance, which must not open a second window. fn present_window(app: &adw::Application) { @@ -306,7 +345,15 @@ fn present_window(app: &adw::Application) { /// Spawn the tray icon process in the background. pub(crate) fn spawn_tray() { - match Command::new("pdfs-tray").spawn() { + // The tray installed next to this app, as the tray finds the app; the + // bare name for a PATH install. An install into ~/.local is often not on + // the PATH of a desktop session. + let tray = std::env::current_exe() + .ok() + .and_then(|p| p.parent().map(|d| d.join("pdfs-tray"))) + .filter(|p| p.exists()) + .unwrap_or_else(|| PathBuf::from("pdfs-tray")); + match Command::new(&tray).spawn() { Ok(child) => { tracing::info!("spawned `pdfs-tray`"); pdfs_core::opener::reap(child); @@ -457,6 +504,8 @@ fn build_window(app: &adw::Application) { files: browser_widgets.files.clone(), history: RefCell::new(Vec::new()), future: RefCell::new(Vec::new()), + places: RefCell::new(HashMap::new()), + restore: RefCell::new(None), crumb: browser_widgets.crumb.clone(), content: browser_widgets.content.clone(), status: browser_widgets.status.clone(), @@ -523,6 +572,7 @@ fn build_window(app: &adw::Application) { jump: Cell::new(None), burst: Cell::new(false), import_banner: gallery_widgets.import_banner.clone(), + upload_bar: gallery_widgets.upload_bar.clone(), upload: gallery_widgets.upload.clone(), empty_actions: gallery_widgets.empty_actions.clone(), title: gallery_widgets.title.clone(), @@ -632,6 +682,13 @@ fn build_window(app: &adw::Application) { views: locations_widgets.views.clone(), banner: locations_widgets.banner.clone(), failing: Cell::new(0), + exclusions: ExclusionsState { + group: locations_widgets.exclusions_group.clone(), + add: locations_widgets.add_pattern.clone(), + rows: RefCell::new(Vec::new()), + folders: RefCell::new(Vec::new()), + generation: Cell::new(0), + }, }, activity: ActivityState { content: activity_widgets.content.clone(), @@ -663,9 +720,11 @@ fn build_window(app: &adw::Application) { progress_bar: takeout_widgets.progress_bar.clone(), summary_group: takeout_widgets.summary_group.clone(), summary_rows: RefCell::new(Vec::new()), + resume_group: takeout_widgets.resume_group.clone(), + resume_row: takeout_widgets.resume_row.clone(), + resume_button: takeout_widgets.resume_button.clone(), inflight: Cell::new(false), running: Cell::new(false), - dry_run: Cell::new(false), }, update, }); @@ -682,6 +741,8 @@ fn build_window(app: &adw::Application) { &main_widgets.purge_button, &main_widgets.mountpoint_button, ); + wire_sync_prefs(&ui, main_widgets.sync_prefs); + wire_apps_prefs(&ui, main_widgets.apps_prefs); wire_sidebar(&ui); wire_browser(&ui); wire_bulk( @@ -711,6 +772,7 @@ fn build_window(app: &adw::Application) { wire_shared_by_me(&ui, &shared_by_me_widgets); wire_devices(&ui, &devices_widgets); wire_locations(&ui, &locations_widgets.retry, &locations_widgets.add_folder); + wire_exclusions(&ui); wire_activity(&ui, &activity_widgets); wire_takeout(&ui, &takeout_widgets); wire_retry(&ui); @@ -726,17 +788,26 @@ fn build_window(app: &adw::Application) { cancel_file_thumbnails(&ui_nav); sync_sidebar(&ui_nav); match st.visible_child_name().as_deref() { - Some("browser") => load_browser(&ui_nav), + Some("browser") => reload_browser_view(&ui_nav), // The Albums and Places listings are re-read in place. Loading the // timeline instead would swap it in under the Albums or Places toggle. + // The timeline, an album or a place comes back as it was left, + // scrolled where it was, unless photos changed while it was out of + // sight (see `follow_changes`) or nothing is loaded yet. Some("gallery") if !ui_nav.gallery.in_collection() => { match ui_nav.gallery.view_switch.active() { VIEW_ALBUMS => load_albums(&ui_nav), VIEW_PLACES => load_places(&ui_nav), - _ => load_gallery(&ui_nav, false), + _ if ui_nav.gallery.timeline_stale.replace(false) + || ui_nav.gallery.model.n_items() == 0 => + { + load_gallery(&ui_nav, false) + } + _ => {} } } - Some("gallery") => load_gallery(&ui_nav, false), + Some("gallery") if ui_nav.gallery.model.n_items() == 0 => load_gallery(&ui_nav, false), + Some("gallery") => {} // Every visit reads the page again. The rows already on screen stay // up while it does, so arriving costs no "Loading…" flash. Some("sharedbyme") => load_shared_by_me(&ui_nav), @@ -831,12 +902,10 @@ fn save_window_state(ui: &Ui, window: &adw::ApplicationWindow) { height, maximized: window.is_maximized(), }; - let mut config = ui.dirs.load_config(); - if config.window == state { + if ui.dirs.load_config().window == state { return; } - config.window = state; - if let Err(e) = ui.dirs.save_config(&config) { + if let Err(e) = ui.dirs.update_config(|config| config.window = state) { tracing::warn!("cannot save the window size: {e}"); } } @@ -1066,7 +1135,7 @@ fn reload_current_page(ui: &Rc) { match ui.stack.visible_child_name().as_deref() { Some("browser") => { let path = ui.browser.path.borrow().clone(); - refresh_then(ui, RefreshScope::Dir { path }, load_browser); + refresh_then(ui, RefreshScope::Dir { path }, reload_browser_view); } // One scope covers the whole photos view, so Refresh reloads whichever of // the views — a grid, or the timeline, an album or a place — is on screen. @@ -1328,7 +1397,7 @@ fn install_window_actions(ui: &Rc, window: &adw::ApplicationWindow) { // The Albums view's New Album button. let new_album = gio::SimpleAction::new("new-album", None); let ui_album = ui.clone(); - new_album.connect_activate(move |_, _| prompt_new_album(&ui_album, Vec::new())); + new_album.connect_activate(move |_, _| prompt_new_album(&ui_album, Vec::new(), None)); window.add_action(&new_album); let shortcuts = gio::SimpleAction::new("shortcuts", None); @@ -1434,10 +1503,21 @@ fn install_launch_actions(ui: &Rc, app: &adw::Application, window: &adw::App show_login(&ui_page); return; } - // Activity lives on as the Sync page's History view. - if page == "activity" { - ui_page.locations.views.set_visible_child_name("history"); + if let Some(name) = preferences_page(page) { + ui_page.status.prefs.set_visible_page_name(name); + ui_page.status.prefs.present(ui_window(&ui_page).as_ref()); + } else if let Some(view) = sync_view(page) { + ui_page.locations.views.set_visible_child_name(view); ui_page.stack.set_visible_child_name("locations"); + } else if let Some((view, map)) = photos_view(page) { + ui_page.stack.set_visible_child_name("gallery"); + if view == VIEW_PLACES { + ui_page + .gallery + .places_layout + .set_active(if map { PLACES_MAP } else { 0 }); + } + ui_page.gallery.view_switch.set_active(view); } else if ui_page.stack.child_by_name(page).is_some() { ui_page.stack.set_visible_child_name(page); } @@ -1506,7 +1586,7 @@ fn debug_info(ui: &Rc) -> String { /// The keyboard-shortcut cheatsheet behind the menu entry, listing exactly what /// [`install_shortcuts`], the Photos page and the photo viewer bind. fn show_shortcuts(window: &adw::ApplicationWindow) { - const GROUPS: [(&str, &[(&str, &str)]); 4] = [ + const GROUPS: [(&str, &[(&str, &str)]); 5] = [ ( gettext_noop("General"), &[ @@ -1539,6 +1619,7 @@ fn show_shortcuts(window: &adw::ApplicationWindow) { ("minus", gettext_noop("Smaller thumbnails")), ("0", gettext_noop("Reset thumbnail size")), ("a", gettext_noop("Select all")), + ("Escape Left", gettext_noop("Back")), ], ), ( @@ -1548,8 +1629,20 @@ fn show_shortcuts(window: &adw::ApplicationWindow) { ("Home End", gettext_noop("First / last photo")), ("i", gettext_noop("Show details")), ("f", gettext_noop("Fullscreen")), + ("F5", gettext_noop("Slideshow")), + ("F10", gettext_noop("More")), + ("c", gettext_noop("Copy Image")), ("Delete", gettext_noop("Move to Trash")), - ("Escape", gettext_noop("Close")), + ("Escape Left", gettext_noop("Close")), + ], + ), + ( + gettext_noop("Video Player"), + &[ + ("space k", gettext_noop("Play / pause")), + ("j l", gettext_noop("Back / forward 10 seconds")), + ("m", gettext_noop("Mute")), + ("f", gettext_noop("Fullscreen")), ], ), ]; @@ -1575,8 +1668,17 @@ fn install_shortcuts(ui: &Rc, window: &adw::ApplicationWindow) { controller.connect_key_pressed(move |_, key, _, state| { let ctrl = state.contains(gtk4::gdk::ModifierType::CONTROL_MASK); let alt = state.contains(gtk4::gdk::ModifierType::ALT_MASK); - let on_browser = ui.stack.visible_child_name().as_deref() == Some("browser"); - let on_trash = ui.stack.visible_child_name().as_deref() == Some("trash"); + let page = ui.stack.visible_child_name(); + let on_browser = page.as_deref() == Some("browser"); + let on_trash = page.as_deref() == Some("trash"); + let on_gallery = page.as_deref() == Some("gallery"); + // Pages with a level to go back to, besides My files' own history. + let in_collection = on_gallery && ui.gallery.in_collection(); + let in_shared_folder = + page.as_deref() == Some("shared") && !ui.shared.nav.borrow().is_empty(); + let in_device_folder = + page.as_deref() == Some("devices") && !ui.devices.nav.borrow().is_empty(); + let back_key = (key == gtk4::gdk::Key::Left && alt) || key == gtk4::gdk::Key::BackSpace; match key { // Refresh works on every page, so it is matched before the // browser-only bindings. @@ -1633,6 +1735,21 @@ fn install_shortcuts(ui: &Rc, window: &adw::ApplicationWindow) { gtk4::gdk::Key::BackSpace if on_browser => { ui.browser.actions.activate_action("back", None) } + // Selection mode ends before Escape leaves anything; the grid + // handles this itself while it has the focus. + gtk4::gdk::Key::Escape if on_gallery && ui.gallery.selecting.get() => { + set_selection_mode(&ui, false) + } + // An album or a place is a level below Photos, as a folder is + // below its parent: Back leaves it, and so does Escape. + _ if in_collection + && !ui.gallery.selecting.get() + && (back_key || key == gtk4::gdk::Key::Escape) => + { + ui.gallery.back.emit_clicked() + } + _ if in_shared_folder && back_key => shared_back(&ui), + _ if in_device_folder && back_key => devices_back(&ui), gtk4::gdk::Key::u | gtk4::gdk::Key::U if ctrl && on_browser => prompt_upload(&ui), gtk4::gdk::Key::Menu if visible_file_list(&ui).is_some() => { if let Some(list) = visible_file_list(&ui) { @@ -1961,6 +2078,27 @@ mod tests { assert_eq!(parse_launch(&args(&["--page"])), Launch::Window); } + #[test] + fn a_page_name_can_open_a_photos_or_sync_view() { + assert_eq!(photos_view("albums"), Some((VIEW_ALBUMS, false))); + assert_eq!(photos_view("places"), Some((VIEW_PLACES, false))); + assert_eq!(photos_view("places-map"), Some((VIEW_PLACES, true))); + // The timeline is the Photos stack page itself. + assert_eq!(photos_view("gallery"), None); + assert_eq!(sync_view("activity"), Some("history")); + assert_eq!(sync_view("sync-folders"), Some("folders")); + assert_eq!(sync_view("locations"), None); + } + + #[test] + fn a_page_name_can_open_a_preferences_page() { + assert_eq!(preferences_page("preferences"), Some("general")); + assert_eq!(preferences_page("preferences-sync"), Some("sync")); + assert_eq!(preferences_page("preferences-apps"), Some("apps")); + assert_eq!(preferences_page("preferences-storage"), Some("storage")); + assert_eq!(preferences_page("sync"), None); + } + /// One file of a shot, for the group-switch tests. fn member(uid: &str, name: Option<&str>, kind: PhotoKind) -> PhotoItem { PhotoItem { @@ -1974,6 +2112,7 @@ mod tests { favorite: false, group_size: 2, has_raw: kind == PhotoKind::Raw, + place_id: None, } } @@ -1996,6 +2135,40 @@ mod tests { assert!(next_group_member(&[], "jpeg").is_none()); } + /// The upload bar counts the upload under way, not the ones finished. + #[test] + fn the_upload_bar_counts_the_upload_under_way() { + assert_eq!(upload_progress_text(0, 12), "Uploading 1 of 12 items…"); + assert_eq!(upload_progress_text(11, 12), "Uploading 12 of 12 items…"); + } + + /// The pill names what kind of shot it switches through, and lights up + /// on the file that is not the shot's main one. + #[test] + fn the_group_pill_names_the_kind_of_shot() { + let pair = [ + member("jpeg", Some("IMG_1.JPG"), PhotoKind::Photo), + member("raw", Some("IMG_1.CR2"), PhotoKind::Raw), + ]; + assert_eq!(group_pill(&pair, "jpeg"), Some(("RAW".into(), false))); + assert_eq!(group_pill(&pair, "raw"), Some(("RAW".into(), true))); + + let live = [ + member("still", Some("IMG_2.HEIC"), PhotoKind::Photo), + member("clip", Some("IMG_2.MOV"), PhotoKind::Video), + ]; + assert_eq!(group_pill(&live, "still"), Some(("Live".into(), false))); + assert_eq!(group_pill(&live, "clip"), Some(("Live".into(), true))); + + let burst = [ + member("a", None, PhotoKind::Photo), + member("b", None, PhotoKind::Photo), + member("c", None, PhotoKind::Photo), + ]; + assert_eq!(group_pill(&burst, "b"), Some(("2 of 3".into(), false))); + assert_eq!(group_pill(&burst, "elsewhere"), None); + } + /// The switch names what it will show; a file whose name the daemon has not /// resolved still gets a sentence rather than a blank. #[test] diff --git a/crates/pdfs-gui/src/app/pages/albums.rs b/crates/pdfs-gui/src/app/pages/albums.rs index 6af6ffd..ce9ca1b 100644 --- a/crates/pdfs-gui/src/app/pages/albums.rs +++ b/crates/pdfs-gui/src/app/pages/albums.rs @@ -225,12 +225,13 @@ fn show_album_menu(ui: &Rc, album: &AlbumInfo, anchor: >k4::Widget, x: f64 /// while the entry is blank. fn prompt_album_name( ui: &Rc, + parent: Option, heading: &str, action: &str, current: &str, on_name: impl Fn(String) + 'static, ) { - let win = ui_window(ui); + let win = parent.or_else(|| ui_window(ui)); let dialog = adw::AlertDialog::builder().heading(heading).build(); let group = adw::PreferencesGroup::new(); let row = adw::EntryRow::builder() @@ -260,10 +261,12 @@ fn prompt_album_name( } /// Ask for a name and create an album, then add `photos` to it, if any. -pub(crate) fn prompt_new_album(ui: &Rc, photos: Vec) { +/// +/// The dialogs open over `parent`, or over the main window without one. +pub(crate) fn prompt_new_album(ui: &Rc, photos: Vec, parent: Option) { let ui_c = ui.clone(); let (heading, action) = (gettext("New Album"), gettext("Create")); - prompt_album_name(ui, &heading, &action, "", move |name| { + prompt_album_name(ui, parent, &heading, &action, "", move |name| { let rx = spawn_request(ui_c.dirs.control_socket(), Request::CreateAlbum { name }); let ui = ui_c.clone(); let photos = photos.clone(); @@ -296,7 +299,7 @@ pub(crate) fn prompt_new_album(ui: &Rc, photos: Vec) { fn prompt_rename_album(ui: &Rc, album: &AlbumInfo) { let (ui_c, uid) = (ui.clone(), album.uid.clone()); let (heading, action) = (gettext("Rename Album"), gettext("Rename")); - prompt_album_name(ui, &heading, &action, &album.name, move |name| { + prompt_album_name(ui, None, &heading, &action, &album.name, move |name| { run_album_request( &ui_c, Request::RenameAlbum { @@ -368,7 +371,7 @@ fn reload_album_grid(ui: &Rc) { } /// Offer our own albums to add `photos` to, plus a new one. -pub(crate) fn prompt_add_to_album(ui: &Rc, photos: Vec) { +pub(crate) fn prompt_add_to_album(ui: &Rc, photos: Vec, parent: Option) { let rx = spawn_request(ui.dirs.control_socket(), Request::PhotoAlbums); let ui = ui.clone(); glib::spawn_future_local(async move { @@ -390,7 +393,7 @@ pub(crate) fn prompt_add_to_album(ui: &Rc, photos: Vec) { } }; if albums.is_empty() { - prompt_new_album(&ui, photos); + prompt_new_album(&ui, photos, parent); return; } @@ -436,11 +439,11 @@ pub(crate) fn prompt_add_to_album(ui: &Rc, photos: Vec) { add_to_album(&ui_c, album.uid.clone(), photos_c.clone()); } }); - let ui_c = ui.clone(); + let (ui_c, parent_c) = (ui.clone(), parent.clone()); dialog.connect_response(Some("new"), move |_, _| { - prompt_new_album(&ui_c, photos.clone()) + prompt_new_album(&ui_c, photos.clone(), parent_c.clone()) }); - dialog.present(ui_window(&ui).as_ref()); + dialog.present(parent.or_else(|| ui_window(&ui)).as_ref()); }); } @@ -595,10 +598,7 @@ pub(crate) fn want_cover(ui: &Rc, uid: String, picture: >k4::Picture) { if ui.gallery.photo_nothumb.borrow().contains(&uid) { return; } - ui.gallery - .thumb_wanted - .borrow_mut() - .insert(uid.clone(), picture.clone()); + ui.gallery.await_thumb(&uid, picture); let mut queue = ui.gallery.thumb_queue.borrow_mut(); if !queue.contains(&uid) { queue.push_back(uid); @@ -664,12 +664,25 @@ pub(crate) fn wire_albums(ui: &Rc) { } close_album(&ui_photos); close_place(&ui_photos); - ui_photos.gallery.filters.set_visible(true); - ui_photos.gallery.content.set_visible_child_name("timeline"); + let gallery = &ui_photos.gallery; + gallery.filters.set_visible(true); + gallery.content.set_visible_child_name("timeline"); + // An album or a place cleared the kind and the month it does not use; + // the filter controls still show them, so the timeline comes back + // filtered the way they say. + let kind = kind_for_tab(gallery.tabs.active() as usize); + let range = gallery + .date_ranges + .borrow() + .get(gallery.dates.selected() as usize) + .copied() + .flatten(); + let kind_changed = gallery.kind.replace(kind) != kind; + let range_changed = gallery.range.replace(range) != range; + let refiltered = kind_changed || range_changed; // The timeline comes back as it was left unless an album took over the // model since; then it has to be reloaded. - if ui_photos.gallery.timeline_stale.replace(false) || ui_photos.gallery.model.n_items() == 0 - { + if gallery.timeline_stale.replace(false) || refiltered || gallery.model.n_items() == 0 { load_gallery(&ui_photos, false); } else { update_gallery_subtitle(&ui_photos); diff --git a/crates/pdfs-gui/src/app/pages/browser.rs b/crates/pdfs-gui/src/app/pages/browser.rs index a3d46a0..36cb539 100644 --- a/crates/pdfs-gui/src/app/pages/browser.rs +++ b/crates/pdfs-gui/src/app/pages/browser.rs @@ -1,6 +1,14 @@ use crate::activation::{DriveActivation, drive_activation, mounted_path, present_within}; use crate::*; +/// How a folder in My files was left: scrolled `offset` px down, with the +/// entries named `selected` highlighted. +#[derive(Clone, Debug, Default, PartialEq)] +pub(crate) struct FolderPlace { + pub(crate) offset: f64, + pub(crate) selected: Vec, +} + pub(crate) struct BrowserState { // Files (browser) page. /// The grid and list; the model is repopulated per directory. @@ -10,6 +18,13 @@ pub(crate) struct BrowserState { /// breadcrumb. pub(crate) history: RefCell>, pub(crate) future: RefCell>, + /// Where each folder left by Back, Forward, Up or a descent was scrolled + /// and what was selected in it, by path. Back, Forward and Up put the + /// folder back that way; opening it afresh starts at the top. + pub(crate) places: RefCell>, + /// The folder whose [`Self::places`] entry the next listing restores, set + /// by Back, Forward and Up. + pub(crate) restore: RefCell>, /// Clickable breadcrumb trail (a button per path segment); rebuilt per load /// by [`repaint_crumb`] so each ancestor folder navigates on click. pub(crate) crumb: gtk4::Box, @@ -883,27 +898,15 @@ fn show_location_column(ui: &Rc, visible: bool) { pub(crate) fn entry_context_menu(ui: &Rc, entry: &DirEntry) -> ActionMenu { let mut menu = ActionMenu::new(); let (ui_c, entry_c) = (ui.clone(), entry.clone()); + // Open does what a double-click does: a video or a song plays in the app, + // and Open With is the way to another player. menu.item(&pgettext("verb", "Open"), move || { - // Open always means "download a local copy and hand off", even for a - // media file — `activate_entry` would otherwise stream it. - if is_streamable_media_entry(&entry_c) { - download_and_open(&ui_c, &entry_c); - } else { - activate_entry(&ui_c, &entry_c); - } + activate_entry(&ui_c, &entry_c) }); let (ui_c, entry_c) = (ui.clone(), entry.clone()); menu.item(&gettext("Open With…"), move || { open_entry_with(&ui_c, &entry_c) }); - // Play streams from the mount, no download; Open above still fetches a - // local copy for anyone who wants one. - if is_streamable_media_entry(entry) { - let (ui_c, entry_c) = (ui.clone(), entry.clone()); - menu.item(&pgettext("verb", "Play"), move || { - stream_entry(&ui_c, &entry_c) - }); - } menu.section(); // Kept through a pinned folder above it, the entry has no pin of its own @@ -1092,38 +1095,8 @@ pub(crate) fn entry_at(model: Option<&impl IsA>, pos: u32) -> Op Some(entry) } -/// Whether an entry can be opened through FUSE instead of fully materialized. -pub(crate) fn is_streamable_media_entry(entry: &DirEntry) -> bool { - drive_activation(&entry.name, entry.is_dir) == DriveActivation::MountedMedia -} - -/// Stream media straight from the mount, no download. Drive folders *are* part -/// of the FUSE mount, so a player pointed at `/` reads the file -/// through [`Core::read_range_remote`] — 4 MB blocks fetched on demand as it -/// seeks and buffers — instead of waiting for the whole file to land like -/// [`Request::OpenFile`] does. This is the point of the feature: a 2 GB HEVC -/// `.mkv` starts playing in seconds. -pub(crate) fn stream_entry(ui: &Rc, entry: &DirEntry) { - let rel = entry_rel(ui, entry); - let mountpoint = ui.dirs.resolved_mountpoint(&ui.dirs.load_config()); - let abs = mounted_path(&mountpoint, &rel); - let Some(path) = abs.to_str() else { - toast_error( - ui, - &gettext("Couldn't play media"), - &gettext("The file path isn't valid UTF-8."), - ); - return; - }; - toast( - ui, - &gettext_f("Streaming “{name}”…", &[("name", &entry.name)]), - ); - play_external(path); -} - -/// Open an entry the Nautilus way: folders descend, media streams from the mount, -/// other files download-and-open. +/// Open an entry the Nautilus way: folders descend, media plays in the app, +/// streamed from the mount, other files download-and-open. pub(crate) fn activate_entry(ui: &Rc, entry: &DirEntry) { let rel = entry_rel(ui, entry); if entry.is_dir { @@ -1136,16 +1109,16 @@ pub(crate) fn activate_entry(ui: &Rc, entry: &DirEntry) { } else if drive_activation(&entry.name, entry.is_dir) == DriveActivation::MountedMedia { // Media streams rather than downloads: that is exactly the "play it, // don't fetch the whole thing" behaviour this is for. - stream_entry(ui, entry); + open_media_player(ui, entry); } else { download_and_open(ui, entry); } } /// Hand a file to the user's default application: through the mount when it is -/// there, otherwise downloaded into the cache first. The open path behind both -/// a plain double-click and the context menu's "Open" (including for a video, -/// when the user wants their default application rather than the player). +/// there, otherwise downloaded into the cache first. The open path behind a +/// double-click and the context menu's "Open" for anything but a folder or +/// media, and for media the mount doesn't show. /// /// The mount comes first because the cache blob is a *copy* keyed by content /// hash — an application saving into it writes where Drive will never look, and @@ -1458,9 +1431,11 @@ fn save_icon_size(ui: &Rc, size: i32) { let ui_save = ui.clone(); let source = glib::timeout_add_local_once(Duration::from_millis(500), move || { ui_save.browser.zoom_save.borrow_mut().take(); - let mut config = ui_save.dirs.load_config(); - config.files_view.icon_size = ui_save.browser.view.get().icon_size; - if let Err(e) = ui_save.dirs.save_config(&config) { + let icon_size = ui_save.browser.view.get().icon_size; + if let Err(e) = ui_save + .dirs + .update_config(|config| config.files_view.icon_size = icon_size) + { tracing::warn!("cannot save the icon size: {e}"); } }); @@ -1491,9 +1466,7 @@ pub(crate) fn set_files_view(ui: &Rc, view: FilesView) { return; } apply_files_view(ui, view); - let mut config = ui.dirs.load_config(); - config.files_view = view; - if let Err(e) = ui.dirs.save_config(&config) { + if let Err(e) = ui.dirs.update_config(|config| config.files_view = view) { toast_error(ui, &gettext("Couldn't save the view"), &e.to_string()); } if (old.sort, old.descending, old.folders_first) @@ -1556,8 +1529,29 @@ pub(crate) fn sort_entries(entries: &mut [DirEntry], view: FilesView) { }); } +/// Remember how the folder on screen is scrolled and what is selected in it, +/// for Back, Forward or Up to bring it back that way. Only a listing of the +/// folder itself counts; search hits are not its rows. +fn remember_place(ui: &Rc) { + let path = ui.browser.path.borrow().clone(); + if ui.browser.listed.borrow().as_deref() != Some(path.as_str()) { + return; + } + let files = &ui.browser.files; + let place = FolderPlace { + offset: files.scroll_offset(), + selected: files + .selected() + .into_iter() + .map(|entry| entry.name) + .collect(), + }; + ui.browser.places.borrow_mut().insert(path, place); +} + /// Open the folder on screen in a new place, remembering where we were. pub(crate) fn browse_to(ui: &Rc, target: String) { + remember_place(ui); let left = ui.browser.path.replace(target.clone()); if left != target { ui.browser.history.borrow_mut().push(left); @@ -1582,6 +1576,8 @@ fn browse_back(ui: &Rc) { let Some(previous) = ui.browser.history.borrow_mut().pop() else { return; }; + remember_place(ui); + *ui.browser.restore.borrow_mut() = Some(previous.clone()); let left = ui.browser.path.replace(previous); ui.browser.future.borrow_mut().push(left); load_browser(ui); @@ -1592,6 +1588,8 @@ fn browse_forward(ui: &Rc) { return; }; ui.browser.search.set_text(""); + remember_place(ui); + *ui.browser.restore.borrow_mut() = Some(next.clone()); let left = ui.browser.path.replace(next); ui.browser.history.borrow_mut().push(left); load_browser(ui); @@ -1604,6 +1602,7 @@ fn browse_up(ui: &Rc) { } ui.browser.search.set_text(""); let parent = path.rfind('/').map(|i| &path[..i]).unwrap_or_default(); + *ui.browser.restore.borrow_mut() = Some(parent.to_string()); browse_to(ui, parent.to_string()); } @@ -2921,6 +2920,18 @@ pub(crate) fn load_browser(ui: &Rc) { }); } +/// Read again what My files shows: the search hits while there is a query, +/// the folder otherwise. [`load_browser`] alone would swap a search's hits +/// for the folder while the query stays in the box. +pub(crate) fn reload_browser_view(ui: &Rc) { + let query = ui.browser.search.text().trim().to_string(); + if query.is_empty() { + load_browser(ui); + } else { + run_search(ui, &query); + } +} + /// Clear the model and show the daemon's error on the status page. Used for /// in-band failures (a bad path, a permission error) — the mount is up, so Retry /// (which restarts the service) wouldn't help and isn't offered. @@ -2959,7 +2970,10 @@ pub(crate) fn browser_unreachable(ui: &Rc) { pub(crate) fn repaint_browser(ui: &Rc, entries: &[DirEntry]) { show_location_column(ui, false); *ui.browser.listing.borrow_mut() = entries.to_vec(); - *ui.browser.listed.borrow_mut() = Some(ui.browser.path.borrow().clone()); + // `listed` still names the folder the rows belong to only on a refresh; + // any other load forgot it (see `load_browser`). + let path = ui.browser.path.borrow().clone(); + let another_folder = ui.browser.listed.replace(Some(path.clone())).as_ref() != Some(&path); ui.browser.summary.set_label(&listing_summary( entries.iter().filter(|entry| !entry.is_dir).count(), entries.iter().filter(|entry| entry.is_dir).count(), @@ -2988,6 +3002,16 @@ pub(crate) fn repaint_browser(ui: &Rc, entries: &[DirEntry]) { // Only the rows that changed are swapped, so a refresh keeps the // selection, the scroll position and the thumbnails already drawn. replace_items(&ui.browser.files.model, &sorted); + // Back, Forward and Up return to a folder as it was left; any other way + // in starts at the top. + let restore = ui.browser.restore.take().filter(|target| *target == path); + if another_folder { + let place = restore.and_then(|path| ui.browser.places.borrow().get(&path).cloned()); + match place { + Some(place) => ui.browser.files.restore(place.offset, &place.selected), + None => ui.browser.files.scroll_to_top(), + } + } } /// Forget which folder the rows belong to and drop the selection, before the @@ -3166,6 +3190,9 @@ pub(crate) fn repaint_search(ui: &Rc, hits: &[SearchHit]) { .collect(); sort_entries(&mut entries, ui.browser.view.get()); replace_items(&ui.browser.files.model, &entries); + // Every search starts over (`run_search` forgets the listing), so its hits + // read from the first one. + ui.browser.files.scroll_to_top(); } fn listing_summary(files: usize, folders: usize, file_bytes: u64) -> String { diff --git a/crates/pdfs-gui/src/app/pages/devices.rs b/crates/pdfs-gui/src/app/pages/devices.rs index 4956266..4628f21 100644 --- a/crates/pdfs-gui/src/app/pages/devices.rs +++ b/crates/pdfs-gui/src/app/pages/devices.rs @@ -190,10 +190,9 @@ pub(crate) fn wire_devices(ui: &Rc, widgets: &DevicesWidgets) { })); let ui_back = ui.clone(); - widgets.back.connect_clicked(move |_| { - ui_back.devices.nav.borrow_mut().pop(); - load_devices(&ui_back); - }); + widgets + .back + .connect_clicked(move |_| devices_back(&ui_back)); let ui_retry = ui.clone(); widgets .retry @@ -211,6 +210,14 @@ pub(crate) fn devices_status(ui: &Rc, icon: &str, title: &str, description: ui.devices.content.set_visible_child_name("status"); } +/// Back out of a computer's folder to the level above: the page's Back button, and +/// Alt+Left or Backspace. A no-op at the top. +pub(crate) fn devices_back(ui: &Rc) { + if ui.devices.nav.borrow_mut().pop().is_some() { + load_devices(ui); + } +} + /// Load the view [`DevicesState::nav`] points at: the device list, or a folder /// of another computer's backup. pub(crate) fn load_devices(ui: &Rc) { @@ -297,6 +304,7 @@ fn load_device_folder(ui: &Rc, nav: &[(String, String)]) { }; ui.devices.content.set_visible_child_name("browse"); repaint_device_crumb(&ui, &nav); + let another_folder = ui.devices.listed.borrow().as_ref() != Some(&nav); *ui.devices.listed.borrow_mut() = Some(nav); if entries.is_empty() { ui.devices.files.model.remove_all(); @@ -304,6 +312,10 @@ fn load_device_folder(ui: &Rc, nav: &[(String, String)]) { } else { ui.devices.files_content.set_visible_child_name("list"); replace_items(&ui.devices.files.model, &entries); + // A refresh keeps its place; another folder starts at the top. + if another_folder { + ui.devices.files.scroll_to_top(); + } } }); } diff --git a/crates/pdfs-gui/src/app/pages/locations.rs b/crates/pdfs-gui/src/app/pages/locations.rs index b6cf862..07a9edc 100644 --- a/crates/pdfs-gui/src/app/pages/locations.rs +++ b/crates/pdfs-gui/src/app/pages/locations.rs @@ -35,6 +35,8 @@ pub(crate) struct LocationsState { pub(crate) banner: adw::Banner, /// Changes the daemon failed to upload, from the last status. pub(crate) failing: Cell, + /// Excluded Files, under the folder list. + pub(crate) exclusions: ExclusionsState, } /// The status card heading the Sync page's Overview, and Pause/Resume in the @@ -120,6 +122,8 @@ pub(crate) struct LocationsWidgets { pub(crate) conflicts_group: adw::PreferencesGroup, pub(crate) views: adw::ViewStack, pub(crate) banner: adw::Banner, + pub(crate) exclusions_group: adw::PreferencesGroup, + pub(crate) add_pattern: gtk4::Button, } /// The Sync page: Overview (the status, conflicts, transfers and queue), @@ -208,8 +212,14 @@ pub(crate) fn build_locations_page(history: >k4::Widget) -> (gtk4::Widget, Loc .child(&status) .build(); + // What the folders leave out, under them: the rules belong to the folders. + let (exclusions_group, add_pattern) = build_exclusions_group(); + let folders = gtk4::Box::new(gtk4::Orientation::Vertical, 24); + folders.append(&group); + folders.append(&exclusions_group); + let content = gtk4::Stack::new(); - content.add_named(&scrolled_column(&group), Some("list")); + content.add_named(&scrolled_column(&folders), Some("list")); content.add_named(&status_scroll, Some("status")); let views = adw::ViewStack::new(); @@ -267,6 +277,8 @@ pub(crate) fn build_locations_page(history: >k4::Widget) -> (gtk4::Widget, Loc conflicts_group, views, banner, + exclusions_group, + add_pattern, }; (frame.upcast(), widgets) } @@ -1243,6 +1255,7 @@ pub(crate) fn repaint_locations(ui: &Rc, items: &[MountSpec]) { }; if !unchanged { rebuild_locations(ui, items, keys); + paint_exclusions(ui, items); } for (row, spec) in ui.locations.rows.borrow().iter().zip(items) { update_location_row(row, spec); @@ -1498,6 +1511,7 @@ fn folder_mode_label(ondemand: bool) -> String { /// synced folder's menu. fn add_device_items(ui: &Rc, menu: &mut ActionMenu, spec: &MountSpec, id: i64) { if spec.mode != MountMode::OnDemand { + let ui_ignore = ui.clone(); // A paused folder skips its passes, so Sync now would do nothing. if !spec.paused { let ui = ui.clone(); @@ -1516,6 +1530,14 @@ fn add_device_items(ui: &Rc, menu: &mut ActionMenu, spec: &MountSpec, id: i6 menu.item(&gettext("Sync From Scratch…"), move || { confirm_reset_sync_folder(&ui, id, &path) }); + // Only a synced copy has local files for the rules to leave out. + if spec.mode == MountMode::Mirror { + let ui = ui_ignore; + let root = PathBuf::from(&spec.local_path); + menu.item(&gettext("Excluded Files…"), move || { + present_ignore_dialog(&ui, id, root.clone()) + }); + } menu.section(); } let ui = ui.clone(); diff --git a/crates/pdfs-gui/src/app/pages/mod.rs b/crates/pdfs-gui/src/app/pages/mod.rs index b89ef4e..fd1a8bf 100644 --- a/crates/pdfs-gui/src/app/pages/mod.rs +++ b/crates/pdfs-gui/src/app/pages/mod.rs @@ -12,9 +12,12 @@ pub(crate) mod photo_viewer; pub(crate) mod photos; pub(crate) mod places; pub(crate) mod places_map; +pub(crate) mod preferences_apps; +pub(crate) mod preferences_sync; pub(crate) mod shared; pub(crate) mod shared_by_me; pub(crate) mod status; +pub(crate) mod sync_exclusions; pub(crate) mod takeout; pub(crate) mod trash; pub(crate) mod verify; diff --git a/crates/pdfs-gui/src/app/pages/photo_viewer.rs b/crates/pdfs-gui/src/app/pages/photo_viewer.rs index 7866633..793c136 100644 --- a/crates/pdfs-gui/src/app/pages/photo_viewer.rs +++ b/crates/pdfs-gui/src/app/pages/photo_viewer.rs @@ -52,9 +52,9 @@ pub(crate) struct Viewer { /// uses for its pin switch, so painting a photo doesn't fire a round-trip. pub(crate) favorite: gtk4::ToggleButton, pub(crate) favorite_suppress: Cell, - /// The button that switches between the files of one shot, and the files it + /// The pill that switches between the files of one shot, and the files it /// switches between (the JPEG and the RAW of a photo, a live photo and its - /// clip). Empty, and the button hidden, for a photo stored as one file. + /// clip). Empty, and the pill hidden, for a photo stored as one file. pub(crate) group_btn: gtk4::Button, pub(crate) group: RefCell>, /// uid of the photo currently on screen. @@ -81,14 +81,25 @@ pub(crate) struct Viewer { /// rests; whether the pointer is over one of them (which keeps them up); /// and the pending fade-out. pub(crate) chrome: Vec, + /// The strip of thumbnails along the bottom, its selection (the photo on + /// screen), and the revealer it fades with. Hidden in a slideshow. + pub(crate) strip: gtk4::ListView, + pub(crate) strip_selection: gtk4::SingleSelection, + pub(crate) strip_reveal: gtk4::Revealer, pub(crate) chrome_hover: Cell, pub(crate) chrome_source: RefCell>, - /// The slideshow toggle; the timer that moves it on; and whether it put - /// the window in fullscreen, so stopping it only leaves a fullscreen it + /// The ⋮ button, and whether its menu is open, which keeps the controls + /// up the way resting the pointer on them does. + pub(crate) more: gtk4::Button, + pub(crate) menu_open: Cell, + /// Whether a slideshow runs; the timer that moves it on; and whether it + /// put the window in fullscreen, so stopping it only leaves a fullscreen it /// entered itself. - pub(crate) slideshow: gtk4::ToggleButton, + pub(crate) slideshow: Cell, pub(crate) slide_source: RefCell>, pub(crate) slide_fullscreened: Cell, + /// Toasts over the lightbox. The main window's would land behind it. + pub(crate) toasts: adw::ToastOverlay, } /// Camera/exposure/location facts pulled from a photo's own EXIF tags, as @@ -163,6 +174,14 @@ fn load_photo_group(ui: &Rc, viewer: &Rc, uid: &str) { .group_btn .set_tooltip_text(Some(&member_tooltip(next))); } + if let Some((label, lit)) = group_pill(&items, &uid) { + viewer.group_btn.set_label(&label); + if lit { + viewer.group_btn.add_css_class("lit"); + } else { + viewer.group_btn.remove_css_class("lit"); + } + } *viewer.group.borrow_mut() = items; viewer.group_btn.set_visible(true); }); @@ -175,6 +194,34 @@ pub(crate) fn next_group_member<'a>(members: &'a [PhotoItem], uid: &str) -> Opti members.get((at + 1) % members.len()) } +/// What the group pill reads for the shot whose files are `members`, with +/// `uid` on screen, and whether it is lit. A raw pair reads "RAW", lit while +/// the raw file is shown; a live photo reads "Live", lit while its clip plays; +/// a burst counts its frames. +pub(crate) fn group_pill(members: &[PhotoItem], uid: &str) -> Option<(String, bool)> { + let at = members.iter().position(|item| item.uid == uid)?; + let shown = members[at].kind; + if members.iter().any(|item| item.kind == PhotoKind::Raw) { + // Translators: badge on a photo tile whose shot includes a raw camera file. + return Some((gettext("RAW"), shown == PhotoKind::Raw)); + } + if members.len() == 2 && members.iter().any(|item| item.kind == PhotoKind::Video) { + // Translators: the switch between a live photo and its short clip in the photo viewer. + return Some((pgettext("live photo", "Live"), shown == PhotoKind::Video)); + } + Some(( + // Translators: the lightbox counter, such as "12 of 340"; {position} is the photo's place, {total} how many there are. + gettext_f( + "{position} of {total}", + &[ + ("position", &thousands(at + 1)), + ("total", &thousands(members.len())), + ], + ), + false, + )) +} + /// What to call one file of a shot: its name, or its kind when the daemon has /// not resolved a name for it. pub(crate) fn member_label(item: &PhotoItem) -> String { @@ -232,6 +279,7 @@ pub(crate) fn load_photo(ui: &Rc, viewer: &Rc, uid: String) { clear_info(viewer); stop_video(viewer); viewer.media.set_visible_child_name("photo"); + sync_strip(viewer); reset_zoom(viewer); // A slideshow waits for this photo to land before it starts counting. disarm_slide(viewer); @@ -309,6 +357,7 @@ pub(crate) fn load_photo(ui: &Rc, viewer: &Rc, uid: String) { None => { viewer.video.set_filename(Some(&path)); viewer.media.set_visible_child_name("video"); + sync_strip(&viewer); *viewer.path.borrow_mut() = Some(path.clone()); show_info(&viewer, size, ExifInfo::default()); } @@ -341,14 +390,15 @@ pub(crate) fn load_photo(ui: &Rc, viewer: &Rc, uid: String) { /// Start the slideshow: fullscreen, controls out of the way, and the photo on /// screen counted down once it has loaded. fn start_slideshow(ui: &Rc, viewer: &Rc) { + if viewer.slideshow.replace(true) { + return; + } if !viewer.window.is_fullscreen() { viewer.window.fullscreen(); viewer.slide_fullscreened.set(true); } - viewer - .slideshow - .set_icon_name("media-playback-pause-symbolic"); hide_chrome(viewer); + sync_strip(viewer); if !viewer.loading.get() { arm_slide(ui, viewer); } @@ -356,13 +406,20 @@ fn start_slideshow(ui: &Rc, viewer: &Rc) { /// Stop the slideshow, leaving fullscreen only if the slideshow entered it. fn stop_slideshow(viewer: &Rc) { + viewer.slideshow.set(false); + sync_strip(viewer); disarm_slide(viewer); if viewer.slide_fullscreened.replace(false) { viewer.window.unfullscreen(); } - viewer.slideshow.set_icon_name("pdfs-slideshow-symbolic"); - if viewer.slideshow.is_active() { - viewer.slideshow.set_active(false); +} + +/// Start the slideshow, or stop the one running. +fn toggle_slideshow(ui: &Rc, viewer: &Rc) { + if viewer.slideshow.get() { + stop_slideshow(viewer); + } else { + start_slideshow(ui, viewer); } } @@ -370,7 +427,7 @@ fn stop_slideshow(viewer: &Rc) { /// [`SLIDESHOW_INTERVAL`] for a still, once it has played through for a video. fn arm_slide(ui: &Rc, viewer: &Rc) { disarm_slide(viewer); - if !viewer.slideshow.is_active() { + if !viewer.slideshow.get() { return; } let playing_video = viewer.media.visible_child_name().as_deref() == Some("video"); @@ -790,7 +847,7 @@ fn show_chrome(viewer: &Rc) { }; // This timer has fired, so its id must not be removed again. viewer.chrome_source.borrow_mut().take(); - if !viewer.chrome_hover.get() { + if !viewer.chrome_hover.get() && !viewer.menu_open.get() { hide_chrome(&viewer); } }); @@ -808,14 +865,14 @@ fn hide_chrome(viewer: &Rc) { } /// Copy the photo on screen to where the user picks, under its Drive name. -pub(crate) fn save_photo_to_disk(ui: &Rc, window: >k4::Window, source: &str, name: &str) { +pub(crate) fn save_photo_to_disk(viewer: &Rc, source: &str, name: &str) { let dialog = gtk4::FileDialog::builder() .title(gettext("Save a Copy")) .initial_name(name) .build(); let source = source.to_string(); - let ui = ui.clone(); - dialog.save(Some(window), gio::Cancellable::NONE, move |res| { + let toasts = viewer.toasts.clone(); + dialog.save(Some(&viewer.window), gio::Cancellable::NONE, move |res| { // An error here is the dialog being dismissed. let Some(dest) = res.ok().and_then(|file| file.path()) else { return; @@ -824,26 +881,67 @@ pub(crate) fn save_photo_to_disk(ui: &Rc, window: >k4::Window, source: &st glib::spawn_future_local(async move { let copied = gio::spawn_blocking(move || std::fs::copy(&source, &dest).map(|_| dest)).await; - match copied { + let message = match copied { Ok(Ok(dest)) => { let saved = dest.file_name().map_or_else( || dest.display().to_string(), |name| name.to_string_lossy().into_owned(), ); // Translators: {name} is the saved file's name. - toast(&ui, &gettext_f("Saved “{name}”", &[("name", &saved)])); + gettext_f("Saved “{name}”", &[("name", &saved)]) } - Ok(Err(e)) => toast_error(&ui, &gettext("Couldn't save the copy"), &e.to_string()), - Err(_) => toast_error( - &ui, + Ok(Err(e)) => failure(&gettext("Couldn't save the copy"), &e.to_string()), + Err(_) => failure( &gettext("Couldn't save the copy"), &gettext("The copy was interrupted."), ), - } + }; + toasts.add_toast( + adw::Toast::builder() + .title(&message) + .use_markup(false) + .build(), + ); }); }); } +/// A failed action's toast text, worded like [`toast_error`]'s. +fn failure(what: &str, detail: &str) -> String { + tracing::warn!("{what}: {detail}"); + // Translators: a failed action's toast. {what} says what failed, such as + // "Couldn't rename"; {detail} is the reason, usually untranslated text + // from the mount service. + gettext_f("{what}: {detail}", &[("what", what), ("detail", detail)]) +} + +/// Put the photo on screen on the clipboard as an image, for pasting into a +/// chat or a document. +fn copy_shown_photo(_ui: &Rc, viewer: &Rc) { + let Some(texture) = viewer + .picture + .paintable() + .and_downcast::() + else { + return; + }; + viewer.window.clipboard().set_texture(&texture); + viewer.toasts.add_toast( + adw::Toast::builder() + // Translators: toast after the photo viewer put the photo on the clipboard. + .title(gettext("Image copied")) + .use_markup(false) + .build(), + ); +} + +/// Open the Share dialog for the photo on screen, over the lightbox. +fn share_shown_photo(ui: &Rc, viewer: &Rc) { + let uid = viewer.uid.borrow().clone(); + let name = viewer.name.borrow().clone().unwrap_or_default(); + share_photo(ui, uid, name, Some(viewer.window.clone())); +} + /// A symlink to the cached file `path` under its Drive `name`, in a directory /// of its own below `base`. The cache names files by content hash, with no /// extension; the app chooser and the app it launches both need the real name @@ -983,16 +1081,124 @@ pub(crate) fn position_label(index: u32, loaded: u32, more: bool, total: Option< } } -/// Set the lightbox's title (the photo's date) and its "12 of 340" counter. +/// The lightbox's title: when the photo was taken, as "Wednesday, October 7, +/// 2026, 13:42". +fn photo_title(capture_time: i64) -> String { + let title = dates::full(capture_time); + if title.is_empty() { + gettext("Unknown Date") + } else { + title + } +} + +/// Set the lightbox's title (the photo's date) and the line under it: the town +/// it was taken in, when known, and its "12 of 340" counter. fn show_photo_position(ui: &Rc, viewer: &Rc, index: u32, capture_time: i64) { let loaded = ui.gallery.model.n_items(); let more = ui.gallery.has_more.get(); viewer.prev.set_sensitive(index > 0); viewer.next.set_sensitive(index + 1 < loaded || more); - viewer.title.set_label(&format_capture_time(capture_time)); + viewer.title.set_label(&photo_title(capture_time)); + let position = position_label(index, loaded, more, gallery_total(ui)); + let place = viewer_item(ui, viewer, &viewer.uid.borrow()) + .and_then(|item| item.place_id) + .and_then(pdfs_core::places::city) + .map(|city| city.name); + let line = match place { + Some(place) => { + let args = [("place", place), ("position", position.as_str())]; + // Translators: the line under the photo viewer's title, such as "Berlin · 12 of 340"; {place} is a town, {position} the counter. + gettext_f("{place} · {position}", &args) + } + None => position, + }; + viewer.counter.set_label(&line); + sync_strip(viewer); + mark_in_strip(viewer, index); +} + +/// Show the strip of thumbnails when there is more than one photo to pick +/// from. Not in a slideshow, and not over a video, where it would cover the +/// player's seek bar. +fn sync_strip(viewer: &Rc) { + let video = viewer.media.visible_child_name().as_deref() == Some("video"); viewer - .counter - .set_label(&position_label(index, loaded, more, gallery_total(ui))); + .strip_reveal + .set_visible(viewer.strip_selection.n_items() > 1 && !viewer.slideshow.get() && !video); +} + +/// Mark the photo at `index` in the strip and bring it into view. +fn mark_in_strip(viewer: &Rc, index: u32) { + if index >= viewer.strip_selection.n_items() { + return; + } + viewer.strip_selection.set_selected(index); + viewer + .strip + .scroll_to(index, gtk4::ListScrollFlags::NONE, None); +} + +/// The strip of thumbnails along the lightbox's bottom edge: the gallery's +/// photos in its order, the one on screen marked. A click steps straight to a +/// photo; the thumbnails come through the gallery's own pipeline and cache. +fn build_strip(ui: &Rc) -> (gtk4::ListView, gtk4::SingleSelection) { + let selection = gtk4::SingleSelection::new(Some(ui.gallery.model.clone())); + selection.set_autoselect(false); + selection.set_can_unselect(true); + + let factory = gtk4::SignalListItemFactory::new(); + factory.connect_setup(|_, item| { + let item = item.downcast_ref::().unwrap(); + let picture = gtk4::Picture::builder() + .content_fit(gtk4::ContentFit::Cover) + .width_request(56) + .height_request(56) + .can_shrink(true) + .build(); + picture.add_css_class("viewer-strip-thumb"); + // The arrow keys belong to the lightbox, not to the strip. + item.set_focusable(false); + item.set_child(Some(&picture)); + }); + let ui_bind = ui.clone(); + factory.connect_bind(move |_, item| { + let item = item.downcast_ref::().unwrap(); + let Some(picture) = item.child().and_downcast::() else { + return; + }; + let Some(boxed) = item.item().and_downcast::() else { + return; + }; + let photo = boxed.borrow::().clone(); + picture.set_paintable(gtk4::gdk::Paintable::NONE); + picture.set_tooltip_text(Some(&dates::full(photo.capture_time))); + want_thumb(&ui_bind, &photo, &picture); + schedule_thumbs(&ui_bind); + }); + let ui_unbind = ui.clone(); + factory.connect_unbind(move |_, item| { + let item = item.downcast_ref::().unwrap(); + let (Some(picture), Some(boxed)) = ( + item.child().and_downcast::(), + item.item().and_downcast::(), + ) else { + return; + }; + let uid = boxed.borrow::().uid.clone(); + ui_unbind + .gallery + .release_thumb(&uid, |waiting| waiting == &picture); + }); + + let list = gtk4::ListView::builder() + .model(&selection) + .factory(&factory) + .orientation(gtk4::Orientation::Horizontal) + .focusable(false) + .build(); + list.add_css_class("viewer-strip"); + (list, selection) } /// The in-app lightbox: the photo or video, edge-to-edge on a dark backdrop, @@ -1001,7 +1207,10 @@ fn show_photo_position(ui: &Rc, viewer: &Rc, index: u32, capture_tim /// /// Closing it is deliberately hard to get wrong — Escape, `q`, Ctrl+W, the close /// button, or a click on the backdrop beside the photo all dismiss it. -pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { +/// +/// Returns the lightbox's window, for a caller that has to put the page +/// behind it back once it closes. +pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) -> gtk4::Window { let parent = ui.stack.root().and_downcast::().unwrap(); // An `adw::Window` for its breakpoints: the details panel lies over the @@ -1038,6 +1247,7 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { .hexpand(true) .vexpand(true) .build(); + wire_player(&video, &window); let media = gtk4::Stack::new(); media.add_named(&scroller, Some("photo")); media.add_named(&video, Some("video")); @@ -1122,22 +1332,21 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { favorite_btn.add_css_class("flat"); favorite_btn.add_css_class("viewer-action-btn"); - let slideshow_btn = gtk4::ToggleButton::builder() - .icon_name("pdfs-slideshow-symbolic") - .tooltip_text(gettext("Slideshow (F5)")) - .valign(gtk4::Align::Center) - .build(); - slideshow_btn.add_css_class("flat"); - slideshow_btn.add_css_class("viewer-action-btn"); - // Only shown for a shot stored as more than one file; `load_photo_group` - // decides that per photo. - let group_btn = action("view-paged-symbolic", &gettext("Other files of this photo")); - group_btn.set_visible(false); + // decides that per photo and labels it ("RAW", "Live", "2 of 3"). + let group_btn = gtk4::Button::builder() + .valign(gtk4::Align::Center) + .visible(false) + .build(); + group_btn.add_css_class("flat"); + group_btn.add_css_class("viewer-group-pill"); - let download_btn = action("document-save-symbolic", &gettext("Save a Copy…")); - let delete_btn = action("user-trash-symbolic", &gettext("Move to Trash (Delete)")); - let open_ext_btn = action("document-open-symbolic", &gettext("Open With…")); + // Everything used less often than the favorite and the details sits in + // one menu, named in words rather than in icons. + let more_btn = action("view-more-symbolic", &gettext("More")); + let fullscreen_btn = fullscreen_button(&window); + fullscreen_btn.add_css_class("flat"); + fullscreen_btn.add_css_class("viewer-action-btn"); let close_btn = action("window-close-symbolic", &gettext("Close (Esc)")); close_btn.add_css_class("viewer-close-btn"); @@ -1147,14 +1356,26 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { top_bar.append(&group_btn); top_bar.append(&favorite_btn); top_bar.append(&info_toggle); - top_bar.append(&slideshow_btn); - top_bar.append(&download_btn); - top_bar.append(&delete_btn); - top_bar.append(&open_ext_btn); + top_bar.append(&fullscreen_btn); + top_bar.append(&more_btn); top_bar.append(&close_btn); let top_reveal = fade(top_bar.upcast_ref(), gtk4::Align::Fill, gtk4::Align::Start); overlay.add_overlay(&top_reveal); + let (strip, strip_selection) = build_strip(ui); + let strip_scroll = gtk4::ScrolledWindow::builder() + .hscrollbar_policy(gtk4::PolicyType::External) + .vscrollbar_policy(gtk4::PolicyType::Never) + .child(&strip) + .build(); + strip_scroll.add_css_class("viewer-strip-bar"); + let strip_reveal = fade( + strip_scroll.upcast_ref(), + gtk4::Align::Fill, + gtk4::Align::End, + ); + overlay.add_overlay(&strip_reveal); + // Details panel: docked beside the image as a real surface, so opening it // narrows the photo instead of covering part of it. let info_rows = gtk4::Box::new(gtk4::Orientation::Vertical, 12); @@ -1242,7 +1463,9 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { narrow.add_setter(&info_split, "collapsed", Some(&true.to_value())); adw_window.add_breakpoint(narrow); let body = info_split.clone(); - adw_window.set_content(Some(&body)); + let toasts = adw::ToastOverlay::new(); + toasts.set_child(Some(&body)); + adw_window.set_content(Some(&toasts)); let viewer = Rc::new(Viewer { window: window.clone(), @@ -1273,12 +1496,23 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { pinch_start: Cell::new(1.0), drag_start: Cell::new((0.0, 0.0)), pointer: Cell::new((0.0, 0.0)), - chrome: vec![top_reveal.clone(), prev_reveal.clone(), next_reveal.clone()], + chrome: vec![ + top_reveal.clone(), + prev_reveal.clone(), + next_reveal.clone(), + strip_reveal.clone(), + ], + strip: strip.clone(), + strip_selection: strip_selection.clone(), + strip_reveal: strip_reveal.clone(), chrome_hover: Cell::new(false), chrome_source: RefCell::new(None), - slideshow: slideshow_btn.clone(), + more: more_btn.clone(), + menu_open: Cell::new(false), + slideshow: Cell::new(false), slide_source: RefCell::new(None), slide_fullscreened: Cell::new(false), + toasts, }); let initial_idx = find_photo_index(&ui.gallery.model, &initial_uid).unwrap_or(0); @@ -1304,7 +1538,7 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { } }); body.add_controller(motion); - for revealer in [&top_reveal, &prev_reveal, &next_reveal] { + for revealer in [&top_reveal, &prev_reveal, &next_reveal, &strip_reveal] { let hover = gtk4::EventControllerMotion::new(); let viewer_enter = Rc::downgrade(&viewer); hover.connect_enter(move |_, _, _| { @@ -1420,13 +1654,52 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { w_close.close(); }); + // A click on a thumbnail selects it; step there. Every selection the + // lightbox sets itself is the photo on screen, and the model's own changes + // (a page landing, a favorite rewritten in place) keep the selected photo + // or drop it, so a selection elsewhere can only be the user's. + let ui_strip = ui.clone(); + let viewer_strip = Rc::downgrade(&viewer); + strip_selection.connect_selection_changed(move |selection, _, _| { + let Some(viewer) = viewer_strip.upgrade() else { + return; + }; + let picked = selection.selected(); + let Some(current) = find_photo_index(&ui_strip.gallery.model, &viewer.uid.borrow()) else { + return; + }; + if picked != gtk4::INVALID_LIST_POSITION && picked != current { + navigate_photo(&ui_strip, &viewer, picked as i32 - current as i32); + } + }); + let viewer_items = Rc::downgrade(&viewer); + let items_handler = ui + .gallery + .model + .connect_items_changed(move |model, _, _, _| { + let Some(viewer) = viewer_items.upgrade() else { + return; + }; + if let Some(index) = find_photo_index(model, &viewer.uid.borrow()) { + viewer.strip_selection.set_selected(index); + } + sync_strip(&viewer); + }); + // The one strong handle on the viewer. Every handler holds a weak one: a // strong one in a handler of a widget the viewer holds is a cycle, which // kept every closed lightbox, its window and its full-size photo in memory // for the life of the app. let keep = RefCell::new(Some(viewer.clone())); + let ui_close = ui.clone(); + let items_handler = RefCell::new(Some(items_handler)); window.connect_close_request(move |_| { + // The gallery's model outlives the lightbox. + if let Some(handler) = items_handler.borrow_mut().take() { + ui_close.gallery.model.disconnect(handler); + } if let Some(viewer) = keep.borrow_mut().take() { + reveal_photo(&ui_close, &viewer.uid.borrow()); stop_video(&viewer); disarm_slide(&viewer); if let Some(source) = viewer.chrome_source.borrow_mut().take() { @@ -1452,36 +1725,14 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { } }); - let ui_slides = ui.clone(); - let viewer_slides = Rc::downgrade(&viewer); - slideshow_btn.connect_toggled(move |toggle| { - let Some(viewer_slides) = viewer_slides.upgrade() else { - return; - }; - if toggle.is_active() { - start_slideshow(&ui_slides, &viewer_slides); - } else { - stop_slideshow(&viewer_slides); - } - }); - let toggle_off = info_toggle.clone(); info_close.connect_clicked(move |_| toggle_off.set_active(false)); let ui_map = ui.clone(); let viewer_map = Rc::downgrade(&viewer); - let w_map = window.downgrade(); info_map.connect_clicked(move |_| { - let Some(w_map) = w_map.upgrade() else { - return; - }; - let Some(viewer_map) = viewer_map.upgrade() else { - return; - }; - let coords = *viewer_map.coords.borrow(); - if let Some((lat, lon)) = coords { - w_map.close(); - show_on_map(&ui_map, lat, lon); + if let Some(viewer_map) = viewer_map.upgrade() { + show_photo_on_map(&ui_map, &viewer_map); } }); @@ -1510,6 +1761,9 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { let failed = match rx.recv().await { Ok(Ok(Response::Ok { .. })) => { set_gallery_favorite(&ui_result, &uid, favorite); + // The tiles hold their own copy of the photo, so the star + // shows on the grid only once it is laid out again. + repaint_gallery(&ui_result); None } Ok(Ok(Response::Error { message, .. })) => Some(message), @@ -1530,24 +1784,6 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { }); }); - // Deleting the photo on screen closes the lightbox: what it was showing is - // gone, and leaving it open on a trashed photo invites a second Delete on - // something the user cannot see. - let ui_delete = ui.clone(); - let viewer_delete = Rc::downgrade(&viewer); - let w_delete = window.downgrade(); - delete_btn.connect_clicked(move |_| { - let Some(w_delete) = w_delete.upgrade() else { - return; - }; - let Some(viewer_delete) = viewer_delete.upgrade() else { - return; - }; - let uid = viewer_delete.uid.borrow().clone(); - w_delete.close(); - trash_photos(&ui_delete, vec![uid], false); - }); - let ui_group = ui.clone(); let viewer_group = Rc::downgrade(&viewer); group_btn.connect_clicked(move |_| { @@ -1563,38 +1799,11 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { } }); - let ui_download = ui.clone(); - let w_download = window.downgrade(); - let viewer_download = Rc::downgrade(&viewer); - download_btn.connect_clicked(move |_| { - let Some(w_download) = w_download.upgrade() else { - return; - }; - let Some(viewer_download) = viewer_download.upgrade() else { - return; - }; - if let Some(path) = viewer_download.path.borrow().as_deref() { - let name = viewer_download.name.borrow(); - save_photo_to_disk( - &ui_download, - &w_download, - path, - name.as_deref().unwrap_or("photo"), - ); - } - }); - - let w_ext = window.downgrade(); - let viewer_ext = Rc::downgrade(&viewer); - open_ext_btn.connect_clicked(move |_| { - let Some(w_ext) = w_ext.upgrade() else { - return; - }; - let Some(viewer_ext) = viewer_ext.upgrade() else { - return; - }; - if let Some(path) = viewer_ext.path.borrow().as_deref() { - open_with(&w_ext, path, viewer_ext.name.borrow().as_deref()); + let ui_more = ui.clone(); + let viewer_more = Rc::downgrade(&viewer); + more_btn.connect_clicked(move |_| { + if let Some(viewer_more) = viewer_more.upgrade() { + open_viewer_menu(&ui_more, &viewer_more); } }); @@ -1672,13 +1881,22 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { return glib::Propagation::Proceed; }; let ctrl = state.contains(gtk4::gdk::ModifierType::CONTROL_MASK); + let alt = state.contains(gtk4::gdk::ModifierType::ALT_MASK); let playing_video = viewer_key.media.visible_child_name().as_deref() == Some("video"); + if playing_video + && let Some(name) = key.name() + && player_key(&viewer_key.video, &name, state) + { + return glib::Propagation::Stop; + } match key.name().as_deref() { Some("space") if playing_video => { if let Some(stream) = viewer_key.video.media_stream() { stream.set_playing(!stream.is_playing()); } } + // Alt+Left is Back everywhere else in the app. + Some("Left") if alt => w_key.close(), Some("Left" | "Up" | "BackSpace") => navigate_photo(&ui_key, &viewer_key, -1), Some("Right" | "Down" | "space") => navigate_photo(&ui_key, &viewer_key, 1), Some("Home") => navigate_photo(&ui_key, &viewer_key, i32::MIN / 2), @@ -1687,30 +1905,20 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { Some("minus" | "KP_Subtract") => zoom_by(&viewer_key, 1.0 / ZOOM_STEP), Some("0" | "KP_0") => reset_zoom(&viewer_key), Some("1" | "KP_1") => zoom_to(&viewer_key, Some(1.0), None), - Some("F5") => viewer_key - .slideshow - .set_active(!viewer_key.slideshow.is_active()), + Some("F5") => toggle_slideshow(&ui_key, &viewer_key), // Escape ends a slideshow before it closes anything. - Some("Escape") if viewer_key.slideshow.is_active() => { - viewer_key.slideshow.set_active(false) - } + Some("Escape") if viewer_key.slideshow.get() => stop_slideshow(&viewer_key), + Some("F10" | "Menu") => open_viewer_menu(&ui_key, &viewer_key), Some("i") => viewer_key .info_toggle .set_active(!viewer_key.info_toggle.is_active()), - Some("f" | "F11") => { - if w_key.is_fullscreen() { - w_key.unfullscreen(); - } else { - w_key.fullscreen(); - } - } - Some("Delete" | "KP_Delete") => { - let uid = viewer_key.uid.borrow().clone(); - w_key.close(); - trash_photos(&ui_key, vec![uid], false); - } + Some("f" | "F11") => toggle_fullscreen(&w_key), + Some("Delete" | "KP_Delete") => trash_shown_photo(&ui_key, &viewer_key), Some("Escape" | "q") => w_key.close(), Some("w") if ctrl => w_key.close(), + Some("c") if ctrl && viewer_key.path.borrow().is_some() && !playing_video => { + copy_shown_photo(&ui_key, &viewer_key) + } _ => return glib::Propagation::Proceed, } glib::Propagation::Stop @@ -1718,6 +1926,113 @@ pub(crate) fn open_photo_viewer(ui: &Rc, initial_uid: String) { window.add_controller(key_controller); window.present(); + window +} + +/// The ⋮ menu: what the lightbox can do with the photo on screen besides +/// starring it and showing its details. Built when it opens, so it offers +/// only what applies to this photo. +fn open_viewer_menu(ui: &Rc, viewer: &Rc) { + let weak = Rc::downgrade(viewer); + let with_viewer = move |run: fn(&Rc, &Rc), ui: &Rc| { + let (ui, weak) = (ui.clone(), weak.clone()); + move || { + if let Some(viewer) = weak.upgrade() { + run(&ui, &viewer); + } + } + }; + let mut menu = ActionMenu::new(); + menu.item(&gettext("Slideshow"), with_viewer(toggle_slideshow, ui)); + if viewer.media.visible_child_name().as_deref() == Some("video") { + let video = viewer.video.clone(); + menu.toggle(&gettext("Repeat"), video.is_loop(), move |on| { + video.set_loop(on) + }); + } + menu.section(); + // These need the full-size file, which may still be downloading. + if viewer.path.borrow().is_some() { + let still = viewer.media.visible_child_name().as_deref() == Some("photo"); + if still { + // Translators: menu item that puts the photo on the clipboard as an image. + menu.item(&gettext("Copy Image"), with_viewer(copy_shown_photo, ui)); + } + menu.item(&gettext("Save a Copy…"), with_viewer(save_shown_photo, ui)); + menu.item(&gettext("Open With…"), with_viewer(open_shown_photo, ui)); + } + if photo_shareable(ui) { + menu.item(&gettext("Share…"), with_viewer(share_shown_photo, ui)); + } + // A RAW is not on the timeline, and an album holds the shot it belongs to. + if find_photo_index(&ui.gallery.model, &viewer.uid.borrow()).is_some() { + menu.item( + &gettext("Add to Album…"), + with_viewer(add_shown_photo_to_album, ui), + ); + } + if viewer.coords.borrow().is_some() { + menu.item(&gettext("Show on Map"), with_viewer(show_photo_on_map, ui)); + } + menu.section(); + menu.item( + &gettext("Move to Trash…"), + with_viewer(trash_shown_photo, ui), + ); + + let button = &viewer.more; + let popover = menu.popup_at( + button, + f64::from(button.width()) / 2.0, + f64::from(button.height()), + ); + viewer.menu_open.set(true); + let weak = Rc::downgrade(viewer); + popover.connect_closed(move |_| { + if let Some(viewer) = weak.upgrade() { + viewer.menu_open.set(false); + // A slideshow started from the menu keeps the controls hidden. + if !viewer.slideshow.get() { + show_chrome(&viewer); + } + } + }); +} + +fn save_shown_photo(_ui: &Rc, viewer: &Rc) { + if let Some(path) = viewer.path.borrow().as_deref() { + let name = viewer.name.borrow(); + save_photo_to_disk(viewer, path, name.as_deref().unwrap_or("photo")); + } +} + +fn open_shown_photo(_ui: &Rc, viewer: &Rc) { + if let Some(path) = viewer.path.borrow().as_deref() { + open_with(&viewer.window, path, viewer.name.borrow().as_deref()); + } +} + +fn add_shown_photo_to_album(ui: &Rc, viewer: &Rc) { + let uid = viewer.uid.borrow().clone(); + prompt_add_to_album(ui, vec![uid], Some(viewer.window.clone())); +} + +/// Close the lightbox and show where the photo was taken. +fn show_photo_on_map(ui: &Rc, viewer: &Rc) { + let coords = *viewer.coords.borrow(); + if let Some((lat, lon)) = coords { + viewer.window.close(); + show_on_map(ui, lat, lon); + } +} + +/// Ask, then move the photo on screen to the trash. Confirmed, the lightbox +/// closes first: what it was showing is gone, and leaving it open on a +/// trashed photo invites a second Delete on something the user cannot see. +fn trash_shown_photo(ui: &Rc, viewer: &Rc) { + let uid = viewer.uid.borrow().clone(); + let window = viewer.window.clone(); + ask_trash_photos(ui, vec![uid], Some(window.clone()), move || window.close()); } /// Whether `(x, y)` — in `picture`'s coordinates — lands on the photo itself diff --git a/crates/pdfs-gui/src/app/pages/photos.rs b/crates/pdfs-gui/src/app/pages/photos.rs index 09c74b5..5699eda 100644 --- a/crates/pdfs-gui/src/app/pages/photos.rs +++ b/crates/pdfs-gui/src/app/pages/photos.rs @@ -37,6 +37,9 @@ pub(crate) struct GalleryState { pub(crate) burst: Cell, /// Says a Google Photos import is running, with a way to its page. pub(crate) import_banner: adw::Banner, + /// The bar that counts uploads from this computer through, under the + /// header while any run. + pub(crate) upload_bar: UploadBar, pub(crate) upload: adw::SplitButton, /// Upload / Import offered on the *empty timeline* status page, so a fresh /// account is a place to start rather than a dead end. Hidden on every other @@ -157,10 +160,11 @@ pub(crate) struct GalleryState { /// Photos the daemon reported as having no thumbnail at all, so a tile that /// can never be filled isn't requested again on every scroll past it. pub(crate) photo_nothumb: RefCell>, - /// Tiles on screen still waiting for their thumbnail, by uid. Populated as - /// sections are bound, drained as batches land, cleared on unbind — so a - /// batch only ever paints a widget that is still showing that photo. - pub(crate) thumb_wanted: RefCell>, + /// Pictures on screen still waiting for their thumbnail, by uid. Populated + /// as they are bound, drained as batches land, cleared on unbind — so a + /// batch only ever paints a widget that is still showing that photo. One + /// photo can be waited for twice: by its tile and by the lightbox's strip. + pub(crate) thumb_wanted: RefCell>>, /// Uids queued for the next [`Request::PhotoThumbs`] batch, and whether a /// batch is already in flight (only one at a time, so a long scroll can't /// stack requests on the daemon). @@ -243,6 +247,27 @@ impl GalleryState { pub(crate) fn in_collection(&self) -> bool { self.album.borrow().is_some() || self.place.borrow().is_some() } + + /// Have `picture` painted with the thumbnail of `uid` once it lands. + pub(crate) fn await_thumb(&self, uid: &str, picture: >k4::Picture) { + let mut wanted = self.thumb_wanted.borrow_mut(); + let waiting = wanted.entry(uid.to_string()).or_default(); + if !waiting.contains(picture) { + waiting.push(picture.clone()); + } + } + + /// Stop waiting for `uid` in the pictures `drop` picks, because they show + /// another photo now. + pub(crate) fn release_thumb(&self, uid: &str, drop: impl Fn(>k4::Picture) -> bool) { + let mut wanted = self.thumb_wanted.borrow_mut(); + if let Some(waiting) = wanted.get_mut(uid) { + waiting.retain(|picture| !drop(picture)); + if waiting.is_empty() { + wanted.remove(uid); + } + } + } } /// How many photos to pull per [`Request::PhotosTimeline`] page. @@ -456,6 +481,9 @@ pub(crate) struct GalleryWidgets { pub(crate) scrubber: Scrubber, /// Says a Google Photos import is running, with a way to its page. pub(crate) import_banner: adw::Banner, + /// The bar that counts uploads from this computer through, under the + /// header while any run. + pub(crate) upload_bar: UploadBar, pub(crate) list: gtk4::ListView, pub(crate) scroll: gtk4::ScrolledWindow, pub(crate) retry: gtk4::Button, @@ -881,6 +909,8 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) { .action_name("win.show-import") .build(); + let upload_bar = UploadBar::new(); + let inner = gtk4::Box::new(gtk4::Orientation::Vertical, 12); inner.set_margin_top(12); inner.set_margin_bottom(12); @@ -891,6 +921,7 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) { let body = gtk4::Box::new(gtk4::Orientation::Vertical, 0); body.append(&import_banner); + body.append(&upload_bar.revealer); body.append(&inner); // The title slot holds the view switch on the page itself, the album's or // place's name inside one, and the count in selection mode (see @@ -921,6 +952,7 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) { pager, scrubber, import_banner, + upload_bar, list, scroll, retry, @@ -1314,14 +1346,10 @@ pub(crate) fn wire_gallery( if let Some(obj) = item.item().and_downcast::() && let GalleryRow::Tiles { tiles, .. } = &*obj.borrow::() { - let mut wanted = ui_unbind.gallery.thumb_wanted.borrow_mut(); for tile in tiles { - if wanted - .get(&tile.photo.uid) - .is_some_and(|picture| picture.is_ancestor(&row_box)) - { - wanted.remove(&tile.photo.uid); - } + ui_unbind + .gallery + .release_thumb(&tile.photo.uid, |picture| picture.is_ancestor(&row_box)); } } }); @@ -1366,6 +1394,13 @@ pub(crate) fn wire_gallery( sync_float_heading(&ui_scroll); ui_scroll.gallery.scrubber.flash(); }); + // Zooming out or making the window taller can leave every loaded photo on + // one screen. Nothing scrolls then, so the scrolling above never asks for + // the next page: ask whenever the content shrinks under the viewport. + let ui_fill = ui.clone(); + scroll + .vadjustment() + .connect_changed(move |_| fill_viewport(&ui_fill)); wire_scrubber(ui); wire_drag_select(ui); @@ -1441,7 +1476,7 @@ pub(crate) fn wire_gallery( let ui_album = ui.clone(); selection.album.connect_clicked(move |_| { let uids: Vec = ui_album.gallery.selected.borrow().iter().cloned().collect(); - prompt_add_to_album(&ui_album, uids); + prompt_add_to_album(&ui_album, uids, None); }); let ui_remove = ui.clone(); selection.remove.connect_clicked(move |_| { @@ -1495,9 +1530,11 @@ pub(crate) fn wire_gallery( gtk4::gdk::DragAction::COPY, ); let ui_drop = ui.clone(); - drop.connect_accept(move |_, _| { + drop.connect_accept(move |_, drop| { let gallery = &ui_drop.gallery; - !gallery.in_collection() + // Photos dragged out of the timeline are already in it. + drop.drag().is_none() + && !gallery.in_collection() && gallery.view_switch.active() == VIEW_TIMELINE && gallery.library.get() && *ui_drop.mounted.borrow() @@ -1895,6 +1932,86 @@ pub(crate) fn copy_without_overwrite( unreachable!("some numbered name is free") } +/// How far the uploads from this computer have come: a count and a bar, so a +/// drop of a few hundred photos shows it is moving. Batches started while one +/// runs join its count. +#[derive(Clone)] +pub(crate) struct UploadBar { + pub(crate) revealer: gtk4::Revealer, + label: gtk4::Label, + progress: gtk4::ProgressBar, + /// Uploads finished, and uploads asked for, since the bar last emptied. + count: Rc>, +} + +impl UploadBar { + fn new() -> Self { + let label = gtk4::Label::builder() + .halign(gtk4::Align::Start) + .ellipsize(gtk4::pango::EllipsizeMode::End) + .build(); + let progress = gtk4::ProgressBar::builder().hexpand(true).build(); + let content = gtk4::Box::new(gtk4::Orientation::Vertical, 6); + content.set_margin_top(10); + content.set_margin_bottom(4); + content.set_margin_start(16); + content.set_margin_end(16); + content.append(&label); + content.append(&progress); + let revealer = gtk4::Revealer::builder() + .transition_type(gtk4::RevealerTransitionType::SlideDown) + .child(&content) + .build(); + Self { + revealer, + label, + progress, + count: Rc::default(), + } + } + + /// Count `n` more uploads in. + pub(crate) fn add(&self, n: usize) { + let (done, total) = self.count.get(); + self.count.set((done, total + n)); + self.paint(); + } + + /// Count one upload as finished, whether or not it went through. + pub(crate) fn finish(&self) { + let (done, total) = self.count.get(); + let done = (done + 1).min(total); + if done == total { + self.count.set((0, 0)); + self.revealer.set_reveal_child(false); + return; + } + self.count.set((done, total)); + self.paint(); + } + + fn paint(&self) { + let (done, total) = self.count.get(); + if total == 0 { + return; + } + self.label.set_label(&upload_progress_text(done, total)); + self.progress.set_fraction(done as f64 / total as f64); + self.revealer.set_reveal_child(true); + } +} + +/// "Uploading 3 of 12 items…": the upload under way is number `done + 1`. +pub(crate) fn upload_progress_text(done: usize, total: usize) -> String { + // Translators: progress above the photo timeline while photos upload; {position} is the upload under way, {n} how many there are. + ngettext_f( + "Uploading {position} of {n} item…", + "Uploading {position} of {n} items…", + total as u64, + &[("position", &thousands(done + 1))], + ) +} + /// Upload photos and videos from this computer into the timeline, one after /// the other, then reload it once. pub(crate) fn upload_media(ui: &Rc, paths: Vec) { @@ -1909,15 +2026,7 @@ pub(crate) fn upload_media(ui: &Rc, paths: Vec) { let total = uploads.len(); ui.busy_begin(); if total > 1 { - toast( - ui, - &ngettext_f( - "Uploading {n} item…", - "Uploading {n} items…", - total as u64, - &[], - ), - ); + ui.gallery.upload_bar.add(total); } let ui = ui.clone(); glib::spawn_future_local(async move { @@ -1955,6 +2064,9 @@ pub(crate) fn upload_media(ui: &Rc, paths: Vec) { )) } } + if total > 1 { + ui.gallery.upload_bar.finish(); + } } ui.busy_end(); if uploaded > 0 { @@ -2017,16 +2129,17 @@ fn save_photos_view(ui: &Rc) { let ui_save = ui.clone(); let source = glib::timeout_add_local_once(Duration::from_millis(500), move || { ui_save.gallery.zoom_save.borrow_mut().take(); - let mut config = ui_save.dirs.load_config(); let view = PhotosView { row_height: Some(ui_save.gallery.row_height.get()), square: ui_save.gallery.square.get(), }; - if config.photos_view == view { + if ui_save.dirs.load_config().photos_view == view { return; } - config.photos_view = view; - if let Err(e) = ui_save.dirs.save_config(&config) { + if let Err(e) = ui_save + .dirs + .update_config(|config| config.photos_view = view) + { tracing::warn!("cannot save the Photos view: {e}"); } }); @@ -2363,6 +2476,23 @@ impl TileWidget { }); button.add_controller(context); + // A picked photo drags out, with the rest of the selection, into a + // file manager or any app that takes files. A drag from anywhere else + // selects instead (see `wire_drag_select`). + let drag_out = gtk4::DragSource::new(); + drag_out.set_actions(gtk4::gdk::DragAction::COPY); + let ui_drag = ui.clone(); + let drag_uid = uid.clone(); + drag_out.connect_prepare(move |source, x, y| { + let photos = dragged_photos(&ui_drag, &drag_uid()?)?; + if let Some(tile) = source.widget() { + let icon = gtk4::WidgetPaintable::new(Some(&tile)); + source.set_icon(Some(&icon), x as i32, y as i32); + } + Some(PhotoDrag::new(ui_drag.dirs.control_socket(), photos).upcast()) + }); + button.add_controller(drag_out); + // A tile opens in the in-app lightbox, which plays a video in place. // In selection mode a tile picks instead of opening: the lightbox is // one Escape away. @@ -2455,13 +2585,28 @@ impl TileWidget { let Some(photo) = self.photo.borrow_mut().take() else { return; }; - let mut wanted = ui.gallery.thumb_wanted.borrow_mut(); - if wanted.get(&photo.uid) == Some(&self.picture) { - wanted.remove(&photo.uid); - } + ui.gallery + .release_thumb(&photo.uid, |picture| picture == &self.picture); } } +/// The photos a drag that starts on `uid` carries out: the whole selection, +/// in timeline order, as uid and name. `None` unless `uid` is picked. +fn dragged_photos(ui: &Rc, uid: &str) -> Option)>> { + let selected = ui.gallery.selected.borrow(); + if !ui.gallery.selecting.get() || !selected.contains(uid) { + return None; + } + let model = &ui.gallery.model; + let photos: Vec<(String, Option)> = (0..model.n_items()) + .filter_map(|index| model.item(index).and_downcast::()) + .map(|boxed| boxed.borrow::().clone()) + .filter(|photo| selected.contains(&photo.uid)) + .map(|photo| (photo.uid, photo.name)) + .collect(); + (!photos.is_empty()).then_some(photos) +} + /// Add or remove one CSS class, for widgets that are rebound rather than /// rebuilt. fn set_css_class(widget: &impl IsA, class: &str, on: bool) { @@ -2540,6 +2685,9 @@ struct DragSelect { pointer: (f64, f64), /// The edge autoscroll, while the pointer is near the top or bottom. scroll: Option, + /// The drag started on a picked photo, so it carries the selection out + /// of the app rather than selecting. + out: bool, } /// Dragging across the timeline selects every photo from where the drag @@ -2556,21 +2704,33 @@ fn wire_drag_select(ui: &Rc) { let state = Rc::new(RefCell::new(DragSelect::default())); let state_begin = state.clone(); - drag.connect_drag_begin(move |_, x, y| { + let ui_begin = ui.clone(); + drag.connect_drag_begin(move |gesture, x, y| { let mut drag = state_begin.borrow_mut(); if let Some(source) = drag.scroll.take() { source.remove(); } + let out = ui_begin.gallery.selecting.get() + && tile_at(&ui_begin, x, y) + .is_some_and(|uid| ui_begin.gallery.selected.borrow().contains(&uid)); *drag = DragSelect { start: (x, y), pointer: (x, y), + out, ..DragSelect::default() }; + // Left to the tile's own drag source. + if out { + gesture.set_state(gtk4::EventSequenceState::Denied); + } }); let ui_update = ui.clone(); let state_update = state.clone(); drag.connect_drag_update(move |gesture, dx, dy| { + if state_update.borrow().out { + return; + } let (sx, sy) = state_update.borrow().start; state_update.borrow_mut().pointer = (sx + dx, sy + dy); if !state_update.borrow().active { @@ -2764,6 +2924,18 @@ pub(crate) fn delete_selected(ui: &Rc) { /// Confirm, then move the photos behind `uids` to Proton trash. pub(crate) fn confirm_trash_photos(ui: &Rc, uids: Vec) { + ask_trash_photos(ui, uids, None, || {}); +} + +/// [`confirm_trash_photos`] over `parent` rather than the main window, for +/// the photo viewer, which covers it. `before` runs once the user confirms, +/// before the photos go. +pub(crate) fn ask_trash_photos( + ui: &Rc, + uids: Vec, + parent: Option, + before: impl Fn() + 'static, +) { if uids.is_empty() { return; } @@ -2803,10 +2975,11 @@ pub(crate) fn confirm_trash_photos(ui: &Rc, uids: Vec) { dialog.set_default_response(Some("cancel")); dialog.set_close_response("cancel"); - let win = ui_window(ui); + let win = parent.or_else(|| ui_window(ui)); let ui = ui.clone(); dialog.connect_response(None, move |_, response| { if response == "trash" { + before(); trash_photos(&ui, uids.clone(), false); } }); @@ -2951,10 +3124,7 @@ pub(crate) fn want_thumb(ui: &Rc, photo: &PhotoItem, picture: >k4::Picture return; } - ui.gallery - .thumb_wanted - .borrow_mut() - .insert(photo.uid.clone(), picture.clone()); + ui.gallery.await_thumb(&photo.uid, picture); match photo.thumb_path.as_deref() { Some(path) => { @@ -3173,7 +3343,8 @@ pub(crate) fn schedule_decode(ui: &Rc) { } }; ui.store_texture(&uid, texture.clone()); - if let Some(picture) = ui.gallery.thumb_wanted.borrow_mut().remove(&uid) { + let waiting = ui.gallery.thumb_wanted.borrow_mut().remove(&uid); + for picture in waiting.into_iter().flatten() { picture.set_paintable(Some(&texture)); } } @@ -3288,6 +3459,16 @@ fn top_anchor(ui: &Rc) -> Option<(String, f32)> { .or(first) } +/// Scroll the grid just far enough to show photo `uid`, as the lightbox +/// closes: paging with the arrow keys may have left the grid far behind. +pub(crate) fn reveal_photo(ui: &Rc, uid: &str) { + if let Some(row) = row_of_photo(&ui.gallery.groups, uid) { + ui.gallery + .list + .scroll_to(row, gtk4::ListScrollFlags::NONE, None); + } +} + /// Which row of the rendered model holds `uid`, if any. fn row_of_photo(store: &gio::ListStore, uid: &str) -> Option { (0..store.n_items()).find(|i| { @@ -3917,17 +4098,32 @@ fn continue_jump(ui: &Rc) { /// Load the next page when the timeline does not fill the window yet: with no /// scrollbar there is no scrolling to trigger it. fn fill_viewport(ui: &Rc) { + if !ui.gallery.has_more.get() || ui.gallery.loading.get() { + return; + } let ui = ui.clone(); glib::idle_add_local_once(move || { let Some(adj) = ui.gallery.list.vadjustment() else { return; }; - if ui.gallery.has_more.get() && adj.upper() <= adj.page_size() + 1.0 { + let timeline = ui.gallery.content.visible_child_name().as_deref() == Some("timeline"); + if timeline + && ui.gallery.has_more.get() + && adj.page_size() > 0.0 + && viewport_unfilled(adj.upper(), adj.page_size()) + { load_gallery(&ui, true); } }); } +/// Whether content `upper` px tall leaves a viewport `page_size` px tall short +/// of a screen and a half to scroll through, the margin the scrolling pager +/// keeps. Below it the pager would never fire. +pub(crate) fn viewport_unfilled(upper: f64, page_size: f64) -> bool { + upper <= page_size * 2.5 + 1.0 +} + /// Select every photo loaded so far (Ctrl+A). pub(crate) fn select_all_photos(ui: &Rc) { set_selection_mode(ui, true); @@ -3952,7 +4148,9 @@ fn show_photo_menu(ui: &Rc, photo: &PhotoItem, anchor: >k4::Button, x: f64 } else { pgettext("verb", "Open") }; - menu.item(&label, move || open_photo_viewer(&ui_c, uid.clone())); + menu.item(&label, move || { + open_photo_viewer(&ui_c, uid.clone()); + }); let (ui_c, uid) = (ui.clone(), photo.uid.clone()); // Translators: a check item in a photo's menu, on while the photo is a favorite. let favorite_label = pgettext("state", "Favorite"); @@ -3967,8 +4165,18 @@ fn show_photo_menu(ui: &Rc, photo: &PhotoItem, anchor: >k4::Button, x: f64 menu.section(); let (ui_c, uid) = (ui.clone(), photo.uid.clone()); menu.item(&gettext("Add to Album…"), move || { - prompt_add_to_album(&ui_c, vec![uid.clone()]) + prompt_add_to_album(&ui_c, vec![uid.clone()], None) }); + if photo_shareable(ui) { + let (ui_c, uid, name) = ( + ui.clone(), + photo.uid.clone(), + photo.name.clone().unwrap_or_default(), + ); + menu.item(&gettext("Share…"), move || { + share_photo(&ui_c, uid.clone(), name.clone(), None) + }); + } let open_album = ui.gallery.album.borrow().clone(); if let Some(album) = open_album.filter(|album| !album.shared) { let (ui_c, uid) = (ui.clone(), photo.uid.clone()); @@ -4020,8 +4228,44 @@ fn set_photo_favorite(ui: &Rc, uid: String, favorite: bool) { }); } +/// Whether the photos on show are the user's own to share: everything but an +/// album someone else shared, whose photos live on that person's volume. +pub(crate) fn photo_shareable(ui: &Ui) -> bool { + !ui.gallery + .album + .borrow() + .as_ref() + .is_some_and(|album| album.shared) +} + +/// Open the Share dialog for one photo, addressed by uid: the photos volume +/// has no path in any mount. Over `parent` when the lightbox is open. +pub(crate) fn share_photo(ui: &Rc, uid: String, name: String, parent: Option) { + let entry = DirEntry { + uid, + name, + ..DirEntry::default() + }; + open_share_dialog_over(ui, &entry, parent); +} + +/// Whether the photo grid is the view the Photos page shows: an album or a +/// place, or the timeline itself. Under Albums or Places it is out of sight. +pub(crate) fn photo_grid_shown(ui: &Ui) -> bool { + ui.gallery.in_collection() || ui.gallery.view_switch.active() == VIEW_TIMELINE +} + pub(crate) fn load_gallery(ui: &Rc, append: bool) { sync_filter_badge(ui); + // A load would put the grid, or its status page, on screen in place of + // the Albums or Places view the user is looking at. Leave the timeline to + // be read again when they switch back to it. + if !photo_grid_shown(ui) { + ui.gallery.timeline_stale.set(true); + // A scrubber jump was paging the grid the user has just left. + ui.gallery.jump.set(None); + return; + } if ui.gallery.loading.get() { // A filter flipped while a page is on its way: that page answers the // old question, so fetch again once it lands. More pages can wait. @@ -4037,6 +4281,9 @@ pub(crate) fn load_gallery(ui: &Rc, append: bool) { let place = ui.gallery.place.borrow().as_ref().map(|p| p.id); let collection = album.is_some() || place.is_some(); if !append { + // A scrubber jump looks for a month of the listing being replaced: + // another filter, album or place has its own months, if any. + ui.gallery.jump.set(None); // Fresh load: clear the timeline and show Loading until the first page lands. ui.gallery.model.remove_all(); gallery_status( @@ -4098,6 +4345,12 @@ pub(crate) fn load_gallery(ui: &Rc, append: bool) { if !waiters.is_empty() { glib::idle_add_local_once(move || waiters.into_iter().for_each(|waiter| waiter())); } + // The user left for Albums or Places while the page was on its way; + // painting it now would switch the view under them. + if !photo_grid_shown(&ui) { + ui.gallery.timeline_stale.set(true); + return; + } match result { Ok(Ok(Response::Photos { available, @@ -4138,6 +4391,20 @@ pub(crate) fn load_gallery(ui: &Rc, append: bool) { ui.gallery.model.append(&BoxedAnyObject::new(item.clone())); } repaint_gallery(&ui); + // The rows are swapped in place, which keeps the old scroll + // offset; a fresh load (another kind, month, album or place) + // reads from its newest photo. + if !append + && ui + .gallery + .list + .model() + .is_some_and(|rows| rows.n_items() > 0) + { + ui.gallery + .list + .scroll_to(0, gtk4::ListScrollFlags::NONE, None); + } if ui.gallery.model.n_items() == 0 { let filtered = ui.gallery.kind.get().is_some() || ui.gallery.favorites.get() @@ -4236,24 +4503,13 @@ pub(crate) fn gallery_unreachable(ui: &Rc) { service_unreachable(ui, "gallery", gallery_status, |ui| load_gallery(ui, false)); } -/// Play a video with an external player. Prefers `mpv` — it sniffs the container -/// from the bytes, so the cache's extensionless blob plays fine, and it is the -/// right tool for the HEVC `.mkv`s this is aimed at — and falls back to the -/// user's default handler when mpv isn't installed. -pub(crate) fn play_external(path: &str) { - match Command::new("mpv").arg(path).spawn() { - Ok(child) => pdfs_core::opener::reap(child), - Err(_) => open_path(path), - } -} - #[cfg(test)] mod tests { use super::{ - Grouping, ROW_DEFAULT, ROW_MAX, ROW_MIN, ROW_STEP, TILE_GAP, TILE_RATIO_MAX, + Grouping, PHOTOS_PAGE, ROW_DEFAULT, ROW_MAX, ROW_MIN, ROW_STEP, TILE_GAP, TILE_RATIO_MAX, TILE_RATIO_MIN, active_filters, copy_without_overwrite, empty_timeline_text, grouping_for, memory_years, month_index, month_range, plan_grid, plan_justified, section_heading, - tile_ratio, + tile_ratio, viewport_unfilled, }; use pdfs_core::control::{PhotoItem, PhotoKind, PhotoMonth}; @@ -4376,6 +4632,18 @@ mod tests { std::fs::remove_dir_all(&dir).unwrap(); } + /// A first page zoomed all the way out fits on one screen, with nothing + /// to scroll: the next page has to load without a scroll asking for it. + #[test] + fn a_page_zoomed_out_onto_one_screen_asks_for_more() { + let (columns, size) = plan_grid(1580, ROW_MIN); + let rows = PHOTOS_PAGE.div_ceil(columns) as i32; + let height = f64::from(rows * (size + TILE_GAP) + 40); + assert!(viewport_unfilled(height, 880.0)); + assert!(viewport_unfilled(880.0, 880.0)); + assert!(!viewport_unfilled(880.0 * 3.0, 880.0)); + } + #[test] fn zooming_in_puts_fewer_tiles_in_a_row() { assert!(plan_grid(1920, ROW_MIN).0 > plan_grid(1920, ROW_DEFAULT).0); @@ -4440,6 +4708,7 @@ mod tests { favorite: false, group_size: 1, has_raw: false, + place_id: None, } }; // Newest first, the way the daemon sends them. diff --git a/crates/pdfs-gui/src/app/pages/places.rs b/crates/pdfs-gui/src/app/pages/places.rs index e2bb9ed..a589bd3 100644 --- a/crates/pdfs-gui/src/app/pages/places.rs +++ b/crates/pdfs-gui/src/app/pages/places.rs @@ -267,7 +267,12 @@ fn want_map_thumb(ui: &Rc, photo: &PhotoItem) { } // A town's card may be waiting for the same photo: share its picture // rather than take its place in the queue. - let waiting = ui.gallery.thumb_wanted.borrow().get(&photo.uid).cloned(); + let waiting = ui + .gallery + .thumb_wanted + .borrow() + .get(&photo.uid) + .and_then(|waiting| waiting.first().cloned()); let picture = waiting.unwrap_or_else(|| { let picture = gtk4::Picture::new(); want_thumb(ui, photo, &picture); @@ -362,7 +367,12 @@ fn show_spot_photos(ui: &Rc, photos: Vec<(PlaceInfo, PhotoItem)>, (x, y): (f /// Show photo's thumbnail in `picture`, sharing one another widget is /// already waiting for. fn spot_thumb(ui: &Rc, photo: &PhotoItem, picture: >k4::Picture) { - let waiting = ui.gallery.thumb_wanted.borrow().get(&photo.uid).cloned(); + let waiting = ui + .gallery + .thumb_wanted + .borrow() + .get(&photo.uid) + .and_then(|waiting| waiting.first().cloned()); match waiting { Some(waiting) => { waiting @@ -392,21 +402,45 @@ fn load_place_locations(ui: &Rc, id: u32) { }); } -/// Open `place` and then photo `uid` in it, paging through the town until -/// the photo has loaded. +/// Open photo `uid` of `place` from the map or a spot's list, paging through +/// the town until the photo has loaded. +/// +/// The lightbox steps through the gallery's model, so the town is opened +/// behind it. Closing the lightbox goes back to the map rather than leaving +/// the user in a town timeline they never asked for. fn open_place_photo(ui: &Rc, place: PlaceInfo, uid: String) { + let id = place.id; open_place(ui, place); - view_when_loaded(ui, uid); + view_when_loaded(ui, id, uid); } -fn view_when_loaded(ui: &Rc, uid: String) { +/// The open place's id, if a place is open. +fn open_place_id(ui: &Ui) -> Option { + ui.gallery.place.borrow().as_ref().map(|place| place.id) +} + +fn view_when_loaded(ui: &Rc, id: u32, uid: String) { let ui_page = ui.clone(); ui.gallery.page_waiters.borrow_mut().push(Box::new(move || { let ui = ui_page; + // The user went elsewhere while the town loaded: whatever page lands + // now belongs to that, and must not pop the lightbox up over it. + if open_place_id(&ui) != Some(id) { + return; + } if find_photo_index(&ui.gallery.model, &uid).is_some() { - open_photo_viewer(&ui, uid); - } else if ui.gallery.has_more.get() && ui.gallery.place.borrow().is_some() { - view_when_loaded(&ui, uid); + let window = open_photo_viewer(&ui, uid); + let ui_close = ui.clone(); + window.connect_close_request(move |_| { + // Only if the town is still what the lightbox was showing: "Show + // on map" in the lightbox has already moved on by itself. + if open_place_id(&ui_close) == Some(id) { + show_place_grid(&ui_close); + } + glib::Propagation::Proceed + }); + } else if ui.gallery.has_more.get() { + view_when_loaded(&ui, id, uid); ui.gallery.burst.set(true); load_gallery(&ui, true); } diff --git a/crates/pdfs-gui/src/app/pages/preferences_apps.rs b/crates/pdfs-gui/src/app/pages/preferences_apps.rs new file mode 100644 index 0000000..5cb34a9 --- /dev/null +++ b/crates/pdfs-gui/src/app/pages/preferences_apps.rs @@ -0,0 +1,606 @@ +//! Preferences → Apps: which program opens which file (`open_with`) and how +//! the search launcher shows itself (`prompt`). +//! +//! Neither involves the service. The app and the search launcher read both +//! again each time they open something, so a change applies to the next file +//! opened or the next search. + +use crate::*; +use pdfs_core::menu::{PromptConfig, PromptMode}; +use pdfs_core::opener::{ + OpenRule, OpenWith, command_available, format_command, parse_command, resolve_terminal, +}; + +/// The widgets of the Apps page, built with the rest of the dialog and wired +/// once the app's state exists. +pub(crate) struct AppsPrefs { + pub(crate) page: adw::PreferencesPage, + rules: adw::PreferencesGroup, + add_rule: gtk4::Button, + other_row: adw::EntryRow, + terminal_row: adw::EntryRow, + mode_row: adw::ComboRow, + menu_row: adw::EntryRow, + prompt_terminal_row: adw::EntryRow, + limit_row: adw::SpinRow, + icons_row: adw::SwitchRow, +} + +/// The launcher modes in the order the combo row lists them. +const MODES: [PromptMode; 3] = [PromptMode::Gtk, PromptMode::Dmenu, PromptMode::Fzf]; + +/// The file classes a rule can name, with what the dialog calls them. +fn classes() -> [(&'static str, String); 6] { + [ + ("@text", gettext("Text")), + ("@document", gettext("Documents")), + ("@image", gettext("Images")), + ("@media", gettext("Audio and Video")), + ("@dir", gettext("Folders")), + ("@any", gettext("Everything")), + ] +} + +pub(crate) fn build_apps_prefs() -> AppsPrefs { + let add_rule = gtk4::Button::builder() + .label(gettext("Add Rule…")) + .valign(gtk4::Align::Center) + .build(); + add_rule.add_css_class("flat"); + let rules = adw::PreferencesGroup::builder() + .title(gettext("Open Files With")) + .description(gettext( + "For files opened from Proton Drive and the search launcher. The first rule that matches a file decides; other files open in their default app.", + )) + .header_suffix(&add_rule) + .build(); + + let other_row = adw::EntryRow::builder() + .title(gettext("Other files (empty: default app)")) + .show_apply_button(true) + .build(); + other_row.add_css_class("monospace"); + let terminal_row = adw::EntryRow::builder() + .title(terminal_title()) + .show_apply_button(true) + .build(); + terminal_row.add_css_class("monospace"); + let other = adw::PreferencesGroup::new(); + other.add(&other_row); + other.add(&terminal_row); + + let mode_row = adw::ComboRow::builder() + .title(gettext("Opens as")) + .model(>k4::StringList::new(&[ + &gettext("Window"), + &gettext("Your Launcher"), + &gettext("fzf in a Terminal"), + ])) + .build(); + let menu_row = adw::EntryRow::builder() + .title(gettext("Launcher command (empty: find one)")) + .show_apply_button(true) + .build(); + menu_row.add_css_class("monospace"); + let prompt_terminal_row = adw::EntryRow::builder() + .title(terminal_title()) + .show_apply_button(true) + .build(); + prompt_terminal_row.add_css_class("monospace"); + let limit_row = adw::SpinRow::builder() + .title(gettext("Results shown")) + .adjustment(>k4::Adjustment::new( + pdfs_core::menu::DEFAULT_MENU_LIMIT as f64, + 5.0, + 500.0, + 5.0, + 25.0, + 0.0, + )) + .build(); + let icons_row = adw::SwitchRow::builder() + .title(gettext("Show icons")) + .subtitle(gettext("When the launcher can show them")) + .build(); + let launcher = adw::PreferencesGroup::builder() + .title(gettext("Search Launcher")) + .description(gettext( + "How “pdfs-prompt” shows search results, for example from a keyboard shortcut. Your launcher can be any program that reads choices like dmenu, such as rofi, wofi, fuzzel or bemenu.", + )) + .build(); + launcher.add(&mode_row); + launcher.add(&menu_row); + launcher.add(&prompt_terminal_row); + launcher.add(&limit_row); + launcher.add(&icons_row); + + let page = adw::PreferencesPage::builder() + .title(gettext("Apps")) + .name("apps") + .icon_name("application-x-executable-symbolic") + .build(); + page.add(&rules); + page.add(&other); + page.add(&launcher); + AppsPrefs { + page, + rules, + add_rule, + other_row, + terminal_row, + mode_row, + menu_row, + prompt_terminal_row, + limit_row, + icons_row, + } +} + +/// "Terminal", naming the one used when the row is left empty. +fn terminal_title() -> String { + match resolve_terminal() { + // Translators: {terminal} is a terminal command line such as "foot -e". + Some(found) => gettext_f( + "Terminal (empty: {terminal})", + &[("terminal", &format_command(&found))], + ), + None => gettext("Terminal"), + } +} + +/// Shared by every handler of the page. +struct Apps { + ui: Rc, + widgets: AppsPrefs, + rule_rows: RefCell>, +} + +/// Fill the page from the config and connect its controls. +pub(crate) fn wire_apps_prefs(ui: &Rc, widgets: AppsPrefs) { + let apps = Rc::new(Apps { + ui: ui.clone(), + widgets, + rule_rows: RefCell::default(), + }); + let config = ui.dirs.load_config(); + let open_with = config.resolved_open_with(); + let prompt = config.resolved_prompt(); + let w = &apps.widgets; + + let suppress = ui.status.settings_suppress.replace(true); + paint_rules(&apps, &open_with); + w.other_row + .set_text(&format_command(open_with.default.as_deref().unwrap_or(&[]))); + w.terminal_row.set_text(&format_command( + open_with.terminal.as_deref().unwrap_or(&[]), + )); + let mode = prompt.resolved_mode(); + w.mode_row + .set_selected(MODES.iter().position(|m| *m == mode).unwrap_or_default() as u32); + w.menu_row + .set_text(&format_command(prompt.menu.as_deref().unwrap_or(&[]))); + w.prompt_terminal_row + .set_text(&format_command(prompt.terminal.as_deref().unwrap_or(&[]))); + w.limit_row.set_value(prompt.resolved_menu_limit() as f64); + w.icons_row.set_active(prompt.resolved_menu_icons()); + show_mode_rows(w, mode); + ui.status.settings_suppress.set(suppress); + + let a = apps.clone(); + w.add_rule + .connect_clicked(move |_| edit_rule(&a, None, OpenRule::default())); + + // Command lines: checked as typed, saved on apply. Empty means "unset". + connect_command_row(&apps, &w.other_row, |config, argv| { + open_with_of(config).default = argv; + }); + connect_command_row(&apps, &w.terminal_row, |config, argv| { + open_with_of(config).terminal = argv; + }); + connect_command_row(&apps, &w.menu_row, |config, argv| { + prompt_of(config).menu = argv; + }); + connect_command_row(&apps, &w.prompt_terminal_row, |config, argv| { + prompt_of(config).terminal = argv; + }); + + let a = apps.clone(); + w.mode_row.connect_selected_notify(move |row| { + if a.ui.status.settings_suppress.get() { + return; + } + let mode = MODES + .get(row.selected() as usize) + .copied() + .unwrap_or_default(); + show_mode_rows(&a.widgets, mode); + save(&a, move |config| { + prompt_of(config).mode = (mode != PromptMode::default()).then_some(mode); + }); + }); + let a = apps.clone(); + w.limit_row.connect_value_notify(move |row| { + if a.ui.status.settings_suppress.get() { + return; + } + let limit = row.value().round() as usize; + save(&a, move |config| { + prompt_of(config).menu_limit = + (limit != pdfs_core::menu::DEFAULT_MENU_LIMIT).then_some(limit); + }); + }); + let a = apps.clone(); + w.icons_row.connect_active_notify(move |row| { + if a.ui.status.settings_suppress.get() { + return; + } + let on = row.is_active(); + save(&a, move |config| { + prompt_of(config).menu_icons = (!on).then_some(false) + }); + }); +} + +/// Show only the rows the chosen launcher mode uses. +fn show_mode_rows(w: &AppsPrefs, mode: PromptMode) { + w.menu_row.set_visible(mode == PromptMode::Dmenu); + w.icons_row.set_visible(mode == PromptMode::Dmenu); + w.prompt_terminal_row.set_visible(mode == PromptMode::Fzf); + w.limit_row.set_visible(mode != PromptMode::Gtk); +} + +/// The `open_with` section, created when absent. +fn open_with_of(config: &mut pdfs_core::config::AppConfig) -> &mut OpenWith { + config.open_with.get_or_insert_with(OpenWith::default) +} + +/// The `prompt` section, created when absent. +fn prompt_of(config: &mut pdfs_core::config::AppConfig) -> &mut PromptConfig { + config.prompt.get_or_insert_with(PromptConfig::default) +} + +/// Save a change, then drop sections left at their defaults so the file only +/// holds what the user chose. Says whether it saved. +fn save(apps: &Rc, change: impl FnOnce(&mut pdfs_core::config::AppConfig)) -> bool { + let saved = apps.ui.dirs.update_config(|config| { + change(config); + if config.open_with.as_ref() == Some(&OpenWith::default()) { + config.open_with = None; + } + if config.prompt.as_ref() == Some(&PromptConfig::default()) { + config.prompt = None; + } + config.resolved_open_with() + }); + match saved { + Ok(open_with) => { + paint_rules(apps, &open_with); + true + } + Err(e) => { + prefs_toast( + &apps.ui, + &gettext_f( + "{what}: {detail}", + &[ + ("what", &gettext("Couldn't save the setting")), + ("detail", &e.to_string()), + ], + ), + ); + false + } + } +} + +/// Check a command line as it is typed, and save it when applied. +fn connect_command_row( + apps: &Rc, + row: &adw::EntryRow, + store: impl Fn(&mut pdfs_core::config::AppConfig, Option>) + 'static, +) { + row.connect_changed(|row| mark_command(row, false)); + let a = apps.clone(); + row.connect_apply(move |row| { + let Some(argv) = parse_command(&row.text()) else { + prefs_toast(&a.ui, &gettext("A quote is not closed in the command")); + return; + }; + if !argv.is_empty() && !command_available(&argv) { + prefs_toast( + &a.ui, + // Translators: {program} is a program name such as "nvim". + &gettext_f( + "“{program}” isn't installed; saved anyway", + &[("program", &argv[0])], + ), + ); + } + let argv = (!argv.is_empty()).then_some(argv); + save(&a, |config| store(config, argv)); + mark_command(row, true); + }); +} + +/// Mark a command line that doesn't parse as an error, and, once `applied`, +/// one whose program isn't found as a warning. +fn mark_command(row: &adw::EntryRow, applied: bool) { + row.remove_css_class("error"); + row.remove_css_class("warning"); + match parse_command(&row.text()) { + None => row.add_css_class("error"), + Some(argv) if applied && !argv.is_empty() && !command_available(&argv) => { + row.add_css_class("warning") + } + Some(_) => {} + } +} + +/// Show the rules, one row each, in the order they are tried. +fn paint_rules(apps: &Rc, open_with: &OpenWith) { + let group = &apps.widgets.rules; + for row in apps.rule_rows.borrow_mut().drain(..) { + group.remove(&row); + } + let count = open_with.rules.len(); + if count == 0 { + let row = adw::ActionRow::builder() + .title(gettext("No rules: every file opens in its default app")) + .build(); + row.add_css_class("dim-label"); + group.add(&row); + apps.rule_rows.borrow_mut().push(row); + return; + } + let names = classes(); + for (index, rule) in open_with.rules.iter().enumerate() { + let patterns: Vec = rule + .patterns + .iter() + .map(|p| { + names + .iter() + .find(|(class, _)| class == p) + .map_or_else(|| p.clone(), |(_, name)| name.clone()) + }) + .collect(); + let command = format_command(&rule.command); + let subtitle = if rule.terminal { + // Translators: {command} is a command line such as "nvim". + gettext_f("{command}, in a terminal", &[("command", &command)]) + } else { + command + }; + let row = adw::ActionRow::builder() + .title(patterns.join(", ")) + .subtitle(subtitle) + .use_markup(false) + .activatable(true) + .build(); + if !command_available(&rule.command) { + let warning = gtk4::Image::builder() + .icon_name("dialog-warning-symbolic") + // Translators: {program} is a program name such as "nvim". + .tooltip_text(gettext_f( + "“{program}” isn't installed", + &[("program", rule.command.first().map_or("", String::as_str))], + )) + .build(); + warning.add_css_class("warning"); + row.add_prefix(&warning); + } + let up = rule_button("go-up-symbolic", &gettext("Move Up"), index > 0); + let down = rule_button("go-down-symbolic", &gettext("Move Down"), index + 1 < count); + let remove = rule_button("user-trash-symbolic", &gettext("Remove"), true); + let a = apps.clone(); + up.connect_clicked(move |_| { + save(&a, |config| { + open_with_of(config).rules.swap(index - 1, index) + }); + }); + let a = apps.clone(); + down.connect_clicked(move |_| { + save(&a, |config| { + open_with_of(config).rules.swap(index, index + 1) + }); + }); + let a = apps.clone(); + remove.connect_clicked(move |_| { + save(&a, |config| { + let rules = &mut open_with_of(config).rules; + if index < rules.len() { + rules.remove(index); + } + }); + }); + row.add_suffix(&up); + row.add_suffix(&down); + row.add_suffix(&remove); + let a = apps.clone(); + let rule = rule.clone(); + row.connect_activated(move |_| edit_rule(&a, Some(index), rule.clone())); + group.add(&row); + apps.rule_rows.borrow_mut().push(row); + } +} + +fn rule_button(icon: &str, tooltip: &str, sensitive: bool) -> gtk4::Button { + let button = gtk4::Button::builder() + .icon_name(icon) + .tooltip_text(tooltip) + .valign(gtk4::Align::Center) + .sensitive(sensitive) + .build(); + button.add_css_class("flat"); + button +} + +/// Split a typed pattern list into patterns, dropping empty ones. +fn split_patterns(text: &str) -> Vec { + text.split(',') + .map(str::trim) + .filter(|p| !p.is_empty()) + .map(str::to_string) + .collect() +} + +/// Edit one rule, or a new one when `index` is `None`, in a dialog over the +/// Preferences. +fn edit_rule(apps: &Rc, index: Option, rule: OpenRule) { + let patterns_row = adw::EntryRow::builder() + .title(gettext("Files, such as *.md, *.txt")) + .text(rule.patterns.join(", ")) + .build(); + patterns_row.add_css_class("monospace"); + let chips = gtk4::FlowBox::builder() + .selection_mode(gtk4::SelectionMode::None) + .column_spacing(6) + .row_spacing(6) + .max_children_per_line(6) + .margin_top(6) + .build(); + for (class, name) in classes() { + let chip = gtk4::Button::builder().label(&name).build(); + chip.add_css_class("pill"); + chip.add_css_class("small"); + let row = patterns_row.clone(); + chip.connect_clicked(move |_| { + let mut list = split_patterns(&row.text()); + if !list.iter().any(|p| p == class) { + list.push(class.to_string()); + } + row.set_text(&list.join(", ")); + }); + chips.insert(&chip, -1); + } + let command_row = adw::EntryRow::builder() + .title(gettext("Command, such as nvim or code --wait")) + .text(format_command(&rule.command)) + .build(); + command_row.add_css_class("monospace"); + let terminal_row = adw::SwitchRow::builder() + .title(gettext("Run in a terminal")) + .subtitle(gettext("For programs that run in a terminal, such as vim")) + .active(rule.terminal) + .build(); + let hint = gtk4::Label::builder() + .wrap(true) + .xalign(0.0) + .margin_top(6) + .build(); + hint.add_css_class("caption"); + hint.add_css_class("dim-label"); + + let group = adw::PreferencesGroup::new(); + group.add(&patterns_row); + let files = adw::PreferencesGroup::builder() + .description(gettext("Or pick kinds of files:")) + .build(); + files.add(&chips); + let program = adw::PreferencesGroup::new(); + program.add(&command_row); + program.add(&terminal_row); + program.add(&hint); + let page = adw::PreferencesPage::new(); + page.add(&group); + page.add(&files); + page.add(&program); + + let cancel = gtk4::Button::with_label(&gettext("Cancel")); + let save_button = gtk4::Button::with_label(&gettext("Save")); + save_button.add_css_class("suggested-action"); + let header = adw::HeaderBar::builder() + .show_start_title_buttons(false) + .show_end_title_buttons(false) + .build(); + header.pack_start(&cancel); + header.pack_end(&save_button); + let view = adw::ToolbarView::new(); + view.add_top_bar(&header); + view.set_content(Some(&page)); + let dialog = adw::Dialog::builder() + .title(if index.is_some() { + gettext("Edit Rule") + } else { + gettext("New Rule") + }) + .content_width(480) + .child(&view) + .build(); + + // Save only a rule that matches something and runs something. + let check = { + let (patterns_row, command_row, save_button, hint) = ( + patterns_row.clone(), + command_row.clone(), + save_button.clone(), + hint.clone(), + ); + move || { + let patterns = split_patterns(&patterns_row.text()); + let argv = parse_command(&command_row.text()); + command_row.remove_css_class("error"); + hint.set_text(""); + match &argv { + None => { + command_row.add_css_class("error"); + hint.set_text(&gettext("A quote is not closed in the command.")); + } + Some(argv) if !argv.is_empty() && !command_available(argv) => { + hint.set_text(&gettext_f( + "“{program}” isn't installed.", + &[("program", &argv[0])], + )); + } + Some(_) => {} + } + save_button + .set_sensitive(!patterns.is_empty() && argv.is_some_and(|argv| !argv.is_empty())); + } + }; + check(); + let c = check.clone(); + patterns_row.connect_changed(move |_| c()); + command_row.connect_changed(move |_| check()); + + let d = dialog.clone(); + cancel.connect_clicked(move |_| { + d.close(); + }); + let (a, d) = (apps.clone(), dialog.clone()); + save_button.connect_clicked(move |_| { + let Some(command) = parse_command(&command_row.text()) else { + return; + }; + let rule = OpenRule { + patterns: split_patterns(&patterns_row.text()), + command, + terminal: terminal_row.is_active(), + }; + let saved = save(&a, move |config| { + let rules = &mut open_with_of(config).rules; + match index { + Some(i) if i < rules.len() => rules[i] = rule, + _ => rules.push(rule), + } + }); + if saved { + d.close(); + } + }); + dialog.present(Some(&apps.ui.status.prefs)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn typed_patterns_are_split_on_commas() { + assert_eq!( + split_patterns(" *.md, @text ,, notes-*.txt "), + ["*.md", "@text", "notes-*.txt"] + ); + assert!(split_patterns(" , ").is_empty()); + } +} diff --git a/crates/pdfs-gui/src/app/pages/preferences_sync.rs b/crates/pdfs-gui/src/app/pages/preferences_sync.rs new file mode 100644 index 0000000..cce5da7 --- /dev/null +++ b/crates/pdfs-gui/src/app/pages/preferences_sync.rs @@ -0,0 +1,346 @@ +//! Preferences → Sync: the patterns every synced folder leaves out +//! (`ignore_patterns`) and what the conflict sweep may do (`conflict_sweep`). +//! +//! Both live in `config.json`. The daemon reads the patterns again on every +//! sync pass, so a change here is followed by a Sync Now and applies at once; +//! the sweep mode is read when the service starts, so a change offers a +//! restart, as the mountpoint does. + +use crate::*; +use pdfs_core::config::SweepMode; +use pdfs_core::syncignore::{DEFAULT_IGNORE_PATTERNS, check_pattern}; + +/// The widgets of the Sync page, built with the rest of the dialog and wired +/// once the app's state exists. +pub(crate) struct SyncPrefs { + pub(crate) page: adw::PreferencesPage, + patterns: adw::PreferencesGroup, + add_row: adw::EntryRow, + reset: gtk4::Button, + sweep_row: adw::ComboRow, + /// Whether the built-in patterns are shown, kept across repaints. + builtin_expanded: Cell, +} + +/// The sweep modes in the order the combo row lists them. +const SWEEP_MODES: [SweepMode; 3] = [SweepMode::Report, SweepMode::Enforce, SweepMode::Off]; + +pub(crate) fn build_sync_prefs() -> SyncPrefs { + let reset = gtk4::Button::builder() + .label(gettext("Reset")) + .tooltip_text(gettext("Use the built-in patterns again")) + .valign(gtk4::Align::Center) + .build(); + reset.add_css_class("flat"); + let patterns = adw::PreferencesGroup::builder() + .title(gettext("Excluded Files")) + .description(gettext( + "Files and folders matching these patterns are not synced in any synced folder. Use * for any characters, end a pattern with / to match folders only, and start it with ! to include something again. Excluding never deletes anything from Proton Drive.", + )) + .header_suffix(&reset) + .build(); + let add_row = adw::EntryRow::builder() + .title(gettext("Add Pattern")) + .show_apply_button(true) + .build(); + add_row.add_css_class("monospace"); + patterns.add(&add_row); + + let sweep_row = adw::ComboRow::builder() + .title(gettext("Identical conflict copies")) + .subtitle(gettext( + "A copy made when a file changed in two places, found to be the same as the file", + )) + .model(>k4::StringList::new(&[ + &gettext("Report Only"), + &gettext("Move to Trash"), + &gettext("Off"), + ])) + .build(); + let conflicts = adw::PreferencesGroup::builder() + .title(gettext("Conflict Copies")) + .build(); + conflicts.add(&sweep_row); + + let page = adw::PreferencesPage::builder() + .title(gettext("Sync")) + .name("sync") + .icon_name("pdfs-sync-symbolic") + .build(); + page.add(&patterns); + page.add(&conflicts); + SyncPrefs { + page, + patterns, + add_row, + reset, + sweep_row, + builtin_expanded: Cell::new(false), + } +} + +/// Fill the page from the config and connect its controls. +pub(crate) fn wire_sync_prefs(ui: &Rc, widgets: SyncPrefs) { + let widgets = Rc::new(widgets); + let rows: Rc>> = Rc::default(); + let config = ui.dirs.load_config(); + paint_patterns(ui, &widgets, &rows, &config); + // The Sync page and `pdfs sync ignore` change the patterns too: show the + // saved ones each time the page comes up. + let (ui_map, w_map, rows_map) = (ui.clone(), widgets.clone(), rows.clone()); + widgets.page.connect_map(move |_| { + let config = ui_map.dirs.load_config(); + paint_patterns(&ui_map, &w_map, &rows_map, &config); + }); + + let mode = config.conflict_sweep.unwrap_or_default(); + let suppress = ui.status.settings_suppress.replace(true); + widgets.sweep_row.set_selected( + SWEEP_MODES + .iter() + .position(|m| *m == mode) + .unwrap_or_default() as u32, + ); + ui.status.settings_suppress.set(suppress); + + widgets.add_row.connect_changed(|row| { + let text = row.text(); + if text.trim().is_empty() || check_pattern(text.trim()).is_ok() { + row.remove_css_class("error"); + } else { + row.add_css_class("error"); + } + }); + let (ui_add, w_add, rows_add) = (ui.clone(), widgets.clone(), rows.clone()); + widgets.add_row.connect_apply(move |row| { + let pattern = row.text().trim().to_string(); + if pattern.is_empty() { + return; + } + if let Err(e) = check_pattern(&pattern) { + tracing::debug!(%pattern, error = %e, "not a valid ignore pattern"); + prefs_toast( + &ui_add, + // Translators: {pattern} is what the user typed as a pattern. + &gettext_f( + "“{pattern}” isn't a valid pattern", + &[("pattern", &pattern)], + ), + ); + return; + } + let added = change_patterns(&ui_add, &w_add, &rows_add, |list| { + if !list.contains(&pattern) { + list.push(pattern.clone()); + } + }); + if added { + row.set_text(""); + } + }); + + let (ui_reset, w_reset, rows_reset) = (ui.clone(), widgets.clone(), rows.clone()); + widgets.reset.connect_clicked(move |_| { + let (ui, w, rows) = (ui_reset.clone(), w_reset.clone(), rows_reset.clone()); + confirm_destructive( + &ui_reset.status.prefs, + &gettext("Reset Excluded Files?"), + &gettext("Your own patterns are removed, and the built-in ones apply again."), + &gettext("Reset"), + move || { + change_patterns(&ui, &w, &rows, |list| { + *list = default_patterns(); + }); + }, + ); + }); + + let ui_sweep = ui.clone(); + widgets.sweep_row.connect_selected_notify(move |row| { + if ui_sweep.status.settings_suppress.get() { + return; + } + let Some(mode) = SWEEP_MODES.get(row.selected() as usize).copied() else { + return; + }; + let stored = (mode != SweepMode::default()).then_some(mode); + match ui_sweep + .dirs + .update_config(|config| config.conflict_sweep = stored) + { + Ok(()) => offer_restart(&ui_sweep), + Err(e) => prefs_toast( + &ui_sweep, + &gettext_f( + "{what}: {detail}", + &[ + ("what", &gettext("Couldn't save the setting")), + ("detail", &e.to_string()), + ], + ), + ), + } + }); +} + +fn default_patterns() -> Vec { + DEFAULT_IGNORE_PATTERNS + .iter() + .map(|p| (*p).to_string()) + .collect() +} + +/// Apply `edit` to the patterns in effect and save them, then show them and +/// have every synced folder sync with them. Says whether it saved. +fn change_patterns( + ui: &Rc, + widgets: &Rc, + rows: &Rc>>, + edit: impl FnOnce(&mut Vec), +) -> bool { + match ui.dirs.change_ignore_patterns(edit) { + Ok(config) => { + paint_patterns(ui, widgets, rows, &config); + // Best effort: the next pass picks the change up anyway. + let _ = spawn_request(ui.dirs.control_socket(), Request::SyncNow { id: None }); + true + } + Err(e) => { + prefs_toast( + ui, + &gettext_f( + "{what}: {detail}", + &[ + ("what", &gettext("Couldn't save the patterns")), + ("detail", &e.to_string()), + ], + ), + ); + false + } + } +} + +/// Show the patterns in effect below the entry row: the user's own one row +/// each, then the built-in ones folded into one row, since they are many and +/// rarely changed. +fn paint_patterns( + ui: &Rc, + widgets: &Rc, + rows: &Rc>>, + config: &pdfs_core::config::AppConfig, +) { + for row in rows.borrow_mut().drain(..) { + widgets.patterns.remove(&row); + } + widgets + .reset + .set_sensitive(config.ignore_patterns.is_some()); + let list = config.resolved_ignore_patterns(); + let (builtin, own): (Vec, Vec) = list + .into_iter() + .partition(|p| DEFAULT_IGNORE_PATTERNS.contains(&p.as_str())); + for pattern in own { + let row = pattern_row(ui, widgets, rows, pattern); + widgets.patterns.add(&row); + rows.borrow_mut().push(row.upcast()); + } + // Every built-in pattern, on or off, so one turned off can be turned on + // again without losing the user's own. + let on = builtin.len(); + let total = DEFAULT_IGNORE_PATTERNS.len(); + let expander = adw::ExpanderRow::builder() + .title(gettext("Built-in patterns")) + // Translators: {on} and {total} are numbers of patterns. + .subtitle(gettext_f( + "{on} of {total} on: version control, dependency and build folders, and what editors, downloads and systems leave behind", + &[("on", &on.to_string()), ("total", &total.to_string())], + )) + .expanded(widgets.builtin_expanded.get()) + .build(); + let w = widgets.clone(); + expander.connect_expanded_notify(move |row| w.builtin_expanded.set(row.is_expanded())); + for pattern in DEFAULT_IGNORE_PATTERNS { + let row = adw::SwitchRow::builder() + .title(format!("{}", glib::markup_escape_text(pattern))) + .use_markup(true) + .active(builtin.iter().any(|p| p == pattern)) + .build(); + let (ui, w, r) = (ui.clone(), widgets.clone(), rows.clone()); + row.connect_active_notify(move |row| { + let active = row.is_active(); + // Repainting replaces this row; let the switch finish first. + let (ui, w, r) = (ui.clone(), w.clone(), r.clone()); + glib::idle_add_local_once(move || { + change_patterns(&ui, &w, &r, move |list| { + list.retain(|p| p != pattern); + if active { + list.push((*pattern).to_string()); + } + }); + }); + }); + expander.add_row(&row); + } + widgets.patterns.add(&expander); + rows.borrow_mut().push(expander.upcast()); +} + +/// One pattern, in monospace, with a button that removes it. +fn pattern_row( + ui: &Rc, + widgets: &Rc, + rows: &Rc>>, + pattern: String, +) -> adw::ActionRow { + let row = adw::ActionRow::builder() + .title(format!("{}", glib::markup_escape_text(&pattern))) + .use_markup(true) + .build(); + let remove = gtk4::Button::builder() + .icon_name("user-trash-symbolic") + .tooltip_text(gettext("Remove")) + .valign(gtk4::Align::Center) + .build(); + remove.add_css_class("flat"); + let (ui, w, r) = (ui.clone(), widgets.clone(), rows.clone()); + remove.connect_clicked(move |_| { + let pattern = pattern.clone(); + change_patterns(&ui, &w, &r, move |list| list.retain(|p| *p != pattern)); + }); + row.add_suffix(&remove); + row +} + +/// The sweep mode is read when the service starts; offer that restart. +fn offer_restart(ui: &Rc) { + let confirm = adw::AlertDialog::builder() + .heading(gettext("Restart Proton Drive?")) + .body(gettext( + "The new setting applies after Proton Drive restarts.", + )) + .build(); + confirm.add_response("later", &gettext("Later")); + confirm.add_response("restart", &gettext("Restart Now")); + confirm.set_response_appearance("restart", adw::ResponseAppearance::Suggested); + confirm.set_default_response(Some("restart")); + confirm.set_close_response("later"); + let ui_restart = ui.clone(); + confirm.connect_response(None, move |_, resp| { + if resp == "restart" { + restart_service_then(&ui_restart, |_| {}); + } + }); + confirm.present(Some(&ui.status.prefs)); +} + +/// A toast on the Preferences dialog, which covers the window's own toasts. +pub(crate) fn prefs_toast(ui: &Rc, message: &str) { + ui.status.prefs.add_toast( + adw::Toast::builder() + .title(message) + .use_markup(false) + .timeout(6) + .build(), + ); +} diff --git a/crates/pdfs-gui/src/app/pages/shared.rs b/crates/pdfs-gui/src/app/pages/shared.rs index 2bfd3d5..24065c9 100644 --- a/crates/pdfs-gui/src/app/pages/shared.rs +++ b/crates/pdfs-gui/src/app/pages/shared.rs @@ -279,10 +279,7 @@ pub(crate) fn wire_shared(ui: &Rc, widgets: &SharedWidgets) { })); let ui_back = ui.clone(); - widgets.back.connect_clicked(move |_| { - ui_back.shared.nav.borrow_mut().pop(); - load_shared(&ui_back); - }); + widgets.back.connect_clicked(move |_| shared_back(&ui_back)); let ui_retry = ui.clone(); widgets .retry @@ -318,6 +315,14 @@ pub(crate) fn shared_status(ui: &Rc, icon: &str, title: &str, description: & ui.shared.content.set_visible_child_name("status"); } +/// Back out of a shared folder to the level above: the page's Back button, and +/// Alt+Left or Backspace. A no-op at the top. +pub(crate) fn shared_back(ui: &Rc) { + if ui.shared.nav.borrow_mut().pop().is_some() { + load_shared(ui); + } +} + /// Fetch the top level (shared-with-me, invitations, saved links) in parallel /// and repaint the page once all three land. /// @@ -443,6 +448,7 @@ fn repaint_shared_files(ui: &Rc, entries: &[DirEntry]) { let nav = ui.shared.nav.borrow().clone(); repaint_shared_crumb(ui, &nav); let top = nav.is_empty(); + let another_folder = ui.shared.listed.borrow().as_ref() != Some(&nav); *ui.shared.listed.borrow_mut() = Some(nav); if entries.is_empty() { ui.shared.files.model.remove_all(); @@ -467,8 +473,11 @@ fn repaint_shared_files(ui: &Rc, entries: &[DirEntry]) { } ui.shared.files_content.set_visible_child_name("list"); // Only the rows that changed are swapped, so a refresh keeps the selection - // and the scroll position. + // and the scroll position. Another folder starts from its first entry. replace_items(&ui.shared.files.model, entries); + if another_folder { + ui.shared.files.scroll_to_top(); + } } /// Rebuild the breadcrumb trail for `nav`: "Shared with me", then each folder diff --git a/crates/pdfs-gui/src/app/pages/status.rs b/crates/pdfs-gui/src/app/pages/status.rs index b4b2f65..2a82564 100644 --- a/crates/pdfs-gui/src/app/pages/status.rs +++ b/crates/pdfs-gui/src/app/pages/status.rs @@ -169,6 +169,9 @@ pub(crate) struct MainWidgets { pub(crate) tray_row: adw::SwitchRow, pub(crate) update_row: adw::SwitchRow, pub(crate) language_row: adw::ComboRow, + /// Preferences → Sync and → Apps, wired once the app's state exists. + pub(crate) sync_prefs: SyncPrefs, + pub(crate) apps_prefs: AppsPrefs, } /// Build the two surfaces that replaced the old Settings page: @@ -267,6 +270,7 @@ pub(crate) fn build_main_page() -> MainWidgets { let general = adw::PreferencesPage::builder() .title(gettext("General")) + .name("general") .icon_name("preferences-system-symbolic") .build(); general.add(&startup_group); @@ -335,6 +339,7 @@ pub(crate) fn build_main_page() -> MainWidgets { let storage = adw::PreferencesPage::builder() .title(gettext("Storage")) + .name("storage") .icon_name("drive-harddisk-symbolic") .build(); storage.add(&storage_group); @@ -343,7 +348,11 @@ pub(crate) fn build_main_page() -> MainWidgets { let prefs = adw::PreferencesDialog::builder() .search_enabled(false) .build(); + let sync_prefs = build_sync_prefs(); + let apps_prefs = build_apps_prefs(); prefs.add(&general); + prefs.add(&sync_prefs.page); + prefs.add(&apps_prefs.page); prefs.add(&storage); // ---- Sidebar footer @@ -463,6 +472,8 @@ pub(crate) fn build_main_page() -> MainWidgets { tray_row, update_row, language_row, + sync_prefs, + apps_prefs, } } @@ -678,9 +689,10 @@ pub(crate) fn wire_settings( let rows = list.model.n_items(); list.model.items_changed(0, rows, rows); } - let mut config = ui_theme.dirs.load_config(); - config.proton_theme = Some(on); - if let Err(e) = ui_theme.dirs.save_config(&config) { + if let Err(e) = ui_theme + .dirs + .update_config(|config| config.proton_theme = Some(on)) + { toast_error( &ui_theme, &gettext("Couldn't save the theme"), @@ -700,9 +712,7 @@ pub(crate) fn wire_settings( .gallery .places_map .set_online(on, ui_map.dirs.cache_dir()); - let mut config = ui_map.dirs.load_config(); - config.online_map = on; - if let Err(e) = ui_map.dirs.save_config(&config) { + if let Err(e) = ui_map.dirs.update_config(|config| config.online_map = on) { toast_error( &ui_map, &gettext("Couldn't save the map setting"), @@ -717,9 +727,10 @@ pub(crate) fn wire_settings( if ui_update.status.settings_suppress.get() { return; } - let mut config = ui_update.dirs.load_config(); - config.update_check = row.is_active(); - if let Err(e) = ui_update.dirs.save_config(&config) { + if let Err(e) = ui_update + .dirs + .update_config(|config| config.update_check = row.is_active()) + { toast_error( &ui_update, &gettext("Couldn't save the update setting"), @@ -739,9 +750,10 @@ pub(crate) fn wire_settings( return; } let show = row.is_active(); - let mut config = ui_tray.dirs.load_config(); - config.tray_hidden = !show; - if let Err(e) = ui_tray.dirs.save_config(&config) { + if let Err(e) = ui_tray + .dirs + .update_config(|config| config.tray_hidden = !show) + { toast_error( &ui_tray, &gettext("Couldn't save the tray setting"), @@ -763,9 +775,9 @@ pub(crate) fn wire_settings( if ui_language.status.settings_suppress.get() { return; } - let mut config = ui_language.dirs.load_config(); - config.language = language_at(row.selected()).map(str::to_string); - if let Err(e) = ui_language.dirs.save_config(&config) { + if let Err(e) = ui_language.dirs.update_config(|config| { + config.language = language_at(row.selected()).map(str::to_string) + }) { toast_error( &ui_language, &gettext("Couldn't save the language"), @@ -864,9 +876,10 @@ fn use_mountpoint(ui: &Rc, path: &std::path::Path) { let path_str = path.display().to_string(); // Persist the choice to config so the next mount uses it. - let mut config = ui.dirs.load_config(); - config.mountpoint = Some(path_str.clone()); - if let Err(e) = ui.dirs.save_config(&config) { + if let Err(e) = ui + .dirs + .update_config(|config| config.mountpoint = Some(path_str.clone())) + { toast_error(ui, &gettext("Couldn't save the folder"), &e.to_string()); return; } @@ -1115,9 +1128,6 @@ pub(crate) fn repaint_transfers(ui: &Rc, items: &[TransferItem], jobs: &[Job // count falling to zero is what "the sync finished" looks like from here. // Jobs are deliberately not counted — a bulk upload retires its scan job and // starts its upload job mid-flight, which is not a thing finishing. - // The Import page reads its progress off the same snapshot rather than - // polling the daemon a second time for numbers already on the wire. - takeout_progress(ui, jobs); let previous = ui.status.active_transfers.replace(items.len()); if !items.is_empty() { @@ -1211,13 +1221,18 @@ pub(crate) fn repaint_transfers(ui: &Rc, items: &[TransferItem], jobs: &[Job pub(crate) fn job_line(j: &JobItem) -> ActivityLine { let done = j.done.to_string(); let total = j.total.to_string(); + let (title, detail) = if j.title == IMPORT_JOB { + import_job_text(&j.detail) + } else { + (j.title.clone(), j.detail.clone()) + }; let args: [(&str, &str); 4] = [ - ("title", &j.title), - ("detail", &j.detail), + ("title", &title), + ("detail", &detail), ("done", &done), ("total", &total), ]; - let text = match (j.total > 0, j.detail.is_empty()) { + let text = match (j.total > 0, detail.is_empty()) { // Translators: a background job's progress. {title} is the job, such as // "Scanning Photos"; {done} and {total} are counts. (true, true) => gettext_f("{title} — {done} of {total}", &args), diff --git a/crates/pdfs-gui/src/app/pages/sync_exclusions.rs b/crates/pdfs-gui/src/app/pages/sync_exclusions.rs new file mode 100644 index 0000000..60eec7f --- /dev/null +++ b/crates/pdfs-gui/src/app/pages/sync_exclusions.rs @@ -0,0 +1,426 @@ +//! The Excluded Files group on the Sync page's Folders view: what is left out +//! of every synced folder and of each one on its own, with how many patterns +//! each has, and an Add Pattern dialog that asks where a pattern goes. +//! +//! The rows open the editors that already exist: Preferences → Sync for the +//! patterns of all folders, the Excluded Files dialog for a folder's own +//! `.pdfsignore`. Only folders kept on this computer are listed; an +//! online-only folder has no local files for patterns to leave out. With none +//! of those, the group still shows, so the patterns can be found and set up +//! before a folder is switched to Synced, and says why they do nothing yet. + +use crate::*; +use pdfs_core::syncignore::{FolderRules, add_folder_pattern, check_pattern}; + +pub(crate) struct ExclusionsState { + pub(crate) group: adw::PreferencesGroup, + pub(crate) add: gtk4::Button, + pub(crate) rows: RefCell>, + /// The synced folders kept on this computer the rows were built for, as + /// id and root. + pub(crate) folders: RefCell>, + /// Counts painted, so a slow read from an older paint does not land on + /// newer rows. + pub(crate) generation: Cell, +} + +pub(crate) fn build_exclusions_group() -> (adw::PreferencesGroup, gtk4::Button) { + let add = gtk4::Button::builder() + .child( + &adw::ButtonContent::builder() + .label(gettext("Add Pattern")) + .icon_name("list-add-symbolic") + .build(), + ) + .tooltip_text(gettext( + "Leave files matching a pattern out of all synced folders or one of them", + )) + .valign(gtk4::Align::Center) + .build(); + add.add_css_class("flat"); + let group = adw::PreferencesGroup::builder() + .title(gettext("Excluded Files")) + .description(gettext( + "Files matching these patterns stay on this computer and are not synced. Excluding never deletes anything from Proton Drive.", + )) + .header_suffix(&add) + .visible(false) + .build(); + (group, add) +} + +pub(crate) fn wire_exclusions(ui: &Rc) { + let ui_add = ui.clone(); + ui.locations + .exclusions + .add + .connect_clicked(move |_| present_add_pattern_dialog(&ui_add, None)); + // Preferences change the patterns of all folders; show the new count. + let ui_prefs = ui.clone(); + ui.status + .prefs + .connect_closed(move |_| refresh_exclusions(&ui_prefs)); +} + +/// Rebuild the rows for the folders in `items` when the set of synced folders +/// kept on this computer changed, or on the first paint. The tick repaints the folder list every few +/// seconds; the rows and their counts stay as they are in between. +pub(crate) fn paint_exclusions(ui: &Rc, items: &[MountSpec]) { + let folders: Vec<(i64, PathBuf)> = items + .iter() + .filter(|spec| spec.mode == MountMode::Mirror) + .filter_map(|spec| match spec.kind { + MountKind::Device { sync_folder_id } | MountKind::Attached { sync_folder_id } => { + Some((sync_folder_id, PathBuf::from(&spec.local_path))) + } + _ => None, + }) + .collect(); + let state = &ui.locations.exclusions; + if *state.folders.borrow() == folders && !state.rows.borrow().is_empty() { + return; + } + *state.folders.borrow_mut() = folders; + refresh_exclusions(ui); +} + +/// Build the rows again from the folders last painted, and read every count +/// anew: after a pattern was added or a list edited. +pub(crate) fn refresh_exclusions(ui: &Rc) { + let state = &ui.locations.exclusions; + for row in state.rows.borrow_mut().drain(..) { + state.group.remove(&row); + } + let folders = state.folders.borrow().clone(); + state.group.set_visible(true); + let generation = state.generation.get() + 1; + state.generation.set(generation); + + let config = ui.dirs.load_config(); + let all = adw::ActionRow::builder() + .title(gettext("All Synced Folders")) + .subtitle(glib::markup_escape_text(&all_folders_subtitle(&config)).as_str()) + .activatable(true) + .build(); + all.add_prefix(>k4::Image::from_icon_name("view-list-symbolic")); + all.add_suffix(>k4::Image::from_icon_name("go-next-symbolic")); + let ui_all = ui.clone(); + all.connect_activated(move |_| { + let prefs = &ui_all.status.prefs; + prefs.set_visible_page_name("sync"); + prefs.present(ui_window(&ui_all).as_ref()); + }); + state.group.add(&all); + let mut rows = vec![all]; + if folders.is_empty() { + let none = adw::ActionRow::builder() + .title(gettext("No folder is set to Synced")) + .subtitle(gettext( + "Patterns apply only to folders kept on this computer. Online-only folders upload every file saved in them.", + )) + .build(); + none.add_prefix(>k4::Image::from_icon_name("info-outline-symbolic")); + state.group.add(&none); + rows.push(none); + } + + let mut counted = Vec::new(); + for (id, root) in folders { + let name = root + .file_name() + .map(|name| name.to_string_lossy().into_owned()) + .unwrap_or_else(|| root.display().to_string()); + let row = adw::ActionRow::builder() + .title(glib::markup_escape_text(&name).as_str()) + .tooltip_text(root.display().to_string()) + .subtitle(glib::markup_escape_text(&tilde_path(&root.display().to_string())).as_str()) + .subtitle_lines(1) + .activatable(true) + .build(); + row.add_prefix(>k4::Image::from_icon_name("folder-symbolic")); + row.add_suffix(>k4::Image::from_icon_name("go-next-symbolic")); + let (ui_row, root_row) = (ui.clone(), root.clone()); + row.connect_activated(move |_| present_ignore_dialog(&ui_row, id, root_row.clone())); + state.group.add(&row); + counted.push((row.clone(), root)); + rows.push(row); + } + *state.rows.borrow_mut() = rows; + + // A folder may sit on a slow or sleeping disk: count off the main thread. + let ui = ui.clone(); + glib::spawn_future_local(async move { + let roots: Vec = counted.iter().map(|(_, root)| root.clone()).collect(); + let counts = gio::spawn_blocking(move || { + roots + .iter() + .map(|root| { + FolderRules::read(root) + .ok() + .map(|rules| rules.patterns().len()) + }) + .collect::>() + }) + .await + .unwrap_or_default(); + if ui.locations.exclusions.generation.get() != generation { + return; + } + for ((row, root), count) in counted.iter().zip(counts) { + let state = match count { + Some(0) => gettext("no own patterns"), + // Translators: how many patterns a synced folder has of its own. + Some(n) => ngettext_f("{n} own pattern", "{n} own patterns", n as u64, &[]), + None => gettext("own patterns unreadable"), + }; + // Translators: a synced folder's location and how many patterns of its own it has, such as "~/Projects · 2 own patterns". + let subtitle = gettext_f( + "{path} · {state}", + &[ + ("path", &tilde_path(&root.display().to_string())), + ("state", &state), + ], + ); + row.set_subtitle(glib::markup_escape_text(&subtitle).as_str()); + } + }); +} + +/// How many patterns apply to every synced folder, and whether they are the +/// built-in ones. +fn all_folders_subtitle(config: &pdfs_core::config::AppConfig) -> String { + let n = config.resolved_ignore_patterns().len() as u64; + match (&config.ignore_patterns, n) { + (_, 0) => gettext("No patterns: every file is synced"), + (None, n) => ngettext_f("{n} built-in pattern", "{n} built-in patterns", n, &[]), + (Some(_), n) => ngettext_f("{n} pattern", "{n} patterns", n, &[]), + } +} + +/// Ask for a pattern and where it applies: every synced folder, or one of +/// them (`folder`, preselected when given). +pub(crate) fn present_add_pattern_dialog(ui: &Rc, folder: Option) { + let folders = ui.locations.exclusions.folders.borrow().clone(); + let pattern_row = adw::EntryRow::builder().title(gettext("Pattern")).build(); + pattern_row.add_css_class("monospace"); + let mut places = vec![gettext("All synced folders")]; + places.extend( + folders + .iter() + .map(|(_, root)| tilde_path(&root.display().to_string())), + ); + let places: Vec<&str> = places.iter().map(String::as_str).collect(); + let scope_row = adw::ComboRow::builder() + .title(gettext("Apply To")) + .model(>k4::StringList::new(&places)) + .build(); + if let Some(at) = folder.and_then(|id| folders.iter().position(|(f, _)| *f == id)) { + scope_row.set_selected(at as u32 + 1); + } + let rows = adw::PreferencesGroup::new(); + rows.add(&pattern_row); + rows.add(&scope_row); + + let hint = gtk4::Label::builder() + .label(gettext( + "Use * for any characters, such as *.db, and end a pattern with / to match folders only, such as build/.", + )) + .wrap(true) + .xalign(0.0) + .build(); + hint.add_css_class("dim-label"); + hint.add_css_class("caption"); + let problem = gtk4::Label::builder() + .wrap(true) + .xalign(0.0) + .visible(false) + .build(); + problem.add_css_class("error"); + + let body = gtk4::Box::new(gtk4::Orientation::Vertical, 12); + body.set_margin_top(12); + body.set_margin_bottom(18); + body.set_margin_start(18); + body.set_margin_end(18); + body.append(&rows); + body.append(&hint); + body.append(&problem); + + let cancel = gtk4::Button::with_label(&gettext("Cancel")); + let add = gtk4::Button::with_label(&gettext("Add")); + add.add_css_class("suggested-action"); + add.set_sensitive(false); + let header = adw::HeaderBar::builder() + .show_start_title_buttons(false) + .show_end_title_buttons(false) + .build(); + header.pack_start(&cancel); + header.pack_end(&add); + let view = adw::ToolbarView::new(); + view.add_top_bar(&header); + view.set_content(Some(&body)); + let dialog = adw::Dialog::builder() + .title(gettext("Add Pattern")) + .content_width(460) + .child(&view) + .build(); + + let (add_check, problem_check) = (add.clone(), problem.clone()); + pattern_row.connect_changed(move |row| { + let verdict = pattern_problem(&row.text()); + match &verdict { + Some(message) if !row.text().trim().is_empty() => { + row.add_css_class("error"); + problem_check.set_text(message); + problem_check.set_visible(true); + } + _ => { + row.remove_css_class("error"); + problem_check.set_visible(false); + } + } + add_check.set_sensitive(verdict.is_none()); + }); + let d = dialog.clone(); + cancel.connect_clicked(move |_| { + d.close(); + }); + + let submit = { + let (ui, dialog, pattern_row, scope_row, add, problem) = ( + ui.clone(), + dialog.clone(), + pattern_row.clone(), + scope_row.clone(), + add.clone(), + problem.clone(), + ); + move || { + if !add.is_sensitive() { + return; + } + let pattern = pattern_row.text().trim().to_string(); + let target = match scope_row.selected() { + 0 => None, + at => folders.get(at as usize - 1).cloned(), + }; + // One click, one save: the button comes back if the save fails. + add.set_sensitive(false); + add_pattern(&ui, &dialog, &add, &problem, pattern, target); + } + }; + let submit_enter = submit.clone(); + pattern_row.connect_entry_activated(move |_| submit_enter()); + add.connect_clicked(move |_| submit()); + + dialog.present(ui_window(ui).as_ref()); + pattern_row.grab_focus(); +} + +/// What is wrong with `text` as a pattern to add, or `None` when it is one. +fn pattern_problem(text: &str) -> Option { + let text = text.trim(); + if text.is_empty() { + return Some(String::new()); + } + if text.starts_with('#') { + return Some(gettext( + "A line starting with # is a comment, not a pattern.", + )); + } + check_pattern(text).err().map(|e| { + tracing::debug!(pattern = %text, error = %e, "not a valid ignore pattern"); + // Translators: {pattern} is what the user typed as a pattern. + gettext_f("“{pattern}” isn't a valid pattern.", &[("pattern", text)]) + }) +} + +/// Save `pattern` for every synced folder (`target` is `None`) or for the +/// folder `target`, then close `dialog`, say so and sync. A failure shows in +/// `problem`, in the dialog, which stays open. +fn add_pattern( + ui: &Rc, + dialog: &adw::Dialog, + add: >k4::Button, + problem: >k4::Label, + pattern: String, + target: Option<(i64, PathBuf)>, +) { + let (ui, dialog, add, problem) = (ui.clone(), dialog.clone(), add.clone(), problem.clone()); + glib::spawn_future_local(async move { + let (id, place) = match &target { + None => (None, gettext("all synced folders")), + Some((id, root)) => (Some(*id), tilde_path(&root.display().to_string())), + }; + let added: Result = match target { + None => { + let mut added = false; + ui.dirs + .change_ignore_patterns(|list| { + if !list.contains(&pattern) { + list.push(pattern.clone()); + added = true; + } + }) + .map(|_| added) + .map_err(|e| e.to_string()) + } + Some((_, root)) => { + let p = pattern.clone(); + gio::spawn_blocking(move || add_folder_pattern(&root, &p)) + .await + .unwrap_or_else(|_| Err(std::io::Error::other("the save did not finish"))) + .map_err(|e| e.to_string()) + } + }; + match added { + Ok(added) => { + dialog.close(); + let message = if added { + // Translators: {pattern} is an ignore pattern, {place} "all synced folders" or a folder path. + gettext_f( + "“{pattern}” is now excluded in {place}", + &[("pattern", &pattern), ("place", &place)], + ) + } else { + gettext_f( + "“{pattern}” was already excluded in {place}", + &[("pattern", &pattern), ("place", &place)], + ) + }; + toast(&ui, &message); + refresh_exclusions(&ui); + if added { + // Best effort: the next pass picks the change up anyway. + let _ = spawn_request(ui.dirs.control_socket(), Request::SyncNow { id }); + } + } + Err(e) => { + add.set_sensitive(true); + problem.set_text(&gettext_f( + "{what}: {detail}", + &[ + ("what", &gettext("Couldn't save the patterns")), + ("detail", &e), + ], + )); + problem.set_visible(true); + } + } + }); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_real_patterns_can_be_added() { + assert!(pattern_problem("*.db").is_none()); + assert!(pattern_problem(" build/ ").is_none()); + assert!(pattern_problem("").is_some()); + assert!(pattern_problem(" ").is_some()); + assert!(pattern_problem("# note").is_some()); + assert!(pattern_problem("*.{jpg,png").is_some()); + } +} diff --git a/crates/pdfs-gui/src/app/pages/takeout.rs b/crates/pdfs-gui/src/app/pages/takeout.rs index 62a6049..37a4370 100644 --- a/crates/pdfs-gui/src/app/pages/takeout.rs +++ b/crates/pdfs-gui/src/app/pages/takeout.rs @@ -15,17 +15,30 @@ //! Import — staging is local, so a mis-drop costs nothing. //! //! The daemon owns the run itself ([`Request::ImportTakeout`]): this page only -//! stages paths, launches, asks [`Request::ImportStatus`] for the final report, -//! and reads live progress off the import's job in the transfer snapshot the -//! daemon already pushes (see [`takeout_progress`]). +//! stages paths, launches, and asks [`Request::ImportStatus`] for the step, the +//! running counts and the final report. It asks again whenever the import's job +//! moves in the transfer snapshot the daemon already pushes. A run the service +//! did not get to finish can be picked up with [`Request::ResumeImport`]. use crate::*; /// The title the daemon gives the import's job in [`Request::GetQueueStatus`]. -/// Matching on it is how this page borrows the progress the daemon already -/// pushes instead of asking for it separately. +/// Seeing it move is the cue to ask for the import's status; the Activity list +/// shows it under its translated name ([`import_job_text`]). pub(crate) const IMPORT_JOB: &str = "Importing Google Photos"; +/// The import job's title and detail in the user's language. The daemon names +/// its steps in English; the file being uploaded passes through as it is. +pub(crate) fn import_job_text(detail: &str) -> (String, String) { + let detail = match detail { + "reading archives" => gettext("Reading the archives"), + "checking for photos you already have" => gettext("Checking for photos you already have"), + "sorting photos into albums" => gettext("Sorting photos into albums"), + other => other.to_string(), + }; + (gettext("Importing from Google Photos"), detail) +} + /// What the archive list says before anything is staged. `repaint_archives` /// swaps in the count and total once there is something to report, and swaps /// this back when the list is emptied again. @@ -95,18 +108,19 @@ pub(crate) struct TakeoutState { pub(crate) progress_group: adw::PreferencesGroup, pub(crate) progress_label: gtk4::Label, pub(crate) progress_bar: gtk4::ProgressBar, - /// Report group for the last finished run, hidden until there is one. + /// The running counts, or the report for the last finished run; hidden + /// until there is one. pub(crate) summary_group: adw::PreferencesGroup, pub(crate) summary_rows: RefCell>, + /// Offers to resume an import the service did not get to finish. + pub(crate) resume_group: adw::PreferencesGroup, + pub(crate) resume_row: adw::ActionRow, + pub(crate) resume_button: gtk4::Button, /// One [`Request::ImportStatus`] in flight at a time, so a wedged daemon /// can't stack worker threads on the 2s tick. pub(crate) inflight: Cell, /// Whether the daemon reported an import running on the last poll. pub(crate) running: Cell, - /// Whether the run in flight was launched as a dry run, so the report can - /// say "would upload" rather than claiming photos were sent. The daemon's - /// summary carries no such flag — it is the same shape either way. - pub(crate) dry_run: Cell, } /// Widgets the Import page hands back for wiring. @@ -126,6 +140,9 @@ pub(crate) struct TakeoutWidgets { pub(crate) progress_label: gtk4::Label, pub(crate) progress_bar: gtk4::ProgressBar, pub(crate) summary_group: adw::PreferencesGroup, + pub(crate) resume_group: adw::PreferencesGroup, + pub(crate) resume_row: adw::ActionRow, + pub(crate) resume_button: gtk4::Button, } pub(crate) fn build_takeout_page() -> (gtk4::Widget, TakeoutWidgets) { @@ -261,7 +278,23 @@ pub(crate) fn build_takeout_page() -> (gtk4::Widget, TakeoutWidgets) { .visible(false) .build(); + // An import cut short by a restart or a lost connection, offered back at the + // top of the page: picking the archives again is exactly what a reboot in + // the middle of a migration should not cost. + let resume_button = gtk4::Button::builder() + .label(pgettext("verb", "Resume")) + .valign(gtk4::Align::Center) + .build(); + resume_button.add_css_class("suggested-action"); + let resume_row = adw::ActionRow::builder() + .title(gettext("The last import didn't finish")) + .build(); + resume_row.add_suffix(&resume_button); + let resume_group = adw::PreferencesGroup::builder().visible(false).build(); + resume_group.add(&resume_row); + let groups = gtk4::Box::new(gtk4::Orientation::Vertical, 18); + groups.append(&resume_group); groups.append(&dropzone); groups.append(&list_group); groups.append(&actions); @@ -301,6 +334,9 @@ pub(crate) fn build_takeout_page() -> (gtk4::Widget, TakeoutWidgets) { progress_label: progress_label.clone(), progress_bar: progress_bar.clone(), summary_group: summary_group.clone(), + resume_group, + resume_row, + resume_button, }; (frame.upcast(), widgets) } @@ -427,6 +463,28 @@ pub(crate) fn wire_takeout(ui: &Rc, widgets: &TakeoutWidgets) { }); }); + let ui_resume = ui.clone(); + widgets.resume_button.connect_clicked(move |_| { + let ui = ui_resume.clone(); + ui.busy_begin(); + let rx = spawn_request(ui.dirs.control_socket(), Request::ResumeImport); + glib::spawn_future_local(async move { + let result = rx.recv().await; + ui.busy_end(); + match result { + Ok(Ok(Response::Ok { .. })) => import_started(&ui, false), + Ok(Ok(Response::Error { message, kind })) => { + toast_failure(&ui, &gettext("Couldn't resume the import"), &message, kind); + } + _ => toast_error( + &ui, + &gettext("Couldn't resume the import"), + &gettext("The Proton Drive service didn't respond."), + ), + } + }); + }); + let ui_back = ui.clone(); widgets .back_button @@ -559,6 +617,7 @@ fn sync_takeout_actions(ui: &Rc) { .import_button .set_sensitive(has_files && !running); ui.takeout.clear_button.set_sensitive(has_files && !running); + ui.takeout.resume_button.set_sensitive(!running); ui.takeout.cancel_button.set_visible(running); } @@ -577,7 +636,6 @@ fn start_import(ui: &Rc, dry_run: bool) { return; } - ui.takeout.dry_run.set(dry_run); ui.busy_begin(); let rx = spawn_request( ui.dirs.control_socket(), @@ -588,27 +646,7 @@ fn start_import(ui: &Rc, dry_run: bool) { let result = rx.recv().await; ui.busy_end(); match result { - Ok(Ok(Response::Ok { .. })) => { - ui.takeout.running.set(true); - ui.gallery.import_banner.set_revealed(true); - ui.takeout.summary_group.set_visible(false); - ui.takeout.progress_group.set_visible(true); - ui.takeout.progress_bar.set_fraction(0.0); - ui.takeout.progress_label.set_text(&if dry_run { - gettext("Reading archives…") - } else { - gettext("Starting import…") - }); - sync_takeout_actions(&ui); - toast( - &ui, - &if dry_run { - gettext("Scanning the export…") - } else { - gettext("Import started — you can leave this page, it keeps running.") - }, - ); - } + Ok(Ok(Response::Ok { .. })) => import_started(&ui, dry_run), Ok(Ok(Response::Error { message, kind })) => { toast_failure(&ui, &gettext("Couldn't start the import"), &message, kind); } @@ -621,9 +659,39 @@ fn start_import(ui: &Rc, dry_run: bool) { }); } -/// Ask the daemon for the import's state, and paint the report once a run has -/// finished. Asked on arrival and while import progress keeps arriving. Cheap -/// and inflight-guarded. +/// Switch the page over to reporting progress once the daemon took the run. +fn import_started(ui: &Rc, dry_run: bool) { + ui.takeout.running.set(true); + if !dry_run { + ui.gallery.import_banner.set_revealed(true); + } + ui.takeout.resume_group.set_visible(false); + ui.takeout.summary_group.set_visible(false); + ui.takeout.progress_group.set_visible(true); + ui.takeout.progress_group.set_title(&if dry_run { + gettext("Scan in progress") + } else { + gettext("Import in progress") + }); + ui.takeout.progress_bar.set_fraction(0.0); + ui.takeout + .progress_label + .set_text(&gettext("Reading the archives…")); + sync_takeout_actions(ui); + toast( + ui, + &if dry_run { + gettext("Scanning the export…") + } else { + gettext("Import started — you can leave this page, it keeps running.") + }, + ); +} + +/// Ask the daemon for the import's state and paint it: the step and counts +/// while it runs, the report once it has ended, and the offer to resume a run +/// that did not finish. Asked on arrival and while import progress keeps +/// arriving. Cheap and inflight-guarded. pub(crate) fn refresh_takeout(ui: &Rc) { if ui.takeout.inflight.get() { return; @@ -634,124 +702,250 @@ pub(crate) fn refresh_takeout(ui: &Rc) { glib::spawn_future_local(async move { let result = rx.recv().await; ui.takeout.inflight.set(false); - let Ok(Ok(Response::ImportStatus { running, summary })) = result else { + let Ok(Ok(Response::ImportStatus { + running, + summary, + progress, + resumable, + })) = result + else { return; }; let was_running = ui.takeout.running.replace(running); - ui.gallery.import_banner.set_revealed(running); + let dry = summary.as_ref().is_some_and(|s| s.dry_run); + ui.gallery.import_banner.set_revealed(running && !dry); + ui.takeout.progress_group.set_visible(running); if running { - ui.takeout.progress_group.set_visible(true); - } else { - ui.takeout.progress_group.set_visible(false); - // An import runs for hours, so its ending is announced rather than - // left to be discovered: a toast for whoever is looking at the - // window, and a desktop notification for whoever is not. - let failed = summary.as_ref().and_then(|s| s.error.clone()); - if was_running && let Some(error) = &failed { - let headline = gettext("Couldn't import from Google Photos"); - toast_error(&ui, &headline, error); - notify("takeout-import", &headline, error); - } else if was_running { - let dry = ui.takeout.dry_run.get(); - let headline = if dry { - gettext("Scan finished") - } else { - gettext("Import finished") - }; - toast(&ui, &headline); - if !dry { - let body = match &summary { - // Translators: {n} is the number of photos uploaded, {failed} the number that failed. - Some(s) if s.failed > 0 => ngettext_f( - "{n} photo added, {failed} failed.", - "{n} photos added, {failed} failed.", - s.uploaded as u64, - &[("failed", &s.failed.to_string())], - ), - Some(s) if s.cancelled => ngettext_f( - "Stopped after {n} photo.", - "Stopped after {n} photos.", - s.uploaded as u64, - &[], - ), - Some(s) => ngettext_f( - "{n} photo added to Proton Photos.", - "{n} photos added to Proton Photos.", - s.uploaded as u64, - &[], - ), - None => gettext("The Google Photos import has finished."), - }; - notify("takeout-import", &headline, &body); - } - } + ui.takeout.progress_group.set_title(&if dry { + gettext("Scan in progress") + } else { + gettext("Import in progress") + }); + paint_progress(&ui, progress.as_ref()); + } else if was_running && let Some(summary) = &summary { + announce_end(&ui, summary); } if was_running != running { sync_takeout_actions(&ui); } - match summary { - Some(summary) => repaint_summary(&ui, &summary), + match &summary { + Some(summary) => repaint_summary(&ui, summary, running), None => ui.takeout.summary_group.set_visible(false), } + paint_resume(&ui, running, summary.as_ref(), &resumable); }); } -/// Paint the last run's report. -fn repaint_summary(ui: &Rc, summary: &ImportSummary) { +/// Tell the user how a run ended. An import runs for hours, so its ending is +/// announced rather than left to be discovered: a toast for whoever is looking +/// at the window, and a desktop notification for whoever is not. +fn announce_end(ui: &Rc, summary: &ImportSummary) { + if summary.dry_run { + toast(ui, &gettext("Scan finished")); + return; + } + if let Some(error) = &summary.error { + let kind = summary.error_kind.unwrap_or_default(); + let headline = gettext("Couldn't import from Google Photos"); + toast_failure(ui, &headline, error, kind); + notify( + "takeout-import", + &headline, + &gettext("You can resume it once the problem is solved."), + ); + return; + } + let (headline, body) = if summary.interrupted { + ( + gettext("Import interrupted"), + gettext("You can resume it where it stopped."), + ) + } else if summary.cancelled { + ( + gettext("Import stopped"), + ngettext_f( + "Stopped after {n} photo.", + "Stopped after {n} photos.", + summary.uploaded as u64, + &[], + ), + ) + } else if summary.failed > 0 { + ( + gettext("Import finished"), + // Translators: {n} is the number of photos uploaded, {failed} the number that failed. + ngettext_f( + "{n} photo added, {failed} failed.", + "{n} photos added, {failed} failed.", + summary.uploaded as u64, + &[("failed", &summary.failed.to_string())], + ), + ) + } else { + ( + gettext("Import finished"), + ngettext_f( + "{n} photo added to Proton Photos.", + "{n} photos added to Proton Photos.", + summary.uploaded as u64, + &[], + ), + ) + }; + toast(ui, &headline); + notify("takeout-import", &headline, &body); + + // A complete import is done with its archives; keeping them staged would + // only invite running the same export again. + if summary.complete() { + ui.takeout.archives.borrow_mut().clear(); + repaint_archives(ui); + } +} + +/// Paint where a running import is. +fn paint_progress(ui: &Rc, progress: Option<&ImportProgress>) { + let Some(progress) = progress else { + ui.takeout.progress_bar.pulse(); + return; + }; + let done = progress.done.to_string(); + let total = progress.total.to_string(); + let current = progress.current.clone().unwrap_or_default(); + let counted = progress.total > 0; + let args = [ + ("done", done.as_str()), + ("total", total.as_str()), + ("name", current.as_str()), + ]; + let text = match (progress.phase, counted) { + (ImportPhase::Scanning, _) => gettext("Reading the archives…"), + (ImportPhase::Checking, false) => gettext("Checking for photos you already have…"), + // Translators: {done} and {total} are counts of photos. + (ImportPhase::Checking, true) => gettext_f( + "Checking for photos you already have — {done} of {total}", + &args, + ), + (ImportPhase::Uploading, false) => gettext("Uploading…"), + (ImportPhase::Uploading, true) if current.is_empty() => { + // Translators: {done} and {total} are counts of photos. + gettext_f("Uploading {done} of {total}", &args) + } + // Translators: {done} and {total} are counts of photos; {name} is the file being uploaded. + (ImportPhase::Uploading, true) => gettext_f("Uploading {done} of {total} — {name}", &args), + (ImportPhase::Albums, false) => gettext("Sorting photos into albums…"), + // Translators: {done} and {total} are counts of albums. + (ImportPhase::Albums, true) => { + gettext_f("Sorting photos into albums — {done} of {total}", &args) + } + }; + ui.takeout.progress_label.set_text(&text); + if counted { + ui.takeout + .progress_bar + .set_fraction((progress.done as f64 / progress.total as f64).min(1.0)); + } else { + ui.takeout.progress_bar.pulse(); + } +} + +/// Offer to resume the last run when the service says it can be. +fn paint_resume(ui: &Rc, running: bool, summary: Option<&ImportSummary>, resumable: &[String]) { + let offer = !running && !resumable.is_empty(); + ui.takeout.resume_group.set_visible(offer); + if !offer { + return; + } + let uploaded = summary.map_or(0, |s| s.uploaded) as u64; + // Translators: {n} is the number of photos uploaded before the import stopped. + ui.takeout.resume_row.set_subtitle(&ngettext_f( + "{n} photo uploaded before it stopped. Resuming skips what is already there.", + "{n} photos uploaded before it stopped. Resuming skips what is already there.", + uploaded, + &[], + )); +} + +/// Paint the counts of a running import, or the report of the last one. +fn repaint_summary(ui: &Rc, summary: &ImportSummary, running: bool) { for row in ui.takeout.summary_rows.borrow_mut().drain(..) { ui.takeout.summary_group.remove(&row); } - let dry = ui.takeout.dry_run.get(); - ui.takeout.summary_group.set_title(&if dry { + let dry = summary.dry_run; + ui.takeout.summary_group.set_title(&if running { + gettext("So far") + } else if dry { gettext("Scan result") } else { gettext("Last import") }); - ui.takeout.summary_group.set_description(Some(&if let Some(error) = &summary.error { - // The reason is the service's own text, which is not translated. A - // group's description is markup, and the reason may name a path. - glib::markup_escape_text(&gettext_f( - "{what}: {detail}", - &[ - ("what", &gettext("Couldn't import from Google Photos")), - ("detail", error), - ], - )) - .to_string() + let description = if running { + None + } else if let Some(error) = &summary.error { + // The reason is the service's own text, which is not translated; the + // headline in front of it is. A group's description is markup, and the + // reason may name a path. + let what = gettext("Couldn't import from Google Photos"); + Some( + glib::markup_escape_text(&gettext_f( + "{what}: {detail}", + &[ + ( + "what", + error_headline(summary.error_kind.unwrap_or_default(), &what), + ), + ("detail", error), + ], + )) + .to_string(), + ) } else if dry { - gettext("Nothing was uploaded — this is what an import would do.") + Some(gettext( + "Nothing was uploaded — this is what an import would do.", + )) + } else if summary.interrupted { + Some(gettext( + "The Proton Drive service stopped while the import ran. Resume it to continue where it left off.", + )) } else if summary.cancelled { - gettext("Stopped early. Everything uploaded so far is in your timeline; run it again to continue where it left off.") + Some(gettext( + "Stopped early. Everything uploaded so far is in your timeline; run it again to continue where it left off.", + )) + } else if !summary.complete() { + Some(gettext( + "Finished with problems. Run it again to retry what didn't upload.", + )) } else { - gettext("Finished.") - })); + Some(gettext("Finished.")) + }; + ui.takeout + .summary_group + .set_description(description.as_deref()); // Only lines that carry information: a run with no failures should not have // to show a "0 failed" row for the user to read past. - let mut lines: Vec<(String, String)> = vec![ - (gettext("Photos in the export"), summary.found.to_string()), - ( - if dry { - gettext("Would upload") - } else { - gettext("Uploaded") - }, - summary.uploaded.to_string(), - ), - ]; + let mut lines: Vec<(String, String)> = + vec![(gettext("Photos in the export"), summary.found.to_string())]; + if dry { + lines.push((gettext("Would upload"), summary.to_upload.to_string())); + } else { + lines.push((gettext("Uploaded"), summary.uploaded.to_string())); + } if summary.duplicates > 0 { lines.push(( gettext("Already in Proton Photos"), summary.duplicates.to_string(), )); } + if summary.live_photos > 0 { + lines.push(( + gettext("Live photos kept together"), + summary.live_photos.to_string(), + )); + } if summary.albums_created > 0 { lines.push(( - if dry { - gettext("Albums to create") - } else { - gettext("Albums created") - }, + gettext("Albums created"), summary.albums_created.to_string(), )); } @@ -767,25 +961,31 @@ fn repaint_summary(ui: &Rc, summary: &ImportSummary) { summary.skipped_trashed.to_string(), )); } - if summary.bytes > 0 { + if dry && summary.to_upload_bytes > 0 { lines.push(( - if dry { - gettext("Data to upload") - } else { - gettext("Data uploaded") - }, - human_bytes(summary.bytes), + gettext("Data to upload"), + human_bytes(summary.to_upload_bytes), )); + } else if summary.bytes > 0 { + lines.push((gettext("Data uploaded"), human_bytes(summary.bytes))); } if summary.failed > 0 { lines.push((pgettext("state", "Failed"), summary.failed.to_string())); } + for path in &summary.unreadable_archives { + let name = Path::new(path) + .file_name() + .map(|n| n.to_string_lossy().to_string()) + .unwrap_or_else(|| path.clone()); + lines.push((gettext("Couldn't read this archive"), name)); + } let mut rows = Vec::new(); for (title, value) in lines { let row = adw::ActionRow::builder() .title(title) .subtitle(value) + .use_markup(false) .build(); row.add_css_class("property"); ui.takeout.summary_group.add(&row); @@ -794,35 +994,3 @@ fn repaint_summary(ui: &Rc, summary: &ImportSummary) { *ui.takeout.summary_rows.borrow_mut() = rows; ui.takeout.summary_group.set_visible(true); } - -/// Paint the import's progress from the transfer snapshot the daemon pushed, -/// so this page costs no extra request. A job with no known total -/// pulses instead of showing a fraction, the same as the Activity list. -pub(crate) fn takeout_progress(ui: &Rc, jobs: &[JobItem]) { - let Some(job) = jobs.iter().find(|job| job.title == IMPORT_JOB) else { - return; - }; - ui.takeout.progress_group.set_visible(true); - if job.total > 0 { - ui.takeout - .progress_bar - .set_fraction((job.done as f64 / job.total as f64).min(1.0)); - let done = job.done.to_string(); - let total = job.total.to_string(); - // Translators: progress such as "120 of 4000 — IMG_0001.jpg"; {detail} is what is being worked on. - let text = gettext_f( - "{done} of {total} — {detail}", - &[("done", &done), ("total", &total), ("detail", &job.detail)], - ); - ui.takeout.progress_label.set_text(&text); - } else { - ui.takeout.progress_bar.pulse(); - ui.takeout - .progress_label - .set_text(&if job.detail.is_empty() { - gettext("Working…") - } else { - job.detail.clone() - }); - } -} diff --git a/crates/pdfs-gui/src/app/update.rs b/crates/pdfs-gui/src/app/update.rs index b364757..cab063d 100644 --- a/crates/pdfs-gui/src/app/update.rs +++ b/crates/pdfs-gui/src/app/update.rs @@ -82,10 +82,10 @@ pub(crate) fn start_update_checks(ui: &Rc) { match fetch_latest().await { Ok(latest) => { checked = Some(unix_now()); - let mut config = ui.dirs.load_config(); - config.update_last_checked = checked; - config.update_latest = latest; - if let Err(e) = ui.dirs.save_config(&config) { + if let Err(e) = ui.dirs.update_config(|config| { + config.update_last_checked = checked; + config.update_latest = latest; + }) { tracing::warn!("update check: cannot save the result: {e}"); } show_latest(&ui); diff --git a/crates/pdfs-gui/src/app/widgets/file_list.rs b/crates/pdfs-gui/src/app/widgets/file_list.rs index 06d2d46..97f6d8e 100644 --- a/crates/pdfs-gui/src/app/widgets/file_list.rs +++ b/crates/pdfs-gui/src/app/widgets/file_list.rs @@ -224,6 +224,49 @@ impl FileList { .set_visible_child_name(if list { "list" } else { "grid" }); } + /// Bring both views back to the first entry. For a listing of something + /// else: [`replace_items`] keeps the scroll position, which is right for a + /// refresh, but another folder would open where the last one was left — + /// often at its bottom. + pub(crate) fn scroll_to_top(&self) { + if self.model.n_items() == 0 { + return; + } + self.grid.scroll_to(0, gtk4::ListScrollFlags::NONE, None); + self.column_view + .scroll_to(0, None, gtk4::ListScrollFlags::NONE, None); + } + + /// How far the view on screen is scrolled, in px from the top. + pub(crate) fn scroll_offset(&self) -> f64 { + self.views + .visible_child() + .and_downcast::() + .map_or(0.0, |scroll| scroll.vadjustment().value()) + } + + /// Select the entries named `selected` and scroll the view on screen to + /// `offset`, as they were when the folder was left. The scroll waits for + /// an idle, when the views have measured the rows just put in. + pub(crate) fn restore(&self, offset: f64, selected: &[String]) { + self.selection.unselect_all(); + for i in 0..self.selection.n_items() { + if entry_at(Some(&self.selection), i) + .is_some_and(|entry| selected.contains(&entry.name)) + { + self.selection.select_item(i, false); + } + } + let Some(scroll) = self + .views + .visible_child() + .and_downcast::() + else { + return; + }; + glib::idle_add_local_once(move || scroll.vadjustment().set_value(offset)); + } + /// Whether the list, rather than the grid, is on screen. pub(crate) fn shows_list(&self) -> bool { self.views.visible_child_name().as_deref() == Some("list") diff --git a/crates/pdfs-gui/src/app/widgets/ignore_dialog.rs b/crates/pdfs-gui/src/app/widgets/ignore_dialog.rs new file mode 100644 index 0000000..1841862 --- /dev/null +++ b/crates/pdfs-gui/src/app/widgets/ignore_dialog.rs @@ -0,0 +1,343 @@ +//! The Excluded Files dialog of one synced folder: its own `.pdfsignore`, +//! edited as text, with a check of what the rules do to a path. +//! +//! The file sits in the synced folder and syncs like any other file, so it can +//! change on another computer while it is open here. Saving checks for that +//! and asks, rather than writing over the other edit. + +use crate::*; +use pdfs_core::syncignore::{FolderRules, IgnoreRules, RuleSource, Verdict, WriteRulesError}; + +/// Open the dialog for the synced folder `id` at `root`. +pub(crate) fn present_ignore_dialog(ui: &Rc, id: i64, root: PathBuf) { + let root_display = tilde_path(&root.display().to_string()); + let intro = gtk4::Label::builder() + // Translators: {folder} is a local folder path such as ~/Projects. + .label(gettext_f( + "Patterns for {folder} only, one per line, on top of the ones for all synced folders. Lines starting with # are comments.", + &[("folder", &root_display)], + )) + .wrap(true) + .xalign(0.0) + .build(); + let text = gtk4::TextView::builder() + .monospace(true) + .wrap_mode(gtk4::WrapMode::WordChar) + .top_margin(8) + .bottom_margin(8) + .left_margin(8) + .right_margin(8) + .sensitive(false) + .build(); + let scroller = gtk4::ScrolledWindow::builder() + .child(&text) + .min_content_height(180) + .vexpand(true) + .build(); + scroller.add_css_class("card"); + let problems = gtk4::Label::builder() + .wrap(true) + .xalign(0.0) + .visible(false) + .build(); + problems.add_css_class("error"); + + let check_row = adw::EntryRow::builder() + .title(gettext("Check a path in the folder")) + .build(); + let verdict = gtk4::Label::builder() + .wrap(true) + .xalign(0.0) + .margin_top(6) + .build(); + verdict.add_css_class("dim-label"); + let check_group = adw::PreferencesGroup::new(); + check_group.add(&check_row); + + let all_folders = gtk4::Button::builder() + .label(gettext("Patterns for All Folders…")) + .halign(gtk4::Align::Start) + .build(); + all_folders.add_css_class("flat"); + + let body = gtk4::Box::new(gtk4::Orientation::Vertical, 12); + body.set_margin_top(12); + body.set_margin_bottom(18); + body.set_margin_start(18); + body.set_margin_end(18); + body.append(&intro); + body.append(&scroller); + body.append(&problems); + body.append(&check_group); + body.append(&verdict); + body.append(&all_folders); + + let cancel = gtk4::Button::with_label(&gettext("Cancel")); + let save = gtk4::Button::with_label(&gettext("Save")); + save.add_css_class("suggested-action"); + save.set_sensitive(false); + let header = adw::HeaderBar::builder() + .show_start_title_buttons(false) + .show_end_title_buttons(false) + .build(); + header.pack_start(&cancel); + header.pack_end(&save); + let toasts = adw::ToastOverlay::new(); + toasts.set_child(Some(&body)); + let view = adw::ToolbarView::new(); + view.add_top_bar(&header); + view.set_content(Some(&toasts)); + let dialog = adw::Dialog::builder() + .title(gettext("Excluded Files")) + .content_width(560) + .content_height(560) + .child(&view) + .build(); + + // What was read, so the save can tell whether the file changed since. + let loaded: Rc>> = Rc::default(); + let globals = Rc::new(ui.dirs.load_config().resolved_ignore_patterns()); + + let show_verdict = { + let (text, check_row, verdict, root, globals) = ( + text.clone(), + check_row.clone(), + verdict.clone(), + root.clone(), + globals.clone(), + ); + move || { + let typed = check_row.text(); + let typed = typed.trim(); + if typed.is_empty() { + verdict.set_text(""); + return; + } + let path = Path::new(typed); + let rel = path.strip_prefix(&root).unwrap_or(path); + let rel = rel.to_string_lossy(); + let rel = rel.trim_start_matches('/'); + let is_dir = typed.ends_with('/') || root.join(rel).is_dir(); + let rules = IgnoreRules::with_folder_text(&root, &globals, &buffer_text(&text)); + verdict.set_text(&verdict_text(&rules.explain(rel, is_dir))); + } + }; + let on_text = { + let (problems, save, show_verdict) = (problems.clone(), save.clone(), show_verdict.clone()); + move |text: &str| { + let bad = pdfs_core::syncignore::check_rules(text); + problems.set_visible(!bad.is_empty()); + if let Some((line, _)) = bad.first() { + problems.set_text(&ngettext_f( + "Line {line} isn't a valid pattern.", + "Line {line} and {more} more aren't valid patterns.", + bad.len() as u64, + &[ + ("line", &line.to_string()), + ("more", &(bad.len() - 1).to_string()), + ], + )); + } + save.set_sensitive(bad.is_empty()); + show_verdict(); + } + }; + let on_text_changed = on_text.clone(); + text.buffer() + .connect_changed(move |buffer| on_text_changed(&buffer_text_of(buffer))); + check_row.connect_changed(move |_| show_verdict()); + + let d = dialog.clone(); + cancel.connect_clicked(move |_| { + d.close(); + }); + let (ui_all, d) = (ui.clone(), dialog.clone()); + all_folders.connect_clicked(move |_| { + d.close(); + let prefs = &ui_all.status.prefs; + prefs.set_visible_page_name("sync"); + prefs.present(ui_window(&ui_all).as_ref()); + }); + + // Read off the main thread: the folder may be on a slow disk. + let reader = { + let (text, loaded, root, toasts) = + (text.clone(), loaded.clone(), root.clone(), toasts.clone()); + move || { + let (text, loaded, root, toasts) = + (text.clone(), loaded.clone(), root.clone(), toasts.clone()); + glib::spawn_future_local(async move { + let read = gio::spawn_blocking(move || FolderRules::read(&root)) + .await + .unwrap_or_else(|_| Err(std::io::Error::other("the read did not finish"))); + match read { + Ok(rules) => { + text.buffer().set_text(rules.text.as_deref().unwrap_or("")); + text.set_sensitive(true); + *loaded.borrow_mut() = Some(rules); + } + Err(e) => dialog_toast( + &toasts, + &gettext_f( + "{what}: {detail}", + &[ + ("what", &gettext("Couldn't read the patterns")), + ("detail", &e.to_string()), + ], + ), + ), + } + }); + } + }; + reader(); + + let (ui_save, d) = (ui.clone(), dialog.clone()); + save.connect_clicked(move |_| { + let Some(rules) = loaded.borrow().clone() else { + return; + }; + write_rules( + &ui_save, + &d, + &toasts, + id, + rules, + buffer_text(&text), + reader.clone(), + ); + }); + + dialog.present(ui_window(ui).as_ref()); +} + +/// Save `body` over `rules`, then close and sync the folder. When the file +/// changed since it was read, ask whether to load the other version or to +/// replace it; `reload` reads the file into the dialog again. +fn write_rules( + ui: &Rc, + dialog: &adw::Dialog, + toasts: &adw::ToastOverlay, + id: i64, + rules: FolderRules, + body: String, + reload: impl Fn() + Clone + 'static, +) { + let (ui, dialog, toasts) = (ui.clone(), dialog.clone(), toasts.clone()); + glib::spawn_future_local(async move { + let to_write = rules.clone(); + let text = body.clone(); + let written = gio::spawn_blocking(move || to_write.write(&text)) + .await + .unwrap_or_else(|_| { + Err(WriteRulesError::Io(std::io::Error::other( + "the write did not finish", + ))) + }); + match written { + Ok(()) => { + dialog.close(); + toast(&ui, &gettext("Excluded files saved")); + refresh_exclusions(&ui); + let _ = spawn_request(ui.dirs.control_socket(), Request::SyncNow { id: Some(id) }); + } + Err(WriteRulesError::Changed) => { + let ask = adw::AlertDialog::builder() + .heading(gettext("Patterns Changed Elsewhere")) + .body(gettext( + "The patterns of this folder changed while you edited them, for example on another computer. Load the other version, or replace it with yours?", + )) + .build(); + ask.add_response("cancel", &gettext("Cancel")); + ask.add_response("reload", &gettext("Load Other Version")); + ask.add_response("replace", &gettext("Replace")); + ask.set_response_appearance("replace", adw::ResponseAppearance::Destructive); + ask.set_default_response(Some("cancel")); + ask.set_close_response("cancel"); + let (ui, d, t) = (ui.clone(), dialog.clone(), toasts.clone()); + ask.connect_response(None, move |_, response| match response { + "reload" => reload(), + "replace" => { + // Read the other version only as the base to check + // against, then write over it. + let (ui, d, t, body, reload) = ( + ui.clone(), + d.clone(), + t.clone(), + body.clone(), + reload.clone(), + ); + let root = rules.path.parent().map(Path::to_path_buf); + glib::spawn_future_local(async move { + let Some(root) = root else { return }; + let read = gio::spawn_blocking(move || FolderRules::read(&root)) + .await + .ok() + .and_then(Result::ok); + if let Some(current) = read { + write_rules(&ui, &d, &t, id, current, body, reload); + } + }); + } + _ => {} + }); + ask.present(Some(&dialog)); + } + Err(WriteRulesError::Io(e)) => dialog_toast( + &toasts, + &gettext_f( + "{what}: {detail}", + &[ + ("what", &gettext("Couldn't save the patterns")), + ("detail", &e.to_string()), + ], + ), + ), + } + }); +} + +/// Say what the rules do to the path being checked. +fn verdict_text(verdict: &Verdict) -> String { + match verdict { + Verdict::Synced => gettext("Synced: no pattern matches it."), + // Translators: {pattern} is an ignore pattern such as *.log. + Verdict::Excluded { + pattern, + source: RuleSource::ThisFolder, + } => gettext_f( + "Not synced: excluded by “{pattern}” above.", + &[("pattern", pattern)], + ), + Verdict::Excluded { + pattern, + source: RuleSource::AllFolders, + } => gettext_f( + "Not synced: excluded by “{pattern}” in the patterns for all folders.", + &[("pattern", pattern)], + ), + Verdict::Included { pattern, .. } => gettext_f( + "Synced: “{pattern}” includes it again.", + &[("pattern", pattern)], + ), + } +} + +fn buffer_text(view: >k4::TextView) -> String { + buffer_text_of(&view.buffer()) +} + +fn buffer_text_of(buffer: >k4::TextBuffer) -> String { + let (start, end) = buffer.bounds(); + buffer.text(&start, &end, false).to_string() +} + +fn dialog_toast(toasts: &adw::ToastOverlay, message: &str) { + toasts.add_toast( + adw::Toast::builder() + .title(message) + .use_markup(false) + .timeout(6) + .build(), + ); +} diff --git a/crates/pdfs-gui/src/app/widgets/menu.rs b/crates/pdfs-gui/src/app/widgets/menu.rs index d78b0c1..d530ea8 100644 --- a/crates/pdfs-gui/src/app/widgets/menu.rs +++ b/crates/pdfs-gui/src/app/widgets/menu.rs @@ -101,8 +101,14 @@ impl ActionMenu { /// Pop the menu up over `anchor` at `(x, y)` in its coordinates. The /// popover removes itself once dismissed; it waits for an idle turn, so the - /// item that closed it still finds its action. - pub(crate) fn popup_at(self, anchor: &impl IsA, x: f64, y: f64) { + /// item that closed it still finds its action. Returned for a caller that + /// has to know when it closes. + pub(crate) fn popup_at( + self, + anchor: &impl IsA, + x: f64, + y: f64, + ) -> gtk4::PopoverMenu { let (menu, group) = self.finish(); let anchor = anchor.as_ref(); let host = popover_host(anchor); @@ -122,6 +128,7 @@ impl ActionMenu { glib::idle_add_local_once(move || p.unparent()); }); popover.popup(); + popover } /// A flat ⋮ button that opens this menu. diff --git a/crates/pdfs-gui/src/app/widgets/mod.rs b/crates/pdfs-gui/src/app/widgets/mod.rs index e0f5620..78cded0 100644 --- a/crates/pdfs-gui/src/app/widgets/mod.rs +++ b/crates/pdfs-gui/src/app/widgets/mod.rs @@ -2,7 +2,10 @@ pub(crate) mod compat; pub(crate) mod crumb; pub(crate) mod details; pub(crate) mod file_list; +pub(crate) mod ignore_dialog; pub(crate) mod menu; +pub(crate) mod photo_drag; +pub(crate) mod player; pub(crate) mod scrubber; pub(crate) mod share_dialog; pub(crate) mod thumbnails; diff --git a/crates/pdfs-gui/src/app/widgets/photo_drag.rs b/crates/pdfs-gui/src/app/widgets/photo_drag.rs new file mode 100644 index 0000000..955a77f --- /dev/null +++ b/crates/pdfs-gui/src/app/widgets/photo_drag.rs @@ -0,0 +1,237 @@ +//! Dragging photos out of the timeline into a file manager or another app. +//! +//! A photo is not a file anywhere the receiver can read until it has been +//! downloaded, and a drag has to start the moment the pointer moves. So the +//! drag offers a list of files without having them: [`PhotoDrag`] downloads +//! the photos only when the receiver asks for the list, which it does once the +//! photos are dropped on it, and then hands over copies under their own names. + +use std::future::Future; +use std::pin::Pin; +use std::time::{Duration, SystemTime}; + +use gtk4::gdk; +use gtk4::gdk::subclass::prelude::*; + +use crate::*; + +/// The one format a drag of photos offers. GTK turns it into a file list for +/// a receiver in this app, and every file manager reads it. +const URI_LIST: &str = "text/uri-list"; + +/// How long the copies handed to a receiver are kept. The receiver copies +/// them on drop, so a day is far more than it needs. +const KEEP_DRAGGED: Duration = Duration::from_secs(24 * 60 * 60); + +mod imp { + use super::*; + + #[derive(Default)] + pub struct PhotoDrag { + /// The photos dragged, as uid and Drive name. + pub(super) photos: RefCell)>>, + pub(super) socket: RefCell, + } + + #[glib::object_subclass] + impl ObjectSubclass for PhotoDrag { + const NAME: &'static str = "PdfsPhotoDrag"; + type Type = super::PhotoDrag; + type ParentType = gdk::ContentProvider; + } + + impl ObjectImpl for PhotoDrag {} + + impl ContentProviderImpl for PhotoDrag { + fn formats(&self) -> gdk::ContentFormats { + gdk::ContentFormats::new(&[URI_LIST]) + } + + fn write_mime_type_future( + &self, + mime_type: &str, + stream: &gio::OutputStream, + io_priority: glib::Priority, + ) -> Pin> + 'static>> { + let photos = self.photos.borrow().clone(); + let socket = self.socket.borrow().clone(); + let stream = stream.clone(); + let wanted = mime_type == URI_LIST; + Box::pin(async move { + if !wanted { + return Err(glib::Error::new( + gio::IOErrorEnum::NotSupported, + "photos are offered as a list of files only", + )); + } + let files = fetch_dragged(socket, photos).await?; + let list: String = files + .iter() + .map(|path| format!("{}\r\n", gio::File::for_path(path).uri())) + .collect(); + stream + .write_all_future(list.into_bytes(), io_priority) + .await + .map_err(|(_, e)| e)?; + Ok(()) + }) + } + } +} + +glib::wrapper! { + /// The content of a drag of photos: their files, fetched when dropped. + pub struct PhotoDrag(ObjectSubclass) + @extends gdk::ContentProvider; +} + +impl PhotoDrag { + pub(crate) fn new(socket: PathBuf, photos: Vec<(String, Option)>) -> Self { + let drag: Self = glib::Object::new(); + *drag.imp().photos.borrow_mut() = photos; + *drag.imp().socket.borrow_mut() = socket; + drag + } +} + +/// Download each dragged photo and put a copy under its Drive name where the +/// receiver can read it. A photo that fails is left out; only when every one +/// fails does the drop fail. +async fn fetch_dragged( + socket: PathBuf, + photos: Vec<(String, Option)>, +) -> Result, glib::Error> { + let base = glib::user_cache_dir().join("pdfs-drag"); + let mut files = Vec::new(); + let mut last_error = None; + for (uid, name) in photos { + let rx = spawn_request(socket.clone(), Request::OpenPhoto { uid: uid.clone() }); + let source = match rx.recv().await { + Ok(Ok(Response::FilePath { path })) => path, + Ok(Ok(Response::Error { message, .. })) => { + last_error = Some(message); + continue; + } + _ => { + last_error = Some("the Proton Drive service didn't respond".to_string()); + continue; + } + }; + let base = base.clone(); + let copied = gio::spawn_blocking(move || { + prune_dragged(&base, SystemTime::now()); + let name = name.unwrap_or_else(|| "photo".to_string()); + hand_over(&base, Path::new(&source), &uid, &name) + }) + .await; + match copied { + Ok(Ok(path)) => files.push(path), + Ok(Err(e)) => last_error = Some(e.to_string()), + Err(_) => last_error = Some("the copy was interrupted".to_string()), + } + } + if files.is_empty() { + let message = last_error.unwrap_or_else(|| "nothing to drop".to_string()); + tracing::warn!("dragging photos out failed: {message}"); + return Err(glib::Error::new(gio::IOErrorEnum::Failed, &message)); + } + Ok(files) +} + +/// Put the cached photo `source` at `base//`, where a receiver +/// copies it from. +/// +/// A hard link when the cache is on the same file system, so a large video is +/// not copied twice; a copy otherwise. Not a symlink: a file manager copies a +/// symlink as a link, which would leave the user a link into the cache. +pub(crate) fn hand_over( + base: &Path, + source: &Path, + uid: &str, + name: &str, +) -> std::io::Result { + let file_name = Path::new(name) + .file_name() + .ok_or_else(|| std::io::Error::other("the photo has no usable name"))?; + let dir = base.join(uid.replace('/', "_")); + std::fs::create_dir_all(&dir)?; + let dest = dir.join(file_name); + let _ = std::fs::remove_file(&dest); + if std::fs::hard_link(source, &dest).is_err() { + std::fs::copy(source, &dest)?; + } + Ok(dest) +} + +/// Remove the copies handed over more than [`KEEP_DRAGGED`] before `now`. +pub(crate) fn prune_dragged(base: &Path, now: SystemTime) { + let Ok(entries) = std::fs::read_dir(base) else { + return; + }; + for entry in entries.flatten() { + let old = entry + .metadata() + .and_then(|meta| meta.modified()) + .is_ok_and(|modified| { + now.duration_since(modified) + .is_ok_and(|age| age > KEEP_DRAGGED) + }); + if old { + let _ = std::fs::remove_dir_all(entry.path()); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn scratch(name: &str) -> PathBuf { + let dir = + std::env::temp_dir().join(format!("pdfs-drag-test-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn a_dragged_photo_is_handed_over_under_its_own_name() { + let dir = scratch("name"); + let source = dir.join("0f3a9c"); + std::fs::write(&source, b"jpeg bytes").unwrap(); + let base = dir.join("out"); + + let path = hand_over(&base, &source, "vol~link", "IMG_0001.JPG").unwrap(); + assert_eq!(path, base.join("vol~link").join("IMG_0001.JPG")); + assert_eq!(std::fs::read(&path).unwrap(), b"jpeg bytes"); + assert!(!path.is_symlink()); + + // Dragged again, it replaces the copy rather than failing on it. + std::fs::write(&source, b"new bytes").unwrap(); + let again = hand_over(&base, &source, "vol~link", "IMG_0001.JPG").unwrap(); + assert_eq!(std::fs::read(again).unwrap(), b"new bytes"); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn a_name_cannot_climb_out_of_its_folder() { + let dir = scratch("climb"); + let source = dir.join("blob"); + std::fs::write(&source, b"x").unwrap(); + let base = dir.join("out"); + let path = hand_over(&base, &source, "u", "../../escape.jpg").unwrap(); + assert_eq!(path, base.join("u").join("escape.jpg")); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn only_old_handovers_are_pruned() { + let dir = scratch("prune"); + std::fs::create_dir_all(dir.join("fresh")).unwrap(); + prune_dragged(&dir, SystemTime::now()); + assert!(dir.join("fresh").exists()); + prune_dragged(&dir, SystemTime::now() + KEEP_DRAGGED * 2); + assert!(!dir.join("fresh").exists()); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/crates/pdfs-gui/src/app/widgets/player.rs b/crates/pdfs-gui/src/app/widgets/player.rs new file mode 100644 index 0000000..894c3a1 --- /dev/null +++ b/crates/pdfs-gui/src/app/widgets/player.rs @@ -0,0 +1,356 @@ +//! Playing videos and music inside the app. +//! +//! The pieces both players share live here: the one My files opens a video or +//! a song in ([`open_media_player`]) and the Photos lightbox. A GTK +//! [`gtk4::Video`] brings play, pause, a seek bar and the volume; this adds +//! fullscreen, the keys every video player answers to, a repeat switch and a +//! volume that carries over from one video to the next. + +use crate::activation::{mounted_path, present_within}; +use crate::*; + +/// How far `J` and `L` (or Shift+←/→) jump. +const SEEK_LONG: i64 = 10; + +/// How far ← and → jump in the My files player, where they don't move to +/// another photo. +const SEEK_SHORT: i64 = 5; + +/// How much ↑ and ↓ change the volume in the My files player. +const VOLUME_STEP: f64 = 0.1; + +/// The edge of a song's cover in the My files player, in logical pixels. +const COVER_SIZE: i32 = 320; + +thread_local! { + /// The volume and mute the user last set, applied to the next stream so + /// muting one video doesn't leave the next one blaring. Kept for the run + /// of the app only. + static LEVEL: Cell> = const { Cell::new(None) }; +} + +/// Where a jump of `delta_secs` from `now` lands, in microseconds, as a +/// [`gtk4::MediaStream`] counts. Kept inside the stream; a `duration` of 0 +/// means the stream doesn't know its length yet, which bounds only the start. +pub(crate) fn seek_target(now: i64, duration: i64, delta_secs: i64) -> i64 { + let target = now + .saturating_add(delta_secs.saturating_mul(1_000_000)) + .max(0); + if duration > 0 { + target.min(duration) + } else { + target + } +} + +/// Jump the stream `delta_secs` forward, or back when negative. +pub(crate) fn seek_by(video: >k4::Video, delta_secs: i64) { + if let Some(stream) = video.media_stream() + && stream.is_seekable() + { + stream.seek(seek_target( + stream.timestamp(), + stream.duration(), + delta_secs, + )); + } +} + +/// Play a paused stream, pause a playing one. +pub(crate) fn toggle_playing(video: >k4::Video) { + if let Some(stream) = video.media_stream() { + stream.set_playing(!stream.is_playing()); + } +} + +fn toggle_muted(video: >k4::Video) { + if let Some(stream) = video.media_stream() { + stream.set_muted(!stream.is_muted()); + } +} + +fn change_volume(video: >k4::Video, delta: f64) { + if let Some(stream) = video.media_stream() { + stream.set_muted(false); + stream.set_volume((stream.volume() + delta).clamp(0.0, 1.0)); + } +} + +/// Enter fullscreen, or leave it. +pub(crate) fn toggle_fullscreen(window: >k4::Window) { + if window.is_fullscreen() { + window.unfullscreen(); + } else { + window.fullscreen(); + } +} + +/// The keys both players answer to the same way: `K` plays and pauses, `J` and +/// `L` (or Shift+← and Shift+→) jump ten seconds, `M` mutes. True when `key` +/// was one of them. +pub(crate) fn player_key(video: >k4::Video, key: &str, state: gtk4::gdk::ModifierType) -> bool { + let shift = state.contains(gtk4::gdk::ModifierType::SHIFT_MASK); + let plain = !state + .intersects(gtk4::gdk::ModifierType::CONTROL_MASK | gtk4::gdk::ModifierType::ALT_MASK); + if !plain { + return false; + } + match key { + "k" | "K" => toggle_playing(video), + "j" | "J" => seek_by(video, -SEEK_LONG), + "l" | "L" => seek_by(video, SEEK_LONG), + "Left" if shift => seek_by(video, -SEEK_LONG), + "Right" if shift => seek_by(video, SEEK_LONG), + "m" | "M" => toggle_muted(video), + _ => return false, + } + true +} + +/// Make `video` carry the volume over between streams and toggle `window`'s +/// fullscreen on a double-click. +pub(crate) fn wire_player(video: >k4::Video, window: >k4::Window) { + video.connect_media_stream_notify(|video| { + let Some(stream) = video.media_stream() else { + return; + }; + if let Some((volume, muted)) = LEVEL.get() { + stream.set_volume(volume); + stream.set_muted(muted); + } + let remember = |stream: >k4::MediaStream| { + LEVEL.set(Some((stream.volume(), stream.is_muted()))); + }; + stream.connect_volume_notify(remember); + stream.connect_muted_notify(remember); + }); + + // The controls' own buttons and seek bar claim their clicks, so this only + // sees a double-click on the picture. + let click = gtk4::GestureClick::new(); + let window = window.downgrade(); + click.connect_pressed(move |_, n_press, _, _| { + if n_press == 2 + && let Some(window) = window.upgrade() + { + toggle_fullscreen(&window); + } + }); + video.add_controller(click); +} + +/// A button that enters and leaves `window`'s fullscreen, showing which one +/// it will do. +pub(crate) fn fullscreen_button(window: >k4::Window) -> gtk4::Button { + let button = gtk4::Button::builder().valign(gtk4::Align::Center).build(); + let paint = |button: >k4::Button, fullscreen: bool| { + if fullscreen { + button.set_icon_name("view-restore-symbolic"); + button.set_tooltip_text(Some(&gettext("Leave Fullscreen (F)"))); + } else { + button.set_icon_name("view-fullscreen-symbolic"); + button.set_tooltip_text(Some(&gettext("Fullscreen (F)"))); + } + }; + paint(&button, window.is_fullscreen()); + let weak = button.downgrade(); + window.connect_fullscreened_notify(move |window| { + if let Some(button) = weak.upgrade() { + paint(&button, window.is_fullscreen()); + } + }); + let target = window.downgrade(); + button.connect_clicked(move |_| { + if let Some(window) = target.upgrade() { + toggle_fullscreen(&window); + } + }); + button +} + +/// Play a video or a song from My files in a window of its own, streamed from +/// the mount: the player reads the blocks it needs as it plays and seeks, so a +/// long video starts in seconds instead of after a full download. A file the +/// mount doesn't show, or the service is down, opens in the default +/// application instead, after a download. +pub(crate) fn open_media_player(ui: &Rc, entry: &DirEntry) { + let rel = entry_rel(ui, entry); + let mountpoint = ui.dirs.resolved_mountpoint(&ui.dirs.load_config()); + let (ui, entry) = (ui.clone(), entry.clone()); + glib::spawn_future_local(async move { + match present_within(mounted_path(&mountpoint, &rel)).await { + Some(path) => build_media_player(&ui, &entry, &path), + None => download_and_open(&ui, &entry), + } + }); +} + +fn build_media_player(ui: &Rc, entry: &DirEntry, path: &Path) { + let window = adw::Window::builder() + .title(&entry.name) + .default_width(960) + .default_height(600) + .build(); + if let Some(parent) = ui_window(ui) { + window.set_transient_for(Some(&parent)); + } + window.add_css_class("media-player-window"); + let gtk_window: gtk4::Window = window.clone().upcast(); + + let video = gtk4::Video::builder() + .autoplay(true) + .hexpand(true) + .vexpand(true) + .build(); + wire_player(&video, >k_window); + + // A song has no picture, which would leave a black window above the + // controls: its cover stands in for it, the one its listing tile shows, + // and a note while there is none. + let note = file_thumbnail_widget(COVER_SIZE, 128); + note.set_can_target(false); + note.set_visible(false); + note.add_css_class("media-player-note"); + if let Some(cover) = note + .child() + .and_then(|fallback| fallback.next_sibling()) + .and_downcast::() + { + // The whole cover, not a square cut out of it. + cover.set_content_fit(gtk4::ContentFit::Contain); + } + bind_file_thumbnail(ui, ¬e, entry, true); + let overlay = gtk4::Overlay::new(); + overlay.set_child(Some(&video)); + overlay.add_overlay(¬e); + + let failed = adw::StatusPage::builder() + .icon_name("dialog-warning-symbolic") + .title(gettext("Couldn't play this file")) + .build(); + let open_with = gtk4::Button::builder() + .label(gettext("Open With…")) + .halign(gtk4::Align::Center) + .build(); + open_with.add_css_class("pill"); + open_with.add_css_class("suggested-action"); + failed.set_child(Some(&open_with)); + + let stack = gtk4::Stack::new(); + stack.add_named(&overlay, Some("player")); + stack.add_named(&failed, Some("failed")); + + let header = adw::HeaderBar::new(); + header.pack_end(&fullscreen_button(>k_window)); + let mut menu = ActionMenu::new(); + let (ui_c, entry_c) = (ui.clone(), entry.clone()); + menu.item(&gettext("Open With…"), move || { + open_entry_with(&ui_c, &entry_c) + }); + let video_c = video.clone(); + // Translators: menu item of the video player that plays the file again + // and again. + menu.toggle(&gettext("Repeat"), false, move |on| video_c.set_loop(on)); + header.pack_end(&menu.button()); + + let toolbar = adw::ToolbarView::new(); + toolbar.add_top_bar(&header); + toolbar.set_content(Some(&stack)); + // Fullscreen is the video alone, its own controls still at hand. + window + .bind_property("fullscreened", &toolbar, "reveal-top-bars") + .invert_boolean() + .sync_create() + .build(); + window.set_content(Some(&toolbar)); + + let (ui_c, entry_c) = (ui.clone(), entry.clone()); + open_with.connect_clicked(move |_| open_entry_with(&ui_c, &entry_c)); + + let (stack_c, failed_c, note_c) = (stack.clone(), failed.clone(), note.clone()); + video.connect_media_stream_notify(move |video| { + let Some(stream) = video.media_stream() else { + return; + }; + let (stack, failed) = (stack_c.clone(), failed_c.clone()); + stream.connect_error_notify(move |stream| { + if let Some(error) = stream.error() { + tracing::warn!("in-app player failed: {error}"); + failed.set_description(Some(&error.to_string())); + stack.set_visible_child_name("failed"); + } + }); + let note = note_c.clone(); + stream.connect_prepared_notify(move |stream| { + note.set_visible(stream.is_prepared() && stream.has_audio() && !stream.has_video()); + }); + }); + video.set_filename(Some(path)); + + let keys = gtk4::EventControllerKey::new(); + let (video_k, window_k) = (video.clone(), gtk_window.downgrade()); + keys.connect_key_pressed(move |_, key, _code, state| { + let Some(window) = window_k.upgrade() else { + return glib::Propagation::Proceed; + }; + let Some(name) = key.name() else { + return glib::Propagation::Proceed; + }; + // The open menu keeps its arrows and Enter. + let in_menu = gtk4::prelude::GtkWindowExt::focus(&window) + .is_some_and(|focus| focus.ancestor(gtk4::Popover::static_type()).is_some()); + if in_menu { + return glib::Propagation::Proceed; + } + if player_key(&video_k, &name, state) { + return glib::Propagation::Stop; + } + let ctrl = state.contains(gtk4::gdk::ModifierType::CONTROL_MASK); + match name.as_str() { + "space" => toggle_playing(&video_k), + "Left" => seek_by(&video_k, -SEEK_SHORT), + "Right" => seek_by(&video_k, SEEK_SHORT), + "Up" => change_volume(&video_k, VOLUME_STEP), + "Down" => change_volume(&video_k, -VOLUME_STEP), + "f" | "F11" => toggle_fullscreen(&window), + // Escape leaves fullscreen before it closes anything. + "Escape" if window.is_fullscreen() => window.unfullscreen(), + "Escape" | "q" => window.close(), + "w" if ctrl => window.close(), + _ => return glib::Propagation::Proceed, + } + glib::Propagation::Stop + }); + // Capture, so the arrows and Space reach the player rather than moving the + // focus between its controls. + keys.set_propagation_phase(gtk4::PropagationPhase::Capture); + window.add_controller(keys); + + // Stop the sound with the window, not when the window is finally freed. + let video_c = video.clone(); + window.connect_close_request(move |_| { + video_c.set_media_stream(gtk4::MediaStream::NONE); + glib::Propagation::Proceed + }); + window.present(); +} + +#[cfg(test)] +mod tests { + use super::*; + + const SEC: i64 = 1_000_000; + + #[test] + fn a_jump_stays_inside_the_video() { + assert_eq!(seek_target(30 * SEC, 60 * SEC, 10), 40 * SEC); + assert_eq!(seek_target(30 * SEC, 60 * SEC, -10), 20 * SEC); + assert_eq!(seek_target(5 * SEC, 60 * SEC, -10), 0); + assert_eq!(seek_target(55 * SEC, 60 * SEC, 10), 60 * SEC); + } + + #[test] + fn a_jump_forward_in_a_stream_of_unknown_length_is_not_held_back() { + assert_eq!(seek_target(30 * SEC, 0, 10), 40 * SEC); + } +} diff --git a/crates/pdfs-gui/src/app/widgets/share_dialog.rs b/crates/pdfs-gui/src/app/widgets/share_dialog.rs index 77c8abd..19e9594 100644 --- a/crates/pdfs-gui/src/app/widgets/share_dialog.rs +++ b/crates/pdfs-gui/src/app/widgets/share_dialog.rs @@ -334,6 +334,13 @@ pub(crate) struct ShareDialog { /// Open the per-node Share dialog: invite Proton/external users, manage who has /// access and their roles, and create/copy/remove a public link. pub(crate) fn open_share_dialog(ui: &Rc, entry: &DirEntry) { + open_share_dialog_over(ui, entry, None); +} + +/// [`open_share_dialog`] over `parent` rather than the main window. The photo +/// viewer is a window of its own, and a dialog on the main window would open +/// behind it. +pub(crate) fn open_share_dialog_over(ui: &Rc, entry: &DirEntry, parent: Option) { if !*ui.mounted.borrow() { toast_error( ui, @@ -491,7 +498,7 @@ pub(crate) fn open_share_dialog(ui: &Rc, entry: &DirEntry) { } }); share_dialog_reload(&state); - dialog.present(ui_window(ui).as_ref()); + dialog.present(parent.or_else(|| ui_window(ui)).as_ref()); } /// Re-fetch the node's share and rebuild the people + public-link sections. diff --git a/crates/pdfs-gui/src/app/widgets/thumbnails.rs b/crates/pdfs-gui/src/app/widgets/thumbnails.rs index ad9abd4..d843b51 100644 --- a/crates/pdfs-gui/src/app/widgets/thumbnails.rs +++ b/crates/pdfs-gui/src/app/widgets/thumbnails.rs @@ -11,6 +11,7 @@ use pdfs_core::control::{FileThumbRequest, is_thumbnail_media_name, is_video_fil const THUMB_BATCH: usize = 32; const THUMB_CACHE_MAX: usize = 256; const THUMB_MISSING_MAX: usize = 2048; +const THUMB_BLANK_MAX: usize = 1024; const THUMB_DEBOUNCE: Duration = Duration::from_millis(40); const THUMB_RETRY: Duration = Duration::from_secs(2); @@ -20,6 +21,9 @@ pub(crate) struct FileThumbnailState { missing: RefCell>, missing_order: RefCell>, wanted: RefCell>>, + /// Tiles on screen still showing their icon, by key, kept across a + /// cancelled request so [`refresh_file_thumbnails`] can ask for them again. + blank: RefCell>, queue: RefCell>, inflight: Cell, source: RefCell>, @@ -27,6 +31,18 @@ pub(crate) struct FileThumbnailState { generation_loading: Cell, } +/// What a tile left blank needs to be asked for again. +struct BlankTile { + request: FileThumbRequest, + pictures: Vec>, +} + +/// Whether a picture registered as blank under `key` still is: bound to that +/// key, not painted, and in a window rather than in a list's spare pool. +fn tile_still_blank(key: &str, bound: &str, painted: bool, attached: bool) -> bool { + bound == key && !painted && attached +} + #[derive(Debug, PartialEq, Eq)] enum ReplyDisposition { Ignore, @@ -55,6 +71,7 @@ impl FileThumbnailState { missing: RefCell::new(HashSet::new()), missing_order: RefCell::new(VecDeque::new()), wanted: RefCell::new(HashMap::new()), + blank: RefCell::new(HashMap::new()), queue: RefCell::new(VecDeque::new()), inflight: Cell::new(false), source: RefCell::new(None), @@ -88,6 +105,85 @@ impl FileThumbnailState { } } } + + fn remember_blank(&self, key: &str, request: &FileThumbRequest, picture: >k4::Picture) { + let mut blank = self.blank.borrow_mut(); + if blank.len() >= THUMB_BLANK_MAX { + prune_blank(&mut blank); + } + let tile = blank.entry(key.to_string()).or_insert_with(|| BlankTile { + request: request.clone(), + pictures: Vec::new(), + }); + if !tile + .pictures + .iter() + .any(|weak| weak.upgrade().as_ref() == Some(picture)) + { + tile.pictures.push(picture.downgrade()); + } + } +} + +/// Drop the tiles that were painted, rebound to another file, or freed. +fn prune_blank(blank: &mut HashMap) { + blank.retain(|key, tile| { + tile.pictures.retain(|weak| { + weak.upgrade().is_some_and(|picture| { + tile_still_blank( + key, + &picture.widget_name(), + picture.is_visible(), + picture.root().is_some(), + ) + }) + }); + !tile.pictures.is_empty() + }); +} + +/// Ask again for every tile on screen still showing its icon. Sent when the +/// daemon says thumbnails landed, so a folder fills in while a build runs in +/// it, or after its own requests were cancelled, without being opened again. +pub(crate) fn refresh_file_thumbnails(ui: &Rc) { + let tiles: Vec<(String, FileThumbRequest, Vec)> = { + let mut blank = ui.file_thumbs.blank.borrow_mut(); + prune_blank(&mut blank); + blank + .iter() + .map(|(key, tile)| { + let pictures = tile.pictures.iter().filter_map(|w| w.upgrade()).collect(); + (key.clone(), tile.request.clone(), pictures) + }) + .collect() + }; + if tiles.is_empty() { + return; + } + for (key, request, pictures) in tiles { + let cached = ui.file_thumbs.textures.borrow().get(&key).cloned(); + if let Some(texture) = cached { + for picture in &pictures { + paint(picture, &key, &texture); + } + ui.file_thumbs.blank.borrow_mut().remove(&key); + continue; + } + // A miss from before the thumbnail landed is no verdict any more. + ui.file_thumbs.missing.borrow_mut().remove(&key); + let mut wanted = ui.file_thumbs.wanted.borrow_mut(); + let waiters = wanted.entry(key).or_default(); + for picture in pictures { + if !waiters.contains(&picture) { + waiters.push(picture); + } + } + drop(wanted); + let mut queue = ui.file_thumbs.queue.borrow_mut(); + queue.retain(|item| item.uid != request.uid); + queue.push_back(request); + } + schedule_file_thumbs(ui); } /// Abandon every opportunistic ordinary-file thumbnail associated with the @@ -230,6 +326,12 @@ pub(crate) fn bind_file_thumbnail( paint(&picture, &key, texture); return; } + let request = FileThumbRequest { + uid: entry.uid.clone(), + modified: entry.modified, + name: entry.name.clone(), + }; + ui.file_thumbs.remember_blank(&key, &request, &picture); if ui.file_thumbs.missing.borrow().contains(&key) { return; } @@ -242,11 +344,7 @@ pub(crate) fn bind_file_thumbnail( .push(picture); let mut queue = ui.file_thumbs.queue.borrow_mut(); queue.retain(|item| item.uid != entry.uid); - queue.push_back(FileThumbRequest { - uid: entry.uid.clone(), - modified: entry.modified, - name: entry.name.clone(), - }); + queue.push_back(request); drop(queue); schedule_file_thumbs(ui); } @@ -387,6 +485,7 @@ fn flush_file_thumbs(ui: &Rc) { match gtk4::gdk::Texture::from_filename(&path) { Ok(texture) => { ui.file_thumbs.store_texture(&key, texture.clone()); + ui.file_thumbs.blank.borrow_mut().remove(&key); if let Some(pictures) = ui.file_thumbs.wanted.borrow_mut().remove(&key) { @@ -447,7 +546,16 @@ fn retry_file_thumbs(ui: &Rc, items: Vec) { #[cfg(test)] mod tests { - use super::{ReplyDisposition, reply_disposition}; + use super::{ReplyDisposition, reply_disposition, tile_still_blank}; + + #[test] + fn only_a_tile_still_showing_its_icon_is_asked_for_again() { + assert!(tile_still_blank("a:1", "a:1", false, true)); + assert!(!tile_still_blank("a:1", "a:1", true, true), "painted"); + assert!(!tile_still_blank("a:1", "b:1", false, true), "rebound"); + assert!(!tile_still_blank("a:1", "a:2", false, true), "file changed"); + assert!(!tile_still_blank("a:1", "a:1", false, false), "spare row"); + } fn is_image_name(name: &str) -> bool { pdfs_core::control::is_thumbnail_image_name(name) diff --git a/crates/pdfs-gui/src/i18n.rs b/crates/pdfs-gui/src/i18n.rs index 609b0e0..26c76ec 100644 --- a/crates/pdfs-gui/src/i18n.rs +++ b/crates/pdfs-gui/src/i18n.rs @@ -13,6 +13,7 @@ use gettextrs::LocaleCategory; use pdfs_core::config::AppDirs; +use std::path::{Path, PathBuf}; /// The gettext text domain, matching `po/pdfs.pot` and the installed `.mo` name. pub(crate) const DOMAIN: &str = "pdfs"; @@ -53,14 +54,11 @@ pub(crate) fn init() { if let Some(code) = language { apply_language(&code); } - let dir = std::env::var(LOCALEDIR_ENV) - .ok() - .filter(|dir| !dir.is_empty()) - .unwrap_or_else(|| { - option_env!("PDFS_LOCALEDIR") - .unwrap_or(DEFAULT_LOCALEDIR) - .to_string() - }); + let dir = locale_dir( + std::env::var(LOCALEDIR_ENV).ok(), + option_env!("PDFS_LOCALEDIR"), + std::env::current_exe().ok(), + ); if let Err(e) = gettextrs::bindtextdomain(DOMAIN, dir) { tracing::warn!("bindtextdomain failed: {e}"); } @@ -73,6 +71,33 @@ pub(crate) fn init() { } } +/// Where the catalogs are: `$PDFS_LOCALEDIR`, then the directory the build +/// named, then `share/locale` beside the `bin` directory the program runs +/// from when that holds a catalog of ours, then [`DEFAULT_LOCALEDIR`]. +/// +/// The third is for an install outside `/usr`, such as the release tarball's +/// `install.sh` into `~/.local`: its programs find their translations without +/// a wrapper setting the variable. +fn locale_dir(env: Option, build: Option<&str>, exe: Option) -> PathBuf { + if let Some(dir) = env.filter(|dir| !dir.is_empty()) { + return PathBuf::from(dir); + } + if let Some(dir) = build { + return PathBuf::from(dir); + } + let beside = exe + .as_deref() + .and_then(Path::parent) + .and_then(Path::parent) + .map(|prefix| prefix.join("share/locale")) + .filter(|dir| { + LANGUAGES + .iter() + .any(|(code, _)| dir.join(code).join("LC_MESSAGES/pdfs.mo").is_file()) + }); + beside.unwrap_or_else(|| PathBuf::from(DEFAULT_LOCALEDIR)) +} + /// The iso-codes text domain that translates ISO 3166-1 country names. const ISO_3166_DOMAIN: &str = "iso_3166-1"; @@ -234,6 +259,32 @@ fn fill(template: &str, args: &[(&str, &str)]) -> String { mod tests { use super::*; + #[test] + fn the_catalogs_are_found_beside_an_install_outside_usr() { + let prefix = std::env::temp_dir().join(format!("pdfs-locale-{}", std::process::id())); + let mo = prefix.join("share/locale/de/LC_MESSAGES/pdfs.mo"); + std::fs::create_dir_all(mo.parent().unwrap()).unwrap(); + std::fs::write(&mo, b"").unwrap(); + let exe = Some(prefix.join("bin/pdfs-app")); + + assert_eq!( + locale_dir(None, None, exe.clone()), + prefix.join("share/locale") + ); + // The variable and the build's own choice still win. + assert_eq!( + locale_dir(Some("/x".into()), Some("/y"), exe.clone()), + PathBuf::from("/x") + ); + assert_eq!(locale_dir(None, Some("/y"), exe), PathBuf::from("/y")); + // A prefix without our catalogs falls back to the system's. + assert_eq!( + locale_dir(None, None, Some(PathBuf::from("/nonexistent/bin/pdfs-app"))), + PathBuf::from(DEFAULT_LOCALEDIR) + ); + let _ = std::fs::remove_dir_all(&prefix); + } + #[test] fn fill_replaces_named_placeholders_in_any_order() { assert_eq!( diff --git a/crates/pdfs-gui/src/main.rs b/crates/pdfs-gui/src/main.rs index 8471f79..ea754ae 100644 --- a/crates/pdfs-gui/src/main.rs +++ b/crates/pdfs-gui/src/main.rs @@ -398,12 +398,10 @@ fn open_manager(args: &[&str]) { /// Remember that the user hid the icon, so the next login's autostart and the /// app both leave it away, then exit. fn hide_tray() { - if let Ok(dirs) = AppDirs::new() { - let mut config = dirs.load_config(); - config.tray_hidden = true; - if let Err(e) = dirs.save_config(&config) { - tracing::warn!("hide tray icon: {e}"); - } + if let Ok(dirs) = AppDirs::new() + && let Err(e) = dirs.update_config(|config| config.tray_hidden = true) + { + tracing::warn!("hide tray icon: {e}"); } std::process::exit(0); } diff --git a/crates/pdfs-gui/tests/icon_names.rs b/crates/pdfs-gui/tests/icon_names.rs index 8753565..be37f59 100644 --- a/crates/pdfs-gui/tests/icon_names.rs +++ b/crates/pdfs-gui/tests/icon_names.rs @@ -24,6 +24,8 @@ const STOCK: &[&str] = &[ // GTK's built-in icons "info-outline-symbolic", // Adwaita + "application-x-executable-symbolic", + "audio-x-generic-symbolic", "checkbox-checked-symbolic", "checkbox-symbolic", "computer-symbolic", @@ -68,12 +70,14 @@ const STOCK: &[&str] = &[ "text-x-generic-symbolic", "user-trash-symbolic", "video-x-generic-symbolic", + "view-fullscreen-symbolic", "view-grid-symbolic", "view-list-symbolic", "view-more-symbolic", "view-paged-symbolic", "view-pin-symbolic", "view-refresh-symbolic", + "view-restore-symbolic", "window-close-symbolic", "zoom-fit-best-symbolic", "zoom-in-symbolic", diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index fe07de2..178d8e9 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -467,6 +467,7 @@ both with `pdfs_core::search::relevance_score`, so the prompt can merge them int | `pdfs-sync`, `pdfs-sync-poll` | Mirror passes and the remote poll (§7) | | Tokio runtime | Remote calls, and the Drive and photos event streams (§6) | | `pdfs-online-probe` | Probes Drive while offline | +| `pdfs-pin-keeper` | Checks pins once a second for work: every pin at startup, when the link returns and after the event feed loses continuity, and the pins a batch of remote events changed. Downloads files kept offline that are missing or stale; failures retry with a backoff of 30 s to 30 min (`docs/BUGS.md` B282) | | `pdfs-supervisor` | Logs workers that hold one job for minutes, a queue that stops draining, and control requests that never return; samples memory; pings the systemd watchdog only after a round trip over the control socket succeeds (`WatchdogSec=120`) | | `pdfs-conflict-sweep` | Optional. After 30 s, then every 5 minutes, finds `(sync-conflict …)` copies identical (size and SHA-1) to their original. `conflict_sweep` decides: `off`, `report` (default) or `enforce`, which trashes them after re-checking each one | | Smaller helpers | `pdfs-localindex` (home index for search), `pdfs-pause` (ends a timed pause), `pdfs-similar` (similar photos, on request), `pdfs-diagnostics` | diff --git a/docs/BUGS.md b/docs/BUGS.md index 2338590..e58ea2e 100644 --- a/docs/BUGS.md +++ b/docs/BUGS.md @@ -109,7 +109,16 @@ a live account since. | Entry | Title | |---|---| +| [B291](#b291--on-fedora-45s-oo7-every-saved-sign-in-is-lost-at-the-next-reboot) | On Fedora 45's oo7, every saved sign-in is lost at the next reboot | +| [B289](#b289--the-takeout-import-mixes-in-other-services-and-drops-location-and-live-photos) | The Takeout import mixes in other services and drops location and live photos | +| [B288](#b288--an-interrupted-takeout-import-reads-as-finished-and-one-error-ends-it) | An interrupted Takeout import reads as finished, and one error ends it | +| [B287](#b287--a-photo-draft-left-by-an-interrupted-upload-blocks-that-name) | A photo draft left by an interrupted upload blocks that name | +| [B286](#b286--resuming-a-takeout-import-fails-on-a-draft-with-no-content-hash) | Resuming a Takeout import fails on a draft with no content hash | +| [B285](#b285--a-restarted-secret-service-leaves-the-process-on-a-dead-session) | A restarted Secret Service leaves the process on a dead session | +| [B284](#b284--a-keyring-entry-reported-missing-discards-refreshed-session-tokens) | A keyring entry reported missing discards refreshed session tokens | | [B283](#b283--videos-have-no-thumbnail-outside-photos-and-a-ts-recording-looks-like-source-code) | Videos have no thumbnail outside Photos, and a `.ts` recording looks like source code | +| [B282](#b282--a-file-kept-offline-is-downloaded-again-after-a-change-made-elsewhere) | A file kept offline is downloaded again after a change made elsewhere | +| [B281](#b281--a-file-in-a-synced-folder-cannot-be-kept-offline) | A file in a synced folder cannot be kept offline | | [B280](#b280--uploading-a-batch-of-large-photos-swaps-the-machine-until-the-daemon-is-killed) | Uploading a batch of large photos swaps the machine until the daemon is killed | | [B279](#b279--after-a-watchdog-kill-the-mount-never-comes-back) | After a watchdog kill the mount never comes back | | [B278](#b278--sync-list-and-devices-list-show-the-last-sync-as-epoch-seconds) | `sync list` and `devices list` show the last sync as epoch seconds | @@ -297,6 +306,8 @@ a live account since. | Entry | Title | |---|---| +| [B292](#b292--two-programs-changing-settings-at-once-lose-one-of-the-changes) | Two programs changing settings at once lose one of the changes | +| [B290](#b290--reopening-the-database-right-after-closing-it-can-be-refused-as-another-daemon) | Reopening the database right after closing it can be refused as another daemon | | [B276](#b276--a-stop-can-end-with-a-panic-in-a-thread-the-stop-never-waited-for) | A stop can end with a panic in a thread the stop never waited for | | [B258](#b258--journalctl--p-warning-shows-nothing-whatever-the-daemon-logged) | `journalctl -p warning` shows nothing, whatever the daemon logged | | [B256](#b256--pdfs-copy-right-after-a-save-copies-the-old-version) | `pdfs copy` right after a save copies the old version | @@ -384,6 +395,283 @@ a live account since. Newest first. +### B292 — Two programs changing settings at once lose one of the changes + +**Status:** Fixed + +**Found:** 2026-11-15, reading every writer of `config.json` while adding settings to Preferences. + +**Where:** `crates/pdfs-core/src/config.rs`, `AppDirs::save_config` and its callers in the app, +the tray and the daemon + +**Repro:** Change the cache size in Preferences (the daemon writes `cache_budget`) while the app +saves the window size or the tray hides itself. Both read the file before either wrote it, and +the one that wrote last puts the other setting back. Not seen in the field; the window is a few +milliseconds, but more settings in the app make it more likely. + +**Cause:** Every writer loaded the file, changed one field and wrote the whole file back, with +nothing ordering them. The atomic rename kept the file whole, not the changes. + +**Fix:** `AppDirs::update_config` reads, changes and writes under an `flock` on +`config.json.lock`, and every writer goes through it. It refuses an unreadable file before +applying the change (B223). A lock held longer than 5 seconds is reported as an error instead of +waited on. + +**Verified:** `cargo test -p pdfs-core config::tests` on 2026-11-15, +`concurrent_changes_to_different_settings_are_all_kept` and +`a_change_is_never_applied_over_an_unreadable_config`. + +--- + +### B291 — On Fedora 45's oo7, every saved sign-in is lost at the next reboot + +**Status:** Fixed, not verified live +**Found:** 2026-11-15, from the reporter's package list in +[issue #78](https://github.com/narrrl/proton-drive-linux/issues/78): `oo7-daemon-0.7.0~beta^1.e91b98a`, +the build Fedora 45 and 46 ship as stable, from before oo7's +[7fb11dd](https://github.com/linux-credentials/oo7/commit/7fb11dd111c4be8fec01996e3caa31c44fa962b3) +("server: Write item changes to the keyring file"). See +[the investigation](ISSUE-78.md#setsecret-is-kept-only-in-memory-in-fedoras-oo7). +**Where:** `crates/pdfs-core/src/auth.rs`, `write_stored`; `crates/pdfs-core/src/secretservice.rs` + +**Repro:** With oo7-daemon at e91b98a, save the session blob, save it again with a new refresh +token, and restart oo7-daemon. The read after the restart returns the first blob. The keyring +store writes an existing item with `SetSecret`, and that oo7 build applies `SetSecret` only to the +item in memory; the keyring file keeps what `CreateItem` wrote. Within a boot every read returns +the new tokens, so nothing looks wrong. After a reboot the daemon resumes with the refresh token +of the last `pdfs login`, which Proton spent long ago: `InvalidRefreshToken`, and the user has to +sign in again. Typing the password at login, or the fixes for B284 and B285, change nothing. + +**Cause:** A bug in that oo7 build, fixed in oo7 0.7.0 but not packaged by Fedora. Every token +refresh pdfs saved went through the one call it does not persist. + +**Fix:** An existing entry is saved by `CreateItem` with `replace`, which that build writes to the +file: a new item in the old one's collection, with its label and all its attributes, so the +provider replaces the old one. A duplicate that a provider leaves behind is removed; if it cannot +be, the new item is taken out again and the save falls back to the usual `SetSecret`. The first +save, a locked item, two matching items, or any failure before the new item exists also fall back +to it. The connection used for this never shows a prompt. The daemon log now has a short +fingerprint of the refresh token at each start and each save, so a log across a reboot shows +whether the keyring returned the last token saved. + +**Verified:** 2026-11-15, `real_secret_service_keeps_the_last_save_across_a_provider_restart` +(ignored; needs a throwaway Secret Service) against oo7-daemon built at e91b98a: with the +in-place write it reads `r1` after the restart, with this change `r3` and one item. The same test +and `real_secret_service_survives_a_provider_restart` pass against gnome-keyring. Not yet run on +a Fedora 45 desktop with a real account. + +--- + +### B290 — Reopening the database right after closing it can be refused as another daemon + +**Status:** Fixed, 2026-11-15 +**Found:** 2026-11-15, CI on `chore/release-3.8.0` (fd6b2d3): +`db::tests::what_a_create_sent_outlives_a_newer_blob_its_trash_and_a_restart` failed once with +"another Proton Drive daemon is already using /tmp/pdfs-create-sent-….db". It passes on its own. +**Where:** `crates/pdfs-core/src/db/mod.rs`, `acquire_single_writer_lock` + +**Repro:** In one process, drop a `Db` and open the same file again while another thread spawns a +child process. Rare; seen once in about 500 tests that run in parallel, several of which spawn +processes. + +**Cause:** The single-writer lock is a `flock` on `.lock`, and a `flock` belongs to the open +file, not to one descriptor. A child holds a copy of every descriptor from its fork until its exec +closes the `O_CLOEXEC` ones. A lock dropped by the parent in that moment stays held by the child, +and an immediate reopen gets `EWOULDBLOCK`, which reads as a second daemon. + +**Fix:** A refused lock is retried every 20 ms for up to 500 ms before it counts as another +daemon. That covers any fork-to-exec window. A real second daemon is still refused, half a +second later. + +**Verified:** `cargo test -p pdfs-core` on 2026-11-15, including +`a_second_writer_is_refused_while_the_first_holds_the_database`. + +--- + +### B289 — The Takeout import mixes in other services and drops location and live photos + +**Status:** Fixed, not verified live +**Found:** 2026-11-15, auditing the Google Photos import alongside +[issue #94](https://github.com/narrrl/proton-drive-linux/issues/94). +**Where:** `crates/pdfs-core/src/takeout.rs`; `plan` and `upload_takeout_photo` in +`crates/pdfs-fuse/src/takeout.rs` + +**Repro:** Import a Takeout export that also holds `Drive/` or `YouTube/`, with Pixel motion +photos, iPhone live photos and photos that have a place in Google Photos. + +**Cause.** Five gaps in what the import carried over: +- The service folder was dropped without being checked, so media from other Google services in + the same export landed in the photo timeline. +- `geoData` was never read, although `docs/PHOTOS.md` said the import takes locations from the + export, and the SDK had no way to upload a location. +- The two halves of a live or motion photo uploaded as unrelated photos, the `.MOV` half usually + without a date, and the `.MP` half of a Pixel motion photo was skipped as unknown. +- `creationTime`, the day the photo reached Google, outranked the date in the file name and the + photo's own EXIF. +- A sidecar with `null` in `favorited`, a numeric `timestamp` or a `0` timestamp lost its other + fields or filed the photo under 1970. + +**Fix:** The Photos service is the folder whose sidecars carry `photoTakenTime`, and only it is +imported (everything, when no folder does). `geoData`, else `geoDataExif`, becomes the photo's +`Location` attribute through SDK 0.12.0's `PhotoUploadMetadata::location`; `0, 0` is no position. +Stills and videos with the same stem in one folder (`IMG_1.HEIC` + `IMG_1.MOV`, +`PXL_1.MP.jpg` + `PXL_1.MP`) are paired unless their dates are minutes apart: the still is +tagged as a live or motion photo, the video is uploaded after it with `main_photo_uid` and takes +its date and place. The capture time is `photoTakenTime`, then the name, then EXIF, then +`creationTime`. Favorites are also set on photos that were already on the account. + +**Verified:** `takeout::tests` in `pdfs-core` (services, locations, timestamps, live keys) and +`pdfs-fuse` (pairing, skew, folded copies). Not run against a real export and account. + +--- + +### B288 — An interrupted Takeout import reads as finished, and one error ends it + +**Status:** Fixed, not verified live +**Found:** 2026-11-15, auditing the Google Photos import alongside +[issue #94](https://github.com/narrrl/proton-drive-linux/issues/94). +**Where:** `crates/pdfs-fuse/src/takeout.rs`; `refresh_takeout` in +`crates/pdfs-gui/src/app/pages/takeout.rs`; `cmd_import_google_photos` in +`crates/pdfs-cli/src/main.rs` + +**Repro:** Start an import of several archives, one of them damaged, and restart the service +while it uploads. + +**Cause.** +- The import state lived only in memory. After a restart `ImportStatus` had no summary, and the + app announced "Import finished". The archives had to be chosen again to continue. +- A daemon shutdown broke the upload loop but still filed albums and logged a success. +- One unreadable archive failed the scan, and a failed request in the middle of the run + discarded every count and skipped the albums for what had uploaded. +- The progress total counted photos already on the account, so the bar stopped short of the + end; a dry run reported "Would upload: 0"; the job's phases reached the app in English. + +**Fix:** The last run's archives and summary are kept in `takeout-import.json` in the state +directory. A run still marked running at start-up is `interrupted`, and `ImportStatus` lists +its archives as `resumable`; `ResumeImport` (`pdfs import-google-photos --resume`, **Resume** in +the app) starts it again, and the duplicate check skips what is there. An unreadable archive is +listed in `unreadable_archives` and the rest imports. A failure keeps the counts and still files +albums. `ImportStatus` carries the phase and its own progress, the summary says `dry_run`, +`to_upload` and `error_kind`, and the app words them in the user's language. The inline +timeline refresh of an empty gallery waits for a refresh already running instead of starting a +second one (`refresh_timeline_joined`). + +**Verified:** `takeout::tests` in `pdfs-fuse` (saved state, resumable runs) and `pdfs-core` +(unreadable archive), and the CLI tests for `--resume` and the exit status. Not run live. + +--- + +### B287 — A photo draft left by an interrupted upload blocks that name + +**Status:** Fixed, not verified live +**Found:** 2026-11-15, reading the SDK upload path for +[issue #94](https://github.com/narrrl/proton-drive-linux/issues/94). +**Where:** `upload_photo_from`, `upload_file_from_inner` and `abandon_file_draft` in +`proton-drive-rs` (SDK 0.12.0) + +**Repro:** Interrupt a photo upload after its draft is created, sign in again, and upload a +photo with the same name. + +**Cause.** Photo uploads replaced a leftover draft only when its `ClientUID` was the current +session's. A new login is a new session, so the next upload of that name failed with 2500. A +failed upload also never deleted its own draft, which the C# and TypeScript SDKs do. + +**Fix:** SDK 0.12.0 adds `PhotoUploadMetadata::replace_existing_draft`, which the Takeout import +sets, and deletes the draft of a new file or photo whose upload fails. After a failed seal it +only does so on a definite rejection, because a seal lost in transit may have been applied. + +**Verified:** SDK unit test `draft_cleanup_only_after_a_definite_seal_rejection`. Not run live. + +--- + +### B286 — Resuming a Takeout import fails on a draft with no content hash + +**Status:** Fixed, not verified live +**Found:** 2026-11-15, [issue #94](https://github.com/narrrl/proton-drive-linux/issues/94) +(3.7.0, Fedora). An import stopped by a restart failed every re-run at once with +`check for existing photos: serialization error: invalid type: null, expected a string at line +1 column 2063`. +**Where:** `FoundPhotoDuplicateDto` in `proton-drive-rs` (SDK 0.11.0) + +**Repro:** Interrupt a photo upload after its draft is created, then run the import again. + +**Cause.** The duplicates endpoint sends `ContentHash` (and can send `Hash`) as `null` for a +draft, as its API schema says. The SDK declared both as `String` with `#[serde(default)]`, +which covers a missing field but not `null`, so one draft failed the whole batch of 150 names +and the import ended before uploading anything. + +**Fix:** SDK 0.12.0 makes both fields optional and skips drafts, trashed links and candidates +without hashes, like the TypeScript SDK. Every other defaulted DTO field reads `null` as its +default too, and an undecodable response is logged with its path. + +**Verified:** SDK unit tests `duplicate_response_tolerates_null_fields_and_skips_drafts` and +`duplicate_response_tolerates_a_null_list`. Not run against the reporter's account. + +--- + +### B285 — A restarted Secret Service leaves the process on a dead session + +**Status:** Fixed (unverified) +**Found:** 2026-11-14, following up [issue #78](https://github.com/narrrl/proton-drive-linux/issues/78) +(signed out after every cold boot on Fedora 45, which replaced gnome-keyring with oo7). See +[the investigation](ISSUE-78.md#a-restarted-secret-service). +**Where:** `crates/pdfs-core/src/auth.rs`, `with_keyring_entry` + +**Repro:** Write the session blob, restart the Secret Service provider +(`gnome-keyring-daemon --replace`, or `oo7-daemon --replace`), then read or write it again from +the same process. `dbus-secret-service-keyring-store` keeps one D-Bus connection and one Secret +Service session for the life of the process, and pdfs installed it once. The session exists only +in the provider's memory, so the new provider answers every `GetSecret` and `SetSecret` with +`org.freedesktop.Secret.Error.NoSession`, for as long as the process runs. A fresh store in the +same process reads the same entry fine. oo7's systemd unit restarts the daemon on failure, and +both providers can be replaced, so the daemon could meet this mid-session. A token refresh after +that point reached only memory and the kernel keyring: B61's retry kept asking on the dead +session, and the next cold boot read the spent refresh token (`InvalidRefreshToken`). + +**Fix:** Every keyring call goes through `with_keyring_entry`. A platform failure (which is what +`NoSession` and other D-Bus errors become) logs "the Secret Service failed this process's +connection; reconnecting", drops the store unless another thread already replaced it, and makes +the call once more on a new connection and session. A locked keyring, a dismissed prompt and a +missing entry come back unchanged. Affects the daemon, the app and the tray alike. + +**Verified:** `auth::tests::a_dropped_secret_service_session_is_reconnected_and_retried`, +`a_rotation_lands_at_once_after_the_keyring_restarts` and `a_locked_keyring_is_not_reconnected` +on the in-memory store. The ignored `auth::tests::real_secret_service_survives_a_provider_restart` +passes against gnome-keyring 51.1 and oo7 0.7.1 (built from its tag), each in a throwaway session +bus and restarted with `--replace`; it checks that a reconnect actually happened. A standalone +program holding one store, as before the fix, gets `NoSession` on read and write after the same +restart under both providers. Whether the reporter's oo7 restarts during a session is not known. + +--- + +### B284 — A keyring entry reported missing discards refreshed session tokens + +**Status:** Fixed (unverified) +**Found:** 2026-11-14, investigating [issue #78](https://github.com/narrrl/proton-drive-linux/issues/78) +after the reporter's oo7 0.7.0 retest ruled out the original persistence fix as a sufficient +explanation. See [the investigation and isolated reproducer](ISSUE-78.md). +**Where:** `crates/pdfs-core/src/auth.rs`, `TokenSink::flush` + +**Repro:** Refresh a session while the keyring lookup reports `NoEntry` for its existing blob. +oo7 0.7.0 does this for a locked collection (upstream #614), reproduced with fake tokens on a +private D-Bus connection. pdfs dropped the pending rotation and returned success, leaving the +spent refresh token in the saved blob without triggering B61's retry or kernel recovery paths. +The running session can still work, but restarting it exposes the stale saved credentials. + +**Fix:** Propagate the missing-session error so the rotation stays pending and is retried. +Persistence still requires the matching existing blob; a retry never recreates a deleted +session or overwrites a new login. This does not make a locked keyring writable, and pending +tokens still need to reach it before power-off to survive a cold boot. + +**Verified:** The two regression tests +`auth::tests::a_temporarily_missing_session_does_not_discard_a_rotation` and +`auth::tests::retrying_a_missing_session_never_recreates_it_or_overwrites_a_new_login` +failed before the fix and pass afterward. The oo7 fixture confirms its locked lookup behavior +and repeated binary `SetSecret` persistence. The reporter's complete Fedora cold-boot sequence +has not been reproduced; issue #78's precise trigger remains unconfirmed. + +--- + ### B283 — Videos have no thumbnail outside Photos, and a `.ts` recording looks like source code **Status:** Fixed, not verified live @@ -427,6 +715,76 @@ videos of over 70 MiB and takes nothing from a video with an uploader's thumbnai `real_video_fixture_yields_a_frame` passed on 2026-11-14 with ffmpeg 9.0 for a 46 MB MP4 with its index at the end, an MPEG-TS and an MKV. +### B282 — A file kept offline is downloaded again after a change made elsewhere + +**Status:** Fixed, not verified live +**Found:** 2026-11-13, while looking into B281. Nothing downloaded a pinned file again after the +pin had downloaded it once. +**Where:** `apply_event` in `crates/pdfs-fuse/src/background.rs`; the new pin keeper in +`crates/pdfs-fuse/src/keeper.rs` + +**Repro:** Pin a file, then change it from another device. The event deletes the cached copy. +The file stays listed as kept offline, `cached` is false, and the next read downloads it, or +fails without a network. Rename or move it elsewhere and the same happens, though its content +did not change. Add a file to a pinned folder from another device and it is never downloaded. +A folder pin whose downloads failed while the link was down never retries them. + +**Cause:** A remote change to a known node called `ContentCache::evict`, which does not check +pins, and only the pin request itself downloaded anything. The copy was also stored under the +size the listing held at pin time. That is the ciphertext size until the size upgrade lands +(B12), and a copy stored under it never matches the plaintext it holds. + +**Fix:** A remote change no longer evicts a pinned file's copy. The copy is checked against the +revision's `(mtime, size)`, so a read of a newer revision does not take it. A rename keeps the +revision's mtime (B123), so the copy stays valid. A pin keeper thread checks pins again and +downloads files that are missing or stale: + +- the pins a batch of remote events changed, after the batch's listings are dropped; +- a pinned folder that gets a new file; +- every pin at startup, when the link comes back, and after the event feed loses continuity; +- a file whose version is restored. + +Failed downloads are tried again with a backoff from 30 s to 30 min. Pinning settles a +provisional size before it stores the copy. + +**Test:** `a_file_kept_offline_stays_on_disk_through_changes_made_elsewhere` in +`crates/pdfs-fuse/src/sim/daemon.rs`. Another device edits a pinned file and a file in a pinned +folder, and adds a file to that folder. The test waits until all three are cached, then reads +them with the link down. With the keeper disabled, it fails with all three uncached. + +--- + +### B281 — A file in a synced folder cannot be kept offline + +**Status:** Fixed, not verified live +**Found:** 2026-11-13. An image was made available offline in the app, and it opened from the app +with no download. `feh` opened it from a terminal and downloaded it each time. The two were not +the same file. The app had kept `~/ProtonDrive/Pictures/photo.png`, in My files. The +terminal opened `~/Pictures/photo.png`, in the on-demand synced folder `~/Pictures`. That +copy is a separate node under Computers, uploaded on its own, with the same name and size but a +different mtime. The daemon treats every program the same way: it does not look at the process +that reads. +**Where:** the `Pin` and `Unpin` handlers in `crates/pdfs-fuse/src/control.rs` + +**Repro:** Add a synced folder in on-demand mode and put a file in it. `pdfs pin` on its absolute +path fails with "is not under the mountpoint". A relative path is resolved against My files, so +it keeps a file of the same name there, if one exists, or fails. Reading the file in the synced +folder then downloads it. + +**Cause:** Both handlers turned the path into a mount-relative one with `rel_to_mount`, which only +knows the primary mount. B86 moved `ls`, `refresh`, `rm` and `mv` to `route_to_mount`, which finds +the mount that serves an absolute path. Pin and unpin were left out. + +**Fix:** Pin and unpin route through `route_to_mount` and run on the mount that owns the path. A +path in a mirror folder gets an error that says its files are already on this computer. The pin +registry records the absolute local path for a pin made through an on-demand folder, so +`pdfs pins`, the prompt and the HUD open the file through the folder it was kept from. Pins +under the primary mount still record the path relative to it. + +**Test:** `pinning_a_file_in_an_on_demand_folder_keeps_that_folders_copy` in +`crates/pdfs-fuse/src/sim/daemon.rs`. It pins a file through an on-demand folder's absolute path, +takes the link down, and reads the file through that folder. + --- ### B280 — Uploading a batch of large photos swaps the machine until the daemon is killed diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 018fbd8..a5bdfe1 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -11,32 +11,73 @@ scratch (user data in `staging/` and `recovery/` is never touched by this). ## [Unreleased] -### Changed +## [3.8.0] — 2026-11-14 -- Update `proton-sdk` and `proton-drive-rs` to 0.11.0. - Convert epoch timestamps at the SDK boundary and explicitly preserve photo modification - times now that they are independent of capture times. See [the SDK review](SDK-0.11.0.md) - for features and breaking changes. -Schema: **45**. Synced folders now distinguish device backups from My files attachments. -Existing folders remain device backups; older builds cannot open the upgraded database. +Schema: **45**. Synced folders now tell device backups apart from folders attached to My files. +Existing folders stay device backups. Older builds cannot open the upgraded database. ### Added -- Shell completion for `pdfs` in bash, zsh and fish, installed by every package, plus elvish - and PowerShell through `pdfs completions SHELL`. Besides commands and options it completes - paths in My files and the ids and uids the running service knows (synced folders, the queue, - computers, trash, albums, shares, invitations, bookmarks, conflicts and versions), each with - a description. Share roles are now checked by the CLI (`viewer`, `editor`, `admin`) before - the request. -- `pdfs sync attach PATH --to REMOTE` connects an existing local directory to an existing - My files folder. The first sync preserves one-sided files and differing versions (#67). - Attachments stay in mirror mode, are excluded from device profiles, and cannot delete - the remote root when syncing is stopped. +- **Excluded files, opening files and the search launcher in Preferences** (#99). Settings that + could only be changed in `config.json` now have a place in the app: + - **Sync → Excluded Files**: the patterns every synced folder leaves out. Add your own, such as + `*.db` or `build/`, and turn each built-in pattern on or off. A change applies at once. + - **Sync → Conflict Copies**: whether identical conflict copies are only reported, moved to the + trash, or not looked for. + - **Apps → Open Files With**: which program opens which kind of file, in order, optionally in a + terminal, for example `nvim` for text files. A change applies to the next file opened. + - **Apps → Search Launcher**: whether `pdfs-prompt` opens its own window, your dmenu-style + launcher (rofi, wofi, fuzzel, bemenu) or fzf in a terminal. + + The Sync page's Folders view lists **Excluded Files** under the folders: how many patterns + apply to all of them and to each one, and **Add Pattern** to add one for all synced folders or + for a single folder. A folder's own rules (its `.pdfsignore`) open in an editor, also from the + folder's menu. It checks every line as you type, tells you whether a path is synced and which + pattern decides, and asks before replacing rules that changed on another computer meanwhile. + `pdfs sync ignore list|add|remove|reset|check` does the same from a terminal, and `--folder` + picks a single folder. + `pdfs-app --page preferences-sync` (or `-apps`, `-storage`, plain `preferences`) opens + Preferences at that page. +- **More clutter left out of synced folders by default**: `desktop.ini`, `.directory`, trash + folders, `.fuse_hidden*` and `.nfs*` leftovers, partial downloads (`*.part`, `*.crdownload`), + Kate swap files and office lock files. This only changes folders where you never set your own + list. Leaving a file out never deletes its copy on Proton Drive. +- **An install without a package, for immutable distributions** (#99). The release `.tar.gz` now + carries everything around the programs and an `install.sh` that installs them into your home + folder, no root needed: `~/.local` for the programs, menu entry, icon, translations and shell + completion, `~/.config` for the service and the tray's login entry. It checks the libraries + first and names the packages to install, refuses to install next to a package of the client, + updates in place (restarting a running service) and removes itself with `--uninstall`, leaving + settings and data alone. The app finds its translations and the tray beside its own program, so + nothing has to be on the `PATH`. [Installation](INSTALL.md) also shows how to layer the COPR + package on Fedora Atomic (Silverblue, Kinoite, Bazzite, Bluefin). +- **Sync a local folder with a folder you already have in My files** (#67, #77). + `pdfs sync attach ~/Work --to Work` keeps `~/Work` an ordinary local folder and syncs it both + ways with My files/Work. The first sync uploads files that are only on this computer, downloads + files that are only on Proton Drive, and keeps both versions of a file that differs: the one on + Proton Drive keeps its name, and the local one becomes a conflict copy next to it and is + uploaded too. Attached folders are always kept on this computer, never online only. They are + listed apart from device backups and are not part of the device profile used to restore another + computer. Stopping one keeps both copies, and `--delete-remote` is refused for it. If the folder + on Proton Drive is trashed or moved out of My files, syncing stops with an error and the local + files stay. Attaching a folder is done from a terminal for now. + +- **Tab completion for `pdfs`** in bash, zsh and fish, installed by every package. Besides + commands and options it completes paths in My files (`pdfs pin Doc`) and what the running + service knows, each with a description: synced folders, queued changes, computers, trash, albums, + items shared with you, invitations, bookmarks and conflict copies, and once a path is typed, the + file's versions or the share's members and link. Without the service it still completes commands + and options. Elvish and PowerShell load it from `pdfs completions SHELL`, and so does a source + build; the [command-line reference](CLI.md#shell-completion) has the line for each shell. + `pdfs share` and `pdfs share-role` now check the role (`viewer`, `editor` or `admin`) before + sending the request, and `pdfs public-link` does the same for `viewer` and `editor`. + - **Video thumbnails in My files, Shared, Trash and the launcher**, as in Photos, with a play badge in the grid. A video's thumbnail is the one its uploader attached when it has one. Otherwise the daemon takes a frame with ffmpeg from the start of the file, plus an MP4's index, and never downloads more than about 16 MiB of a video for it, however large the video is. **Build Thumbnails** covers videos too (B283). + - **A new layout for Photos.** - The grid shows every photo at its own shape, in rows that fill the width (portraits are no longer cropped to squares). **Square Tiles** in the page menu brings the even @@ -61,18 +102,174 @@ Existing folders remain device backups; older builds cannot open the upgraded da - The zoom level and the tile shape are remembered between runs. - In the photo viewer, the details panel lies over the photo when the window is narrow instead of squeezing it. + - **Larger icons in My files.** The icon size slider goes up to 256 px, big enough to tell photos apart by their thumbnail. Ctrl+scroll, Ctrl+plus, Ctrl+minus and Ctrl+0 change it too, and the size is remembered between runs. +- **Back takes you to where you were.** Back, Forward and Up in My files return to a folder + scrolled where it was left, with the same items selected, and closing the photo viewer scrolls + the grid to the photo it was showing. Alt+Left and Backspace go back a level in Shared with me + and Computers and leave an open album or place in Photos, as Escape does too. + +- **Share photos, and more in the photo viewer.** + - **Share…** in a photo's menu and in the viewer's ⋮ menu opens the usual Share dialog: invite + people or make a public link. It is not offered for a photo in an album someone else shared. + - **Copy Image** (Ctrl+C) puts the photo on screen on the clipboard. + - A strip of thumbnails along the bottom of the viewer shows the photos around the one on + screen; click one to jump to it. It fades with the other controls and stays hidden during a + slideshow. + - The line under the date names the town the photo was taken in, when it is known. + +- **Drag photos out of Photos.** Select photos, then drag one of them into a file manager, a chat + or any app that takes files. The photos download when dropped and arrive under their own + names. A drag that starts on a photo that is not selected still selects, as before. + +- **Upload progress in Photos.** Uploading several photos at once shows "Uploading 3 of 12 + items…" with a progress bar above the timeline, instead of a single toast at the start. + +- **Videos and music play in the app.** Opening a video or a song in My files, by a double-click + or **Open**, plays it in a player window, streamed from the mount like before. **Open With…** + still hands it to another player, and the separate **Play** item is gone. When the file can't + be played (a codec GStreamer lacks), the window says so and offers **Open With…**. Both this + player and the one in Photos gained a fullscreen button, fullscreen on a double-click, + **Repeat** in the ⋮ menu, `K` to play and pause, `J` and `L` (or Shift+←/→) to jump + 10 seconds, and `M` to mute. The volume carries over from one video to the next, and the + thumbnail strip in Photos stays off a video, where it covered the seek bar. + +- **Album covers for songs.** A song in My files, in search and in Shared shows its cover as its + thumbnail, and the player shows it above the controls instead of a music note. The cover is the + picture in the song's tags (MP3, FLAC, Ogg, Opus, M4A, WAV, AIFF, WavPack, Monkey's Audio), or + else a `cover`, `folder`, `front` or `album` image (`.jpg`, `.png`, `.webp`) in the same folder. + Only the song's tags are downloaded for it, not the whole song. Songs uploaded from this computer + carry their cover as their thumbnail, so they show it at once on other devices too. A cover + image added to a folder later is picked up once the song changes or the service restarts. + +- **Resume a Google Photos import** (B288). The archives and the outcome of the last import are + kept, so an import cut short by a restart or a reboot no longer has to be set up again. The + Import page offers **Resume**, and `pdfs import-google-photos --resume` does the same. Photos + that are already uploaded are skipped as before. + +- **More pages to open the app on.** `pdfs-app --page` also takes `albums`, `places`, + `places-map` and `sync-folders`. + +### Changed + +- **`proton-sdk` and `proton-drive-rs` 0.12.0.** Timestamps are converted where they enter + from the SDK, and a photo keeps its own modification time now that it no longer follows the + capture time. 0.12.0 fixes the photo duplicate check and failed-upload drafts below. + [The 0.11.0 review](SDK-0.11.0.md) and [the 0.12.0 review](SDK-0.12.0.md) list the new + features and breaking changes. + +- **The Google Photos import brings more of the export along** (B289). + - Places from Google Photos are kept, so imported photos show up under **Places**. + - Live photos and Pixel motion photos stay one photo: the video is filed with its still and + takes its date. The `.MP` half of a motion photo is no longer skipped. + - Only the Google Photos part of an export is imported. Files from Google Drive, YouTube and + other services in the same Takeout stay out of the timeline. + - A photo without a date of its own is dated by its file name and then by its EXIF before + the day it reached Google Photos. + - Favorites are also set on photos that were already in Proton Photos. + +- **The import reports what it is doing** (B288). The Import page shows the current step + (reading, checking for photos you already have, uploading, sorting into albums), the counts + so far, and a progress bar that reaches the end. A scan says how many photos and how much + data an import would upload. `pdfs import-google-photos --wait` prints its progress, and + `pdfs import-status` shows the step. + ### Fixed -- Large `.ts` recordings (MPEG transport streams) show and open as videos: the video icon, a - thumbnail, and the `@media` open rule rather than `@text`. A `.ts` of 1 MiB or more counts - as a video, a smaller one stays TypeScript source, and the daemon checks the stream's sync - byte before it takes a frame (B283). -- A video over 256 MiB never got a thumbnail, and a smaller one was downloaded whole into - memory for one frame (B283). +- **Two programs changing settings at once no longer lose one of the changes** (B292). The app, + the tray and the service now change `config.json` under a lock, so for example a new cache + size saved by the service and the window size saved by the app both stay. +- **Resuming a Google Photos import no longer fails at once** (#94, B286, B287). A photo upload + that was cut short left a draft behind, and every later run stopped with + `check for existing photos: serialization error: invalid type: null`. Such drafts are now + skipped by the duplicate check and replaced by the next upload of that photo, also after + signing in again. A failed upload removes its draft. + +- **One bad archive or error no longer ends the whole import** (B288). An archive that cannot be + read is left out and named in the report, and the rest of the export imports. When an import + stops early, it keeps the counts of what it did and still files the uploaded photos into their + albums. A restart of the service while importing is reported as an interruption, not as a + finished import. + +- **Photos zoomed all the way out shows the whole timeline.** The first 200 photos fit on one + screen there, and with nothing to scroll no more were loaded. More now load whenever the photos + don't fill the window, also after a zoom or a resize. + +- **Signed out after every reboot on Fedora 45** (issue #78, B284, B285, B291). Fedora 45 replaced + the GNOME keyring with oo7, and the oo7 build it ships keeps a changed keyring entry only in + memory: every refreshed sign-in the client saved was gone after a reboot, and the next start + found one Proton had already retired. The client now saves the sign-in as a new entry that + replaces the old one, which that oo7 does write to disk. It also no longer drops a refreshed + sign-in when oo7 reports the entries of a locked keyring as missing, and it reconnects to a + keyring service that restarted. + +- **Thumbnails appear in the folder you are looking at as soon as they are made.** During + **Build thumbnails**, the folder on screen kept its file icons until you left it and opened it + again. Its tiles now fill in while the build runs, and the same goes for any tile whose + thumbnail is made while it is on screen. + +- **My files comes back after the service was killed for hanging** (B279). A killed service left + its folders behind as dead mounts, and every restart then failed with `File exists` and retried + forever without opening anything, until the mounts were cleared by hand. The service now clears + a dead mount before it opens the folder. On a large account a restart that did open My files was + killed again, because loading the account took longer than the 2 minutes the service has to + report in. Startup now gets 10 minutes, and the 2 minutes apply again once the service answers. + +- **Uploading many large photos no longer runs the computer out of memory** (B280). Every image + upload decodes the whole picture to make its thumbnail, and up to 16 did so at once. A batch of + 100-megapixel photos took about 9 GiB, filled the swap, and the service was killed on every + restart. Thumbnails now share 512 MiB between them: phone photos are still made side by side, + and a very large photo waits and is made on its own. + +- **Keep a file offline in an on-demand synced folder** (B281). `pdfs pin` and `pdfs unpin` now + accept a path in a synced folder such as `~/Pictures`. Before, they only took paths under the + Proton Drive mount, so the copy in the synced folder was downloaded again by every program that + read it. + +- **Files kept offline stay on this computer when they change elsewhere** (B282). A change from + another device, even a rename, deleted the copy of a file kept offline, and nothing downloaded + it again, so the next read downloaded it or failed without a network. Files added to a folder + kept offline from another device were never downloaded. The copy now stays until the new + version is downloaded, which happens on its own. Files kept offline are also checked at + startup and when the connection returns, and failed downloads are tried again. + +- **Large `.ts` recordings show and open as videos** (B283). An MPEG transport stream got the + text icon, no thumbnail, and the `@text` open rule. It now gets the video icon, a thumbnail + and the `@media` rule. A `.ts` of 1 MiB or more counts as a video, a smaller one stays + TypeScript source, and the daemon checks the stream's sync byte before it takes a frame. + +- **Videos over 256 MiB get a thumbnail** (B283). They never got one, and a smaller video was + downloaded whole into memory for one frame. + +- **A folder opens at the top.** A folder opened in My files, Shared with me or Computers could + open scrolled to where its parent was left, often at the bottom. Another folder, a new search + and a new Photos filter, album or place now start at the top, and a refresh keeps its place. + +- **Closing a photo opened from the Places map goes back to the map**, not to the town's + timeline. + +- **Albums and Places stay on screen.** They could switch to the timeline by themselves when a + timeline page arrived late, after an upload or after a trash Undo. Coming back to Photos no + longer reloads the timeline and loses its scroll position, unless photos changed while it was + out of sight. After an album or a place, the timeline came back unfiltered while the kind tab + and the date still showed the filter. + +- **A search in My files stays.** It was replaced by the folder's listing whenever files + changed, on refresh and on coming back to the page, while the query stayed in the box. + +- **The photo viewer's buttons are readable on any photo.** They were white on a see-through + white, and vanished over a bright photo. The bar now keeps only the favorite star, the details + and a menu, which names Slideshow, Save a Copy, Open With, Add to Album, Show on Map and Move to + Trash in words where eight icons stood. A shot stored as more than one file shows a "RAW" or + "Live" switch, or "2 of 3" for a burst, where an unlabelled icon was. The title reads + "Wednesday, October 7, 2026, 13:42" rather than a timestamp. Delete asks first, as it does in + the grid, and Alt+Left closes the viewer. + +- **A date jump in Photos stops when you look elsewhere.** A jump still loading the timeline + kept going after another filter, album or place was opened. ## [3.7.0] — 2026-11-02 diff --git a/docs/CLI.md b/docs/CLI.md index df95563..c3dc131 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -102,7 +102,7 @@ commands are for scripts and for cases where the mount is not available. | Command | Description | |---|---| -| `pdfs pin PATH` | Download a file completely and keep it on this computer. For a folder, its files download in the background (`pdfs transfers` shows the progress). | +| `pdfs pin PATH` | Download a file completely and keep it on this computer. For a folder, its files download in the background (`pdfs transfers` shows the progress). PATH can be under the Proton Drive mount or in an on-demand synced folder. A file in a mirror folder is already on this computer. | | `pdfs unpin PATH` | Release it back to online only. A file inside a folder kept offline cannot be released on its own. | | `pdfs pins` | List files and folders kept available offline. | @@ -119,6 +119,10 @@ commands are for scripts and for cases where the mount is not available. | `pdfs sync reset ID` | Forget what a synced folder synced before and compare it with Proton Drive again. A file on one side only is copied to the other, and a file that differs keeps both versions. Nothing is deleted. | | `pdfs sync pause [--for 1h] [--folder ID]` | Pause uploads and synced folders. Reading files keeps working. `--for` ends the pause by itself (`30m`, `1h`, `2d`). | | `pdfs sync resume [--folder ID]` | Resume after a pause. | +| `pdfs sync ignore list [--folder PATH]` | Print the patterns every synced folder leaves out; built-in ones are marked. With `--folder`, the own patterns of the synced folder holding `PATH`. | +| `pdfs sync ignore add PATTERN [--folder PATH]` / `remove PATTERN [--folder PATH]` | Leave out, or stop leaving out, files matching a gitignore-style pattern such as `*.db` or `build/`, in every synced folder or, with `--folder`, in that folder's `.pdfsignore` only. A pattern starting with `-` goes after `--`. | +| `pdfs sync ignore reset` | Use the built-in patterns again. | +| `pdfs sync ignore check PATH` | Say whether a file in a synced folder is synced, and which pattern decides. | | `pdfs sync limit [--up RATE] [--down RATE]` | Cap bandwidth, for example `500K` or `2M` per second. `0` removes a cap. Without options, prints the current caps. | | `pdfs sync queue` | List changes that have not reached Proton Drive yet. | | `pdfs sync issues` | List queued changes Proton Drive refused or that keep failing, and what to do. | @@ -196,6 +200,7 @@ commands are for scripts and for cases where the mount is not available. | `pdfs add-to-album UID PHOTOS...` | Add photos to an album. | | `pdfs remove-from-album UID PHOTOS...` | Take photos out of an album. | | `pdfs import-google-photos ARCHIVES... [--dry-run] [--wait]` | Import a Google Takeout export. Pass every `.zip` of the export. | +| `pdfs import-google-photos --resume [--wait]` | Start the last import again with the same archives after it was interrupted, stopped or failed. | | `pdfs import-status` / `pdfs cancel-import` | Follow or stop a running import. | | `pdfs redate-photos [--dry-run] [--from DATE] [--to DATE] [--limit N] [--wait]` | Fix photos filed under their upload day when the file name carries the real date. | | `pdfs redate-status` / `pdfs cancel-redate` | Follow or stop a running re-date. | diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 1f3fd27..1ce6da5 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -44,9 +44,13 @@ the [security model](SECURITY_MODEL.md#what-is-kept-on-this-computer). ## `config.json` The file is created with defaults on first start. Every key is optional; a missing key takes its -default. Edit it while the app is closed, then restart the service +default. Most keys can be changed in the app, under **Preferences** (the "In app" column below). +To edit the file by hand, close the app first, then restart the service (`systemctl --user restart proton-drive.service`) for service settings to apply. +The app, the tray and the service each change the file under a lock (`config.json.lock` next to +it), so two of them changing different settings at once both keep their change. + An unreadable file is never overwritten: the programs log an error and run with defaults until you fix it. @@ -80,9 +84,9 @@ fix it. | `cache_budget` | bytes | 5 GiB | Storage → Cache | Soft limit for cached content. `0` means unlimited. Available-offline files do not count against it | | `upload_limit` | bytes/s | none | General → Network | Shared cap for all uploads. `0` or `null` means none | | `download_limit` | bytes/s | none | General → Network | Shared cap for all downloads, reads from the mount included | -| `ignore_patterns` | list | see below | — | Global ignore rules for synced folders | +| `ignore_patterns` | list | see below | Sync → Excluded Files | Global ignore rules for synced folders | | `device_uid` | string | none | Computers → Continue This Backup Here… or Sync With This Computer… | The computer this machine syncs into. Unset means "match by hostname" | -| `conflict_sweep` | `off` / `report` / `enforce` | `report` | — | See [`conflict_sweep`](#conflict_sweep) | +| `conflict_sweep` | `off` / `report` / `enforce` | `report` | Sync → Conflict Copies | See [`conflict_sweep`](#conflict_sweep) | | `local_first` | bool | `true` | — | See [`local_first`](#local_first) | | `language` | gettext code | system | General → Appearance | Language of the app, tray and prompt, such as `de` or `pt_BR` | | `proton_theme` | bool | follow system | General → Appearance | Proton's colours instead of the system theme | @@ -93,8 +97,8 @@ fix it. | `update_latest` | version | none | — | The latest release the update check found | | `files_view` | object | grid, by name | My files view menu | `list`, `sort` (`name`, `size`, `modified`), `descending`, `folders_first` | | `window` | object | 980 × 680 | Resizing the app window | `width`, `height` (the unmaximized size), `maximized` | -| `open_with` | object | `xdg-open` | — | See [`open_with`](#open_with) | -| `prompt` | object | built-in window | — | See [`prompt`](#prompt) | +| `open_with` | object | `xdg-open` | Apps → Open Files With | See [`open_with`](#open_with) | +| `prompt` | object | built-in window | Apps → Search Launcher | See [`prompt`](#prompt) | `app_version` and `user_agent` identify the client to Proton. When the keys are absent, the client sends its own release (`external-drive-linux@-stable`, `proton-drive-linux/`). @@ -109,10 +113,14 @@ Gitignore-style rules applied to every synced folder, in addition to each folder ```json [".git/", ".hg/", ".svn/", "node_modules/", "target/", ".venv/", "__pycache__/", - "*~", "*.swp", "*.tmp", ".DS_Store", "Thumbs.db"] + "*~", "*.swp", "*.tmp", ".DS_Store", "Thumbs.db", + "desktop.ini", ".directory", ".Trash-*/", ".Trashes/", ".Spotlight-V100/", + ".fuse_hidden*", ".nfs*", "*.part", "*.crdownload", "*.kate-swp", ".~lock.*#", "~$*"] ``` -Set it to `[]` to turn the defaults off. See +Preferences → Sync → Excluded Files edits this list, **Add Pattern** on the Sync page's Folders +view adds to it, and `pdfs sync ignore` does the same from a terminal. A list equal to the defaults is saved as absent, so it follows the defaults of later +releases. Set it to `[]` to turn the defaults off. See [Synced folders](SYNCED_FOLDERS.md#excluding-files-with-pdfsignore) for how excluding behaves. ### `conflict_sweep` diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index 3b1804d..23fe6ea 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -161,6 +161,29 @@ links, copies the images and writes the sidebar. `docs/README.md` becomes the wi Never edit the wiki in the browser: the next publish replaces every page. +**Screenshots.** The pictures in `images/` (except the cover) are taken by a script, from an +invented account, so they never show anyone's files: + +```bash +scripts/screenshots.sh # all of them, into images/ +scripts/screenshots.sh --only photos,files # just these +scripts/screenshots.sh --website ../proton-drive-linux-website # and copy them to the website +``` + +It builds the app and runs it against `crates/pdfs-core/examples/demo_daemon.rs`, a stand-in +daemon that answers the control socket with made-up files, photos, places, computers and history. +The app runs in a headless `cage` compositor with its own HOME, XDG directories, session bus and +keyring, so nothing touches your account, your keyring or a running service. It needs `cage`, +`grim`, `wlr-randr`, ImageMagick, `dbus-run-session` and `gnome-keyring-daemon`, plus `wtype` for +the launcher's query, and `fzf` and `alacritty` for its fzf variant. The demo photos come from +[picsum.photos](https://picsum.photos) (Unsplash licence) on the first run and are cached in +`~/.cache/pdfs-screenshots`. Dates are relative to the day you run it. + +Retake them before a release, and look at each one before committing. A page that grew new data +needs a matching answer in the demo daemon; the requests it has no answer for are logged in +`demo_daemon.log` (keep it with `--keep`). The demo daemon is built with every `cargo test` and +`cargo clippy --all-targets`, so a change to the control protocol that breaks it fails CI. + ## Releasing Releases are cut from `main` by pushing a `v*` tag. `.github/workflows/release.yml` then: diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 3731583..5870efc 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -21,7 +21,8 @@ again. | [Arch Linux](#arch-linux-aur) | `proton-drive-for-linux` in the AUR | With your AUR helper | | [Debian, Ubuntu 24.04 or newer](#debian-and-ubuntu-2404-or-newer) | `.deb` from the release page | Download each release | | [NixOS](#nixos-flake) | The flake in this repository | With `nix flake update` | -| [Anything else](#other-distributions) | `.tar.gz` from the release page, or [build from source](#build-from-source) | By hand | +| [Fedora Atomic](#fedora-atomic-silverblue-kinoite-bazzite-bluefin) (Silverblue, Kinoite, Bazzite, Bluefin) | The COPR repository, layered with `rpm-ostree` | With `rpm-ostree upgrade` | +| [Anything else](#other-distributions), immutable ones included | `.tar.gz` from the release page, installed into your home folder by its `install.sh`, or [build from source](#build-from-source) | Run the new release's `install.sh` | Release packages are for x86_64. You also need a Secret Service provider, such as GNOME Keyring, KWallet or KeePassXC, to keep your session. Most desktops have one already. @@ -102,7 +103,7 @@ That URL follows `main`. To stay on releases, pin the input to a release tag ins the tag to update: ```nix -inputs.proton-drive-linux.url = "github:narrrl/proton-drive-linux/v3.7.0"; +inputs.proton-drive-linux.url = "github:narrrl/proton-drive-linux/v3.8.0"; ``` Each release tag is built with Nix before the release is published. @@ -139,12 +140,42 @@ the module) and a Secret Service such as GNOME Keyring or KWallet. To leave out it video thumbnails, override the package with `withFfmpeg = false`. The recipe is in [`packaging/nix/`](../packaging/nix/). +### Fedora Atomic (Silverblue, Kinoite, Bazzite, Bluefin) + +The system is read-only, so layer the COPR package on top of it. It brings its libraries along: + +```bash +sudo curl -o /etc/yum.repos.d/proton-drive-linux.repo \ + "https://copr.fedorainfracloud.org/coprs/narl-rs/proton-drive-linux/repo/fedora-$(rpm -E %fedora)/narl-rs-proton-drive-linux-fedora-$(rpm -E %fedora).repo" +rpm-ostree install proton-drive-linux +systemctl reboot +``` + +`rpm-ostree upgrade` brings new releases with the system's own updates. + ### Other distributions -Unpack the `.tar.gz` from the [latest release](https://github.com/narrrl/proton-drive-linux/releases/latest) -into a directory on your `PATH`, then install the service as shown in -[Using a binary outside `/usr/bin`](#using-a-binary-outside-usrbin). The binaries need GTK 4.12, -libadwaita 1.5, WebKitGTK 6.0 and FUSE 3; see [Requirements](#requirements). +The `.tar.gz` from the [latest release](https://github.com/narrrl/proton-drive-linux/releases/latest) +installs into your home folder, without root, so it also works where the system is read-only: + +```bash +tar -xzf proton-drive-linux-*-x86_64.tar.gz +proton-drive-linux-*-x86_64/install.sh +``` + +It puts the programs in `~/.local/bin`, the menu entry, icon, translations and shell completion in +`~/.local/share`, the service in `~/.config/systemd/user` and the tray's login entry in +`~/.config/autostart`, each pointing at `~/.local/bin`. Then open **Proton Drive for Linux** from +your applications menu. `--prefix DIR` installs somewhere else. + +The system has to provide glibc 2.39 or newer (the binaries are built on Ubuntu 24.04), GTK 4.12, +libadwaita 1.5, WebKitGTK 6.0 and FUSE 3 with a setuid `fusermount3`; see +[Requirements](#requirements). `install.sh` checks the libraries first and names the packages to +install for each distribution. It refuses to install next to a package of this client, since the +two would run different versions of the service. + +To update, download the new release and run its `install.sh`; a running service restarts on the new +version. Turn on **Preferences → General → Check for updates** to hear about new releases. ## Optional extras @@ -195,7 +226,9 @@ browser, solve it, then press Enter. | COPR (Fedora) | `sudo dnf upgrade` | | AUR | `paru -Syu`, or your AUR helper's equivalent | | Nix flake | `nix flake update proton-drive-linux`, or change the pinned tag, then rebuild | -| `.deb`, release `.rpm`, `.tar.gz` | Download the new release and install it over the old one | +| Fedora Atomic | `rpm-ostree upgrade`, then reboot | +| `.deb`, release `.rpm` | Download the new release and install it over the old one | +| `.tar.gz` | Download the new release and run its `install.sh` | The service switches to the new version the next time it starts. To switch at once: @@ -245,6 +278,8 @@ open in an older release, so going back to an older version means clearing the l | `.deb` | `sudo apt remove proton-drive-linux` | | AUR | `sudo pacman -R proton-drive-for-linux` (or the `-bin` or `-git` package) | | NixOS | Remove the module and the flake input, then rebuild | + | Fedora Atomic | `rpm-ostree uninstall proton-drive-linux`, remove `/etc/yum.repos.d/proton-drive-linux.repo`, then reboot | + | `.tar.gz` | `~/.local/share/proton-drive-linux/install.sh --uninstall` | Removing the package also stops the service of every logged-in user, in case step 1 was skipped. @@ -353,8 +388,10 @@ sudo dnf install packaging/out/x86_64/proton-drive-linux-*.rpm ### Using a binary outside `/usr/bin` -The service unit starts `/usr/bin/pdfs daemon`. If you installed the programs somewhere else, for -example `~/.local/bin`, install the unit for your user and point it at your binary: +The release tarball's `install.sh` does all of this for you (see +[Other distributions](#other-distributions)). For programs you built yourself: the service unit +starts `/usr/bin/pdfs daemon`. If you installed the programs somewhere else, for example +`~/.local/bin`, install the unit for your user and point it at your binary: ```bash mkdir -p ~/.config/systemd/user @@ -382,3 +419,7 @@ Every package installs the same files: | `/usr/lib/systemd/user/proton-drive.service` | User service that keeps the drive mounted | | `/etc/xdg/autostart/io.narl.proton-drive-linux-tray.desktop` | Starts the tray at login | | `/usr/share/locale/*/LC_MESSAGES/pdfs.mo` | Translations | + +The release tarball's `install.sh` puts the same files under `~/.local` instead of `/usr`, the +service in `~/.config/systemd/user` and the tray's login entry in `~/.config/autostart`. It keeps a +list of them in `~/.local/share/proton-drive-linux/installed-files` for `--uninstall`. diff --git a/docs/ISSUE-78.md b/docs/ISSUE-78.md new file mode 100644 index 0000000..8ce83f0 --- /dev/null +++ b/docs/ISSUE-78.md @@ -0,0 +1,175 @@ +# Issue #78: session loss on Fedora 45 + +Investigation on 2026-11-14 of [issue #78](https://github.com/narrrl/proton-drive-linux/issues/78), +using its report and comments through `gh`, pdfs 3.7.0/current source, and the oo7 0.7.0 tag. + +## What is established + +The reporter still saw the problem with oo7 0.7.0 on November 7. The original +[SetSecret persistence fix](https://github.com/linux-credentials/oo7/commit/7fb11dd111c4be8fec01996e3caa31c44fa962b3) +is present in that release. In an isolated test, three successive binary `SetSecret` updates +survived reopening the keyring file. Repeating the original persistence explanation is therefore +insufficient. + +A different, reproducible failure exists at the boundary between oo7 and pdfs: + +1. oo7 0.7.0's `Collection::search_inner_items` returns an empty list for a locked collection. + `SearchItems` consequently returns **both** the unlocked and locked lists empty even when the + matching item is on disk. The isolated test reproduces this with pdfs's exact lookup attributes. + This is upstream [oo7 #614](https://github.com/linux-credentials/oo7/issues/614). +2. `dbus-secret-service-keyring-store` 1.0.1 maps an empty search to `keyring_core::Error::NoEntry`; + pdfs maps that to `Error::NotLoggedIn`. The client cannot offer an unlock when no locked item + is reported. +3. In pdfs 3.7.0 and the code before this change, `TokenSink::flush` treated `NotLoggedIn` as a + logout, cleared the pending rotation and returned success. It skipped the error path that + retains the new tokens in the kernel keyring and retries their persistence. The code was + unchanged between v3.7.0 and this investigation. +4. If a token refresh encounters that reply, the saved blob retains a spent refresh token. The + running process can keep working with the new tokens in memory; a restart then exposes the + stale saved credentials. A regression test failed before the fix and passes afterward. + +The same unsafe assumption also applies to transient empty lookups such as the race reported in +[oo7 #652](https://github.com/linux-credentials/oo7/issues/652), fixed in 0.7.1. That issue's +replacement race was not independently reproduced here; pdfs normally updates existing items +with `SetSecret`, so it must not be assumed to encounter the same-item `CreateItem` race. + +## Change and limits + +`TokenSink::flush` now propagates a missing-session error instead of discarding the rotation. +The existing retry, kernel-keyring recovery and unsaved-token status paths can then run. Once +the collection is unlocked and readable, the pending rotation is saved. A genuinely deleted +session is never recreated: flushing still requires a readable blob with the matching session +ID and expected refresh token. A new login causes the old rotation to be discarded as before. + +This is a confirmed pdfs defect (B284), **not a confirmed complete explanation of the reporter's +cold-boot failure**. The issue has no oo7 logs or record showing that its collection was locked +when a token rotated. A locked keyring only at startup can hide a login, but does not by itself +explain `InvalidRefreshToken`. The token-loss path requires a successful refresh while the saved +entry is unreadable. + +The fix does not make a locked keyring writable. If it stays unreadable until power-off, tokens +retained in process/kernel memory still cannot survive a cold boot. No new disk credential store +or credential format is introduced. + +Other upstream Fedora-upgrade issues were checked: [oo7 #651](https://github.com/linux-credentials/oo7/issues/651) +creates duplicate placeholder/migrated collections and refuses writes; [oo7 #662](https://github.com/linux-credentials/oo7/issues/662) +can resolve the default alias to a locked collection. Both have fixes in +[0.7.1](https://github.com/linux-credentials/oo7/releases/tag/0.7.1), released November 10, +after the reporter's November 7 retest. Neither can be assigned as this report's cause without +matching runtime evidence. + +## SetSecret is kept only in memory in Fedora's oo7 + +Follow-up on 2026-11-15. The reporter's `rpm -qa 'oo7*'` shows +`oo7-daemon-0.7.0~beta^1.e91b98a-1.fc45`, not the 0.7.0 release assumed above. Bodhi lists that +build as the stable oo7 for Fedora 45 and 46; neither has 0.7.0 or 0.7.1. Commit e91b98a +(October 24) predates +[7fb11dd](https://github.com/linux-credentials/oo7/commit/7fb11dd111c4be8fec01996e3caa31c44fa962b3) +(October 28): "Setting the Label or Attributes of an item, or calling SetSecret, only changed the +item in memory, so the old values came back once the daemon restarted." 0.7.0 carries it as +37e3334. + +pdfs writes an existing entry with `SetSecret`, so on that build every saved token rotation was +lost at the next reboot. This explains the reporter's log: the keyring was unlocked one second +before pdfs started (they type their password at login) and still held a spent refresh token. +It does not need a locked keyring or a restarted provider. + +pdfs now saves an existing entry with `CreateItem` and `replace` instead +(`crates/pdfs-core/src/secretservice.rs`, B291), which e91b98a writes to the keyring file. +Reproduced and verified with oo7-daemon built at e91b98a in a throwaway session bus: + +```sh +git clone https://github.com/linux-credentials/oo7.git /tmp/oo7 && git -C /tmp/oo7 checkout e91b98a +cargo build --manifest-path /tmp/oo7/Cargo.toml -p oo7-daemon --bin oo7-daemon +OO7=/tmp/oo7/target/debug/oo7-daemon +T=$(mktemp -d /tmp/ss.XXXX); mkdir -p $T/data $T/run; chmod 700 $T/run +XDG_DATA_HOME=$T/data XDG_RUNTIME_DIR=$T/run OO7=$OO7 PDFS_SECRET_SERVICE_RESTART_TEST=1 \ + PDFS_RESTART_PROVIDER='pkill -x oo7-daemon; sleep 1; (printf pw | setsid "$OO7" --login >/dev/null 2>&1 &); sleep 2' \ + dbus-run-session -- sh -c 'printf pw | setsid "$OO7" --login >/dev/null 2>&1 & sleep 2; + cargo test -p pdfs-core real_secret_service_keeps -- --ignored' +``` + +With the in-place write, the read after the restart returns the first of three saves (`r1`); +with the replacing write it returns the last (`r3`), and one item is left. + +The daemon also logs a short fingerprint of the refresh token when it resumes the stored session +and each time it saves a rotation (`refresh=1a2b3c4d`). If a start is refused, compare the +fingerprint it resumed with against the last one saved in the boot before: + +```sh +journalctl --user -b -1 -u proton-drive.service | grep -E 'refresh=' | tail -2 +journalctl --user -b 0 -u proton-drive.service | grep -E 'refresh=' | head -2 +``` + +The same fingerprint means the keyring kept the last save and Proton refused it; a different one +means the keyring lost it. + +## A restarted Secret Service + +A second path to the same symptom, independent of locking (B285). The keyring store pdfs used +holds one D-Bus connection and one Secret Service session for the whole process, and was +installed once. The session lives only in the provider's memory. When the provider restarts, +the new one answers every `GetSecret` and `SetSecret` on the old session with +`org.freedesktop.Secret.Error.NoSession`, until the pdfs process itself restarts. Reproduced +with both gnome-keyring 51.1 and oo7 0.7.1 using `--replace`. + +gnome-keyring practically never restarts within a desktop session. oo7 can: +`oo7-daemon.service` has `Restart=on-failure`, the daemon allows itself to be replaced, and it +exits on a wrong credential password and is D-Bus-activated again. A daemon that met a restarted +provider kept every later token refresh only in memory and the kernel keyring, and a cold boot +then left the spent refresh token in the saved blob. + +Now a platform failure drops the store and the call is retried once on a new connection. The +daemon log shows `the Secret Service failed this process's connection; reconnecting` when that +happens. To check whether a machine meets this, look for oo7 restarts in the same boot: + +```sh +journalctl --user -b -1 -u oo7-daemon.service | grep -E 'Started|Stopped|Starting oo7|Main process exited' +journalctl --user -b -1 -u proton-drive.service | grep -iE 'keyring|reconnecting|refresh' +``` + +Reproduce in a throwaway session bus (never against the desktop's keyring; the runtime directory +must be short, as it holds a Unix socket): + +```sh +T=$(mktemp -d /tmp/ss.XXXX); mkdir -p $T/data $T/run; chmod 700 $T/run +XDG_DATA_HOME=$T/data XDG_RUNTIME_DIR=$T/run PDFS_SECRET_SERVICE_RESTART_TEST=1 \ + PDFS_RESTART_PROVIDER='echo -n pw | gnome-keyring-daemon --replace --unlock --components=secrets >/dev/null' \ + dbus-run-session -- sh -c 'echo -n pw | gnome-keyring-daemon --unlock --components=secrets >/dev/null; sleep 1; + cargo test -p pdfs-core real_secret_service -- --ignored' +``` + +For oo7, start `oo7-daemon --login` with the password on stdin instead, and restart it with +`--login --replace` in the background. + +## Reproduce without a Proton account or desktop keyring + +The oo7 fixture uses fake tokens, a temporary disk keyring and a private peer-to-peer D-Bus +connection. It asserts the observed 0.7.0 behavior, including the incorrect empty locked lookup; +passing means that behavior was reproduced. It does not simulate Proton refresh or a full boot. + +From the pdfs repository, choose an unused temporary checkout path: + +```sh +git clone --depth 1 --branch 0.7.0 https://github.com/linux-credentials/oo7.git /tmp/pdfs-oo7-repro +git -C /tmp/pdfs-oo7-repro apply "$PWD/docs/repro/oo7-0.7.0-issue78.patch" +cargo test --manifest-path /tmp/pdfs-oo7-repro/Cargo.toml \ + -p oo7-daemon --lib --features test-util issue78 -- --nocapture +``` + +Observed output: + +```text +oo7 0.7.0: locked session is on disk but SearchItems returns (0, 0) +oo7 0.7.0: all three binary SetSecret updates survived reopening the disk keyring +test item::tests::issue78_locked_lookup_hides_saved_tokens ... ok +``` + +The pdfs side is covered by `cargo test -p pdfs-core auth::tests`, specifically +`a_temporarily_missing_session_does_not_discard_a_rotation` and +`retrying_a_missing_session_never_recreates_it_or_overwrites_a_new_login`. + +Remaining live validation: reproduce on a Fedora 45 GNOME session across logout/shutdown, +correlating oo7 lock/write errors with pdfs refresh and retry logs, and checking the running +daemon's package version rather than just the CLI version. The investigation did not use a real +Proton account, alter a desktop keyring, or post to either GitHub issue. diff --git a/docs/LAUNCHER_AND_TRAY.md b/docs/LAUNCHER_AND_TRAY.md index 4022f34..af31c3c 100644 --- a/docs/LAUNCHER_AND_TRAY.md +++ b/docs/LAUNCHER_AND_TRAY.md @@ -69,9 +69,10 @@ windowrulev2 = center, class:^(pdfs-prompt)$ bind = SUPER, space, exec, pdfs-prompt --fzf ``` -To make `--dmenu` or `--fzf` the default without changing your shortcut, set `prompt.mode` in -`config.json` (see [Configuration → `prompt`](CONFIGURATION.md#prompt)). `--gtk` always opens the -built-in window. +To make `--dmenu` or `--fzf` the default without changing your shortcut, pick it in +**Preferences → Apps → Search Launcher**, or set `prompt.mode` in `config.json` (see +[Configuration → `prompt`](CONFIGURATION.md#prompt)). `--gtk` always opens the built-in window. +Which program opens a file you pick is set under **Preferences → Apps → Open Files With**. ## Tray diff --git a/docs/PHOTOS.md b/docs/PHOTOS.md index 80ee746..80be461 100644 --- a/docs/PHOTOS.md +++ b/docs/PHOTOS.md @@ -55,6 +55,7 @@ server learns which areas you look at. 2. In the app, open the **Upload** menu on the Photos page and choose **Import from Google Photos…**. Select all the `.zip` files at once, because a photo and its details are often in different parts of the export. +3. Choose **Scan** to see what an import would do without uploading anything, then **Import**. From a terminal: @@ -63,8 +64,22 @@ pdfs import-google-photos --dry-run ~/Downloads/takeout-*.zip # show what woul pdfs import-google-photos --wait ~/Downloads/takeout-*.zip ``` -The import recreates your albums and takes capture dates and locations from the export. Photos -already in your library are skipped, so an interrupted import can simply be run again. +What the import takes from the export: + +- **Albums** are recreated. A photo that is in several albums is uploaded once and added to each. +- **Dates** come from the export's details, else from a date in the file name, else from the + photo's EXIF. +- **Places** come from the export, so imported photos appear under [Places](#places). +- **Favorites** are kept. +- **Live photos and motion photos** stay one photo: the video is filed with its still. + +What it leaves out: photos in Google's trash, and files from other Google services (Drive, +YouTube) that are in the same export. A `.zip` that cannot be read is left out and named in the +report; the rest still imports. + +Photos already in your library are skipped, so an interrupted import can simply be run again. +If the import was cut short, for example by a restart, the Import page offers **Resume**, and +`pdfs import-google-photos --resume` does the same without naming the archives again. `pdfs import-status` shows how far an import is, and `pdfs cancel-import` stops it. ## Fixing photos filed under the wrong date diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 4ca340e..3dd278b 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -63,7 +63,8 @@ them along. | Add someone else's photo to your album | Blocked | Needs `copyPhoto` in the SDK | | Photo name search | Not started | Photos search by date and filters only | | Automatic photo upload from a folder | Not started | | -| Faster Google Photos import | Not started | Uploads run one at a time. Trash folders of locales missing from `TRASH_FOLDERS` import as photos; motion-photo `.MP` parts are skipped | +| Faster Google Photos import | Not started | Uploads run one at a time. Trash folders of locales missing from `TRASH_FOLDERS` import as photos | +| More of a Google Photos export | Not started | `.tgz` exports and extracted folders; shared albums told apart from own ones; album descriptions and covers; photo descriptions | | Content-addressed block cache | Idea | Store blocks by hash to deduplicate identical content | ## Performance and correctness diff --git a/docs/SDK-0.12.0.md b/docs/SDK-0.12.0.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/SYNCED_FOLDERS.md b/docs/SYNCED_FOLDERS.md index 65a33ee..9513fec 100644 --- a/docs/SYNCED_FOLDERS.md +++ b/docs/SYNCED_FOLDERS.md @@ -100,10 +100,39 @@ apply together: !important.log ``` -2. The `ignore_patterns` list in `config.json`, for every synced folder (see - [Configuration](CONFIGURATION.md#ignore_patterns)). When you have not set it, it skips - `.git/`, `.hg/`, `.svn/`, `node_modules/`, `target/`, `.venv/`, `__pycache__/`, `*~`, - `*.swp`, `*.tmp`, `.DS_Store` and `Thumbs.db`. +2. The patterns for every synced folder: **Preferences → Sync → Excluded Files** in the app, or + `ignore_patterns` in `config.json` (see [Configuration](CONFIGURATION.md#ignore_patterns)). + Built in are version control (`.git/`, `.hg/`, `.svn/`), dependency and build folders + (`node_modules/`, `target/`, `.venv/`, `__pycache__/`), and what editors, downloads and other + systems leave behind (`*~`, `*.swp`, `*.tmp`, `*.part`, `*.crdownload`, `*.kate-swp`, office + lock files, `.DS_Store`, `Thumbs.db`, `desktop.ini`, `.directory`, trash folders, `.nfs*` and + `.fuse_hidden*`). Each built-in pattern can be turned off on its own. + +In the app, open the **Sync** page, then **Folders**. Under the folder list, **Excluded Files** +shows how many patterns apply to all synced folders and how many each folder has of its own: + +- **Add Pattern** asks for a pattern and where it applies: all synced folders, or one of them. +- **All Synced Folders** opens Preferences → Sync, with every pattern and the built-in ones. +- A folder opens the editor for its own rules, which **Excluded Files…** in the folder's menu + opens too. It checks every line as you type, and **Check a path** tells you whether a file is + synced and which pattern decides. If the file changed meanwhile, for example on another + computer, it asks before replacing that version. + +From a terminal: + +```bash +pdfs sync ignore list # the patterns for every synced folder +pdfs sync ignore add '*.db' # leave out files ending in .db +pdfs sync ignore remove '*.db' +pdfs sync ignore reset # back to the built-in patterns +pdfs sync ignore add 'cache/' --folder ~/Projects # only in ~/Projects/.pdfsignore +pdfs sync ignore list --folder ~/Projects +pdfs sync ignore check ~/Projects/app/node_modules +``` + +The rules apply to folders kept on this computer. Files you save into `~/ProtonDrive` itself or into +an online-only folder are always uploaded, except download and editor scratch files, which wait for +their final name. Rules are read again on every sync pass. Excluding never deletes anything: a file that becomes excluded is no longer synced, and its copy on Proton Drive stays. diff --git a/docs/USER_GUIDE.md b/docs/USER_GUIDE.md index dad46f7..e756eef 100644 --- a/docs/USER_GUIDE.md +++ b/docs/USER_GUIDE.md @@ -53,6 +53,8 @@ The app uses these words, and so do these pages: | Page | Settings | |---|---| | **General** | Start on login · Proton Drive folder location · upload and download limits · Proton theme, street map for Places, tray icon · language · check for updates | +| **Sync** | Files and folders every synced folder leaves out · what happens to identical conflict copies | +| **Apps** | Which program opens which kind of file · how the search launcher opens | | **Storage** | Cache size limit · clear the cache · files kept available offline | The account menu shows the storage you use and holds **Sign Out…**. Settings that are not in the diff --git a/docs/repro/oo7-0.7.0-issue78.patch b/docs/repro/oo7-0.7.0-issue78.patch new file mode 100644 index 0000000..8c6d290 --- /dev/null +++ b/docs/repro/oo7-0.7.0-issue78.patch @@ -0,0 +1,53 @@ +diff --git a/server/src/item/tests.rs b/server/src/item/tests.rs +index 99c25dc..2ba705d 100644 +--- a/server/src/item/tests.rs ++++ b/server/src/item/tests.rs +@@ -5,6 +5,48 @@ use tokio_stream::StreamExt; + + use crate::{service::PrompterType, tests::TestServiceSetup}; + ++// Investigation for proton-drive-linux #78. Fake tokens and a private p2p bus; ++// no desktop keyring or Proton account is used. ++#[tokio::test] ++async fn issue78_locked_lookup_hides_saved_tokens() -> Result<(), Box> { ++ let setup = ++ TestServiceSetup::with_login_keyring(oo7::Secret::from("test-password-long-enough")) ++ .await?; ++ let collection = setup.default_collection().await?; ++ let attributes = [("service", "proton-drive-linux"), ("username", "session")]; ++ let secret = setup.create_dbus_secret(oo7::Secret::blob(b"fake-refresh-r1"))?; ++ collection ++ .create_item("pdfs fake session", &attributes, &secret, false, None) ++ .await?; ++ ++ let (unlocked, locked) = setup.service_api.search_items(&attributes).await?; ++ assert_eq!((unlocked.len(), locked.len()), (1, 0)); ++ setup.lock_collection(collection).await?; ++ assert!(collection.is_locked().await?); ++ ++ let (unlocked, locked) = setup.service_api.search_items(&attributes).await?; ++ // This is the problematic reply, not the Secret Service behavior expected ++ // by keyring-store: the saved item is neither unlocked nor reported locked. ++ assert_eq!((unlocked.len(), locked.len()), (0, 0)); ++ assert_eq!(setup.login_keyring().await?.items().await?.len(), 1); ++ eprintln!("oo7 0.7.0: locked session is on disk but SearchItems returns (0, 0)"); ++ ++ setup.unlock_collection(collection).await?; ++ let (unlocked, locked) = setup.service_api.search_items(&attributes).await?; ++ assert_eq!((unlocked.len(), locked.len()), (1, 0)); ++ ++ // Exercise the binary SetSecret path used by pdfs's keyring-store. Reopen ++ // the disk keyring after each update, rather than trusting a memory read. ++ for refresh in [b"fake-refresh-r2", b"fake-refresh-r3", b"fake-refresh-r4"] { ++ let secret = setup.create_dbus_secret(oo7::Secret::blob(refresh))?; ++ unlocked[0].set_secret(&secret).await?; ++ let items = setup.login_keyring().await?.items().await?; ++ assert_eq!(items[0].secret().as_bytes(), refresh); ++ } ++ eprintln!("oo7 0.7.0: all three binary SetSecret updates survived reopening the disk keyring"); ++ Ok(()) ++} ++ + #[tokio::test] + async fn label_property() -> Result<(), Box> { + let setup = TestServiceSetup::plain_session(true).await?; diff --git a/images/computers.png b/images/computers.png index e098b44..32f9d44 100644 Binary files a/images/computers.png and b/images/computers.png differ diff --git a/images/conflicts.png b/images/conflicts.png index 8e75e38..33242fc 100644 Binary files a/images/conflicts.png and b/images/conflicts.png differ diff --git a/images/files.png b/images/files.png index 9079805..f5e08a5 100644 Binary files a/images/files.png and b/images/files.png differ diff --git a/images/login.png b/images/login.png index ec40c13..666e263 100644 Binary files a/images/login.png and b/images/login.png differ diff --git a/images/photos.png b/images/photos.png index 8d1b417..b66d01f 100644 Binary files a/images/photos.png and b/images/photos.png differ diff --git a/images/photos_map.png b/images/photos_map.png index dc0f057..7eccfd8 100644 Binary files a/images/photos_map.png and b/images/photos_map.png differ diff --git a/images/prompt_fzf.png b/images/prompt_fzf.png index 47e6116..b1627ed 100644 Binary files a/images/prompt_fzf.png and b/images/prompt_fzf.png differ diff --git a/images/prompt_gtk.png b/images/prompt_gtk.png index eb7d81a..8fcf837 100644 Binary files a/images/prompt_gtk.png and b/images/prompt_gtk.png differ diff --git a/images/sync.png b/images/sync.png index fd225f4..bcf7480 100644 Binary files a/images/sync.png and b/images/sync.png differ diff --git a/packaging/PKGBUILD b/packaging/PKGBUILD index d36f144..d78e9ca 100644 --- a/packaging/PKGBUILD +++ b/packaging/PKGBUILD @@ -9,7 +9,7 @@ # Build + install: cd packaging && makepkg -fi pkgname=proton-drive-linux -pkgver=3.7.0 +pkgver=3.8.0 pkgrel=1 pkgdesc="Unofficial Proton Drive client for Linux: files-on-demand FUSE mount, CLI, GTK4 app + tray" arch=('x86_64') diff --git a/packaging/aur/proton-drive-for-linux-bin/.SRCINFO b/packaging/aur/proton-drive-for-linux-bin/.SRCINFO index 951bdcb..6ad78ba 100644 --- a/packaging/aur/proton-drive-for-linux-bin/.SRCINFO +++ b/packaging/aur/proton-drive-for-linux-bin/.SRCINFO @@ -31,16 +31,6 @@ pkgbase = proton-drive-for-linux-bin options = !strip options = !debug source = proton-drive-linux-3.7.0-x86_64.tar.gz::https://github.com/narrrl/proton-drive-linux/releases/download/v3.7.0/proton-drive-linux-3.7.0-x86_64.tar.gz - source = io.narl.proton-drive-linux-3.7.0.desktop::https://raw.githubusercontent.com/narrrl/proton-drive-linux/v3.7.0/packaging/io.narl.proton-drive-linux.desktop - source = io.narl.proton-drive-linux-tray-3.7.0.desktop::https://raw.githubusercontent.com/narrrl/proton-drive-linux/v3.7.0/packaging/io.narl.proton-drive-linux-tray.desktop - source = io.narl.proton-drive-linux-3.7.0.svg::https://raw.githubusercontent.com/narrrl/proton-drive-linux/v3.7.0/packaging/io.narl.proton-drive-linux.svg - source = proton-drive-3.7.0.service::https://raw.githubusercontent.com/narrrl/proton-drive-linux/v3.7.0/packaging/proton-drive.service - source = LICENSE-3.7.0::https://raw.githubusercontent.com/narrrl/proton-drive-linux/v3.7.0/LICENSE - sha256sums = 3f0d16c19b10b30e3f181e3c7fb439f0a8c38980e8ab7a4a96ae6596b349787c - sha256sums = d190d6771fff0b975271fb62fd3fb0fa8bbf17d7278c10853c18557855b0c123 - sha256sums = 1d95e250370220e3bb6aead6ce34d1d6cc759f42c4803b75f1667e78ff47c6c0 - sha256sums = e598a93c2715c3a9fdc7fb937902083d4a1135e8fbdfe6fe7e37f0383ebbd828 - sha256sums = 44c25dca6e759c46fca50b58099aa6194abe78293dee07d971bbbce0ff3ca58f - sha256sums = 02c419e7f9af08dda720e4f852f3e57cf5a69e192605440f750e9d244fa2817c + sha256sums = SKIP pkgname = proton-drive-for-linux-bin diff --git a/packaging/aur/proton-drive-for-linux-bin/PKGBUILD b/packaging/aur/proton-drive-for-linux-bin/PKGBUILD index a261940..9da76af 100644 --- a/packaging/aur/proton-drive-for-linux-bin/PKGBUILD +++ b/packaging/aur/proton-drive-for-linux-bin/PKGBUILD @@ -25,54 +25,37 @@ options=('!strip' '!debug') # Stops the running daemon before removal, so the mount stops mirroring deletes. install=proton-drive.install _tag="v$pkgver" -_raw="https://raw.githubusercontent.com/narrrl/proton-drive-linux/$_tag" -# The release tarball holds the four binaries only, so the desktop integration -# files come straight from the tagged tree. Every one carries $pkgver in its -# name: makepkg reuses a cached file of the same name, so an unversioned name -# keeps the old copy after upstream changes it and fails the checksum. -source=("proton-drive-linux-$pkgver-x86_64.tar.gz::$_repo/releases/download/$_tag/proton-drive-linux-$pkgver-x86_64.tar.gz" - "io.narl.proton-drive-linux-$pkgver.desktop::$_raw/packaging/io.narl.proton-drive-linux.desktop" - "io.narl.proton-drive-linux-tray-$pkgver.desktop::$_raw/packaging/io.narl.proton-drive-linux-tray.desktop" - "io.narl.proton-drive-linux-$pkgver.svg::$_raw/packaging/io.narl.proton-drive-linux.svg" - "proton-drive-$pkgver.service::$_raw/packaging/proton-drive.service" - "LICENSE-$pkgver::$_raw/LICENSE") -sha256sums=('3f0d16c19b10b30e3f181e3c7fb439f0a8c38980e8ab7a4a96ae6596b349787c' - 'd190d6771fff0b975271fb62fd3fb0fa8bbf17d7278c10853c18557855b0c123' - '1d95e250370220e3bb6aead6ce34d1d6cc759f42c4803b75f1667e78ff47c6c0' - 'e598a93c2715c3a9fdc7fb937902083d4a1135e8fbdfe6fe7e37f0383ebbd828' - '44c25dca6e759c46fca50b58099aa6194abe78293dee07d971bbbce0ff3ca58f' - '02c419e7f9af08dda720e4f852f3e57cf5a69e192605440f750e9d244fa2817c') +# The release tarball holds the binaries and everything installed around them +# (desktop files, icon, user service, translations, completions) in a /usr-like +# tree under one directory. +source=("proton-drive-linux-$pkgver-x86_64.tar.gz::$_repo/releases/download/$_tag/proton-drive-linux-$pkgver-x86_64.tar.gz") +sha256sums=('SKIP') package() { - install -Dm755 "$srcdir/pdfs" "$pkgdir/usr/bin/pdfs" - install -Dm755 "$srcdir/pdfs-tray" "$pkgdir/usr/bin/pdfs-tray" - install -Dm755 "$srcdir/pdfs-app" "$pkgdir/usr/bin/pdfs-app" - install -Dm755 "$srcdir/pdfs-prompt" "$pkgdir/usr/bin/pdfs-prompt" + cd "$srcdir/proton-drive-linux-$pkgver-x86_64" + local exe + for exe in pdfs pdfs-tray pdfs-app pdfs-prompt; do + install -Dm755 "bin/$exe" "$pkgdir/usr/bin/$exe" + done - install -Dm644 "$srcdir/io.narl.proton-drive-linux-$pkgver.desktop" \ + install -Dm644 share/applications/io.narl.proton-drive-linux.desktop \ "$pkgdir/usr/share/applications/io.narl.proton-drive-linux.desktop" - install -Dm644 "$srcdir/io.narl.proton-drive-linux-tray-$pkgver.desktop" \ + install -Dm644 etc/xdg/autostart/io.narl.proton-drive-linux-tray.desktop \ "$pkgdir/etc/xdg/autostart/io.narl.proton-drive-linux-tray.desktop" - install -Dm644 "$srcdir/io.narl.proton-drive-linux-$pkgver.svg" \ + install -Dm644 share/icons/hicolor/scalable/apps/io.narl.proton-drive-linux.svg \ "$pkgdir/usr/share/icons/hicolor/scalable/apps/io.narl.proton-drive-linux.svg" # systemd user unit for the auto-mount daemon; enabled per user, not by pacman. - install -Dm644 "$srcdir/proton-drive-$pkgver.service" \ + install -Dm644 lib/systemd/user/proton-drive.service \ "$pkgdir/usr/lib/systemd/user/proton-drive.service" - # Translations ship precompiled in the release tarball. + # Translations and shell completion for bash, zsh and fish. install -dm755 "$pkgdir/usr/share" - cp -r --no-preserve=ownership "$srcdir/locale" "$pkgdir/usr/share/locale" + cp -r --no-preserve=ownership share/locale share/bash-completion share/zsh share/fish \ + "$pkgdir/usr/share/" - # Shell completion for bash, zsh and fish, written by the binary itself. - local share="$pkgdir/usr/share" - install -d "$share/bash-completion/completions" "$share/zsh/site-functions" \ - "$share/fish/vendor_completions.d" - "$srcdir/pdfs" completions bash > "$share/bash-completion/completions/pdfs" - "$srcdir/pdfs" completions zsh > "$share/zsh/site-functions/_pdfs" - "$srcdir/pdfs" completions fish > "$share/fish/vendor_completions.d/pdfs.fish" - - install -Dm644 "$srcdir/LICENSE-$pkgver" "$pkgdir/usr/share/licenses/$pkgname/LICENSE" + install -Dm644 share/licenses/proton-drive-linux/LICENSE \ + "$pkgdir/usr/share/licenses/$pkgname/LICENSE" # pacman brings new releases, so the app never checks for them itself. install -Dm644 /dev/null "$pkgdir/usr/share/proton-drive-linux/managed-updates" diff --git a/packaging/proton-drive-linux.spec b/packaging/proton-drive-linux.spec index cda09e1..d383a70 100644 --- a/packaging/proton-drive-linux.spec +++ b/packaging/proton-drive-linux.spec @@ -10,7 +10,7 @@ # Match Arch PKGBUILD `!lto` — LTO has broken some GTK/Rust links in practice. %global _lto_cflags %{nil} -%{!?version: %global version 3.7.0} +%{!?version: %global version 3.8.0} %global forge https://github.com/narrrl/proton-drive-linux # Files come from the checkout, or from the unpacked tag archive. %global src %{?git_dir}%{!?git_dir:.} diff --git a/packaging/tarball/build.sh b/packaging/tarball/build.sh new file mode 100755 index 0000000..e58c5f3 --- /dev/null +++ b/packaging/tarball/build.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# Lay out the release tarball: built binaries, desktop files, icon, user +# service, translations, shell completions and install.sh, under one top-level +# directory with a /usr-like tree, then pack it. +# +# usage: packaging/tarball/build.sh +# e.g. packaging/tarball/build.sh target/release 3.8.0 dist +# Writes /proton-drive-linux--x86_64.tar.gz and prints +# its path. +set -euo pipefail + +if [ $# -ne 3 ]; then + echo "usage: $0 " >&2 + exit 2 +fi +bin_dir="$1" +version="$2" +out="$3" +repo="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" + +name="proton-drive-linux-$version-x86_64" +stage="$(mktemp -d)" +trap 'rm -rf "$stage"' EXIT +root="$stage/$name" + +for exe in pdfs pdfs-app pdfs-tray pdfs-prompt; do + install -Dm755 "$bin_dir/$exe" "$root/bin/$exe" +done +install -Dm644 "$repo/packaging/io.narl.proton-drive-linux.desktop" \ + "$root/share/applications/io.narl.proton-drive-linux.desktop" +install -Dm644 "$repo/packaging/io.narl.proton-drive-linux-tray.desktop" \ + "$root/etc/xdg/autostart/io.narl.proton-drive-linux-tray.desktop" +install -Dm644 "$repo/packaging/io.narl.proton-drive-linux.svg" \ + "$root/share/icons/hicolor/scalable/apps/io.narl.proton-drive-linux.svg" +install -Dm644 "$repo/packaging/proton-drive.service" \ + "$root/lib/systemd/user/proton-drive.service" +install -Dm644 "$repo/LICENSE" "$root/share/licenses/proton-drive-linux/LICENSE" +"$repo/po/build.sh" "$root/share/locale" +"$repo/packaging/completions.sh" "$root/bin/pdfs" "$root/share" +install -Dm755 "$repo/packaging/tarball/install.sh" "$root/install.sh" + +mkdir -p "$out" +tarball="$(cd "$out" && pwd)/$name.tar.gz" +tar -czf "$tarball" -C "$stage" --owner=0 --group=0 "$name" +echo "$tarball" diff --git a/packaging/tarball/install.sh b/packaging/tarball/install.sh new file mode 100755 index 0000000..df33c46 --- /dev/null +++ b/packaging/tarball/install.sh @@ -0,0 +1,194 @@ +#!/usr/bin/env bash +# Install Proton Drive for Linux from this release tarball into your home +# directory, or remove it again. Needs no root, so it works on distributions +# where the system is read-only, as long as GTK 4, libadwaita, WebKitGTK 6 and +# FUSE 3 are on the system. +set -Eeuo pipefail + +usage() { + cat >&2 <<'EOF' +usage: ./install.sh [--prefix DIR] [--no-service] [--force] + ./install.sh --uninstall [--prefix DIR] [--no-service] + +Installs the app, the tray, the search launcher and the `pdfs` command into +~/.local (bin/, share/), the background service into ~/.config/systemd/user and +the tray's login entry into ~/.config/autostart. Run it again from a newer +tarball to update; the running service restarts on the new version. + + --prefix DIR install under DIR instead of ~/.local + --uninstall remove what an earlier run installed; your settings, cache + and the files in Proton Drive stay + --no-service leave systemd alone (no reload, restart or stop) + --force install although libraries are missing or a package + already installs Proton Drive for Linux +EOF + exit 2 +} + +prefix="" +uninstall=0 +service=1 +force=0 +while [[ $# -gt 0 ]]; do + case "$1" in + --prefix) [[ $# -ge 2 ]] || usage; prefix="$2"; shift ;; + --prefix=*) prefix="${1#*=}" ;; + --uninstall) uninstall=1 ;; + --no-service) service=0 ;; + --force) force=1 ;; + -h | --help) usage ;; + *) echo "unknown option: $1" >&2; usage ;; + esac + shift +done +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# The copy kept for --uninstall knows where it was installed. +if [[ -z "$prefix" && -f "$here/installed-files" ]]; then + prefix="$(dirname "$(dirname "$here")")" +fi +prefix="${prefix:-$HOME/.local}" +[[ "$prefix" == /* ]] || prefix="$PWD/$prefix" +prefix="${prefix%/}" + +config_home="${XDG_CONFIG_HOME:-$HOME/.config}" +unit_dir="$config_home/systemd/user" +autostart_dir="$config_home/autostart" +data_dir="$prefix/share/proton-drive-linux" +manifest="$data_dir/installed-files" +unit=proton-drive.service + +say() { printf '%s\n' "$*"; } +warn() { printf 'warning: %s\n' "$*" >&2; } +die() { printf 'error: %s\n' "$*" >&2; exit 1; } + +systemctl_user() { + [[ $service -eq 1 ]] || return 0 + command -v systemctl >/dev/null || return 0 + systemctl --user "$@" 2>/dev/null || true +} + +# Remove every file listed in the manifest `$1` that is not in the list `$2` +# (one path per line), then the directories that leaves empty. +remove_listed() { + local list="$1" keep="${2:-}" path + [[ -f "$list" ]] || return 0 + while IFS= read -r path; do + [[ -n "$path" ]] || continue + if [[ -n "$keep" ]] && grep -Fxq -- "$path" <<<"$keep"; then + continue + fi + rm -f -- "$path" + rmdir -p --ignore-fail-on-non-empty -- "$(dirname -- "$path")" 2>/dev/null || true + done <"$list" +} + +if [[ $uninstall -eq 1 ]]; then + [[ -f "$manifest" ]] || die "nothing installed under $prefix (no $manifest)" + # Stopping unmounts My files; queued changes stay and upload on the next start. + systemctl_user disable --now "$unit" + remove_listed "$manifest" + rm -f -- "$manifest" "$data_dir/install.sh" + rmdir --ignore-fail-on-non-empty -- "$data_dir" 2>/dev/null || true + systemctl_user daemon-reload + say "Removed Proton Drive for Linux from $prefix." + say "Your settings, cache and sign-in stay; see the uninstall section of docs/INSTALL.md to remove them too." + exit 0 +fi + +[[ -x "$here/bin/pdfs" ]] || die "run install.sh from the unpacked tarball (no bin/pdfs next to it)" + +# A package and this install would each bring a service unit, and the one in +# ~/.config wins: the two would run different versions against one database. +if [[ $force -eq 0 && ( -e /usr/bin/pdfs || -e /usr/lib/systemd/user/$unit ) ]]; then + die "a package already installs Proton Drive for Linux (/usr/bin/pdfs). Remove it first, or keep using it." +fi + +# Missing libraries would only show when the app does not start. +missing="$(for exe in pdfs pdfs-app pdfs-tray pdfs-prompt; do + ldd "$here/bin/$exe" 2>&1 | grep -E 'not found' || true +done | sort -u)" +if [[ -n "$missing" ]]; then + printf '%s\n' "$missing" | sed 's/^[[:space:]]*/ /' >&2 + if grep -q 'GLIBC_' <<<"$missing"; then + warn "these binaries need glibc 2.39 or newer (Ubuntu 24.04, Debian 13, Fedora 40)" + fi + cat >&2 <<'EOF' +Install the missing libraries, then run this again: + Fedora, Fedora Atomic gtk4 libadwaita webkitgtk6.0 fuse3 + (on Silverblue and its siblings, layer the COPR package + instead: see docs/INSTALL.md) + Debian, Ubuntu libgtk-4-1 libadwaita-1-0 libwebkitgtk-6.0-4 fuse3 + Arch Linux gtk4 libadwaita webkitgtk-6.0 fuse3 + openSUSE libgtk-4-1 libadwaita-1-0 libwebkitgtk-6_0-4 fuse3 +EOF + [[ $force -eq 1 ]] || die "missing libraries (--force installs anyway)" +fi + +fusermount="$(command -v fusermount3 || true)" +if [[ -z "$fusermount" ]]; then + warn "fusermount3 is missing; install fuse3, or My files cannot be mounted" +elif [[ ! -u "$fusermount" ]]; then + warn "$fusermount is not setuid root; My files may not mount" +fi + +# Where each file of the tarball goes, as "sourcetarget" lines. +targets() { + local file rel + while IFS= read -r -d '' file; do + rel="${file#"$here"/}" + case "$rel" in + bin/* | share/*) printf '%s\t%s\n' "$rel" "$prefix/$rel" ;; + lib/systemd/user/*) printf '%s\t%s\n' "$rel" "$unit_dir/${rel##*/}" ;; + etc/xdg/autostart/*) printf '%s\t%s\n' "$rel" "$autostart_dir/${rel##*/}" ;; + esac + done < <(find "$here/bin" "$here/share" "$here/lib" "$here/etc" -type f -print0 | sort -z) +} + +# A desktop entry and a unit file take a quoted path; double quotes and +# backslashes inside it are escaped. +quoted() { + local path="${1//\\/\\\\}" + printf '"%s"' "${path//\"/\\\"}" +} + +new_files="" +while IFS=$'\t' read -r rel target; do + mode=644 + [[ "$rel" == bin/* ]] && mode=755 + install -Dm"$mode" -- "$here/$rel" "$target" + case "$rel" in + share/applications/*.desktop | etc/xdg/autostart/*.desktop) + exe="$(sed -n 's/^Exec=\([^ ]*\).*/\1/p' "$target" | head -n1)" + sed -i "s|^Exec=$exe|Exec=$(quoted "$prefix/bin/$exe" | sed 's/[&|]/\\&/g')|" "$target" + ;; + lib/systemd/user/*) + sed -i "s|^ExecStart=/usr/bin/pdfs|ExecStart=$(quoted "$prefix/bin/pdfs" | sed 's/[&|]/\\&/g')|" "$target" + ;; + esac + new_files+="$target"$'\n' +done < <(targets) + +# Drop what an older version installed and this one does not. +remove_listed "$manifest" "$new_files" +mkdir -p -- "$data_dir" +printf '%s' "$new_files" >"$manifest" +install -m755 -- "$here/install.sh" "$data_dir/install.sh" + +if command -v gtk-update-icon-cache >/dev/null && + [[ -f "$prefix/share/icons/hicolor/index.theme" ]]; then + gtk-update-icon-cache -q -t "$prefix/share/icons/hicolor" 2>/dev/null || true +fi + +systemctl_user daemon-reload +if [[ $service -eq 1 ]] && command -v systemctl >/dev/null && + systemctl --user is-active -q "$unit" 2>/dev/null; then + systemctl --user restart "$unit" && say "Restarted the running service on the new version." +fi + +say "Installed Proton Drive for Linux $("$prefix/bin/pdfs" --version 2>/dev/null | awk '{print $NF}') into $prefix." +say "Open \"Proton Drive for Linux\" from your applications menu and sign in; the app starts the service." +case ":$PATH:" in + *":$prefix/bin:"*) ;; + *) say "To use \`pdfs\` in a terminal, add $prefix/bin to your PATH." ;; +esac +say "To update, run install.sh from a newer tarball. To remove: $data_dir/install.sh --uninstall" diff --git a/packaging/tarball/test.sh b/packaging/tarball/test.sh new file mode 100755 index 0000000..d25847f --- /dev/null +++ b/packaging/tarball/test.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash +# Check a release tarball: install it into a throwaway home, check that every +# file landed with its paths rewritten and that the programs run, update it in +# place, then uninstall and check nothing is left. Touches no real home and no +# systemd (--no-service). +# +# usage: packaging/tarball/test.sh +set -euo pipefail + +if [ $# -ne 1 ]; then + echo "usage: $0 " >&2 + exit 2 +fi +tarball="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT + +fail() { echo "FAIL: $*" >&2; exit 1; } + +tar -xzf "$tarball" -C "$work" +src="$(find "$work" -mindepth 1 -maxdepth 1 -type d -name 'proton-drive-linux-*')" +[ -x "$src/install.sh" ] || fail "no install.sh in the tarball" + +export HOME="$work/home" XDG_CONFIG_HOME="$work/home/.config" +mkdir -p "$HOME" +prefix="$HOME/.local" +"$src/install.sh" --no-service --force >"$work/install.log" 2>&1 || + { cat "$work/install.log" >&2; fail "install.sh failed"; } + +for exe in pdfs pdfs-app pdfs-tray pdfs-prompt; do + [ -x "$prefix/bin/$exe" ] || fail "$exe was not installed" +done +"$prefix/bin/pdfs" --version >/dev/null || fail "pdfs does not run" +app="$prefix/share/applications/io.narl.proton-drive-linux.desktop" +tray="$XDG_CONFIG_HOME/autostart/io.narl.proton-drive-linux-tray.desktop" +unit="$XDG_CONFIG_HOME/systemd/user/proton-drive.service" +grep -qx "Exec=\"$prefix/bin/pdfs-app\"" "$app" || fail "the app's Exec= was not rewritten: $(grep ^Exec= "$app")" +grep -qx "Exec=\"$prefix/bin/pdfs-tray\"" "$tray" || fail "the tray's Exec= was not rewritten: $(grep ^Exec= "$tray")" +grep -qx "ExecStart=\"$prefix/bin/pdfs\" daemon" "$unit" || fail "ExecStart= was not rewritten: $(grep ^ExecStart= "$unit")" +[ -f "$prefix/share/locale/de/LC_MESSAGES/pdfs.mo" ] || fail "no translations" +[ -f "$prefix/share/icons/hicolor/scalable/apps/io.narl.proton-drive-linux.svg" ] || fail "no icon" +[ -f "$prefix/share/bash-completion/completions/pdfs" ] || fail "no bash completion" +[ ! -e "$prefix/share/proton-drive-linux/managed-updates" ] || fail "the update check must stay on offer" +uninstaller="$prefix/share/proton-drive-linux/install.sh" +[ -x "$uninstaller" ] || fail "install.sh was not kept for --uninstall" + +# An update from the same tarball leaves the same files. +before="$(cd "$work/home" && find . -type f | sort)" +"$src/install.sh" --no-service --force >"$work/update.log" 2>&1 || + { cat "$work/update.log" >&2; fail "updating failed"; } +after="$(cd "$work/home" && find . -type f | sort)" +[ "$before" = "$after" ] || fail "an update changed the set of files: $(diff <(echo "$before") <(echo "$after"))" + +# A file an older version installed and this one does not is removed. +stale="$prefix/share/locale/xx/LC_MESSAGES/pdfs.mo" +mkdir -p "$(dirname "$stale")" && : >"$stale" +echo "$stale" >>"$prefix/share/proton-drive-linux/installed-files" +"$src/install.sh" --no-service --force >/dev/null 2>&1 +[ ! -e "$stale" ] || fail "a file the new version does not ship was left behind" + +# Settings are not the installer's: uninstalling keeps them. +mkdir -p "$XDG_CONFIG_HOME/proton-drive-linux" +echo '{}' >"$XDG_CONFIG_HOME/proton-drive-linux/config.json" +"$uninstaller" --uninstall --no-service >"$work/uninstall.log" 2>&1 || + { cat "$work/uninstall.log" >&2; fail "uninstall failed"; } +left="$(cd "$work/home" && find . -type f | sort)" +[ "$left" = "./.config/proton-drive-linux/config.json" ] || fail "uninstall left files behind: $left" + +echo "tarball OK: $(basename "$tarball")" diff --git a/po/POTFILES.in b/po/POTFILES.in index fd55328..3b066df 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -13,9 +13,12 @@ crates/pdfs-gui/src/app/pages/photo_viewer.rs crates/pdfs-gui/src/app/pages/photos.rs crates/pdfs-gui/src/app/pages/places.rs crates/pdfs-gui/src/app/pages/places_map.rs +crates/pdfs-gui/src/app/pages/preferences_apps.rs +crates/pdfs-gui/src/app/pages/preferences_sync.rs crates/pdfs-gui/src/app/pages/shared.rs crates/pdfs-gui/src/app/pages/shared_by_me.rs crates/pdfs-gui/src/app/pages/status.rs +crates/pdfs-gui/src/app/pages/sync_exclusions.rs crates/pdfs-gui/src/app/pages/takeout.rs crates/pdfs-gui/src/app/pages/trash.rs crates/pdfs-gui/src/app/pages/verify.rs @@ -23,7 +26,9 @@ crates/pdfs-gui/src/app/update.rs crates/pdfs-gui/src/app/widgets/compat.rs crates/pdfs-gui/src/app/widgets/details.rs crates/pdfs-gui/src/app/widgets/file_list.rs +crates/pdfs-gui/src/app/widgets/ignore_dialog.rs crates/pdfs-gui/src/app/widgets/menu.rs +crates/pdfs-gui/src/app/widgets/player.rs crates/pdfs-gui/src/app/widgets/share_dialog.rs crates/pdfs-gui/src/app/widgets/thumbnails.rs crates/pdfs-gui/src/app/widgets/versions_dialog.rs diff --git a/po/de.po b/po/de.po index e72ce10..1d9fbac 100644 --- a/po/de.po +++ b/po/de.po @@ -5,7 +5,7 @@ msgid "" msgstr "" "Project-Id-Version: proton-drive-linux 2.1.0\n" "Report-Msgid-Bugs-To: https://github.com/narrrl/proton-drive-linux/issues\n" -"POT-Creation-Date: 2026-11-14 02:47+0100\n" +"POT-Creation-Date: 2026-11-16 00:38+0100\n" "PO-Revision-Date: 2026-09-23 12:00+0200\n" "Last-Translator: Proton Drive for Linux contributors\n" "Language-Team: German\n" @@ -31,7 +31,7 @@ msgstr[1] "vor {n} Min." #. Translators: strftime format for the time a photo was taken, shown on its tile, such as "14:05". #: crates/pdfs-gui/src/app/dates.rs:44 #: crates/pdfs-gui/src/app/pages/activity.rs:840 -#: crates/pdfs-gui/src/app/pages/photos.rs:3534 +#: crates/pdfs-gui/src/app/pages/photos.rs:3715 msgid "%H:%M" msgstr "%H:%M" @@ -57,87 +57,95 @@ msgstr "%-d. %b %Y" msgid "%A, %B %-d, %Y, %H:%M" msgstr "%A, %-d. %B %Y, %H:%M" -#: crates/pdfs-gui/src/app/main.rs:865 +#: crates/pdfs-gui/src/app/main.rs:934 #: crates/pdfs-gui/src/app/pages/activity_text.rs:543 -#: crates/pdfs-gui/src/app/pages/browser.rs:458 -#: crates/pdfs-gui/src/app/pages/browser.rs:838 +#: crates/pdfs-gui/src/app/pages/browser.rs:473 +#: crates/pdfs-gui/src/app/pages/browser.rs:880 #: crates/pdfs-gui/src/app/pages/browser.rs:1254 -#: crates/pdfs-gui/src/app/pages/browser.rs:2213 -#: crates/pdfs-gui/src/app/pages/browser.rs:2292 -#: crates/pdfs-gui/src/app/pages/locations.rs:1721 +#: crates/pdfs-gui/src/app/pages/browser.rs:2239 +#: crates/pdfs-gui/src/app/pages/browser.rs:2318 +#: crates/pdfs-gui/src/app/pages/locations.rs:1743 #: crates/pdfs-gui/src/app/pages/shared_by_me.rs:252 #: crates/pdfs-gui/src/app/pages/trash.rs:191 crates/pdfs-gui/src/prompt.rs:171 msgid "My files" msgstr "Meine Dateien" -#: crates/pdfs-gui/src/app/main.rs:866 crates/pdfs-gui/src/app/main.rs:1536 +#: crates/pdfs-gui/src/app/main.rs:935 crates/pdfs-gui/src/app/main.rs:1616 #: crates/pdfs-gui/src/app/pages/albums.rs:636 -#: crates/pdfs-gui/src/app/pages/photos.rs:898 -#: crates/pdfs-gui/src/app/pages/places.rs:439 +#: crates/pdfs-gui/src/app/pages/photos.rs:929 +#: crates/pdfs-gui/src/app/pages/places.rs:473 msgid "Photos" msgstr "Fotos" -#: crates/pdfs-gui/src/app/main.rs:869 +#: crates/pdfs-gui/src/app/main.rs:938 #: crates/pdfs-gui/src/app/pages/shared.rs:168 -#: crates/pdfs-gui/src/app/pages/shared.rs:484 +#: crates/pdfs-gui/src/app/pages/shared.rs:493 msgid "Shared with me" msgstr "Mit mir geteilt" -#: crates/pdfs-gui/src/app/main.rs:874 +#: crates/pdfs-gui/src/app/main.rs:943 #: crates/pdfs-gui/src/app/pages/shared_by_me.rs:120 msgid "Shared by me" msgstr "Von mir geteilt" -#: crates/pdfs-gui/src/app/main.rs:877 +#: crates/pdfs-gui/src/app/main.rs:946 #: crates/pdfs-gui/src/app/pages/devices.rs:140 -#: crates/pdfs-gui/src/app/pages/devices.rs:493 +#: crates/pdfs-gui/src/app/pages/devices.rs:505 msgid "Computers" msgstr "Computer" -#: crates/pdfs-gui/src/app/main.rs:878 +#: crates/pdfs-gui/src/app/main.rs:947 #: crates/pdfs-gui/src/app/pages/trash.rs:101 msgid "Trash" msgstr "Papierkorb" #. Translators: sidebar entry for the page listing this computer's synced folders. -#: crates/pdfs-gui/src/app/main.rs:880 +#: crates/pdfs-gui/src/app/main.rs:949 +#: crates/pdfs-gui/src/app/pages/preferences_sync.rs:66 msgid "Sync" msgstr "Synchronisierung" #. Translators: a failed action's toast. {what} says what failed, such as #. "Couldn't rename"; {detail} is the reason, usually untranslated text #. from the mount service. -#: crates/pdfs-gui/src/app/main.rs:1230 -#: crates/pdfs-gui/src/app/pages/takeout.rs:714 +#: crates/pdfs-gui/src/app/main.rs:1299 +#: crates/pdfs-gui/src/app/pages/photo_viewer.rs:915 +#: crates/pdfs-gui/src/app/pages/preferences_apps.rs:284 +#: crates/pdfs-gui/src/app/pages/preferences_sync.rs:175 +#: crates/pdfs-gui/src/app/pages/preferences_sync.rs:212 +#: crates/pdfs-gui/src/app/pages/sync_exclusions.rs:401 +#: crates/pdfs-gui/src/app/pages/takeout.rs:891 +#: crates/pdfs-gui/src/app/widgets/ignore_dialog.rs:182 +#: crates/pdfs-gui/src/app/widgets/ignore_dialog.rs:289 #, rust-format msgid "{what}: {detail}" msgstr "{what}: {detail}" -#: crates/pdfs-gui/src/app/main.rs:1253 +#: crates/pdfs-gui/src/app/main.rs:1322 msgid "You're offline" msgstr "Keine Internetverbindung" -#: crates/pdfs-gui/src/app/main.rs:1254 +#: crates/pdfs-gui/src/app/main.rs:1323 msgid "That's not there any more" msgstr "Das ist nicht mehr vorhanden" -#: crates/pdfs-gui/src/app/main.rs:1255 +#: crates/pdfs-gui/src/app/main.rs:1324 msgid "You don't have access to that" msgstr "Kein Zugriff darauf" -#: crates/pdfs-gui/src/app/main.rs:1256 +#: crates/pdfs-gui/src/app/main.rs:1325 msgid "Something changed this first" msgstr "Etwas anderes hat dies zuerst geändert" -#: crates/pdfs-gui/src/app/main.rs:1257 +#: crates/pdfs-gui/src/app/main.rs:1326 msgid "Your Proton Drive is full" msgstr "Das Proton Drive ist voll" -#: crates/pdfs-gui/src/app/main.rs:1344 +#: crates/pdfs-gui/src/app/main.rs:1413 msgid "Proton Drive for Linux" msgstr "Proton Drive for Linux" -#: crates/pdfs-gui/src/app/main.rs:1352 +#: crates/pdfs-gui/src/app/main.rs:1421 msgid "" "Files-on-demand Proton Drive for the Linux desktop.\n" "\n" @@ -149,23 +157,23 @@ msgstr "" "Inoffizieller Client – nicht mit Proton AG verbunden, von Proton AG " "unterstützt oder befürwortet." -#: crates/pdfs-gui/src/app/main.rs:1357 +#: crates/pdfs-gui/src/app/main.rs:1426 msgid "Source Code" msgstr "Quellcode" -#: crates/pdfs-gui/src/app/main.rs:1361 +#: crates/pdfs-gui/src/app/main.rs:1430 msgid "Proton Terms of Service" msgstr "Nutzungsbedingungen von Proton" -#: crates/pdfs-gui/src/app/main.rs:1365 +#: crates/pdfs-gui/src/app/main.rs:1434 msgid "Proton Privacy Policy" msgstr "Datenschutzerklärung von Proton" -#: crates/pdfs-gui/src/app/main.rs:1370 +#: crates/pdfs-gui/src/app/main.rs:1439 msgid "Disclaimer" msgstr "Haftungsausschluss" -#: crates/pdfs-gui/src/app/main.rs:1374 +#: crates/pdfs-gui/src/app/main.rs:1443 msgid "" "This is an unofficial, community-built client. It is not affiliated with, " "endorsed by, or supported by Proton AG.\n" @@ -190,7 +198,7 @@ msgstr "" "bereitgestellt. Daten, deren Verlust nicht hinnehmbar ist, sollten " "zusätzlich gesichert werden." -#: crates/pdfs-gui/src/app/main.rs:1382 +#: crates/pdfs-gui/src/app/main.rs:1451 msgid "" "Places are named with data from GeoNames, licensed under GeoNames, lizenziert unter CC BY 4.0." -#: crates/pdfs-gui/src/app/main.rs:1390 +#: crates/pdfs-gui/src/app/main.rs:1459 msgid "" "The places map is drawn from Natural Earth land outlines and country borders, which are in the " @@ -210,7 +218,7 @@ msgstr "" "href=\"https://www.naturalearthdata.com/\">Natural Earth gezeichnet, die " "gemeinfrei sind." -#: crates/pdfs-gui/src/app/main.rs:1398 +#: crates/pdfs-gui/src/app/main.rs:1467 msgid "" "The street map, when turned on, comes from OpenFreeMap in the GeoNames, licensed under GeoNames, licensed under CC BY 4.0." -#: crates/pdfs-gui/src/app/main.rs:1390 +#: crates/pdfs-gui/src/app/main.rs:1459 msgid "" "The places map is drawn from Natural Earth land outlines and country borders, which are in the " @@ -207,7 +215,7 @@ msgstr "" "\">Natural Earth land outlines and country borders, which are in the " "public domain." -#: crates/pdfs-gui/src/app/main.rs:1398 +#: crates/pdfs-gui/src/app/main.rs:1467 msgid "" "The street map, when turned on, comes from OpenFreeMap in the GeoNames, licensed under GeoNames, con licencia CC BY 4.0." -#: crates/pdfs-gui/src/app/main.rs:1390 +#: crates/pdfs-gui/src/app/main.rs:1459 msgid "" "The places map is drawn from Natural Earth land outlines and country borders, which are in the " @@ -208,7 +216,7 @@ msgstr "" "fronteras de países de Natural " "Earth, que son de dominio público." -#: crates/pdfs-gui/src/app/main.rs:1398 +#: crates/pdfs-gui/src/app/main.rs:1467 msgid "" "The street map, when turned on, comes from OpenFreeMap in the GeoNames, licensed under GeoNames, sous licence CC BY 4.0." -#: crates/pdfs-gui/src/app/main.rs:1390 +#: crates/pdfs-gui/src/app/main.rs:1459 msgid "" "The places map is drawn from Natural Earth land outlines and country borders, which are in the " @@ -209,7 +217,7 @@ msgstr "" "frontières des pays de Natural " "Earth, qui sont dans le domaine public." -#: crates/pdfs-gui/src/app/main.rs:1398 +#: crates/pdfs-gui/src/app/main.rs:1467 msgid "" "The street map, when turned on, comes from OpenFreeMap in the GeoNames, licensed under GeoNames, con licenza CC BY 4.0." -#: crates/pdfs-gui/src/app/main.rs:1390 +#: crates/pdfs-gui/src/app/main.rs:1459 msgid "" "The places map is drawn from Natural Earth land outlines and country borders, which are in the " @@ -207,7 +215,7 @@ msgstr "" "confini nazionali di Natural " "Earth, che sono di pubblico dominio." -#: crates/pdfs-gui/src/app/main.rs:1398 +#: crates/pdfs-gui/src/app/main.rs:1467 msgid "" "The street map, when turned on, comes from OpenFreeMap in the GeoNames, licensed under GeoNames, gelicentieerd onder CC BY 4.0." -#: crates/pdfs-gui/src/app/main.rs:1390 +#: crates/pdfs-gui/src/app/main.rs:1459 msgid "" "The places map is drawn from Natural Earth land outlines and country borders, which are in the " @@ -208,7 +216,7 @@ msgstr "" "href=\"https://www.naturalearthdata.com/\">Natural Earth, die in het " "publieke domein vallen." -#: crates/pdfs-gui/src/app/main.rs:1398 +#: crates/pdfs-gui/src/app/main.rs:1467 msgid "" "The street map, when turned on, comes from OpenFreeMap in the \n" "Language-Team: LANGUAGE \n" @@ -34,7 +34,7 @@ msgstr[1] "" #. Translators: strftime format for the time a photo was taken, shown on its tile, such as "14:05". #: crates/pdfs-gui/src/app/dates.rs:44 #: crates/pdfs-gui/src/app/pages/activity.rs:840 -#: crates/pdfs-gui/src/app/pages/photos.rs:3534 +#: crates/pdfs-gui/src/app/pages/photos.rs:3715 msgid "%H:%M" msgstr "" @@ -60,87 +60,95 @@ msgstr "" msgid "%A, %B %-d, %Y, %H:%M" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:865 +#: crates/pdfs-gui/src/app/main.rs:934 #: crates/pdfs-gui/src/app/pages/activity_text.rs:543 -#: crates/pdfs-gui/src/app/pages/browser.rs:458 -#: crates/pdfs-gui/src/app/pages/browser.rs:838 +#: crates/pdfs-gui/src/app/pages/browser.rs:473 +#: crates/pdfs-gui/src/app/pages/browser.rs:880 #: crates/pdfs-gui/src/app/pages/browser.rs:1254 -#: crates/pdfs-gui/src/app/pages/browser.rs:2213 -#: crates/pdfs-gui/src/app/pages/browser.rs:2292 -#: crates/pdfs-gui/src/app/pages/locations.rs:1721 +#: crates/pdfs-gui/src/app/pages/browser.rs:2239 +#: crates/pdfs-gui/src/app/pages/browser.rs:2318 +#: crates/pdfs-gui/src/app/pages/locations.rs:1743 #: crates/pdfs-gui/src/app/pages/shared_by_me.rs:252 #: crates/pdfs-gui/src/app/pages/trash.rs:191 crates/pdfs-gui/src/prompt.rs:171 msgid "My files" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:866 crates/pdfs-gui/src/app/main.rs:1536 +#: crates/pdfs-gui/src/app/main.rs:935 crates/pdfs-gui/src/app/main.rs:1616 #: crates/pdfs-gui/src/app/pages/albums.rs:636 -#: crates/pdfs-gui/src/app/pages/photos.rs:898 -#: crates/pdfs-gui/src/app/pages/places.rs:439 +#: crates/pdfs-gui/src/app/pages/photos.rs:929 +#: crates/pdfs-gui/src/app/pages/places.rs:473 msgid "Photos" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:869 +#: crates/pdfs-gui/src/app/main.rs:938 #: crates/pdfs-gui/src/app/pages/shared.rs:168 -#: crates/pdfs-gui/src/app/pages/shared.rs:484 +#: crates/pdfs-gui/src/app/pages/shared.rs:493 msgid "Shared with me" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:874 +#: crates/pdfs-gui/src/app/main.rs:943 #: crates/pdfs-gui/src/app/pages/shared_by_me.rs:120 msgid "Shared by me" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:877 +#: crates/pdfs-gui/src/app/main.rs:946 #: crates/pdfs-gui/src/app/pages/devices.rs:140 -#: crates/pdfs-gui/src/app/pages/devices.rs:493 +#: crates/pdfs-gui/src/app/pages/devices.rs:505 msgid "Computers" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:878 +#: crates/pdfs-gui/src/app/main.rs:947 #: crates/pdfs-gui/src/app/pages/trash.rs:101 msgid "Trash" msgstr "" #. Translators: sidebar entry for the page listing this computer's synced folders. -#: crates/pdfs-gui/src/app/main.rs:880 +#: crates/pdfs-gui/src/app/main.rs:949 +#: crates/pdfs-gui/src/app/pages/preferences_sync.rs:66 msgid "Sync" msgstr "" #. Translators: a failed action's toast. {what} says what failed, such as #. "Couldn't rename"; {detail} is the reason, usually untranslated text #. from the mount service. -#: crates/pdfs-gui/src/app/main.rs:1230 -#: crates/pdfs-gui/src/app/pages/takeout.rs:714 +#: crates/pdfs-gui/src/app/main.rs:1299 +#: crates/pdfs-gui/src/app/pages/photo_viewer.rs:915 +#: crates/pdfs-gui/src/app/pages/preferences_apps.rs:284 +#: crates/pdfs-gui/src/app/pages/preferences_sync.rs:175 +#: crates/pdfs-gui/src/app/pages/preferences_sync.rs:212 +#: crates/pdfs-gui/src/app/pages/sync_exclusions.rs:401 +#: crates/pdfs-gui/src/app/pages/takeout.rs:891 +#: crates/pdfs-gui/src/app/widgets/ignore_dialog.rs:182 +#: crates/pdfs-gui/src/app/widgets/ignore_dialog.rs:289 #, rust-format msgid "{what}: {detail}" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1253 +#: crates/pdfs-gui/src/app/main.rs:1322 msgid "You're offline" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1254 +#: crates/pdfs-gui/src/app/main.rs:1323 msgid "That's not there any more" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1255 +#: crates/pdfs-gui/src/app/main.rs:1324 msgid "You don't have access to that" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1256 +#: crates/pdfs-gui/src/app/main.rs:1325 msgid "Something changed this first" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1257 +#: crates/pdfs-gui/src/app/main.rs:1326 msgid "Your Proton Drive is full" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1344 +#: crates/pdfs-gui/src/app/main.rs:1413 msgid "Proton Drive for Linux" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1352 +#: crates/pdfs-gui/src/app/main.rs:1421 msgid "" "Files-on-demand Proton Drive for the Linux desktop.\n" "\n" @@ -148,23 +156,23 @@ msgid "" "AG." msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1357 +#: crates/pdfs-gui/src/app/main.rs:1426 msgid "Source Code" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1361 +#: crates/pdfs-gui/src/app/main.rs:1430 msgid "Proton Terms of Service" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1365 +#: crates/pdfs-gui/src/app/main.rs:1434 msgid "Proton Privacy Policy" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1370 +#: crates/pdfs-gui/src/app/main.rs:1439 msgid "Disclaimer" msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1374 +#: crates/pdfs-gui/src/app/main.rs:1443 msgid "" "This is an unofficial, community-built client. It is not affiliated with, " "endorsed by, or supported by Proton AG.\n" @@ -177,21 +185,21 @@ msgid "" "of any data you cannot afford to lose." msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1382 +#: crates/pdfs-gui/src/app/main.rs:1451 msgid "" "Places are named with data from GeoNames, licensed under CC BY 4.0." msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1390 +#: crates/pdfs-gui/src/app/main.rs:1459 msgid "" "The places map is drawn from Natural Earth land outlines and country borders, which are in the " "public domain." msgstr "" -#: crates/pdfs-gui/src/app/main.rs:1398 +#: crates/pdfs-gui/src/app/main.rs:1467 msgid "" "The street map, when turned on, comes from OpenFreeMap in the GeoNames, licensed under GeoNames udostępnionych na licencji CC BY 4.0." -#: crates/pdfs-gui/src/app/main.rs:1390 +#: crates/pdfs-gui/src/app/main.rs:1459 msgid "" "The places map is drawn from Natural Earth land outlines and country borders, which are in the " @@ -211,7 +219,7 @@ msgstr "" "www.naturalearthdata.com/\">Natural Earth, które należą do domeny " "publicznej." -#: crates/pdfs-gui/src/app/main.rs:1398 +#: crates/pdfs-gui/src/app/main.rs:1467 msgid "" "The street map, when turned on, comes from OpenFreeMap in the GeoNames, licensed under GeoNames, licenciados sob CC BY 4.0." -#: crates/pdfs-gui/src/app/main.rs:1390 +#: crates/pdfs-gui/src/app/main.rs:1459 msgid "" "The places map is drawn from Natural Earth land outlines and country borders, which are in the " @@ -207,7 +215,7 @@ msgstr "" "fronteiras de países do Natural Earth, que são de domínio público." -#: crates/pdfs-gui/src/app/main.rs:1398 +#: crates/pdfs-gui/src/app/main.rs:1467 msgid "" "The street map, when turned on, comes from OpenFreeMap in the &2 <<'EOF' +usage: scripts/screenshots.sh [OPTION ...] + +Builds the app, starts it against a stand-in daemon that serves an invented +account (crates/pdfs-core/examples/demo_daemon.rs), and captures every page in +a headless Wayland compositor. No Proton account, no FUSE mount, nothing from +your own files: HOME, every XDG directory, the session bus and the keyring +are throwaway ones under a temporary directory. + +Options: + --out DIR where the PNGs go (default: images/) + --only LIST capture only these, comma-separated (default: all of + login,files,photos,photos_map,computers,sync,conflicts, + prompt_gtk,prompt_fzf). preferences_sync and + preferences_apps exist too, for pull requests only + --website DIR afterwards, copy them into the website checkout at DIR + with its scripts/sync-screenshots.sh + --settle SECS how long a page gets to load before its capture (default 3) + --debug use a debug build instead of a release build + --keep keep the temporary directory, for a look at the logs + +Needs cage, grim, wlr-randr, ImageMagick, dbus-run-session and +gnome-keyring-daemon; prompt_gtk types its query with wtype when there is one, +and prompt_fzf needs fzf and alacritty. Demo photos come from picsum.photos +(Unsplash licence) on the first run and are cached in +~/.cache/pdfs-screenshots. +EOF + exit 2 +} + +repo="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +all=login,files,photos,photos_map,computers,sync,conflicts,prompt_gtk,prompt_fzf +# Not in the README, so not taken by default. +extra=preferences_sync,preferences_apps + +# The capture itself, run by cage inside the throwaway session (see below). +if [[ "${1:-}" == --capture ]]; then + shots=",$SHOTS," + wants() { [[ "$shots" == *",$1,"* ]]; } + output() { wlr-randr --output "$(wlr-randr | awk 'NR==1 {print $1}')" --custom-mode "$1"; } + shoot() { + sleep "$SETTLE" + grim "$OUT/$1.png" + echo "captured $1" + } + # Cut the compositor's black background off a window smaller than it. + trim() { + magick "$OUT/$1.png" -bordercolor black -border 1 -trim +repage "$OUT/$1.png" + } + quit() { + kill "$@" 2>/dev/null || true + wait "$@" 2>/dev/null || true + } + + output 1836x1006 + if wants login; then + "$BIN/pdfs-app" & + app=$! + shoot login + quit "$app" + fi + "$BIN/demo_daemon" seed-keyring + "$BIN/pdfs-app" & + app=$! + sleep "$SETTLE" + for shot in files:browser photos:gallery photos_map:places-map computers:devices \ + sync:sync-folders conflicts:activity \ + preferences_sync:preferences-sync preferences_apps:preferences-apps; do + name=${shot%%:*} + wants "$name" || continue + "$BIN/pdfs-app" --page "${shot#*:}" + shoot "$name" + done + quit "$app" + + if wants prompt_gtk; then + output 860x723 + "$BIN/pdfs-prompt" --gtk & + prompt=$! + sleep 1 + # The window has no --query; type one, as a person would. + # The first key only sets up the keymap. + command -v wtype >/dev/null && wtype -k Shift_L -s 200 report + shoot prompt_gtk + quit "$prompt" + trim prompt_gtk + fi + if wants prompt_fzf; then + if command -v fzf >/dev/null && command -v alacritty >/dev/null; then + output 800x600 + alacritty -o font.size=11 -e "$BIN/pdfs-prompt" --fzf --inner --query report & + term=$! + shoot prompt_fzf + quit "$term" + trim prompt_fzf + else + echo "skipped prompt_fzf: needs fzf and alacritty" >&2 + fi + fi + exit 0 +fi + +# Inside the private session bus: an unlocked, empty keyring, the demo daemon, +# then the compositor running the capture above. +if [[ "${1:-}" == --session ]]; then + # A password it never asks for again: an empty one leaves no keyring. + printf demo | gnome-keyring-daemon --unlock --components=secrets >/dev/null + "$BIN/demo_daemon" serve "$PHOTOS" 2>"$PDFS_DEMO_ROOT/demo_daemon.log" & + daemon=$! + trap 'kill "$daemon" 2>/dev/null || true' EXIT + for _ in $(seq 50); do + [[ -S "$XDG_STATE_HOME/proton-drive-linux/control.sock" ]] && break + sleep 0.1 + done + WLR_BACKENDS=headless WLR_LIBINPUT_NO_DEVICES=1 \ + cage -- "$repo/scripts/screenshots.sh" --capture 2>"$PDFS_DEMO_ROOT/cage.log" + exit 0 +fi + +out="$repo/images" +shots=$all +website= +settle=3 +profile=release +keep= +while (($#)); do + case "$1" in + --out) out=$(realpath -m -- "${2:?}"); shift ;; + --only) shots=${2:?}; shift ;; + --website) website=$(realpath -- "${2:?}"); shift ;; + --settle) settle=${2:?}; shift ;; + --debug) profile=debug ;; + --keep) keep=1 ;; + -h | --help) usage ;; + *) echo "unknown option: $1" >&2; usage ;; + esac + shift +done +for shot in ${shots//,/ }; do + [[ ",$all,$extra," == *",$shot,"* ]] || { echo "no screenshot called $shot" >&2; exit 2; } +done +for tool in cage grim wlr-randr magick dbus-run-session gnome-keyring-daemon cargo curl; do + command -v "$tool" >/dev/null || { echo "$tool is required" >&2; exit 2; } +done + +# Demo photos, fetched once. Fixed picsum ids keep every run the same; a +# trailing -p marks a portrait one. +photos="${XDG_CACHE_HOME:-$HOME/.cache}/pdfs-screenshots" +mkdir -p "$photos" +for id in 10 11 13 15 16 17 28 29 37 42 43 49 54 57 58 76 82 83 84 96 101 103 110 \ + 111 116 119 122 128 129 135 142 145 152 164 167 174 176 180 184 188 192 200 \ + 210 218 219 225 231 237; do + size=600/400 file="$photos/$id.jpg" + if ((id % 7 == 0)); then size=400/600 file="$photos/$id-p.jpg"; fi + [[ -s "$file" ]] || curl -fsSL "https://picsum.photos/id/$id/$size.jpg" -o "$file" || + { rm -f "$file"; echo "could not fetch demo photo $id; skipped" >&2; } +done + +cargo_flags=(--manifest-path "$repo/Cargo.toml" --locked) +[[ "$profile" == release ]] && cargo_flags+=(--release) +cargo build "${cargo_flags[@]}" -p pdfs-gui --bin pdfs-app --bin pdfs-prompt +cargo build "${cargo_flags[@]}" -p pdfs-core --example demo_daemon +target=$(cargo metadata --manifest-path "$repo/Cargo.toml" --format-version 1 --no-deps | + python3 -c 'import json, sys; print(json.load(sys.stdin)["target_directory"])') +bin="$(mktemp -d)" +ln -s "$target/$profile/pdfs-app" "$target/$profile/pdfs-prompt" \ + "$target/$profile/examples/demo_daemon" "$bin/" + +root=$(mktemp -d -t pdfs-screenshots.XXXXXX) +cleanup() { + rm -rf "$bin" + if [[ -n "$keep" ]]; then echo "kept $root" >&2; else rm -rf "$root"; fi +} +trap cleanup EXIT +mkdir -p "$root"/{home,config,state,cache,data} "$root/run" "$out" +chmod 700 "$root/run" +# The user's own fonts, so the shots look like the app does here. +mkdir -p "$root/data/fonts" +[[ -d "$HOME/.local/share/fonts" ]] && cp -rs "$HOME/.local/share/fonts/." "$root/data/fonts/" +mkdir -p "$root/config/proton-drive-linux" +cat >"$root/config/proton-drive-linux/config.json" <<'EOF' +{ + "proton_theme": true, + "tray_hidden": true, + "update_check": false, + "online_map": false, + "files_view": { "list": false }, + "ignore_patterns": [".git/", "node_modules/", "target/", "*~", "*.swp", ".DS_Store", + "*.iso", "build/", "*.sqlite-wal"], + "open_with": { + "rules": [ + { "match": ["@text", "*.md"], "command": ["nvim"], "terminal": true }, + { "match": ["@image"], "command": ["loupe"] } + ] + } +} +EOF + +# Only a close button, as GNOME shows it. +mkdir -p "$root/config/gtk-4.0" +printf '[Settings]\ngtk-decoration-layout=:close\n' >"$root/config/gtk-4.0/settings.ini" + +# The demo user's home, so paths read "Home / …" for them, not for you. A +# folder that does not exist takes no writes; everything goes to XDG_*. +home=/home/alex +[[ -e "$home" ]] && home="$root/home" + +outer_bus=${DBUS_SESSION_BUS_ADDRESS:-unset} +env -i \ + PATH="$PATH" LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 TZ="${TZ:-Europe/Berlin}" \ + HOME="$home" \ + XDG_CONFIG_HOME="$root/config" XDG_STATE_HOME="$root/state" \ + XDG_CACHE_HOME="$root/cache" XDG_DATA_HOME="$root/data" \ + XDG_RUNTIME_DIR="$root/run" \ + PDFS_DEMO_ROOT="$root" PDFS_DEMO_OUTER_BUS="$outer_bus" \ + GSETTINGS_BACKEND=memory ADW_DEBUG_COLOR_SCHEME=prefer-dark GTK_A11Y=none GDK_DEBUG=no-portals \ + BIN="$bin" PHOTOS="$photos" OUT="$out" SHOTS="$shots" SETTLE="$settle" \ + dbus-run-session -- "$repo/scripts/screenshots.sh" --session 2>"$root/session.log" + +for shot in ${shots//,/ }; do + [[ -s "$out/$shot.png" ]] || echo "missing: $out/$shot.png (logs: --keep)" >&2 +done +if [[ -n "$website" ]]; then + "$website/scripts/sync-screenshots.sh" "$repo" +fi