mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-19 18:05:04 +01:00
Compare commits
71
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb5ff8b8da | ||
|
|
c57d98799a | ||
|
|
7d9d5850a4 | ||
|
|
a69ebbdd10 | ||
|
|
ddf7335894 | ||
|
|
140bd2eee3 | ||
|
|
2462cb24bd | ||
|
|
a1dd2b5a9b | ||
|
|
e9d6d4a907 | ||
|
|
6fa91e83f1 | ||
|
|
f239dffbcc | ||
|
|
e011a0d89f | ||
|
|
f2c4c05f27 | ||
|
|
8598aff7dd | ||
|
|
38a8db0e3e | ||
|
|
db8c0cb382 | ||
|
|
a7baf2313c | ||
|
|
7e0d7d020e | ||
|
|
0df4b3c5f9 | ||
|
|
14b9e0fe64 | ||
|
|
64864562ef | ||
|
|
ce3bfa217d | ||
|
|
593df5c401 | ||
|
|
fd6b2d3e85 | ||
|
|
34902390f3 | ||
|
|
1dcf7c84ca | ||
|
|
a05772aa13 | ||
|
|
2f2fe96cdf | ||
|
|
2ec3b7ff62 | ||
|
|
38bd282d3a | ||
|
|
d773ba15d3 | ||
|
|
d93982baa9 | ||
|
|
3f5001b2d2 | ||
|
|
2308021aa2 | ||
|
|
602f1c9577 | ||
|
|
9132c7ebd5 | ||
|
|
94304ccc1a | ||
|
|
74cd6fd2a9 | ||
|
|
4b669168ee | ||
|
|
ba5b6a8bf2 | ||
|
|
3295eda1f0 | ||
|
|
41212efdf3 | ||
|
|
5f0b15fe70 | ||
|
|
033c8d3d71 | ||
|
|
5c184013f1 | ||
|
|
6be8a8f9bc | ||
|
|
7d445bb02c | ||
|
|
57acb0b849 | ||
|
|
68ff8c9e80 | ||
|
|
4552093c53 | ||
|
|
f2a9a0f64b | ||
|
|
e571f22d5d | ||
|
|
1c50797772 | ||
|
|
0183f17435 | ||
|
|
14a663cee9 | ||
|
|
3f45e89391 | ||
|
|
b49631e18f | ||
|
|
ad648ab5cc | ||
|
|
f25c2459a0 | ||
|
|
ce35a83a04 | ||
|
|
762d06765e | ||
|
|
2b95708260 | ||
|
|
c78bb529bc | ||
|
|
8d824af656 | ||
|
|
a0ab243be6 | ||
|
|
c77c548d7e | ||
|
|
2930b457d4 | ||
|
|
d9da0e2263 | ||
|
|
d4a14a17a8 | ||
|
|
5ca0272f15 | ||
|
|
800705ccb0 |
No files matched your search
@@ -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
|
||||
|
||||
@@ -102,14 +105,17 @@ jobs:
|
||||
- name: Simulation runs
|
||||
run: 'cargo test -p pdfs-fuse --lib --locked sim:: -- --ignored --test-threads=1'
|
||||
|
||||
# A hung step writes every daemon thread's stack next to the run's
|
||||
# state; without them a hang that does not replay cannot be told apart.
|
||||
- name: Keep the stacks of a hung run
|
||||
# Retain the operation trace and daemon logs for the failing seed, plus
|
||||
# every daemon thread's stack if the watchdog caught a hang.
|
||||
- name: Keep simulation failure diagnostics
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: simulation-stacks
|
||||
path: /tmp/pdfs-sim-*/stacks.txt
|
||||
name: simulation-diagnostics
|
||||
path: |
|
||||
/tmp/pdfs-sim-*/steps.log
|
||||
/tmp/pdfs-sim-*/daemon.log
|
||||
/tmp/pdfs-sim-*/stacks.txt
|
||||
if-no-files-found: ignore
|
||||
|
||||
# Keeps the release-profile dependencies cached on main, where the release
|
||||
|
||||
@@ -119,23 +119,22 @@ 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)
|
||||
run: |
|
||||
cargo vendor --locked dist/vendor > /dev/null
|
||||
tar -cJf proton-drive-linux-vendor-${{ env.VERSION }}.tar.xz -C dist vendor
|
||||
mkdir -p dist/.cargo
|
||||
(
|
||||
cd dist
|
||||
cargo vendor --locked --manifest-path ../Cargo.toml vendor > .cargo/config.toml
|
||||
)
|
||||
tar -cJf proton-drive-linux-vendor-${{ env.VERSION }}.tar.xz -C dist vendor .cargo/config.toml
|
||||
|
||||
- name: Package Debian package (.deb)
|
||||
run: |
|
||||
@@ -160,6 +159,7 @@ jobs:
|
||||
cp packaging/proton-drive.service deb/usr/lib/systemd/user/
|
||||
cp LICENSE deb/usr/share/licenses/proton-drive-linux/
|
||||
po/build.sh deb/usr/share/locale
|
||||
packaging/completions.sh target/release/pdfs deb/usr/share
|
||||
|
||||
# Stops the running daemon before removal, so the mount stops mirroring deletes
|
||||
install -m755 packaging/deb/prerm deb/DEBIAN/prerm
|
||||
|
||||
Generated
+164
-8
@@ -49,6 +49,7 @@ dependencies = [
|
||||
"cipher 0.5.2",
|
||||
"cpubits",
|
||||
"cpufeatures 0.3.1",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -77,6 +78,7 @@ dependencies = [
|
||||
"ctr 0.10.1",
|
||||
"ctutils",
|
||||
"ghash 0.6.0",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -754,6 +756,18 @@ dependencies = [
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_complete"
|
||||
version = "4.6.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "037e2a1a92236d0aff7e845093f64661d6df4c02c9fcc61a60e9e1d736fa392f"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"clap_lex",
|
||||
"is_executable",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.6.7"
|
||||
@@ -1102,6 +1116,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"
|
||||
@@ -1860,6 +1880,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2eecf2d5dc9b66b732b97707a0210906b1d30523eb773193ab777c0c84b3e8d5"
|
||||
dependencies = [
|
||||
"polyval 0.7.3",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2219,6 +2240,12 @@ version = "1.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
|
||||
|
||||
[[package]]
|
||||
name = "httpdate"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
|
||||
|
||||
[[package]]
|
||||
name = "hybrid-array"
|
||||
version = "0.4.15"
|
||||
@@ -2242,6 +2269,7 @@ dependencies = [
|
||||
"http",
|
||||
"http-body",
|
||||
"httparse",
|
||||
"httpdate",
|
||||
"itoa",
|
||||
"pin-project-lite",
|
||||
"smallvec",
|
||||
@@ -2551,6 +2579,15 @@ version = "2.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0"
|
||||
|
||||
[[package]]
|
||||
name = "is_executable"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82cb6a9f675da968c63b6208c641b9dca58fc0133ae53375736b1767b0cab8bd"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
@@ -2881,6 +2918,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"
|
||||
@@ -3302,6 +3365,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"
|
||||
@@ -3520,10 +3592,11 @@ checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4"
|
||||
|
||||
[[package]]
|
||||
name = "pdfs-cli"
|
||||
version = "3.7.0"
|
||||
version = "3.8.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"clap_complete",
|
||||
"pdfs-core",
|
||||
"pdfs-fuse",
|
||||
"rpassword",
|
||||
@@ -3536,10 +3609,13 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pdfs-core"
|
||||
version = "3.7.0"
|
||||
version = "3.8.1"
|
||||
dependencies = [
|
||||
"argon2",
|
||||
"async-trait",
|
||||
"base64 0.23.1",
|
||||
"chrono",
|
||||
"dbus-secret-service",
|
||||
"dbus-secret-service-keyring-store",
|
||||
"directories",
|
||||
"ignore",
|
||||
@@ -3554,6 +3630,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"sha1 0.11.0",
|
||||
"sha2 0.11.1",
|
||||
"shell-words",
|
||||
"thiserror 2.0.21",
|
||||
"tokio",
|
||||
"tracing",
|
||||
@@ -3563,32 +3640,42 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pdfs-fuse"
|
||||
version = "3.7.0"
|
||||
version = "3.8.1"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.23.1",
|
||||
"fuser",
|
||||
"futures",
|
||||
"getrandom 0.4.3",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper-util",
|
||||
"image",
|
||||
"libc",
|
||||
"lofty",
|
||||
"notify",
|
||||
"parking_lot 0.13.0",
|
||||
"pdfs-core",
|
||||
"pg-app",
|
||||
"pg-core",
|
||||
"proton-drive-rs",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha1 0.11.0",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"zeroize",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pdfs-gui"
|
||||
version = "3.7.0"
|
||||
version = "3.8.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-channel",
|
||||
"base64 0.23.1",
|
||||
"gettext-rs",
|
||||
"gtk4",
|
||||
"kamadak-exif",
|
||||
@@ -3596,6 +3683,7 @@ dependencies = [
|
||||
"libadwaita",
|
||||
"pangocairo",
|
||||
"pdfs-core",
|
||||
"pg-core",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -3620,6 +3708,65 @@ version = "2.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
|
||||
|
||||
[[package]]
|
||||
name = "pg-app"
|
||||
version = "0.2.0"
|
||||
source = "git+https://github.com/narrrl/proton-gallery.git?rev=72d83a29ad2836c698b18e2f5189a423c60c2399#72d83a29ad2836c698b18e2f5189a423c60c2399"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"hex",
|
||||
"pg-core",
|
||||
"pg-drive",
|
||||
"proton-drive-rs",
|
||||
"proton-sdk",
|
||||
"serde_json",
|
||||
"sha2 0.11.1",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pg-core"
|
||||
version = "0.2.0"
|
||||
source = "git+https://github.com/narrrl/proton-gallery.git?rev=72d83a29ad2836c698b18e2f5189a423c60c2399#72d83a29ad2836c698b18e2f5189a423c60c2399"
|
||||
dependencies = [
|
||||
"aes-gcm 0.11.1",
|
||||
"async-trait",
|
||||
"getrandom 0.4.3",
|
||||
"hex",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2 0.11.1",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pg-drive"
|
||||
version = "0.2.0"
|
||||
source = "git+https://github.com/narrrl/proton-gallery.git?rev=72d83a29ad2836c698b18e2f5189a423c60c2399#72d83a29ad2836c698b18e2f5189a423c60c2399"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"hex",
|
||||
"pg-core",
|
||||
"proton-drive-rs",
|
||||
"proton-sdk",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2 0.11.1",
|
||||
"thiserror 2.0.21",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pgp"
|
||||
version = "0.21.0"
|
||||
@@ -3757,6 +3904,7 @@ dependencies = [
|
||||
"cpubits",
|
||||
"cpufeatures 0.3.1",
|
||||
"universal-hash 0.6.1",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3825,13 +3973,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proton-drive-rs"
|
||||
version = "0.10.1"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8fa2a88130883010b32035e208aaa36fb275f6d7d78ed44a1069b1d33d24481"
|
||||
checksum = "d05ad4be849ce2104759631063dd06867fbe6b6f0e5c37a172a84b5a90203fa7"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.23.1",
|
||||
"bytes",
|
||||
"chrono",
|
||||
"futures",
|
||||
"getrandom 0.4.3",
|
||||
"hex",
|
||||
@@ -3852,9 +4001,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proton-sdk"
|
||||
version = "0.10.1"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af0bf8d50fa829d1a65b09c6855b606578d02734d4089874c971d1063c23c353"
|
||||
checksum = "80c381e9066c0bca3c32597df2e902eecaa4c6e05e202e70ec9d0ac2eec78bfe"
|
||||
dependencies = [
|
||||
"aes-gcm 0.11.1",
|
||||
"async-trait",
|
||||
@@ -4737,6 +4886,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"
|
||||
@@ -5116,6 +5271,7 @@ dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
"futures-util",
|
||||
"libc",
|
||||
"pin-project-lite",
|
||||
"tokio",
|
||||
|
||||
+18
-3
@@ -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.1"
|
||||
edition = "2024"
|
||||
license = "MIT"
|
||||
authors = ["Nils Pukropp <contact@narl.io>"]
|
||||
@@ -15,8 +15,11 @@ keywords = ["proton", "drive", "fuse", "sync", "linux"]
|
||||
categories = ["filesystem", "command-line-utilities"]
|
||||
|
||||
[workspace.dependencies]
|
||||
proton-sdk = { version = "0.10.1" }
|
||||
proton-drive-rs = { version = "0.10.1" }
|
||||
proton-sdk = { version = "0.12.1" }
|
||||
proton-drive-rs = { version = "0.12.1" }
|
||||
pg-app = { git = "https://github.com/narrrl/proton-gallery.git", rev = "72d83a29ad2836c698b18e2f5189a423c60c2399" }
|
||||
pg-core = { git = "https://github.com/narrrl/proton-gallery.git", rev = "72d83a29ad2836c698b18e2f5189a423c60c2399" }
|
||||
chrono = { version = "0.4", default-features = false, features = ["std"] }
|
||||
|
||||
# Internal crates.
|
||||
pdfs-core = { path = "crates/pdfs-core" }
|
||||
@@ -38,6 +41,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.
|
||||
@@ -49,13 +54,23 @@ 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.
|
||||
clap_complete = { version = "4.6", features = ["unstable-dynamic"] }
|
||||
rpassword = "7"
|
||||
async-trait = "0.1"
|
||||
futures = "0.3"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<div align="center">
|
||||
|
||||
<img src="images/logo.svg" alt="" width="112" height="112">
|
||||
<a href="images/logo.svg"><img src="images/logo.svg" alt="Proton Drive for Linux icon" width="128" height="128"></a>
|
||||
|
||||
# Proton Drive for Linux
|
||||
|
||||
@@ -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**
|
||||
|
||||
@@ -125,7 +126,7 @@ The documentation is in [`docs/`](docs/README.md) and on the
|
||||
| [Getting started](docs/GETTING_STARTED.md) | From installing to your first synced folder |
|
||||
| [User guide](docs/USER_GUIDE.md) | Everything the app, tray and launcher do |
|
||||
| [FAQ](docs/FAQ.md) | Short answers to common questions |
|
||||
| [Command-line reference](docs/CLI.md) | Every `pdfs` command and scripting with `--json` |
|
||||
| [Command-line reference](docs/CLI.md) | Every `pdfs` command, Tab completion and scripting with `--json` |
|
||||
| [Configuration](docs/CONFIGURATION.md) | `config.json`, file locations, environment variables, the service |
|
||||
| [Troubleshooting](docs/TROUBLESHOOTING.md) | Diagnosing problems and fixing common ones |
|
||||
| [Recovering on a new computer](docs/RECOVERY.md) | Getting your synced folders back after a reinstall or a lost machine |
|
||||
|
||||
@@ -21,6 +21,7 @@ anyhow.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
clap.workspace = true
|
||||
clap_complete.workspace = true
|
||||
rpassword.workspace = true
|
||||
serde_json.workspace = true
|
||||
zeroize.workspace = true
|
||||
@@ -0,0 +1,731 @@
|
||||
//! Shell completion for `pdfs`.
|
||||
//!
|
||||
//! Built on clap_complete's dynamic engine: the script a shell loads is a thin
|
||||
//! stub that calls `pdfs` back on every Tab with the words typed so far, and
|
||||
//! `pdfs` answers from its own clap definition. That lets an argument complete
|
||||
//! from the running service as well as from the command tree — `pdfs sync rm
|
||||
//! <Tab>` offers the synced-folder ids with their paths, `pdfs pin Doc<Tab>`
|
||||
//! walks My files.
|
||||
//!
|
||||
//! Every value that comes from the service is best effort. With no service, a
|
||||
//! slow one or an error, a completer offers nothing: a Tab that hangs or prints
|
||||
//! an error into the prompt is worse than one that does nothing.
|
||||
//!
|
||||
//! The engine sits behind clap_complete's `unstable-dynamic` feature, so all of
|
||||
//! this crate's use of it is kept to this module.
|
||||
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
use clap::CommandFactory;
|
||||
use clap_complete::engine::{CompletionCandidate, PathCompleter, ValueCompleter};
|
||||
use clap_complete::env::{Bash, CompleteEnv, Elvish, EnvCompleter, Fish, Powershell, Zsh};
|
||||
use pdfs_core::control::{
|
||||
AlbumInfo, BookmarkInfo, ConflictInfo, DeviceInfo, DirEntry, InvitationInfo, PendingOpInfo,
|
||||
PublicLinkInfo, Request, Response, RevisionInfo, ShareEntry, ShareEntryKind, SyncFolderInfo,
|
||||
};
|
||||
|
||||
/// The environment variable that turns a run of `pdfs` into a completion
|
||||
/// request. Not clap's generic `COMPLETE`: the same binary is the systemd
|
||||
/// daemon, and an unrelated `COMPLETE` in its environment must not stop it.
|
||||
pub const VAR: &str = "PDFS_COMPLETE";
|
||||
|
||||
/// How long a completer waits for the service. A Tab is interactive, so a
|
||||
/// listing the service cannot give soon is better skipped. Long enough for the
|
||||
/// listings that ask Proton (computers take over a second).
|
||||
const SERVICE_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
|
||||
/// How long a listing that asks Proton is reused. Every Tab is a new `pdfs`, so
|
||||
/// without it `pdfs devices rm <Tab>`, a letter and another Tab would wait for
|
||||
/// Proton twice. A uid that went away in the meantime only fails the command.
|
||||
const CACHE_TTL: Duration = Duration::from_secs(30);
|
||||
|
||||
/// Answer a completion request and exit, if this run is one. Must run before
|
||||
/// anything writes to stdout.
|
||||
pub fn handle_request() {
|
||||
CompleteEnv::with_factory(crate::Cli::command)
|
||||
.var(VAR)
|
||||
.complete();
|
||||
}
|
||||
|
||||
/// The shells `pdfs completions` writes a script for.
|
||||
#[derive(Clone, Copy, clap::ValueEnum)]
|
||||
pub enum ShellArg {
|
||||
Bash,
|
||||
Zsh,
|
||||
Fish,
|
||||
Elvish,
|
||||
Powershell,
|
||||
}
|
||||
|
||||
impl ShellArg {
|
||||
fn completer(self) -> &'static dyn EnvCompleter {
|
||||
match self {
|
||||
ShellArg::Bash => &Bash,
|
||||
ShellArg::Zsh => &Zsh,
|
||||
ShellArg::Fish => &Fish,
|
||||
ShellArg::Elvish => &Elvish,
|
||||
ShellArg::Powershell => &Powershell,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Write the script that registers `pdfs` completion with `shell`.
|
||||
///
|
||||
/// The script calls `pdfs` from `PATH`, not this binary's path, so one written
|
||||
/// at packaging time keeps working once installed.
|
||||
pub fn write_script(shell: ShellArg, out: &mut dyn std::io::Write) -> std::io::Result<()> {
|
||||
shell
|
||||
.completer()
|
||||
.write_registration(VAR, "pdfs", "pdfs", "pdfs", out)?;
|
||||
if let ShellArg::Zsh = shell {
|
||||
// Installed as `_pdfs` in site-functions, the script is autoloaded as
|
||||
// that function's body on the first Tab. The body above only defines
|
||||
// and registers the completer, so this line also runs it, or the first
|
||||
// Tab would complete nothing. Sourced, `funcstack` names no `_pdfs`.
|
||||
writeln!(
|
||||
out,
|
||||
"if [ \"$funcstack[1]\" = \"_pdfs\" ]; then\n _clap_dynamic_completer_pdfs \"$@\"\nfi"
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Talking to the service
|
||||
|
||||
/// One request to the service, or `None` if there is no answer worth showing.
|
||||
fn ask(request: Request) -> Option<Response> {
|
||||
let socket = pdfs_core::config::AppDirs::new().ok()?.control_socket();
|
||||
ask_at(&socket, &request)
|
||||
}
|
||||
|
||||
fn ask_at(socket: &Path, request: &Request) -> Option<Response> {
|
||||
match pdfs_core::control::send_with_timeout(socket, request, SERVICE_TIMEOUT) {
|
||||
Ok(Response::Error { .. }) | Err(_) => None,
|
||||
Ok(response) => Some(response),
|
||||
}
|
||||
}
|
||||
|
||||
/// [`ask`] for a listing that comes from Proton rather than the local
|
||||
/// database, reusing an answer younger than [`CACHE_TTL`]. `key` names the
|
||||
/// listing's cache file.
|
||||
fn ask_cached(key: &str, request: Request) -> Option<Response> {
|
||||
let file = cache_file(key);
|
||||
if let Some(hit) = file.as_deref().and_then(read_fresh) {
|
||||
return Some(hit);
|
||||
}
|
||||
let response = ask(request)?;
|
||||
if let Some(file) = &file {
|
||||
write_cache(file, &response);
|
||||
}
|
||||
Some(response)
|
||||
}
|
||||
|
||||
/// In `$XDG_RUNTIME_DIR`: private to the user, in memory and gone at logout,
|
||||
/// which suits names from the account. With no runtime dir, nothing is cached.
|
||||
fn cache_file(key: &str) -> Option<PathBuf> {
|
||||
let runtime = PathBuf::from(std::env::var_os("XDG_RUNTIME_DIR")?);
|
||||
Some(
|
||||
runtime
|
||||
.join("proton-drive-linux")
|
||||
.join(format!("completion-{key}.json")),
|
||||
)
|
||||
}
|
||||
|
||||
fn read_fresh(file: &Path) -> Option<Response> {
|
||||
let age = std::fs::metadata(file)
|
||||
.ok()?
|
||||
.modified()
|
||||
.ok()?
|
||||
.elapsed()
|
||||
.ok()?;
|
||||
if age > CACHE_TTL {
|
||||
return None;
|
||||
}
|
||||
serde_json::from_slice(&std::fs::read(file).ok()?).ok()
|
||||
}
|
||||
|
||||
/// Best effort: a cache that cannot be written only costs the next Tab a wait.
|
||||
fn write_cache(file: &Path, response: &Response) {
|
||||
use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt};
|
||||
|
||||
let (Some(dir), Ok(json)) = (file.parent(), serde_json::to_vec(response)) else {
|
||||
return;
|
||||
};
|
||||
let _ = std::fs::DirBuilder::new()
|
||||
.recursive(true)
|
||||
.mode(0o700)
|
||||
.create(dir);
|
||||
let staging = file.with_extension(format!("{}.tmp", std::process::id()));
|
||||
let written = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.mode(0o600)
|
||||
.open(&staging)
|
||||
.and_then(|mut f| std::io::Write::write_all(&mut f, &json));
|
||||
if written.is_err() || std::fs::rename(&staging, file).is_err() {
|
||||
let _ = std::fs::remove_file(&staging);
|
||||
}
|
||||
}
|
||||
|
||||
fn candidate(value: impl Into<OsString>, help: impl Into<String>) -> CompletionCandidate {
|
||||
let help = help.into();
|
||||
let candidate = CompletionCandidate::new(value);
|
||||
if help.is_empty() {
|
||||
candidate
|
||||
} else {
|
||||
candidate.help(Some(help.into()))
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Paths in Proton Drive
|
||||
|
||||
/// A path in the mount: an absolute path completes from the local filesystem
|
||||
/// (through the mount), anything else from the service's listing of My files,
|
||||
/// which is what the commands resolve a relative path against.
|
||||
pub fn drive_path(current: &OsStr) -> Vec<CompletionCandidate> {
|
||||
complete_drive(current, false)
|
||||
}
|
||||
|
||||
/// [`drive_path`] for an argument that must be a folder.
|
||||
pub fn drive_dir(current: &OsStr) -> Vec<CompletionCandidate> {
|
||||
complete_drive(current, true)
|
||||
}
|
||||
|
||||
/// `pdfs refresh`: a folder, or one of the two named caches.
|
||||
pub fn refresh_target(current: &OsStr) -> Vec<CompletionCandidate> {
|
||||
let typed = current.to_string_lossy();
|
||||
let mut candidates: Vec<_> = [
|
||||
("trash", "the trash listing"),
|
||||
("photos", "the photos timeline"),
|
||||
]
|
||||
.into_iter()
|
||||
.filter(|(name, _)| name.starts_with(typed.as_ref()))
|
||||
.map(|(name, help)| candidate(name, help))
|
||||
.collect();
|
||||
candidates.extend(drive_dir(current));
|
||||
candidates
|
||||
}
|
||||
|
||||
fn complete_drive(current: &OsStr, dirs_only: bool) -> Vec<CompletionCandidate> {
|
||||
let Some(word) = current.to_str() else {
|
||||
return Vec::new();
|
||||
};
|
||||
if word.starts_with('/') || word.starts_with('~') {
|
||||
let local = if dirs_only {
|
||||
PathCompleter::dir()
|
||||
} else {
|
||||
PathCompleter::any()
|
||||
};
|
||||
return local.complete(current);
|
||||
}
|
||||
let (parent, prefix) = split_drive_word(word);
|
||||
let listing = Request::ListDir {
|
||||
path: parent.trim_end_matches('/').to_owned(),
|
||||
};
|
||||
match ask(listing) {
|
||||
Some(Response::Entries { entries }) => {
|
||||
drive_candidates(parent, prefix, &entries, dirs_only)
|
||||
}
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Split a typed drive path into the folder to list (with its trailing `/`)
|
||||
/// and the start of the name being completed in it.
|
||||
fn split_drive_word(word: &str) -> (&str, &str) {
|
||||
match word.rfind('/') {
|
||||
Some(at) => word.split_at(at + 1),
|
||||
None => ("", word),
|
||||
}
|
||||
}
|
||||
|
||||
/// The entries of `parent` whose name starts with `prefix`. A folder ends in
|
||||
/// `/`, which tells every shell not to add a space after it, so the next Tab
|
||||
/// goes on into it.
|
||||
fn drive_candidates(
|
||||
parent: &str,
|
||||
prefix: &str,
|
||||
entries: &[DirEntry],
|
||||
dirs_only: bool,
|
||||
) -> Vec<CompletionCandidate> {
|
||||
entries
|
||||
.iter()
|
||||
.filter(|e| e.name.starts_with(prefix) && (e.is_dir || !dirs_only))
|
||||
.map(|e| {
|
||||
let slash = if e.is_dir { "/" } else { "" };
|
||||
CompletionCandidate::new(format!("{parent}{}{slash}", e.name))
|
||||
.hide(e.name.starts_with('.') && !prefix.starts_with('.'))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Ids and uids from the service's listings
|
||||
|
||||
pub fn sync_folder_id() -> Vec<CompletionCandidate> {
|
||||
match ask(Request::ListSyncFolders) {
|
||||
Some(Response::SyncFolders { items }) => sync_folder_candidates(&items),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn sync_folder_candidates(items: &[SyncFolderInfo]) -> Vec<CompletionCandidate> {
|
||||
items
|
||||
.iter()
|
||||
.map(|f| candidate(f.id.to_string(), format!("{} ({})", f.local_path, f.mode)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn queue_id() -> Vec<CompletionCandidate> {
|
||||
match ask(Request::ListPendingOps) {
|
||||
Some(Response::PendingOps { items }) => queue_candidates(&items),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn queue_candidates(items: &[PendingOpInfo]) -> Vec<CompletionCandidate> {
|
||||
items
|
||||
.iter()
|
||||
.map(|op| candidate(op.id.to_string(), format!("{} {}", op.kind, op.path)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn device_uid() -> Vec<CompletionCandidate> {
|
||||
match ask_cached("devices", Request::ListDevices) {
|
||||
Some(Response::Devices { items }) => device_candidates(&items),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn device_candidates(items: &[DeviceInfo]) -> Vec<CompletionCandidate> {
|
||||
items
|
||||
.iter()
|
||||
.map(|d| {
|
||||
let this = if d.this_device {
|
||||
" (this computer)"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
candidate(d.uid.as_str(), format!("{}{this}", d.name))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn trash_uid() -> Vec<CompletionCandidate> {
|
||||
match ask_cached("trash", Request::ListTrash) {
|
||||
Some(Response::Entries { entries }) => trash_candidates(&entries),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn trash_candidates(entries: &[DirEntry]) -> Vec<CompletionCandidate> {
|
||||
entries
|
||||
.iter()
|
||||
.map(|e| {
|
||||
let help = match e.trashed_from.as_deref() {
|
||||
Some(from) if !from.is_empty() => format!("{} (from {from})", e.name),
|
||||
_ => e.name.clone(),
|
||||
};
|
||||
candidate(e.uid.as_str(), help)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn album_uid() -> Vec<CompletionCandidate> {
|
||||
match ask_cached("albums", Request::PhotoAlbums) {
|
||||
Some(Response::Albums { items, .. }) => album_candidates(&items),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn album_candidates(items: &[AlbumInfo]) -> Vec<CompletionCandidate> {
|
||||
items
|
||||
.iter()
|
||||
.map(|a| candidate(a.uid.as_str(), a.name.as_str()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn shared_uid() -> Vec<CompletionCandidate> {
|
||||
match ask_cached("shared-with-me", Request::ListSharedWithMe) {
|
||||
Some(Response::Entries { entries }) => shared_candidates(&entries),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn shared_candidates(entries: &[DirEntry]) -> Vec<CompletionCandidate> {
|
||||
entries
|
||||
.iter()
|
||||
.map(|e| {
|
||||
let help = if e.shared_by.is_empty() {
|
||||
e.name.clone()
|
||||
} else {
|
||||
format!("{} (from {})", e.name, e.shared_by)
|
||||
};
|
||||
candidate(e.uid.as_str(), help)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn invitation_id() -> Vec<CompletionCandidate> {
|
||||
match ask_cached("invitations", Request::ListInvitations) {
|
||||
Some(Response::Invitations { items }) => invitation_candidates(&items),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn invitation_candidates(items: &[InvitationInfo]) -> Vec<CompletionCandidate> {
|
||||
items
|
||||
.iter()
|
||||
.map(|i| {
|
||||
let help = match i.name.as_deref() {
|
||||
Some(name) => format!("{name} from {}", i.inviter_email),
|
||||
None => format!("from {}", i.inviter_email),
|
||||
};
|
||||
candidate(i.id.as_str(), help)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn bookmark_token() -> Vec<CompletionCandidate> {
|
||||
match ask_cached("bookmarks", Request::ListBookmarks) {
|
||||
Some(Response::Bookmarks { items }) => bookmark_candidates(&items),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn bookmark_candidates(items: &[BookmarkInfo]) -> Vec<CompletionCandidate> {
|
||||
items
|
||||
.iter()
|
||||
.map(|b| candidate(b.token.as_str(), b.name.as_deref().unwrap_or(&b.url)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn conflict_path() -> Vec<CompletionCandidate> {
|
||||
match ask(Request::ListConflicts) {
|
||||
Some(Response::Conflicts { items }) => conflict_candidates(&items),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn conflict_candidates(items: &[ConflictInfo]) -> Vec<CompletionCandidate> {
|
||||
items
|
||||
.iter()
|
||||
.map(|c| candidate(c.path.as_str(), format!("copy of {}", c.original_path)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Ids that belong to the path typed before them
|
||||
|
||||
/// A version of the file named earlier on the line (`pdfs versions restore
|
||||
/// <path> <Tab>`).
|
||||
pub fn version_id(current: &OsStr) -> Vec<CompletionCandidate> {
|
||||
let Some(path) = typed_path() else {
|
||||
return Vec::new();
|
||||
};
|
||||
match ask(Request::ListRevisions { path }) {
|
||||
Some(Response::Revisions { items }) => starting_with(current, revision_candidates(&items)),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn revision_candidates(items: &[RevisionInfo]) -> Vec<CompletionCandidate> {
|
||||
items
|
||||
.iter()
|
||||
.map(|r| {
|
||||
let current = if r.is_active { ", current" } else { "" };
|
||||
let size = r.claimed_size.unwrap_or(r.size_on_storage).max(0);
|
||||
candidate(
|
||||
r.id.as_str(),
|
||||
format!(
|
||||
"{}, {}{current}",
|
||||
crate::format_epoch_utc(r.created),
|
||||
crate::human_bytes(size as u64)
|
||||
),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A member or invitation on the share of the path typed earlier on the line.
|
||||
pub fn share_entry_id(current: &OsStr) -> Vec<CompletionCandidate> {
|
||||
match share_of_typed_path() {
|
||||
Some((entries, _)) => starting_with(current, share_entry_candidates(&entries)),
|
||||
None => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn share_entry_candidates(entries: &[ShareEntry]) -> Vec<CompletionCandidate> {
|
||||
entries
|
||||
.iter()
|
||||
.map(|e| {
|
||||
// The kind is the next argument, so the help names it in the words
|
||||
// that argument takes.
|
||||
let kind = match e.kind {
|
||||
ShareEntryKind::Member => "member",
|
||||
ShareEntryKind::ProtonInvite => "proton",
|
||||
ShareEntryKind::ExternalInvite => "external",
|
||||
};
|
||||
candidate(e.id.as_str(), format!("{} ({}, {kind})", e.email, e.role))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The public link of the path typed earlier on the line.
|
||||
pub fn public_link_id(current: &OsStr) -> Vec<CompletionCandidate> {
|
||||
match share_of_typed_path() {
|
||||
Some((_, Some(link))) => starting_with(current, vec![public_link_candidate(&link)]),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Described without its URL: the fragment is the link's password, and a Tab
|
||||
/// menu is on screen for anyone watching.
|
||||
fn public_link_candidate(link: &PublicLinkInfo) -> CompletionCandidate {
|
||||
let help = match link.expires {
|
||||
Some(expires) => format!(
|
||||
"{} link, expires {}",
|
||||
link.role,
|
||||
crate::format_epoch_utc(expires)
|
||||
),
|
||||
None => format!("{} link", link.role),
|
||||
};
|
||||
candidate(link.id.as_str(), help)
|
||||
}
|
||||
|
||||
fn share_of_typed_path() -> Option<(Vec<ShareEntry>, Option<PublicLinkInfo>)> {
|
||||
match ask(Request::ListShare {
|
||||
path: typed_path()?,
|
||||
})? {
|
||||
Response::Share { entries, link } => Some((entries, link)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// [`clap_complete::engine::ArgValueCompleter`] candidates are not filtered by
|
||||
/// the engine, unlike [`clap_complete::engine::ArgValueCandidates`].
|
||||
fn starting_with(
|
||||
current: &OsStr,
|
||||
candidates: Vec<CompletionCandidate>,
|
||||
) -> Vec<CompletionCandidate> {
|
||||
let typed = current.to_string_lossy();
|
||||
candidates
|
||||
.into_iter()
|
||||
.filter(|c| c.get_value().to_string_lossy().starts_with(typed.as_ref()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The `path` argument of the command being completed, as the service takes
|
||||
/// it. A completer only receives the word under the cursor, so this reads the
|
||||
/// line from the process arguments, where the shell's stub passes it.
|
||||
fn typed_path() -> Option<String> {
|
||||
let args: Vec<OsString> = std::env::args_os().collect();
|
||||
let index = std::env::var("_CLAP_COMPLETE_INDEX")
|
||||
.ok()
|
||||
.and_then(|i| i.parse().ok());
|
||||
path_in(words_before_cursor(&args, index))
|
||||
}
|
||||
|
||||
/// The words of the line before the one being completed. The stub runs
|
||||
/// `pdfs -- <words>`; bash, zsh and elvish say which word the cursor is on,
|
||||
/// fish and PowerShell pass the line cut at the cursor, so it is the last.
|
||||
fn words_before_cursor(args: &[OsString], index: Option<usize>) -> Vec<OsString> {
|
||||
let start = args
|
||||
.iter()
|
||||
.position(|a| a == "--")
|
||||
.map_or(args.len(), |at| at + 1);
|
||||
let mut words = args[start..].to_vec();
|
||||
let index = index.unwrap_or(words.len().saturating_sub(1));
|
||||
words.truncate(index);
|
||||
words
|
||||
}
|
||||
|
||||
/// Parse `words` as far as they go and take the innermost command's `path`.
|
||||
fn path_in(words: Vec<OsString>) -> Option<String> {
|
||||
let matches = crate::Cli::command()
|
||||
.ignore_errors(true)
|
||||
.try_get_matches_from(words)
|
||||
.ok()?;
|
||||
let mut leaf = &matches;
|
||||
while let Some((_, sub)) = leaf.subcommand() {
|
||||
leaf = sub;
|
||||
}
|
||||
let path = leaf.try_get_one::<PathBuf>("path").ok().flatten()?;
|
||||
crate::path_arg(path).ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn values(candidates: &[CompletionCandidate]) -> Vec<String> {
|
||||
candidates
|
||||
.iter()
|
||||
.map(|c| c.get_value().to_string_lossy().into_owned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn help(candidate: &CompletionCandidate) -> String {
|
||||
candidate
|
||||
.get_help()
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn entry(name: &str, is_dir: bool) -> DirEntry {
|
||||
serde_json::from_value(serde_json::json!({
|
||||
"name": name,
|
||||
"is_dir": is_dir,
|
||||
"size": 0,
|
||||
"modified": 0,
|
||||
"pinned": false,
|
||||
"uid": format!("vol~{name}"),
|
||||
}))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn words(line: &[&str]) -> Vec<OsString> {
|
||||
line.iter().map(OsString::from).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_command_definition_is_valid() {
|
||||
crate::Cli::command().debug_assert();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completions_script_is_written_for_every_shell() {
|
||||
for shell in [
|
||||
ShellArg::Bash,
|
||||
ShellArg::Zsh,
|
||||
ShellArg::Fish,
|
||||
ShellArg::Elvish,
|
||||
ShellArg::Powershell,
|
||||
] {
|
||||
let mut script = Vec::new();
|
||||
write_script(shell, &mut script).unwrap();
|
||||
let script = String::from_utf8(script).unwrap();
|
||||
assert!(script.contains(VAR), "{script}");
|
||||
assert!(script.contains("pdfs"), "{script}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drive_path_splits_parent_and_prefix() {
|
||||
assert_eq!(split_drive_word(""), ("", ""));
|
||||
assert_eq!(split_drive_word("Doc"), ("", "Doc"));
|
||||
assert_eq!(split_drive_word("Documents/"), ("Documents/", ""));
|
||||
assert_eq!(split_drive_word("Documents/Rep"), ("Documents/", "Rep"));
|
||||
assert_eq!(split_drive_word("a/b/c"), ("a/b/", "c"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drive_dirs_end_in_a_slash() {
|
||||
let entries = [
|
||||
entry("Reports", true),
|
||||
entry("Receipt.pdf", false),
|
||||
entry("Other", true),
|
||||
];
|
||||
let all = drive_candidates("Documents/", "Re", &entries, false);
|
||||
assert_eq!(
|
||||
values(&all),
|
||||
["Documents/Reports/", "Documents/Receipt.pdf"]
|
||||
);
|
||||
let dirs = drive_candidates("Documents/", "", &entries, true);
|
||||
assert_eq!(values(&dirs), ["Documents/Reports/", "Documents/Other/"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hidden_drive_entries_need_a_typed_dot() {
|
||||
let entries = [entry(".git", true)];
|
||||
assert!(drive_candidates("", "", &entries, false)[0].is_hide_set());
|
||||
assert!(!drive_candidates("", ".", &entries, false)[0].is_hide_set());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_folder_ids_complete_with_their_local_path() {
|
||||
let folders: Vec<SyncFolderInfo> = serde_json::from_value(serde_json::json!([{
|
||||
"id": 3,
|
||||
"local_path": "/home/me/Documents",
|
||||
"remote_uid": "vol~docs",
|
||||
"mode": "mirror",
|
||||
"state": "idle",
|
||||
"last_sync": 0,
|
||||
}]))
|
||||
.unwrap();
|
||||
let candidates = sync_folder_candidates(&folders);
|
||||
assert_eq!(values(&candidates), ["3"]);
|
||||
assert_eq!(help(&candidates[0]), "/home/me/Documents (mirror)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trash_uids_complete_with_their_name() {
|
||||
let mut from_docs = entry("a.pdf", false);
|
||||
from_docs.trashed_from = Some("Documents".into());
|
||||
let candidates = trash_candidates(&[from_docs, entry("b", true)]);
|
||||
assert_eq!(values(&candidates), ["vol~a.pdf", "vol~b"]);
|
||||
assert_eq!(help(&candidates[0]), "a.pdf (from Documents)");
|
||||
assert_eq!(help(&candidates[1]), "b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completer_without_a_service_offers_nothing() {
|
||||
let dir = std::env::temp_dir().join(format!("pdfs-complete-{}", std::process::id()));
|
||||
let socket = dir.join("control.sock");
|
||||
assert!(ask_at(&socket, &Request::ListSyncFolders).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cached_listing_is_reused_until_it_is_stale() {
|
||||
let dir = std::env::temp_dir().join(format!("pdfs-complete-cache-{}", std::process::id()));
|
||||
let file = dir.join("completion-devices.json");
|
||||
write_cache(&file, &Response::Devices { items: Vec::new() });
|
||||
assert!(matches!(read_fresh(&file), Some(Response::Devices { .. })));
|
||||
let mode = std::os::unix::fs::PermissionsExt::mode(
|
||||
&std::fs::metadata(&file).unwrap().permissions(),
|
||||
);
|
||||
assert_eq!(mode & 0o777, 0o600);
|
||||
|
||||
let stale = std::time::SystemTime::now() - CACHE_TTL - Duration::from_secs(1);
|
||||
std::fs::File::options()
|
||||
.write(true)
|
||||
.open(&file)
|
||||
.unwrap()
|
||||
.set_modified(stale)
|
||||
.unwrap();
|
||||
assert!(read_fresh(&file).is_none());
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn words_before_cursor_stop_at_the_cursor() {
|
||||
let args = words(&[
|
||||
"pdfs", "--", "pdfs", "versions", "restore", "a.txt", "", "x",
|
||||
]);
|
||||
assert_eq!(
|
||||
words_before_cursor(&args, Some(4)),
|
||||
words(&["pdfs", "versions", "restore", "a.txt"])
|
||||
);
|
||||
// fish: the line arrives cut at the cursor, the current word last.
|
||||
let fish = words(&["pdfs", "--", "pdfs", "versions", "restore", "a.txt", "ab"]);
|
||||
assert_eq!(
|
||||
words_before_cursor(&fish, None),
|
||||
words(&["pdfs", "versions", "restore", "a.txt"])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completing_words_finds_the_path_before_the_id() {
|
||||
let line = words(&["pdfs", "versions", "restore", "Documents/a.txt"]);
|
||||
assert_eq!(path_in(line).as_deref(), Some("Documents/a.txt"));
|
||||
let line = words(&["pdfs", "--json", "share-role", "Shared/x"]);
|
||||
assert_eq!(path_in(line).as_deref(), Some("Shared/x"));
|
||||
let line = words(&["pdfs", "public-link", "update", "--role", "editor", "a"]);
|
||||
assert_eq!(path_in(line).as_deref(), Some("a"));
|
||||
assert_eq!(path_in(words(&["pdfs", "versions", "restore"])), None);
|
||||
}
|
||||
}
|
||||
+575
-61
File diff suppressed because it is too large.
Load diff
@@ -10,8 +10,10 @@ homepage.workspace = true
|
||||
description = "Core auth/session/config for the Proton Drive Linux client"
|
||||
|
||||
[dependencies]
|
||||
argon2 = "0.5"
|
||||
proton-sdk.workspace = true
|
||||
proton-drive-rs.workspace = true
|
||||
chrono.workspace = true
|
||||
tokio.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
@@ -19,12 +21,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
|
||||
|
||||
@@ -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 <photos-dir> 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<String> = std::env::args().skip(1).collect();
|
||||
match args.iter().map(String::as_str).collect::<Vec<_>>()[..] {
|
||||
["serve", photos] => serve(Path::new(photos)),
|
||||
["seed-keyring"] => seed_keyring(),
|
||||
_ => Err("usage: demo_daemon serve <photos-dir> | 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<keyring_core::CredentialStore> =
|
||||
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<PathBuf>,
|
||||
photos: Vec<PhotoItem>,
|
||||
/// Each photo's place, by index into [`PLACES`].
|
||||
place_of: Vec<Option<usize>>,
|
||||
}
|
||||
|
||||
/// 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<Self, String> {
|
||||
let mut pictures: Vec<PathBuf> = 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<i64> = (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<PhotoItem> = 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<PhotoKind>) -> Vec<PhotoMonth> {
|
||||
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<AlbumInfo> {
|
||||
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<Item = &PhotoItem> {
|
||||
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<Item = (usize, &PhotoItem)> {
|
||||
self.photos
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(move |(i, _)| self.place_of[*i].is_some_and(|p| p as u32 + 1 == id))
|
||||
}
|
||||
|
||||
fn places(&self) -> Vec<PlaceInfo> {
|
||||
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<PhotoLocation> {
|
||||
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<DirEntry> {
|
||||
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<String> {
|
||||
let name = uid.rsplit('/').next()?;
|
||||
if !name.ends_with(".jpg") {
|
||||
return None;
|
||||
}
|
||||
let index = name.bytes().map(usize::from).sum::<usize>() % self.pictures.len();
|
||||
Some(self.pictures[index].display().to_string())
|
||||
}
|
||||
|
||||
fn pins(&self) -> Vec<Pin> {
|
||||
[
|
||||
("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<DeviceInfo> {
|
||||
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<SyncFolderInfo> {
|
||||
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<MountSpec> {
|
||||
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<ActivityEntry> {
|
||||
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<SearchHit> {
|
||||
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<LocalHit> {
|
||||
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()
|
||||
}
|
||||
+382
-34
@@ -123,13 +123,126 @@ fn client_config() -> ProtonClientConfiguration {
|
||||
ProtonClientConfiguration::new(app_version).with_user_agent(user_agent)
|
||||
}
|
||||
|
||||
fn keyring_entry() -> Result<Entry> {
|
||||
// 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<T>(op: impl Fn(&Entry) -> keyring_core::Result<T>) -> Result<T> {
|
||||
with_keyring_entry(KEYRING_SERVICE, KEYRING_USER, op)
|
||||
}
|
||||
|
||||
/// Verify the machine-local Gallery passphrase, setting it on first use.
|
||||
pub fn unlock_gallery(username: &str, passphrase: &str) -> std::result::Result<(), String> {
|
||||
use argon2::password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString};
|
||||
use std::io::Read;
|
||||
|
||||
if passphrase.is_empty() || passphrase.len() > 1024 {
|
||||
return Err("Enter your Gallery passphrase.".into());
|
||||
}
|
||||
static VERIFY: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
|
||||
let _verify = VERIFY.lock();
|
||||
let service = "io.narl.protongallery";
|
||||
let user = format!("hidden-albums:{username}");
|
||||
let argon = argon2::Argon2::default();
|
||||
match with_keyring_entry(service, &user, Entry::get_password) {
|
||||
Ok(stored) => {
|
||||
let hash = PasswordHash::new(&stored)
|
||||
.map_err(|_| "The saved Gallery passphrase is unreadable.")?;
|
||||
argon
|
||||
.verify_password(passphrase.as_bytes(), &hash)
|
||||
.map_err(|_| "The Gallery passphrase is incorrect.".into())
|
||||
}
|
||||
Err(Error::Keyring(keyring_core::Error::NoEntry)) => {
|
||||
if passphrase.chars().count() < 12 {
|
||||
return Err("Choose a Gallery passphrase with at least 12 characters.".into());
|
||||
}
|
||||
let mut bytes = [0; 16];
|
||||
std::fs::File::open("/dev/urandom")
|
||||
.and_then(|mut file| file.read_exact(&mut bytes))
|
||||
.map_err(|_| "Couldn’t generate the passphrase salt.")?;
|
||||
let salt = SaltString::encode_b64(&bytes)
|
||||
.map_err(|_| "Couldn’t generate the passphrase salt.")?;
|
||||
let hash = argon
|
||||
.hash_password(passphrase.as_bytes(), &salt)
|
||||
.map_err(|_| "Couldn’t protect the Gallery passphrase.")?
|
||||
.to_string();
|
||||
with_keyring_entry(service, &user, |entry| entry.set_password(&hash))
|
||||
.map_err(|_| "Couldn’t save the Gallery passphrase in the system keyring.".into())
|
||||
}
|
||||
Err(_) => Err("The system keyring is locked or unavailable.".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<T>(
|
||||
service: &str,
|
||||
user: &str,
|
||||
op: impl Fn(&Entry) -> keyring_core::Result<T>,
|
||||
) -> Result<T> {
|
||||
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<Arc<keyring_core::CredentialStore>> {
|
||||
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<keyring_core::CredentialStore>) {
|
||||
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<Arc<keyring_core::CredentialStore>> {
|
||||
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<Arc<keyring_core::CredentialStore>> {
|
||||
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 +532,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<StoredSession> {
|
||||
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 +737,9 @@ pub fn logout() -> Result<bool> {
|
||||
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 +782,10 @@ fn resume_stored(stored: &StoredSession) -> Result<ProtonApiSession> {
|
||||
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 +1148,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 +1165,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 +1362,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::CredentialStore> = 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<Option<Arc<keyring_core::CredentialStore>>> =
|
||||
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::<keyring_core::mock::Cred>()
|
||||
.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 +1590,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::<keyring_core::mock::Cred>()
|
||||
.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 +1618,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 {
|
||||
|
||||
@@ -579,6 +579,7 @@ impl ContentCache {
|
||||
// the disk.
|
||||
cache.reseed_totals();
|
||||
cache.import_legacy_pins()?;
|
||||
cache.set_budget(max_bytes);
|
||||
Ok(cache)
|
||||
}
|
||||
|
||||
@@ -1153,16 +1154,18 @@ impl ContentCache {
|
||||
};
|
||||
let mut skipped = 0usize;
|
||||
while running > cap {
|
||||
// Fetch a batch past whatever we have already skipped as exempt.
|
||||
let want = skipped + EVICT_BATCH;
|
||||
let Ok(batch) = self.db.cache_eviction_candidates(kind, want) else {
|
||||
// A whole batch can be pinned. Keep looking until the index has
|
||||
// no entries past the ones we have skipped.
|
||||
let Ok(batch) = self
|
||||
.db
|
||||
.cache_eviction_candidates(kind, EVICT_BATCH, skipped)
|
||||
else {
|
||||
break;
|
||||
};
|
||||
if batch.len() <= skipped {
|
||||
break; // nothing new to consider
|
||||
if batch.is_empty() {
|
||||
break;
|
||||
}
|
||||
let mut evicted_any = false;
|
||||
for (key, size) in batch.into_iter().skip(skipped) {
|
||||
for (key, size) in batch {
|
||||
if running <= cap {
|
||||
break;
|
||||
}
|
||||
@@ -1170,14 +1173,18 @@ impl ContentCache {
|
||||
skipped += 1; // counts toward the total, never a victim
|
||||
continue;
|
||||
}
|
||||
let _ = std::fs::remove_file(dir.join(&key));
|
||||
if let Err(error) = std::fs::remove_file(dir.join(&key))
|
||||
&& error.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
skipped += 1;
|
||||
continue;
|
||||
}
|
||||
let _ = std::fs::remove_file(dir.join(format!("{key}.meta")));
|
||||
let _ = self.db.cache_remove(&key);
|
||||
if self.db.cache_remove(&key).is_err() {
|
||||
skipped += 1;
|
||||
continue;
|
||||
}
|
||||
running = running.saturating_sub(size);
|
||||
evicted_any = true;
|
||||
}
|
||||
if !evicted_any {
|
||||
break; // every remaining candidate is exempt
|
||||
}
|
||||
}
|
||||
// The index is the truth; realign the fast path with it.
|
||||
@@ -1405,7 +1412,7 @@ impl ContentCache {
|
||||
.collect();
|
||||
let content_dir = self.content_dir.clone();
|
||||
// Force pool enforcement ignoring current cap check to free disk space immediately
|
||||
let Ok(list) = self.db.cache_eviction_candidates(KIND_BLOB, 10) else {
|
||||
let Ok(list) = self.db.cache_eviction_candidates(KIND_BLOB, 10, 0) else {
|
||||
return;
|
||||
};
|
||||
for (key, bytes) in list {
|
||||
@@ -1883,13 +1890,13 @@ impl ContentCache {
|
||||
// Pinned blobs (by cache key) are exempt from eviction. Resolves direct
|
||||
// pins plus every descendant of a recursively-pinned folder, hashed into
|
||||
// cache keys to match on-disk filenames.
|
||||
let pinned: HashSet<String> = self
|
||||
.db
|
||||
.pinned_uids()
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.map(|uid| Self::key_str(uid))
|
||||
.collect();
|
||||
let pinned = match self.db.pinned_uids() {
|
||||
Ok(uids) => uids.iter().map(|uid| Self::key_str(uid)).collect(),
|
||||
Err(error) => {
|
||||
tracing::warn!(%error, "reading pins for cache eviction failed");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let content_dir = self.content_dir.clone();
|
||||
self.enforce_pool(KIND_BLOB, &content_dir, Some(&pinned));
|
||||
}
|
||||
@@ -2010,8 +2017,7 @@ impl ContentCache {
|
||||
}
|
||||
|
||||
/// Retune the soft byte cap (`0` = unlimited) at runtime and immediately
|
||||
/// enforce it: blobs and blocks are LRU-evicted back under the new cap (a
|
||||
/// lower cap frees space now; a higher one is a no-op until the next store).
|
||||
/// enforce it across all three pools.
|
||||
/// Called by the daemon when the Settings page changes the cache budget.
|
||||
pub fn set_budget(&self, bytes: u64) {
|
||||
self.max_bytes.store(bytes, Ordering::Relaxed);
|
||||
@@ -2245,19 +2251,50 @@ mod tests {
|
||||
// crash) but the same on-disk cache: reconcile repopulates from disk.
|
||||
let db2 = Arc::new(Db::open_in_memory().unwrap());
|
||||
assert!(db2.cache_entries_by_kind("blob").unwrap().is_empty());
|
||||
let c = ContentCache::open(content, pins, 8, db2.clone()).unwrap();
|
||||
let c = ContentCache::open(content, pins, 0, db2.clone()).unwrap();
|
||||
assert_eq!(db2.cache_entries_by_kind("blob").unwrap().len(), 3);
|
||||
|
||||
// With the index rebuilt, a further store enforces the budget against the
|
||||
// reconciled entries instead of leaking them.
|
||||
c.store(&uid("d"), 1, 4, b"dddd").unwrap();
|
||||
let total: u64 = db2
|
||||
.cache_entries_by_kind("blob")
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|(_, s)| s)
|
||||
.sum();
|
||||
assert!(total <= 8, "budget enforced after reconcile, total={total}");
|
||||
assert_eq!(c.usage(), 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reopening_trims_all_cache_pools_even_when_the_oldest_batch_is_pinned() {
|
||||
let (c, _dir) = cache();
|
||||
for i in 0..EVICT_BATCH {
|
||||
let u = uid(&format!("pinned-{i}"));
|
||||
c.store(&u, 1, 1, b"p").unwrap();
|
||||
c.add_pin(&u, Path::new("pinned"), false).unwrap();
|
||||
std::fs::File::open(c.blob_path(&u))
|
||||
.unwrap()
|
||||
.set_modified(UNIX_EPOCH)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let unpinned = uid("unpinned");
|
||||
let payload = [0u8; 64];
|
||||
let size = payload.len() as u64;
|
||||
c.store(&unpinned, 1, size, &payload).unwrap();
|
||||
c.store_block(&unpinned, 1, size, blk(size, 0), &payload)
|
||||
.unwrap();
|
||||
c.store_thumbnail(&unpinned, 1, 1, &payload).unwrap();
|
||||
let recovery = c.recovery_dir.join("recovered.bin");
|
||||
std::fs::write(&recovery, b"recovered write").unwrap();
|
||||
|
||||
let budget = (EVICT_BATCH as u64 * 100).div_ceil(SPLIT_BLOB);
|
||||
let reopened = ContentCache::open(
|
||||
c.content_dir.clone(),
|
||||
c.pins_path.clone(),
|
||||
budget,
|
||||
c.db.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(reopened.usage(), EVICT_BATCH as u64);
|
||||
assert!(!reopened.blob_path(&unpinned).exists());
|
||||
for i in 0..EVICT_BATCH {
|
||||
assert!(reopened.is_cached(&uid(&format!("pinned-{i}")), 1, 1));
|
||||
}
|
||||
assert_eq!(std::fs::read(recovery).unwrap(), b"recovered write");
|
||||
}
|
||||
|
||||
/// An eviction removes the blob, then its tag. A crash in between, or a
|
||||
|
||||
+287
-54
@@ -163,6 +163,10 @@ pub struct AppConfig {
|
||||
/// Defaulted for configs predating the field.
|
||||
#[serde(default)]
|
||||
pub files_view: FilesView,
|
||||
/// How the desktop app's Photos page lays out its grid. Defaulted for
|
||||
/// configs predating the field.
|
||||
#[serde(default)]
|
||||
pub photos_view: PhotosView,
|
||||
/// The desktop app's main window size, remembered between runs. Defaulted
|
||||
/// for configs predating the field.
|
||||
#[serde(default)]
|
||||
@@ -220,6 +224,10 @@ pub struct FilesView {
|
||||
pub descending: bool,
|
||||
/// Folders ahead of files, whatever the sort.
|
||||
pub folders_first: bool,
|
||||
/// The icon grid's thumbnail edge in logical pixels. `None` means the
|
||||
/// app's default.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub icon_size: Option<i32>,
|
||||
}
|
||||
|
||||
impl Default for FilesView {
|
||||
@@ -229,10 +237,23 @@ impl Default for FilesView {
|
||||
sort: FileSort::Name,
|
||||
descending: false,
|
||||
folders_first: true,
|
||||
icon_size: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The Photos page's grid choices, remembered between runs.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
#[serde(default)]
|
||||
pub struct PhotosView {
|
||||
/// The zoom level: the row height the grid aims for, in logical pixels.
|
||||
/// `None` means the app's default.
|
||||
pub row_height: Option<i32>,
|
||||
/// Crop every photo to a square tile instead of showing it at its own
|
||||
/// shape.
|
||||
pub square: bool,
|
||||
}
|
||||
|
||||
/// The desktop app's main window, as it was when it last closed.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[serde(default)]
|
||||
@@ -370,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<String>),
|
||||
) -> Result<AppConfig> {
|
||||
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<()> {
|
||||
@@ -386,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::<AppConfig>(&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<R>(path: &Path, change: impl FnOnce(&mut AppConfig) -> R) -> Result<R> {
|
||||
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<std::fs::File> {
|
||||
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,
|
||||
@@ -405,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::<AppConfig>(&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<R>(&self, change: impl FnOnce(&mut AppConfig) -> R) -> Result<R> {
|
||||
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<String>)) -> Result<AppConfig> {
|
||||
change_ignore_patterns_at(&self.config_path(), edit)
|
||||
}
|
||||
|
||||
/// This machine's id (e.g. ~/.config/proton-drive-linux/machine-id). See
|
||||
@@ -541,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")
|
||||
@@ -655,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]
|
||||
@@ -761,6 +977,11 @@ mod tests {
|
||||
sort: FileSort::Modified,
|
||||
descending: true,
|
||||
folders_first: false,
|
||||
icon_size: Some(192),
|
||||
},
|
||||
photos_view: PhotosView {
|
||||
row_height: Some(240),
|
||||
square: true,
|
||||
},
|
||||
window: WindowState {
|
||||
width: 1200,
|
||||
@@ -792,6 +1013,7 @@ mod tests {
|
||||
assert_eq!(decoded.upload_limit, Some(500_000));
|
||||
assert_eq!(decoded.download_limit, None);
|
||||
assert_eq!(decoded.files_view, config.files_view);
|
||||
assert_eq!(decoded.photos_view, config.photos_view);
|
||||
assert_eq!(decoded.window, config.window);
|
||||
}
|
||||
|
||||
@@ -835,6 +1057,17 @@ mod tests {
|
||||
let partial: FilesView = serde_json::from_str(r#"{"sort":"size"}"#).unwrap();
|
||||
assert_eq!(partial.sort, FileSort::Size);
|
||||
assert!(partial.folders_first);
|
||||
assert_eq!(partial.icon_size, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_config_without_a_photos_view_gets_the_default() {
|
||||
let decoded: AppConfig =
|
||||
serde_json::from_str(r#"{"app_version":"a","user_agent":"b"}"#).unwrap();
|
||||
assert_eq!(decoded.photos_view, PhotosView::default());
|
||||
let partial: PhotosView = serde_json::from_str(r#"{"square":true}"#).unwrap();
|
||||
assert!(partial.square);
|
||||
assert_eq!(partial.row_height, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+615
-100
File diff suppressed because it is too large.
Load diff
@@ -163,31 +163,47 @@ impl Db {
|
||||
album_uid: &str,
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
) -> Result<Vec<StoredPhoto>> {
|
||||
self.album_photos_page_visible(album_uid, offset, limit, "[]")
|
||||
}
|
||||
|
||||
pub fn album_photos_page_visible(
|
||||
&self,
|
||||
album_uid: &str,
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
excluded: &str,
|
||||
) -> Result<Vec<StoredPhoto>> {
|
||||
let conn = self.read();
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT uid, capture_time, name, ratio, thumb_state, kind FROM album_photos
|
||||
WHERE album_uid = ?1 ORDER BY seq LIMIT ?2 OFFSET ?3",
|
||||
WHERE album_uid = ?1 AND uid NOT IN (SELECT value FROM json_each(?4))
|
||||
ORDER BY seq LIMIT ?2 OFFSET ?3",
|
||||
)?;
|
||||
let rows = stmt
|
||||
.query_map(params![album_uid, limit as i64, offset as i64], |r| {
|
||||
Ok(StoredPhoto {
|
||||
uid: r.get(0)?,
|
||||
capture_time: r.get(1)?,
|
||||
name: r.get(2)?,
|
||||
ratio: r.get(3)?,
|
||||
thumb_state: r.get(4)?,
|
||||
kind: crate::control::PhotoKind::from_i64(r.get(5)?),
|
||||
// Album rows have no favorite state of their own: an album
|
||||
// photo on someone else's volume is not in our timeline, and
|
||||
// favouriting one is not supported yet anyway.
|
||||
favorite: false,
|
||||
// Nor are album rows grouped: an album is a list the user
|
||||
// made, and it shows exactly what they put in it.
|
||||
group_size: 1,
|
||||
has_raw: false,
|
||||
})
|
||||
})?
|
||||
.query_map(
|
||||
params![album_uid, limit as i64, offset as i64, excluded],
|
||||
|r| {
|
||||
Ok(StoredPhoto {
|
||||
uid: r.get(0)?,
|
||||
capture_time: r.get(1)?,
|
||||
name: r.get(2)?,
|
||||
ratio: r.get(3)?,
|
||||
thumb_state: r.get(4)?,
|
||||
kind: crate::control::PhotoKind::from_i64(r.get(5)?),
|
||||
// Album rows have no favorite state of their own: an album
|
||||
// photo on someone else's volume is not in our timeline, and
|
||||
// favouriting one is not supported yet anyway.
|
||||
favorite: false,
|
||||
// Nor are album rows grouped: an album is a list the user
|
||||
// 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::<rusqlite::Result<_>>()?;
|
||||
Ok(rows)
|
||||
}
|
||||
@@ -247,6 +263,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::<rusqlite::Result<_>>()?;
|
||||
|
||||
@@ -108,7 +108,8 @@ impl Db {
|
||||
Ok(total.max(0) as u64)
|
||||
}
|
||||
|
||||
/// The `limit` least-recently-accessed entries of `kind`, oldest first.
|
||||
/// The `limit` least-recently-accessed entries of `kind` after `offset`,
|
||||
/// oldest first.
|
||||
///
|
||||
/// Eviction wants a handful of victims, not the whole table. Bounded so a
|
||||
/// pass over a large cache does not read every row to drop a few, and so the
|
||||
@@ -118,13 +119,14 @@ impl Db {
|
||||
&self,
|
||||
kind: &str,
|
||||
limit: usize,
|
||||
offset: usize,
|
||||
) -> Result<Vec<(String, u64)>> {
|
||||
let conn = self.read();
|
||||
let mut stmt = conn.prepare_cached(
|
||||
"SELECT cache_key, size_bytes FROM cache_entries
|
||||
WHERE kind = ?1 ORDER BY last_accessed ASC LIMIT ?2",
|
||||
WHERE kind = ?1 ORDER BY last_accessed ASC LIMIT ?2 OFFSET ?3",
|
||||
)?;
|
||||
let rows = stmt.query_map(params![kind, limit as i64], |r| {
|
||||
let rows = stmt.query_map(params![kind, limit as i64, offset as i64], |r| {
|
||||
Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)?.max(0) as u64))
|
||||
})?;
|
||||
let mut out = Vec::new();
|
||||
|
||||
@@ -11,7 +11,7 @@ use super::Db;
|
||||
use crate::Result;
|
||||
|
||||
/// Current schema version. Bump on every forward migration added below.
|
||||
pub(super) const SCHEMA_VERSION: i64 = 44;
|
||||
pub(super) const SCHEMA_VERSION: i64 = 45;
|
||||
|
||||
impl Db {
|
||||
pub(super) fn migrate(&self) -> Result<()> {
|
||||
@@ -467,6 +467,29 @@ impl Db {
|
||||
tx.execute_batch(MIGRATION_V44)?;
|
||||
}
|
||||
}
|
||||
if current < 45 {
|
||||
let has_folders: bool = tx.query_row(
|
||||
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'sync_folder'",
|
||||
[],
|
||||
|row| row.get::<_, i64>(0),
|
||||
)? > 0;
|
||||
let has_kind: bool = tx.query_row(
|
||||
"SELECT COUNT(*) FROM pragma_table_info('sync_folder') WHERE name = 'kind'",
|
||||
[],
|
||||
|row| row.get::<_, i64>(0),
|
||||
)? > 0;
|
||||
if has_folders && !has_kind {
|
||||
tx.execute_batch(MIGRATION_V45_FOLDERS)?;
|
||||
let has_mounts: bool = tx.query_row(
|
||||
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'mount'",
|
||||
[],
|
||||
|row| row.get::<_, i64>(0),
|
||||
)? > 0;
|
||||
if has_mounts {
|
||||
tx.execute_batch(MIGRATION_V45_MOUNTS)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
tx.execute(
|
||||
"INSERT INTO sync_state (key, value) VALUES ('schema_version', ?1)
|
||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value",
|
||||
@@ -1382,3 +1405,57 @@ CREATE INDEX IF NOT EXISTS pending_op_kind_name ON pending_op(kind, name);
|
||||
const MIGRATION_V44: &str = "
|
||||
ALTER TABLE sync_entry ADD COLUMN remote_revision_id TEXT;
|
||||
";
|
||||
|
||||
/// Schema v45: distinguish device backups from My files attachments. Existing
|
||||
/// released rows are device backups. Attachments cannot evict their local tree.
|
||||
const MIGRATION_V45_FOLDERS: &str = "
|
||||
ALTER TABLE sync_folder ADD COLUMN kind TEXT NOT NULL DEFAULT 'device'
|
||||
CHECK (kind IN ('device', 'attached'));
|
||||
CREATE TRIGGER sync_folder_attached_insert
|
||||
BEFORE INSERT ON sync_folder
|
||||
WHEN NEW.kind = 'attached' AND (NEW.mode != 'mirror' OR NEW.pending_mode IS NOT NULL)
|
||||
BEGIN SELECT RAISE(ABORT, 'attached folders only support mirror mode'); END;
|
||||
CREATE TRIGGER sync_folder_attached_update
|
||||
BEFORE UPDATE ON sync_folder
|
||||
WHEN NEW.kind = 'attached' AND (NEW.mode != 'mirror' OR NEW.pending_mode IS NOT NULL)
|
||||
BEGIN SELECT RAISE(ABORT, 'attached folders only support mirror mode'); END;
|
||||
";
|
||||
|
||||
const MIGRATION_V45_MOUNTS: &str = "
|
||||
DROP TRIGGER mount_sync_folder_insert;
|
||||
DROP INDEX mount_myfiles;
|
||||
DROP INDEX mount_device;
|
||||
DROP INDEX mount_shared;
|
||||
ALTER TABLE mount RENAME TO mount_v44;
|
||||
CREATE TABLE mount (
|
||||
id INTEGER PRIMARY KEY,
|
||||
kind TEXT NOT NULL CHECK (kind IN ('myfiles', 'device', 'attached', 'shared')),
|
||||
sync_folder_id INTEGER REFERENCES sync_folder(id) ON DELETE CASCADE,
|
||||
share_root_uid TEXT,
|
||||
local_path TEXT NOT NULL DEFAULT '',
|
||||
root_uid TEXT NOT NULL DEFAULT '',
|
||||
root_share_id TEXT NOT NULL DEFAULT '',
|
||||
mode TEXT NOT NULL DEFAULT 'unknown',
|
||||
access TEXT NOT NULL DEFAULT 'rw' CHECK (access IN ('rw', 'ro')),
|
||||
CHECK (
|
||||
(kind = 'myfiles' AND sync_folder_id IS NULL AND share_root_uid IS NULL)
|
||||
OR (kind IN ('device', 'attached') AND sync_folder_id IS NOT NULL AND share_root_uid IS NULL)
|
||||
OR (kind = 'shared' AND sync_folder_id IS NULL AND share_root_uid IS NOT NULL)
|
||||
)
|
||||
);
|
||||
INSERT INTO mount SELECT * FROM mount_v44;
|
||||
DROP TABLE mount_v44;
|
||||
CREATE UNIQUE INDEX mount_myfiles ON mount(kind) WHERE kind = 'myfiles';
|
||||
CREATE UNIQUE INDEX mount_device ON mount(sync_folder_id) WHERE kind IN ('device', 'attached');
|
||||
CREATE UNIQUE INDEX mount_shared ON mount(share_root_uid) WHERE kind = 'shared';
|
||||
CREATE TRIGGER mount_sync_folder_insert
|
||||
AFTER INSERT ON sync_folder
|
||||
BEGIN
|
||||
INSERT INTO mount (kind, sync_folder_id) VALUES (NEW.kind, NEW.id);
|
||||
END;
|
||||
CREATE TRIGGER mount_sync_folder_kind_update
|
||||
AFTER UPDATE OF kind ON sync_folder
|
||||
BEGIN
|
||||
UPDATE mount SET kind = NEW.kind WHERE sync_folder_id = NEW.id;
|
||||
END;
|
||||
";
|
||||
@@ -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<Option<File>> {
|
||||
if is_in_memory(path) {
|
||||
return Ok(None);
|
||||
@@ -456,22 +463,32 @@ fn acquire_single_writer_lock(path: &Path) -> Result<Option<File>> {
|
||||
.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");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Unified mount/location presentation rows.
|
||||
//!
|
||||
//! A device row stores only its `sync_folder` identity. Its path, remote root,
|
||||
//! A device or attached row stores only its `sync_folder` identity. Its path, remote root,
|
||||
//! mode, state, pending mode, and last-sync timestamp are selected from
|
||||
//! `sync_folder`, which remains the authoritative sync-engine table.
|
||||
|
||||
@@ -72,7 +72,7 @@ impl Db {
|
||||
)? > 0)
|
||||
}
|
||||
|
||||
/// Every local location, with device fields joined from `sync_folder`.
|
||||
/// Every local location, with synced-folder fields joined from `sync_folder`.
|
||||
pub fn mount_list(&self) -> Result<Vec<MountSpec>> {
|
||||
let conn = self.read();
|
||||
let mut stmt = conn.prepare(
|
||||
@@ -81,14 +81,14 @@ impl Db {
|
||||
m.kind,
|
||||
m.sync_folder_id,
|
||||
m.share_root_uid,
|
||||
CASE WHEN m.kind = 'device' THEN sf.local_path ELSE m.local_path END,
|
||||
CASE WHEN m.kind = 'device' THEN sf.remote_uid ELSE m.root_uid END,
|
||||
CASE WHEN m.kind = 'device' THEN sf.remote_share_id ELSE m.root_share_id END,
|
||||
CASE WHEN m.kind = 'device' THEN sf.mode ELSE m.mode END,
|
||||
CASE WHEN m.kind IN ('device', 'attached') THEN sf.local_path ELSE m.local_path END,
|
||||
CASE WHEN m.kind IN ('device', 'attached') THEN sf.remote_uid ELSE m.root_uid END,
|
||||
CASE WHEN m.kind IN ('device', 'attached') THEN sf.remote_share_id ELSE m.root_share_id END,
|
||||
CASE WHEN m.kind IN ('device', 'attached') THEN sf.mode ELSE m.mode END,
|
||||
m.access,
|
||||
CASE WHEN m.kind = 'device' THEN sf.state ELSE 'idle' END,
|
||||
CASE WHEN m.kind = 'device' THEN sf.last_sync ELSE 0 END,
|
||||
CASE WHEN m.kind = 'device' THEN sf.pending_mode ELSE NULL END
|
||||
CASE WHEN m.kind IN ('device', 'attached') THEN sf.state ELSE 'idle' END,
|
||||
CASE WHEN m.kind IN ('device', 'attached') THEN sf.last_sync ELSE 0 END,
|
||||
CASE WHEN m.kind IN ('device', 'attached') THEN sf.pending_mode ELSE NULL END
|
||||
FROM mount AS m
|
||||
LEFT JOIN sync_folder AS sf ON sf.id = m.sync_folder_id
|
||||
ORDER BY CASE m.kind
|
||||
@@ -138,6 +138,11 @@ impl Db {
|
||||
Error::Other(format!("mount row {id} has no sync_folder_id"))
|
||||
})?,
|
||||
},
|
||||
"attached" => MountKind::Attached {
|
||||
sync_folder_id: sync_folder_id.ok_or_else(|| {
|
||||
Error::Other(format!("mount row {id} has no sync_folder_id"))
|
||||
})?,
|
||||
},
|
||||
"shared" => MountKind::Shared {
|
||||
share_root_uid: share_root_uid.ok_or_else(|| {
|
||||
Error::Other(format!("mount row {id} has no share_root_uid"))
|
||||
|
||||
@@ -252,9 +252,32 @@ const UID_NOW: &str = "COALESCE((SELECT uid FROM nodes WHERE lid = pending_op.li
|
||||
const PARENT_NOW: &str =
|
||||
"COALESCE((SELECT uid FROM nodes WHERE lid = pending_op.parent_lid), parent_uid)";
|
||||
|
||||
/// Whether any queued move takes a node out of the folder `source.uid`.
|
||||
/// A move updates the local node tree immediately, so its original parent
|
||||
/// comes from the queue, including while a worker is sending it to Drive.
|
||||
const MOVES_OUT_OF_FOLDER: &str = "
|
||||
COALESCE((SELECT is_dir FROM nodes WHERE uid = source.uid), 1) = 1
|
||||
AND EXISTS (
|
||||
WITH RECURSIVE folders(uid, lid) AS (
|
||||
SELECT source.uid, (SELECT lid FROM nodes WHERE uid = source.uid)
|
||||
UNION
|
||||
SELECT n.uid, n.lid FROM nodes n JOIN folders f
|
||||
ON n.parent_lid = f.lid OR (n.parent_lid IS NULL AND n.parent_uid = f.uid)
|
||||
WHERE n.is_dir = 1
|
||||
)
|
||||
SELECT 1 FROM pending_op AS move
|
||||
WHERE move.kind = 'rename'
|
||||
AND CASE WHEN json_valid(move.meta_json)
|
||||
THEN json_extract(move.meta_json, '$.original_parent_uid') END
|
||||
IN (SELECT uid FROM folders)
|
||||
AND move.parent_uid IS NOT NULL
|
||||
AND NOT EXISTS (SELECT 1 FROM folders f
|
||||
WHERE f.lid = move.parent_lid OR f.uid = move.parent_uid)
|
||||
)";
|
||||
|
||||
/// The `WHERE` terms for a row a drain worker may claim once it falls due: not
|
||||
/// claimed already, no other claimed op on the same node, and not under a
|
||||
/// folder that has yet to land.
|
||||
/// claimed already, no other claimed op on the same node, no pending creation
|
||||
/// of its parent, and no moves that still need a folder we are about to trash.
|
||||
///
|
||||
/// One fragment for [`Db::claim_next_due_op`] and [`Db::earliest_due_at`]. A
|
||||
/// row the claim skips but the wait counts is due "now" for as long as it is
|
||||
@@ -265,7 +288,9 @@ fn claimable() -> String {
|
||||
AND (parent_uid IS NULL OR substr({PARENT_NOW}, 1, {n}) <> '{v}~')
|
||||
AND uid NOT IN (SELECT uid FROM pending_op WHERE claimed_at <> 0)
|
||||
AND (lid IS NULL OR lid NOT IN (
|
||||
SELECT lid FROM pending_op WHERE claimed_at <> 0 AND lid IS NOT NULL))",
|
||||
SELECT lid FROM pending_op WHERE claimed_at <> 0 AND lid IS NOT NULL))
|
||||
AND (kind <> 'trash' OR NOT EXISTS (
|
||||
SELECT 1 FROM (SELECT {UID_NOW} AS uid) AS source WHERE {MOVES_OUT_OF_FOLDER}))",
|
||||
v = LOCAL_VOLUME,
|
||||
n = LOCAL_VOLUME.len() + 1,
|
||||
)
|
||||
@@ -475,19 +500,40 @@ pub struct AttachedBlob {
|
||||
}
|
||||
|
||||
impl Db {
|
||||
/// Whether a queued trash still holds `name` somewhere.
|
||||
/// A folder can look empty locally while Drive still holds the queued
|
||||
/// moves' source files. A trash has to go through the drain in that case.
|
||||
pub fn has_pending_moves_out_of(&self, uid: &str) -> Result<bool> {
|
||||
let conn = self.read();
|
||||
Ok(conn
|
||||
.prepare_cached(&format!(
|
||||
"SELECT EXISTS (SELECT 1 FROM (SELECT ?1 AS uid) AS source
|
||||
WHERE {MOVES_OUT_OF_FOLDER})"
|
||||
))?
|
||||
.query_row(params![uid], |row| row.get(0))?)
|
||||
}
|
||||
|
||||
/// Whether a queued trash still holds `name` in `parent_uid`.
|
||||
///
|
||||
/// A replacing rename or a delete-then-recreate queues the trash of the old
|
||||
/// node ahead of the op that wants its name. With several drain workers the
|
||||
/// later op can run first and find the name still taken; this tells it to
|
||||
/// wait for the trash rather than land under a conflict name.
|
||||
pub fn has_pending_trash_named(&self, name: &str) -> Result<bool> {
|
||||
/// wait for the trash rather than land under a conflict name. Older rows
|
||||
/// whose node and parent are unknown are matched by name alone.
|
||||
pub fn has_pending_trash_named(&self, name: &str, parent_uid: Option<&str>) -> Result<bool> {
|
||||
let conn = self.read();
|
||||
conn.query_row(
|
||||
"SELECT EXISTS(
|
||||
SELECT 1 FROM pending_op WHERE kind = ?1 AND name = ?2
|
||||
)",
|
||||
params![OP_TRASH, name],
|
||||
SELECT 1 FROM pending_op AS p
|
||||
WHERE p.kind = ?1 AND p.name = ?2
|
||||
AND (?3 IS NULL OR ?3 = COALESCE(
|
||||
(SELECT uid FROM nodes WHERE lid = p.parent_lid),
|
||||
p.parent_uid,
|
||||
(SELECT COALESCE(parent.uid, n.parent_uid)
|
||||
FROM nodes n LEFT JOIN nodes parent ON parent.lid = n.parent_lid
|
||||
WHERE n.uid = p.uid),
|
||||
?3))
|
||||
)",
|
||||
params![OP_TRASH, name, parent_uid],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.map_err(Into::into)
|
||||
@@ -1219,7 +1265,7 @@ impl Db {
|
||||
/// the claim is a column: a row is offered to exactly one worker, and the
|
||||
/// worker that took it is the only one that may retire, defer or fail it.
|
||||
///
|
||||
/// Two exclusions on top of [`next_due_op`](Self::next_due_op)'s:
|
||||
/// The selected row must also satisfy [`claimable`]:
|
||||
///
|
||||
/// * `claimed_at = 0` — not already in flight somewhere else.
|
||||
/// * no *other* claimed op shares this `uid`. Ordering only has to hold per
|
||||
@@ -1229,6 +1275,8 @@ impl Db {
|
||||
/// pending entry. `ORDER BY id` plus this exclusion gives per-uid serial
|
||||
/// order for free — the older op is claimed first and the newer one is
|
||||
/// invisible until it retires.
|
||||
/// * its destination folder already exists remotely.
|
||||
/// * a folder trash waits for pending moves out of that folder's subtree.
|
||||
///
|
||||
/// Select and mark are one transaction, so two workers cannot both see the
|
||||
/// row as unclaimed.
|
||||
|
||||
@@ -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<u32>,
|
||||
}
|
||||
|
||||
/// A town photos were taken in: its GeoNames id (see [`crate::places`]), how
|
||||
@@ -256,7 +258,7 @@ fn group_photos(entries: &[Grouped]) -> Vec<String> {
|
||||
/// 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<StoredPhoto> {
|
||||
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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -510,24 +513,42 @@ impl Db {
|
||||
range: Option<(i64, i64)>,
|
||||
favorites: bool,
|
||||
not_in_album: bool,
|
||||
) -> Result<Vec<StoredPhoto>> {
|
||||
self.photos_page_visible(offset, limit, kind, range, favorites, not_in_album, "[]")
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn photos_page_visible(
|
||||
&self,
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
kind: Option<crate::control::PhotoKind>,
|
||||
range: Option<(i64, i64)>,
|
||||
favorites: bool,
|
||||
not_in_album: bool,
|
||||
excluded: &str,
|
||||
) -> Result<Vec<StoredPhoto>> {
|
||||
let conn = self.read();
|
||||
// Built up so any combination of the optional filters is one indexed
|
||||
// query rather than a statement per case.
|
||||
let mut sql = String::from(PHOTO_SELECT);
|
||||
let mut binds: Vec<i64> = Vec::new();
|
||||
let mut binds: Vec<rusqlite::types::Value> = Vec::new();
|
||||
let mut conds: Vec<String> = Vec::new();
|
||||
if excluded != "[]" {
|
||||
binds.push(excluded.to_owned().into());
|
||||
conds.push("p.uid NOT IN (SELECT value FROM json_each(?1))".to_owned());
|
||||
}
|
||||
if kind != Some(crate::control::PhotoKind::Raw) {
|
||||
conds.push("p.uid = COALESCE(p.group_key, p.uid)".to_string());
|
||||
}
|
||||
if let Some(k) = kind {
|
||||
binds.push(k.as_i64());
|
||||
binds.push((k.as_i64()).into());
|
||||
conds.push(format!("p.kind = ?{}", binds.len()));
|
||||
}
|
||||
if let Some((from, to)) = range {
|
||||
binds.push(from);
|
||||
binds.push((from).into());
|
||||
conds.push(format!("p.capture_time >= ?{}", binds.len()));
|
||||
binds.push(to);
|
||||
binds.push((to).into());
|
||||
conds.push(format!("p.capture_time < ?{}", binds.len()));
|
||||
}
|
||||
if favorites {
|
||||
@@ -546,9 +567,9 @@ impl Db {
|
||||
sql.push_str(" WHERE ");
|
||||
sql.push_str(&conds.join(" AND "));
|
||||
}
|
||||
binds.push(limit as i64);
|
||||
binds.push((limit as i64).into());
|
||||
let limit_pos = binds.len();
|
||||
binds.push(offset as i64);
|
||||
binds.push((offset as i64).into());
|
||||
let offset_pos = binds.len();
|
||||
sql.push_str(&format!(
|
||||
" ORDER BY p.seq LIMIT ?{limit_pos} OFFSET ?{offset_pos}"
|
||||
@@ -577,6 +598,25 @@ impl Db {
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
/// Include associated files so a public preview cannot reveal a hidden
|
||||
/// RAW, JPEG or Live Photo through another part of the same shot.
|
||||
pub fn photo_group_members_for(&self, uids: &HashSet<String>) -> Result<HashSet<String>> {
|
||||
if uids.is_empty() {
|
||||
return Ok(HashSet::new());
|
||||
}
|
||||
let ids = serde_json::to_string(uids).expect("file identifiers serialize");
|
||||
let conn = self.read();
|
||||
let mut query = conn.prepare(
|
||||
"SELECT uid FROM photos WHERE COALESCE(group_key, uid) IN (
|
||||
SELECT COALESCE(group_key, uid) FROM photos
|
||||
WHERE uid IN (SELECT value FROM json_each(?1))
|
||||
)",
|
||||
)?;
|
||||
Ok(query
|
||||
.query_map([ids], |row| row.get(0))?
|
||||
.collect::<rusqlite::Result<_>>()?)
|
||||
}
|
||||
|
||||
/// The whole persisted timeline, in stored order — every file, not one row
|
||||
/// per group. Only for whole-library passes (the photo re-date); the gallery
|
||||
/// pages with [`Db::photos_page`].
|
||||
@@ -598,27 +638,33 @@ impl Db {
|
||||
pub fn photos_months(
|
||||
&self,
|
||||
kind: Option<crate::control::PhotoKind>,
|
||||
) -> Result<Vec<(i32, i32, usize)>> {
|
||||
self.photos_months_visible(kind, "[]")
|
||||
}
|
||||
|
||||
pub fn photos_months_visible(
|
||||
&self,
|
||||
kind: Option<crate::control::PhotoKind>,
|
||||
excluded: &str,
|
||||
) -> Result<Vec<(i32, i32, usize)>> {
|
||||
let conn = self.read();
|
||||
// The scrubber has to agree with the page it scrubs, so it counts what
|
||||
// the page shows: groups everywhere except on the Raw tab.
|
||||
let (filter, binds): (&str, Vec<i64>) = match kind {
|
||||
Some(crate::control::PhotoKind::Raw) => (" WHERE kind = 2", Vec::new()),
|
||||
Some(k) => (
|
||||
" WHERE uid = COALESCE(group_key, uid) AND kind = ?1",
|
||||
vec![k.as_i64()],
|
||||
),
|
||||
None => (" WHERE uid = COALESCE(group_key, uid)", Vec::new()),
|
||||
let filter = match kind {
|
||||
Some(crate::control::PhotoKind::Raw) => "kind = ?2",
|
||||
Some(_) => "uid = COALESCE(group_key, uid) AND kind = ?2",
|
||||
None => "uid = COALESCE(group_key, uid) AND ?2 IS NULL",
|
||||
};
|
||||
let sql = format!(
|
||||
"SELECT CAST(strftime('%Y', capture_time, 'unixepoch', 'localtime') AS INTEGER) AS y, \
|
||||
CAST(strftime('%m', capture_time, 'unixepoch', 'localtime') AS INTEGER) AS m, \
|
||||
COUNT(*) \
|
||||
FROM photos{filter} GROUP BY y, m ORDER BY y DESC, m DESC"
|
||||
FROM photos WHERE {filter} AND uid NOT IN (SELECT value FROM json_each(?1))
|
||||
GROUP BY y, m ORDER BY y DESC, m DESC"
|
||||
);
|
||||
let mut stmt = conn.prepare(&sql)?;
|
||||
let rows = stmt
|
||||
.query_map(rusqlite::params_from_iter(binds), |r| {
|
||||
.query_map(params![excluded, kind.map(|kind| kind.as_i64())], |r| {
|
||||
Ok((
|
||||
r.get::<_, i32>(0)?,
|
||||
r.get::<_, i32>(1)?,
|
||||
@@ -634,6 +680,15 @@ impl Db {
|
||||
/// pick the date, and its own year is left out, because today's photos are
|
||||
/// what the timeline already opens on. One row per group, like the timeline.
|
||||
pub fn photos_on_this_day(&self, today: i64, limit: usize) -> Result<Vec<StoredPhoto>> {
|
||||
self.photos_on_this_day_visible(today, limit, "[]")
|
||||
}
|
||||
|
||||
pub fn photos_on_this_day_visible(
|
||||
&self,
|
||||
today: i64,
|
||||
limit: usize,
|
||||
excluded: &str,
|
||||
) -> Result<Vec<StoredPhoto>> {
|
||||
let conn = self.read();
|
||||
let sql = format!(
|
||||
"{PHOTO_SELECT} WHERE p.uid = COALESCE(p.group_key, p.uid) \
|
||||
@@ -641,11 +696,12 @@ impl Db {
|
||||
= strftime('%m-%d', ?1, 'unixepoch', 'localtime') \
|
||||
AND strftime('%Y', p.capture_time, 'unixepoch', 'localtime') \
|
||||
< strftime('%Y', ?1, 'unixepoch', 'localtime') \
|
||||
AND p.uid NOT IN (SELECT value FROM json_each(?3))
|
||||
ORDER BY p.seq LIMIT ?2"
|
||||
);
|
||||
let mut stmt = conn.prepare(&sql)?;
|
||||
let rows = stmt
|
||||
.query_map(params![today, limit as i64], stored_photo)?
|
||||
.query_map(params![today, limit as i64, excluded], stored_photo)?
|
||||
.collect::<rusqlite::Result<_>>()?;
|
||||
Ok(rows)
|
||||
}
|
||||
@@ -654,6 +710,10 @@ impl Db {
|
||||
/// cover, most photographed first. Counted per group, like the timeline;
|
||||
/// photos without a location, or taken far from any town, are in none.
|
||||
pub fn photo_places(&self) -> Result<Vec<StoredPlace>> {
|
||||
self.photo_places_visible("[]")
|
||||
}
|
||||
|
||||
pub fn photo_places_visible(&self, excluded: &str) -> Result<Vec<StoredPlace>> {
|
||||
let rows: Vec<(u32, usize, String)> = {
|
||||
let conn = self.read();
|
||||
// SQLite fills the bare `uid` from the row `MIN(seq)` picked, which
|
||||
@@ -661,9 +721,10 @@ impl Db {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT place_id, COUNT(*), uid, MIN(seq) FROM photos \
|
||||
WHERE uid = COALESCE(group_key, uid) AND place_id IS NOT NULL \
|
||||
AND uid NOT IN (SELECT value FROM json_each(?1))
|
||||
GROUP BY place_id ORDER BY COUNT(*) DESC, MIN(seq)",
|
||||
)?;
|
||||
stmt.query_map([], |r| {
|
||||
stmt.query_map([excluded], |r| {
|
||||
Ok((r.get(0)?, r.get::<_, i64>(1)? as usize, r.get(2)?))
|
||||
})?
|
||||
.collect::<rusqlite::Result<_>>()?
|
||||
@@ -692,15 +753,29 @@ impl Db {
|
||||
id: u32,
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
) -> Result<Vec<StoredPhoto>> {
|
||||
self.photos_at_place_visible(id, offset, limit, "[]")
|
||||
}
|
||||
|
||||
pub fn photos_at_place_visible(
|
||||
&self,
|
||||
id: u32,
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
excluded: &str,
|
||||
) -> Result<Vec<StoredPhoto>> {
|
||||
let conn = self.read();
|
||||
let sql = format!(
|
||||
"{PHOTO_SELECT} WHERE p.uid = COALESCE(p.group_key, p.uid) AND p.place_id = ?1 \
|
||||
"{PHOTO_SELECT} WHERE p.uid = COALESCE(p.group_key, p.uid) AND p.place_id = ?1
|
||||
AND p.uid NOT IN (SELECT value FROM json_each(?4))
|
||||
ORDER BY p.seq LIMIT ?2 OFFSET ?3"
|
||||
);
|
||||
let mut stmt = conn.prepare(&sql)?;
|
||||
let rows = stmt
|
||||
.query_map(params![id, limit as i64, offset as i64], stored_photo)?
|
||||
.query_map(
|
||||
params![id, limit as i64, offset as i64, excluded],
|
||||
stored_photo,
|
||||
)?
|
||||
.collect::<rusqlite::Result<_>>()?;
|
||||
Ok(rows)
|
||||
}
|
||||
@@ -881,6 +956,10 @@ impl Db {
|
||||
|
||||
/// Per-tab counts for the Photos page subtitle: `(photos, videos, raw)`.
|
||||
pub fn photos_counts(&self) -> Result<(usize, usize, usize)> {
|
||||
self.photos_counts_visible("[]")
|
||||
}
|
||||
|
||||
pub fn photos_counts_visible(&self, excluded: &str) -> Result<(usize, usize, usize)> {
|
||||
use crate::control::PhotoKind;
|
||||
let conn = self.read();
|
||||
// Photos and Videos count groups — one shot is one entry, however many
|
||||
@@ -888,9 +967,12 @@ impl Db {
|
||||
// them individually.
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT kind, COUNT(*) FROM photos \
|
||||
WHERE uid = COALESCE(group_key, uid) OR kind = 2 GROUP BY kind",
|
||||
WHERE (uid = COALESCE(group_key, uid) OR kind = 2)
|
||||
AND uid NOT IN (SELECT value FROM json_each(?1)) GROUP BY kind",
|
||||
)?;
|
||||
let rows = stmt.query_map([], |r| Ok((r.get::<_, i64>(0)?, r.get::<_, i64>(1)?)))?;
|
||||
let rows = stmt.query_map([excluded], |r| {
|
||||
Ok((r.get::<_, i64>(0)?, r.get::<_, i64>(1)?))
|
||||
})?;
|
||||
let (mut photos, mut videos, mut raw) = (0usize, 0usize, 0usize);
|
||||
for row in rows {
|
||||
let (kind, n) = row?;
|
||||
|
||||
@@ -9,6 +9,8 @@ use std::collections::HashMap;
|
||||
|
||||
pub struct StoredSyncFolder {
|
||||
pub id: i64,
|
||||
/// `device` backup or `attached` My files mirror.
|
||||
pub kind: String,
|
||||
pub local_path: String,
|
||||
pub remote_uid: String,
|
||||
pub remote_share_id: String,
|
||||
@@ -59,12 +61,32 @@ impl Db {
|
||||
local_path: &str,
|
||||
remote_uid: &str,
|
||||
remote_share_id: &str,
|
||||
) -> Result<i64> {
|
||||
self.sync_folder_insert(local_path, remote_uid, remote_share_id, "device")
|
||||
}
|
||||
|
||||
/// Attach an existing My files folder, without creating a device backup.
|
||||
pub fn sync_folder_attach(
|
||||
&self,
|
||||
local_path: &str,
|
||||
remote_uid: &str,
|
||||
remote_share_id: &str,
|
||||
) -> Result<i64> {
|
||||
self.sync_folder_insert(local_path, remote_uid, remote_share_id, "attached")
|
||||
}
|
||||
|
||||
fn sync_folder_insert(
|
||||
&self,
|
||||
local_path: &str,
|
||||
remote_uid: &str,
|
||||
remote_share_id: &str,
|
||||
kind: &str,
|
||||
) -> Result<i64> {
|
||||
let conn = self.conn.lock();
|
||||
conn.execute(
|
||||
"INSERT INTO sync_folder (local_path, remote_uid, remote_share_id)
|
||||
VALUES (?1, ?2, ?3)",
|
||||
params![local_path, remote_uid, remote_share_id],
|
||||
"INSERT INTO sync_folder (local_path, remote_uid, remote_share_id, kind)
|
||||
VALUES (?1, ?2, ?3, ?4)",
|
||||
params![local_path, remote_uid, remote_share_id, kind],
|
||||
)?;
|
||||
Ok(conn.last_insert_rowid())
|
||||
}
|
||||
@@ -73,7 +95,7 @@ impl Db {
|
||||
pub fn sync_folder_list(&self) -> Result<Vec<StoredSyncFolder>> {
|
||||
let conn = self.read();
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT id, local_path, remote_uid, remote_share_id, mode, pending_mode, state, last_sync
|
||||
"SELECT id, local_path, remote_uid, remote_share_id, mode, pending_mode, state, last_sync, kind
|
||||
FROM sync_folder ORDER BY id",
|
||||
)?;
|
||||
let rows = stmt.query_map([], |r| {
|
||||
@@ -86,6 +108,7 @@ impl Db {
|
||||
pending_mode: r.get(5)?,
|
||||
state: r.get(6)?,
|
||||
last_sync: r.get(7)?,
|
||||
kind: r.get(8)?,
|
||||
})
|
||||
})?;
|
||||
let mut items = Vec::new();
|
||||
@@ -99,7 +122,7 @@ impl Db {
|
||||
pub fn sync_folder_get(&self, id: i64) -> Result<Option<StoredSyncFolder>> {
|
||||
let conn = self.read();
|
||||
conn.query_row(
|
||||
"SELECT id, local_path, remote_uid, remote_share_id, mode, pending_mode, state, last_sync
|
||||
"SELECT id, local_path, remote_uid, remote_share_id, mode, pending_mode, state, last_sync, kind
|
||||
FROM sync_folder WHERE id = ?1",
|
||||
params![id],
|
||||
|r| {
|
||||
@@ -112,6 +135,7 @@ impl Db {
|
||||
pending_mode: r.get(5)?,
|
||||
state: r.get(6)?,
|
||||
last_sync: r.get(7)?,
|
||||
kind: r.get(8)?,
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
@@ -337,6 +337,10 @@ fn places_group_photos_by_the_nearest_town() {
|
||||
.map(|p| p.uid)
|
||||
.collect();
|
||||
assert_eq!(berlin, ["b2", "b1-jpeg"]);
|
||||
let visible = db.photo_places_visible(r#"["b2","p1"]"#).unwrap();
|
||||
assert_eq!(visible.len(), 1);
|
||||
assert_eq!(visible[0].count, 1);
|
||||
assert_eq!(visible[0].cover.uid, "b1-jpeg");
|
||||
}
|
||||
|
||||
/// A town's photos come with where each was taken, one per RAW+JPEG shot.
|
||||
@@ -363,6 +367,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<Option<u32>> = 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]
|
||||
@@ -2076,7 +2097,11 @@ fn migration_v17_adds_share_access_to_a_v16_fixture() {
|
||||
{
|
||||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||||
conn.execute_batch(
|
||||
"DROP TRIGGER mount_sync_folder_insert;
|
||||
"DROP TRIGGER IF EXISTS mount_sync_folder_kind_update;
|
||||
DROP TRIGGER mount_sync_folder_insert;
|
||||
DROP TRIGGER sync_folder_attached_insert;
|
||||
DROP TRIGGER sync_folder_attached_update;
|
||||
ALTER TABLE sync_folder DROP COLUMN kind;
|
||||
DROP TABLE mount;
|
||||
DROP TABLE share_access;
|
||||
DROP TABLE album_photos;
|
||||
@@ -2719,15 +2744,34 @@ fn atomic_trash_replacement_returns_owned_blobs_after_commit() {
|
||||
#[test]
|
||||
fn a_queued_trash_holds_its_name_until_it_lands() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
db.upsert_node(&file("held-name", "root", "report.txt", 1))
|
||||
.unwrap();
|
||||
let node = uid("held-name").to_string();
|
||||
assert!(!db.has_pending_trash_named("report.txt").unwrap());
|
||||
let root = uid("root").to_string();
|
||||
assert!(
|
||||
!db.has_pending_trash_named("report.txt", Some(&root))
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let (trash_id, _) = db.replace_ops_with_trash(&node, "report.txt", 1).unwrap();
|
||||
assert!(db.has_pending_trash_named("report.txt").unwrap());
|
||||
assert!(!db.has_pending_trash_named("other.txt").unwrap());
|
||||
assert!(
|
||||
db.has_pending_trash_named("report.txt", Some(&root))
|
||||
.unwrap()
|
||||
);
|
||||
assert!(
|
||||
!db.has_pending_trash_named("other.txt", Some(&root))
|
||||
.unwrap()
|
||||
);
|
||||
assert!(
|
||||
!db.has_pending_trash_named("report.txt", Some(&uid("other").to_string()))
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
db.delete_op(trash_id).unwrap();
|
||||
assert!(!db.has_pending_trash_named("report.txt").unwrap());
|
||||
assert!(
|
||||
!db.has_pending_trash_named("report.txt", Some(&root))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
/// A create that found its name held by a queued trash backed off. The trash
|
||||
@@ -6073,7 +6117,7 @@ fn cache_eviction_candidates_are_lru_ordered_and_limited() {
|
||||
}
|
||||
db.cache_touch("other", "block", 100, 0).unwrap();
|
||||
|
||||
let batch = db.cache_eviction_candidates("blob", 3).unwrap();
|
||||
let batch = db.cache_eviction_candidates("blob", 3, 0).unwrap();
|
||||
assert_eq!(batch.len(), 3, "honours the limit");
|
||||
let keys: Vec<&str> = batch.iter().map(|(k, _)| k.as_str()).collect();
|
||||
assert_eq!(keys, ["k9", "k8", "k7"], "least-recently-accessed first");
|
||||
@@ -6904,6 +6948,54 @@ fn two_workers_never_share_a_node() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trashing_a_folder_waits_for_moves_out_of_its_subtree() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
db.upsert_nodes(&[
|
||||
folder("root", None, "My Files"),
|
||||
folder("source", Some("root"), "source"),
|
||||
folder("nested", Some("source"), "nested"),
|
||||
folder("destination", Some("root"), "destination"),
|
||||
file("photo", "destination", "photo.jpg", 1),
|
||||
])
|
||||
.unwrap();
|
||||
let source = uid("source").to_string();
|
||||
let destination = uid("destination").to_string();
|
||||
let (move_id, _) = db
|
||||
.enqueue_op(&PendingOp {
|
||||
kind: OP_RENAME.to_string(),
|
||||
uid: uid("photo").to_string(),
|
||||
parent_uid: Some(destination.clone()),
|
||||
name: Some("photo.jpg".into()),
|
||||
blob_path: None,
|
||||
meta_json: Some(
|
||||
serde_json::to_string(&RenameMeta {
|
||||
original_parent_uid: uid("nested").to_string(),
|
||||
original_name: Some("photo.jpg".into()),
|
||||
})
|
||||
.unwrap(),
|
||||
),
|
||||
next_attempt_at: 20,
|
||||
..bulk_op(0, &destination)
|
||||
})
|
||||
.unwrap();
|
||||
assert!(db.has_pending_moves_out_of(&source).unwrap());
|
||||
assert!(!db.has_pending_moves_out_of(&destination).unwrap());
|
||||
assert!(
|
||||
!db.has_pending_moves_out_of(&uid("root").to_string())
|
||||
.unwrap()
|
||||
);
|
||||
let (trash_id, _) = db.replace_ops_with_trash(&source, "source", 2).unwrap();
|
||||
|
||||
assert!(db.claim_next_due_op(10).unwrap().is_none());
|
||||
assert_eq!(db.earliest_due_at().unwrap(), Some(20));
|
||||
assert_eq!(db.claim_next_due_op(20).unwrap().unwrap().id, move_id);
|
||||
assert!(db.claim_next_due_op(20).unwrap().is_none());
|
||||
assert_eq!(db.earliest_due_at().unwrap(), None);
|
||||
db.delete_op(move_id).unwrap();
|
||||
assert_eq!(db.claim_next_due_op(20).unwrap().unwrap().id, trash_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn claims_do_not_survive_the_process_that_took_them() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
@@ -7077,6 +7169,29 @@ fn a_raw_and_its_jpeg_are_one_entry_in_the_grid() {
|
||||
["jpeg", "raw"]
|
||||
);
|
||||
assert_eq!(db.photos_group("alone").unwrap().len(), 1);
|
||||
|
||||
let hidden = db
|
||||
.photo_group_members_for(&["raw".to_owned()].into_iter().collect())
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
hidden,
|
||||
["raw".to_owned(), "jpeg".to_owned()].into_iter().collect()
|
||||
);
|
||||
let excluded = serde_json::to_string(&hidden).unwrap();
|
||||
assert_eq!(db.photos_counts_visible(&excluded).unwrap(), (1, 0, 0));
|
||||
let months = db.photos_months_visible(None, &excluded).unwrap();
|
||||
assert_eq!(months.len(), 1);
|
||||
assert_eq!(months[0].2, 1);
|
||||
let public = db
|
||||
.photos_page_visible(0, 1, None, None, false, false, &excluded)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
public
|
||||
.iter()
|
||||
.map(|photo| photo.uid.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
["alone"]
|
||||
);
|
||||
}
|
||||
|
||||
/// Pixel names the two files of one raw capture after the role each plays, so
|
||||
@@ -7691,3 +7806,69 @@ fn a_queued_op_stops_existing_once_the_node_is_deleted() {
|
||||
assert_eq!(blobs, vec!["/staging/journal"]);
|
||||
assert!(!db.op_exists(id).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attachments_have_their_own_location_and_cannot_evict_local_files() {
|
||||
use crate::mounts::MountKind;
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let id = db
|
||||
.sync_folder_attach("/work", "vol~work", "myfiles-share")
|
||||
.unwrap();
|
||||
let folder = db.sync_folder_get(id).unwrap().unwrap();
|
||||
assert_eq!(folder.kind, "attached");
|
||||
assert_eq!(folder.mode, "mirror");
|
||||
assert_eq!(db.sync_folder_list().unwrap()[0].kind, "attached");
|
||||
let locations = db.mount_list().unwrap();
|
||||
assert!(
|
||||
matches!(locations[0].kind, MountKind::Attached { sync_folder_id } if sync_folder_id == id)
|
||||
);
|
||||
assert_eq!(locations[0].local_path, "/work");
|
||||
assert_eq!(locations[0].root_share_id, "myfiles-share");
|
||||
assert!(db.sync_folder_set_mode(id, "ondemand").is_err());
|
||||
assert!(
|
||||
db.sync_folder_set_pending_mode(id, Some("ondemand"))
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(db.sync_folder_get(id).unwrap().unwrap().mode, "mirror");
|
||||
db.sync_folder_remove(id).unwrap();
|
||||
assert!(db.mount_list().unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn migration_v45_keeps_existing_device_folders_and_location_ids() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let id = db
|
||||
.sync_folder_add("/backup", "vol~backup", "share")
|
||||
.unwrap();
|
||||
db.sync_folder_set_mode(id, "ondemand").unwrap();
|
||||
db.sync_folder_set_pending_mode(id, Some("mirror")).unwrap();
|
||||
let location_id = db.mount_list().unwrap()[0].id;
|
||||
db.conn
|
||||
.lock()
|
||||
.execute_batch(
|
||||
"DROP TRIGGER sync_folder_attached_insert;
|
||||
DROP TRIGGER sync_folder_attached_update;
|
||||
DROP TRIGGER IF EXISTS mount_sync_folder_kind_update;
|
||||
DROP TRIGGER mount_sync_folder_insert;
|
||||
ALTER TABLE sync_folder DROP COLUMN kind;
|
||||
CREATE TRIGGER mount_sync_folder_insert AFTER INSERT ON sync_folder BEGIN
|
||||
INSERT INTO mount (kind, sync_folder_id) VALUES ('device', NEW.id); END;
|
||||
UPDATE sync_state SET value = '44' WHERE key = 'schema_version';",
|
||||
)
|
||||
.unwrap();
|
||||
db.migrate().unwrap();
|
||||
let folder = db.sync_folder_get(id).unwrap().unwrap();
|
||||
assert_eq!(folder.kind, "device");
|
||||
assert_eq!(folder.mode, "ondemand");
|
||||
assert_eq!(folder.pending_mode.as_deref(), Some("mirror"));
|
||||
assert_eq!(db.mount_list().unwrap()[0].id, location_id);
|
||||
let attached = db
|
||||
.sync_folder_attach("/work", "vol~work", "myfiles")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
db.sync_folder_get(attached).unwrap().unwrap().kind,
|
||||
"attached"
|
||||
);
|
||||
db.migrate().unwrap();
|
||||
assert_eq!(db.mount_list().unwrap().len(), 2);
|
||||
}
|
||||
@@ -23,12 +23,14 @@ pub mod places;
|
||||
pub mod profile;
|
||||
pub mod sdkcache;
|
||||
pub mod search;
|
||||
mod secretservice;
|
||||
pub mod service;
|
||||
pub mod shell;
|
||||
pub mod similar;
|
||||
pub mod syncignore;
|
||||
pub mod syncroot;
|
||||
pub mod takeout;
|
||||
pub mod timestamp;
|
||||
pub mod tray;
|
||||
pub mod update;
|
||||
|
||||
|
||||
@@ -81,6 +81,8 @@ pub enum MountKind {
|
||||
MyFiles,
|
||||
/// A folder owned by this machine's Proton Drive device.
|
||||
Device { sync_folder_id: i64 },
|
||||
/// A real local mirror of an existing My files folder.
|
||||
Attached { sync_folder_id: i64 },
|
||||
/// A future standalone mount rooted at a node shared with this account.
|
||||
Shared { share_root_uid: String },
|
||||
}
|
||||
@@ -88,8 +90,8 @@ pub enum MountKind {
|
||||
/// One local Proton Drive location as exposed over the control protocol.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct MountSpec {
|
||||
/// Presentation-row id. Device actions use the `sync_folder_id` carried by
|
||||
/// [`MountKind::Device`], not this id.
|
||||
/// Presentation-row id. Synced-folder actions use the `sync_folder_id`
|
||||
/// carried by [`MountKind::Device`] or [`MountKind::Attached`], not this id.
|
||||
pub id: i64,
|
||||
pub kind: MountKind,
|
||||
pub local_path: String,
|
||||
|
||||
+126
-19
@@ -27,6 +27,7 @@ use std::os::unix::process::CommandExt;
|
||||
use std::path::Path;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
use crate::control::is_video_file_name;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Environment override for the terminal used by `"terminal": true` rules.
|
||||
@@ -108,11 +109,12 @@ impl OpenWith {
|
||||
/// a Markdown file). The rules are about *what the user opened*, so the
|
||||
/// caller passes that name and the argv still gets the real path.
|
||||
pub fn command_for_named(&self, path: &Path, name: &str, is_dir: bool) -> Vec<String> {
|
||||
let kind = Kind::of(path, name, is_dir);
|
||||
for rule in &self.rules {
|
||||
if rule.command.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if !rule.patterns.iter().any(|p| matches(p, name, is_dir)) {
|
||||
if !rule.patterns.iter().any(|p| matches(p, name, kind)) {
|
||||
continue;
|
||||
}
|
||||
let argv = expand(&rule.command, path);
|
||||
@@ -204,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<OpenWith> = 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
|
||||
@@ -264,6 +263,33 @@ fn with_exec_flag(program: &str) -> Vec<String> {
|
||||
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<Vec<String>> {
|
||||
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;
|
||||
@@ -325,16 +351,46 @@ fn expand_env(token: &str) -> Option<String> {
|
||||
value.filter(|v| !v.is_empty())
|
||||
}
|
||||
|
||||
/// What is being opened, as far as the `@class` patterns care.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Kind {
|
||||
Dir,
|
||||
Video,
|
||||
File,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
/// `.ts` names TypeScript and MPEG transport streams alike, so for that
|
||||
/// one extension the size on disk decides (see
|
||||
/// [`is_video_file_name`](crate::control::is_video_file_name)). No other
|
||||
/// name costs a `stat`.
|
||||
fn of(path: &Path, name: &str, is_dir: bool) -> Kind {
|
||||
if is_dir {
|
||||
return Kind::Dir;
|
||||
}
|
||||
let size = extension(name)
|
||||
.eq_ignore_ascii_case("ts")
|
||||
.then(|| std::fs::metadata(path).ok().map(|meta| meta.len()))
|
||||
.flatten();
|
||||
if is_video_file_name(name, size) {
|
||||
Kind::Video
|
||||
} else {
|
||||
Kind::File
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `pattern` — a glob or an `@class` — matches this name.
|
||||
fn matches(pattern: &str, name: &str, is_dir: bool) -> bool {
|
||||
fn matches(pattern: &str, name: &str, kind: Kind) -> bool {
|
||||
let name_lower = name.to_lowercase();
|
||||
let file = kind == Kind::File;
|
||||
match pattern.to_lowercase().as_str() {
|
||||
"@any" => true,
|
||||
"@dir" => is_dir,
|
||||
"@text" => !is_dir && class_text(&name_lower),
|
||||
"@document" => !is_dir && class_document(&name_lower),
|
||||
"@image" => !is_dir && class_image(&name_lower),
|
||||
"@media" => !is_dir && class_media(&name_lower),
|
||||
"@dir" => kind == Kind::Dir,
|
||||
"@text" => file && class_text(&name_lower),
|
||||
"@document" => file && class_document(&name_lower),
|
||||
"@image" => file && class_image(&name_lower),
|
||||
"@media" => kind == Kind::Video || (file && class_media(&name_lower)),
|
||||
other if other.starts_with('@') => false,
|
||||
other => glob_match(other, &name_lower),
|
||||
}
|
||||
@@ -533,6 +589,37 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ts_file_opens_as_media_or_text_by_its_size() {
|
||||
let policy = OpenWith {
|
||||
terminal: Some(vec!["alacritty".into(), "-e".into()]),
|
||||
default: None,
|
||||
rules: vec![
|
||||
rule(&["@text"], &["nvim"], true),
|
||||
rule(&["@media"], &["mpv"], false),
|
||||
],
|
||||
};
|
||||
let dir = std::env::temp_dir().join(format!("pdfs-opener-ts-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let source = dir.join("blob-source");
|
||||
std::fs::write(&source, "export const answer = 42;\n").unwrap();
|
||||
let recording = dir.join("blob-recording");
|
||||
std::fs::File::create(&recording)
|
||||
.unwrap()
|
||||
.set_len(crate::control::TS_VIDEO_MIN_BYTES)
|
||||
.unwrap();
|
||||
|
||||
let source_argv = policy.command_for_named(&source, "app.ts", false);
|
||||
let recording_argv = policy.command_for_named(&recording, "recording.ts", false);
|
||||
// A file that is not on this machine yet has no size to go by.
|
||||
let unknown_argv = policy.command_for_named(&dir.join("missing"), "recording.ts", false);
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
|
||||
assert_eq!(source_argv[..3], ["alacritty", "-e", "nvim"]);
|
||||
assert_eq!(recording_argv[0], "mpv");
|
||||
assert_eq!(unknown_argv[0], "mpv");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_policy_is_xdg_open() {
|
||||
let policy = OpenWith::default();
|
||||
@@ -640,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::<String>::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()]));
|
||||
}
|
||||
}
|
||||
@@ -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<Replaced, dbus_secret_service::Error> {
|
||||
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<Path<'static>> {
|
||||
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<String> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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.<name>#`, Microsoft Office's `~$<name>`.
|
||||
".~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<String>,
|
||||
}
|
||||
|
||||
/// 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<std::io::Error> 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<Self> {
|
||||
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<bool> {
|
||||
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<bool> {
|
||||
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<String>,
|
||||
) -> std::io::Result<bool> {
|
||||
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<Gitignore>,
|
||||
@@ -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();
|
||||
|
||||
+488
-70
@@ -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 `<folder>/<file>`. A folder is an
|
||||
//! second is the service, and the rest is `<folder>/<file>`. 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<i64>,
|
||||
/// 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<i64>,
|
||||
/// 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<GeoPoint>,
|
||||
/// 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<String>,
|
||||
}
|
||||
|
||||
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<UnreadableArchive>,
|
||||
/// 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<SidecarTime>,
|
||||
#[serde(default)]
|
||||
favorited: bool,
|
||||
favorited: Option<bool>,
|
||||
/// The position as edited in Google Photos, else as the camera wrote it.
|
||||
#[serde(rename = "geoData", default)]
|
||||
geo_data: Option<SidecarGeo>,
|
||||
#[serde(rename = "geoDataExif", default)]
|
||||
geo_data_exif: Option<SidecarGeo>,
|
||||
}
|
||||
|
||||
/// A sidecar timestamp: epoch seconds, as a *string*.
|
||||
impl PhotoSidecar {
|
||||
fn location(&self) -> Option<GeoPoint> {
|
||||
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<String>,
|
||||
timestamp: Option<SidecarNumber>,
|
||||
}
|
||||
|
||||
#[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<i64> {
|
||||
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<f64>,
|
||||
#[serde(default)]
|
||||
longitude: Option<f64>,
|
||||
}
|
||||
|
||||
impl SidecarGeo {
|
||||
fn point(&self) -> Option<GeoPoint> {
|
||||
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<TakeoutScan> {
|
||||
let mut entries: Vec<RawEntry> = Vec::new();
|
||||
let mut sidecars: HashMap<String, Vec<u8>> = 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<TakeoutScan> {
|
||||
let mut entries: Vec<(String, RawEntry)> = Vec::new();
|
||||
let mut sidecars: Vec<(String, String, Vec<u8>)> = 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<TakeoutScan> {
|
||||
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<u8>)>,
|
||||
) -> (Vec<RawEntry>, HashMap<String, Vec<u8>>, usize) {
|
||||
#[derive(Deserialize)]
|
||||
struct Marker {
|
||||
#[serde(rename = "photoTakenTime")]
|
||||
photo_taken_time: Option<serde::de::IgnoredAny>,
|
||||
}
|
||||
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::<Marker>(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<RawEntry>, sidecars: &HashMap<String, Vec<u8>>) -> 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<RawEntry>, sidecars: &HashMap<String, Vec<u8>>) -> 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 `<folder>/<file>` or `<file>`.
|
||||
/// Split a path inside the archive into the localized service folder and the
|
||||
/// rest, dropping the export root (`Takeout/`): `("Google Fotos",
|
||||
/// "<folder>/<file>")`.
|
||||
///
|
||||
/// `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/<album>/<file>` then has three parts like
|
||||
// `Takeout/Google Photos/<file>`, 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<String> {
|
||||
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 `<folder>/<file>` 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<String, Vec<u8>>, 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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//! Convert the client's stored epoch seconds at the SDK boundary.
|
||||
|
||||
use proton_drive_rs::ModificationTime;
|
||||
|
||||
/// A UTC modification time, or `None` when outside the SDK's supported range.
|
||||
pub fn modification_time(seconds: i64) -> Option<ModificationTime> {
|
||||
chrono::DateTime::from_timestamp(seconds, 0).map(|time| time.fixed_offset())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn epoch_seconds_preserve_the_instant_including_before_1970() {
|
||||
for seconds in [-1, 0, 1_709_208_000, 1_782_482_460] {
|
||||
let time = modification_time(seconds).unwrap();
|
||||
assert_eq!(time.timestamp(), seconds);
|
||||
assert_eq!(time.offset().local_minus_utc(), 0);
|
||||
assert_eq!(time.timestamp_subsec_nanos(), 0);
|
||||
}
|
||||
assert_eq!(
|
||||
modification_time(-1).unwrap().to_rfc3339(),
|
||||
"1969-12-31T23:59:59+00:00"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_range_seconds_omit_the_timestamp() {
|
||||
assert!(modification_time(i64::MIN).is_none());
|
||||
assert!(modification_time(i64::MAX).is_none());
|
||||
}
|
||||
}
|
||||
@@ -14,15 +14,24 @@ pdfs-core.workspace = true
|
||||
zip.workspace = true
|
||||
sha1.workspace = true
|
||||
proton-drive-rs.workspace = true
|
||||
pg-app.workspace = true
|
||||
pg-core.workspace = true
|
||||
hyper = { version = "1", features = ["http1", "server"] }
|
||||
hyper-util = { version = "0.1", features = ["tokio"] }
|
||||
http-body-util = "0.1"
|
||||
getrandom = "0.4"
|
||||
zeroize.workspace = true
|
||||
futures.workspace = true
|
||||
async-trait.workspace = true
|
||||
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
|
||||
|
||||
|
||||
@@ -154,6 +154,7 @@ impl Core {
|
||||
album: &NodeUid,
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
excluded: &str,
|
||||
) -> CoreResult<Vec<PhotoItem>> {
|
||||
let key = album.to_string();
|
||||
let stamp = format!("{ALBUM_SYNCED_PREFIX}{key}");
|
||||
@@ -172,7 +173,7 @@ impl Core {
|
||||
|
||||
let page = self
|
||||
.db
|
||||
.album_photos_page(&key, offset, limit)
|
||||
.album_photos_page_visible(&key, offset, limit, excluded)
|
||||
.map_err(CoreError::from)?;
|
||||
Ok(page.into_iter().map(|p| self.photo_item(p)).collect())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
//! Explicitly bind an existing My files folder to a real local mirror.
|
||||
//!
|
||||
//! Unlike a FUSE mount, this never covers or evicts the local directory.
|
||||
//! The existing three-way engine performs the initial, non-destructive union.
|
||||
|
||||
use super::{Core, parse_uid, sync};
|
||||
use pdfs_core::db::StoredSyncFolder;
|
||||
use pdfs_core::{CoreError, CoreResult};
|
||||
use proton_drive_rs::proton_sdk::ids::NodeUid;
|
||||
use std::collections::HashSet;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
impl Core {
|
||||
/// A bounded, server-verified ancestry walk. Do not decide ownership or
|
||||
/// remote-root overlap from a possibly incomplete cached folder listing.
|
||||
pub(super) fn attach_ancestors(&self, start: &NodeUid) -> CoreResult<Vec<NodeUid>> {
|
||||
let mut ancestors = Vec::new();
|
||||
let mut seen = HashSet::new();
|
||||
let mut next = Some(start.clone());
|
||||
while let Some(uid) = next {
|
||||
if !seen.insert(uid.clone()) || ancestors.len() >= 256 {
|
||||
return Err(CoreError::invalid(
|
||||
"invalid or excessively deep remote folder ancestry",
|
||||
));
|
||||
}
|
||||
ancestors.push(uid.clone());
|
||||
if uid == self.primary_root_uid {
|
||||
break;
|
||||
}
|
||||
let nodes = self
|
||||
.rt
|
||||
.block_on(self.drive.enumerate_nodes(std::slice::from_ref(&uid)))
|
||||
.map_err(|e| CoreError::from_api(&e, "verify sync folder ancestry"))?;
|
||||
let node = nodes
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| CoreError::not_found("remote sync folder is missing"))?;
|
||||
if node.trashed {
|
||||
return Err(CoreError::invalid("remote sync folder is in the trash"));
|
||||
}
|
||||
next = node.parent_uid;
|
||||
}
|
||||
Ok(ancestors)
|
||||
}
|
||||
|
||||
pub(crate) fn attach_sync_folder(
|
||||
&self,
|
||||
local: &Path,
|
||||
remote: &Path,
|
||||
mountpoint: &Path,
|
||||
) -> CoreResult<StoredSyncFolder> {
|
||||
let _registration = super::devices::SYNC_ROOT_REGISTRATION.lock();
|
||||
if !self.primary {
|
||||
return Err(CoreError::invalid("attach must use My files"));
|
||||
}
|
||||
if remote.is_absolute()
|
||||
|| remote
|
||||
.components()
|
||||
.any(|c| !matches!(c, Component::Normal(_) | Component::CurDir))
|
||||
{
|
||||
return Err(CoreError::invalid(
|
||||
"remote folder must stay inside My files",
|
||||
));
|
||||
}
|
||||
let local = local
|
||||
.canonicalize()
|
||||
.map_err(|e| CoreError::invalid(format!("resolve {}: {e}", local.display())))?;
|
||||
if !local.is_dir() {
|
||||
return Err(CoreError::invalid("local sync path is not a directory"));
|
||||
}
|
||||
let existing = self
|
||||
.db
|
||||
.sync_folder_list()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?;
|
||||
let others = existing
|
||||
.iter()
|
||||
.map(|f| PathBuf::from(&f.local_path))
|
||||
.collect::<Vec<_>>();
|
||||
let mut reserved = crate::devices::sync_root_reserved();
|
||||
reserved.mountpoint = Some(
|
||||
mountpoint
|
||||
.canonicalize()
|
||||
.map_err(|e| CoreError::internal(format!("resolve mountpoint: {e}")))?,
|
||||
);
|
||||
if let Some(problem) = pdfs_core::syncroot::problem(&local, &others, &reserved) {
|
||||
return Err(CoreError::invalid(problem));
|
||||
}
|
||||
let (ino, remote_uid) = self.resolve(remote)?;
|
||||
let is_folder = self
|
||||
.state()
|
||||
.entries
|
||||
.get(&ino)
|
||||
.is_some_and(|e| e.node.is_folder() && !e.node.trashed);
|
||||
if !is_folder {
|
||||
return Err(CoreError::invalid("remote sync path is not a folder"));
|
||||
}
|
||||
let ancestors = self.attach_ancestors(&remote_uid)?;
|
||||
if remote_uid == self.primary_root_uid || !ancestors.contains(&self.primary_root_uid) {
|
||||
return Err(CoreError::invalid(
|
||||
"choose an owned folder below My files, not its root or a shared folder",
|
||||
));
|
||||
}
|
||||
for other in existing.iter().filter(|f| f.kind == "attached") {
|
||||
let uid = parse_uid(&other.remote_uid)
|
||||
.ok_or_else(|| CoreError::internal("invalid existing remote sync root"))?;
|
||||
if uid == remote_uid
|
||||
|| ancestors.contains(&uid)
|
||||
|| self.attach_ancestors(&uid)?.contains(&remote_uid)
|
||||
{
|
||||
return Err(CoreError::invalid(
|
||||
"remote folder overlaps an existing synced folder",
|
||||
));
|
||||
}
|
||||
}
|
||||
let share_id = self
|
||||
.rt
|
||||
.block_on(self.drive.context_share_id(&self.primary_root_uid))
|
||||
.map_err(|e| CoreError::from_api(&e, "resolve My files share"))?
|
||||
.to_string();
|
||||
if share_id.is_empty() {
|
||||
return Err(CoreError::invalid("My files share is unavailable"));
|
||||
}
|
||||
let id = self
|
||||
.db
|
||||
.sync_folder_attach(&local.to_string_lossy(), &remote_uid.to_string(), &share_id)
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?;
|
||||
// An empty baseline makes one-sided files new, never deletions. The
|
||||
// engine hashes matching-size files and preserves divergent versions.
|
||||
let _ = self.sync_tx.send(sync::SyncMsg::Rewatch);
|
||||
let _ = self.sync_tx.send(sync::SyncMsg::Reconcile(id));
|
||||
self.db
|
||||
.sync_folder_get(id)
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?
|
||||
.ok_or_else(|| CoreError::not_found("new sync folder disappeared"))
|
||||
}
|
||||
}
|
||||
@@ -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<u64> = 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<NodeUid, HashSet<NodeUid>>,
|
||||
/// Pinned files and folders the batch changed, for the pin keeper to
|
||||
/// check once the listings above are dropped (`keeper`).
|
||||
kept: HashSet<NodeUid>,
|
||||
}
|
||||
|
||||
/// 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<NodeUid> {
|
||||
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
|
||||
|
||||
+222
-60
@@ -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
|
||||
@@ -360,6 +396,11 @@ fn touched_topics(request: &CtlRequest) -> &'static [Topic] {
|
||||
use CtlRequest as R;
|
||||
match request {
|
||||
R::Pin { .. } | R::Unpin { .. } => &[Topic::Files],
|
||||
R::DriveGallery {
|
||||
action:
|
||||
pdfs_core::control::GalleryAction::Lock
|
||||
| pdfs_core::control::GalleryAction::Unlock { .. },
|
||||
} => &[Topic::GalleryVisibility],
|
||||
R::CreateAlbum { .. }
|
||||
| R::RenameAlbum { .. }
|
||||
| R::DeleteAlbum { .. }
|
||||
@@ -385,6 +426,7 @@ fn touched_topics(request: &CtlRequest) -> &'static [Topic] {
|
||||
R::RenameDevice { .. } | R::DeleteDevice { .. } => &[Topic::Devices],
|
||||
R::AdoptDevice { .. } | R::MigrateDevice { .. } => &[Topic::Devices, Topic::Locations],
|
||||
R::AddSyncFolder { .. }
|
||||
| R::AttachSyncFolder { .. }
|
||||
| R::RemoveSyncFolder { .. }
|
||||
| R::SetSyncFolderMode { .. }
|
||||
| R::SetSyncFolderPaused { .. }
|
||||
@@ -515,13 +557,40 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
// Registered before the request is even parsed, so a handler that wedges
|
||||
// inside the daemon shows up by name in `pdfs diagnostics`.
|
||||
let _inflight = super::diagnostics::InflightGuard::new(super::diagnostics::request_kind(&line));
|
||||
let visibility = if matches!(
|
||||
&request,
|
||||
Ok(CtlRequest::PhotosTimeline { .. }
|
||||
| CtlRequest::PhotoDuplicates
|
||||
| CtlRequest::PhotoSimilar
|
||||
| CtlRequest::PhotoPlaces
|
||||
| CtlRequest::PlacePhotos { .. }
|
||||
| CtlRequest::PlaceLocations { .. }
|
||||
| CtlRequest::PhotosOnThisDay { .. }
|
||||
| CtlRequest::PhotoGroup { .. }
|
||||
| CtlRequest::PhotoAlbums
|
||||
| CtlRequest::AlbumPhotos { .. }
|
||||
| CtlRequest::PhotoMonths { .. }
|
||||
| CtlRequest::PhotoThumbs { .. }
|
||||
| CtlRequest::OpenGalleryImage { .. }
|
||||
| CtlRequest::OpenPhoto { .. })
|
||||
) {
|
||||
core.gallery_visibility()
|
||||
} else {
|
||||
Ok(std::sync::Arc::default())
|
||||
};
|
||||
let visibility = match visibility {
|
||||
Ok(visibility) => visibility,
|
||||
Err(error) => return write_response(stream, CtlResponse::error(error)),
|
||||
};
|
||||
let excluded =
|
||||
serde_json::to_string(&visibility.media).expect("a set of file identifiers serializes");
|
||||
let response = match request {
|
||||
Ok(CtlRequest::Status) => status_response(core, username, mountpoint),
|
||||
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 {
|
||||
@@ -532,8 +601,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 {
|
||||
@@ -631,11 +700,12 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
range,
|
||||
favorites,
|
||||
not_in_album,
|
||||
&excluded,
|
||||
) {
|
||||
Ok(Some(items)) => CtlResponse::Photos {
|
||||
available: true,
|
||||
items,
|
||||
counts: core.db.photos_counts().ok(),
|
||||
counts: core.db.photos_counts_visible(&excluded).ok(),
|
||||
},
|
||||
Ok(None) => CtlResponse::Photos {
|
||||
available: false,
|
||||
@@ -656,7 +726,7 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
},
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
Ok(CtlRequest::PhotoPlaces) => match core.photo_places() {
|
||||
Ok(CtlRequest::PhotoPlaces) => match core.photo_places(&excluded) {
|
||||
Ok(items) => CtlResponse::Places {
|
||||
items,
|
||||
mapping: core.timeline_progress(),
|
||||
@@ -664,7 +734,7 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
Ok(CtlRequest::PlacePhotos { id, offset, limit }) => {
|
||||
match core.place_photos(id, offset, clamp_limit(limit)) {
|
||||
match core.place_photos(id, offset, clamp_limit(limit), &excluded) {
|
||||
Ok(items) => CtlResponse::Photos {
|
||||
available: true,
|
||||
items,
|
||||
@@ -678,7 +748,7 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
Ok(CtlRequest::PhotosOnThisDay { limit }) => {
|
||||
match core.photos_on_this_day(clamp_limit(limit)) {
|
||||
match core.photos_on_this_day(clamp_limit(limit), &excluded) {
|
||||
Ok(items) => CtlResponse::Photos {
|
||||
available: true,
|
||||
items,
|
||||
@@ -723,7 +793,10 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
// An album page is a timeline page as far as the front-end is
|
||||
// concerned, so it comes back in the same reply — with `counts`
|
||||
// unset, which describe the whole timeline rather than this album.
|
||||
Some(album) => match core.album_photos(&album, offset, clamp_limit(limit)) {
|
||||
Some(album) => match visibility
|
||||
.permits(&uid)
|
||||
.and_then(|()| core.album_photos(&album, offset, clamp_limit(limit), &excluded))
|
||||
{
|
||||
Ok(items) => CtlResponse::Photos {
|
||||
available: true,
|
||||
items,
|
||||
@@ -765,17 +838,23 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
core.remove_from_album(album, photos)
|
||||
})
|
||||
}
|
||||
Ok(CtlRequest::PhotoMonths { kind }) => match core.db.photos_months(kind) {
|
||||
Ok(months) => CtlResponse::PhotoMonths {
|
||||
months: months
|
||||
.into_iter()
|
||||
.map(|(year, month, count)| PhotoMonth { year, month, count })
|
||||
.collect(),
|
||||
},
|
||||
Err(e) => CtlResponse::error(e.into()),
|
||||
},
|
||||
Ok(CtlRequest::PhotoMonths { kind }) => {
|
||||
match core.db.photos_months_visible(kind, &excluded) {
|
||||
Ok(months) => CtlResponse::PhotoMonths {
|
||||
months: months
|
||||
.into_iter()
|
||||
.map(|(year, month, count)| PhotoMonth { year, month, count })
|
||||
.collect(),
|
||||
},
|
||||
Err(e) => CtlResponse::error(e.into()),
|
||||
}
|
||||
}
|
||||
Ok(CtlRequest::PhotoThumbs { uids }) => {
|
||||
let parsed: Vec<NodeUid> = uids.iter().filter_map(|u| parse_uid(u)).collect();
|
||||
let parsed: Vec<NodeUid> = uids
|
||||
.iter()
|
||||
.filter(|uid| !visibility.media.contains(*uid))
|
||||
.filter_map(|u| parse_uid(u))
|
||||
.collect();
|
||||
CtlResponse::Thumbs {
|
||||
items: core.photo_thumbs(&parsed),
|
||||
}
|
||||
@@ -825,7 +904,7 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
running: core.timeline_refreshing(),
|
||||
},
|
||||
Ok(CtlRequest::OpenPhoto { uid }) => match parse_uid(&uid) {
|
||||
Some(u) => match core.open_photo(&u) {
|
||||
Some(u) => match visibility.permits(&uid).and_then(|()| core.open_photo(&u)) {
|
||||
Ok(p) => CtlResponse::FilePath {
|
||||
path: p.display().to_string(),
|
||||
},
|
||||
@@ -833,6 +912,27 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
},
|
||||
None => CtlResponse::error(CoreError::invalid(format!("bad uid: {uid}"))),
|
||||
},
|
||||
Ok(CtlRequest::DriveGallery { action }) => core
|
||||
.drive_gallery(action)
|
||||
.unwrap_or_else(CtlResponse::error),
|
||||
Ok(CtlRequest::OpenGalleryVideo { uid }) => core
|
||||
.open_gallery_video(&uid)
|
||||
.unwrap_or_else(CtlResponse::error),
|
||||
Ok(CtlRequest::OpenGalleryImage { uid }) => match visibility
|
||||
.permits(&uid)
|
||||
.and_then(|()| core.open_shared_file(&uid))
|
||||
{
|
||||
Ok(path) => CtlResponse::FilePath {
|
||||
path: path.display().to_string(),
|
||||
},
|
||||
Err(error) => CtlResponse::error(error),
|
||||
},
|
||||
Ok(CtlRequest::CloseGalleryVideo { token }) => core
|
||||
.close_gallery_video(&token)
|
||||
.unwrap_or_else(CtlResponse::error),
|
||||
Ok(CtlRequest::GalleryThumbnail { uid }) => core
|
||||
.gallery_thumbnail(&uid)
|
||||
.unwrap_or_else(CtlResponse::error),
|
||||
// The scan alone walks every archive's central directory, so even the
|
||||
// dry run goes to a background thread and is read back through
|
||||
// `ImportStatus` rather than answered inline.
|
||||
@@ -840,35 +940,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<PathBuf> = archives.into_iter().map(PathBuf::from).collect();
|
||||
let missing: Vec<String> = 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 {
|
||||
@@ -938,6 +1026,8 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
});
|
||||
let metadata = proton_drive_rs::PhotoUploadMetadata {
|
||||
capture_time,
|
||||
last_modification_time: capture_time
|
||||
.and_then(pdfs_core::timestamp::modification_time),
|
||||
..Default::default()
|
||||
};
|
||||
let thumbnails = core.upload_thumbnails(Path::new(&source_path), &name);
|
||||
@@ -1293,11 +1383,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 {
|
||||
@@ -1317,10 +1405,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!(
|
||||
@@ -1397,6 +1485,20 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
message: format!("adding {local_path}"),
|
||||
}
|
||||
}
|
||||
Ok(CtlRequest::AttachSyncFolder {
|
||||
local_path,
|
||||
remote_path,
|
||||
}) => {
|
||||
let result = rel_to_mount(mountpoint, &remote_path).and_then(|remote| {
|
||||
core.attach_sync_folder(Path::new(&local_path), &remote, mountpoint)
|
||||
});
|
||||
match result {
|
||||
Ok(folder) => CtlResponse::Ok {
|
||||
message: format!("sync folder {} attached: {}", folder.id, folder.local_path),
|
||||
},
|
||||
Err(error) => CtlResponse::error(error),
|
||||
}
|
||||
}
|
||||
Ok(CtlRequest::ListSyncFolders) => match core.list_sync_folders() {
|
||||
Ok(items) => CtlResponse::SyncFolders { items },
|
||||
Err(e) => CtlResponse::error(e),
|
||||
@@ -1890,9 +1992,14 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
// The request did not parse: the caller sent something malformed.
|
||||
Err(e) => CtlResponse::error(CoreError::invalid(format!("bad request: {e}"))),
|
||||
};
|
||||
let response = filter_gallery_response(response, &visibility);
|
||||
if !touched.is_empty() && !matches!(response, CtlResponse::Error { .. }) {
|
||||
core.events.publish(touched);
|
||||
}
|
||||
write_response(stream, response);
|
||||
}
|
||||
|
||||
fn write_response(mut stream: UnixStream, response: CtlResponse) {
|
||||
let mut out = match serde_json::to_vec(&response) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
@@ -1901,10 +2008,40 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
}
|
||||
};
|
||||
out.push(b'\n');
|
||||
let mut stream = stream;
|
||||
let _ = stream.write_all(&out);
|
||||
}
|
||||
|
||||
fn filter_gallery_response(
|
||||
mut response: CtlResponse,
|
||||
visibility: &super::gallery::Visibility,
|
||||
) -> CtlResponse {
|
||||
let visible = |item: &pdfs_core::control::PhotoItem| !visibility.media.contains(&item.uid);
|
||||
match &mut response {
|
||||
CtlResponse::Photos { items, .. } => items.retain(visible),
|
||||
CtlResponse::Albums { items, .. } => {
|
||||
items.retain(|item| !visibility.albums.contains(&item.uid));
|
||||
for album in items {
|
||||
if album
|
||||
.cover_uid
|
||||
.as_ref()
|
||||
.is_some_and(|uid| visibility.media.contains(uid))
|
||||
{
|
||||
album.cover_uid = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
CtlResponse::PhotoDuplicates { sets } | CtlResponse::PhotoSimilar { sets, .. } => {
|
||||
for set in sets.iter_mut() {
|
||||
set.retain(visible);
|
||||
}
|
||||
sets.retain(|set| set.len() > 1);
|
||||
}
|
||||
CtlResponse::PhotoLocations { items } => items.retain(|located| visible(&located.photo)),
|
||||
_ => {}
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
/// Listen on the control socket, serving one command per connection, each on its
|
||||
/// own thread. Runs on its own thread; returns only if the listener itself fails.
|
||||
///
|
||||
@@ -1956,6 +2093,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<PathBuf>, dry_run: bool) -> CtlResponse {
|
||||
let missing: Vec<String> = 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::*;
|
||||
|
||||
@@ -33,6 +33,11 @@ use super::{
|
||||
now_secs, parse_uid, spawn_session, sync_folder_info, this_hostname,
|
||||
};
|
||||
|
||||
/// Registration validates local and remote root overlap before inserting a
|
||||
/// mapping. Serialize all registration paths so concurrent control requests
|
||||
/// cannot both validate against the same old set of roots.
|
||||
pub(super) static SYNC_ROOT_REGISTRATION: Mutex<()> = Mutex::new(());
|
||||
|
||||
/// A mode that this daemon is allowed to apply.
|
||||
///
|
||||
/// Persisted and control-protocol values first pass through [`MountMode`] so
|
||||
@@ -118,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}")))
|
||||
}
|
||||
|
||||
@@ -152,7 +155,9 @@ impl Core {
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?;
|
||||
for location in &mut locations {
|
||||
location.mounted = self.states.is_mounted_at(Path::new(&location.local_path));
|
||||
if let MountKind::Device { sync_folder_id } = &location.kind {
|
||||
if let MountKind::Device { sync_folder_id } | MountKind::Attached { sync_folder_id } =
|
||||
&location.kind
|
||||
{
|
||||
location.progress = progress.get(sync_folder_id).cloned();
|
||||
location.paused = self.sync_folder_paused(*sync_folder_id);
|
||||
}
|
||||
@@ -495,7 +500,7 @@ impl Core {
|
||||
}
|
||||
|
||||
/// Everything directly under a device root, trashed nodes included.
|
||||
fn device_root_nodes(&self, root_uid: &NodeUid) -> CoreResult<Vec<Node>> {
|
||||
pub(super) fn device_root_nodes(&self, root_uid: &NodeUid) -> CoreResult<Vec<Node>> {
|
||||
let uids = self
|
||||
.rt
|
||||
.block_on(self.drive.enumerate_folder_children_node_uids(root_uid))
|
||||
@@ -513,6 +518,7 @@ impl Core {
|
||||
/// tree into it once, and record the mapping. Phase 1 is a one-shot upload —
|
||||
/// the two-way engine (Phase 2) reconciles later changes.
|
||||
pub(crate) fn add_sync_folder(&self, local: &Path) -> CoreResult<StoredSyncFolder> {
|
||||
let _registration = SYNC_ROOT_REGISTRATION.lock();
|
||||
let meta = std::fs::metadata(local)
|
||||
.map_err(|e| CoreError::internal(format!("stat {}: {e}", local.display())))?;
|
||||
if !meta.is_dir() {
|
||||
@@ -699,6 +705,11 @@ impl Core {
|
||||
.sync_folder_get(id)
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?
|
||||
.ok_or_else(|| CoreError::not_found(format!("no synced folder with id {id}")))?;
|
||||
if folder.kind == "attached" && delete_remote {
|
||||
return Err(CoreError::invalid(
|
||||
"attached My files folders cannot be deleted by stopping sync; omit --delete-remote",
|
||||
));
|
||||
}
|
||||
// An `ondemand` folder *is* a FUSE mount over its local path, so dropping
|
||||
// only the row would strand the mount: the path would keep serving a
|
||||
// folder the daemon no longer tracks, and nothing would ever unmount it.
|
||||
@@ -785,6 +796,11 @@ impl Core {
|
||||
.sync_folder_get(id)
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?
|
||||
.ok_or_else(|| CoreError::not_found(format!("no synced folder with id {id}")))?;
|
||||
if folder.kind == "attached" && target_mode != ApplicableMountMode::Mirror {
|
||||
return Err(CoreError::invalid(
|
||||
"attached My files folders only support mirror mode; use the My files mount for online-only access",
|
||||
));
|
||||
}
|
||||
let current_mode = ApplicableMountMode::parse(&folder.mode)
|
||||
.map_err(|error| CoreError::invalid(format!("unsupported current mode: {error}")))?;
|
||||
// A switch waits on the folder's reconcile, which a pause skips; refuse
|
||||
@@ -960,6 +976,11 @@ impl Core {
|
||||
.sync_folder_get(id)
|
||||
.map_err(|e| SwitchBlocked::Failed(format!("db: {e:?}")))?
|
||||
.ok_or_else(|| SwitchBlocked::Failed(format!("no synced folder with id {id}")))?;
|
||||
if folder.kind == "attached" && mode != ApplicableMountMode::Mirror {
|
||||
return Err(SwitchBlocked::Failed(
|
||||
"attached My files folders only support mirror mode".into(),
|
||||
));
|
||||
}
|
||||
if !pending_intent_matches(folder.pending_mode.as_deref(), expected_pending) {
|
||||
return Err(SwitchBlocked::Superseded);
|
||||
}
|
||||
@@ -1199,7 +1220,7 @@ impl Core {
|
||||
}
|
||||
};
|
||||
for folder in folders {
|
||||
if folder.mode != "ondemand" {
|
||||
if folder.kind != "device" || folder.mode != "ondemand" {
|
||||
continue;
|
||||
}
|
||||
let Some(root_uid) = parse_uid(&folder.remote_uid) else {
|
||||
@@ -1395,6 +1416,7 @@ mod tests {
|
||||
|
||||
fn folder(local_path: &str, remote_uid: &str, mode: &str) -> StoredSyncFolder {
|
||||
StoredSyncFolder {
|
||||
kind: "device".into(),
|
||||
id: 7,
|
||||
local_path: local_path.to_string(),
|
||||
remote_uid: remote_uid.to_string(),
|
||||
|
||||
+101
-16
@@ -20,7 +20,7 @@
|
||||
//! are kept as a conflict copy rather than discarded — see
|
||||
//! [`Core::revision_conflict`].
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs::File;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
@@ -53,6 +53,21 @@ use super::{
|
||||
use proton_drive_rs::proton_sdk::api::ResponseCode;
|
||||
use proton_drive_rs::proton_sdk::error::ProtonError;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct TrashedMove(String);
|
||||
|
||||
impl std::fmt::Display for TrashedMove {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"‘{}’ is in Proton Drive Trash. Restore it before retrying this move.",
|
||||
self.0
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for TrashedMove {}
|
||||
|
||||
const DRAIN_ACCESS_RECHECK: Duration = Duration::from_secs(5);
|
||||
|
||||
/// How long an op may stay access-deferred before the deferral is reported as a
|
||||
@@ -795,13 +810,13 @@ impl Core {
|
||||
let parent = parse_node_uid(parent_str).ok_or("rename op has an unparseable parent")?;
|
||||
let name = op.name.clone().ok_or("rename op has no name")?;
|
||||
|
||||
// The node we were asked to rename may be gone or trashed by now. Either
|
||||
// way there is nothing to rename and nothing to lose — a rename holds no
|
||||
// bytes — so the op is satisfied rather than retried forever.
|
||||
// A trashed node can be restored. Only retire the operation if the node
|
||||
// is gone entirely.
|
||||
let node = match self.fetch_node_remote(&uid)? {
|
||||
Some(n) if !n.trashed => n,
|
||||
_ => {
|
||||
warn!(%uid, name, "renamed node is gone or trashed remotely; dropping the rename");
|
||||
Some(n) if n.trashed => return Err(TrashedMove(n.name).into()),
|
||||
Some(n) => n,
|
||||
None => {
|
||||
warn!(%uid, name, "renamed node is gone remotely; dropping the rename");
|
||||
self.db.delete_op(op.id)?;
|
||||
return Ok(());
|
||||
}
|
||||
@@ -827,23 +842,38 @@ impl Core {
|
||||
// no window for the requirements to go stale between two calls —
|
||||
// the race B46 queued this op to get away from.
|
||||
let target = (node.name != name).then_some(name.as_str());
|
||||
match self.move_rename_remote(&uid, &parent, target) {
|
||||
let try_move = |target| {
|
||||
self.move_rename_remote(&uid, &parent, target).map_err(
|
||||
|error| -> Box<dyn std::error::Error> {
|
||||
if (is_gone(&error) || refusal(&error) == Some(Some(SyncIssue::Access)))
|
||||
&& let Some(name) = trashed_move_folder(
|
||||
node.parent_uid.iter().chain(std::iter::once(&parent)),
|
||||
|uid| self.fetch_node_remote(uid).ok().flatten(),
|
||||
)
|
||||
{
|
||||
return TrashedMove(name).into();
|
||||
}
|
||||
error.into()
|
||||
},
|
||||
)
|
||||
};
|
||||
match try_move(target) {
|
||||
Ok(()) => name.clone(),
|
||||
// The destination holds that name already. Landing under a
|
||||
// *different* name is the non-destructive resolution: it neither
|
||||
// clobbers their file nor drops ours, and it is visible.
|
||||
Err(e) if is_already_exists(&e) => {
|
||||
Err(e) if is_already_exists(e.as_ref()) => {
|
||||
if self.name_is_held(op, Some(&parent), &name)? {
|
||||
return Err(held_by_queued_change(&name));
|
||||
}
|
||||
// What held it may have let go since Drive answered
|
||||
// (docs/BUGS.md B144).
|
||||
match self.move_rename_remote(&uid, &parent, target) {
|
||||
match try_move(target) {
|
||||
Ok(()) => name.clone(),
|
||||
Err(e) if is_already_exists(&e) => {
|
||||
Err(e) if is_already_exists(e.as_ref()) => {
|
||||
let alt = conflict_name(&name, now_secs());
|
||||
warn!(%uid, name, alt, "destination already holds that name; using a conflict name");
|
||||
self.move_rename_remote(&uid, &parent, Some(&alt))?;
|
||||
try_move(Some(&alt))?;
|
||||
self.adopt_drained_name(&uid, &alt);
|
||||
self.log_activity(
|
||||
ActivityKind::Rename,
|
||||
@@ -853,7 +883,7 @@ impl Core {
|
||||
);
|
||||
alt
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
// The destination folder is gone. Leaving the node in its current
|
||||
@@ -862,7 +892,7 @@ impl Core {
|
||||
// the user chose still applies there. Retrying the move could
|
||||
// only fail again — the folder is not coming back — and would
|
||||
// wedge the queue.
|
||||
Err(e) if is_gone(&e) => {
|
||||
Err(e) if is_gone(e.as_ref()) => {
|
||||
warn!(%uid, name, %parent, "move destination is gone; leaving the node where it is");
|
||||
let landed = match self.drain_rename_in_place(
|
||||
op,
|
||||
@@ -885,7 +915,7 @@ impl Core {
|
||||
);
|
||||
landed
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
};
|
||||
self.db.delete_op(op.id)?;
|
||||
@@ -1349,7 +1379,11 @@ impl Core {
|
||||
parent: Option<&NodeUid>,
|
||||
name: &str,
|
||||
) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
if self.db.has_pending_trash_named(name)? {
|
||||
let parent_uid = parent.map(NodeUid::to_string);
|
||||
if self
|
||||
.db
|
||||
.has_pending_trash_named(name, parent_uid.as_deref())?
|
||||
{
|
||||
return Ok(true);
|
||||
}
|
||||
if op.attempts >= NAME_HOLD_ATTEMPTS {
|
||||
@@ -2695,6 +2729,29 @@ fn moved_elsewhere(meta: &RenameMeta, node: &Node, parent: &NodeUid, name: &str)
|
||||
!at(&meta.original_parent_uid, original_name) && !at(&parent.to_string(), name)
|
||||
}
|
||||
|
||||
/// A child can have a normal metadata record even when an ancestor is trashed.
|
||||
/// Check both live parent chains before interpreting a refused move as an
|
||||
/// access problem. Shared ancestors only need to be fetched once.
|
||||
fn trashed_move_folder<'a>(
|
||||
parents: impl IntoIterator<Item = &'a NodeUid>,
|
||||
mut fetch: impl FnMut(&NodeUid) -> Option<Node>,
|
||||
) -> Option<String> {
|
||||
let mut seen = HashSet::new();
|
||||
for parent in parents {
|
||||
let mut next = Some(parent.clone());
|
||||
while let Some(uid) = next.take()
|
||||
&& seen.insert(uid.clone())
|
||||
&& let Some(node) = fetch(&uid)
|
||||
{
|
||||
if node.trashed {
|
||||
return Some(node.name);
|
||||
}
|
||||
next = node.parent_uid;
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// The answer from Drive somewhere in `e`'s chain, if it got that far.
|
||||
fn proton_error<'e>(e: &'e (dyn std::error::Error + 'static)) -> Option<&'e ProtonError> {
|
||||
std::iter::successors(Some(e), |e| e.source()).find_map(|e| e.downcast_ref::<ProtonError>())
|
||||
@@ -2704,6 +2761,9 @@ fn proton_error<'e>(e: &'e (dyn std::error::Error + 'static)) -> Option<&'e Prot
|
||||
/// could not be reached, which says nothing; otherwise the issue Drive's answer
|
||||
/// is, or `Some(None)` for a failure that is not a refusal.
|
||||
fn refusal(e: &(dyn std::error::Error + 'static)) -> Option<Option<SyncIssue>> {
|
||||
if e.is::<TrashedMove>() {
|
||||
return Some(Some(SyncIssue::Trashed));
|
||||
}
|
||||
let proton = proton_error(e)?;
|
||||
// A refused session refuses everything, so it says nothing about this
|
||||
// op either; the mount is offline for it (`Core::lost_link`).
|
||||
@@ -3111,6 +3171,31 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_refused_move_identifies_a_trashed_ancestor_on_either_side() {
|
||||
let source = folder_node("source", Some("old"));
|
||||
let destination = folder_node("destination", Some("new"));
|
||||
let mut old = folder_node("old", None);
|
||||
let mut new = folder_node("new", None);
|
||||
let find = |old: &Node, new: &Node| {
|
||||
trashed_move_folder([&source.uid, &destination.uid], |uid| {
|
||||
[&source, &destination, old, new]
|
||||
.into_iter()
|
||||
.find(|node| node.uid == *uid)
|
||||
.cloned()
|
||||
})
|
||||
};
|
||||
old.trashed = true;
|
||||
assert_eq!(find(&old, &new).as_deref(), Some("old"));
|
||||
old.trashed = false;
|
||||
new.trashed = true;
|
||||
let error = TrashedMove(find(&old, &new).unwrap());
|
||||
assert_eq!(refusal(&error), Some(Some(SyncIssue::Trashed)));
|
||||
assert!(error.to_string().contains("‘new’"));
|
||||
new.trashed = false;
|
||||
assert_eq!(find(&old, &new), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queued_cross_parent_rename_retains_viewer_source_authority_after_local_move() {
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
@@ -26,10 +26,10 @@ use futures::StreamExt as _;
|
||||
use futures::stream::BoxStream;
|
||||
use proton_drive_rs::proton_sdk::account::Quota;
|
||||
use proton_drive_rs::proton_sdk::error::Result;
|
||||
use proton_drive_rs::proton_sdk::ids::{DriveEventId, NodeUid};
|
||||
use proton_drive_rs::proton_sdk::ids::{DriveEventId, NodeUid, ShareId};
|
||||
use proton_drive_rs::{
|
||||
AvailableNameTarget, Device, DeviceType, DriveEvent, DriveEventScopeId, Node, NodeMoveItem,
|
||||
ProtonDriveClient, RevisionReader, Thumbnail,
|
||||
ProtonDriveClient, RevisionReader, Thumbnail, ThumbnailType,
|
||||
};
|
||||
|
||||
/// One outcome per node of a batch request; see [`ProtonDriveClient::trash_nodes`].
|
||||
@@ -43,6 +43,8 @@ pub(crate) type OutcomeStream<'a> = BoxStream<'a, Result<(NodeUid, Result<()>)>>
|
||||
pub(crate) trait DriveApi: Send + Sync {
|
||||
async fn get_my_files_folder(&self) -> Result<Node>;
|
||||
|
||||
async fn context_share_id(&self, uid: &NodeUid) -> Result<ShareId>;
|
||||
|
||||
async fn get_node(&self, uid: &NodeUid) -> Result<Option<Node>>;
|
||||
|
||||
async fn enumerate_nodes(&self, uids: &[NodeUid]) -> Result<Vec<Node>>;
|
||||
@@ -116,6 +118,14 @@ pub(crate) trait DriveApi: Send + Sync {
|
||||
|
||||
async fn open_revision(&self, uid: &NodeUid) -> Result<Arc<dyn RevisionRead>>;
|
||||
|
||||
/// The file's thumbnail of `thumbnail_type`, or `None` when its revision
|
||||
/// carries none.
|
||||
async fn download_thumbnail(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
thumbnail_type: ThumbnailType,
|
||||
) -> Result<Option<Vec<u8>>>;
|
||||
|
||||
async fn download_file_to_dyn(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
@@ -305,6 +315,10 @@ impl RevisionRead for RevisionReader {
|
||||
|
||||
#[async_trait]
|
||||
impl DriveApi for ProtonDriveClient {
|
||||
async fn context_share_id(&self, uid: &NodeUid) -> Result<ShareId> {
|
||||
ProtonDriveClient::context_share_id(self, uid).await
|
||||
}
|
||||
|
||||
async fn get_my_files_folder(&self) -> Result<Node> {
|
||||
ProtonDriveClient::get_my_files_folder(self).await
|
||||
}
|
||||
@@ -346,7 +360,13 @@ impl DriveApi for ProtonDriveClient {
|
||||
name: &str,
|
||||
last_modification_time: Option<i64>,
|
||||
) -> Result<NodeUid> {
|
||||
ProtonDriveClient::create_folder(self, parent_uid, name, last_modification_time).await
|
||||
ProtonDriveClient::create_folder(
|
||||
self,
|
||||
parent_uid,
|
||||
name,
|
||||
last_modification_time.and_then(pdfs_core::timestamp::modification_time),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn upload_file(
|
||||
@@ -378,7 +398,7 @@ impl DriveApi for ProtonDriveClient {
|
||||
reader,
|
||||
intended_size,
|
||||
thumbnails,
|
||||
last_modification_time,
|
||||
last_modification_time.and_then(pdfs_core::timestamp::modification_time),
|
||||
aead,
|
||||
)
|
||||
.await
|
||||
@@ -403,7 +423,7 @@ impl DriveApi for ProtonDriveClient {
|
||||
reader,
|
||||
intended_size,
|
||||
thumbnails,
|
||||
last_modification_time,
|
||||
last_modification_time.and_then(pdfs_core::timestamp::modification_time),
|
||||
aead,
|
||||
)
|
||||
.await
|
||||
@@ -423,7 +443,7 @@ impl DriveApi for ProtonDriveClient {
|
||||
reader,
|
||||
intended_size,
|
||||
thumbnails,
|
||||
last_modification_time,
|
||||
last_modification_time.and_then(pdfs_core::timestamp::modification_time),
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -433,6 +453,14 @@ impl DriveApi for ProtonDriveClient {
|
||||
Ok(Arc::new(reader))
|
||||
}
|
||||
|
||||
async fn download_thumbnail(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
thumbnail_type: ThumbnailType,
|
||||
) -> Result<Option<Vec<u8>>> {
|
||||
ProtonDriveClient::download_thumbnail(self, uid, thumbnail_type).await
|
||||
}
|
||||
|
||||
async fn download_file_to_dyn(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
|
||||
@@ -2372,17 +2372,19 @@ impl ProtonFs {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Offline: queue it. Trashing is the one mutation a user expects to work
|
||||
// regardless — the file is gone from their point of view the moment the
|
||||
// command returns. The same when the network goes
|
||||
// away under the remote call below.
|
||||
// Queue a folder trash while Drive still holds children that were moved
|
||||
// out locally. The drain waits for those moves before sending it.
|
||||
let queue_it = |reply: ReplyEmpty| match self.core.queue_trash(&uid, name, open_now) {
|
||||
Ok(()) => reply.ok(),
|
||||
Err(e) => reply.error(e),
|
||||
};
|
||||
if !self.core.sends_inline() {
|
||||
queue_it(reply);
|
||||
return;
|
||||
return queue_it(reply);
|
||||
}
|
||||
match self.core.has_pending_moves_out_of(&uid) {
|
||||
Ok(false) => {}
|
||||
Ok(true) => return queue_it(reply),
|
||||
Err(error) => return reply.error(error),
|
||||
}
|
||||
if let Err(e) = self.core.require_uid_writable(&uid) {
|
||||
reply.error(e);
|
||||
|
||||
@@ -0,0 +1,431 @@
|
||||
//! Host Proton Gallery's account and catalog services in the Drive daemon.
|
||||
|
||||
mod video;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use pdfs_core::control::{
|
||||
GalleryAction, GalleryAlbum, GalleryFolder, GalleryPlace, PhotoItem, PhotoKind, Response,
|
||||
};
|
||||
use pdfs_core::{CoreError, CoreResult};
|
||||
use pg_app::{App, View};
|
||||
use pg_core::CancellationToken;
|
||||
use pg_core::model::{AssetKey, SourceId};
|
||||
use proton_drive_rs::proton_sdk::session::ProtonApiSession;
|
||||
use tokio::sync::{Mutex, OnceCell};
|
||||
|
||||
use super::{Core, NodeUid, parse_uid};
|
||||
|
||||
pub(super) struct Gallery {
|
||||
root: PathBuf,
|
||||
session: ProtonApiSession,
|
||||
cancel: CancellationToken,
|
||||
app: OnceCell<App>,
|
||||
checked: Mutex<Option<(Instant, Option<Arc<Visibility>>)>>,
|
||||
unlocked: AtomicBool,
|
||||
video: video::Server,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Eq)]
|
||||
pub(super) struct Visibility {
|
||||
pub(super) media: HashSet<String>,
|
||||
pub(super) albums: HashSet<String>,
|
||||
}
|
||||
|
||||
impl Visibility {
|
||||
pub(super) fn permits(&self, uid: &str) -> CoreResult<()> {
|
||||
if self.media.contains(uid) || self.albums.contains(uid) {
|
||||
return Err(CoreError::invalid("Unlock hidden albums to open this item"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Gallery {
|
||||
pub(super) fn new(root: PathBuf, session: ProtonApiSession) -> Self {
|
||||
Self {
|
||||
root,
|
||||
session,
|
||||
cancel: CancellationToken::new(),
|
||||
app: OnceCell::new(),
|
||||
checked: Mutex::new(None),
|
||||
unlocked: AtomicBool::new(false),
|
||||
video: video::Server::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn cancel(&self) {
|
||||
self.cancel.cancel();
|
||||
self.video.close_all();
|
||||
}
|
||||
|
||||
async fn app(&self, core: &Core) -> CoreResult<&App> {
|
||||
self.app
|
||||
.get_or_try_init(|| async {
|
||||
App::open_hosted(
|
||||
self.root.clone(),
|
||||
self.session.clone(),
|
||||
core.client.clone(),
|
||||
self.cancel.child_token(),
|
||||
)
|
||||
.await
|
||||
.map_err(gallery_error)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn ensure_visibility(
|
||||
&self,
|
||||
core: &Core,
|
||||
app: &App,
|
||||
force: bool,
|
||||
) -> CoreResult<Arc<Visibility>> {
|
||||
if self.cancel.is_cancelled() {
|
||||
return Err(CoreError::remote("The Gallery session expired"));
|
||||
}
|
||||
let mut checked = self.checked.lock().await;
|
||||
if !force
|
||||
&& let Some((instant, Some(visibility))) = checked.as_ref()
|
||||
&& instant.elapsed() < Duration::from_secs(60)
|
||||
{
|
||||
return Ok(visibility.clone());
|
||||
}
|
||||
let previous = checked.as_ref().map(|(_, visibility)| visibility.as_ref());
|
||||
let visibility = async {
|
||||
let account = self.account()?;
|
||||
app.refresh_gallery_configuration(false)
|
||||
.await
|
||||
.map_err(gallery_error)?;
|
||||
let mut visibility = Visibility::default();
|
||||
for (volume, node) in app
|
||||
.catalog
|
||||
.hidden_assets(account.clone())
|
||||
.await
|
||||
.map_err(gallery_error)?
|
||||
{
|
||||
visibility
|
||||
.media
|
||||
.insert(NodeUid::new(volume.into(), node.into()).to_string());
|
||||
}
|
||||
visibility.media.extend(
|
||||
core.db
|
||||
.photo_group_members_for(&visibility.media)
|
||||
.map_err(CoreError::from)?,
|
||||
);
|
||||
for album in app
|
||||
.catalog
|
||||
.hidden_configuration_albums(account)
|
||||
.await
|
||||
.map_err(gallery_error)?
|
||||
{
|
||||
visibility.albums.insert(
|
||||
NodeUid::new(album.volume.as_str().into(), album.node.as_str().into())
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
Ok::<_, CoreError>(Arc::new(visibility))
|
||||
}
|
||||
.await;
|
||||
let current = visibility.as_ref().ok();
|
||||
if previous.is_some_and(|previous| previous != current) {
|
||||
self.video.close_all();
|
||||
core.events
|
||||
.publish(&[pdfs_core::control::Topic::GalleryVisibilitySynced]);
|
||||
}
|
||||
*checked = Some((Instant::now(), current.cloned()));
|
||||
visibility
|
||||
}
|
||||
|
||||
fn account(&self) -> CoreResult<pg_core::model::AccountId> {
|
||||
pg_core::model::AccountId::new(self.session.user_id().as_str()).map_err(gallery_error)
|
||||
}
|
||||
|
||||
fn lock(&self) {
|
||||
self.unlocked.store(false, Ordering::SeqCst);
|
||||
self.video.close_all();
|
||||
}
|
||||
|
||||
fn unlocked(&self) -> bool {
|
||||
self.unlocked.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
pub(super) async fn poll(self: Arc<Self>, core: Core) {
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
_ = self.cancel.cancelled() => break,
|
||||
_ = tokio::time::sleep(Duration::from_secs(60)) => {}
|
||||
}
|
||||
if let Some(app) = self.app.get() {
|
||||
let _ = self.ensure_visibility(&core, app, false).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn asset(&self, uid: &NodeUid) -> CoreResult<AssetKey> {
|
||||
Ok(AssetKey {
|
||||
account: self.account()?,
|
||||
volume: pg_core::model::VolumeId::new(uid.volume_id.as_str()).map_err(gallery_error)?,
|
||||
node: pg_core::model::NodeId::new(uid.link_id.as_str()).map_err(gallery_error)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn gallery_error(error: impl std::fmt::Display) -> CoreError {
|
||||
CoreError::remote(error.to_string())
|
||||
}
|
||||
|
||||
fn uid(value: &str) -> CoreResult<NodeUid> {
|
||||
parse_uid(value).ok_or_else(|| CoreError::invalid("Invalid gallery node"))
|
||||
}
|
||||
|
||||
fn done() -> Response {
|
||||
Response::Ok {
|
||||
message: "Gallery updated".into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn asset_uid(asset: &AssetKey) -> String {
|
||||
NodeUid::new(asset.volume.as_str().into(), asset.node.as_str().into()).to_string()
|
||||
}
|
||||
|
||||
impl Core {
|
||||
pub(super) fn gallery_visibility(&self) -> CoreResult<Arc<Visibility>> {
|
||||
if self.gallery.is_none() {
|
||||
return Ok(Arc::default());
|
||||
}
|
||||
let gallery = self.gallery_host()?;
|
||||
self.rt.block_on(async {
|
||||
let app = gallery.app(self).await?;
|
||||
let visibility = gallery.ensure_visibility(self, app, false).await?;
|
||||
Ok(if gallery.unlocked() {
|
||||
Arc::default()
|
||||
} else {
|
||||
visibility
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn gallery_host(&self) -> CoreResult<&Gallery> {
|
||||
self.gallery
|
||||
.as_deref()
|
||||
.filter(|gallery| !gallery.cancel.is_cancelled())
|
||||
.ok_or_else(|| CoreError::remote("The Gallery session is unavailable"))
|
||||
}
|
||||
|
||||
pub(super) fn drive_gallery(&self, action: GalleryAction) -> CoreResult<Response> {
|
||||
let gallery = self.gallery_host()?;
|
||||
let action = match action {
|
||||
GalleryAction::Unlock { passphrase } => {
|
||||
let passphrase = zeroize::Zeroizing::new(passphrase);
|
||||
pdfs_core::auth::unlock_gallery(gallery.session.username(), &passphrase)
|
||||
.map_err(gallery_error)?;
|
||||
gallery.unlocked.store(true, Ordering::SeqCst);
|
||||
return Ok(done());
|
||||
}
|
||||
GalleryAction::Lock => {
|
||||
gallery.lock();
|
||||
return Ok(done());
|
||||
}
|
||||
other => other,
|
||||
};
|
||||
self.rt.block_on(async {
|
||||
let app = gallery.app(self).await?;
|
||||
gallery
|
||||
.ensure_visibility(self, app, matches!(action, GalleryAction::Refresh))
|
||||
.await?;
|
||||
let unlocked = gallery.unlocked();
|
||||
match action {
|
||||
GalleryAction::Unlock { .. } | GalleryAction::Lock => unreachable!(),
|
||||
GalleryAction::List {
|
||||
album,
|
||||
offset,
|
||||
limit,
|
||||
} => {
|
||||
let parent = album
|
||||
.as_ref()
|
||||
.map(|a| (a.gallery.as_str(), a.album.as_str()));
|
||||
let albums = if offset == 0 {
|
||||
app.drive_albums(parent, unlocked)
|
||||
.await
|
||||
.map_err(gallery_error)?
|
||||
.into_iter()
|
||||
.map(|entry| GalleryAlbum {
|
||||
place: GalleryPlace {
|
||||
gallery: entry.gallery.to_string(),
|
||||
album: entry.source.id.to_string(),
|
||||
},
|
||||
folder: GalleryFolder {
|
||||
uid: NodeUid::new(
|
||||
entry.source.volume.as_str().into(),
|
||||
entry.source.root.as_str().into(),
|
||||
)
|
||||
.to_string(),
|
||||
name: entry.settings.title.unwrap_or(entry.source.label),
|
||||
},
|
||||
cover: entry.cover.map(|asset| asset_uid(&asset.key)),
|
||||
items: entry.direct_items,
|
||||
folders: entry.child_albums,
|
||||
hidden: entry.settings.hidden,
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let (items, total) = if let Some(GalleryPlace { gallery, album }) = album {
|
||||
let view = app
|
||||
.outline_with_visibility(
|
||||
View::DriveAlbum { gallery, album },
|
||||
false,
|
||||
unlocked,
|
||||
)
|
||||
.await
|
||||
.map_err(gallery_error)?;
|
||||
let total = view.len();
|
||||
let items = app
|
||||
.outline_items(&view, offset.min(total), limit.clamp(1, 250))
|
||||
.await
|
||||
.map_err(gallery_error)?
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|item| PhotoItem {
|
||||
uid: asset_uid(&item.key),
|
||||
capture_time: item.sort_time,
|
||||
thumb_path: None,
|
||||
name: Some(item.name),
|
||||
ratio: item.aspect_ratio,
|
||||
no_thumb: false,
|
||||
kind: if item.is_video {
|
||||
PhotoKind::Video
|
||||
} else {
|
||||
PhotoKind::Photo
|
||||
},
|
||||
favorite: item.favorite,
|
||||
group_size: 1,
|
||||
has_raw: false,
|
||||
place_id: None,
|
||||
})
|
||||
.collect();
|
||||
(items, total)
|
||||
} else {
|
||||
(Vec::new(), 0)
|
||||
};
|
||||
Ok(Response::GalleryPage {
|
||||
albums,
|
||||
items,
|
||||
total,
|
||||
})
|
||||
}
|
||||
GalleryAction::Folders { parent } => {
|
||||
let list = app
|
||||
.browse_drive_folders(parent.as_deref().map(uid).transpose()?)
|
||||
.await
|
||||
.map_err(gallery_error)?;
|
||||
Ok(Response::GalleryFolders {
|
||||
folders: list
|
||||
.children
|
||||
.into_iter()
|
||||
.map(|folder| GalleryFolder {
|
||||
uid: folder.uid.to_string(),
|
||||
name: folder.name,
|
||||
})
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
GalleryAction::Add { uid: node } => {
|
||||
app.add_drive_folder(uid(&node)?)
|
||||
.await
|
||||
.map_err(gallery_error)?;
|
||||
gallery.ensure_visibility(self, app, true).await?;
|
||||
Ok(done())
|
||||
}
|
||||
GalleryAction::Remove { gallery: id } => {
|
||||
app.forget_drive_gallery(SourceId::new(id).map_err(gallery_error)?)
|
||||
.await
|
||||
.map_err(gallery_error)?;
|
||||
gallery.ensure_visibility(self, app, true).await?;
|
||||
Ok(done())
|
||||
}
|
||||
GalleryAction::Refresh => {
|
||||
for album in app.drive_albums(None, true).await.map_err(gallery_error)? {
|
||||
let root = NodeUid::new(
|
||||
album.source.volume.as_str().into(),
|
||||
album.source.root.as_str().into(),
|
||||
);
|
||||
app.scan_drive_folder(root).await.map_err(gallery_error)?;
|
||||
}
|
||||
gallery.ensure_visibility(self, app, true).await?;
|
||||
Ok(done())
|
||||
}
|
||||
GalleryAction::Cover { album, uid: node } => {
|
||||
let id = SourceId::new(&album.album).map_err(gallery_error)?;
|
||||
app.outline(View::DriveAlbum {
|
||||
gallery: album.gallery,
|
||||
album: album.album,
|
||||
})
|
||||
.await
|
||||
.map_err(gallery_error)?;
|
||||
let mut settings = app
|
||||
.catalog
|
||||
.album_settings(id.clone())
|
||||
.await
|
||||
.map_err(gallery_error)?;
|
||||
settings.cover = Some(gallery.asset(&uid(&node)?)?);
|
||||
app.catalog
|
||||
.set_album_settings(id, settings)
|
||||
.await
|
||||
.map_err(gallery_error)?;
|
||||
gallery.ensure_visibility(self, app, true).await?;
|
||||
Ok(done())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn open_gallery_video(&self, node: &str) -> CoreResult<Response> {
|
||||
let gallery = self.gallery_host()?;
|
||||
let uid = uid(node)?;
|
||||
self.rt.block_on(async {
|
||||
let app = gallery.app(self).await?;
|
||||
let visibility = gallery.ensure_visibility(self, app, false).await?;
|
||||
if !gallery.unlocked() {
|
||||
visibility.permits(node)?;
|
||||
}
|
||||
let stream = app
|
||||
.stream_video(&gallery.asset(&uid)?)
|
||||
.await
|
||||
.map_err(gallery_error)?;
|
||||
gallery.video.open(stream, &gallery.cancel).await
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn close_gallery_video(&self, token: &str) -> CoreResult<Response> {
|
||||
self.gallery_host()?.video.close(token);
|
||||
Ok(done())
|
||||
}
|
||||
|
||||
pub(super) fn gallery_thumbnail(&self, node: &str) -> CoreResult<Response> {
|
||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
let gallery = self.gallery_host()?;
|
||||
let asset = gallery.asset(&uid(node)?)?;
|
||||
self.rt.block_on(async {
|
||||
let app = gallery.app(self).await?;
|
||||
let visibility = gallery.ensure_visibility(self, app, false).await?;
|
||||
if !gallery.unlocked() {
|
||||
visibility.permits(node)?;
|
||||
}
|
||||
let data = app
|
||||
.thumbnail(&asset)
|
||||
.await
|
||||
.map_err(gallery_error)?
|
||||
.map(|bytes| STANDARD.encode(bytes));
|
||||
Ok(Response::GalleryThumbnail { data })
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
use std::{collections::VecDeque, io, sync::Arc, time::Duration};
|
||||
|
||||
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use http_body_util::{BodyExt, Empty, StreamBody, combinators::UnsyncBoxBody};
|
||||
use hyper::{
|
||||
Method, Request, Response, StatusCode,
|
||||
body::{Bytes, Frame, Incoming},
|
||||
header,
|
||||
server::conn::http1,
|
||||
service::service_fn,
|
||||
};
|
||||
use hyper_util::rt::TokioIo;
|
||||
use pdfs_core::{CoreResult, control::Response as ControlResponse};
|
||||
use pg_app::video::VideoStream;
|
||||
use pg_core::CancellationToken;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
type Body = UnsyncBoxBody<Bytes, io::Error>;
|
||||
|
||||
const LEASE: Duration = Duration::from_secs(4 * 60 * 60);
|
||||
const MAX_STREAMS: usize = 4;
|
||||
const CHUNK: u32 = 256 * 1024;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Server {
|
||||
active: parking_lot::Mutex<VecDeque<(String, CancellationToken)>>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
pub(super) fn close(&self, token: &str) {
|
||||
let mut active = self.active.lock();
|
||||
if let Some(index) = active.iter().position(|(id, _)| id == token)
|
||||
&& let Some((_, cancel)) = active.remove(index)
|
||||
{
|
||||
cancel.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn close_all(&self) {
|
||||
for (_, cancel) in self.active.lock().drain(..) {
|
||||
cancel.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn open(
|
||||
&self,
|
||||
stream: Arc<VideoStream>,
|
||||
account: &CancellationToken,
|
||||
) -> CoreResult<ControlResponse> {
|
||||
let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0))
|
||||
.await
|
||||
.map_err(super::gallery_error)?;
|
||||
let address = listener.local_addr().map_err(super::gallery_error)?;
|
||||
let mut nonce = [0; 32];
|
||||
getrandom::fill(&mut nonce).map_err(super::gallery_error)?;
|
||||
let token = URL_SAFE_NO_PAD.encode(nonce);
|
||||
let path: Arc<str> = format!("/{token}").into();
|
||||
let cancel = account.child_token();
|
||||
{
|
||||
let mut active = self.active.lock();
|
||||
active.retain(|(_, cancel)| !cancel.is_cancelled());
|
||||
if active.len() >= MAX_STREAMS
|
||||
&& let Some((_, oldest)) = active.pop_front()
|
||||
{
|
||||
oldest.cancel();
|
||||
}
|
||||
active.push_back((token.clone(), cancel.clone()));
|
||||
}
|
||||
let size = stream.length();
|
||||
tokio::spawn(async move {
|
||||
let mut connections = tokio::task::JoinSet::new();
|
||||
let lifetime = tokio::time::sleep(LEASE);
|
||||
tokio::pin!(lifetime);
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
_ = cancel.cancelled() => break,
|
||||
_ = &mut lifetime => break,
|
||||
_ = connections.join_next(), if !connections.is_empty() => {},
|
||||
incoming = listener.accept(), if connections.len() < 8 => {
|
||||
let Ok((socket, _)) = incoming else { break };
|
||||
let stream = stream.clone();
|
||||
let path = path.clone();
|
||||
connections.spawn(async move {
|
||||
let service = service_fn(move |request| reply(request, stream.clone(), path.clone()));
|
||||
let _ = http1::Builder::new()
|
||||
.timer(hyper_util::rt::TokioTimer::new())
|
||||
.header_read_timeout(Duration::from_secs(10))
|
||||
.keep_alive(false)
|
||||
.serve_connection(TokioIo::new(socket), service).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
stream.stop();
|
||||
cancel.cancel();
|
||||
connections.abort_all();
|
||||
});
|
||||
Ok(ControlResponse::GalleryVideo {
|
||||
url: format!("http://{address}/{token}"),
|
||||
token,
|
||||
size,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn range(value: Option<&str>, size: u64) -> Option<(u64, u64)> {
|
||||
let Some(value) = value else {
|
||||
return Some((0, size));
|
||||
};
|
||||
let (first, last) = value.strip_prefix("bytes=")?.split_once('-')?;
|
||||
if first.is_empty() {
|
||||
let suffix = last.parse::<u64>().ok()?.min(size);
|
||||
return (suffix > 0).then_some((size - suffix, size));
|
||||
}
|
||||
let start = first.parse::<u64>().ok()?;
|
||||
let end = if last.is_empty() {
|
||||
size
|
||||
} else {
|
||||
last.parse::<u64>().ok()?.saturating_add(1).min(size)
|
||||
};
|
||||
(start < end).then_some((start, end))
|
||||
}
|
||||
|
||||
fn empty() -> Body {
|
||||
Empty::new().map_err(|never| match never {}).boxed_unsync()
|
||||
}
|
||||
|
||||
async fn reply(
|
||||
request: Request<Incoming>,
|
||||
stream: Arc<VideoStream>,
|
||||
path: Arc<str>,
|
||||
) -> Result<Response<Body>, hyper::http::Error> {
|
||||
let response = Response::builder().header(header::CACHE_CONTROL, "no-store");
|
||||
if request
|
||||
.uri()
|
||||
.path_and_query()
|
||||
.is_none_or(|actual| actual.as_str() != path.as_ref())
|
||||
{
|
||||
return response.status(StatusCode::NOT_FOUND).body(empty());
|
||||
}
|
||||
if !matches!(*request.method(), Method::HEAD | Method::GET) {
|
||||
return response
|
||||
.status(StatusCode::METHOD_NOT_ALLOWED)
|
||||
.body(empty());
|
||||
}
|
||||
let size = stream.length();
|
||||
let requested = request.headers().get(header::RANGE);
|
||||
let parsed = match requested {
|
||||
Some(value) => value
|
||||
.to_str()
|
||||
.ok()
|
||||
.and_then(|value| range(Some(value), size)),
|
||||
None => range(None, size),
|
||||
};
|
||||
let Some((start, end)) = parsed else {
|
||||
return response
|
||||
.status(StatusCode::RANGE_NOT_SATISFIABLE)
|
||||
.header(header::CONTENT_RANGE, format!("bytes */{size}"))
|
||||
.body(empty());
|
||||
};
|
||||
let response = response
|
||||
.header(header::ACCEPT_RANGES, "bytes")
|
||||
.header(header::CONTENT_LENGTH, end - start)
|
||||
.header(header::CONTENT_TYPE, "application/octet-stream");
|
||||
let response = if requested.is_some() {
|
||||
response.status(StatusCode::PARTIAL_CONTENT).header(
|
||||
header::CONTENT_RANGE,
|
||||
format!("bytes {start}-{}/{size}", end - 1),
|
||||
)
|
||||
} else {
|
||||
response
|
||||
};
|
||||
if request.method() == Method::HEAD {
|
||||
return response.body(empty());
|
||||
}
|
||||
let body = futures::stream::try_unfold((stream, start), move |(stream, at)| async move {
|
||||
if at == end {
|
||||
return Ok(None);
|
||||
}
|
||||
let read = (end - at).min(u64::from(CHUNK)) as u32;
|
||||
let bytes = stream.read(at, read).await.map_err(io::Error::other)?;
|
||||
if bytes.is_empty() {
|
||||
return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
|
||||
}
|
||||
let next = at + bytes.len() as u64;
|
||||
Ok(Some((Frame::data(Bytes::from(bytes)), (stream, next))))
|
||||
});
|
||||
response.body(BodyExt::boxed_unsync(StreamBody::new(body)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::range;
|
||||
|
||||
#[test]
|
||||
fn resolves_seek_ranges_and_rejects_invalid_ranges() {
|
||||
assert_eq!(range(None, 10), Some((0, 10)));
|
||||
assert_eq!(range(Some("bytes=3-5"), 10), Some((3, 6)));
|
||||
assert_eq!(range(Some("bytes=9-"), 10), Some((9, 10)));
|
||||
assert_eq!(range(Some("bytes=-3"), 10), Some((7, 10)));
|
||||
assert_eq!(range(Some("bytes=10-"), 10), None);
|
||||
assert_eq!(range(Some("bytes=0-1,3-4"), 10), None);
|
||||
}
|
||||
}
|
||||
@@ -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<NodeUid>,
|
||||
}
|
||||
|
||||
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<NodeUid> = 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<u64> = HashSet::new();
|
||||
let mut files: Vec<u64> = 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<KeptFile> = 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<KeptFile> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
+78
-20
@@ -79,6 +79,7 @@ use proton_drive_rs::{
|
||||
};
|
||||
|
||||
mod albums;
|
||||
mod attach;
|
||||
mod background;
|
||||
mod conflicts;
|
||||
mod control;
|
||||
@@ -90,6 +91,8 @@ mod drive;
|
||||
use drive::{DriveApi, RevisionRead};
|
||||
mod events;
|
||||
mod filesystem;
|
||||
mod gallery;
|
||||
mod keeper;
|
||||
mod link;
|
||||
pub use filesystem::ProtonFs;
|
||||
mod mount;
|
||||
@@ -118,13 +121,14 @@ mod workers;
|
||||
use background::{forget_and_notify, run_event_sync, run_local_index, run_photos_event_sync};
|
||||
use link::BATCH_CALL_TIMEOUT;
|
||||
pub(crate) use mount::is_stale_mount;
|
||||
pub use mount::prepare_mountpoint;
|
||||
pub use mount::{MountOptions, MountOutcome, mount};
|
||||
use mount::{
|
||||
SecondaryInsertRejection, SecondaryMount, clear_stale_mount, fuse_connection_id, spawn_session,
|
||||
};
|
||||
use reads::{BlockFlight, BlockRing, PREFETCH_BUDGET, Prefetch, ReaderSlot, STREAM_BYPASS_MIN};
|
||||
use state::{Entry, Intervals, PendingRevision, State, StateGuard, WriteHandle};
|
||||
pub use systemd::{idle_sleep, status as notify_status};
|
||||
pub use systemd::{idle_sleep, status as notify_status, stretch_watchdog_for_startup};
|
||||
use tracing::{debug, error, info, warn};
|
||||
use transfers::{CountingWriter, JobGuard, OwnedCountingReader, TransferRegistry};
|
||||
use r#virtual::{
|
||||
@@ -496,6 +500,7 @@ struct Core {
|
||||
/// The concrete client, for what [`DriveApi`] does not cover: sharing,
|
||||
/// Photos, devices, invitations and public links.
|
||||
client: ProtonDriveClient,
|
||||
gallery: Option<Arc<gallery::Gallery>>,
|
||||
rt: tokio::runtime::Handle,
|
||||
/// The daemon's My Files root. Forked on-demand sessions retain this value
|
||||
/// so ownership checks never mistake their own mount root for account scope.
|
||||
@@ -587,6 +592,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<bool>, Condvar)>,
|
||||
/// The pins the keeper owes a check (`keeper`).
|
||||
pin_work: Arc<Mutex<keeper::PinWork>>,
|
||||
/// 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<shutdown::Shutdown>,
|
||||
@@ -3163,6 +3170,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<NodeUid> {
|
||||
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<NodeUid> {
|
||||
let state = self.state();
|
||||
let entry = state
|
||||
@@ -3985,6 +4014,15 @@ impl Core {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn has_pending_moves_out_of(&self, uid: &NodeUid) -> Result<bool, Errno> {
|
||||
self.db
|
||||
.has_pending_moves_out_of(&uid.to_string())
|
||||
.map_err(|error| {
|
||||
error!(%uid, %error, "checking for moves out of a trash target failed");
|
||||
Errno::EIO
|
||||
})
|
||||
}
|
||||
|
||||
/// Queue trashing a node the server knows about, and drop it from the tree
|
||||
/// now.
|
||||
///
|
||||
@@ -4268,7 +4306,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<String> {
|
||||
///
|
||||
/// `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<String> {
|
||||
let (ino, uid) = self.resolve(rel)?;
|
||||
let (name, is_folder, mtime, size) = {
|
||||
let st = self.state();
|
||||
@@ -4292,7 +4335,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();
|
||||
@@ -4302,9 +4345,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)
|
||||
}
|
||||
@@ -4322,14 +4371,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();
|
||||
}
|
||||
@@ -4363,9 +4418,9 @@ impl Core {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Every file in the subtree rooted at folder `ino`.
|
||||
fn pin_subtree_files(&self, ino: u64) -> CoreResult<Vec<(NodeUid, String, i64, u64)>> {
|
||||
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<Vec<u64>> {
|
||||
let mut files: Vec<u64> = Vec::new();
|
||||
let mut stack = vec![ino];
|
||||
while let Some(dir) = stack.pop() {
|
||||
if self.shutdown.is_stopping() {
|
||||
@@ -4380,12 +4435,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5351,6 +5401,11 @@ impl Core {
|
||||
&parent_uid,
|
||||
)
|
||||
.map_err(|error| self.errno_error(error, "trash access"))?;
|
||||
if self.db.has_pending_moves_out_of(&uid.to_string())? {
|
||||
return Err(CoreError::conflict(
|
||||
"wait for pending moves out of this folder to sync before trashing it",
|
||||
));
|
||||
}
|
||||
self.rt
|
||||
.block_on(self.drive.trash_nodes(std::slice::from_ref(&uid)))
|
||||
.and_then(batch::into_unit)
|
||||
@@ -5415,11 +5470,13 @@ impl Core {
|
||||
} else {
|
||||
uid
|
||||
};
|
||||
// Local-first or offline, or the network goes away under the call:
|
||||
// queue the trash. The rename that follows queues as well, behind it.
|
||||
// Local-first or offline: queue the trash and the rename that follows.
|
||||
if !self.sends_inline() {
|
||||
return self.queue_trash(uid, name, open);
|
||||
}
|
||||
if self.has_pending_moves_out_of(uid)? {
|
||||
return Err(Errno::EBUSY);
|
||||
}
|
||||
self.require_uid_writable(uid)?;
|
||||
if let Err(e) = self
|
||||
.block_on_bounded(self.drive.trash_nodes(std::slice::from_ref(uid)))
|
||||
@@ -6441,6 +6498,7 @@ fn sync_folder_info(
|
||||
) -> SyncFolderInfo {
|
||||
SyncFolderInfo {
|
||||
id: f.id,
|
||||
kind: f.kind,
|
||||
local_path: f.local_path,
|
||||
remote_uid: f.remote_uid,
|
||||
mode: f.mode,
|
||||
|
||||
@@ -232,6 +232,9 @@ impl Core {
|
||||
/// Unless the keyring holds a newer session than the refused one: then
|
||||
/// the mount ends, for the daemon to mount again on that one.
|
||||
pub(crate) fn mark_session_expired(&self, what: &str, error: &dyn std::fmt::Display) {
|
||||
if let Some(gallery) = &self.gallery {
|
||||
gallery.cancel();
|
||||
}
|
||||
if self.link.resume_stored.load(Ordering::Relaxed) {
|
||||
return self.mark_offline(what, error);
|
||||
}
|
||||
@@ -286,6 +289,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.
|
||||
|
||||
@@ -196,6 +196,20 @@ pub(super) fn clear_stale_mount(mountpoint: &Path) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Make sure `mountpoint` is a directory a mount can go on, clearing a dead
|
||||
/// mount a killed daemon left there first.
|
||||
///
|
||||
/// A dead mount answers `ENOTCONN`, which `create_dir_all` takes for "absent";
|
||||
/// it then tries `mkdir` and fails with `EEXIST`. The daemon retried that error
|
||||
/// forever and never got as far as [`check_mountpoint`], which would have
|
||||
/// cleared the mount (`docs/BUGS.md` B279).
|
||||
pub fn prepare_mountpoint(mountpoint: &Path) -> std::io::Result<()> {
|
||||
if is_stale_mount(mountpoint) {
|
||||
clear_stale_mount(mountpoint);
|
||||
}
|
||||
std::fs::create_dir_all(mountpoint)
|
||||
}
|
||||
|
||||
/// Refuse to mount My files over a folder that already has files in it: FUSE
|
||||
/// would hide them, unsent and out of sight, until the service stops
|
||||
/// (`docs/BUGS.md` B277). Runs before the root fetch and before any worker
|
||||
@@ -296,6 +310,7 @@ enum Start {
|
||||
pub struct MountOptions {
|
||||
/// Proton account the session belongs to, for display and per-user paths.
|
||||
pub username: String,
|
||||
pub gallery_session: Option<proton_drive_rs::proton_sdk::session::ProtonApiSession>,
|
||||
/// What the background conflict sweep may do. See `docs/BUGS.md` B71.
|
||||
pub sweep_mode: SweepMode,
|
||||
/// Upload and download caps in bytes per second (`0` = no cap).
|
||||
@@ -442,6 +457,7 @@ pub(crate) fn mount_with(
|
||||
) -> std::io::Result<MountOutcome> {
|
||||
let MountOptions {
|
||||
username,
|
||||
gallery_session,
|
||||
sweep_mode,
|
||||
upload_limit,
|
||||
download_limit,
|
||||
@@ -466,9 +482,13 @@ pub(crate) fn mount_with(
|
||||
let (sync_tx, sync_rx) = std::sync::mpsc::channel::<sync::SyncMsg>();
|
||||
|
||||
let paused_until = pause::load_paused_until(&db);
|
||||
let gallery_dir = control_socket.with_file_name("gallery-catalog");
|
||||
let gallery =
|
||||
gallery_session.map(|session| Arc::new(super::gallery::Gallery::new(gallery_dir, session)));
|
||||
let core = Core {
|
||||
drive,
|
||||
client,
|
||||
gallery,
|
||||
rt: rt.clone(),
|
||||
maintenance: Arc::new(Mutex::new(Default::default())),
|
||||
primary_root_uid: root.uid.clone(),
|
||||
@@ -497,6 +517,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())),
|
||||
@@ -607,9 +628,23 @@ 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) {
|
||||
if let Some(gallery) = &core.gallery {
|
||||
rt.spawn(gallery.clone().poll(core.clone()));
|
||||
}
|
||||
let db = core.db.clone();
|
||||
let indexing = core.indexing.clone();
|
||||
let transfers = core.transfers.clone();
|
||||
@@ -928,6 +963,9 @@ fn keep_or_abandon(
|
||||
/// Signal every worker to stop, then join them up to [`WORKER_JOIN_DEADLINE`].
|
||||
fn halt_workers(core: &Core, control_socket: &Path, workers: Vec<std::thread::JoinHandle<()>>) {
|
||||
core.shutdown.stop();
|
||||
if let Some(gallery) = &core.gallery {
|
||||
gallery.cancel();
|
||||
}
|
||||
core.wake_drain();
|
||||
let _ = core.sync_tx.send(sync::SyncMsg::Stop);
|
||||
// Connect while the socket still exists — that is the whole point of the
|
||||
|
||||
+1191
-82
File diff suppressed because it is too large.
Load diff
@@ -141,19 +141,11 @@ impl Core {
|
||||
|
||||
/// Snapshot the local arrangement into a [`Profile`].
|
||||
fn build_profile(&self, device_uid: &str, machine_id: &str) -> CoreResult<Profile> {
|
||||
let folders = self
|
||||
.db
|
||||
.sync_folder_list()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?
|
||||
.into_iter()
|
||||
.map(|f| ProfileFolder {
|
||||
remote_uid: f.remote_uid,
|
||||
local_path: f.local_path,
|
||||
// A queued switch is where the user asked the folder to be, so
|
||||
// that is what the next machine should set up.
|
||||
mode: MountMode::from(f.pending_mode.unwrap_or(f.mode).as_str()),
|
||||
})
|
||||
.collect();
|
||||
let folders = profile_folders(
|
||||
self.db
|
||||
.sync_folder_list()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?,
|
||||
);
|
||||
let pins = self
|
||||
.db
|
||||
.pin_list()
|
||||
@@ -389,6 +381,7 @@ impl Core {
|
||||
items: &[pdfs_core::control::RestoreItem],
|
||||
device: Option<&str>,
|
||||
) -> CoreResult<RestoreOutcome> {
|
||||
let _registration = super::devices::SYNC_ROOT_REGISTRATION.lock();
|
||||
if items.is_empty() {
|
||||
return Err(CoreError::invalid("nothing to restore"));
|
||||
}
|
||||
@@ -398,6 +391,11 @@ impl Core {
|
||||
.sync_folder_list()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?;
|
||||
|
||||
// A caller must not smuggle a My files root into a device mapping,
|
||||
// where destructive device-only operations would otherwise be allowed.
|
||||
let device_root = super::parse_uid(&device.root_uid)
|
||||
.ok_or_else(|| CoreError::internal("invalid device root"))?;
|
||||
let children = self.device_root_nodes(&device_root)?;
|
||||
let reserved = crate::devices::sync_root_reserved();
|
||||
let mut roots: Vec<PathBuf> = existing
|
||||
.iter()
|
||||
@@ -406,6 +404,18 @@ impl Core {
|
||||
let mut restored = 0usize;
|
||||
let mut skipped: Vec<String> = Vec::new();
|
||||
for item in items {
|
||||
if !children.iter().any(|node| {
|
||||
node.uid.to_string() == item.remote_uid
|
||||
&& node.is_folder()
|
||||
&& !node.trashed
|
||||
&& node.name != PROFILE_DIR_NAME
|
||||
}) {
|
||||
skipped.push(format!(
|
||||
"{}: not a folder under this device",
|
||||
item.local_path
|
||||
));
|
||||
continue;
|
||||
}
|
||||
// The path is proposed from the profile another machine wrote, and
|
||||
// may hold a folder name from Drive: checked like any new root.
|
||||
let local = match pdfs_core::syncroot::resolve(Path::new(&item.local_path)) {
|
||||
@@ -468,6 +478,19 @@ impl Core {
|
||||
}
|
||||
}
|
||||
|
||||
/// Only device backups belong in the device's cross-machine profile.
|
||||
fn profile_folders(folders: Vec<pdfs_core::db::StoredSyncFolder>) -> Vec<ProfileFolder> {
|
||||
folders
|
||||
.into_iter()
|
||||
.filter(|f| f.kind == "device")
|
||||
.map(|f| ProfileFolder {
|
||||
remote_uid: f.remote_uid,
|
||||
local_path: f.local_path,
|
||||
mode: MountMode::from(f.pending_mode.unwrap_or(f.mode).as_str()),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// What a restore did: how many folders it attached, and why it left out the
|
||||
/// others.
|
||||
pub(crate) struct RestoreOutcome {
|
||||
@@ -530,6 +553,21 @@ fn default_local_path(name: &str) -> PathBuf {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn attached_my_files_folders_are_not_in_the_device_profile() {
|
||||
let dir = crate::sim::daemon::scratch("profile-folder-kinds");
|
||||
let db = pdfs_core::db::Db::open(&dir.join("cache.db")).unwrap();
|
||||
db.sync_folder_add("/backup", "vol~backup", "device-share")
|
||||
.unwrap();
|
||||
db.sync_folder_attach("/work", "vol~work", "my-files-share")
|
||||
.unwrap();
|
||||
let folders = profile_folders(db.sync_folder_list().unwrap());
|
||||
assert_eq!(folders.len(), 1);
|
||||
assert_eq!(folders[0].remote_uid, "vol~backup");
|
||||
drop(db);
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_home_is_not_reused() {
|
||||
assert!(!path_is_usable_here(Path::new(
|
||||
|
||||
@@ -200,6 +200,7 @@ impl Core {
|
||||
let reader = CountingReader::new(std::io::BufReader::new(file), &guard);
|
||||
let metadata = PhotoUploadMetadata {
|
||||
capture_time: Some(candidate.wanted),
|
||||
last_modification_time: pdfs_core::timestamp::modification_time(candidate.wanted),
|
||||
..Default::default()
|
||||
};
|
||||
let uploaded = self.rt.block_on(photos.upload_photo_from(
|
||||
|
||||
@@ -555,6 +555,7 @@ mod tests {
|
||||
|
||||
fn folder(id: i64, local_path: &str, mode: &str) -> StoredSyncFolder {
|
||||
StoredSyncFolder {
|
||||
kind: "device".into(),
|
||||
id,
|
||||
local_path: local_path.to_string(),
|
||||
remote_uid: format!("vol~root{id}"),
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<ShareEntry>, Option<PublicLinkInfo>)> {
|
||||
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<i64>,
|
||||
) -> CoreResult<PublicLinkInfo> {
|
||||
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<String>,
|
||||
expires: LinkChange<i64>,
|
||||
) -> CoreResult<PublicLinkInfo> {
|
||||
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<StopSharingOutcome> {
|
||||
let uid = self.resolve_anywhere(uid)?;
|
||||
let uid = self.resolve_shareable(uid)?;
|
||||
self.stop_sharing_for_uid(&uid)
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,7 @@ impl Daemon {
|
||||
db,
|
||||
MountOptions {
|
||||
username: "sim".into(),
|
||||
gallery_session: None,
|
||||
sweep_mode: AppConfig::default().resolved_conflict_sweep(),
|
||||
upload_limit: 0,
|
||||
download_limit: 0,
|
||||
@@ -1917,6 +1918,68 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"]
|
||||
fn a_move_blocked_by_trash_can_be_retried_after_restore() {
|
||||
use pdfs_core::control::SyncIssue;
|
||||
|
||||
let drive = FakeDrive::new();
|
||||
let dir = scratch("move-out-of-trash");
|
||||
let daemon = Daemon::start(&dir, drive.client(1, Faults::none())).unwrap();
|
||||
let mount = &daemon.mountpoint;
|
||||
std::fs::create_dir_all(mount.join("source/nested")).unwrap();
|
||||
std::fs::create_dir(mount.join("destination")).unwrap();
|
||||
std::fs::write(mount.join("source/nested/wallpaper.jpg"), b"image").unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || daemon.drained()
|
||||
&& drive.lookup("source/nested/wallpaper.jpg").is_some()));
|
||||
let source = drive.lookup("source").unwrap();
|
||||
|
||||
let pause = |paused| Request::SetSyncPaused {
|
||||
paused,
|
||||
until: None,
|
||||
};
|
||||
daemon.request(&pause(true)).unwrap();
|
||||
std::fs::rename(
|
||||
mount.join("source/nested/wallpaper.jpg"),
|
||||
mount.join("destination/wallpaper.jpg"),
|
||||
)
|
||||
.unwrap();
|
||||
drive.device().trash("source");
|
||||
daemon.request(&pause(false)).unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || daemon
|
||||
.pending()
|
||||
.is_ok_and(|ops| ops.iter().any(|op| op.issue
|
||||
== Some(SyncIssue::Trashed)
|
||||
&& op
|
||||
.last_error
|
||||
.as_deref()
|
||||
.is_some_and(|message| message.contains("‘source’"))))));
|
||||
|
||||
daemon
|
||||
.request(&Request::RetryPendingOp { id: None })
|
||||
.unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || daemon
|
||||
.pending()
|
||||
.is_ok_and(|ops| ops.iter().any(|op| op.attempts >= 2))));
|
||||
daemon
|
||||
.request(&Request::Restore {
|
||||
uids: vec![source.to_string()],
|
||||
})
|
||||
.unwrap();
|
||||
daemon
|
||||
.request(&Request::RetryPendingOp { id: None })
|
||||
.unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || daemon.drained()
|
||||
&& drive.lookup("destination/wallpaper.jpg").is_some()));
|
||||
assert_eq!(
|
||||
std::fs::read(mount.join("destination/wallpaper.jpg")).unwrap(),
|
||||
b"image"
|
||||
);
|
||||
|
||||
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_write_over_a_create_whose_read_back_failed_is_not_a_conflict() {
|
||||
@@ -2912,6 +2975,433 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run explicitly on an isolated fake Drive"]
|
||||
fn attaching_my_files_preserves_local_files_and_syncs_both_ways() {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let drive = FakeDrive::new();
|
||||
let dir = scratch("attach-my-files");
|
||||
let daemon = Daemon::start(&dir, drive.client(1, Faults::lan())).unwrap();
|
||||
std::fs::create_dir(daemon.mountpoint.join("Work")).unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
daemon.pending().is_ok_and(|items| items.is_empty()) && drive.lookup("Work").is_some()
|
||||
}));
|
||||
drive.device().write("Work/cloud.txt", b"from cloud");
|
||||
drive.device().write("Work/common.txt", b"remote version");
|
||||
drive.device().write("Work/identical.txt", b"same bytes");
|
||||
let local = dir.join("local");
|
||||
std::fs::create_dir(&local).unwrap();
|
||||
std::fs::write(local.join("local.txt"), b"only on this computer").unwrap();
|
||||
std::fs::write(local.join("common.txt"), b"local version").unwrap();
|
||||
std::fs::write(local.join("identical.txt"), b"same bytes").unwrap();
|
||||
daemon
|
||||
.request(&Request::AttachSyncFolder {
|
||||
local_path: local.to_str().unwrap().into(),
|
||||
remote_path: "Work".into(),
|
||||
})
|
||||
.unwrap();
|
||||
let folder = match daemon.request(&Request::ListSyncFolders).unwrap() {
|
||||
Response::SyncFolders { items } => {
|
||||
items
|
||||
.into_iter()
|
||||
.find(|f| f.local_path == local.to_str().unwrap())
|
||||
.unwrap()
|
||||
.id
|
||||
}
|
||||
other => panic!("unexpected reply: {other:?}"),
|
||||
};
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
drive.lookup("Work/local.txt").is_some()
|
||||
&& std::fs::read(local.join("cloud.txt")).is_ok_and(|b| b == b"from cloud")
|
||||
&& std::fs::read(local.join("common.txt")).is_ok_and(|b| b == b"remote version")
|
||||
&& !conflict_copies(&local, "common").is_empty()
|
||||
}));
|
||||
assert_ne!(
|
||||
std::fs::metadata(&local).unwrap().dev(),
|
||||
std::fs::metadata(&daemon.mountpoint).unwrap().dev()
|
||||
);
|
||||
assert!(
|
||||
conflict_copies(&local, "common")
|
||||
.iter()
|
||||
.any(|p| std::fs::read(p).is_ok_and(|b| b == b"local version"))
|
||||
);
|
||||
assert!(
|
||||
daemon
|
||||
.request(&Request::AttachSyncFolder {
|
||||
local_path: local.to_str().unwrap().into(),
|
||||
remote_path: "Work".into()
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
let duplicate = dir.join("duplicate");
|
||||
std::fs::create_dir(&duplicate).unwrap();
|
||||
assert!(
|
||||
daemon
|
||||
.request(&Request::AttachSyncFolder {
|
||||
local_path: duplicate.to_str().unwrap().into(),
|
||||
remote_path: "Work".into()
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
daemon
|
||||
.request(&Request::AttachSyncFolder {
|
||||
local_path: duplicate.to_str().unwrap().into(),
|
||||
remote_path: "../Work".into()
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
// Parent/child mappings, the account root and files cannot be attached.
|
||||
std::fs::create_dir(daemon.mountpoint.join("Work/Sub")).unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
daemon.pending().is_ok_and(|items| items.is_empty())
|
||||
&& drive.lookup("Work/Sub").is_some()
|
||||
}));
|
||||
for remote in ["Work/Sub", ".", "Work/cloud.txt"] {
|
||||
assert!(
|
||||
daemon
|
||||
.request(&Request::AttachSyncFolder {
|
||||
local_path: duplicate.to_str().unwrap().into(),
|
||||
remote_path: remote.into(),
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
// Local overlap and symlink aliases into the actual cloud mount are refused.
|
||||
let alias = dir.join("cloud-alias");
|
||||
std::os::unix::fs::symlink(daemon.mountpoint.join("Work"), &alias).unwrap();
|
||||
std::fs::create_dir_all(local.join("Sub")).unwrap();
|
||||
for path in [
|
||||
local.join("Sub"),
|
||||
dir.clone(),
|
||||
daemon.mountpoint.join("Work"),
|
||||
alias,
|
||||
] {
|
||||
let error = daemon
|
||||
.request(&Request::AttachSyncFolder {
|
||||
local_path: path.to_string_lossy().into_owned(),
|
||||
remote_path: "Work/Sub".into(),
|
||||
})
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(error.contains("overlaps"), "{error}");
|
||||
}
|
||||
assert!(conflict_copies(&local, "identical").is_empty());
|
||||
assert_eq!(
|
||||
std::fs::read(local.join("identical.txt")).unwrap(),
|
||||
b"same bytes"
|
||||
);
|
||||
// Simultaneous requests cannot register two local trees for one cloud root.
|
||||
std::fs::create_dir(daemon.mountpoint.join("Other")).unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
daemon.pending().is_ok_and(|items| items.is_empty()) && drive.lookup("Other").is_some()
|
||||
}));
|
||||
let candidate_a = dir.join("candidate-a");
|
||||
let candidate_b = dir.join("candidate-b");
|
||||
std::fs::create_dir(&candidate_a).unwrap();
|
||||
std::fs::create_dir(&candidate_b).unwrap();
|
||||
let outcomes = std::thread::scope(|scope| {
|
||||
let a = scope.spawn(|| {
|
||||
daemon
|
||||
.request(&Request::AttachSyncFolder {
|
||||
local_path: candidate_a.to_str().unwrap().into(),
|
||||
remote_path: "Other".into(),
|
||||
})
|
||||
.map_err(|e| e.to_string())
|
||||
});
|
||||
let b = scope.spawn(|| {
|
||||
daemon
|
||||
.request(&Request::AttachSyncFolder {
|
||||
local_path: candidate_b.to_str().unwrap().into(),
|
||||
remote_path: "Other".into(),
|
||||
})
|
||||
.map_err(|e| e.to_string())
|
||||
});
|
||||
[a.join().unwrap(), b.join().unwrap()]
|
||||
});
|
||||
assert_eq!(
|
||||
outcomes.iter().filter(|result| result.is_ok()).count(),
|
||||
1,
|
||||
"exactly one mapping may own a remote root: {outcomes:?}"
|
||||
);
|
||||
drive
|
||||
.device()
|
||||
.write("Work/cloud.txt", b"new cloud revision");
|
||||
daemon
|
||||
.request(&Request::SyncNow { id: Some(folder) })
|
||||
.unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || std::fs::read(
|
||||
local.join("cloud.txt")
|
||||
)
|
||||
.is_ok_and(|b| b == b"new cloud revision")));
|
||||
std::fs::write(local.join("local.txt"), b"new local revision").unwrap();
|
||||
daemon
|
||||
.request(&Request::SyncNow { id: Some(folder) })
|
||||
.unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || drive
|
||||
.tree()
|
||||
.get("Work/local.txt")
|
||||
== Some(&Entry::File(Arc::new(b"new local revision".to_vec())))));
|
||||
assert!(wait_until(Duration::from_secs(30), || listed_folder(
|
||||
&daemon, folder
|
||||
)
|
||||
.is_some_and(|f| f.state == "idle")));
|
||||
daemon
|
||||
.request(&Request::SetSyncFolderPaused {
|
||||
id: folder,
|
||||
paused: true,
|
||||
})
|
||||
.unwrap();
|
||||
let before = drive.tree();
|
||||
for entry in std::fs::read_dir(&local).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
if path.is_dir() {
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
} else {
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
}
|
||||
daemon
|
||||
.request(&Request::SetSyncFolderPaused {
|
||||
id: folder,
|
||||
paused: false,
|
||||
})
|
||||
.unwrap();
|
||||
daemon
|
||||
.request(&Request::SyncNow { id: Some(folder) })
|
||||
.unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || listed_folder(
|
||||
&daemon, folder
|
||||
)
|
||||
.is_some_and(|f| f.state == "error")));
|
||||
assert_eq!(
|
||||
drive.tree(),
|
||||
before,
|
||||
"the wipe guard must preserve the remote tree"
|
||||
);
|
||||
assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown)));
|
||||
let db = Db::open(&dir.join("pdfs.db")).unwrap();
|
||||
assert_eq!(
|
||||
db.sync_folder_get(folder).unwrap().unwrap().remote_share_id,
|
||||
"my-files-share"
|
||||
);
|
||||
drop(db);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
fn attached_my_files(daemon: &Daemon, drive: &FakeDrive, local: &Path) -> i64 {
|
||||
std::fs::create_dir(daemon.mountpoint.join("Work")).unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
daemon.drained() && drive.lookup("Work").is_some()
|
||||
}));
|
||||
std::fs::create_dir(local).unwrap();
|
||||
daemon
|
||||
.request(&Request::AttachSyncFolder {
|
||||
local_path: local.to_string_lossy().into_owned(),
|
||||
remote_path: "Work".into(),
|
||||
})
|
||||
.unwrap();
|
||||
match daemon.request(&Request::ListSyncFolders).unwrap() {
|
||||
Response::SyncFolders { items } => {
|
||||
items
|
||||
.into_iter()
|
||||
.find(|f| f.local_path == local.to_string_lossy())
|
||||
.unwrap()
|
||||
.id
|
||||
}
|
||||
other => panic!("unexpected reply: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run explicitly on an isolated fake Drive"]
|
||||
fn attached_my_files_views_preserve_concurrent_edits() {
|
||||
let drive = FakeDrive::new();
|
||||
let dir = scratch("attach-views");
|
||||
let daemon = Daemon::start(&dir, drive.client(1, Faults::lan())).unwrap();
|
||||
let local = dir.join("local");
|
||||
let id = attached_my_files(&daemon, &drive, &local);
|
||||
let mounted = daemon.mountpoint.join("Work");
|
||||
// Prime the mount listing before a mirror upload, to catch stale listings.
|
||||
assert_eq!(std::fs::read_dir(&mounted).unwrap().count(), 0);
|
||||
std::fs::write(local.join("mirror.txt"), b"mirror bytes").unwrap();
|
||||
daemon.request(&Request::SyncNow { id: Some(id) }).unwrap();
|
||||
assert!(
|
||||
wait_until(Duration::from_secs(30), || {
|
||||
std::fs::read_dir(&mounted)
|
||||
.unwrap()
|
||||
.any(|e| e.unwrap().file_name() == "mirror.txt")
|
||||
&& std::fs::read(mounted.join("mirror.txt")).is_ok_and(|b| b == b"mirror bytes")
|
||||
}),
|
||||
"mirror upload must reach the mounted view without a restart"
|
||||
);
|
||||
std::fs::write(mounted.join("mount.txt"), b"mounted bytes").unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || daemon.drained()));
|
||||
daemon.request(&Request::SyncNow { id: Some(id) }).unwrap();
|
||||
assert!(
|
||||
wait_until(Duration::from_secs(30), || {
|
||||
std::fs::read(local.join("mount.txt")).is_ok_and(|b| b == b"mounted bytes")
|
||||
&& listed_folder(&daemon, id).is_some_and(|f| f.state == "idle")
|
||||
}),
|
||||
"mounted writes must reach the mirror"
|
||||
);
|
||||
|
||||
daemon
|
||||
.request(&Request::SetSyncFolderPaused { id, paused: true })
|
||||
.unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
listed_folder(&daemon, id).is_some_and(|f| f.state != "syncing")
|
||||
}));
|
||||
std::fs::write(mounted.join("mount.txt"), b"mount changed").unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || daemon.drained()));
|
||||
std::fs::write(local.join("mount.txt"), b"local changed").unwrap();
|
||||
daemon
|
||||
.request(&Request::SetSyncFolderPaused { id, paused: false })
|
||||
.unwrap();
|
||||
daemon.request(&Request::SyncNow { id: Some(id) }).unwrap();
|
||||
assert!(
|
||||
wait_until(Duration::from_secs(30), || {
|
||||
std::fs::read(local.join("mount.txt")).is_ok_and(|b| b == b"mount changed")
|
||||
&& conflict_copies(&local, "mount")
|
||||
.iter()
|
||||
.any(|p| std::fs::read(p).is_ok_and(|b| b == b"local changed"))
|
||||
&& drive.tree().iter().any(|(path, entry)| {
|
||||
path.starts_with("Work/mount (sync-conflict")
|
||||
&& entry == &Entry::File(Arc::new(b"local changed".to_vec()))
|
||||
})
|
||||
}),
|
||||
"both edits must survive the next pass"
|
||||
);
|
||||
|
||||
let error = daemon
|
||||
.request(&Request::SetSyncFolderMode {
|
||||
id,
|
||||
mode: "ondemand".into(),
|
||||
})
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(error.contains("only support mirror"), "{error}");
|
||||
let error = daemon
|
||||
.request(&Request::RemoveSyncFolder {
|
||||
id,
|
||||
delete_remote: true,
|
||||
})
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(error.contains("cannot be deleted"), "{error}");
|
||||
let folder = listed_folder(&daemon, id).unwrap();
|
||||
assert_eq!(folder.kind, "attached");
|
||||
assert_eq!(folder.mode, "mirror");
|
||||
assert!(folder.pending_mode.is_none());
|
||||
// Restore must not reclassify a My files root as a device backup,
|
||||
// which would make the device-only destructive operations available.
|
||||
let restore_path = dir.join("forged-device-restore");
|
||||
daemon
|
||||
.request(&Request::RestoreSyncFolders {
|
||||
items: vec![control::RestoreItem {
|
||||
remote_uid: folder.remote_uid.clone(),
|
||||
local_path: restore_path.to_string_lossy().into_owned(),
|
||||
mode: "ondemand".into(),
|
||||
}],
|
||||
})
|
||||
.unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
matches!(daemon.request(&Request::ListActivity { limit: 100 }),
|
||||
Ok(Response::Activity { items }) if items.iter().any(|item| {
|
||||
!item.ok && item.detail.contains("not a folder under this device")
|
||||
}))
|
||||
}));
|
||||
assert!(!restore_path.exists());
|
||||
match daemon.request(&Request::ListLocations).unwrap() {
|
||||
Response::Locations { items } => assert!(items.iter().any(|m| {
|
||||
matches!(m.kind, pdfs_core::mounts::MountKind::Attached { sync_folder_id } if sync_folder_id == id)
|
||||
})),
|
||||
other => panic!("unexpected reply: {other:?}"),
|
||||
}
|
||||
daemon
|
||||
.request(&Request::RemoveSyncFolder {
|
||||
id,
|
||||
delete_remote: false,
|
||||
})
|
||||
.unwrap();
|
||||
assert!(listed_folder(&daemon, id).is_none());
|
||||
assert!(drive.lookup("Work/mount.txt").is_some());
|
||||
assert_eq!(
|
||||
std::fs::read(local.join("mount.txt")).unwrap(),
|
||||
b"mount changed"
|
||||
);
|
||||
daemon.stop().unwrap();
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run explicitly on an isolated fake Drive"]
|
||||
fn attached_my_files_trashed_root_keeps_local_files() {
|
||||
attached_root_loss_keeps_local_files(false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run explicitly on an isolated fake Drive"]
|
||||
fn attached_my_files_root_moved_outside_my_files_keeps_local_files() {
|
||||
attached_root_loss_keeps_local_files(true);
|
||||
}
|
||||
|
||||
fn attached_root_loss_keeps_local_files(move_outside: bool) {
|
||||
let drive = FakeDrive::new();
|
||||
let dir = scratch("attach-root-gone");
|
||||
let daemon = Daemon::start(&dir, drive.client(1, Faults::lan())).unwrap();
|
||||
let local = dir.join("local");
|
||||
let id = attached_my_files(&daemon, &drive, &local);
|
||||
std::fs::write(local.join("keep.txt"), b"keep my bytes").unwrap();
|
||||
daemon.request(&Request::SyncNow { id: Some(id) }).unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
drive.lookup("Work/keep.txt").is_some()
|
||||
&& listed_folder(&daemon, id).is_some_and(|f| f.state == "idle")
|
||||
}));
|
||||
daemon
|
||||
.request(&Request::SetSyncFolderPaused { id, paused: true })
|
||||
.unwrap();
|
||||
if move_outside {
|
||||
use crate::drive::DriveApi;
|
||||
let rt = daemon.rt.as_ref().unwrap();
|
||||
let device = rt
|
||||
.block_on(
|
||||
daemon
|
||||
.client
|
||||
.create_device("other computer", proton_drive_rs::DeviceType::Linux),
|
||||
)
|
||||
.unwrap();
|
||||
let root = drive.lookup("Work").unwrap();
|
||||
rt.block_on(daemon.client.move_node(&root, &device.root_folder_uid))
|
||||
.unwrap();
|
||||
} else {
|
||||
drive.device().trash("Work");
|
||||
}
|
||||
daemon
|
||||
.request(&Request::SetSyncFolderPaused { id, paused: false })
|
||||
.unwrap();
|
||||
daemon.request(&Request::SyncNow { id: Some(id) }).unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
listed_folder(&daemon, id).is_some_and(|f| f.state == "error")
|
||||
}));
|
||||
assert_eq!(
|
||||
std::fs::read(local.join("keep.txt")).unwrap(),
|
||||
b"keep my bytes"
|
||||
);
|
||||
assert_eq!(std::fs::read_dir(&local).unwrap().count(), 1);
|
||||
let daemon = daemon.restart().unwrap();
|
||||
daemon.request(&Request::SyncNow { id: Some(id) }).unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
listed_folder(&daemon, id).is_some_and(|f| f.state == "error")
|
||||
}));
|
||||
assert_eq!(
|
||||
std::fs::read(local.join("keep.txt")).unwrap(),
|
||||
b"keep my bytes"
|
||||
);
|
||||
daemon.stop().unwrap();
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
/// Register `local` as a synced folder in `mode` and wait until the daemon
|
||||
/// lists it that way, returning its id.
|
||||
fn sync_folder(daemon: &Daemon, local: &Path, mode: &str) -> i64 {
|
||||
@@ -3020,6 +3510,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<control::SyncFolderInfo> {
|
||||
match daemon.request(&Request::ListSyncFolders) {
|
||||
@@ -3192,4 +3794,192 @@ mod tests {
|
||||
assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown)));
|
||||
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
|
||||
/// downloaded at all, and a `.ts` that is TypeScript gets no thumbnail.
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"]
|
||||
fn a_video_thumbnail_downloads_a_small_part_of_the_video() {
|
||||
if std::process::Command::new("ffmpeg")
|
||||
.arg("-version")
|
||||
.output()
|
||||
.is_err()
|
||||
{
|
||||
eprintln!("skipped: needs ffmpeg");
|
||||
return;
|
||||
}
|
||||
let dir = scratch("video-thumbs");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
// Twenty seconds at 30 Mbit/s: larger than a thumbnail downloads whole.
|
||||
// An MP4 written this way has its index at the end.
|
||||
let encode = |name: &str| {
|
||||
let path = dir.join(name);
|
||||
let status = std::process::Command::new("ffmpeg")
|
||||
.args(["-nostdin", "-loglevel", "error", "-y", "-f", "lavfi"])
|
||||
.args(["-i", "testsrc2=size=1280x720:rate=30", "-t", "20"])
|
||||
.args(["-c:v", "libx264", "-preset", "ultrafast"])
|
||||
.args(["-b:v", "30M", "-maxrate", "30M", "-bufsize", "30M"])
|
||||
.arg(&path)
|
||||
.status()
|
||||
.unwrap();
|
||||
assert!(status.success());
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
std::fs::remove_file(&path).unwrap();
|
||||
assert!(bytes.len() > 30 << 20, "{name} is {} bytes", bytes.len());
|
||||
bytes
|
||||
};
|
||||
let (recording, clip) = (encode("recording.ts"), encode("clip.mp4"));
|
||||
let source = b"export const answer = 42;\n".repeat(64 * 1024);
|
||||
|
||||
let drive = FakeDrive::new();
|
||||
drive.device().write("recording.ts", &recording);
|
||||
drive.device().write("clip.mp4", &clip);
|
||||
drive.device().write("app.ts", &source);
|
||||
let phone = drive.device().write("phone.mp4", &clip);
|
||||
let mut frame = Vec::new();
|
||||
image::codecs::jpeg::JpegEncoder::new(&mut frame)
|
||||
.encode_image(&image::RgbImage::new(320, 180))
|
||||
.unwrap();
|
||||
let client = drive.client(1, Faults::lan());
|
||||
client.set_thumbnail(&phone, frame);
|
||||
let daemon = Daemon::start(&dir.join("daemon"), client.clone()).unwrap();
|
||||
let Ok(Response::Entries { entries }) = daemon.request(&Request::ListDir {
|
||||
path: String::new(),
|
||||
}) else {
|
||||
panic!("listing");
|
||||
};
|
||||
let items: Vec<control::FileThumbRequest> = entries
|
||||
.iter()
|
||||
.filter(|entry| !entry.is_dir)
|
||||
.map(|entry| control::FileThumbRequest {
|
||||
uid: entry.uid.clone(),
|
||||
modified: entry.modified,
|
||||
name: entry.name.clone(),
|
||||
})
|
||||
.collect();
|
||||
let Ok(Response::FileThumbGeneration { generation }) =
|
||||
daemon.request(&Request::ReserveFileThumbGeneration)
|
||||
else {
|
||||
panic!("generation");
|
||||
};
|
||||
let read_before = client.content_read();
|
||||
|
||||
let mut paths = std::collections::HashMap::new();
|
||||
let settled = wait_until(Duration::from_secs(120), || {
|
||||
let Ok(Response::Thumbs { items: replies }) = daemon.request(&Request::FileThumbs {
|
||||
items: items.clone(),
|
||||
generation,
|
||||
}) else {
|
||||
return false;
|
||||
};
|
||||
paths = replies
|
||||
.iter()
|
||||
.filter_map(|reply| {
|
||||
let name = &items.iter().find(|item| item.uid == reply.uid)?.name;
|
||||
Some((name.clone(), (reply.path.clone(), reply.pending)))
|
||||
})
|
||||
.collect();
|
||||
paths.values().all(|(_, pending)| !pending) && paths.len() == 4
|
||||
});
|
||||
assert!(settled, "{paths:?}");
|
||||
let read = client.content_read() - read_before;
|
||||
|
||||
for name in ["recording.ts", "clip.mp4", "phone.mp4"] {
|
||||
assert!(paths[name].0.is_some(), "{name} has a thumbnail: {paths:?}");
|
||||
}
|
||||
assert_eq!(paths["app.ts"].0, None, "TypeScript is no video");
|
||||
// Two 16 MiB heads, one MP4 index and the TypeScript file, and nothing
|
||||
// of the video that came with a thumbnail.
|
||||
assert!(
|
||||
read < 34 << 20,
|
||||
"downloaded {} MiB for three thumbnails",
|
||||
read >> 20
|
||||
);
|
||||
assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown)));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,7 @@ use proton_drive_rs::proton_sdk::ids::{
|
||||
};
|
||||
use proton_drive_rs::{
|
||||
AvailableNameTarget, Device as Computer, DeviceType, DriveEvent, DriveEventScopeId, Node,
|
||||
NodeKind, NodeMoveItem, RevisionState, Thumbnail,
|
||||
NodeKind, NodeMoveItem, RevisionState, Thumbnail, ThumbnailType,
|
||||
};
|
||||
use sha1::{Digest as _, Sha1};
|
||||
|
||||
@@ -160,6 +160,17 @@ impl Server {
|
||||
.is_some_and(|remote| remote.file.is_none() && !remote.trashed)
|
||||
}
|
||||
|
||||
fn in_trash(&self, uid: &NodeUid) -> bool {
|
||||
let mut node = self.current(uid);
|
||||
while let Some(remote) = node {
|
||||
if remote.trashed {
|
||||
return true;
|
||||
}
|
||||
node = remote.parent.as_ref().and_then(|uid| self.current(uid));
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn is_below(&self, uid: &NodeUid, ancestor: &NodeUid) -> bool {
|
||||
let mut cursor = Some(uid.clone());
|
||||
while let Some(at) = cursor {
|
||||
@@ -396,6 +407,8 @@ impl FakeDrive {
|
||||
hold_answer: Mutex::new(None),
|
||||
hold_trash: Mutex::new(None),
|
||||
hold_download: Mutex::new(None),
|
||||
thumbnails: Mutex::new(HashMap::new()),
|
||||
content_read: AtomicU64::new(0),
|
||||
hold_any_landing: AtomicBool::new(false),
|
||||
hold_landing: Mutex::new(None),
|
||||
drop_after_create: AtomicBool::new(false),
|
||||
@@ -677,6 +690,10 @@ struct ClientInner {
|
||||
hold_trash: Mutex<Option<Arc<HoldState>>>,
|
||||
/// The download [`FakeClient::hold_next_download`] is waiting for.
|
||||
hold_download: Mutex<Option<Arc<HoldState>>>,
|
||||
/// Thumbnails set by [`FakeClient::set_thumbnail`].
|
||||
thumbnails: Mutex<HashMap<NodeUid, Vec<u8>>>,
|
||||
/// File content bytes served, by downloads and ranged reads alike.
|
||||
content_read: AtomicU64,
|
||||
/// The landing [`FakeClient::hold_next_landing`] is waiting for.
|
||||
hold_landing: Mutex<Option<Arc<HoldState>>>,
|
||||
/// Set by [`FakeClient::hold_next_landing_of_any`]: hold whichever node
|
||||
@@ -821,6 +838,16 @@ impl FakeClient {
|
||||
|
||||
/// Hold the next download of `uid`'s content once Drive has answered it,
|
||||
/// before the bytes reach the daemon, until the answer is dropped.
|
||||
/// Give `uid` a server thumbnail, as an upload that carried one would.
|
||||
pub(crate) fn set_thumbnail(&self, uid: &NodeUid, bytes: Vec<u8>) {
|
||||
self.0.thumbnails.lock().insert(uid.clone(), bytes);
|
||||
}
|
||||
|
||||
/// File content bytes served so far, by whole downloads and ranged reads.
|
||||
pub(crate) fn content_read(&self) -> u64 {
|
||||
self.0.content_read.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
pub(crate) fn hold_next_download(&self, uid: &NodeUid) -> Held {
|
||||
let state = Arc::new(HoldState {
|
||||
uid: uid.clone(),
|
||||
@@ -1021,6 +1048,12 @@ impl FakeClient {
|
||||
}
|
||||
let parent = new_parent.cloned().or(remote.parent.clone());
|
||||
let name = new_name.unwrap_or(&remote.name).to_owned();
|
||||
if server.in_trash(uid) || parent.as_ref().is_some_and(|uid| server.in_trash(uid)) {
|
||||
return Err(api(
|
||||
ResponseCode::NotEnoughPermissions,
|
||||
"an item involved in the move is in Trash",
|
||||
));
|
||||
}
|
||||
if let Some(parent) = &parent {
|
||||
if !server.is_folder(parent) {
|
||||
return Err(api(
|
||||
@@ -1123,6 +1156,14 @@ fn delete_one(server: &mut Server, uid: &NodeUid) -> Result<()> {
|
||||
|
||||
#[async_trait]
|
||||
impl DriveApi for FakeClient {
|
||||
async fn context_share_id(&self, uid: &NodeUid) -> Result<ShareId> {
|
||||
self.request(false, |server| {
|
||||
assert_eq!(uid, &server.root);
|
||||
Ok(ShareId::new("my-files-share"))
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn get_my_files_folder(&self) -> Result<Node> {
|
||||
self.request(false, |server| {
|
||||
let root = server.root.clone();
|
||||
@@ -1467,6 +1508,21 @@ impl DriveApi for FakeClient {
|
||||
}))
|
||||
}
|
||||
|
||||
async fn download_thumbnail(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
_thumbnail_type: ThumbnailType,
|
||||
) -> Result<Option<Vec<u8>>> {
|
||||
self.request(false, |server| {
|
||||
server
|
||||
.current(uid)
|
||||
.map(|_| ())
|
||||
.ok_or_else(|| api(ResponseCode::DoesNotExist, "file not found"))
|
||||
})
|
||||
.await?;
|
||||
Ok(self.0.thumbnails.lock().get(uid).cloned())
|
||||
}
|
||||
|
||||
async fn download_file_to_dyn(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
@@ -1482,6 +1538,9 @@ impl DriveApi for FakeClient {
|
||||
.await?;
|
||||
self.wait_if_held(&self.0.hold_download, std::slice::from_ref(uid))
|
||||
.await;
|
||||
self.0
|
||||
.content_read
|
||||
.fetch_add(content.len() as u64, Ordering::SeqCst);
|
||||
output.write_all(&content).map_err(io_error)
|
||||
}
|
||||
|
||||
@@ -1725,13 +1784,19 @@ impl RevisionRead for FakeRevision {
|
||||
|
||||
async fn read_at(&self, offset: u64, length: u64) -> Result<Vec<u8>> {
|
||||
let content = self.content.clone();
|
||||
self.client
|
||||
let read = self
|
||||
.client
|
||||
.request(false, move |_| {
|
||||
let start = (offset as usize).min(content.len());
|
||||
let end = (offset.saturating_add(length) as usize).min(content.len());
|
||||
Ok(content[start..end].to_vec())
|
||||
})
|
||||
.await
|
||||
.await?;
|
||||
self.client
|
||||
.0
|
||||
.content_read
|
||||
.fetch_add(read.len() as u64, Ordering::SeqCst);
|
||||
Ok(read)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -112,6 +112,8 @@ struct Run {
|
||||
/// Run `profile` on `seed`. It answers a report of what went wrong, if
|
||||
/// anything did.
|
||||
pub(crate) fn run(seed: u64, profile: &'static Profile) -> Result<(), String> {
|
||||
// The simulation suite runs serially. Discard logs left by earlier tests.
|
||||
take_logged();
|
||||
let dir = scratch(&format!("{}-{seed}", profile.name));
|
||||
let mut rng = Rng::new(seed);
|
||||
let drive = FakeDrive::new();
|
||||
@@ -137,6 +139,7 @@ pub(crate) fn run(seed: u64, profile: &'static Profile) -> Result<(), String> {
|
||||
let _ = daemon.stop();
|
||||
}
|
||||
}
|
||||
run.logged.extend(take_logged());
|
||||
match outcome {
|
||||
Ok(()) => {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
@@ -432,6 +435,7 @@ impl Run {
|
||||
|
||||
fn report(&self, failure: &str, dir: &Path) -> String {
|
||||
let _ = std::fs::write(dir.join("steps.log"), self.log.join("\n") + "\n");
|
||||
let _ = std::fs::write(dir.join("daemon.log"), self.logged.join("\n") + "\n");
|
||||
let tail = &self.log[self.log.len().saturating_sub(LOG_TAIL)..];
|
||||
format!(
|
||||
"{} seed {} failed: {failure}\nreplay with PDFS_SIM_SEED={}; state kept in {}\nlast steps:\n{}",
|
||||
|
||||
@@ -57,7 +57,10 @@ pub(crate) fn run(core: Core, control_socket: PathBuf) {
|
||||
}
|
||||
|
||||
let mut last_rss = Instant::now();
|
||||
let mut last_ping = Instant::now();
|
||||
// `None` until the first probe, which runs on the first tick: startup has
|
||||
// already used up much of the watchdog's time (`docs/BUGS.md` B279).
|
||||
let mut last_ping: Option<Instant> = None;
|
||||
let mut restored = false;
|
||||
// Per-lane completion counts from the previous tick, to tell a queue that is
|
||||
// being worked through from one that is not moving at all.
|
||||
let mut last_completed = (0_u64, 0_u64);
|
||||
@@ -84,16 +87,22 @@ pub(crate) fn run(core: Core, control_socket: PathBuf) {
|
||||
// throw away in-flight uploads. The probe decides that; a stall only
|
||||
// gets said out loud.
|
||||
if let Some(period) = watchdog
|
||||
&& last_ping.elapsed() >= period
|
||||
&& ping_due(last_ping, period)
|
||||
{
|
||||
if probe(&control_socket) {
|
||||
if !restored {
|
||||
// Startup ran on the longer timeout the daemon asked for.
|
||||
// The socket answers now, so the unit's own one applies.
|
||||
systemd::restore_watchdog();
|
||||
restored = true;
|
||||
}
|
||||
systemd::watchdog_ping();
|
||||
} else {
|
||||
warn!(
|
||||
"daemon did not answer its own control socket; withholding the watchdog ping"
|
||||
);
|
||||
}
|
||||
last_ping = Instant::now();
|
||||
last_ping = Some(Instant::now());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,6 +174,11 @@ fn report_stalls(report: &pdfs_core::control::Diagnostics, last_completed: (u64,
|
||||
stalled
|
||||
}
|
||||
|
||||
/// Whether the watchdog wants its next ping. The first one is due at once.
|
||||
fn ping_due(last_ping: Option<Instant>, period: Duration) -> bool {
|
||||
last_ping.is_none_or(|at| at.elapsed() >= period)
|
||||
}
|
||||
|
||||
/// One round trip over the control socket: connect, ask for the cheapest
|
||||
/// request there is, read the answer.
|
||||
///
|
||||
@@ -205,6 +219,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// The first ping goes out on the first tick, not a full period after the
|
||||
/// supervisor starts: startup has already spent most of the watchdog's time
|
||||
/// (`docs/BUGS.md` B279).
|
||||
#[test]
|
||||
fn first_watchdog_ping_is_due_at_once() {
|
||||
let period = Duration::from_secs(60);
|
||||
assert!(ping_due(None, period));
|
||||
assert!(!ping_due(Some(Instant::now()), period));
|
||||
let long_ago = Instant::now().checked_sub(period).expect("uptime");
|
||||
assert!(ping_due(Some(long_ago), period));
|
||||
}
|
||||
|
||||
/// A worker doing normal work must not be reported: a WARN every tick is a
|
||||
/// WARN nobody reads.
|
||||
#[test]
|
||||
|
||||
@@ -170,7 +170,24 @@ impl Core {
|
||||
}
|
||||
let started = Instant::now();
|
||||
self.progress_begin(folder.id);
|
||||
let result = if push_only {
|
||||
// An attachment's root can be trashed or moved by the web app. A
|
||||
// missing root must never turn into a scan of an empty remote tree.
|
||||
let attached_root = if current.kind == "attached" {
|
||||
self.attach_ancestors(&remote_root).and_then(|ancestors| {
|
||||
if ancestors.contains(&self.primary_root_uid) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(pdfs_core::CoreError::invalid(
|
||||
"attached folder is no longer below My files",
|
||||
))
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
};
|
||||
let result = if let Err(error) = attached_root {
|
||||
Err(PassAbort::Failed(error.to_string()))
|
||||
} else if push_only {
|
||||
self.push_pass(folder.id, &local_root, &remote_root, &rules)
|
||||
} else {
|
||||
match self.do_reconcile(folder.id, &local_root, &remote_root, &rules) {
|
||||
|
||||
@@ -102,6 +102,37 @@ pub fn idle_sleep(duration: Duration) {
|
||||
}
|
||||
}
|
||||
|
||||
/// How long a mount may take to start before the watchdog counts it as hung.
|
||||
///
|
||||
/// Startup has no control socket to probe yet, and on a large account it is
|
||||
/// long: hydrating 48 000 nodes took 70 s on 2026-11-12, and the secondary
|
||||
/// mounts followed. Under the 2-minute `WatchdogSec` that run was aborted just
|
||||
/// before its first ping, and left six dead mounts behind (`docs/BUGS.md` B279).
|
||||
const STARTUP_WATCHDOG: Duration = Duration::from_secs(600);
|
||||
|
||||
/// Give the mount about to start [`STARTUP_WATCHDOG`] before the watchdog fires.
|
||||
///
|
||||
/// Sends `WATCHDOG_USEC=`, which replaces the unit's timeout and restarts its
|
||||
/// timer. The supervisor puts the configured timeout back with
|
||||
/// [`restore_watchdog`] once it can prove the daemon answers. A startup that
|
||||
/// wedges for good is still aborted, only later.
|
||||
pub fn stretch_watchdog_for_startup() {
|
||||
if watchdog_period().is_some() {
|
||||
notify(&format!("WATCHDOG_USEC={}\n", STARTUP_WATCHDOG.as_micros()));
|
||||
}
|
||||
}
|
||||
|
||||
/// Put back the timeout the unit configured, undoing
|
||||
/// [`stretch_watchdog_for_startup`].
|
||||
pub(crate) fn restore_watchdog() {
|
||||
if watchdog_period().is_none() {
|
||||
return;
|
||||
}
|
||||
if let Ok(usec) = std::env::var("WATCHDOG_USEC") {
|
||||
notify(&format!("WATCHDOG_USEC={usec}\n"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Tell the manager the daemon is stopping, so a slow teardown is not mistaken
|
||||
/// for a hang by the watchdog it is racing.
|
||||
pub(crate) fn stopping() {
|
||||
|
||||
+894
-233
File diff suppressed because it is too large.
Load diff
@@ -22,6 +22,8 @@ name = "pdfs-prompt"
|
||||
path = "src/prompt.rs"
|
||||
|
||||
[dependencies]
|
||||
pg-core.workspace = true
|
||||
base64.workspace = true
|
||||
pdfs-core.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Album: a photo in front of the edge of another, a stack of them. -->
|
||||
<path fill="#222222" d="M 4,1 H 13.5 A 1.5,1.5 0 0 1 15,2.5 V 12 H 13.5 V 2.5 H 4 Z"/>
|
||||
<path fill="#222222" fill-rule="evenodd" d="M 2.5,4 H 10.5 A 1.5,1.5 0 0 1 12,5.5 V 13.5 A 1.5,1.5 0 0 1 10.5,15 H 2.5 A 1.5,1.5 0 0 1 1,13.5 V 5.5 A 1.5,1.5 0 0 1 2.5,4 Z M 2.5,5.5 V 13.5 H 10.5 V 5.5 Z"/>
|
||||
<path fill="#222222" d="M 3.5,12.5 L 5.5,9.2 L 7.2,11.2 L 8.2,10.2 L 9.5,12.5 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 550 B |
@@ -0,0 +1,8 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Photos: a framed landscape with a sun. Stock themes only have
|
||||
image-x-generic, a file type icon, which reads as "an image file"
|
||||
rather than "your photo library". -->
|
||||
<path fill="#222222" fill-rule="evenodd" d="M 3,2 H 13 A 2,2 0 0 1 15,4 V 12 A 2,2 0 0 1 13,14 H 3 A 2,2 0 0 1 1,12 V 4 A 2,2 0 0 1 3,2 Z M 3,3.5 A 0.5,0.5 0 0 0 2.5,4 V 12 A 0.5,0.5 0 0 0 3,12.5 H 13 A 0.5,0.5 0 0 0 13.5,12 V 4 A 0.5,0.5 0 0 0 13,3.5 Z"/>
|
||||
<path fill="#222222" d="M 3.5,11.5 L 6.5,7 L 9,10.2 L 10.5,8.6 L 12.5,11.5 Z"/>
|
||||
<circle fill="#222222" cx="10.5" cy="5.8" r="1.3"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 673 B |
@@ -0,0 +1,6 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Slideshow: a play mark inside a photo frame, so it is not mistaken for
|
||||
playing the video on screen. -->
|
||||
<path fill="#222222" fill-rule="evenodd" d="M 3,2 H 13 A 2,2 0 0 1 15,4 V 12 A 2,2 0 0 1 13,14 H 3 A 2,2 0 0 1 1,12 V 4 A 2,2 0 0 1 3,2 Z M 3,3.5 A 0.5,0.5 0 0 0 2.5,4 V 12 A 0.5,0.5 0 0 0 3,12.5 H 13 A 0.5,0.5 0 0 0 13.5,12 V 4 A 0.5,0.5 0 0 0 13,3.5 Z"/>
|
||||
<path fill="#222222" d="M 6.5,5.2 L 11,8 L 6.5,10.8 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 527 B |
@@ -15,6 +15,9 @@
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-link-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-map-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-people-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-photos-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-album-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-slideshow-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-share-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-sync-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-synced-symbolic.svg</file>
|
||||
|
||||
@@ -53,7 +53,7 @@ button.sidebar-account { padding: 4px 8px; font-weight: normal; }
|
||||
/* The folder on screen, last in the breadcrumb trail: a flat button's side
|
||||
* padding, so the "›" before it is spaced like the ones between buttons. */
|
||||
.crumb-current { padding: 0 10px; }
|
||||
scale.browser-status-meter trough { min-width: 104px; min-height: 6px; }
|
||||
scale.browser-status-meter trough { min-width: 140px; min-height: 6px; }
|
||||
.bulk-bar { padding: 6px 8px; border-radius: 12px; background: color-mix(in srgb, var(--accent-bg-color) 12%, transparent); }
|
||||
.dropzone { border: 2px dashed alpha(currentColor, 0.25); border-radius: 16px; padding: 28px 18px; transition: background 160ms ease, border-color 160ms ease; }
|
||||
.dropzone-active { border-color: var(--accent-bg-color); background: color-mix(in srgb, var(--accent-bg-color) 10%, transparent); }
|
||||
@@ -61,14 +61,28 @@ scale.browser-status-meter trough { min-width: 104px; 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; }
|
||||
@@ -80,7 +94,12 @@ scale.browser-status-meter trough { min-width: 104px; min-height: 6px; }
|
||||
.viewer-info-panel { background-color: var(--window-bg-color); border-left: 1px solid alpha(currentColor, 0.12); }
|
||||
|
||||
/* Photos grid */
|
||||
.gallery-day { font-size: 0.82rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: alpha(currentColor, 0.65); padding: 2px 2px 6px 2px; }
|
||||
.gallery-day { font-size: 1.1rem; font-weight: 700; padding: 4px 2px 8px 2px; }
|
||||
/* The check that picks a whole section: on hover, and always while selecting. */
|
||||
.gallery-day-check { opacity: 0; transition: opacity 160ms ease; min-width: 24px; min-height: 24px; margin-bottom: 4px; }
|
||||
.gallery-heading:hover .gallery-day-check, .gallery-heading.selecting .gallery-day-check, .gallery-day-check:focus-visible { opacity: 1; }
|
||||
/* The section at the top of the grid, floating once its heading scrolled off. */
|
||||
.gallery-float-heading { font-weight: 700; padding: 4px 12px; border-radius: 999px; background-color: color-mix(in srgb, var(--popover-bg-color) 92%, transparent); color: var(--popover-fg-color); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25); }
|
||||
.photo-tile { padding: 0; margin: 0; min-height: 0; min-width: 0; border: none; border-radius: 4px; background: none; box-shadow: none; }
|
||||
.photo-tile:focus { outline: 2px solid var(--accent-color); outline-offset: -2px; }
|
||||
.photo-thumb { transition: transform 220ms ease; }
|
||||
@@ -94,6 +113,7 @@ scale.browser-status-meter trough { min-width: 104px; min-height: 6px; }
|
||||
.photo-video-badge { color: white; background: rgba(0, 0, 0, 0.45); border-radius: 999px; padding: 8px; min-width: 20px; min-height: 20px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5); transition: background 160ms ease; }
|
||||
.photo-video-badge-small { padding: 3px; min-width: 0; min-height: 0; }
|
||||
.photo-tile:hover .photo-video-badge { background: color-mix(in srgb, var(--accent-bg-color) 85%, transparent); }
|
||||
.photo-favorite-badge { color: white; -gtk-icon-shadow: 0 1px 3px rgba(0, 0, 0, 0.9); }
|
||||
.photo-group-badge { font-size: 0.68rem; font-weight: 700; letter-spacing: 0.04em; color: white; background: rgba(0, 0, 0, 0.55); border-radius: 6px; padding: 1px 5px; margin: 6px; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9); }
|
||||
.memory-card { padding: 0; border-radius: 12px; }
|
||||
.memory-cover { border-radius: 12px; background: alpha(currentColor, 0.06); }
|
||||
@@ -103,7 +123,8 @@ scale.browser-status-meter trough { min-width: 104px; min-height: 6px; }
|
||||
.album-cover { border-radius: 16px; background: alpha(currentColor, 0.06); box-shadow: 0 4px 16px rgba(0, 0, 0, 0.32); transition: box-shadow 180ms ease; }
|
||||
.album-card:hover .album-cover { box-shadow: 0 10px 26px rgba(0, 0, 0, 0.5); }
|
||||
.album-card:hover .photo-thumb { transform: scale(1.05); }
|
||||
.view-switch button, .view-switch toggle { padding-left: 18px; padding-right: 18px; }
|
||||
listview.drive-gallery-rows > row { padding: 0; }
|
||||
.view-switch button, .view-switch toggle { padding-left: 12px; padding-right: 12px; }
|
||||
|
||||
/* Shared with Me: what a share lets me do, as a tinted pill beside the name. */
|
||||
.role-pill { padding: 2px 10px; border-radius: 999px; font-weight: 600; color: var(--accent-color); background-color: color-mix(in srgb, var(--accent-bg-color) 16%, transparent); }
|
||||
|
||||
@@ -128,8 +128,48 @@ fn follow_changes(ui: &Rc<Ui>, 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();
|
||||
if changed(Topic::GalleryVisibilitySynced)
|
||||
&& let Some(browser) = ui.drive_galleries.borrow().upgrade()
|
||||
{
|
||||
browser.refresh_visibility();
|
||||
}
|
||||
if changed(Topic::GalleryVisibility) || changed(Topic::GalleryVisibilitySynced) {
|
||||
for window in gtk4::Window::list_toplevels() {
|
||||
if let Ok(window) = window.downcast::<gtk4::Window>()
|
||||
&& window.has_css_class("photo-viewer-window")
|
||||
{
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
ui.gallery.photo_tex.borrow_mut().clear();
|
||||
ui.gallery.photo_tex_order.borrow_mut().clear();
|
||||
ui.gallery.model.remove_all();
|
||||
ui.gallery.groups.remove_all();
|
||||
ui.gallery.timeline_stale.set(true);
|
||||
if page.as_deref() == Some("gallery") {
|
||||
match ui.gallery.content.visible_child_name().as_deref() {
|
||||
Some("albums") => load_albums(ui),
|
||||
Some("places") => load_places(ui),
|
||||
_ => load_gallery(ui, false),
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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")
|
||||
|
||||
+232
-34
@@ -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::*;
|
||||
@@ -71,15 +77,15 @@ use gtk4::glib::BoxedAnyObject;
|
||||
|
||||
use pdfs_core::auth;
|
||||
|
||||
use pdfs_core::config::{AppDirs, FileSort, FilesView, WindowState};
|
||||
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};
|
||||
@@ -154,6 +160,7 @@ struct Ui {
|
||||
pub(crate) details: DetailsState,
|
||||
pub(crate) trash: TrashState,
|
||||
pub(crate) gallery: GalleryState,
|
||||
drive_galleries: RefCell<std::rc::Weak<pages::drive_galleries::Browser>>,
|
||||
pub(crate) shared: SharedState,
|
||||
pub(crate) shared_by_me: SharedByMeState,
|
||||
pub(crate) devices: DevicesState,
|
||||
@@ -295,6 +302,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 +346,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);
|
||||
@@ -412,6 +460,7 @@ fn build_window(app: &adw::Application) {
|
||||
nav: split.clone(),
|
||||
narrow: Cell::new(false),
|
||||
file_thumbs: FileThumbnailState::new(),
|
||||
drive_galleries: RefCell::default(),
|
||||
login: login_widgets,
|
||||
status: StatusState {
|
||||
status_inflight: Cell::new(false),
|
||||
@@ -457,6 +506,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(),
|
||||
@@ -488,6 +539,7 @@ fn build_window(app: &adw::Application) {
|
||||
empty_actions: browser_widgets.empty_actions.clone(),
|
||||
summary: browser_widgets.summary.clone(),
|
||||
zoom: browser_widgets.zoom.clone(),
|
||||
zoom_save: RefCell::new(None),
|
||||
},
|
||||
details: DetailsState {
|
||||
details: browser_widgets.details,
|
||||
@@ -522,6 +574,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(),
|
||||
@@ -587,10 +640,13 @@ fn build_window(app: &adw::Application) {
|
||||
selected: RefCell::new(HashSet::new()),
|
||||
select_anchor: RefCell::new(None),
|
||||
select_btn: gallery_widgets.select_btn.clone(),
|
||||
select_bar: gallery_widgets.select_bar.clone(),
|
||||
select_label: gallery_widgets.select_label.clone(),
|
||||
select_trash: gallery_widgets.select_trash.clone(),
|
||||
select_album: gallery_widgets.select_album.clone(),
|
||||
selection: gallery_widgets.selection.clone(),
|
||||
more: gallery_widgets.more.clone(),
|
||||
upload_face: gallery_widgets.upload_face.clone(),
|
||||
float_heading: gallery_widgets.float_heading.clone(),
|
||||
float_label: gallery_widgets.float_label.clone(),
|
||||
square: Cell::new(false),
|
||||
zoom_save: RefCell::new(None),
|
||||
},
|
||||
shared: SharedState::new(&shared_widgets),
|
||||
shared_by_me: SharedByMeState::new(&shared_by_me_widgets),
|
||||
@@ -628,6 +684,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(),
|
||||
@@ -659,9 +722,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,
|
||||
});
|
||||
@@ -678,6 +743,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(
|
||||
@@ -693,8 +760,12 @@ fn build_window(app: &adw::Application) {
|
||||
&ui,
|
||||
&gallery_widgets.list,
|
||||
&gallery_widgets.scroll,
|
||||
&gallery_widgets.select_done,
|
||||
&gallery_widgets.square_action,
|
||||
);
|
||||
gallery_widgets.drive_galleries.connect_clicked({
|
||||
let ui = ui.clone();
|
||||
move |_| pages::drive_galleries::open(&ui)
|
||||
});
|
||||
wire_gallery_empty(
|
||||
&ui,
|
||||
&gallery_widgets.empty_upload,
|
||||
@@ -707,6 +778,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);
|
||||
@@ -722,17 +794,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),
|
||||
@@ -795,6 +876,7 @@ fn build_window(app: &adw::Application) {
|
||||
/// split collapsed whatever the width.
|
||||
fn set_narrow(ui: &Ui, narrow: bool) {
|
||||
ui.narrow.set(narrow);
|
||||
set_photos_narrow(ui, narrow);
|
||||
if ui.session.borrow().is_some() {
|
||||
ui.nav.set_collapsed(narrow);
|
||||
}
|
||||
@@ -826,12 +908,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}");
|
||||
}
|
||||
}
|
||||
@@ -858,11 +938,7 @@ fn window_hidden(window: &adw::ApplicationWindow) -> bool {
|
||||
/// sync, set off by a separator.
|
||||
const DESTINATIONS: [(&str, &str, &str); 7] = [
|
||||
("browser", gettext_noop("My files"), "folder-symbolic"),
|
||||
(
|
||||
"gallery",
|
||||
gettext_noop("Photos"),
|
||||
"image-x-generic-symbolic",
|
||||
),
|
||||
("gallery", gettext_noop("Photos"), "pdfs-photos-symbolic"),
|
||||
(
|
||||
"shared",
|
||||
gettext_noop("Shared with me"),
|
||||
@@ -1065,7 +1141,7 @@ fn reload_current_page(ui: &Rc<Ui>) {
|
||||
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.
|
||||
@@ -1327,7 +1403,7 @@ fn install_window_actions(ui: &Rc<Ui>, 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);
|
||||
@@ -1433,10 +1509,21 @@ fn install_launch_actions(ui: &Rc<Ui>, 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);
|
||||
}
|
||||
@@ -1505,7 +1592,7 @@ fn debug_info(ui: &Rc<Ui>) -> 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"),
|
||||
&[
|
||||
@@ -1523,6 +1610,9 @@ fn show_shortcuts(window: &adw::ApplicationWindow) {
|
||||
("<Alt>Left <Alt>Right", gettext_noop("Back / forward")),
|
||||
("<Alt>Up", gettext_noop("Parent folder")),
|
||||
("<Control>1 <Control>2", gettext_noop("Grid / list")),
|
||||
("<Control>plus", gettext_noop("Larger thumbnails")),
|
||||
("<Control>minus", gettext_noop("Smaller thumbnails")),
|
||||
("<Control>0", gettext_noop("Reset thumbnail size")),
|
||||
("F2", gettext_noop("Rename")),
|
||||
("Delete", gettext_noop("Move to Trash")),
|
||||
("Escape", gettext_noop("Clear the selection")),
|
||||
@@ -1535,6 +1625,7 @@ fn show_shortcuts(window: &adw::ApplicationWindow) {
|
||||
("<Control>minus", gettext_noop("Smaller thumbnails")),
|
||||
("<Control>0", gettext_noop("Reset thumbnail size")),
|
||||
("<Control>a", gettext_noop("Select all")),
|
||||
("Escape <Alt>Left", gettext_noop("Back")),
|
||||
],
|
||||
),
|
||||
(
|
||||
@@ -1544,8 +1635,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")),
|
||||
("<Control>c", gettext_noop("Copy Image")),
|
||||
("Delete", gettext_noop("Move to Trash")),
|
||||
("Escape", gettext_noop("Close")),
|
||||
("Escape <Alt>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")),
|
||||
],
|
||||
),
|
||||
];
|
||||
@@ -1571,8 +1674,17 @@ fn install_shortcuts(ui: &Rc<Ui>, 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.
|
||||
@@ -1588,6 +1700,21 @@ fn install_shortcuts(ui: &Rc<Ui>, window: &adw::ApplicationWindow) {
|
||||
ui.browser.search.grab_focus();
|
||||
}
|
||||
gtk4::gdk::Key::n | gtk4::gdk::Key::N if ctrl && on_browser => prompt_new_folder(&ui),
|
||||
gtk4::gdk::Key::plus | gtk4::gdk::Key::equal | gtk4::gdk::Key::KP_Add
|
||||
if ctrl && on_browser && !ui.browser.files.shows_list() =>
|
||||
{
|
||||
zoom_files(&ui, 1)
|
||||
}
|
||||
gtk4::gdk::Key::minus | gtk4::gdk::Key::KP_Subtract
|
||||
if ctrl && on_browser && !ui.browser.files.shows_list() =>
|
||||
{
|
||||
zoom_files(&ui, -1)
|
||||
}
|
||||
gtk4::gdk::Key::_0 | gtk4::gdk::Key::KP_0
|
||||
if ctrl && on_browser && !ui.browser.files.shows_list() =>
|
||||
{
|
||||
zoom_files(&ui, 0)
|
||||
}
|
||||
gtk4::gdk::Key::_1 | gtk4::gdk::Key::_2 if ctrl && on_browser => set_files_view(
|
||||
&ui,
|
||||
FilesView {
|
||||
@@ -1614,6 +1741,21 @@ fn install_shortcuts(ui: &Rc<Ui>, 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) {
|
||||
@@ -1942,6 +2084,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 {
|
||||
@@ -1955,6 +2118,7 @@ mod tests {
|
||||
favorite: false,
|
||||
group_size: 2,
|
||||
has_raw: kind == PhotoKind::Raw,
|
||||
place_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1977,6 +2141,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]
|
||||
|
||||
@@ -40,7 +40,7 @@ pub(crate) fn load_albums(ui: &Rc<Ui>) {
|
||||
if ui.gallery.albums_stack.visible_child_name().as_deref() != Some("grid") {
|
||||
albums_status(
|
||||
ui,
|
||||
"view-grid-symbolic",
|
||||
"pdfs-album-symbolic",
|
||||
&gettext("Loading albums…"),
|
||||
&gettext("Reading your Proton Drive albums."),
|
||||
);
|
||||
@@ -62,7 +62,7 @@ pub(crate) fn load_albums(ui: &Rc<Ui>) {
|
||||
),
|
||||
Ok(Ok(Response::Albums { items, .. })) if items.is_empty() => albums_status(
|
||||
&ui,
|
||||
"view-grid-symbolic",
|
||||
"pdfs-album-symbolic",
|
||||
&gettext("No albums"),
|
||||
&gettext("Create one with New Album, or in Proton Photos."),
|
||||
),
|
||||
@@ -136,7 +136,7 @@ fn album_card(ui: &Rc<Ui>, album: &AlbumInfo) -> gtk4::Button {
|
||||
picture.add_css_class("photo-thumb");
|
||||
|
||||
let placeholder = gtk4::Image::builder()
|
||||
.icon_name("view-grid-symbolic")
|
||||
.icon_name("pdfs-album-symbolic")
|
||||
.pixel_size(32)
|
||||
.halign(gtk4::Align::Center)
|
||||
.valign(gtk4::Align::Center)
|
||||
@@ -225,12 +225,13 @@ fn show_album_menu(ui: &Rc<Ui>, album: &AlbumInfo, anchor: >k4::Widget, x: f64
|
||||
/// while the entry is blank.
|
||||
fn prompt_album_name(
|
||||
ui: &Rc<Ui>,
|
||||
parent: Option<gtk4::Window>,
|
||||
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<Ui>, photos: Vec<String>) {
|
||||
///
|
||||
/// The dialogs open over `parent`, or over the main window without one.
|
||||
pub(crate) fn prompt_new_album(ui: &Rc<Ui>, photos: Vec<String>, parent: Option<gtk4::Window>) {
|
||||
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<Ui>, photos: Vec<String>) {
|
||||
fn prompt_rename_album(ui: &Rc<Ui>, 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<Ui>) {
|
||||
}
|
||||
|
||||
/// Offer our own albums to add `photos` to, plus a new one.
|
||||
pub(crate) fn prompt_add_to_album(ui: &Rc<Ui>, photos: Vec<String>) {
|
||||
pub(crate) fn prompt_add_to_album(ui: &Rc<Ui>, photos: Vec<String>, parent: Option<gtk4::Window>) {
|
||||
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<Ui>, photos: Vec<String>) {
|
||||
}
|
||||
};
|
||||
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<Ui>, photos: Vec<String>) {
|
||||
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<Ui>, 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);
|
||||
@@ -618,9 +618,7 @@ pub(crate) fn open_album(ui: &Rc<Ui>, album: AlbumInfo) {
|
||||
// would silently do something else. The Photos/Albums switcher goes too: an
|
||||
// album is a level below it, and back is the way out.
|
||||
ui.gallery.filters.set_visible(false);
|
||||
ui.gallery.view_switch.widget().set_visible(false);
|
||||
ui.gallery.upload.set_visible(false);
|
||||
ui.gallery.back.set_visible(true);
|
||||
sync_header(ui);
|
||||
// A stale kind/date filter would otherwise be sent with the album request.
|
||||
ui.gallery.kind.set(None);
|
||||
ui.gallery.range.set(None);
|
||||
@@ -636,9 +634,7 @@ pub(crate) fn close_album(ui: &Rc<Ui>) {
|
||||
return;
|
||||
}
|
||||
ui.gallery.title.set_title(&gettext("Photos"));
|
||||
ui.gallery.view_switch.widget().set_visible(true);
|
||||
ui.gallery.upload.set_visible(true);
|
||||
ui.gallery.back.set_visible(false);
|
||||
sync_header(ui);
|
||||
}
|
||||
|
||||
/// Show the album grid's own status page (loading, empty, or an error).
|
||||
@@ -668,12 +664,25 @@ pub(crate) fn wire_albums(ui: &Rc<Ui>) {
|
||||
}
|
||||
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);
|
||||
|
||||
@@ -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<String>,
|
||||
}
|
||||
|
||||
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<Vec<String>>,
|
||||
pub(crate) future: RefCell<Vec<String>>,
|
||||
/// 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<HashMap<String, FolderPlace>>,
|
||||
/// The folder whose [`Self::places`] entry the next listing restores, set
|
||||
/// by Back, Forward and Up.
|
||||
pub(crate) restore: RefCell<Option<String>>,
|
||||
/// 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,
|
||||
@@ -77,6 +92,8 @@ pub(crate) struct BrowserState {
|
||||
/// zoom value, and Proton account storage usage.
|
||||
pub(crate) summary: gtk4::Label,
|
||||
pub(crate) zoom: gtk4::Scale,
|
||||
/// Saves the zoom once the slider rests, not on every step.
|
||||
pub(crate) zoom_save: RefCell<Option<glib::SourceId>>,
|
||||
}
|
||||
|
||||
/// Idle pause after the last keystroke before a search query is sent, so typing
|
||||
@@ -87,9 +104,11 @@ pub(crate) const SEARCH_DEBOUNCE: Duration = Duration::from_millis(250);
|
||||
pub(crate) const SEARCH_LIMIT: usize = 200;
|
||||
|
||||
/// Browser grid thumbnail size controlled by the bottom zoom slider. The
|
||||
/// default preserves the former fixed 72 px presentation.
|
||||
/// default preserves the former fixed 72 px presentation. The largest size is
|
||||
/// big enough to tell photos apart by their thumbnail, and thumbnails are
|
||||
/// 512 px, so they stay sharp up to it on a 2× screen.
|
||||
pub(crate) const GRID_THUMB_MIN: i32 = 48;
|
||||
pub(crate) const GRID_THUMB_MAX: i32 = 144;
|
||||
pub(crate) const GRID_THUMB_MAX: i32 = 256;
|
||||
pub(crate) const GRID_THUMB_DEFAULT: i32 = 72;
|
||||
pub(crate) const GRID_THUMB_STEP: i32 = 8;
|
||||
|
||||
@@ -801,7 +820,35 @@ pub(crate) fn wire_browser(ui: &Rc<Ui>) {
|
||||
&[],
|
||||
)));
|
||||
ui_zoom.browser.files.set_thumbnail_size(size);
|
||||
save_icon_size(&ui_zoom, size);
|
||||
});
|
||||
// Ctrl+scroll steps the icon size, as in Photos. Capture phase so the
|
||||
// scrolled window does not scroll the grid as well. A touchpad sends many
|
||||
// small deltas, so they add up to a whole notch before each step.
|
||||
if let Some(grid_scroll) = files.views.child_by_name("grid") {
|
||||
let wheel = gtk4::EventControllerScroll::new(gtk4::EventControllerScrollFlags::VERTICAL);
|
||||
wheel.set_propagation_phase(gtk4::PropagationPhase::Capture);
|
||||
let ui_wheel = ui.clone();
|
||||
let pending = Cell::new(0.0_f64);
|
||||
wheel.connect_scroll(move |controller, _dx, dy| {
|
||||
if !controller
|
||||
.current_event_state()
|
||||
.contains(gtk4::gdk::ModifierType::CONTROL_MASK)
|
||||
{
|
||||
pending.set(0.0);
|
||||
return glib::Propagation::Proceed;
|
||||
}
|
||||
let total = pending.get() + dy;
|
||||
let steps = total.trunc();
|
||||
pending.set(total - steps);
|
||||
if steps != 0.0 {
|
||||
// Scrolling up (negative dy) makes the icons bigger.
|
||||
zoom_files(&ui_wheel, -(steps as i32));
|
||||
}
|
||||
glib::Propagation::Stop
|
||||
});
|
||||
grid_scroll.add_controller(wheel);
|
||||
}
|
||||
|
||||
files
|
||||
.column_view
|
||||
@@ -851,27 +898,15 @@ fn show_location_column(ui: &Rc<Ui>, visible: bool) {
|
||||
pub(crate) fn entry_context_menu(ui: &Rc<Ui>, 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
|
||||
@@ -1060,38 +1095,8 @@ pub(crate) fn entry_at(model: Option<&impl IsA<gio::ListModel>>, 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 `<mountpoint>/<rel>` 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<Ui>, 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<Ui>, entry: &DirEntry) {
|
||||
let rel = entry_rel(ui, entry);
|
||||
if entry.is_dir {
|
||||
@@ -1104,16 +1109,16 @@ pub(crate) fn activate_entry(ui: &Rc<Ui>, 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
|
||||
@@ -1306,6 +1311,11 @@ pub(crate) fn wire_browser_actions(ui: &Rc<Ui>, build_thumbnails: >k4::Button)
|
||||
|
||||
let view = ui.dirs.load_config().files_view;
|
||||
ui.browser.view.set(view);
|
||||
if let Some(size) = view.icon_size {
|
||||
ui.browser
|
||||
.zoom
|
||||
.set_value(f64::from(size.clamp(GRID_THUMB_MIN, GRID_THUMB_MAX)));
|
||||
}
|
||||
let layout = gio::SimpleAction::new_stateful(
|
||||
"view",
|
||||
Some(glib::VariantTy::STRING),
|
||||
@@ -1407,6 +1417,43 @@ pub(crate) fn wire_browser_actions(ui: &Rc<Ui>, build_thumbnails: >k4::Button)
|
||||
ui.browser.content.add_controller(drop);
|
||||
}
|
||||
|
||||
/// Remember the grid's icon size, once the slider has rested for a moment.
|
||||
fn save_icon_size(ui: &Rc<Ui>, size: i32) {
|
||||
let mut view = ui.browser.view.get();
|
||||
if view.icon_size == Some(size) {
|
||||
return;
|
||||
}
|
||||
view.icon_size = Some(size);
|
||||
ui.browser.view.set(view);
|
||||
if let Some(source) = ui.browser.zoom_save.borrow_mut().take() {
|
||||
source.remove();
|
||||
}
|
||||
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 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}");
|
||||
}
|
||||
});
|
||||
*ui.browser.zoom_save.borrow_mut() = Some(source);
|
||||
}
|
||||
|
||||
/// Step the grid's icon size by `steps` slider notches; `0` resets it.
|
||||
pub(crate) fn zoom_files(ui: &Rc<Ui>, steps: i32) {
|
||||
let zoom = &ui.browser.zoom;
|
||||
let size = if steps == 0 {
|
||||
GRID_THUMB_DEFAULT
|
||||
} else {
|
||||
(zoom.value().round() as i32 + steps * GRID_THUMB_STEP)
|
||||
.clamp(GRID_THUMB_MIN, GRID_THUMB_MAX)
|
||||
};
|
||||
zoom.set_value(f64::from(size));
|
||||
}
|
||||
|
||||
fn layout_name(view: FilesView) -> &'static str {
|
||||
if view.list { "list" } else { "grid" }
|
||||
}
|
||||
@@ -1419,9 +1466,7 @@ pub(crate) fn set_files_view(ui: &Rc<Ui>, 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)
|
||||
@@ -1484,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<Ui>) {
|
||||
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<Ui>, target: String) {
|
||||
remember_place(ui);
|
||||
let left = ui.browser.path.replace(target.clone());
|
||||
if left != target {
|
||||
ui.browser.history.borrow_mut().push(left);
|
||||
@@ -1510,6 +1576,8 @@ fn browse_back(ui: &Rc<Ui>) {
|
||||
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);
|
||||
@@ -1520,6 +1588,8 @@ fn browse_forward(ui: &Rc<Ui>) {
|
||||
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);
|
||||
@@ -1532,6 +1602,7 @@ fn browse_up(ui: &Rc<Ui>) {
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -1728,8 +1799,9 @@ fn repaint_thumbnail_build(ui: &Rc<Ui>, status: &ThumbnailBuildStatus) {
|
||||
let text = if status.scanning {
|
||||
ui.browser.thumbnail_progress.pulse();
|
||||
let folders = ngettext_f("{n} folder", "{n} folders", status.folders_scanned, &[]);
|
||||
let images = ngettext_f("{n} image", "{n} images", status.images_found, &[]);
|
||||
// Translators: {root} is a folder path or "Proton Drive"; {folders} and {images} are counts such as "3 folders" and "12 images".
|
||||
// Translators: a count of images and videos that can have a thumbnail.
|
||||
let images = ngettext_f("{n} file", "{n} files", status.images_found, &[]);
|
||||
// Translators: {root} is a folder path or "Proton Drive"; {folders} and {images} are counts such as "3 folders" and "12 files".
|
||||
gettext_f(
|
||||
"Scanning {root}… {folders}, {images}",
|
||||
&[("root", root), ("folders", &folders), ("images", &images)],
|
||||
@@ -1743,7 +1815,7 @@ fn repaint_thumbnail_build(ui: &Rc<Ui>, status: &ThumbnailBuildStatus) {
|
||||
ui.browser
|
||||
.thumbnail_progress
|
||||
.set_fraction(fraction.clamp(0.0, 1.0));
|
||||
// Translators: {root} is a folder path or "Proton Drive"; {done} and {total} are image counts.
|
||||
// Translators: {root} is a folder path or "Proton Drive"; {done} and {total} are counts of images and videos.
|
||||
gettext_f(
|
||||
"Building thumbnails in {root}… {done} of {total}",
|
||||
&[
|
||||
@@ -1756,17 +1828,17 @@ fn repaint_thumbnail_build(ui: &Rc<Ui>, status: &ThumbnailBuildStatus) {
|
||||
ui.browser.thumbnail_progress.set_fraction(1.0);
|
||||
let available = status.completed.saturating_sub(status.failed);
|
||||
match status.failed {
|
||||
// Translators: {root} is a folder path or "Proton Drive".
|
||||
// Translators: {root} is a folder path or "Proton Drive"; {n} counts images and videos.
|
||||
0 => ngettext_f(
|
||||
"Thumbnails ready for {n} image in {root}",
|
||||
"Thumbnails ready for {n} images in {root}",
|
||||
"Thumbnails ready for {n} file in {root}",
|
||||
"Thumbnails ready for {n} files in {root}",
|
||||
available,
|
||||
&[("root", root)],
|
||||
),
|
||||
// Translators: {root} is a folder path or "Proton Drive"; {failed} is how many images could not get a thumbnail.
|
||||
// Translators: {root} is a folder path or "Proton Drive"; {failed} is how many images and videos could not get a thumbnail.
|
||||
failed => ngettext_f(
|
||||
"Thumbnails ready for {n} image in {root}; {failed} unavailable",
|
||||
"Thumbnails ready for {n} images in {root}; {failed} unavailable",
|
||||
"Thumbnails ready for {n} file in {root}; {failed} unavailable",
|
||||
"Thumbnails ready for {n} files in {root}; {failed} unavailable",
|
||||
available,
|
||||
&[("root", root), ("failed", &failed.to_string())],
|
||||
),
|
||||
@@ -2765,6 +2837,11 @@ pub(crate) fn icon_base_for(entry: &DirEntry) -> &'static str {
|
||||
if entry.is_dir {
|
||||
return "folder";
|
||||
}
|
||||
// Every video type, including a `.ts` recording, which by its name alone
|
||||
// is TypeScript.
|
||||
if pdfs_core::control::is_video_file_name(&entry.name, Some(entry.size)) {
|
||||
return "video-x-generic";
|
||||
}
|
||||
let ext = entry
|
||||
.name
|
||||
.rsplit('.')
|
||||
@@ -2775,7 +2852,6 @@ pub(crate) fn icon_base_for(entry: &DirEntry) -> &'static str {
|
||||
"png" | "jpg" | "jpeg" | "gif" | "webp" | "bmp" | "svg" | "heic" | "tiff" => {
|
||||
"image-x-generic"
|
||||
}
|
||||
"mp4" | "mkv" | "mov" | "avi" | "webm" | "m4v" => "video-x-generic",
|
||||
"mp3" | "flac" | "wav" | "ogg" | "opus" | "m4a" => "audio-x-generic",
|
||||
"pdf" | "doc" | "docx" | "odt" => "x-office-document",
|
||||
"xls" | "xlsx" | "ods" | "csv" => "x-office-spreadsheet",
|
||||
@@ -2844,6 +2920,18 @@ pub(crate) fn load_browser(ui: &Rc<Ui>) {
|
||||
});
|
||||
}
|
||||
|
||||
/// 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<Ui>) {
|
||||
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.
|
||||
@@ -2882,7 +2970,10 @@ pub(crate) fn browser_unreachable(ui: &Rc<Ui>) {
|
||||
pub(crate) fn repaint_browser(ui: &Rc<Ui>, 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(),
|
||||
@@ -2911,6 +3002,16 @@ pub(crate) fn repaint_browser(ui: &Rc<Ui>, 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
|
||||
@@ -3089,6 +3190,9 @@ pub(crate) fn repaint_search(ui: &Rc<Ui>, 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 {
|
||||
@@ -3122,7 +3226,7 @@ fn listing_summary(files: usize, folders: usize, file_bytes: u64) -> String {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
MoveTarget, badge_for, copy_target_allowed, hit_location, listing_summary,
|
||||
MoveTarget, badge_for, copy_target_allowed, hit_location, icon_base_for, listing_summary,
|
||||
move_target_allowed, show_thumbnail_build_progress, sort_entries,
|
||||
};
|
||||
use pdfs_core::config::{FileSort, FilesView};
|
||||
@@ -3149,6 +3253,32 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_video_type_gets_the_video_icon() {
|
||||
const MIB: u64 = 1024 * 1024;
|
||||
for name in [
|
||||
"clip.mp4",
|
||||
"clip.MKV",
|
||||
"broadcast.m2ts",
|
||||
"camera.mts",
|
||||
"old.flv",
|
||||
] {
|
||||
assert_eq!(
|
||||
icon_base_for(&entry(name, false, 10, 0)),
|
||||
"video-x-generic",
|
||||
"{name}"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
icon_base_for(&entry("recording.ts", false, 900 * MIB, 0)),
|
||||
"video-x-generic"
|
||||
);
|
||||
assert_eq!(
|
||||
icon_base_for(&entry("app.ts", false, 4096, 0)),
|
||||
"text-x-generic"
|
||||
);
|
||||
}
|
||||
|
||||
fn names(entries: &[DirEntry]) -> Vec<&str> {
|
||||
entries.iter().map(|e| e.name.as_str()).collect()
|
||||
}
|
||||
|
||||
@@ -190,10 +190,9 @@ pub(crate) fn wire_devices(ui: &Rc<Ui>, 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<Ui>, 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<Ui>) {
|
||||
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<Ui>) {
|
||||
@@ -297,6 +304,7 @@ fn load_device_folder(ui: &Rc<Ui>, 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<Ui>, 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();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -588,6 +600,7 @@ fn platform_label(device_type: &str) -> &str {
|
||||
/// How many folders this machine backs up, and whether any of them needs
|
||||
/// attention — the one fact worth surfacing away from the folder list itself.
|
||||
pub(crate) fn this_computer_subtitle(folders: &[SyncFolderInfo]) -> String {
|
||||
let folders: Vec<_> = folders.iter().filter(|f| f.kind == "device").collect();
|
||||
if folders.is_empty() {
|
||||
return gettext("No folders backed up yet");
|
||||
}
|
||||
@@ -939,7 +952,13 @@ pub(crate) fn reload_sync_pages(ui: &Rc<Ui>) {
|
||||
}
|
||||
|
||||
/// Confirm, then stop syncing a folder. Offers to also delete the cloud copy.
|
||||
pub(crate) fn prompt_remove_sync_folder(ui: &Rc<Ui>, id: i64, path: &str, ondemand: bool) {
|
||||
pub(crate) fn prompt_remove_sync_folder(
|
||||
ui: &Rc<Ui>,
|
||||
id: i64,
|
||||
path: &str,
|
||||
ondemand: bool,
|
||||
attached: bool,
|
||||
) {
|
||||
let win = ui_window(ui);
|
||||
// The two modes leave the user in opposite places, so they can't share a
|
||||
// sentence. A mirror folder's files are already on this disk and stay there.
|
||||
@@ -947,7 +966,12 @@ pub(crate) fn prompt_remove_sync_folder(ui: &Rc<Ui>, id: i64, path: &str, ondema
|
||||
// in Proton Drive, so unmounting it leaves an empty directory — which is a
|
||||
// nasty surprise if the dialog claimed the local files were safe, and is
|
||||
// recoverable only by turning On-demand off *first* and letting it download.
|
||||
let body = if ondemand {
|
||||
let body = if attached {
|
||||
gettext_f(
|
||||
"Stop syncing “{path}”? Both the local files and the folder in My files are kept.",
|
||||
&[("path", path)],
|
||||
)
|
||||
} else if ondemand {
|
||||
// Translators: {path} is a local folder path.
|
||||
gettext_f(
|
||||
"Stop syncing “{path}”?\n\nThis folder is online only: its files live in Proton Drive, not on this disk, so the folder will be empty once it stops syncing. To keep a local copy, cancel, switch the folder to Synced, and wait for the download to finish before removing it.",
|
||||
@@ -975,7 +999,9 @@ pub(crate) fn prompt_remove_sync_folder(ui: &Rc<Ui>, id: i64, path: &str, ondema
|
||||
.active(false)
|
||||
.build();
|
||||
group.add(&delete_remote);
|
||||
dialog.set_extra_child(Some(&group));
|
||||
if !attached {
|
||||
dialog.set_extra_child(Some(&group));
|
||||
}
|
||||
dialog.add_response("cancel", &gettext("Cancel"));
|
||||
dialog.add_response("remove", &gettext("Stop Syncing"));
|
||||
dialog.set_response_appearance("remove", adw::ResponseAppearance::Destructive);
|
||||
@@ -988,7 +1014,7 @@ pub(crate) fn prompt_remove_sync_folder(ui: &Rc<Ui>, id: i64, path: &str, ondema
|
||||
&ui,
|
||||
Request::RemoveSyncFolder {
|
||||
id,
|
||||
delete_remote: delete_remote.is_active(),
|
||||
delete_remote: !attached && delete_remote.is_active(),
|
||||
},
|
||||
&gettext("Stopped syncing folder"),
|
||||
&gettext("Couldn't stop syncing the folder"),
|
||||
@@ -1291,6 +1317,7 @@ mod tests {
|
||||
|
||||
fn folder(state: &str) -> SyncFolderInfo {
|
||||
SyncFolderInfo {
|
||||
kind: "device".into(),
|
||||
id: 1,
|
||||
local_path: "/home/u/Documents".into(),
|
||||
remote_uid: "vol~link".into(),
|
||||
@@ -1303,6 +1330,16 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attached_my_files_folders_do_not_count_as_device_backups() {
|
||||
let mut attached = folder("error");
|
||||
attached.kind = "attached".into();
|
||||
assert_eq!(
|
||||
this_computer_subtitle(&[attached]),
|
||||
"No folders backed up yet"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_machine_with_no_folders_says_so() {
|
||||
assert_eq!(this_computer_subtitle(&[]), "No folders backed up yet");
|
||||
|
||||
@@ -0,0 +1,822 @@
|
||||
use crate::*;
|
||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
use pdfs_core::control::{GalleryAction, GalleryAlbum, GalleryFolder, GalleryPlace};
|
||||
|
||||
const PAGE: usize = 150;
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Row {
|
||||
Albums(Vec<GalleryAlbum>, i32),
|
||||
Heading(String),
|
||||
Media(Vec<(PhotoItem, (i32, i32))>),
|
||||
Folder(GalleryFolder),
|
||||
}
|
||||
|
||||
struct Place {
|
||||
album: GalleryAlbum,
|
||||
parent_offset: f64,
|
||||
}
|
||||
|
||||
pub(crate) struct Browser {
|
||||
ui: Rc<Ui>,
|
||||
window: glib::WeakRef<gtk4::Window>,
|
||||
rows: gio::ListStore,
|
||||
scroll: gtk4::ScrolledWindow,
|
||||
title: adw::WindowTitle,
|
||||
status: gtk4::Label,
|
||||
choose: gtk4::Button,
|
||||
trail: RefCell<Vec<Place>>,
|
||||
chooser: RefCell<Option<Vec<GalleryFolder>>>,
|
||||
cover: RefCell<Option<GalleryPlace>>,
|
||||
albums: RefCell<Vec<GalleryAlbum>>,
|
||||
items: RefCell<Vec<PhotoItem>>,
|
||||
width: Cell<i32>,
|
||||
generation: Cell<u64>,
|
||||
total: Cell<usize>,
|
||||
loading: Cell<bool>,
|
||||
unlocked: Cell<bool>,
|
||||
thumbnail_slots: tokio::sync::Semaphore,
|
||||
}
|
||||
|
||||
impl Browser {
|
||||
pub(crate) fn close(&self) {
|
||||
if let Some(window) = self.window.upgrade() {
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn refresh_visibility(self: &Rc<Self>) {
|
||||
if self.chooser.borrow().is_none() {
|
||||
self.trail.borrow_mut().clear();
|
||||
self.cover.borrow_mut().take();
|
||||
self.load(0, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn current(&self) -> Option<GalleryAlbum> {
|
||||
self.trail.borrow().last().map(|place| place.album.clone())
|
||||
}
|
||||
|
||||
fn next_request(&self) -> u64 {
|
||||
let next = self.generation.get().wrapping_add(1);
|
||||
self.generation.set(next);
|
||||
next
|
||||
}
|
||||
|
||||
fn load(self: &Rc<Self>, offset: usize, restore: f64) {
|
||||
self.choose.set_visible(false);
|
||||
self.loading.set(true);
|
||||
let generation = if offset == 0 {
|
||||
self.total.set(0);
|
||||
self.albums.borrow_mut().clear();
|
||||
self.items.borrow_mut().clear();
|
||||
self.rows.remove_all();
|
||||
self.next_request()
|
||||
} else {
|
||||
self.generation.get()
|
||||
};
|
||||
let album = self.current();
|
||||
self.title.set_title(
|
||||
album
|
||||
.as_ref()
|
||||
.map_or_else(|| gettext("Drive Galleries"), |a| a.folder.name.clone())
|
||||
.as_str(),
|
||||
);
|
||||
self.status.set_label(&gettext("Loading…"));
|
||||
let rx = spawn_request(
|
||||
self.ui.dirs.control_socket(),
|
||||
Request::DriveGallery {
|
||||
action: GalleryAction::List {
|
||||
album: album.map(|a| a.place),
|
||||
offset,
|
||||
limit: PAGE,
|
||||
},
|
||||
},
|
||||
);
|
||||
let browser = self.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
let result = rx.recv().await;
|
||||
if browser.generation.get() != generation {
|
||||
return;
|
||||
}
|
||||
browser.loading.set(false);
|
||||
match result {
|
||||
Ok(Ok(Response::GalleryPage {
|
||||
albums,
|
||||
items,
|
||||
total,
|
||||
})) => {
|
||||
if offset == 0 {
|
||||
*browser.albums.borrow_mut() = albums;
|
||||
}
|
||||
browser.items.borrow_mut().extend(items);
|
||||
browser.total.set(total);
|
||||
let empty = total == 0 && browser.albums.borrow().is_empty();
|
||||
let message = if browser.cover.borrow().is_some() {
|
||||
gettext("Choose a photo or video as the album cover.")
|
||||
} else if empty {
|
||||
gettext("This gallery is empty.")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
browser.status.set_label(&message);
|
||||
browser.rebuild();
|
||||
if restore > 0.0 {
|
||||
let scroll = browser.scroll.clone();
|
||||
glib::idle_add_local_once(move || scroll.vadjustment().set_value(restore));
|
||||
}
|
||||
}
|
||||
Ok(Ok(Response::Error { message, .. })) => browser.status.set_label(&message),
|
||||
_ => browser
|
||||
.status
|
||||
.set_label(&gettext("Couldn't reach Proton Drive.")),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn browse_folders(self: &Rc<Self>) {
|
||||
let Some(trail) = self.chooser.borrow().clone() else {
|
||||
return;
|
||||
};
|
||||
let generation = self.next_request();
|
||||
let parent = trail.last().map(|folder| folder.uid.clone());
|
||||
self.choose.set_visible(parent.is_some());
|
||||
self.title.set_title(&gettext("Choose a Drive Folder"));
|
||||
self.rows.remove_all();
|
||||
let rx = spawn_request(
|
||||
self.ui.dirs.control_socket(),
|
||||
Request::DriveGallery {
|
||||
action: GalleryAction::Folders { parent },
|
||||
},
|
||||
);
|
||||
let browser = self.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
let result = rx.recv().await;
|
||||
if browser.generation.get() != generation {
|
||||
return;
|
||||
}
|
||||
match result {
|
||||
Ok(Ok(Response::GalleryFolders { folders })) => {
|
||||
browser
|
||||
.status
|
||||
.set_label(&gettext("Open a folder, then choose Add This Folder."));
|
||||
for folder in folders {
|
||||
browser
|
||||
.rows
|
||||
.append(&BoxedAnyObject::new(Row::Folder(folder)));
|
||||
}
|
||||
}
|
||||
Ok(Ok(Response::Error { message, .. })) => browser.status.set_label(&message),
|
||||
_ => browser
|
||||
.status
|
||||
.set_label(&gettext("Couldn't reach Proton Drive.")),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn change(self: &Rc<Self>, action: GalleryAction) {
|
||||
let unlock = matches!(action, GalleryAction::Unlock { .. });
|
||||
let lock = matches!(action, GalleryAction::Lock);
|
||||
if lock {
|
||||
self.trail.borrow_mut().clear();
|
||||
self.cover.borrow_mut().take();
|
||||
self.items.borrow_mut().clear();
|
||||
self.rows.remove_all();
|
||||
}
|
||||
let generation = self.next_request();
|
||||
self.status.set_label(&gettext("Updating gallery…"));
|
||||
let rx = spawn_request(
|
||||
self.ui.dirs.control_socket(),
|
||||
Request::DriveGallery { action },
|
||||
);
|
||||
let browser = self.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
let result = rx.recv().await;
|
||||
if browser.generation.get() != generation {
|
||||
if unlock && matches!(result, Ok(Ok(Response::Ok { .. }))) {
|
||||
browser.lock();
|
||||
}
|
||||
return;
|
||||
}
|
||||
match result {
|
||||
Ok(Ok(Response::Ok { .. })) => {
|
||||
if unlock || lock {
|
||||
browser.unlocked.set(unlock);
|
||||
}
|
||||
browser.chooser.borrow_mut().take();
|
||||
browser.load(0, 0.0);
|
||||
}
|
||||
Ok(Ok(Response::Error { message, .. })) => browser.status.set_label(&message),
|
||||
_ => browser
|
||||
.status
|
||||
.set_label(&gettext("Couldn't reach Proton Drive.")),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn lock(&self) {
|
||||
let _ = spawn_request(
|
||||
self.ui.dirs.control_socket(),
|
||||
Request::DriveGallery {
|
||||
action: GalleryAction::Lock,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn prompt_unlock(self: &Rc<Self>) {
|
||||
let Some(parent) = self.window.upgrade() else {
|
||||
return;
|
||||
};
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Unlock Hidden Albums"))
|
||||
.body(gettext("Enter your Gallery passphrase. On first use, choose one with at least 12 characters."))
|
||||
.build();
|
||||
let password = gtk4::PasswordEntry::builder().show_peek_icon(true).build();
|
||||
dialog.set_extra_child(Some(&password));
|
||||
dialog.add_response("cancel", &gettext("Cancel"));
|
||||
dialog.add_response("unlock", &gettext("Unlock"));
|
||||
dialog.set_response_appearance("unlock", adw::ResponseAppearance::Suggested);
|
||||
dialog.set_default_response(Some("unlock"));
|
||||
let browser = Rc::downgrade(self);
|
||||
dialog.connect_response(None, move |_, response| {
|
||||
let passphrase = password.text().to_string();
|
||||
password.set_text("");
|
||||
if response == "unlock"
|
||||
&& let Some(browser) = browser.upgrade()
|
||||
{
|
||||
browser.change(GalleryAction::Unlock { passphrase });
|
||||
}
|
||||
});
|
||||
dialog.present(Some(&parent));
|
||||
}
|
||||
|
||||
fn back(self: &Rc<Self>) {
|
||||
let choosing = self.chooser.borrow().is_some();
|
||||
if choosing {
|
||||
let went_up = self
|
||||
.chooser
|
||||
.borrow_mut()
|
||||
.as_mut()
|
||||
.and_then(Vec::pop)
|
||||
.is_some();
|
||||
if went_up {
|
||||
self.browse_folders();
|
||||
} else {
|
||||
self.chooser.borrow_mut().take();
|
||||
self.load(0, 0.0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let at_target = self
|
||||
.cover
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.zip(self.current())
|
||||
.is_some_and(|(target, current)| target.album == current.place.album);
|
||||
if at_target {
|
||||
self.cover.borrow_mut().take();
|
||||
self.status.set_label("");
|
||||
return;
|
||||
}
|
||||
let previous = self.trail.borrow_mut().pop();
|
||||
if let Some(previous) = previous {
|
||||
self.load(0, previous.parent_offset);
|
||||
} else if let Some(window) = self.window.upgrade() {
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
|
||||
fn rebuild(&self) {
|
||||
if self.chooser.borrow().is_some() {
|
||||
return;
|
||||
}
|
||||
let width = (self.width.get() - 24).max(240);
|
||||
let columns = ((width + 8) / 216).max(1);
|
||||
let card = (width - 8 * (columns - 1)) / columns;
|
||||
let mut rows = Vec::new();
|
||||
for albums in self.albums.borrow().chunks(columns as usize) {
|
||||
rows.push(BoxedAnyObject::new(Row::Albums(albums.to_vec(), card)));
|
||||
}
|
||||
let items = self.items.borrow();
|
||||
let period = |item: &PhotoItem| {
|
||||
glib::DateTime::from_unix_local(item.capture_time)
|
||||
.ok()
|
||||
.and_then(|date| date.format("%B %Y").ok())
|
||||
.map(|value| value.to_string())
|
||||
.unwrap_or_else(|| gettext("Unknown date"))
|
||||
};
|
||||
for section in items.chunk_by(|a, b| period(a) == period(b)) {
|
||||
rows.push(BoxedAnyObject::new(Row::Heading(period(§ion[0]))));
|
||||
let ratios: Vec<_> = section
|
||||
.iter()
|
||||
.map(|item| item.ratio.unwrap_or(1.0))
|
||||
.collect();
|
||||
let mut items = section.iter();
|
||||
for sizes in super::photos::plan_justified(&ratios, width, 180) {
|
||||
rows.push(BoxedAnyObject::new(Row::Media(
|
||||
items.by_ref().cloned().zip(sizes).collect(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
self.rows.splice(0, self.rows.n_items(), &rows);
|
||||
}
|
||||
|
||||
fn preview(self: &Rc<Self>, item: PhotoItem) {
|
||||
let video = item.kind == PhotoKind::Video;
|
||||
let request = if video {
|
||||
Request::OpenGalleryVideo { uid: item.uid }
|
||||
} else {
|
||||
Request::OpenGalleryImage { uid: item.uid }
|
||||
};
|
||||
let reply = spawn_request(self.ui.dirs.control_socket(), request);
|
||||
let browser = self.clone();
|
||||
let generation = self.generation.get();
|
||||
glib::spawn_future_local(async move {
|
||||
let result = reply.recv().await;
|
||||
let close = |token| {
|
||||
let _ = spawn_request(
|
||||
browser.ui.dirs.control_socket(),
|
||||
Request::CloseGalleryVideo { token },
|
||||
);
|
||||
};
|
||||
let parent = browser
|
||||
.window
|
||||
.upgrade()
|
||||
.filter(|_| generation == browser.generation.get());
|
||||
let Some(parent) = parent else {
|
||||
if let Ok(Ok(Response::GalleryVideo { token, .. })) = result {
|
||||
close(token);
|
||||
}
|
||||
return;
|
||||
};
|
||||
let preview = gtk4::Window::builder()
|
||||
.transient_for(&parent)
|
||||
.destroy_with_parent(true)
|
||||
.title(item.name.unwrap_or_else(|| gettext("Preview")))
|
||||
.default_width(850)
|
||||
.default_height(600)
|
||||
.build();
|
||||
preview.add_css_class("photo-viewer-window");
|
||||
match result {
|
||||
Ok(Ok(Response::GalleryVideo { url, token, .. })) => {
|
||||
let player = gtk4::Video::builder().autoplay(true).build();
|
||||
player.set_file(Some(&gio::File::for_uri(&url)));
|
||||
preview.set_child(Some(&player));
|
||||
let socket = browser.ui.dirs.control_socket();
|
||||
preview.connect_close_request(move |_| {
|
||||
player.set_media_stream(gtk4::MediaStream::NONE);
|
||||
let _ = spawn_request(
|
||||
socket.clone(),
|
||||
Request::CloseGalleryVideo {
|
||||
token: token.clone(),
|
||||
},
|
||||
);
|
||||
glib::Propagation::Proceed
|
||||
});
|
||||
}
|
||||
Ok(Ok(Response::FilePath { path })) => {
|
||||
let picture = gtk4::Picture::for_filename(path);
|
||||
preview.set_child(Some(&picture));
|
||||
}
|
||||
Ok(Ok(Response::Error { message, .. })) => {
|
||||
browser.status.set_label(&message);
|
||||
return;
|
||||
}
|
||||
_ => {
|
||||
browser
|
||||
.status
|
||||
.set_label(&gettext("Couldn't open the file."));
|
||||
return;
|
||||
}
|
||||
}
|
||||
preview.present();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn open(ui: &Rc<Ui>) {
|
||||
if let Some(browser) = ui.drive_galleries.borrow().upgrade()
|
||||
&& let Some(window) = browser.window.upgrade()
|
||||
{
|
||||
window.present();
|
||||
return;
|
||||
}
|
||||
let Some(parent) = ui_window(ui) else { return };
|
||||
let window = gtk4::Window::builder()
|
||||
.transient_for(&parent)
|
||||
.default_width(1020)
|
||||
.default_height(760)
|
||||
.build();
|
||||
let title = adw::WindowTitle::new(&gettext("Drive Galleries"), "");
|
||||
let header = adw::HeaderBar::builder().title_widget(&title).build();
|
||||
let back = gtk4::Button::from_icon_name("go-previous-symbolic");
|
||||
back.set_tooltip_text(Some(&gettext("Back")));
|
||||
header.pack_start(&back);
|
||||
let add = gtk4::Button::with_label(&gettext("Add Gallery"));
|
||||
let choose = gtk4::Button::with_label(&gettext("Add This Folder"));
|
||||
choose.set_visible(false);
|
||||
header.pack_end(&add);
|
||||
header.pack_end(&choose);
|
||||
let more = gtk4::Button::from_icon_name("view-more-symbolic");
|
||||
more.set_tooltip_text(Some(&gettext("Gallery Options")));
|
||||
header.pack_end(&more);
|
||||
let rows = gio::ListStore::new::<BoxedAnyObject>();
|
||||
let factory = gtk4::SignalListItemFactory::new();
|
||||
let list = gtk4::ListView::builder()
|
||||
.model(>k4::NoSelection::new(Some(rows.clone())))
|
||||
.factory(&factory)
|
||||
.build();
|
||||
list.add_css_class("drive-gallery-rows");
|
||||
let scroll = gtk4::ScrolledWindow::builder()
|
||||
.vexpand(true)
|
||||
.hscrollbar_policy(gtk4::PolicyType::External)
|
||||
.child(&list)
|
||||
.build();
|
||||
let status = gtk4::Label::new(None);
|
||||
status.set_wrap(true);
|
||||
let content = gtk4::Box::new(gtk4::Orientation::Vertical, 0);
|
||||
content.append(&header);
|
||||
content.append(&status);
|
||||
content.append(&scroll);
|
||||
window.set_child(Some(&content));
|
||||
let browser = Rc::new(Browser {
|
||||
ui: ui.clone(),
|
||||
window: window.downgrade(),
|
||||
rows,
|
||||
scroll: scroll.clone(),
|
||||
title,
|
||||
status,
|
||||
choose: choose.clone(),
|
||||
trail: RefCell::new(Vec::new()),
|
||||
chooser: RefCell::new(None),
|
||||
cover: RefCell::new(None),
|
||||
albums: RefCell::new(Vec::new()),
|
||||
items: RefCell::new(Vec::new()),
|
||||
width: Cell::new(0),
|
||||
generation: Cell::new(0),
|
||||
total: Cell::new(0),
|
||||
loading: Cell::new(false),
|
||||
unlocked: Cell::new(false),
|
||||
thumbnail_slots: tokio::sync::Semaphore::new(8),
|
||||
});
|
||||
*ui.drive_galleries.borrow_mut() = Rc::downgrade(&browser);
|
||||
window.connect_close_request({
|
||||
let browser = browser.clone();
|
||||
move |_| {
|
||||
*browser.ui.drive_galleries.borrow_mut() = std::rc::Weak::new();
|
||||
browser.next_request();
|
||||
if browser.unlocked.get() {
|
||||
browser.lock();
|
||||
}
|
||||
glib::Propagation::Proceed
|
||||
}
|
||||
});
|
||||
let weak = Rc::downgrade(&browser);
|
||||
factory.connect_bind(move |_, item| {
|
||||
let Some(browser) = weak.upgrade() else {
|
||||
return;
|
||||
};
|
||||
let Some(list_item) = item.downcast_ref::<gtk4::ListItem>() else {
|
||||
return;
|
||||
};
|
||||
let Some(data) = list_item.item().and_downcast::<BoxedAnyObject>() else {
|
||||
return;
|
||||
};
|
||||
let row = data.borrow::<Row>();
|
||||
let line = gtk4::Box::new(gtk4::Orientation::Horizontal, 8);
|
||||
line.set_margin_start(12);
|
||||
line.set_margin_end(12);
|
||||
line.set_margin_bottom(8);
|
||||
match &*row {
|
||||
Row::Heading(label) => {
|
||||
let label = gtk4::Label::new(Some(label));
|
||||
label.add_css_class("heading");
|
||||
line.append(&label);
|
||||
}
|
||||
Row::Folder(folder) => {
|
||||
let button = gtk4::Button::with_label(&folder.name);
|
||||
button.set_hexpand(true);
|
||||
button.connect_clicked({
|
||||
let weak = Rc::downgrade(&browser);
|
||||
let folder = folder.clone();
|
||||
move |_| {
|
||||
if let Some(browser) = weak.upgrade() {
|
||||
if let Some(trail) = browser.chooser.borrow_mut().as_mut() {
|
||||
trail.push(folder.clone());
|
||||
}
|
||||
browser.browse_folders();
|
||||
}
|
||||
}
|
||||
});
|
||||
line.append(&button);
|
||||
}
|
||||
Row::Albums(albums, width) => {
|
||||
for album in albums {
|
||||
let items = ngettext_f("{n} item", "{n} items", album.items, &[]);
|
||||
let mut detail = if album.folders > 0 {
|
||||
let folders = ngettext_f("{n} folder", "{n} folders", album.folders, &[]);
|
||||
format!("{items} · {folders}")
|
||||
} else {
|
||||
items
|
||||
};
|
||||
if album.hidden {
|
||||
detail = format!("{detail} · {}", gettext("Hidden"));
|
||||
}
|
||||
let button = tile(
|
||||
&browser,
|
||||
&album.folder.name,
|
||||
album.cover.as_deref(),
|
||||
*width,
|
||||
130,
|
||||
Some(&detail),
|
||||
false,
|
||||
);
|
||||
button.connect_clicked({
|
||||
let weak = Rc::downgrade(&browser);
|
||||
let album = album.clone();
|
||||
move |_| {
|
||||
if let Some(browser) = weak.upgrade() {
|
||||
browser.trail.borrow_mut().push(Place {
|
||||
album: album.clone(),
|
||||
parent_offset: browser.scroll.vadjustment().value(),
|
||||
});
|
||||
browser.load(0, 0.0);
|
||||
}
|
||||
}
|
||||
});
|
||||
line.append(&button);
|
||||
}
|
||||
}
|
||||
Row::Media(items) => {
|
||||
line.set_spacing(super::photos::TILE_GAP);
|
||||
for (item, (width, height)) in items {
|
||||
let button = tile(
|
||||
&browser,
|
||||
item.name.as_deref().unwrap_or(""),
|
||||
Some(&item.uid),
|
||||
*width,
|
||||
*height,
|
||||
None,
|
||||
item.kind == PhotoKind::Video,
|
||||
);
|
||||
button.connect_clicked({
|
||||
let weak = Rc::downgrade(&browser);
|
||||
let item = item.clone();
|
||||
move |_| {
|
||||
if let Some(browser) = weak.upgrade() {
|
||||
let cover = browser.cover.borrow_mut().take();
|
||||
if let Some(album) = cover {
|
||||
browser.change(GalleryAction::Cover {
|
||||
album,
|
||||
uid: item.uid.clone(),
|
||||
});
|
||||
} else {
|
||||
browser.preview(item.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
line.append(&button);
|
||||
}
|
||||
}
|
||||
}
|
||||
list_item.set_child(Some(&line));
|
||||
});
|
||||
factory.connect_unbind(|_, item| {
|
||||
if let Some(item) = item.downcast_ref::<gtk4::ListItem>() {
|
||||
item.set_child(gtk4::Widget::NONE);
|
||||
}
|
||||
});
|
||||
back.connect_clicked({
|
||||
let weak = Rc::downgrade(&browser);
|
||||
move |_| {
|
||||
if let Some(browser) = weak.upgrade() {
|
||||
browser.back();
|
||||
}
|
||||
}
|
||||
});
|
||||
add.connect_clicked({
|
||||
let weak = Rc::downgrade(&browser);
|
||||
let choose = choose.clone();
|
||||
move |_| {
|
||||
if let Some(browser) = weak.upgrade() {
|
||||
choose.set_visible(true);
|
||||
*browser.chooser.borrow_mut() = Some(Vec::new());
|
||||
browser.browse_folders();
|
||||
}
|
||||
}
|
||||
});
|
||||
choose.connect_clicked({
|
||||
let weak = Rc::downgrade(&browser);
|
||||
move |button| {
|
||||
if let Some(browser) = weak.upgrade() {
|
||||
let uid = browser
|
||||
.chooser
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.and_then(|trail| trail.last())
|
||||
.map(|f| f.uid.clone());
|
||||
if let Some(uid) = uid {
|
||||
button.set_visible(false);
|
||||
browser.change(GalleryAction::Add { uid });
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
more.connect_clicked({
|
||||
let weak = Rc::downgrade(&browser);
|
||||
move |button| {
|
||||
if let Some(browser) = weak.upgrade() {
|
||||
let mut menu = ActionMenu::new();
|
||||
menu.item(&gettext("Refresh"), {
|
||||
let browser = browser.clone();
|
||||
move || browser.change(GalleryAction::Refresh)
|
||||
});
|
||||
if browser.unlocked.get() {
|
||||
menu.item(&gettext("Lock Hidden Albums"), {
|
||||
let browser = browser.clone();
|
||||
move || browser.change(GalleryAction::Lock)
|
||||
});
|
||||
} else {
|
||||
menu.item(&gettext("Unlock Hidden Albums"), {
|
||||
let browser = browser.clone();
|
||||
move || browser.prompt_unlock()
|
||||
});
|
||||
}
|
||||
if let Some(album) = browser.current() {
|
||||
menu.item(&gettext("Choose Cover"), {
|
||||
let browser = browser.clone();
|
||||
let place = album.place.clone();
|
||||
move || {
|
||||
*browser.cover.borrow_mut() = Some(place.clone());
|
||||
browser.status.set_label(&gettext(
|
||||
"Choose a photo or video from this album or a subfolder.",
|
||||
));
|
||||
}
|
||||
});
|
||||
menu.item(&gettext("Public Link"), {
|
||||
let browser = browser.clone();
|
||||
let folder = album.folder.clone();
|
||||
move || {
|
||||
open_share_dialog_over(
|
||||
&browser.ui,
|
||||
&DirEntry {
|
||||
uid: folder.uid.clone(),
|
||||
name: folder.name.clone(),
|
||||
is_dir: true,
|
||||
..DirEntry::default()
|
||||
},
|
||||
browser.window.upgrade(),
|
||||
)
|
||||
}
|
||||
});
|
||||
if browser.trail.borrow().len() == 1 {
|
||||
menu.item(&gettext("Remove Gallery"), {
|
||||
let browser = browser.clone();
|
||||
move || {
|
||||
browser.trail.borrow_mut().clear();
|
||||
browser.change(GalleryAction::Remove {
|
||||
gallery: album.place.gallery.clone(),
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
menu.popup_at(
|
||||
button,
|
||||
f64::from(button.width()) / 2.0,
|
||||
f64::from(button.height()),
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
scroll.vadjustment().connect_value_changed({
|
||||
let weak = Rc::downgrade(&browser);
|
||||
move |adj| {
|
||||
if let Some(browser) = weak.upgrade() {
|
||||
let offset = browser.items.borrow().len();
|
||||
if browser.chooser.borrow().is_none()
|
||||
&& !browser.loading.get()
|
||||
&& offset < browser.total.get()
|
||||
&& adj.value() + adj.page_size() + 800.0 >= adj.upper()
|
||||
{
|
||||
browser.load(offset, 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
scroll.add_tick_callback({
|
||||
let weak = Rc::downgrade(&browser);
|
||||
move |scroll, _| {
|
||||
let Some(browser) = weak.upgrade() else {
|
||||
return glib::ControlFlow::Break;
|
||||
};
|
||||
let width = scroll.width();
|
||||
if width > 0 && browser.width.replace(width) != width {
|
||||
browser.rebuild();
|
||||
}
|
||||
glib::ControlFlow::Continue
|
||||
}
|
||||
});
|
||||
window.present();
|
||||
browser.load(0, 0.0);
|
||||
}
|
||||
|
||||
fn tile(
|
||||
browser: &Rc<Browser>,
|
||||
title: &str,
|
||||
uid: Option<&str>,
|
||||
width: i32,
|
||||
height: i32,
|
||||
detail: Option<&str>,
|
||||
video: bool,
|
||||
) -> gtk4::Button {
|
||||
let picture = gtk4::Picture::builder()
|
||||
.content_fit(gtk4::ContentFit::Cover)
|
||||
.can_shrink(true)
|
||||
.width_request(if detail.is_some() {
|
||||
(width - 24).max(1)
|
||||
} else {
|
||||
width
|
||||
})
|
||||
.height_request(height)
|
||||
.build();
|
||||
let label = gtk4::Label::new(Some(title));
|
||||
label.set_ellipsize(gtk4::pango::EllipsizeMode::End);
|
||||
label.set_max_width_chars((width / 9).max(1));
|
||||
let box_ = gtk4::Box::new(gtk4::Orientation::Vertical, 4);
|
||||
let cover = gtk4::Overlay::new();
|
||||
cover.set_child(Some(&picture));
|
||||
let icon = gtk4::Image::from_icon_name(if detail.is_some() {
|
||||
"folder-symbolic"
|
||||
} else {
|
||||
"image-x-generic-symbolic"
|
||||
});
|
||||
icon.set_halign(gtk4::Align::Center);
|
||||
icon.set_valign(gtk4::Align::Center);
|
||||
icon.set_pixel_size(32);
|
||||
icon.add_css_class("dim-label");
|
||||
cover.add_overlay(&icon);
|
||||
cover.set_overflow(gtk4::Overflow::Hidden);
|
||||
let button = gtk4::Button::builder().tooltip_text(title).build();
|
||||
if let Some(detail) = detail {
|
||||
box_.append(&cover);
|
||||
box_.append(&label);
|
||||
let caption = gtk4::Label::new(Some(detail));
|
||||
caption.add_css_class("dim-label");
|
||||
caption.add_css_class("caption");
|
||||
caption.set_ellipsize(gtk4::pango::EllipsizeMode::End);
|
||||
box_.append(&caption);
|
||||
button.set_width_request(width);
|
||||
button.set_child(Some(&box_));
|
||||
} else {
|
||||
button.add_css_class("photo-tile");
|
||||
cover.add_css_class("photo-placeholder");
|
||||
label.add_css_class("photo-caption");
|
||||
label.set_valign(gtk4::Align::End);
|
||||
cover.add_overlay(&label);
|
||||
if video {
|
||||
let play = gtk4::Image::from_icon_name("media-playback-start-symbolic");
|
||||
play.add_css_class("photo-video-badge");
|
||||
play.add_css_class("photo-video-badge-small");
|
||||
play.set_halign(gtk4::Align::End);
|
||||
play.set_valign(gtk4::Align::Start);
|
||||
play.set_margin_end(8);
|
||||
play.set_margin_top(8);
|
||||
cover.add_overlay(&play);
|
||||
}
|
||||
button.set_child(Some(&cover));
|
||||
}
|
||||
if let Some(uid) = uid {
|
||||
let browser = browser.clone();
|
||||
let generation = browser.generation.get();
|
||||
let uid = uid.to_owned();
|
||||
let picture = picture.downgrade();
|
||||
let icon = icon.downgrade();
|
||||
glib::spawn_future_local(async move {
|
||||
let Ok(_permit) = browser.thumbnail_slots.acquire().await else {
|
||||
return;
|
||||
};
|
||||
if picture.upgrade().is_none() || generation != browser.generation.get() {
|
||||
return;
|
||||
}
|
||||
let request = spawn_request(
|
||||
browser.ui.dirs.control_socket(),
|
||||
Request::GalleryThumbnail { uid },
|
||||
);
|
||||
let response = request.recv().await;
|
||||
if generation == browser.generation.get()
|
||||
&& let (Some(picture), Ok(Ok(Response::GalleryThumbnail { data: Some(data) }))) =
|
||||
(picture.upgrade(), response)
|
||||
&& let Ok(bytes) = STANDARD.decode(data)
|
||||
&& let Ok(texture) = gtk4::gdk::Texture::from_bytes(&glib::Bytes::from_owned(bytes))
|
||||
{
|
||||
picture.set_paintable(Some(&texture));
|
||||
if let Some(icon) = icon.upgrade() {
|
||||
icon.set_visible(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
button
|
||||
}
|
||||
@@ -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<u64>,
|
||||
/// 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)
|
||||
}
|
||||
@@ -776,6 +788,9 @@ pub(crate) fn issue_text(issue: SyncIssue) -> String {
|
||||
SyncIssue::Access => gettext(
|
||||
"You can no longer change this on Proton Drive. Ask the owner for edit access, or export the file to keep your version.",
|
||||
),
|
||||
SyncIssue::Trashed => {
|
||||
gettext("Restore the item or folder from Trash, then retry the move.")
|
||||
}
|
||||
SyncIssue::Missing => gettext(
|
||||
"The folder this change belongs in is gone from Proton Drive. Export the file to keep your version.",
|
||||
),
|
||||
@@ -1243,6 +1258,7 @@ pub(crate) fn repaint_locations(ui: &Rc<Ui>, 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);
|
||||
@@ -1361,6 +1377,9 @@ fn location_row(ui: &Rc<Ui>, spec: &MountSpec) -> LocationRow {
|
||||
prompt_mountpoint(&ui)
|
||||
});
|
||||
}
|
||||
MountKind::Attached { sync_folder_id } => {
|
||||
add_device_items(ui, &mut menu, spec, *sync_folder_id);
|
||||
}
|
||||
MountKind::Device { sync_folder_id } => {
|
||||
row.add_suffix(&mode_button(ui, spec, *sync_folder_id));
|
||||
add_device_items(ui, &mut menu, spec, *sync_folder_id);
|
||||
@@ -1495,6 +1514,7 @@ fn folder_mode_label(ondemand: bool) -> String {
|
||||
/// synced folder's menu.
|
||||
fn add_device_items(ui: &Rc<Ui>, 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();
|
||||
@@ -1513,6 +1533,14 @@ fn add_device_items(ui: &Rc<Ui>, 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();
|
||||
@@ -1520,8 +1548,9 @@ fn add_device_items(ui: &Rc<Ui>, menu: &mut ActionMenu, spec: &MountSpec, id: i6
|
||||
// The folder's *current* mode, not a queued one: a switch that hasn't landed
|
||||
// yet has not moved the files anywhere.
|
||||
let is_ondemand = spec.mode == MountMode::OnDemand;
|
||||
let attached = matches!(spec.kind, MountKind::Attached { .. });
|
||||
menu.item(&gettext("Stop Syncing…"), move || {
|
||||
prompt_remove_sync_folder(&ui, id, &path, is_ondemand)
|
||||
prompt_remove_sync_folder(&ui, id, &path, is_ondemand, attached)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1699,7 +1728,7 @@ fn sync_folder_now(ui: &Rc<Ui>, id: i64) {
|
||||
pub(crate) fn location_icon(kind: &MountKind) -> &'static str {
|
||||
match kind {
|
||||
MountKind::MyFiles => "folder-remote-symbolic",
|
||||
MountKind::Device { .. } => "folder-symbolic",
|
||||
MountKind::Device { .. } | MountKind::Attached { .. } => "folder-symbolic",
|
||||
MountKind::Shared { .. } => "pdfs-people-symbolic",
|
||||
}
|
||||
}
|
||||
@@ -1716,6 +1745,7 @@ pub(crate) fn location_subtitle(spec: &MountSpec) -> String {
|
||||
match &spec.kind {
|
||||
MountKind::MyFiles => parts.push(gettext("My files")),
|
||||
MountKind::Shared { .. } => parts.push(gettext("Shared folder")),
|
||||
MountKind::Attached { .. } => parts.push(gettext("My files mirror")),
|
||||
MountKind::Device { .. } => match (spec.pending_mode, spec.mode) {
|
||||
(Some(MountMode::OnDemand), _) => parts.push(gettext("Going online only")),
|
||||
(Some(MountMode::Mirror), _) => parts.push(gettext("Switching to synced")),
|
||||
@@ -1725,7 +1755,10 @@ pub(crate) fn location_subtitle(spec: &MountSpec) -> String {
|
||||
(Some(MountMode::Unknown) | None, _) => parts.push(folder_mode_label(false)),
|
||||
},
|
||||
}
|
||||
if matches!(spec.kind, MountKind::Device { .. }) {
|
||||
if matches!(
|
||||
spec.kind,
|
||||
MountKind::Device { .. } | MountKind::Attached { .. }
|
||||
) {
|
||||
parts.push(match &spec.progress {
|
||||
Some(p) => sync_progress_label(p),
|
||||
None if spec.paused => pgettext("state", "paused"),
|
||||
@@ -1737,8 +1770,10 @@ pub(crate) fn location_subtitle(spec: &MountSpec) -> String {
|
||||
}
|
||||
// Only the locations that are supposed to have a session report its absence:
|
||||
// a mirror folder never has one.
|
||||
let expects_session = !matches!(spec.kind, MountKind::Device { .. })
|
||||
|| spec.mode == MountMode::OnDemand
|
||||
let expects_session = !matches!(
|
||||
spec.kind,
|
||||
MountKind::Device { .. } | MountKind::Attached { .. }
|
||||
) || spec.mode == MountMode::OnDemand
|
||||
|| spec.pending_mode == Some(MountMode::OnDemand);
|
||||
if expects_session && !spec.mounted {
|
||||
// Translators: a folder's files can't be opened right now.
|
||||
@@ -1769,6 +1804,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_attached_mirror_is_available_without_a_fuse_session() {
|
||||
let spec = spec(
|
||||
MountKind::Attached { sync_folder_id: 7 },
|
||||
MountMode::Mirror,
|
||||
false,
|
||||
);
|
||||
let subtitle = location_subtitle(&spec);
|
||||
assert!(subtitle.contains("My files mirror"), "{subtitle}");
|
||||
assert!(!subtitle.contains("not available"), "{subtitle}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mirror_folder_is_never_reported_as_unmounted() {
|
||||
// It is a plain local directory: no FUSE session is expected, so saying
|
||||
|
||||
@@ -683,6 +683,10 @@ pub(crate) fn sign_out(ui: &Rc<Ui>) {
|
||||
),
|
||||
&gettext("Sign Out"),
|
||||
move || {
|
||||
let galleries = ui.drive_galleries.borrow().upgrade();
|
||||
if let Some(galleries) = galleries {
|
||||
galleries.close();
|
||||
}
|
||||
// systemctl and the keyring both block, so run them on a worker.
|
||||
let ui = ui.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
|
||||
@@ -3,6 +3,7 @@ pub(crate) mod activity_text;
|
||||
pub(crate) mod albums;
|
||||
pub(crate) mod browser;
|
||||
pub(crate) mod devices;
|
||||
pub(crate) mod drive_galleries;
|
||||
pub(crate) mod duplicates;
|
||||
pub(crate) mod locations;
|
||||
pub(crate) mod login;
|
||||
@@ -12,9 +13,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;
|
||||
File diff suppressed because it is too large.
Load diff
+1342
-254
File diff suppressed because it is too large.
Load diff
@@ -267,7 +267,12 @@ fn want_map_thumb(ui: &Rc<Ui>, 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<Ui>, 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<Ui>, 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<Ui>, 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<Ui>, 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<Ui>, uid: String) {
|
||||
/// The open place's id, if a place is open.
|
||||
fn open_place_id(ui: &Ui) -> Option<u32> {
|
||||
ui.gallery.place.borrow().as_ref().map(|place| place.id)
|
||||
}
|
||||
|
||||
fn view_when_loaded(ui: &Rc<Ui>, 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);
|
||||
}
|
||||
@@ -422,9 +456,7 @@ fn open_place(ui: &Rc<Ui>, place: PlaceInfo) {
|
||||
|
||||
// As in an album: no filters, no Upload, no view switch — back leads out.
|
||||
ui.gallery.filters.set_visible(false);
|
||||
ui.gallery.view_switch.widget().set_visible(false);
|
||||
ui.gallery.upload.set_visible(false);
|
||||
ui.gallery.back.set_visible(true);
|
||||
sync_header(ui);
|
||||
ui.gallery.kind.set(None);
|
||||
ui.gallery.range.set(None);
|
||||
|
||||
@@ -439,9 +471,7 @@ pub(crate) fn close_place(ui: &Rc<Ui>) {
|
||||
return;
|
||||
}
|
||||
ui.gallery.title.set_title(&gettext("Photos"));
|
||||
ui.gallery.view_switch.widget().set_visible(true);
|
||||
ui.gallery.upload.set_visible(true);
|
||||
ui.gallery.back.set_visible(false);
|
||||
sync_header(ui);
|
||||
}
|
||||
|
||||
/// Show the place grid's own status page (loading, empty, or an error).
|
||||
|
||||
@@ -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<Ui>,
|
||||
widgets: AppsPrefs,
|
||||
rule_rows: RefCell<Vec<adw::ActionRow>>,
|
||||
}
|
||||
|
||||
/// Fill the page from the config and connect its controls.
|
||||
pub(crate) fn wire_apps_prefs(ui: &Rc<Ui>, 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<Apps>, 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<Apps>,
|
||||
row: &adw::EntryRow,
|
||||
store: impl Fn(&mut pdfs_core::config::AppConfig, Option<Vec<String>>) + '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<Apps>, 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<String> = 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<String> {
|
||||
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<Apps>, index: Option<usize>, 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());
|
||||
}
|
||||
}
|
||||
@@ -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<bool>,
|
||||
}
|
||||
|
||||
/// 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<Ui>, widgets: SyncPrefs) {
|
||||
let widgets = Rc::new(widgets);
|
||||
let rows: Rc<RefCell<Vec<adw::PreferencesRow>>> = 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<String> {
|
||||
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<Ui>,
|
||||
widgets: &Rc<SyncPrefs>,
|
||||
rows: &Rc<RefCell<Vec<adw::PreferencesRow>>>,
|
||||
edit: impl FnOnce(&mut Vec<String>),
|
||||
) -> 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<Ui>,
|
||||
widgets: &Rc<SyncPrefs>,
|
||||
rows: &Rc<RefCell<Vec<adw::PreferencesRow>>>,
|
||||
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<String>, Vec<String>) = 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!("<tt>{}</tt>", 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<Ui>,
|
||||
widgets: &Rc<SyncPrefs>,
|
||||
rows: &Rc<RefCell<Vec<adw::PreferencesRow>>>,
|
||||
pattern: String,
|
||||
) -> adw::ActionRow {
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(format!("<tt>{}</tt>", 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<Ui>) {
|
||||
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<Ui>, message: &str) {
|
||||
ui.status.prefs.add_toast(
|
||||
adw::Toast::builder()
|
||||
.title(message)
|
||||
.use_markup(false)
|
||||
.timeout(6)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
@@ -279,10 +279,7 @@ pub(crate) fn wire_shared(ui: &Rc<Ui>, 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<Ui>, 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<Ui>) {
|
||||
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<Ui>, 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<Ui>, 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
|
||||
|
||||
@@ -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<Ui>, 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<Ui>, 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<Ui>, 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),
|
||||
|
||||
@@ -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<Vec<adw::ActionRow>>,
|
||||
/// The synced folders kept on this computer the rows were built for, as
|
||||
/// id and root.
|
||||
pub(crate) folders: RefCell<Vec<(i64, PathBuf)>>,
|
||||
/// Counts painted, so a slow read from an older paint does not land on
|
||||
/// newer rows.
|
||||
pub(crate) generation: Cell<u64>,
|
||||
}
|
||||
|
||||
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<Ui>) {
|
||||
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<Ui>, 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<Ui>) {
|
||||
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<PathBuf> = 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::<Vec<_>>()
|
||||
})
|
||||
.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<Ui>, folder: Option<i64>) {
|
||||
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<String> {
|
||||
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<Ui>,
|
||||
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<bool, String> = 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());
|
||||
}
|
||||
}
|
||||
@@ -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<Vec<adw::ActionRow>>,
|
||||
/// 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<bool>,
|
||||
/// Whether the daemon reported an import running on the last poll.
|
||||
pub(crate) running: Cell<bool>,
|
||||
/// 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<bool>,
|
||||
}
|
||||
|
||||
/// 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<Ui>, 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<Ui>) {
|
||||
.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<Ui>, 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<Ui>, 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<Ui>, 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<Ui>, 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<Ui>) {
|
||||
if ui.takeout.inflight.get() {
|
||||
return;
|
||||
@@ -634,124 +702,250 @@ pub(crate) fn refresh_takeout(ui: &Rc<Ui>) {
|
||||
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<Ui>, 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<Ui>, 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<Ui>, 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<Ui>, 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<Ui>, 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<Ui>, 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<Ui>, 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<Ui>, 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()
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -82,10 +82,10 @@ pub(crate) fn start_update_checks(ui: &Rc<Ui>) {
|
||||
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);
|
||||
|
||||
@@ -10,6 +10,12 @@ use crate::*;
|
||||
pub(crate) struct ToggleGroup {
|
||||
widget: gtk4::Widget,
|
||||
options: Options,
|
||||
/// Each option's label, kept while [`Self::set_compact`] shows the icons
|
||||
/// alone.
|
||||
labels: Rc<RefCell<Vec<String>>>,
|
||||
/// Each option's own tooltip, as given.
|
||||
tooltips: Rc<[Option<String>]>,
|
||||
compact: Rc<Cell<bool>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -47,6 +53,9 @@ impl ToggleGroup {
|
||||
Self {
|
||||
widget,
|
||||
options: Options::Toggles(toggles),
|
||||
labels: Rc::new(RefCell::new(labels_of(options))),
|
||||
tooltips: tooltips_of(options),
|
||||
compact: Rc::default(),
|
||||
}
|
||||
} else {
|
||||
let bar = gtk4::Box::new(gtk4::Orientation::Horizontal, 0);
|
||||
@@ -71,6 +80,9 @@ impl ToggleGroup {
|
||||
Self {
|
||||
widget: bar.upcast(),
|
||||
options: Options::Buttons(buttons),
|
||||
labels: Rc::new(RefCell::new(labels_of(options))),
|
||||
tooltips: tooltips_of(options),
|
||||
compact: Rc::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,15 +127,69 @@ impl ToggleGroup {
|
||||
}
|
||||
|
||||
pub(crate) fn set_label(&self, index: u32, label: &str) {
|
||||
if let Some(stored) = self.labels.borrow_mut().get_mut(index as usize) {
|
||||
*stored = label.to_string();
|
||||
}
|
||||
self.show_label(index, label);
|
||||
}
|
||||
|
||||
/// Put an icon before each option's label, in option order.
|
||||
pub(crate) fn set_icons(&self, icons: &[&str]) {
|
||||
match &self.options {
|
||||
Options::Toggles(toggles) => {
|
||||
for (toggle, icon) in toggles.iter().zip(icons) {
|
||||
toggle.set_property("icon-name", *icon);
|
||||
}
|
||||
}
|
||||
Options::Buttons(buttons) => {
|
||||
let labels = self.labels.borrow();
|
||||
for ((button, icon), label) in buttons.iter().zip(icons).zip(labels.iter()) {
|
||||
let face = adw::ButtonContent::builder()
|
||||
.icon_name(*icon)
|
||||
.label(label.as_str())
|
||||
.build();
|
||||
button.set_child(Some(&face));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Show the icons alone, for a narrow window, with each label moved to
|
||||
/// the option's tooltip; or bring the labels back.
|
||||
pub(crate) fn set_compact(&self, compact: bool) {
|
||||
if self.compact.replace(compact) == compact {
|
||||
return;
|
||||
}
|
||||
let labels = self.labels.borrow().clone();
|
||||
for (index, label) in labels.iter().enumerate() {
|
||||
self.show_label(index as u32, label);
|
||||
}
|
||||
}
|
||||
|
||||
/// Show `label` on option `index`, or only in its tooltip while compact.
|
||||
fn show_label(&self, index: u32, label: &str) {
|
||||
let compact = self.compact.get();
|
||||
let shown = if compact { "" } else { label };
|
||||
let given = self.tooltips.get(index as usize).cloned().flatten();
|
||||
let tooltip = if compact {
|
||||
Some(label.to_string())
|
||||
} else {
|
||||
given
|
||||
};
|
||||
match &self.options {
|
||||
Options::Toggles(toggles) => {
|
||||
if let Some(toggle) = toggles.get(index as usize) {
|
||||
toggle.set_property("label", label);
|
||||
toggle.set_property("label", shown);
|
||||
toggle.set_property("tooltip", tooltip.unwrap_or_default());
|
||||
}
|
||||
}
|
||||
Options::Buttons(buttons) => {
|
||||
if let Some(button) = buttons.get(index as usize) {
|
||||
button.set_label(label);
|
||||
match button.child().and_downcast::<adw::ButtonContent>() {
|
||||
Some(face) => face.set_label(shown),
|
||||
None => button.set_label(shown),
|
||||
}
|
||||
button.set_tooltip_text(tooltip.as_deref());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -271,6 +337,19 @@ fn shortcuts_dialog(groups: &[(String, Vec<(&str, String)>)]) -> adw::Dialog {
|
||||
.build()
|
||||
}
|
||||
|
||||
/// The labels of `(label, tooltip)` options.
|
||||
fn labels_of(options: &[(&str, Option<&str>)]) -> Vec<String> {
|
||||
options.iter().map(|(label, _)| label.to_string()).collect()
|
||||
}
|
||||
|
||||
/// The tooltips of `(label, tooltip)` options.
|
||||
fn tooltips_of(options: &[(&str, Option<&str>)]) -> Rc<[Option<String>]> {
|
||||
options
|
||||
.iter()
|
||||
.map(|(_, tooltip)| tooltip.map(str::to_string))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -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::<gtk4::ScrolledWindow>()
|
||||
.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::<gtk4::ScrolledWindow>()
|
||||
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")
|
||||
@@ -478,7 +521,7 @@ pub(crate) fn text_column(
|
||||
|
||||
fn resize_grid_tile(thumbnail: >k4::Overlay, label: >k4::Label, size: i32) {
|
||||
resize_file_thumbnail(thumbnail, size, grid_fallback_size(size));
|
||||
let name_width = (size / 6 + 1).clamp(8, 24);
|
||||
let name_width = (size / 6 + 1).clamp(8, 40);
|
||||
label.set_width_chars(name_width);
|
||||
label.set_max_width_chars(name_width);
|
||||
}
|
||||
|
||||
@@ -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<Ui>, 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<RefCell<Option<FolderRules>>> = 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<Ui>,
|
||||
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(),
|
||||
);
|
||||
}
|
||||
@@ -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<gtk4::Widget>, 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<gtk4::Widget>,
|
||||
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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Vec<(String, Option<String>)>>,
|
||||
pub(super) socket: RefCell<PathBuf>,
|
||||
}
|
||||
|
||||
#[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<Box<dyn Future<Output = Result<(), glib::Error>> + '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<imp::PhotoDrag>)
|
||||
@extends gdk::ContentProvider;
|
||||
}
|
||||
|
||||
impl PhotoDrag {
|
||||
pub(crate) fn new(socket: PathBuf, photos: Vec<(String, Option<String>)>) -> 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<String>)>,
|
||||
) -> Result<Vec<PathBuf>, 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/<uid>/<name>`, 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<PathBuf> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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<Option<(f64, bool)>> = 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<Ui>, 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<Ui>, 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::<gtk4::Picture>()
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@@ -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<Ui>, 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<Ui>, entry: &DirEntry, parent: Option<gtk4::Window>) {
|
||||
if !*ui.mounted.borrow() {
|
||||
toast_error(
|
||||
ui,
|
||||
@@ -491,7 +498,7 @@ pub(crate) fn open_share_dialog(ui: &Rc<Ui>, 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.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Shared thumbnails for ordinary Drive image files.
|
||||
//! Shared thumbnails for ordinary Drive image and video files.
|
||||
//!
|
||||
//! The Photos gallery has timeline-specific batching and aspect-ratio state.
|
||||
//! File listings need a smaller, reusable surface: one loader backs My files,
|
||||
@@ -6,11 +6,12 @@
|
||||
//! so a replaced image never flashes its previous contents.
|
||||
|
||||
use crate::*;
|
||||
use pdfs_core::control::{FileThumbRequest, is_thumbnail_image_name};
|
||||
use pdfs_core::control::{FileThumbRequest, is_thumbnail_media_name, is_video_file_name};
|
||||
|
||||
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<HashSet<String>>,
|
||||
missing_order: RefCell<VecDeque<String>>,
|
||||
wanted: RefCell<HashMap<String, Vec<gtk4::Picture>>>,
|
||||
/// 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<HashMap<String, BlankTile>>,
|
||||
queue: RefCell<VecDeque<FileThumbRequest>>,
|
||||
inflight: Cell<bool>,
|
||||
source: RefCell<Option<glib::SourceId>>,
|
||||
@@ -27,6 +31,18 @@ pub(crate) struct FileThumbnailState {
|
||||
generation_loading: Cell<bool>,
|
||||
}
|
||||
|
||||
/// What a tile left blank needs to be asked for again.
|
||||
struct BlankTile {
|
||||
request: FileThumbRequest,
|
||||
pictures: Vec<glib::WeakRef<gtk4::Picture>>,
|
||||
}
|
||||
|
||||
/// 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<String, BlankTile>) {
|
||||
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<Ui>) {
|
||||
let tiles: Vec<(String, FileThumbRequest, Vec<gtk4::Picture>)> = {
|
||||
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
|
||||
@@ -125,6 +221,18 @@ pub(crate) fn file_thumbnail_widget(size: i32, fallback_size: i32) -> gtk4::Over
|
||||
.build();
|
||||
picture.add_css_class("file-thumbnail");
|
||||
|
||||
// Over a video's frame, so it does not pass for a photo.
|
||||
let play = gtk4::Image::builder()
|
||||
.icon_name("media-playback-start-symbolic")
|
||||
.pixel_size(16)
|
||||
.halign(gtk4::Align::Center)
|
||||
.valign(gtk4::Align::Center)
|
||||
.can_target(false)
|
||||
.visible(false)
|
||||
.build();
|
||||
play.add_css_class("photo-video-badge");
|
||||
play.add_css_class("photo-video-badge-small");
|
||||
|
||||
let overlay = gtk4::Overlay::new();
|
||||
overlay.set_width_request(size);
|
||||
overlay.set_height_request(size);
|
||||
@@ -133,9 +241,21 @@ pub(crate) fn file_thumbnail_widget(size: i32, fallback_size: i32) -> gtk4::Over
|
||||
overlay.set_overflow(gtk4::Overflow::Hidden);
|
||||
overlay.set_child(Some(&fallback));
|
||||
overlay.add_overlay(&picture);
|
||||
overlay.add_overlay(&play);
|
||||
overlay
|
||||
}
|
||||
|
||||
/// The play badge [`file_thumbnail_widget`] puts after `picture`.
|
||||
fn play_badge(picture: >k4::Picture) -> Option<gtk4::Image> {
|
||||
picture
|
||||
.next_sibling()
|
||||
.and_downcast::<gtk4::Image>()
|
||||
.filter(|image| image.has_css_class("photo-video-badge"))
|
||||
}
|
||||
|
||||
/// Marks a play badge to show once its video's frame is painted.
|
||||
const VIDEO_BADGE: &str = "video";
|
||||
|
||||
/// Resize an existing thumbnail surface. Grid rows are recycled, so changing
|
||||
/// these requests in place and rebinding the model is enough to implement the
|
||||
/// status-bar zoom without downloading or decoding an image again.
|
||||
@@ -179,6 +299,16 @@ pub(crate) fn bind_file_thumbnail(
|
||||
fallback.set_visible(true);
|
||||
picture.set_visible(false);
|
||||
picture.set_paintable(gtk4::gdk::Paintable::NONE);
|
||||
// List rows are too small for a badge; their icon says video already.
|
||||
let video = !entry.is_dir && is_video_file_name(&entry.name, Some(entry.size));
|
||||
if let Some(play) = play_badge(&picture) {
|
||||
play.set_visible(false);
|
||||
play.set_widget_name(if video && !symbolic_fallback {
|
||||
VIDEO_BADGE
|
||||
} else {
|
||||
""
|
||||
});
|
||||
}
|
||||
|
||||
// The widget name is a cheap binding token. List factories recycle their
|
||||
// children; a reply for the previous entry must not paint into the new one.
|
||||
@@ -186,13 +316,22 @@ pub(crate) fn bind_file_thumbnail(
|
||||
picture.set_widget_name(&key);
|
||||
remove_waiter(ui, &picture);
|
||||
|
||||
if entry.is_dir || !is_image_name(&entry.name) || entry.uid.is_empty() {
|
||||
if entry.is_dir
|
||||
|| !is_thumbnail_media_name(&entry.name, Some(entry.size))
|
||||
|| entry.uid.is_empty()
|
||||
{
|
||||
return;
|
||||
}
|
||||
if let Some(texture) = ui.file_thumbs.textures.borrow().get(&key) {
|
||||
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;
|
||||
}
|
||||
@@ -205,21 +344,11 @@ 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);
|
||||
}
|
||||
|
||||
/// Whether the file name denotes an image format for which Proton or the local
|
||||
/// decoder can reasonably provide a thumbnail.
|
||||
pub(crate) fn is_image_name(name: &str) -> bool {
|
||||
is_thumbnail_image_name(name)
|
||||
}
|
||||
|
||||
fn thumbnail_key(uid: &str, modified: i64) -> String {
|
||||
format!("{uid}:{modified}")
|
||||
}
|
||||
@@ -238,6 +367,9 @@ fn paint(picture: >k4::Picture, key: &str, texture: >k4::gdk::Texture) {
|
||||
}
|
||||
picture.set_paintable(Some(texture));
|
||||
picture.set_visible(true);
|
||||
if let Some(play) = play_badge(picture) {
|
||||
play.set_visible(play.widget_name() == VIDEO_BADGE);
|
||||
}
|
||||
if let Some(overlay) = picture.parent().and_downcast::<gtk4::Overlay>()
|
||||
&& let Some(fallback) = overlay.child().and_downcast::<gtk4::Image>()
|
||||
{
|
||||
@@ -353,6 +485,7 @@ fn flush_file_thumbs(ui: &Rc<Ui>) {
|
||||
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)
|
||||
{
|
||||
@@ -413,7 +546,20 @@ fn retry_file_thumbs(ui: &Rc<Ui>, items: Vec<FileThumbRequest>) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{ReplyDisposition, is_image_name, 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)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_extensions_are_case_insensitive_and_specific() {
|
||||
|
||||
@@ -10,7 +10,7 @@ use std::time::Duration;
|
||||
use adw::prelude::*;
|
||||
use gtk4::{gio, glib};
|
||||
|
||||
use super::row::{fill_slot, thumb_slot};
|
||||
use super::row::{content_type_for, fill_slot, thumb_slot};
|
||||
use super::thumbs::Thumbs;
|
||||
use crate::{Hit, dates, gettext, human_bytes, pgettext};
|
||||
|
||||
@@ -235,7 +235,7 @@ impl Preview {
|
||||
let content_type = if hit.is_dir() {
|
||||
glib::GString::from("inode/directory")
|
||||
} else {
|
||||
gio::functions::content_type_guess(Some(hit.name()), None).0
|
||||
content_type_for(hit.name(), hit.size())
|
||||
};
|
||||
self.kind
|
||||
.set_label(&gio::functions::content_type_get_description(&content_type));
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
|
||||
use std::rc::Rc;
|
||||
|
||||
use adw::prelude::*;
|
||||
use gtk4::{gdk, gio};
|
||||
use gtk4::{gdk, gio, glib};
|
||||
|
||||
use super::highlight;
|
||||
use super::thumbs::Thumbs;
|
||||
@@ -53,7 +53,7 @@ pub(crate) fn fill_slot(slot: >k4::Overlay, hit: &Hit, thumbs: &Rc<Thumbs>, mo
|
||||
let Some(picture) = fallback.next_sibling().and_downcast::<gtk4::Picture>() else {
|
||||
return;
|
||||
};
|
||||
fallback.set_from_gicon(&icon_for(hit.name(), hit.is_dir()));
|
||||
fallback.set_from_gicon(&icon_for(hit.name(), hit.is_dir(), hit.size()));
|
||||
fallback.set_visible(true);
|
||||
picture.set_visible(false);
|
||||
picture.set_paintable(gdk::Paintable::NONE);
|
||||
@@ -63,7 +63,7 @@ pub(crate) fn fill_slot(slot: >k4::Overlay, hit: &Hit, thumbs: &Rc<Thumbs>, mo
|
||||
/// The desktop's own full-colour icon for a file name, so results look like
|
||||
/// the user's file manager. Folders are Proton's purple ones while the Proton
|
||||
/// theme is on, as in the app's browser.
|
||||
pub(crate) fn icon_for(name: &str, is_dir: bool) -> gio::Icon {
|
||||
pub(crate) fn icon_for(name: &str, is_dir: bool, size: u64) -> gio::Icon {
|
||||
if is_dir {
|
||||
let folder = if proton_theme_active() {
|
||||
"pdfs-proton-folder"
|
||||
@@ -72,8 +72,21 @@ pub(crate) fn icon_for(name: &str, is_dir: bool) -> gio::Icon {
|
||||
};
|
||||
return gio::ThemedIcon::new(folder).upcast();
|
||||
}
|
||||
let (content_type, _uncertain) = gio::functions::content_type_guess(Some(name), None);
|
||||
gio::functions::content_type_get_icon(&content_type)
|
||||
gio::functions::content_type_get_icon(&content_type_for(name, size))
|
||||
}
|
||||
|
||||
/// The content type of a file named `name` of `size` bytes, guessed from the
|
||||
/// name. GIO takes every `.ts` for TypeScript (or Qt translations); a
|
||||
/// recording's size says otherwise (see
|
||||
/// [`is_video_file_name`](pdfs_core::control::is_video_file_name)).
|
||||
pub(crate) fn content_type_for(name: &str, size: u64) -> glib::GString {
|
||||
let transport_stream = std::path::Path::new(name)
|
||||
.extension()
|
||||
.is_some_and(|extension| extension.eq_ignore_ascii_case("ts"));
|
||||
if transport_stream && pdfs_core::control::is_video_file_name(name, Some(size)) {
|
||||
return glib::GString::from("video/mp2t");
|
||||
}
|
||||
gio::functions::content_type_guess(Some(name), None).0
|
||||
}
|
||||
|
||||
/// The sync badge of a Drive file, as in the app's browser: pinned, cached,
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
//! matches the file's;
|
||||
//! 2. for local images, decoding the file itself, and writing the result back
|
||||
//! to that cache for the next time and for every other application;
|
||||
//! 3. for Drive images, the daemon's `FileThumbs`, batched after a short quiet
|
||||
//! period.
|
||||
//! 3. for Drive images and videos, the daemon's `FileThumbs`, batched after a
|
||||
//! short quiet period.
|
||||
//!
|
||||
//! Workers hand back `gdk::Texture`s, never a `Pixbuf`, which may not cross
|
||||
//! threads. Each job carries the render epoch it was queued in, and workers
|
||||
@@ -30,7 +30,9 @@ use gtk4::gdk_pixbuf::Pixbuf;
|
||||
use gtk4::prelude::*;
|
||||
use gtk4::{gdk, glib};
|
||||
|
||||
use pdfs_core::control::{FileThumbRequest, Request, Response, is_thumbnail_image_name};
|
||||
use pdfs_core::control::{
|
||||
FileThumbRequest, Request, Response, is_thumbnail_image_name, is_thumbnail_media_name,
|
||||
};
|
||||
|
||||
use crate::{Hit, spawn_request};
|
||||
|
||||
@@ -165,7 +167,13 @@ impl Thumbs {
|
||||
// The widget name binds the picture to one key: a late reply for
|
||||
// another hit must not paint into it.
|
||||
picture.set_widget_name(&key);
|
||||
if hit.is_dir() || !is_thumbnail_image_name(hit.name()) {
|
||||
// A video's frame comes from the daemon, which has ffmpeg's help; a
|
||||
// local video has only what another application cached.
|
||||
let thumbnailed = match hit {
|
||||
Hit::Drive(drive) => is_thumbnail_media_name(&drive.name, Some(drive.size)),
|
||||
Hit::Local(_) => is_thumbnail_image_name(hit.name()),
|
||||
};
|
||||
if hit.is_dir() || !thumbnailed {
|
||||
return;
|
||||
}
|
||||
if matches!(hit, Hit::Drive(drive) if drive.uid.is_empty()) {
|
||||
|
||||
@@ -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<String>, build: Option<&str>, exe: Option<PathBuf>) -> 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!(
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -421,6 +419,10 @@ impl Tray for DriveTray {
|
||||
self.state.phase.icon().into()
|
||||
}
|
||||
|
||||
fn attention_icon_name(&self) -> String {
|
||||
self.icon_name()
|
||||
}
|
||||
|
||||
fn status(&self) -> Status {
|
||||
match self.state.phase {
|
||||
Phase::Attention | Phase::SessionExpired => Status::NeedsAttention,
|
||||
@@ -674,6 +676,22 @@ mod tests {
|
||||
assert_eq!(phase_of(false, 0, true, false), Phase::Synced);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attention_states_supply_the_icon_the_tray_requests() {
|
||||
let mut tray = DriveTray {
|
||||
state: status_state(&status(""), &[], &[], Path::new("/m")),
|
||||
socket: PathBuf::new(),
|
||||
};
|
||||
for (phase, icon) in [
|
||||
(Phase::Attention, "dialog-warning-symbolic"),
|
||||
(Phase::SessionExpired, "dialog-password-symbolic"),
|
||||
] {
|
||||
tray.state.phase = phase;
|
||||
assert_eq!(tray.status(), Status::NeedsAttention);
|
||||
assert_eq!(tray.attention_icon_name(), icon);
|
||||
}
|
||||
}
|
||||
|
||||
fn status(extra: &str) -> Response {
|
||||
status_from(env!("CARGO_PKG_VERSION"), extra)
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ pub(crate) fn icon_name(hit: &Hit) -> String {
|
||||
let name = hit.name();
|
||||
if is_image(name) {
|
||||
"image-x-generic"
|
||||
} else if is_media(name) {
|
||||
} else if is_media(name) || pdfs_core::control::is_video_file_name(name, Some(hit.size())) {
|
||||
"video-x-generic"
|
||||
} else if is_document(name) {
|
||||
"text-x-generic"
|
||||
|
||||
@@ -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",
|
||||
@@ -52,6 +54,7 @@ const STOCK: &[&str] = &[
|
||||
"image-missing-symbolic",
|
||||
"image-x-generic-symbolic",
|
||||
"list-add-symbolic",
|
||||
"list-remove-symbolic",
|
||||
"mark-location-symbolic",
|
||||
"media-playback-pause-symbolic",
|
||||
"media-playback-start-symbolic",
|
||||
@@ -61,16 +64,20 @@ const STOCK: &[&str] = &[
|
||||
"object-select-symbolic",
|
||||
"preferences-system-symbolic",
|
||||
"process-stop-symbolic",
|
||||
"selection-mode-symbolic",
|
||||
"starred-symbolic",
|
||||
"system-search-symbolic",
|
||||
"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",
|
||||
|
||||
@@ -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` |
|
||||
|
||||
Loaded 100 of 160 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user